-
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 Bindings API: Refactor logic into Block Bindings class and singleton pattern #57742
Merged
+148
−70
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
236b4e6
Move bindings logic to Block Bindings class
artemiomorales 6e1eb1c
Remove erroneous echo statement that was breaking UI
artemiomorales 37e8dbd
Fix error in registering block bindings sources
artemiomorales 27cfb49
Add missing return statement
artemiomorales 892aa17
Add docblocks
artemiomorales 1319d1e
Remove obsolete file
artemiomorales c0e4407
Fix docblock
artemiomorales d596b23
Sync register_source docblock
artemiomorales e607d95
Remove erroneous subpackage declaration
artemiomorales 0a317f1
Update package name
artemiomorales aafcc0f
Fix gutenberg package declarations
artemiomorales 7252c01
Remove extraneous comments
artemiomorales d7e1c93
Move allowed_blocks property to filter function
artemiomorales aa566cc
Update description of Block Bindings class
artemiomorales 9c8bf55
Address PHPCS spacing issue
artemiomorales e6473a7
Rename function call and call using string in filter
artemiomorales 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,69 @@ | ||
<?php | ||
/** | ||
* Block Bindings API | ||
* | ||
* This file contains functions for managing block bindings in WordPress. | ||
* | ||
* @since 17.6.0 | ||
* @package gutenberg | ||
*/ | ||
|
||
/** | ||
* Retrieves the singleton instance of WP_Block_Bindings. | ||
* | ||
* @return WP_Block_Bindings The WP_Block_Bindings instance. | ||
*/ | ||
if ( ! function_exists( 'wp_block_bindings' ) ) { | ||
function wp_block_bindings() { | ||
static $instance = null; | ||
if ( is_null( $instance ) ) { | ||
$instance = new WP_Block_Bindings(); | ||
} | ||
return $instance; | ||
} | ||
} | ||
|
||
/** | ||
* Registers a new source for block bindings. | ||
* | ||
* @param string $source_name The name of the source. | ||
* @param string $label The label of the source. | ||
* @param callable $apply The callback executed when the source is processed during block rendering. The callable should have the following signature: | ||
* function (object $source_attrs, object $block_instance, string $attribute_name): string | ||
* - object $source_attrs: Object containing source ID used to look up the override value, i.e. {"value": "{ID}"}. | ||
* - object $block_instance: The block instance. | ||
* - string $attribute_name: The name of an attribute used to retrieve an override value from the block context. | ||
* The callable should return a string that will be used to override the block's original value. | ||
* @return void | ||
*/ | ||
if ( ! function_exists( 'wp_block_bindings_register_source' ) ) { | ||
function wp_block_bindings_register_source( $source_name, $label, $apply ) { | ||
wp_block_bindings()->register_source( $source_name, $label, $apply ); | ||
} | ||
} | ||
|
||
/** | ||
* Retrieves the list of registered block sources. | ||
* | ||
* @return array The list of registered block sources. | ||
*/ | ||
if ( ! function_exists( 'wp_block_bindings_get_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. | ||
* | ||
* @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. | ||
*/ | ||
if ( ! function_exists( 'wp_block_bindings_replace_html' ) ) { | ||
function wp_block_bindings_replace_html( $block_content, $block_name, $block_attr, $source_value ) { | ||
return wp_block_bindings()->replace_html( $block_content, $block_name, $block_attr, $source_value ); | ||
} | ||
} |
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 was deleted.
Oops, something went wrong.
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
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.
We have a similar check in
block-supports/pattern.php
as seen here: https://github.com/WordPress/gutenberg/pull/57789/files#r1452055620. Do you think we should duplicate this array there? Or should we move this into a sharable JSON config so that it can also be imported by JS? Not sure if it's worth the effort though since it's supposed to be a temporary solution.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.
I think question number one is whether these two lists should always match. Eventually, it should be possible to use block bindings with all blocks and I expect to see a method to opt in for that. The prerequisite is the full support in HTML API to replace HTML tags in the saved content.
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.
Hmm, where would we put the JSON config? My initial reaction is that it's not worth the effort because this is a temporary solution. The temporary solution may be in place for a while, though.
If we do add a JSON config, that means we'd need to make sure support for every block works for both regular blocks and patterns moving forward. To @gziolo's point, that would require more coordination and perhaps may be less productive at this stage.
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, I was basically tossing out three different solutions above 😅:
$allowed_blocks
config inblock-supports/pattern.php
.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.
I think duplicating it is ok for now.
I've put together a PR that does that - Update pattern overrides to use a hard coded support array
It should ensure pattern overrides don't break when this PR is merged (I think this PR is currently a little bit behind trunk).