mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-10-04 14:47:02 +00:00
Editor: Add the Block Bindings API.
This introduces the Block Bindings API for WordPress. The API allows developers to connects block attributes to different sources. In this PR, two such sources are included: "post meta" and "pattern". Attributes connected to sources can have their HTML replaced by values coming from the source in a way defined by the binding. Props czapla, lgladdy, gziolo, sc0ttkclark, swissspidy, artemiosans, kevin940726, fabiankaegy, santosguillamot, talldanwp, wildworks. Fixes #60282. git-svn-id: https://develop.svn.wordpress.org/trunk@57514 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -339,10 +339,15 @@ tests_add_filter( 'send_auth_cookies', '__return_false' );
|
||||
* @since 5.0.0
|
||||
*/
|
||||
function _unhook_block_registration() {
|
||||
// Block types.
|
||||
require __DIR__ . '/unregister-blocks-hooks.php';
|
||||
remove_action( 'init', 'register_core_block_types_from_metadata' );
|
||||
remove_action( 'init', 'register_block_core_legacy_widget' );
|
||||
remove_action( 'init', 'register_block_core_widget_group' );
|
||||
remove_action( 'init', 'register_core_block_types_from_metadata' );
|
||||
|
||||
// Block binding sources.
|
||||
remove_action( 'init', '_register_block_bindings_pattern_overrides_source' );
|
||||
remove_action( 'init', '_register_block_bindings_post_meta_source' );
|
||||
}
|
||||
tests_add_filter( 'init', '_unhook_block_registration', 1000 );
|
||||
|
||||
@@ -0,0 +1,106 @@
|
||||
<?php
|
||||
/**
|
||||
* Unit tests covering the Block Bindings public API.
|
||||
*
|
||||
* @package WordPress
|
||||
* @subpackage Blocks
|
||||
* @since 6.5.0
|
||||
*
|
||||
* @group blocks
|
||||
* @group block-bindings
|
||||
*
|
||||
* @covers register_block_bindings_source
|
||||
*/
|
||||
class WP_Block_Bindings_Test extends WP_UnitTestCase {
|
||||
|
||||
const SOURCE_NAME = 'test/source';
|
||||
const SOURCE_LABEL = array(
|
||||
'label' => 'Test source',
|
||||
);
|
||||
|
||||
/**
|
||||
* Set up before each test.
|
||||
*
|
||||
* @since 6.5.0
|
||||
*/
|
||||
public function set_up() {
|
||||
foreach ( get_all_registered_block_bindings_sources() as $source_name => $source_properties ) {
|
||||
if ( str_starts_with( $source_name, 'test/' ) ) {
|
||||
unregister_block_bindings_source( $source_name );
|
||||
}
|
||||
}
|
||||
|
||||
parent::set_up();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test if the block content is updated with the value returned by the source.
|
||||
*
|
||||
* @ticket 60282
|
||||
*
|
||||
* @covers register_block_bindings_source
|
||||
*/
|
||||
public function test_update_block_with_value_from_source() {
|
||||
$get_value_callback = function () {
|
||||
return 'test source value';
|
||||
};
|
||||
|
||||
register_block_bindings_source(
|
||||
self::SOURCE_NAME,
|
||||
array(
|
||||
'label' => self::SOURCE_LABEL,
|
||||
'get_value_callback' => $get_value_callback,
|
||||
)
|
||||
);
|
||||
|
||||
$block_content = <<<HTML
|
||||
<!-- wp:paragraph {"metadata":{"bindings":{"content":{"source":"test/source"}}}} --><p>This should not appear</p><!-- /wp:paragraph -->
|
||||
HTML;
|
||||
|
||||
$parsed_blocks = parse_blocks( $block_content );
|
||||
$block = new WP_Block( $parsed_blocks[0] );
|
||||
|
||||
$expected = '<p>test source value</p>';
|
||||
$result = $block->render();
|
||||
|
||||
// Check if the block content was updated correctly.
|
||||
$this->assertEquals( $expected, $result, 'The block content should be updated with the value returned by the source.' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test passing arguments to the source.
|
||||
*
|
||||
* @ticket 60282
|
||||
*
|
||||
* @covers register_block_bindings_source
|
||||
*/
|
||||
public function test_passing_arguments_to_source() {
|
||||
$get_value_callback = function ( $source_args, $block_instance, $attribute_name ) {
|
||||
$value = $source_args['key'];
|
||||
return "The attribute name is '$attribute_name' and its binding has argument 'key' with value '$value'.";
|
||||
};
|
||||
|
||||
register_block_bindings_source(
|
||||
self::SOURCE_NAME,
|
||||
array(
|
||||
'label' => self::SOURCE_LABEL,
|
||||
'get_value_callback' => $get_value_callback,
|
||||
)
|
||||
);
|
||||
|
||||
$key = 'test';
|
||||
|
||||
$block_content = <<<HTML
|
||||
<!-- wp:paragraph {"metadata":{"bindings":{"content":{"source":"test/source", "args": {"key": "$key"}}}}} --><p>This should not appear</p><!-- /wp:paragraph -->
|
||||
HTML;
|
||||
|
||||
$parsed_blocks = parse_blocks( $block_content );
|
||||
$block = new WP_Block( $parsed_blocks[0] );
|
||||
|
||||
$expected = "<p>The attribute name is 'content' and its binding has argument 'key' with value 'test'.</p>";
|
||||
$result = $block->render();
|
||||
|
||||
// Check if the block content was updated correctly.
|
||||
$this->assertEquals( $expected, $result, 'The block content should be updated with the value returned by the source.' );
|
||||
}
|
||||
}
|
||||
@@ -16,6 +16,19 @@ class Tests_Block_Bindings_Register extends WP_UnitTestCase {
|
||||
'label' => 'Test source',
|
||||
);
|
||||
|
||||
/**
|
||||
* Set up before each test.
|
||||
*
|
||||
* @since 6.5.0
|
||||
*/
|
||||
public function set_up() {
|
||||
foreach ( get_all_registered_block_bindings_sources() as $source_name => $source_properties ) {
|
||||
unregister_block_bindings_source( $source_name );
|
||||
}
|
||||
|
||||
parent::set_up();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tear down after each test.
|
||||
*
|
||||
@@ -23,9 +36,7 @@ class Tests_Block_Bindings_Register extends WP_UnitTestCase {
|
||||
*/
|
||||
public function tear_down() {
|
||||
foreach ( get_all_registered_block_bindings_sources() as $source_name => $source_properties ) {
|
||||
if ( str_starts_with( $source_name, 'test/' ) ) {
|
||||
unregister_block_bindings_source( $source_name );
|
||||
}
|
||||
unregister_block_bindings_source( $source_name );
|
||||
}
|
||||
|
||||
parent::tear_down();
|
||||
|
||||
Reference in New Issue
Block a user