Skip to content
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

Add the Block Bindings API #5888

Closed
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
76 commits
Select commit Hold shift + click to select a range
8720e12
Add the Block Bindings API
michalczaplinski Jan 17, 2024
8891169
Add tests
artemiomorales Jan 17, 2024
eac5b22
Move tests to different directory
artemiomorales Jan 17, 2024
39e52ed
Fix bug wherein block content was erased
artemiomorales Jan 17, 2024
82e70ff
Update comment format for consistency
artemiomorales Jan 17, 2024
6486471
Move process_block_bindings_function to class-wp-block
michalczaplinski Jan 17, 2024
048330e
WordPress capitalization dangit
michalczaplinski Jan 17, 2024
4a4b825
PHPDoc formatting
michalczaplinski Jan 18, 2024
9a662e4
Add a better PHPDoc
michalczaplinski Jan 18, 2024
c92b6ac
Add a new unit test covering the _process_block_bindings() function
michalczaplinski Jan 18, 2024
abb56fb
More fixes for formatting, capitalization and PHPDocs
michalczaplinski Jan 18, 2024
e8b1195
Add @since to block-bindings.php
michalczaplinski Jan 18, 2024
cc6c2c2
Add version and since tags to class and methods
michalczaplinski Jan 18, 2024
206b6b7
Add a comment on the block class
michalczaplinski Jan 18, 2024
dc8377a
Update assertions in WP_Block_Bindings_Test class
michalczaplinski Jan 18, 2024
751a459
Update block binding registration function comment
michalczaplinski Jan 18, 2024
423bb66
Change the signature of `wp_block_bindings_register_source` & WP_Bloc…
michalczaplinski Jan 23, 2024
d1b0d2e
Add type hints for `register_source()` & `replace_html()`
michalczaplinski Jan 23, 2024
9f2274d
Add the missing array keys
michalczaplinski Jan 23, 2024
02f0ea3
Apply https://github.com/WordPress/gutenberg/pull/58055/
michalczaplinski Jan 23, 2024
9303e02
Make processing of bindings private to the class
artemiomorales Jan 25, 2024
30f4ed7
Update class name and method names
artemiomorales Jan 25, 2024
940fb82
Remove the `wp_block_bindings()` function and create a static get_ins…
michalczaplinski Jan 25, 2024
dbda027
Rename `register_block_bindings_source()` to `register()`
michalczaplinski Jan 25, 2024
34d9dd7
Move the process_block_bindings & replace_html methods to the WP_bloc…
michalczaplinski Jan 25, 2024
74bfae7
Rename WP_Block_Bindings to WP_Block_Bindings_Registry in missing places
michalczaplinski Jan 25, 2024
013535e
Rename class-wp-block-bindings.php to class-wp-block-bindings-registr…
michalczaplinski Jan 25, 2024
89e9833
Rename $source_args to $source_properties to align with other similar…
michalczaplinski Jan 25, 2024
0c931e0
Add `is_registered()` & `get_registered()`
michalczaplinski Jan 25, 2024
7b1ebe2
Register block bindings on 'init' action hook
michalczaplinski Jan 25, 2024
b0c300f
Merge branch 'trunk' into feature/block-bindings-api
michalczaplinski Jan 25, 2024
e0b3883
Fix incorrect loading of block bindings class
artemiomorales Jan 26, 2024
2900bf5
Add validation to prevent sources from being registered twice
artemiomorales Jan 26, 2024
26da23e
Add 'core' namespace to built-in sources
artemiomorales Jan 26, 2024
29213d8
Fix incorrect reading of sources
artemiomorales Jan 26, 2024
4c3d892
Rename 'apply' to 'get_value_callback'
artemiomorales Jan 26, 2024
06a3930
Fix incorrect reading of registered sources
artemiomorales Jan 26, 2024
f53121d
Ensure unauthorized users don't read post meta
artemiomorales Jan 26, 2024
5b8eb4a
Fix PHPCS errors
artemiomorales Jan 26, 2024
45a96f8
Rename sources using naming pattern for block types
artemiomorales Jan 26, 2024
08ddb0a
Add context to translation
artemiomorales Jan 26, 2024
3a16941
Update comments and revise to use proper array annotation
artemiomorales Jan 26, 2024
1729f65
Update callback comment with return type details; fix PHPCS
artemiomorales Jan 26, 2024
9ea2c9a
Code quality improvements for the Block Bindings Registry
gziolo Jan 29, 2024
fd090ab
Ensure that code comments fit into the line length limit
gziolo Jan 29, 2024
6ea69cd
Updates to the test
michalczaplinski Jan 25, 2024
15dea88
Merge branch 'trunk' into feature/block-bindings-api
michalczaplinski Jan 29, 2024
5893867
Remove the files already committed in #5965 and #5966.
michalczaplinski Jan 29, 2024
d10b165
Update block bindings function call in class-wp-block.php
michalczaplinski Jan 29, 2024
c324e65
Add set_up to block-bindings tests
michalczaplinski Jan 29, 2024
a0d550a
Refactor block bindings tests
michalczaplinski Jan 29, 2024
3f242d3
Fix block bindings registration unit test and test setup
michalczaplinski Jan 29, 2024
d202e5d
Move tests to block-bindings directory
michalczaplinski Jan 30, 2024
2a20dee
Remove unnecessary attributes from the test
michalczaplinski Jan 30, 2024
2b4cf33
Incorporate PHP changes from https://github.com/WordPress/gutenberg/p…
michalczaplinski Jan 30, 2024
8604e82
Fix binding source arguments in WP_Block class
michalczaplinski Jan 30, 2024
65a7c09
Test passing arguments to the source
michalczaplinski Jan 30, 2024
1ceef1b
Undo the changes in register.php
michalczaplinski Jan 30, 2024
3d97457
Merge branch 'trunk' into feature/block-bindings-api
michalczaplinski Jan 30, 2024
5f397aa
Update the docstring for `process_block_bindings`
michalczaplinski Jan 30, 2024
f2a611b
Update source_name parameter documentation in block-bindings.php and …
michalczaplinski Jan 30, 2024
a1e1bac
Refactor post_meta_source_callback function to use is_post_publicly_v…
michalczaplinski Jan 30, 2024
26571a7
Remove unnecessary condition for unregistering block bindings sources
michalczaplinski Jan 30, 2024
d189f50
Fix access control logic in post_meta_source_callback function
michalczaplinski Jan 30, 2024
d7201ce
Optimize call to post_password_required()
artemiomorales Jan 30, 2024
69ed754
Update pattern source label and PHPDoc string.
michalczaplinski Jan 31, 2024
951ca16
Merge branch 'trunk' into feature/block-bindings-api
michalczaplinski Jan 31, 2024
f10a4fe
Update block bindings source labels
michalczaplinski Jan 31, 2024
0bec833
Wrap the block binding registrations with 'init' actions
michalczaplinski Feb 1, 2024
3134faa
Use $this->block_type directly in replace_html method
michalczaplinski Feb 1, 2024
9491be3
Mark functions passed to init actions in block binding sources as pri…
michalczaplinski Feb 1, 2024
c95b92f
Prefix block binding function names with `_` to mark as private
michalczaplinski Feb 1, 2024
3619e18
Update the function names again
michalczaplinski Feb 1, 2024
c8232ac
Prevent pattern and post meta sources from registering in unit tests
michalczaplinski Feb 1, 2024
871cedc
Use `call_user_func_array` to call the source binding callback in WP_…
michalczaplinski Feb 1, 2024
604f7a2
Validate that `$block['attrs']['metadata']['bindings']` is an array i…
michalczaplinski Feb 1, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
78 changes: 78 additions & 0 deletions src/wp-includes/block-bindings/block-bindings.php
@@ -0,0 +1,78 @@
<?php
/**
* Block Bindings API
*
* This file contains functions for managing block bindings in WordPress.
*
* @since 6.5.0
* @package WordPress
*/

/**
* Retrieves the singleton instance of WP_Block_Bindings.
*
* @since 6.5.0
*
* @return WP_Block_Bindings The WP_Block_Bindings instance.
*/
function wp_block_bindings() {
static $instance = null;
if ( is_null( $instance ) ) {
$instance = new WP_Block_Bindings();
}
return $instance;
}
michalczaplinski marked this conversation as resolved.
Show resolved Hide resolved


/**
* Registers a new source for block bindings.
*
* @since 6.5.0
*
* @param string $source_name The name of the source.
* @param array $source_args The array of arguments that are used to register a source. The array has two elements:
* 1. string $label The label of the source.
* 2. callback $apply A callback
* executed when the source is processed during
* block rendering. The callback should have the
* following signature:
*
* `function (object $source_attrs, object $block_instance, string $attribute_name): string`
* - @param object $source_attrs: Object containing source ID used to look up the override value, i.e. {"value": "{ID}"}.
* - @param object $block_instance: The block instance.
* - @param string $attribute_name: The name of an attribute used to retrieve an override value from the block context.
* The callback should return a string that will be used to override the block's original value.
*
* @return void
*/
function wp_block_bindings_register_source( string $source_name, array $source_args ) {
wp_block_bindings()->register_source( $source_name, $source_args );
}


/**
* Retrieves the list of registered block sources.
*
* @since 6.5.0
*
* @return array The list of registered block sources.
*/
function wp_block_bindings_get_sources() {
return wp_block_bindings()->get_sources();
}


/**
* Replaces the HTML content of a block based on the provided source value.
*
* @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.
* @return string The modified block content.
*/
function wp_block_bindings_replace_html( string $block_content, string $block_name, string $block_attr, string $source_value ) {
return wp_block_bindings()->replace_html( $block_content, $block_name, $block_attr, $source_value );
}
171 changes: 171 additions & 0 deletions src/wp-includes/block-bindings/class-wp-block-bindings.php
@@ -0,0 +1,171 @@
<?php
michalczaplinski marked this conversation as resolved.
Show resolved Hide resolved
/**
* Block Bindings API: WP_Block_Bindings class.
*
* Support for overriding content in blocks by connecting them to different sources.
*
* @package WordPress
* @subpackage Block Bindings
*/

/**
* Core class used to define supported blocks, register sources, and populate HTML with content from those sources.
*
* @since 6.5.0
*/
class WP_Block_Bindings {

/**
* Holds the registered block bindings sources, keyed by source identifier.
*
* @since 6.5.0
*
* @var array
*/
private $sources = array();

/**
* Function to register a new block binding source.
*
* Sources are used to override block's original attributes with a value
* coming from the source. Once a source is registered, it can be used by a
* block by setting its `metadata.bindings` attribute to a value that refers
* to the source.
*
* @since 6.5.0
*
* @param string $source_name The name of the source.
* @param array $source_args The array of arguments that are used to register a source. The array has two elements:
michalczaplinski marked this conversation as resolved.
Show resolved Hide resolved
* 1. string $label The label of the source.
* 2. callback $apply A callback
gziolo marked this conversation as resolved.
Show resolved Hide resolved
* executed when the source is processed during
* block rendering. The callback should have the
* following signature:
*
* `function (object $source_attrs, object $block_instance, string $attribute_name): string`
* - @param object $source_attrs: Object containing source ID used to look up the override value, i.e. {"value": "{ID}"}.
* - @param object $block_instance: The block instance.
* - @param string $attribute_name: The name of an attribute used to retrieve an override value from the block context.
* The callback should return a string that will be used to override the block's original value.
gziolo marked this conversation as resolved.
Show resolved Hide resolved
*
* @return void
*/
public function register_source( string $source_name, array $source_args ) {
$this->sources[ $source_name ] = $source_args;
}

/**
* 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.
*/
public function replace_html( string $block_content, string $block_name, string $block_attr, string $source_value ) {
michalczaplinski marked this conversation as resolved.
Show resolved Hide resolved
$block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block_name );
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(
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' );
}
}
$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 ) );
return $amended_content->get_updated_html();
break;

default:
return $block_content;
break;
}
return;
}

/**
* Retrieves the list of registered block sources.
*
* @since 6.5.0
*
* @return array The array of registered sources.
*/
public function get_sources() {
return $this->sources;
}
}
23 changes: 23 additions & 0 deletions src/wp-includes/block-bindings/sources/pattern.php
@@ -0,0 +1,23 @@
<?php
/**
* The "pattern" source for the Block Bindings API. This source is used by the
* Partially Synced Patterns.
michalczaplinski marked this conversation as resolved.
Show resolved Hide resolved
*
* @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 );
}

wp_block_bindings_register_source(
michalczaplinski marked this conversation as resolved.
Show resolved Hide resolved
'pattern_attributes',
array(
'label' => __( 'Pattern Attributes' ),
'apply' => 'pattern_source_callback',
)
);
26 changes: 26 additions & 0 deletions src/wp-includes/block-bindings/sources/post-meta.php
@@ -0,0 +1,26 @@
<?php
/**
* Add the post_meta source to the Block Bindings API.
*
* @since 6.5.0
* @package WordPress
*/
function post_meta_source_callback( $source_attrs ) {
youknowriad marked this conversation as resolved.
Show resolved Hide resolved
// 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'];
Copy link
Contributor

Choose a reason for hiding this comment

The 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?

Copy link
Member

Choose a reason for hiding this comment

The 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 $block_instance param.

Choose a reason for hiding this comment

The 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
Copy link
Member

Choose a reason for hiding this comment

The 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 get_the_ID.

Something like this:

$post_id = isset( $block_instance->context['postId'] ) ? $block_instance->context['postId'] : get_the_ID();

Copy link
Contributor

Choose a reason for hiding this comment

The 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 get_the_ID if there's no parent query block.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From what I tested, $block_instance->context is an empty array for the heading, button, and image block, so that's why we couldn't get the postId from there. Is that not expected?

We could definitely use the conditional suggested. However, I'd like to understand if just using $block_instance->context should work.

Copy link
Contributor

Choose a reason for hiding this comment

The 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.

}

