-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
block.json: Allow passing filename as
variations
field (#62092)
Allow passing the name of a PHP file that returns a block's variations in the `block.json` file's `variations` field. Co-authored-by: ockham <bernhard-reiter@git.wordpress.org> Co-authored-by: tjcafferkey <tomjcafferkey@git.wordpress.org> Co-authored-by: gziolo <gziolo@git.wordpress.org>
- Loading branch information
1 parent
a58c1f9
commit 4c93cf2
Showing
8 changed files
with
246 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
https://github.com/WordPress/wordpress-develop/pull/6668 | ||
|
||
* https://github.com/WordPress/gutenberg/pull/62092 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
/** | ||
* Temporary compatibility shims for block APIs present in Gutenberg. | ||
* | ||
* @package gutenberg | ||
*/ | ||
|
||
/** | ||
* Allow passing a PHP file as `variations` field for Core versions < 6.7 | ||
* | ||
* @param array $settings Array of determined settings for registering a block type. | ||
* @param array $metadata Metadata provided for registering a block type. | ||
* @return array The block type settings | ||
*/ | ||
function gutenberg_filter_block_type_metadata_settings_allow_variations_php_file( $settings, $metadata ) { | ||
// If `variations` is a string, it's the name of a PHP file that | ||
// generates the variations. | ||
if ( ! empty( $settings['variations'] ) && is_string( $settings['variations'] ) ) { | ||
$variations_path = wp_normalize_path( | ||
realpath( | ||
dirname( $metadata['file'] ) . '/' . | ||
remove_block_asset_path_prefix( $settings['variations'] ) | ||
) | ||
); | ||
if ( $variations_path ) { | ||
/** | ||
* Generates the list of block variations. | ||
* | ||
* @since 6.7.0 | ||
* | ||
* @return string Returns the list of block variations. | ||
*/ | ||
$settings['variation_callback'] = static function () use ( $variations_path ) { | ||
$variations = require $variations_path; | ||
return $variations; | ||
}; | ||
// The block instance's `variations` field is only allowed to be an array | ||
// (of known block variations). We unset it so that the block instance will | ||
// provide a getter that returns the result of the `variation_callback` instead. | ||
unset( $settings['variations'] ); | ||
} | ||
} | ||
return $settings; | ||
} | ||
add_filter( 'block_type_metadata_settings', 'gutenberg_filter_block_type_metadata_settings_allow_variations_php_file', 10, 2 ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
/** | ||
* Test for the block.json's variations field being a PHP file. | ||
* | ||
* @package WordPress | ||
* @subpackage Blocks | ||
*/ | ||
|
||
/** | ||
* Test that block variations can be registered from a PHP file. | ||
* | ||
* @group blocks | ||
*/ | ||
class Block_Json_Variations_Filename_Test extends WP_UnitTestCase { | ||
/** | ||
* Tear down each test method. | ||
*/ | ||
public function tear_down() { | ||
$registry = WP_Block_Type_Registry::get_instance(); | ||
|
||
if ( $registry->is_registered( 'my-plugin/notice' ) ) { | ||
$registry->unregister( 'my-plugin/notice' ); | ||
} | ||
|
||
parent::tear_down(); | ||
} | ||
|
||
/** | ||
* Tests registering a block with variations from a PHP file. | ||
* | ||
* @covers ::register_block_type_from_metadata | ||
*/ | ||
public function test_register_block_type_from_metadata_with_variations_php_file() { | ||
$filter_metadata_registration = static function ( $metadata ) { | ||
$metadata['variations'] = 'file:./variations.php'; | ||
return $metadata; | ||
}; | ||
|
||
add_filter( 'block_type_metadata', $filter_metadata_registration, 10, 2 ); | ||
$result = register_block_type_from_metadata( GUTENBERG_DIR_TESTFIXTURES ); | ||
remove_filter( 'block_type_metadata', $filter_metadata_registration ); | ||
|
||
$this->assertInstanceOf( 'WP_Block_Type', $result, 'The block was not registered' ); | ||
|
||
$expected_variations = require GUTENBERG_DIR_TESTFIXTURES . '/variations.php'; | ||
$this->assertSame( $expected_variations, $result->variations, "Block variations haven't been set correctly." ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
return array( | ||
array( | ||
'name' => 'warning', | ||
'title' => 'warning', | ||
'description' => 'Shows warning.', | ||
'keywords' => array( 'warning' ), | ||
), | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters