Editor: Return additional block patterns to server-generated settings

Reverts changes from [53155] to ensure backward compatibility.

Companion to Gutenberg changes https://github.com/WordPress/gutenberg/pull/40818. That makes sure that patterns registered with `admin_init` or `current_screen` hooks are not lost.

Props jsnajdr, zieladam, peterwilsoncc, johnstonphilip.
See #55567.


Built from https://develop.svn.wordpress.org/trunk@53404


git-svn-id: http://core.svn.wordpress.org/trunk@52993 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
gziolo 2022-05-17 14:38:14 +00:00
parent 58dfeaa86f
commit 571e1e01b3
5 changed files with 61 additions and 7 deletions

View File

@ -209,6 +209,10 @@ $editor_settings = array(
'enableCustomFields' => (bool) get_user_meta( get_current_user_id(), 'enable_custom_fields', true ),
);
// Add additional back-compat patterns registered by `current_screen` et al.
$editor_settings['__experimentalAdditionalBlockPatterns'] = WP_Block_Patterns_Registry::get_instance()->get_all_registered( true );
$editor_settings['__experimentalAdditionalBlockPatternCategories'] = WP_Block_Pattern_Categories_Registry::get_instance()->get_all_registered( true );
$autosave = wp_get_post_autosave( $post->ID );
if ( $autosave ) {
if ( mysql2date( 'U', $autosave->post_modified_gmt, false ) > mysql2date( 'U', $post->post_modified_gmt, false ) ) {

View File

@ -68,6 +68,11 @@ $custom_settings = array(
'defaultTemplatePartAreas' => get_allowed_block_template_part_areas(),
'__unstableHomeTemplate' => $home_template,
);
// Add additional back-compat patterns registered by `current_screen` et al.
$custom_settings['__experimentalAdditionalBlockPatterns'] = WP_Block_Patterns_Registry::get_instance()->get_all_registered( true );
$custom_settings['__experimentalAdditionalBlockPatternCategories'] = WP_Block_Pattern_Categories_Registry::get_instance()->get_all_registered( true );
$editor_settings = get_block_editor_settings( $custom_settings, $block_editor_context );
if ( isset( $_GET['postType'] ) && ! isset( $_GET['postId'] ) ) {

View File

@ -19,6 +19,14 @@ final class WP_Block_Pattern_Categories_Registry {
*/
private $registered_categories = array();
/**
* Pattern categories registered outside the `init` action.
*
* @since 6.0.0
* @var array[]
*/
private $registered_categories_outside_init = array();
/**
* Container for the main instance of the class.
*
@ -50,11 +58,20 @@ final class WP_Block_Pattern_Categories_Registry {
return false;
}
$this->registered_categories[ $category_name ] = array_merge(
$category = array_merge(
array( 'name' => $category_name ),
$category_properties
);
$this->registered_categories[ $category_name ] = $category;
// If the category is registered inside an action other than `init`, store it
// also to a dedicated array. Used to detect deprecated registrations inside
// `admin_init` or `current_screen`.
if ( current_action() && 'init' !== current_action() ) {
$this->registered_categories_outside_init[ $category_name ] = $category;
}
return true;
}
@ -78,6 +95,7 @@ final class WP_Block_Pattern_Categories_Registry {
}
unset( $this->registered_categories[ $category_name ] );
unset( $this->registered_categories_outside_init[ $category_name ] );
return true;
}
@ -103,10 +121,15 @@ final class WP_Block_Pattern_Categories_Registry {
*
* @since 5.5.0
*
* @param bool $outside_init_only Return only categories registered outside the `init` action.
* @return array[] Array of arrays containing the registered pattern categories properties.
*/
public function get_all_registered() {
return array_values( $this->registered_categories );
public function get_all_registered( $outside_init_only = false ) {
return array_values(
$outside_init_only
? $this->registered_categories_outside_init
: $this->registered_categories
);
}
/**

View File

@ -21,6 +21,14 @@ final class WP_Block_Patterns_Registry {
*/
private $registered_patterns = array();
/**
* Patterns registered outside the `init` action.
*
* @since 6.0.0
* @var array[]
*/
private $registered_patterns_outside_init = array();
/**
* Container for the main instance of the class.
*
@ -92,10 +100,18 @@ final class WP_Block_Patterns_Registry {
return false;
}
$this->registered_patterns[ $pattern_name ] = array_merge(
$pattern = array_merge(
$pattern_properties,
array( 'name' => $pattern_name )
);
$this->registered_patterns[ $pattern_name ] = $pattern;
// If the pattern is registered inside an action other than `init`, store it
// also to a dedicated array. Used to detect deprecated registrations inside
// `admin_init` or `current_screen`.
if ( current_action() && 'init' !== current_action() ) {
$this->registered_patterns_outside_init[ $pattern_name ] = $pattern;
}
return true;
}
@ -120,6 +136,7 @@ final class WP_Block_Patterns_Registry {
}
unset( $this->registered_patterns[ $pattern_name ] );
unset( $this->registered_patterns_outside_init[ $pattern_name ] );
return true;
}
@ -145,11 +162,16 @@ final class WP_Block_Patterns_Registry {
*
* @since 5.5.0
*
* @param bool $outside_init_only Return only patterns registered outside the `init` action.
* @return array[] Array of arrays containing the registered block patterns properties,
* and per style.
*/
public function get_all_registered() {
return array_values( $this->registered_patterns );
public function get_all_registered( $outside_init_only = false ) {
return array_values(
$outside_init_only
? $this->registered_patterns_outside_init
: $this->registered_patterns
);
}
/**

View File

@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '6.1-alpha-53403';
$wp_version = '6.1-alpha-53404';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.