-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Block Bindings API: Refactor logic into Block Bindings class and sing…
…leton pattern (#57742) * Move bindings logic to Block Bindings class * Remove erroneous echo statement that was breaking UI * Fix error in registering block bindings sources * Add missing return statement * Add docblocks * Remove obsolete file * Fix docblock * Sync register_source docblock * Remove erroneous subpackage declaration * Update package name * Fix gutenberg package declarations * Remove extraneous comments * Move allowed_blocks property to filter function * Update description of Block Bindings class * Address PHPCS spacing issue * Rename function call and call using string in filter
- Loading branch information
1 parent
0b9d8ae
commit 81c3c96
Showing
7 changed files
with
148 additions
and
70 deletions.
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