← All changes
|
jetpack_vendor/automattic/jetpack-podcast/src/class-admin-page.php
+99
-8
16.2
→
16.3
View file →
| @@ -10,8 +10,9 @@ | ||
| 10 | 10 | use Automattic\Jetpack\Admin_UI\Admin_Menu; |
| 11 | 11 | use Automattic\Jetpack\Connection\Manager as Connection_Manager; |
| 12 | 12 | use Automattic\Jetpack\Status\Host; |
| 13 | 13 | use Automattic\Jetpack\WP_Build_Polyfills\WP_Build_Polyfills; |
| 14 | +use Automattic\Jetpack\WP_Build_Polyfills\WP_Build_Screen_Id; | |
| 14 | 15 | |
| 15 | 16 | /** |
| 16 | 17 | * Adds the "Jetpack > Podcast" wp-admin screen. |
| 17 | 18 | */ |
| @@ -30,9 +31,9 @@ | ||
| 30 | 31 | |
| 31 | 32 | /** |
| 32 | 33 | * Slug emitted by `@wordpress/build`. wp-build's auto-generated enqueue |
| 33 | 34 | * callback only fires when `$screen->id` matches this value, so we alias |
| 34 | - * the screen id via `current_screen` without changing the user-facing URL. | |
| 35 | + * the screen id around that check without changing the user-facing URL. | |
| 35 | 36 | */ |
| 36 | 37 | const WP_BUILD_SLUG = 'jetpack-podcast-dashboard'; |
| 37 | 38 | |
| 38 | 39 | /** |
| @@ -42,8 +43,22 @@ | ||
| 42 | 43 | */ |
| 43 | 44 | private static $initialized = false; |
| 44 | 45 | |
| 45 | 46 | /** |
| 47 | + * The screen ID alias_screen_id_for_wp_build() replaced, until it is restored. | |
| 48 | + * | |
| 49 | + * @var string|null | |
| 50 | + */ | |
| 51 | + private static $wp_build_original_screen_id = null; | |
| 52 | + | |
| 53 | + /** | |
| 54 | + * The dashboard screen hide_jitms_on_wp_build_dashboard() opts out of JITMs. | |
| 55 | + * | |
| 56 | + * @var string|null | |
| 57 | + */ | |
| 58 | + private static $jitm_opt_out_screen_id = null; | |
| 59 | + | |
| 60 | + /** | |
| 46 | 61 | * Wire admin hooks. Idempotent. |
| 47 | 62 | */ |
| 48 | 63 | public static function init() { |
| 49 | 64 | if ( self::$initialized ) { |
| @@ -87,14 +102,21 @@ | ||
| 87 | 102 | 'Podcast', |
| 88 | 103 | 'Podcast', |
| 89 | 104 | 'manage_options', |
| 90 | 105 | self::ADMIN_PAGE_SLUG, |
| 91 | - $callback | |
| 106 | + $callback, | |
| 107 | + null, | |
| 108 | + // Podcast has no My Jetpack product class, so the module is the only gate available. | |
| 109 | + array( | |
| 110 | + 'module' => 'podcast', | |
| 111 | + 'key' => 'jetpack-podcast', | |
| 112 | + ) | |
| 92 | 113 | ); |
| 93 | 114 | } |
| 94 | 115 | |
| 95 | 116 | if ( $page_suffix ) { |
| 96 | 117 | add_action( 'load-' . $page_suffix, array( __CLASS__, 'admin_init' ) ); |
| 118 | + self::opt_out_of_jitms( $page_suffix ); | |
| 97 | 119 | } |
| 98 | 120 | } |
| 99 | 121 | |
| 100 | 122 | /** |
| @@ -134,10 +156,9 @@ | ||
| 134 | 156 | if ( ! self::is_podcast_admin_request() ) { |
| 135 | 157 | return; |
| 136 | 158 | } |
| 137 | 159 | |
| 138 | - self::load_wp_build(); | |
| 139 | - add_action( 'current_screen', array( __CLASS__, 'alias_screen_id_for_wp_build' ) ); | |
| 160 | + self::load_wp_build_with_screen_alias(); | |
| 140 | 161 | add_filter( 'jetpack_admin_js_script_data', array( __CLASS__, 'inject_podcast_script_data' ) ); |
| 141 | 162 | } |
| 142 | 163 | |
| 143 | 164 | /** |
| @@ -254,18 +275,88 @@ | ||
| 254 | 275 | ); |
| 255 | 276 | } |
| 256 | 277 | |
| 257 | 278 | /** |
| 279 | + * Load wp-build with the screen ID aliased across its generated enqueue check. | |
| 280 | + * | |
| 281 | + * @see WP_Build_Screen_Id::load_with_alias() | |
| 282 | + * @return void | |
| 283 | + */ | |
| 284 | + private static function load_wp_build_with_screen_alias() { | |
| 285 | + // Fallback: an older wp-build-polyfills under the jetpack-autoloader may predate load_with_alias(). | |
| 286 | + if ( method_exists( WP_Build_Screen_Id::class, 'load_with_alias' ) ) { | |
| 287 | + WP_Build_Screen_Id::load_with_alias( | |
| 288 | + array( __CLASS__, 'alias_screen_id_for_wp_build' ), | |
| 289 | + array( __CLASS__, 'restore_screen_id_after_wp_build' ), | |
| 290 | + function () { | |
| 291 | + self::load_wp_build(); | |
| 292 | + } | |
| 293 | + ); | |
| 294 | + return; | |
| 295 | + } | |
| 296 | + | |
| 297 | + add_action( 'admin_enqueue_scripts', array( __CLASS__, 'alias_screen_id_for_wp_build' ) ); | |
| 298 | + self::load_wp_build(); | |
| 299 | + add_action( 'admin_enqueue_scripts', array( __CLASS__, 'restore_screen_id_after_wp_build' ) ); | |
| 300 | + } | |
| 301 | + | |
| 302 | + /** | |
| 258 | 303 | * Alias the current screen id to wp-build's expected slug. |
| 259 | 304 | * |
| 260 | - * @param \WP_Screen|null $screen The current screen object (passed by WP). | |
| 305 | + * @since 2.1.3 Takes no argument; hooked on `admin_enqueue_scripts`. | |
| 261 | 306 | */ |
| 262 | - public static function alias_screen_id_for_wp_build( $screen ) { | |
| 263 | - if ( ! is_object( $screen ) ) { | |
| 307 | + public static function alias_screen_id_for_wp_build() { | |
| 308 | + $screen = get_current_screen(); | |
| 309 | + if ( ! $screen ) { | |
| 264 | 310 | return; |
| 265 | 311 | } |
| 266 | 312 | |
| 267 | - $screen->id = self::WP_BUILD_SLUG; | |
| 313 | + self::$wp_build_original_screen_id = $screen->id; | |
| 314 | + $screen->id = self::WP_BUILD_SLUG; | |
| 315 | + } | |
| 316 | + | |
| 317 | + /** | |
| 318 | + * Undo alias_screen_id_for_wp_build(), so code after the generated check sees the real screen ID. | |
| 319 | + * | |
| 320 | + * @since 2.1.3 | |
| 321 | + */ | |
| 322 | + public static function restore_screen_id_after_wp_build() { | |
| 323 | + $screen = get_current_screen(); | |
| 324 | + if ( ! $screen || null === self::$wp_build_original_screen_id ) { | |
| 325 | + return; | |
| 326 | + } | |
| 327 | + | |
| 328 | + $screen->id = self::$wp_build_original_screen_id; | |
| 329 | + self::$wp_build_original_screen_id = null; | |
| 330 | + } | |
| 331 | + | |
| 332 | + /** | |
| 333 | + * Opt the dashboard's screen out of JITMs. | |
| 334 | + * | |
| 335 | + * @param string $screen_id The hook suffix the page was registered under, which is its screen ID. | |
| 336 | + */ | |
| 337 | + private static function opt_out_of_jitms( $screen_id ) { | |
| 338 | + self::$jitm_opt_out_screen_id = $screen_id; | |
| 339 | + add_filter( 'jetpack_display_jitms_on_screen', array( __CLASS__, 'hide_jitms_on_wp_build_dashboard' ), 10, 2 ); | |
| 340 | + } | |
| 341 | + | |
| 342 | + /** | |
| 343 | + * Keep JITMs off the wp-build dashboard, which has no `#jp-admin-notices` to show them in. | |
| 344 | + * | |
| 345 | + * Fetching a JITM records a view, so one the page hides would still be counted. | |
| 346 | + * | |
| 347 | + * @since 2.1.3 | |
| 348 | + * | |
| 349 | + * @param bool $show Whether to show JITMs on the screen. | |
| 350 | + * @param string $screen_id The screen ID. | |
| 351 | + * @return bool | |
| 352 | + */ | |
| 353 | + public static function hide_jitms_on_wp_build_dashboard( $show, $screen_id ) { | |
| 354 | + if ( null !== self::$jitm_opt_out_screen_id && self::$jitm_opt_out_screen_id === $screen_id ) { | |
| 355 | + return false; | |
| 356 | + } | |
| 357 | + | |
| 358 | + return $show; | |
| 268 | 359 | } |
| 269 | 360 | |
| 270 | 361 | /** |
| 271 | 362 | * Fallback render used when the wp-build artifact is missing. |