sion, 'pro' => aioseo()->pro ] ); } /** * Validate the trust token and tells the microservice that we can reauthenticate. * * @since 4.3.0 * * @return void */ public function reauthenticate() { foreach ( [ 'key', 'token', 'tt' ] as $arg ) { if ( empty( $_REQUEST[ $arg ] ) ) { wp_send_json_error( [ 'error' => 'authenticate_missing_arg', 'message' => 'Authentication request missing parameter: ' . $arg, 'version' => aioseo()->version, 'pro' => aioseo()->pro ] ); } } $trustToken = ! empty( $_REQUEST['tt'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['tt'] ) ) : ''; if ( ! aioseo()->searchStatistics->api->trustToken->validate( $trustToken ) ) { wp_send_json_error( [ 'error' => 'authenticate_invalid_tt', 'message' => 'Invalid TT sent', 'version' => aioseo()->version, 'pro' => aioseo()->pro ] ); } // If the trust token is validated, send a success response to trigger the regular auth process. wp_send_json_success(); } /** * Saves the authenticated account, clear the existing data and redirect back to the settings page. * * @since 4.3.0 * * @return void */ private function saveAndRedirect( $profile ) { // Reset the search statistics data. aioseo()->searchStatistics->reset(); // Save the authenticated profile. aioseo()->searchStatistics->api->auth->setProfile( $profile ); // Reset dismissed alerts. $dismissedAlerts = aioseo()->settings->dismissedAlerts; foreach ( $dismissedAlerts as $key => $alert ) { if ( in_array( $key, [ 'searchConsoleNotConnected', 'searchConsoleSitemapErrors' ], true ) ) { $dismissedAlerts[ $key ] = false; } } aioseo()->settings->dismissedAlerts = $dismissedAlerts; // Maybe verifies the site. aioseo()->searchStatistics->site->maybeVerify(); // Redirects to the original page. wp_safe_redirect( $this->getRedirectUrl() ); exit; } /** * Returns the authenticated domain. * * @since 4.3.0 * * @return string The authenticated domain. */ private function getAuthenticatedDomain() { if ( empty( $_REQUEST['authedsite'] ) ) { return ''; } $authedSite = sanitize_text_field( wp_unslash( $_REQUEST['authedsite'] ) ); if ( false !== aioseo()->helpers->stringIndex( $authedSite, 'sc-domain:' ) ) { $authedSite = str_replace( 'sc-domain:', '', $authedSite ); } return $authedSite; } /** * Gets the redirect URL. * * @since 4.6.2 * * @return string The redirect URL. */ private function getRedirectUrl() { $returnTo = ! empty( $_REQUEST['return-to'] ) ? sanitize_key( $_REQUEST['return-to'] ) : ''; $redirectUrl = 'admin.php?page=aioseo'; switch ( $returnTo ) { case 'webmaster-tools': $redirectUrl = 'admin.php?page=aioseo-settings#/webmaster-tools?activetool=googleSearchConsole'; break; case 'setup-wizard': $redirectUrl = 'index.php?page=aioseo-setup-wizard#/' . aioseo()->standalone->setupWizard->getNextStage(); break; case 'search-statistics': $redirectUrl = 'admin.php?page=aioseo-search-statistics/#search-statistics'; break; } return admin_url( $redirectUrl ); } }