Make WordPress Core

Changeset 53404


Ignore:
Timestamp:
05/17/2022 02:36:22 PM (2 years ago)
Author:
gziolo
Message:

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.

Location:
trunk/src
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/edit-form-blocks.php

    r53197 r53404  
    210210);
    211211
     212// Add additional back-compat patterns registered by `current_screen` et al.
     213$editor_settings['__experimentalAdditionalBlockPatterns']          = WP_Block_Patterns_Registry::get_instance()->get_all_registered( true );
     214$editor_settings['__experimentalAdditionalBlockPatternCategories'] = WP_Block_Pattern_Categories_Registry::get_instance()->get_all_registered( true );
     215
    212216$autosave = wp_get_post_autosave( $post->ID );
    213217if ( $autosave ) {
  • trunk/src/wp-admin/site-editor.php

    r53197 r53404  
    6969    '__unstableHomeTemplate'   => $home_template,
    7070);
     71
     72// Add additional back-compat patterns registered by `current_screen` et al.
     73$custom_settings['__experimentalAdditionalBlockPatterns']          = WP_Block_Patterns_Registry::get_instance()->get_all_registered( true );
     74$custom_settings['__experimentalAdditionalBlockPatternCategories'] = WP_Block_Pattern_Categories_Registry::get_instance()->get_all_registered( true );
     75
    7176$editor_settings      = get_block_editor_settings( $custom_settings, $block_editor_context );
    7277
  • trunk/src/wp-includes/class-wp-block-pattern-categories-registry.php

    r53299 r53404  
    1919     */
    2020    private $registered_categories = array();
     21
     22    /**
     23     * Pattern categories registered outside the `init` action.
     24     *
     25     * @since 6.0.0
     26     * @var array[]
     27     */
     28    private $registered_categories_outside_init = array();
    2129
    2230    /**
     
    5159        }
    5260
    53         $this->registered_categories[ $category_name ] = array_merge(
     61        $category = array_merge(
    5462            array( 'name' => $category_name ),
    5563            $category_properties
    5664        );
     65
     66        $this->registered_categories[ $category_name ] = $category;
     67
     68        // If the category is registered inside an action other than `init`, store it
     69        // also to a dedicated array. Used to detect deprecated registrations inside
     70        // `admin_init` or `current_screen`.
     71        if ( current_action() && 'init' !== current_action() ) {
     72            $this->registered_categories_outside_init[ $category_name ] = $category;
     73        }
    5774
    5875        return true;
     
    7996
    8097        unset( $this->registered_categories[ $category_name ] );
     98        unset( $this->registered_categories_outside_init[ $category_name ] );
    8199
    82100        return true;
     
    104122     * @since 5.5.0
    105123     *
     124     * @param bool $outside_init_only Return only categories registered outside the `init` action.
    106125     * @return array[] Array of arrays containing the registered pattern categories properties.
    107126     */
    108     public function get_all_registered() {
    109         return array_values( $this->registered_categories );
     127    public function get_all_registered( $outside_init_only = false ) {
     128        return array_values(
     129            $outside_init_only
     130                ? $this->registered_categories_outside_init
     131                : $this->registered_categories
     132        );
    110133    }
    111134
  • trunk/src/wp-includes/class-wp-block-patterns-registry.php

    r53299 r53404  
    2121     */
    2222    private $registered_patterns = array();
     23
     24    /**
     25     * Patterns registered outside the `init` action.
     26     *
     27     * @since 6.0.0
     28     * @var array[]
     29     */
     30    private $registered_patterns_outside_init = array();
    2331
    2432    /**
     
    93101        }
    94102
    95         $this->registered_patterns[ $pattern_name ] = array_merge(
     103        $pattern = array_merge(
    96104            $pattern_properties,
    97105            array( 'name' => $pattern_name )
    98106        );
     107        $this->registered_patterns[ $pattern_name ] = $pattern;
     108
     109        // If the pattern is registered inside an action other than `init`, store it
     110        // also to a dedicated array. Used to detect deprecated registrations inside
     111        // `admin_init` or `current_screen`.
     112        if ( current_action() && 'init' !== current_action() ) {
     113            $this->registered_patterns_outside_init[ $pattern_name ] = $pattern;
     114        }
    99115
    100116        return true;
     
    121137
    122138        unset( $this->registered_patterns[ $pattern_name ] );
     139        unset( $this->registered_patterns_outside_init[ $pattern_name ] );
    123140
    124141        return true;
     
    146163     * @since 5.5.0
    147164     *
     165     * @param bool $outside_init_only Return only patterns registered outside the `init` action.
    148166     * @return array[] Array of arrays containing the registered block patterns properties,
    149167     *                 and per style.
    150168     */
    151     public function get_all_registered() {
    152         return array_values( $this->registered_patterns );
     169    public function get_all_registered( $outside_init_only = false ) {
     170        return array_values(
     171            $outside_init_only
     172                ? $this->registered_patterns_outside_init
     173                : $this->registered_patterns
     174        );
    153175    }
    154176
Note: See TracChangeset for help on using the changeset viewer.