Make WordPress Core


Ignore:
Timestamp:
05/17/2022 02:36:22 PM (4 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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.