-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Block Bindings: Add block uses_context defined by block bindings sources
#6079
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
Changes from all commits
63d381f
65e259a
68ce2a8
ade4119
5011e16
929afb6
44125a4
5a931f4
dccef2e
016ec29
e305f7c
49a8b7a
42575c4
e41ba30
4ca8176
46df776
59746c0
7bcc71e
5810180
a578d5c
9f01568
ba7bf25
0c4a795
7e80f5e
d839f33
e82879e
102c940
f4af4f7
f2b3b3a
4f8668d
07a7317
f14817f
edaaffa
03e66ee
0b49cc4
9838fec
59fde29
5f2ddc3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -32,6 +32,31 @@ final class WP_Block_Bindings_Registry { | |
| */ | ||
| private static $instance = null; | ||
|
|
||
| /** | ||
| * Supported source properties that can be passed to the registered source. | ||
| * | ||
| * @since 6.5.0 | ||
| * @var array | ||
| */ | ||
| private $allowed_source_properties = array( | ||
| 'label', | ||
| 'get_value_callback', | ||
| 'uses_context', | ||
| ); | ||
|
|
||
| /** | ||
| * Supported blocks that can use the block bindings API. | ||
| * | ||
| * @since 6.5.0 | ||
| * @var array | ||
| */ | ||
| private $supported_blocks = array( | ||
| 'core/paragraph', | ||
| 'core/heading', | ||
| 'core/image', | ||
| 'core/button', | ||
| ); | ||
|
|
||
| /** | ||
| * Registers a new block bindings source. | ||
| * | ||
|
|
@@ -53,18 +78,19 @@ final class WP_Block_Bindings_Registry { | |
| * @param array $source_properties { | ||
| * The array of arguments that are used to register a source. | ||
| * | ||
| * @type string $label The label of the source. | ||
| * @type callback $get_value_callback A callback executed when the source is processed during block rendering. | ||
| * The callback should have the following signature: | ||
| * | ||
| * `function ($source_args, $block_instance,$attribute_name): mixed` | ||
| * - @param array $source_args Array containing source arguments | ||
| * used to look up the override value, | ||
| * i.e. {"key": "foo"}. | ||
| * - @param WP_Block $block_instance The block instance. | ||
| * - @param string $attribute_name The name of the target attribute. | ||
| * The callback has a mixed return type; it may return a string to override | ||
| * the block's original value, null, false to remove an attribute, etc. | ||
| * @type string $label The label of the source. | ||
| * @type callback $get_value_callback A callback executed when the source is processed during block rendering. | ||
| * The callback should have the following signature: | ||
| * | ||
| * `function ($source_args, $block_instance,$attribute_name): mixed` | ||
| * - @param array $source_args Array containing source arguments | ||
| * used to look up the override value, | ||
| * i.e. {"key": "foo"}. | ||
| * - @param WP_Block $block_instance The block instance. | ||
| * - @param string $attribute_name The name of the target attribute. | ||
| * The callback has a mixed return type; it may return a string to override | ||
| * the block's original value, null, false to remove an attribute, etc. | ||
| * @type array $uses_context (optional) Array of values to add to block `uses_context` needed by the source. | ||
| * } | ||
| * @return WP_Block_Bindings_Source|false Source when the registration was successful, or `false` on failure. | ||
| */ | ||
|
|
@@ -107,7 +133,7 @@ public function register( string $source_name, array $source_properties ) { | |
| return false; | ||
| } | ||
|
|
||
| /* Validate that the source properties contain the label */ | ||
| // Validate that the source properties contain the label. | ||
| if ( ! isset( $source_properties['label'] ) ) { | ||
| _doing_it_wrong( | ||
| __METHOD__, | ||
|
|
@@ -117,7 +143,7 @@ public function register( string $source_name, array $source_properties ) { | |
| return false; | ||
| } | ||
|
|
||
| /* Validate that the source properties contain the get_value_callback */ | ||
| // Validate that the source properties contain the get_value_callback. | ||
| if ( ! isset( $source_properties['get_value_callback'] ) ) { | ||
| _doing_it_wrong( | ||
| __METHOD__, | ||
|
|
@@ -127,7 +153,7 @@ public function register( string $source_name, array $source_properties ) { | |
| return false; | ||
| } | ||
|
|
||
| /* Validate that the get_value_callback is a valid callback */ | ||
| // Validate that the get_value_callback is a valid callback. | ||
| if ( ! is_callable( $source_properties['get_value_callback'] ) ) { | ||
| _doing_it_wrong( | ||
| __METHOD__, | ||
|
|
@@ -137,13 +163,46 @@ public function register( string $source_name, array $source_properties ) { | |
| return false; | ||
| } | ||
|
|
||
| // Validate that the uses_context parameter is an array. | ||
| if ( isset( $source_properties['uses_context'] ) && ! is_array( $source_properties['uses_context'] ) ) { | ||
| _doing_it_wrong( | ||
| __METHOD__, | ||
| __( 'The "uses_context" parameter must be an array.' ), | ||
| '6.5.0' | ||
| ); | ||
| return false; | ||
| } | ||
|
|
||
| if ( ! empty( array_diff( array_keys( $source_properties ), $this->allowed_source_properties ) ) ) { | ||
| _doing_it_wrong( | ||
| __METHOD__, | ||
| __( 'The $source_properties array contains invalid properties.' ), | ||
| '6.5.0' | ||
| ); | ||
| return false; | ||
| } | ||
|
|
||
| $source = new WP_Block_Bindings_Source( | ||
| $source_name, | ||
| $source_properties | ||
| ); | ||
|
|
||
| $this->sources[ $source_name ] = $source; | ||
|
|
||
| // Add `uses_context` defined by block bindings sources. | ||
| add_filter( | ||
| 'get_block_type_uses_context', | ||
| function ( $uses_context, $block_type ) use ( $source ) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One more thing here and not a blocker. I believe we should use a class method and unregister the filter when unregistering the source. It would also help with documenting, as we could put all details from inline comments in the PHPDoc. I'm not entirely sure how that would look in practice, but happy to explore together options. It can be another patch.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This makes sense to me. Although I don't know how it will work 🤔 I was thinking something like this: /**
* Source that is being processed.
*
* @since 6.5.0
* @var WP_Block_Bindings_Source|false
*/
private $source;
/**
* Callback passed to the `get_block_type_uses_context` filter to add the source's `uses_context` to the block.
*
* @since 6.5.0
*
* @param array $uses_context Array of registered uses context for a block type.
* @param WP_Block_Type $block_type The full block type object.
* @return array Modified array of uses_context with the values provided by the source.
*/
public function _add_source_uses_context( $uses_context, $block_type ) {
$source = $this->source;
if ( ! in_array( $block_type->name, $this->supported_blocks, true ) || empty( $source->uses_context ) ) {
return $uses_context;
}
// Use array_values to reset the array keys.
return array_values( array_unique( array_merge( $uses_context, $source->uses_context ) ) );
}But we probably need a different callback name per source. Anyway, I'll add a task in the tracking issue to handle it 🙂 |
||
| if ( ! in_array( $block_type->name, $this->supported_blocks, true ) || empty( $source->uses_context ) ) { | ||
| return $uses_context; | ||
| } | ||
| // Use array_values to reset the array keys. | ||
| return array_values( array_unique( array_merge( $uses_context, $source->uses_context ) ) ); | ||
| }, | ||
| 10, | ||
| 2 | ||
| ); | ||
|
|
||
| return $source; | ||
| } | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.