Make WordPress Core


Ignore:
Timestamp:
01/19/2024 08:52:06 PM (3 years ago)
Author:
joemcgill
Message:

Editor: Support deferred block variation initialization on the server.

When registering blocks on the server using register_block_type() or similar functions, a set of block type variations can also be registered. However, in some cases building this variation data during block registration can be an expensive process, which is not needed in most contexts.

To address this problem, this adds support to the WP_Block_Type object for a new property, variation_callback, which can be used to register a callback for building variation data only when the block variations data is needed. The WP_Block_Type::variations property has been changed to a private property that is now accessed through the magic __get() method. The magic getter makes use of a new public method, WP_Block_Type::get_variations which will build variations from a registered callback if variations have not already been built.

Props spacedmonkey, thekt12, Mamaduka, gaambo, gziolo, mukesh27, joemcgill.
Fixes #59969.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-block-type.php

    r57140 r57315  
    114114         *
    115115         * @since 5.8.0
    116          * @var array[]
    117          */
    118         public $variations = array();
     116         * @since 6.5.0 Only accessible through magic getter. null by default.
     117         * @var array[]|null
     118         */
     119        private $variations = null;
     120
     121        /**
     122         * Block variations callback.
     123         *
     124         * @since 6.5.0
     125         * @var callable|null
     126         */
     127        public $variation_callback = null;
    119128
    120129        /**
     
    297306         *     @type array|null    $example                  Structured data for the block preview.
    298307         *     @type callable|null $render_callback          Block type render callback.
     308         *     @type callable|null $variation_callback       Block type variations callback.
    299309         *     @type array|null    $attributes               Block type attributes property schemas.
    300310         *     @type string[]      $uses_context             Context values inherited by blocks of this type.
     
    326336         */
    327337        public function __get( $name ) {
     338                if ( 'variations' === $name ) {
     339                        return $this->get_variations();
     340                }
     341
    328342                if ( ! in_array( $name, $this->deprecated_properties, true ) ) {
    329343                        return;
     
    354368         */
    355369        public function __isset( $name ) {
     370                if ( 'variations' === $name ) {
     371                        return true;
     372                }
     373
    356374                if ( ! in_array( $name, $this->deprecated_properties, true ) ) {
    357375                        return false;
     
    373391         */
    374392        public function __set( $name, $value ) {
     393                if ( 'variations' === $name ) {
     394                        $this->variations = $value;
     395                        return;
     396                }
     397
    375398                if ( ! in_array( $name, $this->deprecated_properties, true ) ) {
    376399                        $this->{$name} = $value;
     
    541564                        array();
    542565        }
     566
     567        /**
     568         * Get block variations.
     569         *
     570         * @since 6.5.0
     571         *
     572         * @return array[]
     573         */
     574        public function get_variations() {
     575                if ( ! isset( $this->variations ) ) {
     576                        $this->variations = array();
     577                        if ( is_callable( $this->variation_callback ) ) {
     578                                $this->variations = call_user_func( $this->variation_callback );
     579                        }
     580                }
     581
     582                /**
     583                 * Filters the registered variations for a block type.
     584                 *
     585                 * @since 6.5.0
     586                 *
     587                 * @param array         $variations Array of registered variations for a block type.
     588                 * @param WP_Block_Type $block_type The full block type object.
     589                 */
     590                return apply_filters( 'get_block_type_variations', $this->variations, $this );
     591        }
    543592}
Note: See TracChangeset for help on using the changeset viewer.