-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Add the Block Bindings API #5888
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
8720e12
8891169
eac5b22
39e52ed
82e70ff
6486471
048330e
4a4b825
9a662e4
c92b6ac
abb56fb
e8b1195
cc6c2c2
206b6b7
dc8377a
751a459
423bb66
d1b0d2e
9f2274d
02f0ea3
9303e02
30f4ed7
940fb82
dbda027
34d9dd7
74bfae7
013535e
89e9833
0c931e0
7b1ebe2
b0c300f
e0b3883
2900bf5
26da23e
29213d8
4c3d892
06a3930
f53121d
5b8eb4a
45a96f8
08ddb0a
3a16941
1729f65
9ea2c9a
fd090ab
6ea69cd
15dea88
5893867
d10b165
c324e65
a0d550a
3f242d3
d202e5d
2a20dee
2b4cf33
8604e82
65a7c09
1ceef1b
3d97457
5f397aa
f2a611b
a1e1bac
26571a7
d189f50
d7201ce
69ed754
951ca16
f10a4fe
0bec833
3134faa
9491be3
c95b92f
3619e18
c8232ac
871cedc
604f7a2
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 |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| <?php | ||
| /** | ||
| * The "pattern" source for the Block Bindings API. This source is used by the | ||
| * Pattern Overrides. | ||
| * | ||
| * @since 6.5.0 | ||
| * @package WordPress | ||
| */ | ||
| function pattern_source_callback( $source_attrs, $block_instance, $attribute_name ) { | ||
| if ( ! _wp_array_get( $block_instance->attributes, array( 'metadata', 'id' ), false ) ) { | ||
| return null; | ||
| } | ||
| $block_id = $block_instance->attributes['metadata']['id']; | ||
| return _wp_array_get( $block_instance->context, array( 'pattern/overrides', $block_id, $attribute_name ), null ); | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * Registers the "pattern" source for the Block Bindings API. | ||
| * | ||
| * @access private | ||
| * @since 6.5.0 | ||
| */ | ||
| function _register_block_bindings_pattern_overrides_source() { | ||
| register_block_bindings_source( | ||
| 'core/pattern-overrides', | ||
| array( | ||
| 'label' => _x( 'Pattern Overrides', 'block bindings source' ), | ||
| 'get_value_callback' => 'pattern_source_callback', | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| add_action( 'init', '_register_block_bindings_pattern_overrides_source' ); |
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,47 @@ | ||||||||||||||
| <?php | ||||||||||||||
| /** | ||||||||||||||
| * Add the post_meta source to the Block Bindings API. | ||||||||||||||
| * | ||||||||||||||
| * @since 6.5.0 | ||||||||||||||
| * @package WordPress | ||||||||||||||
| */ | ||||||||||||||
| function post_meta_source_callback( $source_attrs ) { | ||||||||||||||
| if ( ! isset( $source_attrs['key'] ) ) { | ||||||||||||||
| return null; | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| // Use the postId attribute if available | ||||||||||||||
gziolo marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||
| if ( isset( $source_attrs['postId'] ) ) { | ||||||||||||||
| $post_id = $source_attrs['postId']; | ||||||||||||||
|
Contributor
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. Do we have use-cases where the post id is an attribute and not something in the context?
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. @SantosGuillamot might know better. I would assume we rather read it from the context or other block attributes through 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. The idea was to allow people to select a specific ID to connect to. So, instead of connecting to the post title of the context, you connect to a specific post title. I added it more as an example of other attributes that could make sense at some point. |
||||||||||||||
| } else { | ||||||||||||||
| // $block_instance->context['postId'] is not available in the Image block. | ||||||||||||||
youknowriad marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||
| $post_id = get_the_ID(); | ||||||||||||||
|
Comment on lines
+17
to
+18
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. I think there might be a bug here. Instead of completely omitting the usage of the $block_instance->context['postId'] here, we should check if the value isset and if so use it over the ID we get from Something like this: $post_id = isset( $block_instance->context['postId'] ) ? $block_instance->context['postId'] : get_the_ID();
Contributor
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. I also wonder if we should always use the context here. as if I'm not wrong the default context should be set as 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. From what I tested, We could definitely use the conditional suggested. However, I'd like to understand if just using
Contributor
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. Maybe we have an issue somewhere but in the client the block context has some "default context values" like the post ID if I'm not wrong, and in the server you're saying that these are not present. IMO, the block context should be the same for the client and the server. I think it's fine if we defer the potential fix to a dedicated issue but we might want to track it somewhere. |
||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
|
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. @SantosGuillamot @gziolo Heads up here -- This is lacking any access rights checks and finding/fixing this now means we avoid having to patch the eventual release. If someone provides any post ID, it could pull meta for any post ID of any post type of any status including password-protected posts. Something like this could help, maybe only in the conditional above where a custom post ID is set but to be safe we could have it here before the return. We may also want to check whether the associated post type is public + publicly_queryable to ensure that we follow the same constraints established by the Query Loop block for dynamically embedding a list posts themselves. 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.
Suggested change
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. In the block editor itself, this logic runs through the REST API which already does this sort of logic. This only impacts the render on the server-side. 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. @sc0ttkclark That makes sense to me. I added the permissions check in f53121d and had to check the post status too to make sure it works as intended. (Pardon the PHPCS errors — those have all been fixed in later commits) 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. Following up on this on the line where this was implemented: https://github.com/WordPress/wordpress-develop/pull/5888/files#r1471516195 |
||||||||||||||
| // If a post isn't public, we need to prevent | ||||||||||||||
| // unauthorized users from accessing the post meta. | ||||||||||||||
| $post = get_post( $post_id ); | ||||||||||||||
| if ( ( ! is_post_publicly_viewable( $post ) && ! current_user_can( 'read_post', $post_id ) ) || post_password_required( $post ) ) { | ||||||||||||||
| return null; | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| return get_post_meta( $post_id, $source_attrs['key'], true ); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| /** | ||||||||||||||
| * Registers the "post_meta" source for the Block Bindings API. | ||||||||||||||
| * | ||||||||||||||
| * @access private | ||||||||||||||
| * @since 6.5.0 | ||||||||||||||
| */ | ||||||||||||||
| function _register_block_bindings_post_meta_source() { | ||||||||||||||
| register_block_bindings_source( | ||||||||||||||
| 'core/post-meta', | ||||||||||||||
| array( | ||||||||||||||
| 'label' => _x( 'Post Meta', 'block bindings source' ), | ||||||||||||||
| 'get_value_callback' => 'post_meta_source_callback', | ||||||||||||||
| ) | ||||||||||||||
| ); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| add_action( 'init', '_register_block_bindings_post_meta_source' ); | ||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -191,6 +191,204 @@ public function __get( $name ) { | |
| return null; | ||
| } | ||
|
|
||
| /** | ||
| * Processes the block bindings in block's attributes. | ||
| * | ||
| * A block might contain bindings in its attributes. Bindings are mappings | ||
| * between an attribute of the block and a source. A "source" is a function | ||
| * registered with `register_block_bindings_source()` that defines how to | ||
| * retrieve a value from outside the block, e.g. from post meta. | ||
| * | ||
| * This function will process those bindings and replace the HTML with the value of the binding. | ||
| * The value is retrieved from the source of the binding. | ||
| * | ||
| * ### Example | ||
| * | ||
| * The "bindings" property for an Image block might look like this: | ||
| * | ||
| * ```json | ||
| * { | ||
| * "metadata": { | ||
| * "bindings": { | ||
| * "title": { | ||
| * "source": "post_meta", | ||
| * "args": { "key": "text_custom_field" } | ||
| * }, | ||
| * "url": { | ||
| * "source": "post_meta", | ||
| * "args": { "key": "url_custom_field" } | ||
| * } | ||
| * } | ||
| * } | ||
| * } | ||
| * ``` | ||
| * | ||
| * The above example will replace the `title` and `url` attributes of the Image | ||
| * block with the values of the `text_custom_field` and `url_custom_field` post meta. | ||
| * | ||
| * @access private | ||
| * @since 6.5.0 | ||
| * | ||
| * @param string $block_content Block content. | ||
| * @param array $block The full block, including name and attributes. | ||
gziolo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| */ | ||
| private function process_block_bindings( $block_content ) { | ||
| $block = $this->parsed_block; | ||
|
|
||
| // Allowed blocks that support block bindings. | ||
| // TODO: Look for a mechanism to opt-in for this. Maybe adding a property to block attributes? | ||
| $allowed_blocks = array( | ||
| 'core/paragraph' => array( 'content' ), | ||
| 'core/heading' => array( 'content' ), | ||
| 'core/image' => array( 'url', 'title', 'alt' ), | ||
| 'core/button' => array( 'url', 'text' ), | ||
| ); | ||
|
|
||
| // If the block doesn't have the bindings property, isn't one of the allowed | ||
| // block types, or the bindings property is not an array, return the block content. | ||
| if ( ! isset( $block['attrs']['metadata']['bindings'] ) || | ||
| ! is_array( $block['attrs']['metadata']['bindings'] ) || | ||
| ! isset( $allowed_blocks[ $this->name ] ) | ||
| ) { | ||
| return $block_content; | ||
| } | ||
|
|
||
| $block_bindings_sources = get_all_registered_block_bindings_sources(); | ||
| $modified_block_content = $block_content; | ||
| foreach ( $block['attrs']['metadata']['bindings'] as $binding_attribute => $binding_source ) { | ||
youknowriad marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // If the attribute is not in the list, process next attribute. | ||
| if ( ! in_array( $binding_attribute, $allowed_blocks[ $this->name ], true ) ) { | ||
| continue; | ||
| } | ||
| // If no source is provided, or that source is not registered, process next attribute. | ||
| if ( ! isset( $binding_source['source'] ) || ! is_string( $binding_source['source'] ) || ! isset( $block_bindings_sources[ $binding_source['source'] ] ) ) { | ||
| continue; | ||
| } | ||
|
|
||
| $source_callback = $block_bindings_sources[ $binding_source['source'] ]['get_value_callback']; | ||
| // Get the value based on the source. | ||
| if ( ! isset( $binding_source['args'] ) ) { | ||
| $source_args = array(); | ||
| } else { | ||
| $source_args = $binding_source['args']; | ||
| } | ||
| $source_value = call_user_func_array( $source_callback, array( $source_args, $this, $binding_attribute ) ); | ||
| // If the value is null, process next attribute. | ||
| if ( is_null( $source_value ) ) { | ||
| continue; | ||
| } | ||
|
|
||
| // Process the HTML based on the block and the attribute. | ||
| $modified_block_content = $this->replace_html( $modified_block_content, $this->name, $binding_attribute, $source_value ); | ||
| } | ||
| return $modified_block_content; | ||
| } | ||
|
|
||
| /** | ||
| * Depending on the block attributes, replace the HTML based on the value returned by the source. | ||
| * | ||
| * @since 6.5.0 | ||
| * | ||
| * @param string $block_content Block content. | ||
| * @param string $block_name The name of the block to process. | ||
| * @param string $block_attr The attribute of the block we want to process. | ||
| * @param string $source_value The value used to replace the HTML. | ||
gziolo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| */ | ||
| private function replace_html( string $block_content, string $block_name, string $block_attr, string $source_value ) { | ||
| $block_type = $this->block_type; | ||
| if ( null === $block_type || ! isset( $block_type->attributes[ $block_attr ] ) ) { | ||
| return $block_content; | ||
| } | ||
|
|
||
| // Depending on the attribute source, the processing will be different. | ||
| switch ( $block_type->attributes[ $block_attr ]['source'] ) { | ||
| case 'html': | ||
| case 'rich-text': | ||
| $block_reader = new WP_HTML_Tag_Processor( $block_content ); | ||
|
|
||
| // TODO: Support for CSS selectors whenever they are ready in the HTML API. | ||
| // In the meantime, support comma-separated selectors by exploding them into an array. | ||
| $selectors = explode( ',', $block_type->attributes[ $block_attr ]['selector'] ); | ||
| // Add a bookmark to the first tag to be able to iterate over the selectors. | ||
| $block_reader->next_tag(); | ||
| $block_reader->set_bookmark( 'iterate-selectors' ); | ||
|
|
||
| // TODO: This shouldn't be needed when the `set_inner_html` function is ready. | ||
| // Store the parent tag and its attributes to be able to restore them later in the button. | ||
| // The button block has a wrapper while the paragraph and heading blocks don't. | ||
| if ( 'core/button' === $block_name ) { | ||
| $button_wrapper = $block_reader->get_tag(); | ||
| $button_wrapper_attribute_names = $block_reader->get_attribute_names_with_prefix( '' ); | ||
| $button_wrapper_attrs = array(); | ||
| foreach ( $button_wrapper_attribute_names as $name ) { | ||
| $button_wrapper_attrs[ $name ] = $block_reader->get_attribute( $name ); | ||
| } | ||
| } | ||
|
|
||
| foreach ( $selectors as $selector ) { | ||
| // If the parent tag, or any of its children, matches the selector, replace the HTML. | ||
| if ( strcasecmp( $block_reader->get_tag( $selector ), $selector ) === 0 || $block_reader->next_tag( | ||
|
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.
Contributor
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. There is currently an error being triggered in To reproduce:
Preview the post 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. I believe having something like this should solve the issue: $parent_tag = $block_reader->get_tag();
if ( ! is_null( $parent_tag ) || strcasecmp( $parent_tag, $selector ) === 0 || $block_reader->next_tag(
array(
'tag_name' => $selector,
)
) )
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. when using it would be better to check the result of if ( ! $block_reader->next_tag() ) {
continue; // or whatever control flow is appropriate
} |
||
| array( | ||
| 'tag_name' => $selector, | ||
| ) | ||
| ) ) { | ||
| $block_reader->release_bookmark( 'iterate-selectors' ); | ||
|
|
||
| // TODO: Use `set_inner_html` method whenever it's ready in the HTML API. | ||
| // Until then, it is hardcoded for the paragraph, heading, and button blocks. | ||
| // Store the tag and its attributes to be able to restore them later. | ||
| $selector_attribute_names = $block_reader->get_attribute_names_with_prefix( '' ); | ||
| $selector_attrs = array(); | ||
| foreach ( $selector_attribute_names as $name ) { | ||
| $selector_attrs[ $name ] = $block_reader->get_attribute( $name ); | ||
| } | ||
| $selector_markup = "<$selector>" . wp_kses_post( $source_value ) . "</$selector>"; | ||
| $amended_content = new WP_HTML_Tag_Processor( $selector_markup ); | ||
| $amended_content->next_tag(); | ||
| foreach ( $selector_attrs as $attribute_key => $attribute_value ) { | ||
| $amended_content->set_attribute( $attribute_key, $attribute_value ); | ||
| } | ||
| if ( 'core/paragraph' === $block_name || 'core/heading' === $block_name ) { | ||
| return $amended_content->get_updated_html(); | ||
| } | ||
| if ( 'core/button' === $block_name ) { | ||
| $button_markup = "<$button_wrapper>{$amended_content->get_updated_html()}</$button_wrapper>"; | ||
| $amended_button = new WP_HTML_Tag_Processor( $button_markup ); | ||
| $amended_button->next_tag(); | ||
| foreach ( $button_wrapper_attrs as $attribute_key => $attribute_value ) { | ||
| $amended_button->set_attribute( $attribute_key, $attribute_value ); | ||
| } | ||
| return $amended_button->get_updated_html(); | ||
| } | ||
| } else { | ||
| $block_reader->seek( 'iterate-selectors' ); | ||
|
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.
I think there's currently a bug in $p = new WP_HTML_Tag_Processor( '<h2></h2>' );
$p->next_tag();
$p->set_bookmark( 'bookmark' );
$p->next_tag();
var_dump( $p->seek( 'bookmark' ) );
var_dump( $p->get_tag() );For instance, I expect the above snippet to output
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. @kevin940726, what was the user interaction, and what HTML was saved for the block that triggered the issue? It's a great opportunity to add a unit test that will help fix the issue.
Contributor
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. I left some further details on the comment above - #5888 (comment)
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. thanks for the find @kevin940726 - pushed out #6021 to fix it.
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. @kevin940726 fixed in |
||
| } | ||
| } | ||
| $block_reader->release_bookmark( 'iterate-selectors' ); | ||
| return $block_content; | ||
|
|
||
| case 'attribute': | ||
| $amended_content = new WP_HTML_Tag_Processor( $block_content ); | ||
| if ( ! $amended_content->next_tag( | ||
| array( | ||
| // TODO: build the query from CSS selector. | ||
| 'tag_name' => $block_type->attributes[ $block_attr ]['selector'], | ||
| ) | ||
| ) ) { | ||
| return $block_content; | ||
| } | ||
| $amended_content->set_attribute( $block_type->attributes[ $block_attr ]['attribute'], esc_attr( $source_value ) ); | ||
michalczaplinski marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return $amended_content->get_updated_html(); | ||
| break; | ||
|
|
||
| default: | ||
| return $block_content; | ||
| break; | ||
| } | ||
| return; | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * Generates the render output for the block. | ||
| * | ||
|
|
@@ -286,6 +484,10 @@ public function render( $options = array() ) { | |
| } | ||
| } | ||
|
|
||
| // Process the block bindings for this block, if any are registered. This | ||
| // will replace the block content with the value from a registered binding source. | ||
| $block_content = $this->process_block_bindings( $block_content ); | ||
|
|
||
| /** | ||
| * Filters the content of a single block. | ||
| * | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.