-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Merged
Changes from 4 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,43 @@ | ||
<?php | ||
/** | ||
* Block Bindings API | ||
* | ||
* @since 6.5.0 | ||
* | ||
* @package WordPress | ||
* @subpackage Script Modules | ||
*/ | ||
|
||
if ( ! function_exists( 'wp_block_bindings' ) ) { | ||
function wp_block_bindings() { | ||
static $instance = null; | ||
if ( is_null( $instance ) ) { | ||
$instance = new WP_Block_Bindings(); | ||
} | ||
return $instance; | ||
} | ||
} | ||
|
||
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 ); | ||
} | ||
} | ||
|
||
if ( ! function_exists( 'wp_block_bindings_get_allowed_blocks' ) ) { | ||
function wp_block_bindings_get_allowed_blocks() { | ||
return wp_block_bindings()->get_allowed_blocks(); | ||
} | ||
} | ||
|
||
if ( ! function_exists( 'wp_block_bindings_get_sources' ) ) { | ||
function wp_block_bindings_get_sources() { | ||
return wp_block_bindings()->get_sources(); | ||
} | ||
} | ||
|
||
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 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.
Just thinking about a future where this mechanism may change (it becomes an opt-in or opt-out, or we support so many core blocks that it becomes easier to list the ones that aren't supported), and ways that the method could be future proofed.
Maybe changing it to something like
get_is_block_type_supported( $block_type )
could be a way to future proof it. That would prevent leaking theallowed_blocks
array into public code and allow us to change the way it's implemented while keeping the interface the same.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 definitely agree with the rationale. What do you think about going even further and making it an implementation detail of the function that takes care of computing the value of the attributes that have a corresponding custom source binding? If there are no attribute changes then there is nothing much to process in the block content's HTML.
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 what Dan suggests makes total sense. The
allowed_blocks
property is alreadyprivate
so if we use a function likeget_is_block_type_supported( $block_type )
instead it should be enough to "future proof" it.I'm not sure I follow your suggestion @gziolo though. Could you elaborate?
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.
If we go with this, we should also update the wrapper function in:
gutenberg/lib/experimental/block-bindings/block-bindings.php
Lines 50 to 54 in 7252c01
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.
What I meant is that with more refactoring, you can make the list of allowed blocks and implementation details of
process_block_bindings
. The only other place where the same list is currently needed is:However, I don't see any reason why it couldn't become:
The same check would happen in the filter instead before it starts any processing:
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.
Thanks Greg! Yeah, that makes sense to me. 👍
There's no need to expose
wp_block_bindings_get_allowed_blocks()
as public API so we can remove it as well as remove the internal$this->get_allowed_blocks()
method.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.
Ok I moved the $allowed_blocks property to the filter function and removed the loop 👍
Perhaps the motivation of the original code was to prevent running the filter function on every block on the page?
In any case, I think what I've revised here addresses the issue. Let me know what you think!