WP_Block_Bindings_Source
云策文档标注
概述
WP_Block_Bindings_Source 类用于表示块绑定源,是 Block Bindings 注册表内部使用的核心组件。开发者应通过 WP_Block_Bindings_Registry 或 register_block_bindings_source 函数来操作,而非直接实例化此类。
关键要点
- 类属性包括 name(源名称)、label(标签)、get_value_callback(获取值的回调函数)和 uses_context(上下文数组)。
- 主要方法有 __construct(构造函数,用于初始化源)、get_value(调用回调函数获取值,并可通过 block_bindings_source_value 过滤器修改)和 __wakeup(反序列化时抛出异常)。
- get_value 方法接受 source_args、block_instance 和 attribute_name 参数,返回源的值,并支持过滤器以自定义输出。
- 此类自 WordPress 6.5.0 版本引入,get_value 方法在 6.7.0 版本增加了 block_bindings_source_value 过滤器。
注意事项
不要直接使用构造函数,应通过注册表或函数来创建源实例;反序列化会抛出 LogicException 异常,确保避免此类操作。
原文内容
Class representing block bindings source.
Description
This class is designed for internal use by the Block Bindings registry.
See also
Methods
| Name | Description |
|---|---|
| WP_Block_Bindings_Source::__construct | Constructor. |
| WP_Block_Bindings_Source::__wakeup | Wakeup magic method. |
| WP_Block_Bindings_Source::get_value | Calls the callback function specified in the `$get_value_callback` property with the given arguments and returns the result. It can be modified with `block_bindings_source_value` filter. |
Source
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;
/**
* The context added to the blocks needed by the source.
*
* @since 6.5.0
* @var string[]|null
*/
public $uses_context = null;
/**
* 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;
foreach ( $source_properties as $property_name => $property_value ) {
$this->$property_name = $property_value;
}
}
/**
* Calls the callback function specified in the `$get_value_callback` property
* with the given arguments and returns the result. It can be modified with
* `block_bindings_source_value` filter.
*
* @since 6.5.0
* @since 6.7.0 `block_bindings_source_value` filter was added.
*
* @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 ) {
$value = call_user_func_array( $this->get_value_callback, array( $source_args, $block_instance, $attribute_name ) );
/**
* Filters the output of a block bindings source.
*
* @since 6.7.0
*
* @param mixed $value The computed value for the source.
* @param string $name The name of the source.
* @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 an attribute.
*/
return apply_filters( 'block_bindings_source_value', $value, $this->name, $source_args, $block_instance, $attribute_name );
}
/**
* Wakeup magic method.
*
* @since 6.5.0
*/
public function __wakeup() {
throw new LogicException( __CLASS__ . ' should never be unserialized' );
}
}
Changelog
| Version | Description |
|---|---|
| 6.5.0 | Introduced. |