Make WordPress Core


Ignore:
Timestamp:
08/10/2023 04:47:00 PM (10 months ago)
Author:
flixos90
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/blocks.php

    r56244 r56382  
    12491249 *
    12501250 * @since 5.8.0
     1251 * @since 6.4.0 The `$feature` parameter now supports a string.
    12511252 *
    12521253 * @param WP_Block_Type $block_type    Block type to check for support.
    1253  * @param array         $feature       Path to a specific feature to check support for.
     1254 * @param string|array  $feature       Feature slug, or path to a specific feature to check support for.
    12541255 * @param mixed         $default_value Optional. Fallback value for feature support. Default false.
    12551256 * @return bool Whether the feature is supported.
     
    12581259    $block_support = $default_value;
    12591260    if ( $block_type && property_exists( $block_type, 'supports' ) ) {
    1260         $block_support = _wp_array_get( $block_type->supports, $feature, $default_value );
     1261        if ( is_array( $feature ) && count( $feature ) === 1 ) {
     1262            $feature = $feature[0];
     1263        }
     1264
     1265        if ( is_array( $feature ) ) {
     1266            $block_support = _wp_array_get( $block_type->supports, $feature, $default_value );
     1267        } elseif ( isset( $block_type->supports[ $feature ] ) ) {
     1268            $block_support = $block_type->supports[ $feature ];
     1269        }
    12611270    }
    12621271
Note: See TracChangeset for help on using the changeset viewer.