函数文档

block_has_support()

💡 云策文档标注

概述

block_has_support() 函数用于检查当前块类型是否支持指定的功能。它接受块类型、功能参数和默认值作为输入,返回布尔值表示支持状态。

关键要点

  • 函数参数:$block_type(WP_Block_Type 类型,必需),$feature(字符串或数组,必需),$default_value(混合类型,可选,默认 false)。
  • 返回值:布尔值,表示功能是否被支持。
  • 内部逻辑:通过访问 $block_type->supports 属性,使用 _wp_array_get() 函数处理数组路径或直接检查键值。
  • 支持情况:如果 $block_support 为 true 或数组,则返回 true。

代码示例

function block_has_support( $block_type, $feature, $default_value = false ) {
    $block_support = $default_value;
    if ( $block_type instanceof WP_Block_Type ) {
        if ( is_array( $feature ) && count( $feature ) === 1 ) {
            $feature = $feature[0];
        }

        if ( is_array( $feature ) ) {
            $block_support = _wp_array_get( $block_type->supports, $feature, $default_value );
        } elseif ( isset( $block_type->supports[ $feature ] ) ) {
            $block_support = $block_type->supports[ $feature ];
        }
    }

    return true === $block_support || is_array( $block_support );
}

注意事项

  • 从 WordPress 6.4.0 版本开始,$feature 参数支持字符串类型。
  • 函数在 WordPress 5.8.0 版本中引入。

📄 原文内容

Checks whether the current block type supports the feature requested.

Parameters

$block_typeWP_Block_Typerequired
Block type to check for support.
$featurestring|arrayrequired
Feature slug, or path to a specific feature to check support for.
$default_valuemixedoptional
Fallback value for feature support.

Default:false

Return

bool Whether the feature is supported.

Source

function block_has_support( $block_type, $feature, $default_value = false ) {
	$block_support = $default_value;
	if ( $block_type instanceof WP_Block_Type ) {
		if ( is_array( $feature ) && count( $feature ) === 1 ) {
			$feature = $feature[0];
		}

		if ( is_array( $feature ) ) {
			$block_support = _wp_array_get( $block_type->supports, $feature, $default_value );
		} elseif ( isset( $block_type->supports[ $feature ] ) ) {
			$block_support = $block_type->supports[ $feature ];
		}
	}

	return true === $block_support || is_array( $block_support );
}

Changelog

Version Description
6.4.0 The $feature parameter now supports a string.
5.8.0 Introduced.