PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3-a.3
Jetpack – WP Security, Backup, Speed, & Growth v16.3-a.3
16.3-beta 16.3-a.5 16.3-a.7 16.3-a.3 16.3-a.1 16.2 16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 All 507 releases
jetpack / jetpack_vendor / automattic / jetpack-seo / src / class-admin-page.php

class-admin-page.php in Jetpack – WP Security, Backup, Speed, & Growth 16.3-a.3, at jetpack_vendor/automattic/jetpack-seo/src/class-admin-page.php

276 lines 9.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * The Jetpack SEO wp-admin page shell.
4 *
5 * Registers the `admin.php?page=jetpack-seo` screen via Admin_Menu so it is
6 * reachable on self-hosted, Atomic/WoA, and Simple sites alike, loads the
7 * `@wordpress/build` (wp-build) dashboard bundle that renders it, and
8 * bootstraps the React app's initial state onto the page.
9 *
10 * @package automattic/jetpack-seo-package
11 */
12
13 namespace Automattic\Jetpack\SEO;
14
15 use Automattic\Jetpack\Admin_UI\Admin_Menu;
16 use Automattic\Jetpack\WP_Build_Polyfills\WP_Build_Polyfills;
17
18 /**
19 * Registers the SEO admin menu and loads the wp-build dashboard bundle.
20 */
21 class Admin_Page {
22
23 /**
24 * URL-facing menu slug (`admin.php?page=jetpack-seo`).
25 */
26 const MENU_SLUG = 'jetpack-seo';
27
28 /**
29 * Slug emitted by `@wordpress/build` (`wpPlugin.pages[0]`). wp-build's
30 * auto-generated enqueue callback only fires when `$screen->id` matches
31 * this value, so we alias the screen id to it around that check without
32 * changing the user-facing URL.
33 */
34 const WP_BUILD_SLUG = 'jetpack-seo-dashboard';
35
36 /**
37 * Render function generated by `@wordpress/build` into
38 * `build/pages/jetpack-seo-dashboard/page-wp-admin.php`. Naming convention:
39 * `{wpPlugin.name}_{page-with-underscores}_wp_admin_render_page`.
40 */
41 const WP_BUILD_RENDER_FN = 'jetpack_seo_jetpack_seo_dashboard_wp_admin_render_page';
42
43 /**
44 * The screen ID alias_screen_id_for_wp_build() replaced, until it is restored.
45 *
46 * @var string|null
47 */
48 private static $wp_build_original_screen_id = null;
49
50 /**
51 * The dashboard screen hide_jitms_on_wp_build_dashboard() opts out of JITMs.
52 *
53 * @var string|null
54 */
55 private static $jitm_opt_out_screen_id = null;
56
57 /**
58 * Register the admin menu item.
59 *
60 * Uses Admin_Menu so the page is reachable on wp-admin across all site
61 * types. The render callback is wp-build's generated render function when
62 * the bundle is loaded (i.e. on the SEO page itself, after
63 * `maybe_load_wp_build()` ran at priority 1); otherwise it falls back to a
64 * bare mount node so the page never fatals on an unbuilt checkout.
65 *
66 * @return void
67 */
68 public static function add_menu_item() {
69 $callback = function_exists( self::WP_BUILD_RENDER_FN )
70 ? self::WP_BUILD_RENDER_FN
71 : array( __CLASS__, 'render_fallback' );
72
73 $page_suffix = Admin_Menu::add_menu(
74 'SEO',
75 'SEO',
76 'manage_options',
77 self::MENU_SLUG,
78 $callback,
79 null,
80 // SEO has no My Jetpack product class, so the module is the only gate available.
81 array(
82 'module' => 'seo-tools',
83 'key' => 'jetpack-seo',
84 )
85 );
86
87 if ( $page_suffix ) {
88 self::opt_out_of_jitms( $page_suffix );
89 }
90 }
91
92 /**
93 * On the SEO admin page, load the wp-build bundle, alias the screen id so
94 * wp-build enqueues its assets, and bootstrap the app's initial state.
95 *
96 * Hooked at `admin_menu` priority 1 so polyfills register and the render
97 * function is defined before `add_menu_item()` runs at priority 10.
98 *
99 * @return void
100 */
101 public static function maybe_load_wp_build() {
102 if ( ! self::is_seo_admin_request() ) {
103 return;
104 }
105
106 // Hooked either side of load_wp_build(), so the alias holds only for the generated
107 // enqueue check it registers at the same priority.
108 add_action( 'admin_enqueue_scripts', array( __CLASS__, 'alias_screen_id_for_wp_build' ) );
109 self::load_wp_build();
110 add_action( 'admin_enqueue_scripts', array( __CLASS__, 'restore_screen_id_after_wp_build' ) );
111 add_filter( 'jetpack_admin_js_script_data', array( __CLASS__, 'inject_script_data' ) );
112 }
113
114 /**
115 * Load wp-build's generated registration file and register the polyfills
116 * the bundle depends on. No-op on a fresh checkout before `pnpm build`, in
117 * which case `add_menu_item()` falls back to {@see self::render_fallback()}.
118 *
119 * @return void
120 */
121 private static function load_wp_build() {
122 $build_index = dirname( __DIR__ ) . '/build/build.php';
123
124 if ( ! file_exists( $build_index ) ) {
125 return;
126 }
127
128 require_once $build_index;
129
130 WP_Build_Polyfills::register(
131 'jetpack-seo',
132 array_merge( WP_Build_Polyfills::SCRIPT_HANDLES, WP_Build_Polyfills::MODULE_IDS )
133 );
134 }
135
136 /**
137 * Alias the current screen id to wp-build's expected slug so its
138 * auto-generated enqueue callback fires for our user-facing page.
139 *
140 * @since 0.9.5 Takes no argument; hooked on `admin_enqueue_scripts`.
141 *
142 * @return void
143 */
144 public static function alias_screen_id_for_wp_build() {
145 $screen = get_current_screen();
146 if ( ! $screen ) {
147 return;
148 }
149
150 self::$wp_build_original_screen_id = $screen->id;
151 $screen->id = self::WP_BUILD_SLUG;
152 }
153
154 /**
155 * Undo alias_screen_id_for_wp_build(), so code after the generated check sees the real screen ID.
156 *
157 * @since 0.9.5
158 *
159 * @return void
160 */
161 public static function restore_screen_id_after_wp_build() {
162 $screen = get_current_screen();
163 if ( ! $screen || null === self::$wp_build_original_screen_id ) {
164 return;
165 }
166
167 $screen->id = self::$wp_build_original_screen_id;
168 self::$wp_build_original_screen_id = null;
169 }
170
171 /**
172 * Opt the dashboard's screen out of JITMs.
173 *
174 * @param string $screen_id The hook suffix the page was registered under, which is its screen ID.
175 * @return void
176 */
177 private static function opt_out_of_jitms( $screen_id ) {
178 self::$jitm_opt_out_screen_id = $screen_id;
179 add_filter( 'jetpack_display_jitms_on_screen', array( __CLASS__, 'hide_jitms_on_wp_build_dashboard' ), 10, 2 );
180 }
181
182 /**
183 * Keep JITMs off the wp-build dashboard, which has no `#jp-admin-notices` to show them in.
184 *
185 * Fetching a JITM records a view, so one the page hides would still be counted.
186 *
187 * @since 0.9.5
188 *
189 * @param bool $show Whether to show JITMs on the screen.
190 * @param string $screen_id The screen ID.
191 * @return bool
192 */
193 public static function hide_jitms_on_wp_build_dashboard( $show, $screen_id ) {
194 if ( null !== self::$jitm_opt_out_screen_id && self::$jitm_opt_out_screen_id === $screen_id ) {
195 return false;
196 }
197
198 return $show;
199 }
200
201 /**
202 * Bootstrap the React app's initial state onto `window.JetpackScriptData.seo`.
203 *
204 * Because wp-build pages load as ES modules, `wp_localize_script` can't
205 * attach data to them; the shared `jetpack_admin_js_script_data` filter
206 * (printed by the Script_Data package onto the `jetpack-script-data` handle
207 * the bundle already depends on) is the supported channel. The per-tab state
208 * is provided as an apiFetch *preload* (mirrors Podcast) so the app resolves
209 * it with no request on a normal load yet can re-fetch when the preload is
210 * missing or stale, rather than dead-ending on a one-shot read.
211 *
212 * @param array $data Script data being injected onto the page.
213 * @return array
214 */
215 public static function inject_script_data( $data ) {
216 if ( ! is_array( $data ) ) {
217 $data = array();
218 }
219
220 // Preload the dashboard's REST reads into the page so the app resolves them from
221 // cache on first paint with no network request — while still being able to
222 // re-fetch if that preload is ever missing or stale. This replaces injecting the
223 // raw payloads, which the app read synchronously once and couldn't recover from
224 // when momentarily absent (the load-error dead-end). See Dashboard_Data::register_rest_reads()
225 // and the client readers `_inc/data/get-preloaded.ts` + `_inc/data/use-ensure-tab-data.ts`.
226 $data[ Initializer::SCRIPT_DATA_KEY ]['preload'] = array_reduce(
227 Dashboard_Data::rest_read_paths(),
228 'rest_preload_api_request',
229 array()
230 );
231
232 // Small synchronous reads used outside the per-tab data stores, and not part of
233 // the load-error path.
234 $data[ Initializer::SCRIPT_DATA_KEY ]['google_verify'] = Dashboard_Data::get_google_verify_data();
235 $data[ Initializer::SCRIPT_DATA_KEY ]['site'] = Dashboard_Data::get_site_data();
236
237 // Plan-gating signal for below-Premium WordPress.com sites: when gated, the
238 // dashboard reduces to a free subset and surfaces the upsell banner. Self-hosted
239 // is never gated (see Initializer::is_gated()). The upsell URL is only meaningful
240 // when gated, so it's built only then — every ungated and self-hosted admin load
241 // otherwise pays for a site-suffix lookup it never uses.
242 $is_gated = Initializer::is_gated();
243 $data[ Initializer::SCRIPT_DATA_KEY ]['gating'] = array(
244 'is_gated' => $is_gated,
245 'upsell_url' => $is_gated ? Initializer::get_upsell_url() : '',
246 );
247
248 return $data;
249 }
250
251 /**
252 * Fallback render used when the wp-build artifact is missing (unbuilt
253 * checkout). Renders a bare wrapper so the page loads without the app.
254 *
255 * @return void
256 */
257 public static function render_fallback() {
258 echo '<div class="wrap"><h1>SEO</h1></div>';
259 }
260
261 /**
262 * Whether the current request targets the SEO admin page.
263 *
264 * @return bool
265 */
266 private static function is_seo_admin_request() {
267 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
268 if ( ! is_admin() || ! isset( $_GET['page'] ) ) {
269 return false;
270 }
271
272 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
273 return self::MENU_SLUG === sanitize_text_field( wp_unslash( $_GET['page'] ) );
274 }
275 }
276