Make WordPress Core

Changeset 58801


Ignore:
Timestamp:
07/24/2024 02:09:58 PM (4 months ago)
Author:
Bernhard Reiter
Message:

block.json: Allow passing PHP filename as variations field.

Previously, the variations field in a block.json file could be used to provide a static list of the block's variations (i.e., an array). Alternatively, the block's variation_callback could be set during server-side block registration to point to a PHP function to generate those variations.

This changeset makes it so that the block.json variations field can be alternatively set to a string, which will be interpreted as the filename of a PHP file that generates the variations.

It is loosely modeled after [54132], which introduced the render field for block.json, as a way to point to a PHP file instead of providing a render_callback.

Props bernhard-reiter, gziolo.
Fixes #61280.

Location:
trunk
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/blocks.php

    r58614 r58801  
    386386 * @since 6.4.0 Added support for `blockHooks` field.
    387387 * @since 6.5.0 Added support for `allowedBlocks`, `viewScriptModule`, and `viewStyle` fields.
     388 * @since 6.7.0 Allow PHP filename as `variations` argument.
    388389 *
    389390 * @param string $file_or_folder Path to the JSON file with metadata definition for
     
    523524    }
    524525
     526    // If `variations` is a string, it's the name of a PHP file that
     527    // generates the variations.
     528    if ( ! empty( $metadata['variations'] ) && is_string( $metadata['variations'] ) ) {
     529        $variations_path = wp_normalize_path(
     530            realpath(
     531                dirname( $metadata['file'] ) . '/' .
     532                remove_block_asset_path_prefix( $metadata['variations'] )
     533            )
     534        );
     535        if ( $variations_path ) {
     536            /**
     537             * Generates the list of block variations.
     538             *
     539             * @since 6.7.0
     540             *
     541             * @return string Returns the list of block variations.
     542             */
     543            $settings['variation_callback'] = static function () use ( $variations_path ) {
     544                $variations = require $variations_path;
     545                return $variations;
     546            };
     547            // The block instance's `variations` field is only allowed to be an array
     548            // (of known block variations). We unset it so that the block instance will
     549            // provide a getter that returns the result of the `variation_callback` instead.
     550            unset( $settings['variations'] );
     551        }
     552    }
     553
    525554    $settings = array_merge( $settings, $args );
    526555
  • trunk/tests/phpunit/tests/blocks/register.php

    r57987 r58801  
    959959
    960960    /**
     961     * Tests registering a block with variations from a PHP file.
     962     *
     963     * @ticket 61280
     964     *
     965     * @covers ::register_block_type_from_metadata
     966     */
     967    public function test_register_block_type_from_metadata_with_variations_php_file() {
     968        $filter_metadata_registration = static function ( $metadata ) {
     969            $metadata['variations'] = 'file:./variations.php';
     970            return $metadata;
     971        };
     972
     973        add_filter( 'block_type_metadata', $filter_metadata_registration, 10, 2 );
     974        $result = register_block_type_from_metadata(
     975            DIR_TESTDATA . '/blocks/notice'
     976        );
     977        remove_filter( 'block_type_metadata', $filter_metadata_registration );
     978
     979        $this->assertInstanceOf( 'WP_Block_Type', $result, 'The block was not registered' );
     980
     981        $this->assertIsCallable( $result->variation_callback, 'The variation callback hasn\'t been set' );
     982        $expected_variations = require DIR_TESTDATA . '/blocks/notice/variations.php';
     983        $this->assertSame(
     984            $expected_variations,
     985            call_user_func( $result->variation_callback ),
     986            'The variation callback hasn\'t been set correctly'
     987        );
     988        $this->assertSame( $expected_variations, $result->variations, 'The block variations are incorrect' );
     989    }
     990
     991    /**
    961992     * Tests that the function returns the registered block when the `block.json`
    962993     * is found in the fixtures directory.
Note: See TracChangeset for help on using the changeset viewer.