PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3-beta
Jetpack – WP Security, Backup, Speed, & Growth v16.3-beta
16.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 All 508 releases
← All changes | jetpack_vendor/automattic/jetpack-seo/src/class-admin-page.php +107 -9 16.2 → 16.3-beta View file →
@@ -13,8 +13,9 @@
13 13 namespace Automattic\Jetpack\SEO;
14 14
15 15 use Automattic\Jetpack\Admin_UI\Admin_Menu;
16 16 use Automattic\Jetpack\WP_Build_Polyfills\WP_Build_Polyfills;
17 +use Automattic\Jetpack\WP_Build_Polyfills\WP_Build_Screen_Id;
17 18
18 19 /**
19 20 * Registers the SEO admin menu and loads the wp-build dashboard bundle.
20 21 */
@@ -27,9 +28,9 @@
27 28
28 29 /**
29 30 * Slug emitted by `@wordpress/build` (`wpPlugin.pages[0]`). wp-build's
30 31 * auto-generated enqueue callback only fires when `$screen->id` matches
31 - * this value, so we alias the screen id to it via `current_screen` without
32 + * this value, so we alias the screen id to it around that check without
32 33 * changing the user-facing URL.
33 34 */
34 35 const WP_BUILD_SLUG = 'jetpack-seo-dashboard';
35 36
@@ -40,8 +41,22 @@
40 41 */
41 42 const WP_BUILD_RENDER_FN = 'jetpack_seo_jetpack_seo_dashboard_wp_admin_render_page';
42 43
43 44 /**
45 + * The screen ID alias_screen_id_for_wp_build() replaced, until it is restored.
46 + *
47 + * @var string|null
48 + */
49 + private static $wp_build_original_screen_id = null;
50 +
51 + /**
52 + * The dashboard screen hide_jitms_on_wp_build_dashboard() opts out of JITMs.
53 + *
54 + * @var string|null
55 + */
56 + private static $jitm_opt_out_screen_id = null;
57 +
58 + /**
44 59 * Register the admin menu item.
45 60 *
46 61 * Uses Admin_Menu so the page is reachable on wp-admin across all site
47 62 * types. The render callback is wp-build's generated render function when
@@ -55,15 +70,25 @@
55 70 $callback = function_exists( self::WP_BUILD_RENDER_FN )
56 71 ? self::WP_BUILD_RENDER_FN
57 72 : array( __CLASS__, 'render_fallback' );
58 73
59 - Admin_Menu::add_menu(
74 + $page_suffix = Admin_Menu::add_menu(
60 75 'SEO',
61 76 'SEO',
62 77 'manage_options',
63 78 self::MENU_SLUG,
64 - $callback
79 + $callback,
80 + null,
81 + // SEO has no My Jetpack product class, so the module is the only gate available.
82 + array(
83 + 'module' => 'seo-tools',
84 + 'key' => 'jetpack-seo',
85 + )
65 86 );
87 +
88 + if ( $page_suffix ) {
89 + self::opt_out_of_jitms( $page_suffix );
90 + }
66 91 }
67 92
68 93 /**
69 94 * On the SEO admin page, load the wp-build bundle, alias the screen id so
@@ -78,11 +103,34 @@
78 103 if ( ! self::is_seo_admin_request() ) {
79 104 return;
80 105 }
81 106
107 + self::load_wp_build_with_screen_alias();
108 + add_filter( 'jetpack_admin_js_script_data', array( __CLASS__, 'inject_script_data' ) );
109 + }
110 +
111 + /**
112 + * Load wp-build with the screen ID aliased across its generated enqueue check.
113 + *
114 + * @see WP_Build_Screen_Id::load_with_alias()
115 + * @return void
116 + */
117 + private static function load_wp_build_with_screen_alias() {
118 + // Fallback: an older wp-build-polyfills under the jetpack-autoloader may predate load_with_alias().
119 + if ( method_exists( WP_Build_Screen_Id::class, 'load_with_alias' ) ) {
120 + WP_Build_Screen_Id::load_with_alias(
121 + array( __CLASS__, 'alias_screen_id_for_wp_build' ),
122 + array( __CLASS__, 'restore_screen_id_after_wp_build' ),
123 + function () {
124 + self::load_wp_build();
125 + }
126 + );
127 + return;
128 + }
129 +
130 + add_action( 'admin_enqueue_scripts', array( __CLASS__, 'alias_screen_id_for_wp_build' ) );
82 131 self::load_wp_build();
83 - add_action( 'current_screen', array( __CLASS__, 'alias_screen_id_for_wp_build' ) );
84 - add_filter( 'jetpack_admin_js_script_data', array( __CLASS__, 'inject_script_data' ) );
132 + add_action( 'admin_enqueue_scripts', array( __CLASS__, 'restore_screen_id_after_wp_build' ) );
85 133 }
86 134
87 135 /**
88 136 * Load wp-build's generated registration file and register the polyfills
@@ -109,17 +157,67 @@
109 157 /**
110 158 * Alias the current screen id to wp-build's expected slug so its
111 159 * auto-generated enqueue callback fires for our user-facing page.
112 160 *
113 - * @param \WP_Screen|null $screen The current screen object (passed by WP).
161 + * @since 0.9.5 Takes no argument; hooked on `admin_enqueue_scripts`.
162 + *
114 163 * @return void
115 164 */
116 - public static function alias_screen_id_for_wp_build( $screen ) {
117 - if ( ! is_object( $screen ) ) {
165 + public static function alias_screen_id_for_wp_build() {
166 + $screen = get_current_screen();
167 + if ( ! $screen ) {
118 168 return;
119 169 }
120 170
121 - $screen->id = self::WP_BUILD_SLUG;
171 + self::$wp_build_original_screen_id = $screen->id;
172 + $screen->id = self::WP_BUILD_SLUG;
173 + }
174 +
175 + /**
176 + * Undo alias_screen_id_for_wp_build(), so code after the generated check sees the real screen ID.
177 + *
178 + * @since 0.9.5
179 + *
180 + * @return void
181 + */
182 + public static function restore_screen_id_after_wp_build() {
183 + $screen = get_current_screen();
184 + if ( ! $screen || null === self::$wp_build_original_screen_id ) {
185 + return;
186 + }
187 +
188 + $screen->id = self::$wp_build_original_screen_id;
189 + self::$wp_build_original_screen_id = null;
190 + }
191 +
192 + /**
193 + * Opt the dashboard's screen out of JITMs.
194 + *
195 + * @param string $screen_id The hook suffix the page was registered under, which is its screen ID.
196 + * @return void
197 + */
198 + private static function opt_out_of_jitms( $screen_id ) {
199 + self::$jitm_opt_out_screen_id = $screen_id;
200 + add_filter( 'jetpack_display_jitms_on_screen', array( __CLASS__, 'hide_jitms_on_wp_build_dashboard' ), 10, 2 );
201 + }
202 +
203 + /**
204 + * Keep JITMs off the wp-build dashboard, which has no `#jp-admin-notices` to show them in.
205 + *
206 + * Fetching a JITM records a view, so one the page hides would still be counted.
207 + *
208 + * @since 0.9.5
209 + *
210 + * @param bool $show Whether to show JITMs on the screen.
211 + * @param string $screen_id The screen ID.
212 + * @return bool
213 + */
214 + public static function hide_jitms_on_wp_build_dashboard( $show, $screen_id ) {
215 + if ( null !== self::$jitm_opt_out_screen_id && self::$jitm_opt_out_screen_id === $screen_id ) {
216 + return false;
217 + }
218 +
219 + return $show;
122 220 }
123 221
124 222 /**
125 223 * Bootstrap the React app's initial state onto `window.JetpackScriptData.seo`.