Copy link

Choose a reason for hiding this comment

The 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.

if ( ! current_user_can( 'read_post', $post_id ) || post_password_required( $post_id ) ) {
	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.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if ( ! current_user_can( 'read_post', $post_id ) || post_password_required( $post_id ) ) {
return '';
}

Copy link

Choose a reason for hiding this comment

The 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.

Copy link

Choose a reason for hiding this comment

The 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)

Choose a reason for hiding this comment

The 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

return get_post_meta( $post_id, $source_attrs['value'], true );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at this here I would love it if the function in core could already take care of looking up the post_id in the current context.

I cannot think of any instance where I wouldn't want to get the ID of the current queried post within a query loop for example. So this code that is shown here will be needed in 100% of the cases where this API is used and feels error prone. If we could make it so that the callback is already getting passed the current post id :)

Copy link

@lgladdy lgladdy Jan 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we also pass the result through a filter here, passing through the post ID, and source_attrs['value'].

This could be useful for ACF to hydrate a raw post meta value into it's formatted value which users would expect.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should pass this through filters, this is its own abstractions if a third-party plugin wants its own kind of values, they can implement their own custom source.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fabiankaegy I'm not sure I understand the comment, can you clarify a bit?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@youknowriad What I mean is that I would love it if the wp_block_bindings_register_source function already passed in the current post ID as a parameter of the callback function.

Having to manually do the check to see if $block_instance->context['postId'] is available and if so using it vs. accessing the id via the get_the_ID function is something that every single block binding source will need to do if it should work correctly in all context such as inside query loops.

I would like it if the callback could be simplified to:

 /**
  * Add the post_meta source to the Block Bindings API.
  *
  * @since 6.5.0
  * @package WordPress
  */
 function post_meta_source_callback( $source_attrs, $post_id ) {
 	// Override the current post id when an attribute called `postId` exists 
 	if ( isset( $source_attrs['postId'] ) ) {
 		$post_id = $source_attrs['postId'];
 	}

 	return get_post_meta( $post_id, $source_attrs['value'], true );
}

If I'm not mistaken there even is a bug in this implementation here. Because it doesn't use the context value at all. It should instead check for the availability of the context and if so use that over get_the_ID. And because of that very reason I think ideally core would already handle that check for context and pass the correct post id as a parameter to the callback.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally it doesn't make sense to me. The sources can be anything, from posts to a random database table, to a call to some remote API... I don't think the signature should accommodate a specific source over any other.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But don't sources always need to know which context they are rendered within?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’m still not sold on the name source, because it implies that the value is sourced from something external. In practice, the value used for replacement could be for example :

  • current date
  • translation
  • hardcoded value

Overall, we are opening an API to dynamically compute the value of the attribute on the server so people don't need to default to custom and dynamic blocks.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That context actually is helpful! In my mind, I was really mostly concerned with dynamic attributes on the post object / external API connections that need to be aware of the current context which is why I was so focussed on the Post ID.

In that case an unrelated side note, I would love it if simply all blocks anywhere had the usesContext: [ 'postId' ] specified because the whole accessing the correct ID is an error-prone thing that often gets overlooked and the a feature doesn't work correctly in query loops etc

}

wp_block_bindings_register_source(
'post_meta',
array(
'label' => __( 'Post Meta' ),
michalczaplinski marked this conversation as resolved.
Show resolved Hide resolved
'apply' => 'post_meta_source_callback',
),

Check failure on line 25 in src/wp-includes/block-bindings/sources/post-meta.php

View workflow job for this annotation

GitHub Actions / Check PHP compatibility

Trailing comma's are not allowed in function calls in PHP 7.2 or earlier
);