Editor: Simplify usage of `block_has_support()` function by supporting a string.

Most block feature checks are for a single feature string, and for such cases it is not intuitive to require an array for the `$feature` parameter of the `block_has_support()` function.

This changeset brings it in line with other functions like `post_type_supports()`, allowing to pass a string for the `$feature`. An array is still supported for more complex cases where support for sub-features needs to be determined. This change furthermore includes a very minor performance tweak by avoiding calls to the `_wp_array_get()` function if a single feature string is being checked for.

Props thekt12, nihar007, mukesh27, swissspidy.
Fixes #58532.

Built from https://develop.svn.wordpress.org/trunk@56382


git-svn-id: http://core.svn.wordpress.org/trunk@55894 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Felix Arntz 2023-08-10 16:48:19 +00:00
parent f106155bf8
commit 268203f8aa
13 changed files with 29 additions and 20 deletions

View File

@ -15,7 +15,7 @@
* @param WP_Block_Type $block_type Block Type.
*/
function wp_register_alignment_support( $block_type ) {
$has_align_support = block_has_support( $block_type, array( 'align' ), false );
$has_align_support = block_has_support( $block_type, 'align', false );
if ( $has_align_support ) {
if ( ! $block_type->attributes ) {
$block_type->attributes = array();
@ -43,7 +43,7 @@ function wp_register_alignment_support( $block_type ) {
*/
function wp_apply_alignment_support( $block_type, $block_attributes ) {
$attributes = array();
$has_align_support = block_has_support( $block_type, array( 'align' ), false );
$has_align_support = block_has_support( $block_type, 'align', false );
if ( $has_align_support ) {
$has_block_alignment = array_key_exists( 'align', $block_attributes );

View File

@ -22,7 +22,7 @@ function wp_register_border_support( $block_type ) {
$block_type->attributes = array();
}
if ( block_has_support( $block_type, array( '__experimentalBorder' ) ) && ! array_key_exists( 'style', $block_type->attributes ) ) {
if ( block_has_support( $block_type, '__experimentalBorder' ) && ! array_key_exists( 'style', $block_type->attributes ) ) {
$block_type->attributes['style'] = array(
'type' => 'object',
);

View File

@ -15,7 +15,7 @@
* @param WP_Block_Type $block_type Block Type.
*/
function wp_register_custom_classname_support( $block_type ) {
$has_custom_classname_support = block_has_support( $block_type, array( 'customClassName' ), true );
$has_custom_classname_support = block_has_support( $block_type, 'customClassName', true );
if ( $has_custom_classname_support ) {
if ( ! $block_type->attributes ) {
@ -42,7 +42,7 @@ function wp_register_custom_classname_support( $block_type ) {
* @return array Block CSS classes and inline styles.
*/
function wp_apply_custom_classname_support( $block_type, $block_attributes ) {
$has_custom_classname_support = block_has_support( $block_type, array( 'customClassName' ), true );
$has_custom_classname_support = block_has_support( $block_type, 'customClassName', true );
$attributes = array();
if ( $has_custom_classname_support ) {
$has_custom_classnames = array_key_exists( 'className', $block_attributes );

View File

@ -29,7 +29,7 @@ function wp_register_dimensions_support( $block_type ) {
return;
}
$has_dimensions_support = block_has_support( $block_type, array( 'dimensions' ), false );
$has_dimensions_support = block_has_support( $block_type, 'dimensions', false );
if ( $has_dimensions_support ) {
$block_type->attributes['style'] = array(

View File

@ -50,7 +50,7 @@ function wp_get_block_default_classname( $block_name ) {
*/
function wp_apply_generated_classname_support( $block_type ) {
$attributes = array();
$has_generated_classname_support = block_has_support( $block_type, array( 'className' ), true );
$has_generated_classname_support = block_has_support( $block_type, 'className', true );
if ( $has_generated_classname_support ) {
$block_classname = wp_get_block_default_classname( $block_type->name );

View File

@ -202,7 +202,7 @@ function wp_get_layout_definitions() {
* @param WP_Block_Type $block_type Block Type.
*/
function wp_register_layout_support( $block_type ) {
$support_layout = block_has_support( $block_type, array( 'layout' ), false ) || block_has_support( $block_type, array( '__experimentalLayout' ), false );
$support_layout = block_has_support( $block_type, 'layout', false ) || block_has_support( $block_type, '__experimentalLayout', false );
if ( $support_layout ) {
if ( ! $block_type->attributes ) {
$block_type->attributes = array();
@ -548,7 +548,7 @@ function wp_get_layout_style( $selector, $layout, $has_block_gap_support = false
*/
function wp_render_layout_support_flag( $block_content, $block ) {
$block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] );
$support_layout = block_has_support( $block_type, array( 'layout' ), false ) || block_has_support( $block_type, array( '__experimentalLayout' ), false );
$support_layout = block_has_support( $block_type, 'layout', false ) || block_has_support( $block_type, '__experimentalLayout', false );
$has_child_layout = isset( $block['attrs']['style']['layout']['selfStretch'] );
if ( ! $support_layout && ! $has_child_layout ) {

View File

@ -15,7 +15,7 @@
* @param WP_Block_Type $block_type Block Type.
*/
function wp_register_position_support( $block_type ) {
$has_position_support = block_has_support( $block_type, array( 'position' ), false );
$has_position_support = block_has_support( $block_type, 'position', false );
// Set up attributes and styles within that if needed.
if ( ! $block_type->attributes ) {
@ -41,7 +41,7 @@ function wp_register_position_support( $block_type ) {
*/
function wp_render_position_support( $block_content, $block ) {
$block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] );
$has_position_support = block_has_support( $block_type, array( 'position' ), false );
$has_position_support = block_has_support( $block_type, 'position', false );
if (
! $has_position_support ||

View File

@ -40,7 +40,7 @@ function _wp_add_block_level_presets_class( $block_content, $block ) {
// return early if the block doesn't have support for settings.
$block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] );
if ( ! block_has_support( $block_type, array( '__experimentalSettings' ), false ) ) {
if ( ! block_has_support( $block_type, '__experimentalSettings', false ) ) {
return $block_content;
}
@ -77,7 +77,7 @@ function _wp_add_block_level_presets_class( $block_content, $block ) {
function _wp_add_block_level_preset_styles( $pre_render, $block ) {
// Return early if the block has not support for descendent block styles.
$block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] );
if ( ! block_has_support( $block_type, array( '__experimentalSettings' ), false ) ) {
if ( ! block_has_support( $block_type, '__experimentalSettings', false ) ) {
return null;
}

View File

@ -15,7 +15,7 @@
* @param WP_Block_Type $block_type Block Type.
*/
function wp_register_shadow_support( $block_type ) {
$has_shadow_support = block_has_support( $block_type, array( 'shadow' ), false );
$has_shadow_support = block_has_support( $block_type, 'shadow', false );
if ( ! $has_shadow_support ) {
return;
@ -50,7 +50,7 @@ function wp_register_shadow_support( $block_type ) {
* @return array Shadow CSS classes and inline styles.
*/
function wp_apply_shadow_support( $block_type, $block_attributes ) {
$has_shadow_support = block_has_support( $block_type, array( 'shadow' ), false );
$has_shadow_support = block_has_support( $block_type, 'shadow', false );
if ( ! $has_shadow_support ) {
return array();

View File

@ -18,7 +18,7 @@
* @param WP_Block_Type $block_type Block Type.
*/
function wp_register_spacing_support( $block_type ) {
$has_spacing_support = block_has_support( $block_type, array( 'spacing' ), false );
$has_spacing_support = block_has_support( $block_type, 'spacing', false );
// Setup attributes and styles within that if needed.
if ( ! $block_type->attributes ) {

View File

@ -1248,16 +1248,25 @@ function unregister_block_style( $block_name, $block_style_name ) {
* Checks whether the current block type supports the feature requested.
*
* @since 5.8.0
* @since 6.4.0 The `$feature` parameter now supports a string.
*
* @param WP_Block_Type $block_type Block type to check for support.
* @param array $feature Path to a specific feature to check support for.
* @param string|array $feature Feature slug, or path to a specific feature to check support for.
* @param mixed $default_value Optional. Fallback value for feature support. Default false.
* @return bool Whether the feature is supported.
*/
function block_has_support( $block_type, $feature, $default_value = false ) {
$block_support = $default_value;
if ( $block_type && property_exists( $block_type, 'supports' ) ) {
$block_support = _wp_array_get( $block_type->supports, $feature, $default_value );
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 );

View File

@ -1263,7 +1263,7 @@ class WP_Theme_JSON {
if ( isset( $block_metadata['name'] ) ) {
$block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block_metadata['name'] );
if ( ! block_has_support( $block_type, array( 'layout' ), false ) && ! block_has_support( $block_type, array( '__experimentalLayout' ), false ) ) {
if ( ! block_has_support( $block_type, 'layout', false ) && ! block_has_support( $block_type, '__experimentalLayout', false ) ) {
return $block_rules;
}
}

View File

@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '6.4-alpha-56381';
$wp_version = '6.4-alpha-56382';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.