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. Built from https://develop.svn.wordpress.org/trunk@57562 git-svn-id: http://core.svn.wordpress.org/trunk@57063 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
parent
b89d86bde8
commit
b19e148a07
|
@ -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 );
|
||||
|
|
|
@ -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 ) ) {
|
||||
|
|
|
@ -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' );
|
||||
}
|
||||
}
|
|
@ -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 ) ) {
|
||||
|
|
|
@ -16,7 +16,7 @@
|
|||
*
|
||||
* @global string $wp_version
|
||||
*/
|
||||
$wp_version = '6.5-alpha-57561';
|
||||
$wp_version = '6.5-alpha-57562';
|
||||
|
||||
/**
|
||||
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
|
||||
|
|
|
@ -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';
|
||||
|
|
Loading…
Reference in New Issue