From 268203f8aad7f856fef05b6cd26c15ee23d2cbc0 Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Thu, 10 Aug 2023 16:48:19 +0000 Subject: [PATCH] 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 --- wp-includes/block-supports/align.php | 4 ++-- wp-includes/block-supports/border.php | 2 +- wp-includes/block-supports/custom-classname.php | 4 ++-- wp-includes/block-supports/dimensions.php | 2 +- wp-includes/block-supports/generated-classname.php | 2 +- wp-includes/block-supports/layout.php | 4 ++-- wp-includes/block-supports/position.php | 4 ++-- wp-includes/block-supports/settings.php | 4 ++-- wp-includes/block-supports/shadow.php | 4 ++-- wp-includes/block-supports/spacing.php | 2 +- wp-includes/blocks.php | 13 +++++++++++-- wp-includes/class-wp-theme-json.php | 2 +- wp-includes/version.php | 2 +- 13 files changed, 29 insertions(+), 20 deletions(-) diff --git a/wp-includes/block-supports/align.php b/wp-includes/block-supports/align.php index 5a1e3a7e58..35591a1e31 100644 --- a/wp-includes/block-supports/align.php +++ b/wp-includes/block-supports/align.php @@ -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 ); diff --git a/wp-includes/block-supports/border.php b/wp-includes/block-supports/border.php index 443b808fd8..e68089e404 100644 --- a/wp-includes/block-supports/border.php +++ b/wp-includes/block-supports/border.php @@ -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', ); diff --git a/wp-includes/block-supports/custom-classname.php b/wp-includes/block-supports/custom-classname.php index 47bfbee6bd..1889918644 100644 --- a/wp-includes/block-supports/custom-classname.php +++ b/wp-includes/block-supports/custom-classname.php @@ -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 ); diff --git a/wp-includes/block-supports/dimensions.php b/wp-includes/block-supports/dimensions.php index 7951f8d881..bffeef992a 100644 --- a/wp-includes/block-supports/dimensions.php +++ b/wp-includes/block-supports/dimensions.php @@ -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( diff --git a/wp-includes/block-supports/generated-classname.php b/wp-includes/block-supports/generated-classname.php index 3ed779700c..bbedd0ee53 100644 --- a/wp-includes/block-supports/generated-classname.php +++ b/wp-includes/block-supports/generated-classname.php @@ -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 ); diff --git a/wp-includes/block-supports/layout.php b/wp-includes/block-supports/layout.php index 1eb1d77715..519574238d 100644 --- a/wp-includes/block-supports/layout.php +++ b/wp-includes/block-supports/layout.php @@ -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 ) { diff --git a/wp-includes/block-supports/position.php b/wp-includes/block-supports/position.php index 8dd5f662e3..164305cf28 100644 --- a/wp-includes/block-supports/position.php +++ b/wp-includes/block-supports/position.php @@ -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 || diff --git a/wp-includes/block-supports/settings.php b/wp-includes/block-supports/settings.php index 0be57f9b03..350eb24fe4 100644 --- a/wp-includes/block-supports/settings.php +++ b/wp-includes/block-supports/settings.php @@ -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; } diff --git a/wp-includes/block-supports/shadow.php b/wp-includes/block-supports/shadow.php index 7a710c5222..6fa05b248f 100644 --- a/wp-includes/block-supports/shadow.php +++ b/wp-includes/block-supports/shadow.php @@ -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(); diff --git a/wp-includes/block-supports/spacing.php b/wp-includes/block-supports/spacing.php index ea9b39822d..3b54472054 100644 --- a/wp-includes/block-supports/spacing.php +++ b/wp-includes/block-supports/spacing.php @@ -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 ) { diff --git a/wp-includes/blocks.php b/wp-includes/blocks.php index 347839f09a..f4da0320d4 100644 --- a/wp-includes/blocks.php +++ b/wp-includes/blocks.php @@ -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 ); diff --git a/wp-includes/class-wp-theme-json.php b/wp-includes/class-wp-theme-json.php index 9176cbacab..dc297fa9b9 100644 --- a/wp-includes/class-wp-theme-json.php +++ b/wp-includes/class-wp-theme-json.php @@ -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; } } diff --git a/wp-includes/version.php b/wp-includes/version.php index db7bfe70c2..917e833219 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -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.