Editor: Introduce WP_Block_Bindings_Source class

Abstracts the block bindings source array into a well-defined object.

Fixes #60447.
See #60282.
Follow-up [57373].
Props czapla, santosguillamot, gziolo.



git-svn-id: https://develop.svn.wordpress.org/trunk@57562 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Greg Ziółkowski 2024-02-08 08:55:18 +00:00
parent fd8c730d6f
commit f03a263277
7 changed files with 262 additions and 67 deletions

View File

@ -88,7 +88,7 @@
* The callback has a mixed return type; it may return a string to override
* the block's original value, null, false to remove an attribute, etc.
* }
* @return array|false Source when the registration was successful, or `false` on failure.
* @return WP_Block_Bindings_Source|false Source when the registration was successful, or `false` on failure.
*/
function register_block_bindings_source( string $source_name, array $source_properties ) {
return WP_Block_Bindings_Registry::get_instance()->register( $source_name, $source_properties );
@ -100,7 +100,7 @@ function register_block_bindings_source( string $source_name, array $source_prop
* @since 6.5.0
*
* @param string $source_name Block bindings source name including namespace.
* @return array|false The unregistered block bindings source on success and `false` otherwise.
* @return WP_Block_Bindings_Source|false The unregistered block bindings source on success and `false` otherwise.
*/
function unregister_block_bindings_source( string $source_name ) {
return WP_Block_Bindings_Registry::get_instance()->unregister( $source_name );
@ -111,7 +111,7 @@ function unregister_block_bindings_source( string $source_name ) {
*
* @since 6.5.0
*
* @return array The array of registered block bindings sources.
* @return WP_Block_Bindings_Source[] The array of registered block bindings sources.
*/
function get_all_registered_block_bindings_sources() {
return WP_Block_Bindings_Registry::get_instance()->get_all_registered();
@ -123,7 +123,7 @@ function get_all_registered_block_bindings_sources() {
* @since 6.5.0
*
* @param string $source_name The name of the source.
* @return array|null The registered block bindings source, or `null` if it is not registered.
* @return WP_Block_Bindings_Source|null The registered block bindings source, or `null` if it is not registered.
*/
function get_block_bindings_source( string $source_name ) {
return WP_Block_Bindings_Registry::get_instance()->get_registered( $source_name );

View File

@ -20,7 +20,7 @@ final class WP_Block_Bindings_Registry {
* Holds the registered block bindings sources, keyed by source identifier.
*
* @since 6.5.0
* @var array
* @var WP_Block_Bindings_Source[]
*/
private $sources = array();
@ -66,7 +66,7 @@ final class WP_Block_Bindings_Registry {
* The callback has a mixed return type; it may return a string to override
* the block's original value, null, false to remove an attribute, etc.
* }
* @return array|false Source when the registration was successful, or `false` on failure.
* @return WP_Block_Bindings_Source|false Source when the registration was successful, or `false` on failure.
*/
public function register( string $source_name, array $source_properties ) {
if ( ! is_string( $source_name ) ) {
@ -107,8 +107,38 @@ final class WP_Block_Bindings_Registry {
return false;
}
$source = array_merge(
array( 'name' => $source_name ),
/* Validate that the source properties contain the label */
if ( ! isset( $source_properties['label'] ) ) {
_doing_it_wrong(
__METHOD__,
__( 'The $source_properties must contain a "label".' ),
'6.5.0'
);
return false;
}
/* Validate that the source properties contain the get_value_callback */
if ( ! isset( $source_properties['get_value_callback'] ) ) {
_doing_it_wrong(
__METHOD__,
__( 'The $source_properties must contain a "get_value_callback".' ),
'6.5.0'
);
return false;
}
/* Validate that the get_value_callback is a valid callback */
if ( ! is_callable( $source_properties['get_value_callback'] ) ) {
_doing_it_wrong(
__METHOD__,
__( 'The "get_value_callback" parameter must be a valid callback.' ),
'6.5.0'
);
return false;
}
$source = new WP_Block_Bindings_Source(
$source_name,
$source_properties
);
@ -123,7 +153,7 @@ final class WP_Block_Bindings_Registry {
* @since 6.5.0
*
* @param string $source_name Block bindings source name including namespace.
* @return array|false The unregistered block bindings source on success and `false` otherwise.
* @return WP_Block_Bindings_Source|false The unregistered block bindings source on success and `false` otherwise.
*/
public function unregister( string $source_name ) {
if ( ! $this->is_registered( $source_name ) ) {
@ -147,7 +177,7 @@ final class WP_Block_Bindings_Registry {
*
* @since 6.5.0
*
* @return array The array of registered sources.
* @return WP_Block_Bindings_Source[] The array of registered sources.
*/
public function get_all_registered() {
return $this->sources;
@ -159,7 +189,7 @@ final class WP_Block_Bindings_Registry {
* @since 6.5.0
*
* @param string $source_name The name of the source.
* @return array|null The registered block bindings source, or `null` if it is not registered.
* @return WP_Block_Bindings_Source|null The registered block bindings source, or `null` if it is not registered.
*/
public function get_registered( string $source_name ) {
if ( ! $this->is_registered( $source_name ) ) {

View File

@ -0,0 +1,88 @@
<?php
/**
* Block Bindings API: WP_Block_Bindings_Source class.
*
*
* @package WordPress
* @subpackage Block Bindings
* @since 6.5.0
*/
/**
* Class representing block bindings source.
*
* This class is designed for internal use by the Block Bindings registry.
*
* @since 6.5.0
* @access private
*
* @see WP_Block_Bindings_Registry
*/
final class WP_Block_Bindings_Source {
/**
* The name of the source.
*
* @since 6.5.0
* @var string
*/
public $name;
/**
* The label of the source.
*
* @since 6.5.0
* @var string
*/
public $label;
/**
* The function used to get the value from the source.
*
* @since 6.5.0
* @var callable
*/
private $get_value_callback;
/**
* Constructor.
*
* Do not use this constructor directly. Instead, use the
* `WP_Block_Bindings_Registry::register` method or the `register_block_bindings_source` function.
*
* @since 6.5.0
*
* @param string $name The name of the source.
* @param array $source_properties The properties of the source.
*/
public function __construct( string $name, array $source_properties ) {
$this->name = $name;
$this->label = $source_properties['label'];
$this->get_value_callback = $source_properties['get_value_callback'];
}
/**
* Retrieves the value from the source.
*
* @since 6.5.0
*
* @param array $source_args Array containing source arguments used to look up the override value, i.e. {"key": "foo"}.
* @param WP_Block $block_instance The block instance.
* @param string $attribute_name The name of the target attribute.
*
* @return mixed The value of the source.
*/
public function get_value( array $source_args, $block_instance, string $attribute_name ) {
return call_user_func_array( $this->get_value_callback, array( $source_args, $block_instance, $attribute_name ) );
}
/**
* Wakeup magic method.
*
* @since 6.5.0
*/
public function __wakeup() {
throw new \LogicException( __CLASS__ . ' should never be unserialized' );
}
}

View File

@ -270,9 +270,8 @@ class WP_Block {
continue;
}
$source_callback = $block_binding_source['get_value_callback'];
$source_args = ! empty( $block_binding['args'] ) && is_array( $block_binding['args'] ) ? $block_binding['args'] : array();
$source_value = call_user_func_array( $source_callback, array( $source_args, $this, $attribute_name ) );
$source_args = ! empty( $block_binding['args'] ) && is_array( $block_binding['args'] ) ? $block_binding['args'] : array();
$source_value = $block_binding_source->get_value( $source_args, $this, $attribute_name );
// If the value is not null, process the HTML based on the block and the attribute.
if ( ! is_null( $source_value ) ) {

View File

@ -332,6 +332,7 @@ require ABSPATH . WPINC . '/sitemaps/class-wp-sitemaps-stylesheet.php';
require ABSPATH . WPINC . '/sitemaps/providers/class-wp-sitemaps-posts.php';
require ABSPATH . WPINC . '/sitemaps/providers/class-wp-sitemaps-taxonomies.php';
require ABSPATH . WPINC . '/sitemaps/providers/class-wp-sitemaps-users.php';
require ABSPATH . WPINC . '/class-wp-block-bindings-source.php';
require ABSPATH . WPINC . '/class-wp-block-bindings-registry.php';
require ABSPATH . WPINC . '/class-wp-block-editor-context.php';
require ABSPATH . WPINC . '/class-wp-block-type.php';

View File

@ -11,10 +11,24 @@
*/
class Tests_Block_Bindings_Register extends WP_UnitTestCase {
const TEST_SOURCE_NAME = 'test/source';
const TEST_SOURCE_PROPERTIES = array(
'label' => 'Test source',
);
public static $test_source_name = 'test/source';
public static $test_source_properties = array();
/**
* Set up before each test.
*
* @since 6.5.0
*/
public function set_up() {
parent::set_up();
self::$test_source_properties = array(
'label' => 'Test source',
'get_value_callback' => function () {
return 'test-value';
},
);
}
/**
* Tear down after each test.
@ -39,24 +53,25 @@ class Tests_Block_Bindings_Register extends WP_UnitTestCase {
* @covers ::register_block_bindings_source
* @covers ::get_all_registered_block_bindings_sources
* @covers ::get_block_bindings_source
* @covers WP_Block_Bindings_Source::__construct
*/
public function test_get_all_registered() {
$source_one_name = 'test/source-one';
$source_one_properties = self::TEST_SOURCE_PROPERTIES;
$source_one_properties = self::$test_source_properties;
register_block_bindings_source( $source_one_name, $source_one_properties );
$source_two_name = 'test/source-two';
$source_two_properties = self::TEST_SOURCE_PROPERTIES;
$source_two_properties = self::$test_source_properties;
register_block_bindings_source( $source_two_name, $source_two_properties );
$source_three_name = 'test/source-three';
$source_three_properties = self::TEST_SOURCE_PROPERTIES;
$source_three_properties = self::$test_source_properties;
register_block_bindings_source( $source_three_name, $source_three_properties );
$expected = array(
$source_one_name => array_merge( array( 'name' => $source_one_name ), $source_one_properties ),
$source_two_name => array_merge( array( 'name' => $source_two_name ), $source_two_properties ),
$source_three_name => array_merge( array( 'name' => $source_three_name ), $source_three_properties ),
$source_one_name => new WP_Block_Bindings_Source( $source_one_name, $source_one_properties ),
$source_two_name => new WP_Block_Bindings_Source( $source_two_name, $source_two_properties ),
$source_three_name => new WP_Block_Bindings_Source( $source_three_name, $source_three_properties ),
'core/post-meta' => get_block_bindings_source( 'core/post-meta' ),
'core/pattern-overrides' => get_block_bindings_source( 'core/pattern-overrides' ),
);
@ -72,15 +87,16 @@ class Tests_Block_Bindings_Register extends WP_UnitTestCase {
*
* @covers ::register_block_bindings_source
* @covers ::unregister_block_bindings_source
* @covers WP_Block_Bindings_Source::__construct
*/
public function test_unregister_block_source() {
register_block_bindings_source( self::TEST_SOURCE_NAME, self::TEST_SOURCE_PROPERTIES );
register_block_bindings_source( self::$test_source_name, self::$test_source_properties );
$result = unregister_block_bindings_source( self::TEST_SOURCE_NAME );
$this->assertSame(
array_merge(
array( 'name' => self::TEST_SOURCE_NAME ),
self::TEST_SOURCE_PROPERTIES
$result = unregister_block_bindings_source( self::$test_source_name );
$this->assertEquals(
new WP_Block_Bindings_Source(
self::$test_source_name,
self::$test_source_properties
),
$result
);

View File

@ -13,10 +13,8 @@
*/
class Tests_Blocks_wpBlockBindingsRegistry extends WP_UnitTestCase {
const TEST_SOURCE_NAME = 'test/source';
const TEST_SOURCE_PROPERTIES = array(
'label' => 'Test source',
);
public static $test_source_name = 'test/source';
public static $test_source_properties = array();
/**
* Fake block bindings registry.
@ -35,6 +33,13 @@ class Tests_Blocks_wpBlockBindingsRegistry extends WP_UnitTestCase {
parent::set_up();
$this->registry = new WP_Block_Bindings_Registry();
self::$test_source_properties = array(
'label' => 'Test source',
'get_value_callback' => function () {
return 'test-value';
},
);
}
/**
@ -58,7 +63,7 @@ class Tests_Blocks_wpBlockBindingsRegistry extends WP_UnitTestCase {
* @expectedIncorrectUsage WP_Block_Bindings_Registry::register
*/
public function test_register_invalid_non_string_names() {
$result = $this->registry->register( 1, self::TEST_SOURCE_PROPERTIES );
$result = $this->registry->register( 1, self::$test_source_properties );
$this->assertFalse( $result );
}
@ -72,7 +77,7 @@ class Tests_Blocks_wpBlockBindingsRegistry extends WP_UnitTestCase {
* @expectedIncorrectUsage WP_Block_Bindings_Registry::register
*/
public function test_register_invalid_names_without_namespace() {
$result = $this->registry->register( 'post-meta', self::TEST_SOURCE_PROPERTIES );
$result = $this->registry->register( 'post-meta', self::$test_source_properties );
$this->assertFalse( $result );
}
@ -100,7 +105,60 @@ class Tests_Blocks_wpBlockBindingsRegistry extends WP_UnitTestCase {
* @expectedIncorrectUsage WP_Block_Bindings_Registry::register
*/
public function test_register_invalid_uppercase_characters() {
$result = $this->registry->register( 'Core/PostMeta', self::TEST_SOURCE_PROPERTIES );
$result = $this->registry->register( 'Core/PostMeta', self::$test_source_properties );
$this->assertFalse( $result );
}
/**
* Should reject block bindings registration without a label.
*
* @ticket 60282
*
* @covers WP_Block_Bindings_Registry::register
*
* @expectedIncorrectUsage WP_Block_Bindings_Registry::register
*/
public function test_register_invalid_missing_label() {
// Remove the label from the properties.
unset( self::$test_source_properties['label'] );
$result = $this->registry->register( self::$test_source_name, self::$test_source_properties );
$this->assertFalse( $result );
}
/**
* Should reject block bindings registration without a get_value_callback.
*
* @ticket 60282
*
* @covers WP_Block_Bindings_Registry::register
*
* @expectedIncorrectUsage WP_Block_Bindings_Registry::register
*/
public function test_register_invalid_missing_get_value_callback() {
// Remove the get_value_callback from the properties.
unset( self::$test_source_properties['get_value_callback'] );
$result = $this->registry->register( self::$test_source_name, self::$test_source_properties );
$this->assertFalse( $result );
}
/**
* Should reject block bindings registration if `get_value_callback` is not a callable.
*
* @ticket 60282
*
* @covers WP_Block_Bindings_Registry::register
*
* @expectedIncorrectUsage WP_Block_Bindings_Registry::register
*/
public function test_register_invalid_incorrect_callback_type() {
self::$test_source_properties['get_value_callback'] = 'not-a-callback';
$result = $this->registry->register( self::$test_source_name, self::$test_source_properties );
$this->assertFalse( $result );
}
@ -110,13 +168,14 @@ class Tests_Blocks_wpBlockBindingsRegistry extends WP_UnitTestCase {
* @ticket 60282
*
* @covers WP_Block_Bindings_Registry::register
* @covers WP_Block_Bindings_Source::__construct
*/
public function test_register_block_binding_source() {
$result = $this->registry->register( self::TEST_SOURCE_NAME, self::TEST_SOURCE_PROPERTIES );
$this->assertSame(
array_merge(
array( 'name' => self::TEST_SOURCE_NAME ),
self::TEST_SOURCE_PROPERTIES
$result = $this->registry->register( self::$test_source_name, self::$test_source_properties );
$this->assertEquals(
new WP_Block_Bindings_Source(
self::$test_source_name,
self::$test_source_properties
),
$result
);
@ -143,15 +202,16 @@ class Tests_Blocks_wpBlockBindingsRegistry extends WP_UnitTestCase {
*
* @covers WP_Block_Bindings_Registry::register
* @covers WP_Block_Bindings_Registry::unregister
* WP_Block_Bindings_Source::__construct
*/
public function test_unregister_block_source() {
$this->registry->register( self::TEST_SOURCE_NAME, self::TEST_SOURCE_PROPERTIES );
$this->registry->register( self::$test_source_name, self::$test_source_properties );
$result = $this->registry->unregister( self::TEST_SOURCE_NAME );
$this->assertSame(
array_merge(
array( 'name' => self::TEST_SOURCE_NAME ),
self::TEST_SOURCE_PROPERTIES
$result = $this->registry->unregister( self::$test_source_name );
$this->assertEquals(
new WP_Block_Bindings_Source(
self::$test_source_name,
self::$test_source_properties
),
$result
);
@ -164,28 +224,29 @@ class Tests_Blocks_wpBlockBindingsRegistry extends WP_UnitTestCase {
*
* @covers WP_Block_Bindings_Registry::register
* @covers WP_Block_Bindings_Registry::get_all_registered
* WP_Block_Bindings_Source::__construct
*/
public function test_get_all_registered() {
$source_one_name = 'test/source-one';
$source_one_properties = self::TEST_SOURCE_PROPERTIES;
$source_one_properties = self::$test_source_properties;
$this->registry->register( $source_one_name, $source_one_properties );
$source_two_name = 'test/source-two';
$source_two_properties = self::TEST_SOURCE_PROPERTIES;
$source_two_properties = self::$test_source_properties;
$this->registry->register( $source_two_name, $source_two_properties );
$source_three_name = 'test/source-three';
$source_three_properties = self::TEST_SOURCE_PROPERTIES;
$source_three_properties = self::$test_source_properties;
$this->registry->register( $source_three_name, $source_three_properties );
$expected = array(
$source_one_name => array_merge( array( 'name' => $source_one_name ), $source_one_properties ),
$source_two_name => array_merge( array( 'name' => $source_two_name ), $source_two_properties ),
$source_three_name => array_merge( array( 'name' => $source_three_name ), $source_three_properties ),
$source_one_name => new WP_Block_Bindings_Source( $source_one_name, $source_one_properties ),
$source_two_name => new WP_Block_Bindings_Source( $source_two_name, $source_two_properties ),
$source_three_name => new WP_Block_Bindings_Source( $source_three_name, $source_three_properties ),
);
$registered = $this->registry->get_all_registered();
$this->assertSame( $expected, $registered );
$this->assertEquals( $expected, $registered );
}
/**
@ -197,7 +258,7 @@ class Tests_Blocks_wpBlockBindingsRegistry extends WP_UnitTestCase {
* @covers WP_Block_Bindings_Registry::get_registered
*/
public function test_get_registered_rejects_unknown_source_name() {
$this->registry->register( self::TEST_SOURCE_NAME, self::TEST_SOURCE_PROPERTIES );
$this->registry->register( self::$test_source_name, self::$test_source_properties );
$source = $this->registry->get_registered( 'test/unknown-source' );
$this->assertNull( $source );
@ -210,26 +271,26 @@ class Tests_Blocks_wpBlockBindingsRegistry extends WP_UnitTestCase {
*
* @covers WP_Block_Bindings_Registry::register
* @covers WP_Block_Bindings_Registry::get_registered
* @covers WP_Block_Bindings_Source::__construct
*/
public function test_get_registered() {
$source_one_name = 'test/source-one';
$source_one_properties = self::TEST_SOURCE_PROPERTIES;
$source_one_properties = self::$test_source_properties;
$this->registry->register( $source_one_name, $source_one_properties );
$source_two_name = 'test/source-two';
$source_two_properties = self::TEST_SOURCE_PROPERTIES;
$source_two_properties = self::$test_source_properties;
$this->registry->register( $source_two_name, $source_two_properties );
$source_three_name = 'test/source-three';
$source_three_properties = self::TEST_SOURCE_PROPERTIES;
$source_three_properties = self::$test_source_properties;
$this->registry->register( $source_three_name, $source_three_properties );
$result = $this->registry->get_registered( 'test/source-two' );
$this->assertSame(
array_merge(
array( 'name' => $source_two_name ),
$source_two_properties
),
$expected = new WP_Block_Bindings_Source( $source_two_name, $source_two_properties );
$result = $this->registry->get_registered( 'test/source-two' );
$this->assertEquals(
$expected,
$result
);
}
@ -255,9 +316,9 @@ class Tests_Blocks_wpBlockBindingsRegistry extends WP_UnitTestCase {
* @covers WP_Block_Bindings_Registry::is_registered
*/
public function test_is_registered_for_known_source() {
$this->registry->register( self::TEST_SOURCE_NAME, self::TEST_SOURCE_PROPERTIES );
$this->registry->register( self::$test_source_name, self::$test_source_properties );
$result = $this->registry->is_registered( self::TEST_SOURCE_NAME );
$result = $this->registry->is_registered( self::$test_source_name );
$this->assertTrue( $result );
}
}