-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
block.json: Allow passing filename as variations
field
#62092
Merged
ockham
merged 17 commits into
trunk
from
update/block-json-schema-to-allow-variations-filename
Jul 9, 2024
Merged
Changes from 16 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
215179e
block.json schema: Allow passing filename as variations argument
ockham ece51bf
Add compat layer
ockham 47949ed
Add unit test
ockham 0a1c1a2
Actually load compat layer
ockham 1430519
Coding Standards
ockham 07aa580
Use 'file:' prefix
ockham eefe6e3
Update docs
ockham 2498f5d
Remove assertions that would break for WP < 6.5
ockham c2cd023
Add backport changelog
ockham bd378a6
Check that the variation is an array before merging now that they can…
tjcafferkey 6f45ac5
Remove note from client-side block registration docs
ockham 8be5189
Document how to shape the variations PHP file
ockham fe484e0
Change example to use __()
ockham fe5cca2
Add note about l10n
ockham 253dcca
Add one more inline comment
ockham 0ea4266
Use `$settings['variations']` instead of `$metadata['variations']`
ockham be8a9c2
Add descriptions for both possible variations field types
ockham File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you think it is worth adding a
description
property here to better document the expected usage of this?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, that's a good point. I thought it might collide with the
description
a couple lines above (i.e. which applies to bothoneOf
options), but it seems to be allowed by the JSON schema spec, so I'll add it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
be8a9c2