From 25391f0a4fcb9dc84c942ea93afe32f2ae97cb7f Mon Sep 17 00:00:00 2001 From: Tonya Mork Date: Mon, 17 Jan 2022 22:40:51 +0000 Subject: [PATCH] Editor: Explicitly load remote block patterns in the block and site editor screens. Remote block patterns from wp.org were to be loaded through a callback hooked into the `current_screen` filter. Within 2 callbacks, i.e. `_load_remote_featured_patterns()` and `_load_remote_block_patterns()`, a guard clause bailed out early if the `$current_screen->is_block_editor` is `false`. However, the `current_screen` filter is unreliable to detect the block editor. Why? In the block and Site Editor screens, `$current_scren->is_block_editor` is not set until after the filter is executed. Whoopsie. This commit no longer uses the `current_screen` filter. Instead, it explicitly loads the remote block patterns by invoking both private functions (now not callbacks) directly in the screen files for the block and site editor screens. With this change, passing `WP_Screen` object into these functions is no longer needed. As the `_load_remote_block_patterns()` function was introduced in 5.8.0, its function parameter is now deprecated and the guard clause retained for backwards compatibility. Follow-up to [51021], [52377]. Props poena, noisysocks, peterwilsoncc, hellofromTonya, audrasjb. Fixes #54806. git-svn-id: https://develop.svn.wordpress.org/trunk@52593 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-admin/edit-form-blocks.php | 4 ++++ src/wp-admin/site-editor.php | 4 ++++ src/wp-includes/block-patterns.php | 21 ++++++++++----------- src/wp-includes/default-filters.php | 2 -- 4 files changed, 18 insertions(+), 13 deletions(-) diff --git a/src/wp-admin/edit-form-blocks.php b/src/wp-admin/edit-form-blocks.php index e05be4c356..5e7b39d619 100644 --- a/src/wp-admin/edit-form-blocks.php +++ b/src/wp-admin/edit-form-blocks.php @@ -28,6 +28,10 @@ $block_editor_context = new WP_Block_Editor_Context( array( 'post' => $post ) ); $current_screen = get_current_screen(); $current_screen->is_block_editor( true ); +// Load block patterns from w.org. +_load_remote_block_patterns(); +_load_remote_featured_patterns(); + // Default to is-fullscreen-mode to avoid jumps in the UI. add_filter( 'admin_body_class', diff --git a/src/wp-admin/site-editor.php b/src/wp-admin/site-editor.php index 421ceb4861..cbbf38e7ea 100644 --- a/src/wp-admin/site-editor.php +++ b/src/wp-admin/site-editor.php @@ -31,6 +31,10 @@ $parent_file = 'themes.php'; $current_screen = get_current_screen(); $current_screen->is_block_editor( true ); +// Load block patterns from w.org. +_load_remote_block_patterns(); +_load_remote_featured_patterns(); + // Default to is-fullscreen-mode to avoid jumps in the UI. add_filter( 'admin_body_class', diff --git a/src/wp-includes/block-patterns.php b/src/wp-includes/block-patterns.php index b1dfaeb403..55c0dd29ba 100644 --- a/src/wp-includes/block-patterns.php +++ b/src/wp-includes/block-patterns.php @@ -48,12 +48,17 @@ function _register_core_block_patterns_and_categories() { * Register Core's official patterns from wordpress.org/patterns. * * @since 5.8.0 + * @since 5.9.0 The $current_screen argument was removed. * - * @param WP_Screen $current_screen The screen that the current request was triggered from. + * @param WP_Screen $deprecated Unused. Formerly the screen that the current request was triggered from. */ -function _load_remote_block_patterns( $current_screen ) { - if ( ! $current_screen->is_block_editor ) { - return; +function _load_remote_block_patterns( $deprecated = null ) { + if ( ! empty( $deprecated ) ) { + _deprecated_argument( __FUNCTION__, '5.9.0' ); + $current_screen = $deprecated; + if ( ! $current_screen->is_block_editor ) { + return; + } } $supports_core_patterns = get_theme_support( 'core-block-patterns' ); @@ -88,14 +93,8 @@ function _load_remote_block_patterns( $current_screen ) { * Register `Featured` (category) patterns from wordpress.org/patterns. * * @since 5.9.0 - * - * @param WP_Screen $current_screen The screen that the current request was triggered from. */ -function _load_remote_featured_patterns( $current_screen ) { - if ( ! $current_screen->is_block_editor ) { - return; - } - +function _load_remote_featured_patterns() { $supports_core_patterns = get_theme_support( 'core-block-patterns' ); /** This filter is documented in wp-includes/block-patterns.php */ diff --git a/src/wp-includes/default-filters.php b/src/wp-includes/default-filters.php index d9663946d6..20559dd179 100644 --- a/src/wp-includes/default-filters.php +++ b/src/wp-includes/default-filters.php @@ -332,8 +332,6 @@ add_action( 'wp_footer', 'wp_print_footer_scripts', 20 ); add_action( 'template_redirect', 'wp_shortlink_header', 11, 0 ); add_action( 'wp_print_footer_scripts', '_wp_footer_scripts' ); add_action( 'init', '_register_core_block_patterns_and_categories' ); -add_action( 'current_screen', '_load_remote_block_patterns' ); -add_action( 'current_screen', '_load_remote_featured_patterns' ); add_action( 'init', 'check_theme_switched', 99 ); add_action( 'init', array( 'WP_Block_Supports', 'init' ), 22 ); add_action( 'switch_theme', array( 'WP_Theme_JSON_Resolver', 'clean_cached_data' ) );