类文档

WP_Block_Supports

💡 云策文档标注

概述

WP_Block_Supports 是 WordPress 中用于封装和实现块支持功能的类。它管理块支持特性,包括注册、初始化和应用,以生成 HTML 属性如类名。

关键要点

  • WP_Block_Supports 是一个单例类,通过 get_instance() 方法获取主实例。
  • 提供 init() 方法初始化块支持,注册块属性。
  • register() 方法用于注册块支持,需指定名称和配置数组。
  • apply_block_supports() 方法应用块支持特性,生成 HTML 属性数组。
  • register_attributes() 私有方法注册块支持所需的块属性。

代码示例

// 获取 WP_Block_Supports 实例
$instance = WP_Block_Supports::get_instance();

// 注册一个块支持
$instance->register('custom_support', array(
    'apply' => 'custom_apply_function',
    'register_attribute' => 'custom_register_function'
));

// 应用块支持生成属性
$attributes = $instance->apply_block_supports();

注意事项

  • 块支持配置需包含 'apply' 和 'register_attribute' 回调函数以实现功能。
  • apply_block_supports() 依赖于当前渲染的块,通过静态属性 $block_to_render 访问。
  • register_attributes() 是私有方法,仅在类内部调用,通常通过 init() 触发。

📄 原文内容

Class encapsulating and implementing Block Supports.

Methods

Name Description
WP_Block_Supports::apply_block_supports Generates an array of HTML attributes, such as classes, by applying to the given block all of the features that the block supports.
WP_Block_Supports::get_instance Utility method to retrieve the main instance of the class.
WP_Block_Supports::init Initializes the block supports. It registers the block supports block attributes.
WP_Block_Supports::register Registers a block support.
WP_Block_Supports::register_attributes Registers the block attributes required by the different block supports.

Source

class WP_Block_Supports {

	/**
	 * Config.
	 *
	 * @since 5.6.0
	 * @var array
	 */
	private $block_supports = array();

	/**
	 * Tracks the current block to be rendered.
	 *
	 * @since 5.6.0
	 * @var array
	 */
	public static $block_to_render = null;

	/**
	 * Container for the main instance of the class.
	 *
	 * @since 5.6.0
	 * @var WP_Block_Supports|null
	 */
	private static $instance = null;

	/**
	 * Utility method to retrieve the main instance of the class.
	 *
	 * The instance will be created if it does not exist yet.
	 *
	 * @since 5.6.0
	 *
	 * @return WP_Block_Supports The main instance.
	 */
	public static function get_instance() {
		if ( null === self::$instance ) {
			self::$instance = new self();
		}

		return self::$instance;
	}

	/**
	 * Initializes the block supports. It registers the block supports block attributes.
	 *
	 * @since 5.6.0
	 */
	public static function init() {
		$instance = self::get_instance();
		$instance->register_attributes();
	}

	/**
	 * Registers a block support.
	 *
	 * @since 5.6.0
	 *
	 * @link https://developer.wordpress.org/block-editor/reference-guides/block-api/block-supports/
	 *
	 * @param string $block_support_name   Block support name.
	 * @param array  $block_support_config Array containing the properties of the block support.
	 */
	public function register( $block_support_name, $block_support_config ) {
		$this->block_supports[ $block_support_name ] = array_merge(
			$block_support_config,
			array( 'name' => $block_support_name )
		);
	}

	/**
	 * Generates an array of HTML attributes, such as classes, by applying to
	 * the given block all of the features that the block supports.
	 *
	 * @since 5.6.0
	 *
	 * @return string[] Array of HTML attribute values keyed by their name.
	 */
	public function apply_block_supports() {
		$block_type = WP_Block_Type_Registry::get_instance()->get_registered(
			self::$block_to_render['blockName']
		);

		// If no render_callback, assume styles have been previously handled.
		if ( ! $block_type || empty( $block_type ) ) {
			return array();
		}

		$block_attributes = array_key_exists( 'attrs', self::$block_to_render ) && is_array( self::$block_to_render['attrs'] )
			? $block_type->prepare_attributes_for_render( self::$block_to_render['attrs'] )
			: array();

		$output = array();
		foreach ( $this->block_supports as $block_support_config ) {
			if ( ! isset( $block_support_config['apply'] ) ) {
				continue;
			}

			$new_attributes = call_user_func(
				$block_support_config['apply'],
				$block_type,
				$block_attributes
			);

			if ( ! empty( $new_attributes ) ) {
				foreach ( $new_attributes as $attribute_name => $attribute_value ) {
					if ( empty( $output[ $attribute_name ] ) ) {
						$output[ $attribute_name ] = $attribute_value;
					} else {
						$output[ $attribute_name ] .= " $attribute_value";
					}
				}
			}
		}

		return $output;
	}

	/**
	 * Registers the block attributes required by the different block supports.
	 *
	 * @since 5.6.0
	 */
	private function register_attributes() {
		$block_registry         = WP_Block_Type_Registry::get_instance();
		$registered_block_types = $block_registry->get_all_registered();
		foreach ( $registered_block_types as $block_type ) {
			if ( ! ( $block_type instanceof WP_Block_Type ) ) {
				continue;
			}
			if ( ! $block_type->attributes ) {
				$block_type->attributes = array();
			}

			foreach ( $this->block_supports as $block_support_config ) {
				if ( ! isset( $block_support_config['register_attribute'] ) ) {
					continue;
				}

				call_user_func(
					$block_support_config['register_attribute'],
					$block_type
				);
			}
		}
	}
}

Changelog

Version Description
5.6.0 Introduced.