r back to after the callback runs.
*
* Falls back to the integrations page when no return URL is stored
* (stale bookmark, no pending flow).
*
* @param int $user_id The WordPress user ID.
*
* @return string The return URL.
*/
private function resolve_return_url( int $user_id ): string {
$fallback = \admin_url( 'admin.php?page=' . General_Page_Integration::PAGE );
if ( $user_id > 0 ) {
$stored = $this->auth_code_handler->get_return_url( $user_id );
if ( \is_string( $stored ) && $stored !== '' ) {
// Defense in depth: the stored URL is only ever written as an
// admin_url by the management route, but validate it against the
// site's own host before redirecting so a tampered store entry
// can't become an open redirect.
return \wp_validate_redirect( $stored, $fallback );
}
}
return $fallback;
}
/**
* Reads a query argument, returning an empty string when missing.
*
* @param string $name The query argument name.
*
* @return string The sanitized value.
*/
private function read_query_arg( string $name ): string {
// phpcs:disable WordPress.Security.NonceVerification.Recommended -- CSRF defense is OAuth `state` validated inside exchange_code.
if ( ! isset( $_GET[ $name ] ) || ! \is_string( $_GET[ $name ] ) ) {
return '';
}
return \sanitize_text_field( \wp_unslash( $_GET[ $name ] ) );
// phpcs:enable WordPress.Security.NonceVerification.Recommended
}
}
**
* Displays the page.
*
* @return void
*/
public function display_page() {
echo '
';
}
/**
* Enqueues the assets.
*
* @return void
*/
public function enqueue_assets() {
// Remove the emoji script as it is incompatible with both React and any contenteditable fields.
\remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );
$this->asset_manager->enqueue_script( self::ASSETS_NAME );
$this->asset_manager->enqueue_style( self::ASSETS_NAME );
$this->asset_manager->localize_script( self::ASSETS_NAME, 'wpseoScriptData', $this->get_script_data() );
}
/**
* Creates the script data.
*
* @return array>> The script data.
*/
private function get_script_data(): array {
return [
'addOns' => $this->add_ons_collector->to_array(),
'linkParams' => $this->short_link_helper->get_query_params(),
'preferences' => [
'isRtl' => \is_rtl(),
],
'currentPromotions' => $this->promotion_manager->get_current_promotions(),
'duplicatePost' => $this->duplicate_post_manager->get_params(),
'userCan' => [
'installPlugin' => \current_user_can( 'install_plugins' ),
'activatePlugin' => \current_user_can( 'activate_plugins' ),
],
];
}
/**
* Removes all current WP notices.
*
* @return void
*/
public function remove_notices() {
\remove_all_actions( 'admin_notices' );
\remove_all_actions( 'user_admin_notices' );
\remove_all_actions( 'network_admin_notices' );
\remove_all_actions( 'all_admin_notices' );
}
}
>',
'edit_posts',
self::PAGE,
static function () {
echo 'redirecting...';
},
];
return $pages;
}
/**
* Redirects to the yoast.com.
*
* @return void
*/
public function do_redirect(): void {
if ( $this->current_page_helper->get_current_yoast_seo_page() !== self::PAGE ) {
return;
}
$link = $this->shortlinker->build_shortlink( 'https://yoa.st/wordpress-menu-upgrade-premium' );
if ( $this->woocommerce_conditional->is_met() ) {
$link = $this->shortlinker->build_shortlink( 'https://yoa.st/wordpress-menu-upgrade-woocommerce' );
}
\wp_redirect( $link );//phpcs:ignore WordPress.Security.SafeRedirect.wp_redirect_wp_redirect -- Safe redirect is used here.
exit();
}
}
! $query->is_main_query() || ! $query->get( self::QUERY_VAR ) ) {
return;
}
/*
* Without control over the headers the response cannot be typed as XML, and echoing the
* document into a half-written HTML body would produce neither a page nor a schema map.
*/
if ( $this->headers_already_sent() ) {
return;
}
/*
* Build the document before committing any headers. Generating it hits the database and can
* throw, and a failure after the 200 and the cache headers were sent would let a proxy store
* an HTML error page at this path for the full max-age.
*/
$xml = $this->schema_map_xml_provider->get_xml();
$this->send_headers();
// DOMDocument::createTextNode() already escaped every value; escaping again would corrupt the XML.
echo $xml; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Raw XML.
$this->finish_request();
}
/**
* Sends the response headers for the schema map.
*
* These replace what `WP::send_headers()` already sent: the HTML content type, and the `Expires`
* and `Cache-Control` pair that `wp_get_nocache_headers()` adds for logged-in users.
*
* @return void
*/
protected function send_headers() {
\status_header( 200 );
// Prevent the search engines from indexing the schema map.
$this->redirect_helper->set_header( 'X-Robots-Tag: noindex, follow' );
$this->redirect_helper->set_header( 'Content-Type: application/xml; charset=UTF-8' );
$this->redirect_helper->set_header( 'Cache-Control: public, max-age=300' );
// The response is public, so the no-cache Expires date from wp_get_nocache_headers() must go.
$this->redirect_helper->remove_header( 'Expires' );
}
/**
* Tells whether the response headers have already been sent.
*
* Wrapped in an overridable method so the unit tests can drive both branches of
* maybe_render_schema_map(); the test process cannot control what headers_sent() reports.
* Protected rather than private because Mockery cannot stub private methods.
*
* @codeCoverageIgnore It only wraps a PHP function.
*
* @return bool Whether the headers have already been sent.
*/
protected function headers_already_sent(): bool {
return \headers_sent();
}
/**
* Ends the request after the schema map has been output.
*
* The exit() is wrapped in an overridable method so the unit tests can stub it out; executing it
* would terminate the test runner. Protected rather than private because Mockery cannot stub
* private methods.
*
* @codeCoverageIgnore It only wraps a language construct that terminates the request.
*
* @return void
*/
protected function finish_request() {
exit();
}
}