# jetpack/16.3/jetpack_vendor/automattic/jetpack-seo/src/class-admin-page.php

Jetpack – WP Security, Backup, Speed, &amp; Growth, version 16.3. 297 lines.

- Page: https://pluginprobe.com/plugins/jetpack/16.3/code/jetpack_vendor/automattic/jetpack-seo/src/class-admin-page.php
- Raw: https://pluginprobe.com/plugins/jetpack/16.3/raw/jetpack_vendor/automattic/jetpack-seo/src/class-admin-page.php
- Modified: 2026-09-29T02:50:08+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/jetpack/16.3/code/jetpack_vendor/automattic/jetpack-seo/src/class-admin-page.php#L10-L20`.

```php
<?php
/**
 * The Jetpack SEO wp-admin page shell.
 *
 * Registers the `admin.php?page=jetpack-seo` screen via Admin_Menu so it is
 * reachable on self-hosted, Atomic/WoA, and Simple sites alike, loads the
 * `@wordpress/build` (wp-build) dashboard bundle that renders it, and
 * bootstraps the React app's initial state onto the page.
 *
 * @package automattic/jetpack-seo-package
 */

namespace Automattic\Jetpack\SEO;

use Automattic\Jetpack\Admin_UI\Admin_Menu;
use Automattic\Jetpack\WP_Build_Polyfills\WP_Build_Polyfills;
use Automattic\Jetpack\WP_Build_Polyfills\WP_Build_Screen_Id;

/**
 * Registers the SEO admin menu and loads the wp-build dashboard bundle.
 */
class Admin_Page {

	/**
	 * URL-facing menu slug (`admin.php?page=jetpack-seo`).
	 */
	const MENU_SLUG = 'jetpack-seo';

	/**
	 * Slug emitted by `@wordpress/build` (`wpPlugin.pages[0]`). wp-build's
	 * auto-generated enqueue callback only fires when `$screen->id` matches
	 * this value, so we alias the screen id to it around that check without
	 * changing the user-facing URL.
	 */
	const WP_BUILD_SLUG = 'jetpack-seo-dashboard';

	/**
	 * Render function generated by `@wordpress/build` into
	 * `build/pages/jetpack-seo-dashboard/page-wp-admin.php`. Naming convention:
	 * `{wpPlugin.name}_{page-with-underscores}_wp_admin_render_page`.
	 */
	const WP_BUILD_RENDER_FN = 'jetpack_seo_jetpack_seo_dashboard_wp_admin_render_page';

	/**
	 * The screen ID alias_screen_id_for_wp_build() replaced, until it is restored.
	 *
	 * @var string|null
	 */
	private static $wp_build_original_screen_id = null;

	/**
	 * The dashboard screen hide_jitms_on_wp_build_dashboard() opts out of JITMs.
	 *
	 * @var string|null
	 */
	private static $jitm_opt_out_screen_id = null;

	/**
	 * Register the admin menu item.
	 *
	 * Uses Admin_Menu so the page is reachable on wp-admin across all site
	 * types. The render callback is wp-build's generated render function when
	 * the bundle is loaded (i.e. on the SEO page itself, after
	 * `maybe_load_wp_build()` ran at priority 1); otherwise it falls back to a
	 * bare mount node so the page never fatals on an unbuilt checkout.
	 *
	 * @return void
	 */
	public static function add_menu_item() {
		$callback = function_exists( self::WP_BUILD_RENDER_FN )
			? self::WP_BUILD_RENDER_FN
			: array( __CLASS__, 'render_fallback' );

		$page_suffix = Admin_Menu::add_menu(
			'SEO',
			'SEO',
			'manage_options',
			self::MENU_SLUG,
			$callback,
			null,
			// SEO has no My Jetpack product class, so the module is the only gate available.
			array(
				'module' => 'seo-tools',
				'key'    => 'jetpack-seo',
			)
		);

		if ( $page_suffix ) {
			self::opt_out_of_jitms( $page_suffix );
		}
	}

	/**
	 * On the SEO admin page, load the wp-build bundle, alias the screen id so
	 * wp-build enqueues its assets, and bootstrap the app's initial state.
	 *
	 * Hooked at `admin_menu` priority 1 so polyfills register and the render
	 * function is defined before `add_menu_item()` runs at priority 10.
	 *
	 * @return void
	 */
	public static function maybe_load_wp_build() {
		if ( ! self::is_seo_admin_request() ) {
			return;
		}

		self::load_wp_build_with_screen_alias();
		add_filter( 'jetpack_admin_js_script_data', array( __CLASS__, 'inject_script_data' ) );
	}

	/**
	 * Load wp-build with the screen ID aliased across its generated enqueue check.
	 *
	 * @see WP_Build_Screen_Id::load_with_alias()
	 * @return void
	 */
	private static function load_wp_build_with_screen_alias() {
		// Fallback: an older wp-build-polyfills under the jetpack-autoloader may predate load_with_alias().
		if ( method_exists( WP_Build_Screen_Id::class, 'load_with_alias' ) ) {
			WP_Build_Screen_Id::load_with_alias(
				array( __CLASS__, 'alias_screen_id_for_wp_build' ),
				array( __CLASS__, 'restore_screen_id_after_wp_build' ),
				function () {
					self::load_wp_build();
				}
			);
			return;
		}

		add_action( 'admin_enqueue_scripts', array( __CLASS__, 'alias_screen_id_for_wp_build' ) );
		self::load_wp_build();
		add_action( 'admin_enqueue_scripts', array( __CLASS__, 'restore_screen_id_after_wp_build' ) );
	}

	/**
	 * Load wp-build's generated registration file and register the polyfills
	 * the bundle depends on. No-op on a fresh checkout before `pnpm build`, in
	 * which case `add_menu_item()` falls back to {@see self::render_fallback()}.
	 *
	 * @return void
	 */
	private static function load_wp_build() {
		$build_index = dirname( __DIR__ ) . '/build/build.php';

		if ( ! file_exists( $build_index ) ) {
			return;
		}

		require_once $build_index;

		WP_Build_Polyfills::register(
			'jetpack-seo',
			array_merge( WP_Build_Polyfills::SCRIPT_HANDLES, WP_Build_Polyfills::MODULE_IDS )
		);
	}

	/**
	 * Alias the current screen id to wp-build's expected slug so its
	 * auto-generated enqueue callback fires for our user-facing page.
	 *
	 * @since 0.9.5 Takes no argument; hooked on `admin_enqueue_scripts`.
	 *
	 * @return void
	 */
	public static function alias_screen_id_for_wp_build() {
		$screen = get_current_screen();
		if ( ! $screen ) {
			return;
		}

		self::$wp_build_original_screen_id = $screen->id;
		$screen->id                        = self::WP_BUILD_SLUG;
	}

	/**
	 * Undo alias_screen_id_for_wp_build(), so code after the generated check sees the real screen ID.
	 *
	 * @since 0.9.5
	 *
	 * @return void
	 */
	public static function restore_screen_id_after_wp_build() {
		$screen = get_current_screen();
		if ( ! $screen || null === self::$wp_build_original_screen_id ) {
			return;
		}

		$screen->id                        = self::$wp_build_original_screen_id;
		self::$wp_build_original_screen_id = null;
	}

	/**
	 * Opt the dashboard's screen out of JITMs.
	 *
	 * @param string $screen_id The hook suffix the page was registered under, which is its screen ID.
	 * @return void
	 */
	private static function opt_out_of_jitms( $screen_id ) {
		self::$jitm_opt_out_screen_id = $screen_id;
		add_filter( 'jetpack_display_jitms_on_screen', array( __CLASS__, 'hide_jitms_on_wp_build_dashboard' ), 10, 2 );
	}

	/**
	 * Keep JITMs off the wp-build dashboard, which has no `#jp-admin-notices` to show them in.
	 *
	 * Fetching a JITM records a view, so one the page hides would still be counted.
	 *
	 * @since 0.9.5
	 *
	 * @param bool   $show      Whether to show JITMs on the screen.
	 * @param string $screen_id The screen ID.
	 * @return bool
	 */
	public static function hide_jitms_on_wp_build_dashboard( $show, $screen_id ) {
		if ( null !== self::$jitm_opt_out_screen_id && self::$jitm_opt_out_screen_id === $screen_id ) {
			return false;
		}

		return $show;
	}

	/**
	 * Bootstrap the React app's initial state onto `window.JetpackScriptData.seo`.
	 *
	 * Because wp-build pages load as ES modules, `wp_localize_script` can't
	 * attach data to them; the shared `jetpack_admin_js_script_data` filter
	 * (printed by the Script_Data package onto the `jetpack-script-data` handle
	 * the bundle already depends on) is the supported channel. The per-tab state
	 * is provided as an apiFetch *preload* (mirrors Podcast) so the app resolves
	 * it with no request on a normal load yet can re-fetch when the preload is
	 * missing or stale, rather than dead-ending on a one-shot read.
	 *
	 * @param array $data Script data being injected onto the page.
	 * @return array
	 */
	public static function inject_script_data( $data ) {
		if ( ! is_array( $data ) ) {
			$data = array();
		}

		// Preload the dashboard's REST reads into the page so the app resolves them from
		// cache on first paint with no network request — while still being able to
		// re-fetch if that preload is ever missing or stale. This replaces injecting the
		// raw payloads, which the app read synchronously once and couldn't recover from
		// when momentarily absent (the load-error dead-end). See Dashboard_Data::register_rest_reads()
		// and the client readers `_inc/data/get-preloaded.ts` + `_inc/data/use-ensure-tab-data.ts`.
		$data[ Initializer::SCRIPT_DATA_KEY ]['preload'] = array_reduce(
			Dashboard_Data::rest_read_paths(),
			'rest_preload_api_request',
			array()
		);

		// Small synchronous reads used outside the per-tab data stores, and not part of
		// the load-error path.
		$data[ Initializer::SCRIPT_DATA_KEY ]['google_verify'] = Dashboard_Data::get_google_verify_data();
		$data[ Initializer::SCRIPT_DATA_KEY ]['site']          = Dashboard_Data::get_site_data();

		// Plan-gating signal for below-Premium WordPress.com sites: when gated, the
		// dashboard reduces to a free subset and surfaces the upsell banner. Self-hosted
		// is never gated (see Initializer::is_gated()). The upsell URL is only meaningful
		// when gated, so it's built only then — every ungated and self-hosted admin load
		// otherwise pays for a site-suffix lookup it never uses.
		$is_gated                                       = Initializer::is_gated();
		$data[ Initializer::SCRIPT_DATA_KEY ]['gating'] = array(
			'is_gated'   => $is_gated,
			'upsell_url' => $is_gated ? Initializer::get_upsell_url() : '',
		);

		return $data;
	}

	/**
	 * Fallback render used when the wp-build artifact is missing (unbuilt
	 * checkout). Renders a bare wrapper so the page loads without the app.
	 *
	 * @return void
	 */
	public static function render_fallback() {
		echo '<div class="wrap"><h1>SEO</h1></div>';
	}

	/**
	 * Whether the current request targets the SEO admin page.
	 *
	 * @return bool
	 */
	private static function is_seo_admin_request() {
		// phpcs:ignore WordPress.Security.NonceVerification.Recommended
		if ( ! is_admin() || ! isset( $_GET['page'] ) ) {
			return false;
		}

		// phpcs:ignore WordPress.Security.NonceVerification.Recommended
		return self::MENU_SLUG === sanitize_text_field( wp_unslash( $_GET['page'] ) );
	}
}

```
