mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-10-04 14:47:02 +00:00
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:
@@ -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' );
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user