Make WordPress Core

Changeset 56382

Timestamp:
08/10/2023 04:47:00 PM (12 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.

Location:
trunk
Files:
13 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/block-supports/align.php

    r52302 r56382 ��
    1616 */
    1717function wp_register_alignment_support( $block_type ) {
    18     $has_align_support = block_has_support( $block_type, array( 'align' ), false );
     18    $has_align_support = block_has_support( $block_type, , false );
    1919    if ( $has_align_support ) {
    2020        if ( ! $block_type->attributes ) {
     
    4444function wp_apply_alignment_support( $block_type, $block_attributes ) {
    4545    $attributes        = array();
    46     $has_align_support = block_has_support( $block_type, array( 'align' ), false );
     46    $has_align_support = block_has_support( $block_type, , false );
    4747    if ( $has_align_support ) {
    4848        $has_block_alignment = array_key_exists( 'align', $block_attributes );
  • trunk/src/wp-includes/block-supports/border.php

    r54211 r56382  
    2323    }
    2424
    25     if ( block_has_support( $block_type, array( '__experimentalBorder' ) ) && ! array_key_exists( 'style', $block_type->attributes ) ) {
     25    if ( block_has_support( $block_type, ) && ! array_key_exists( 'style', $block_type->attributes ) ) {
    2626        $block_type->attributes['style'] = array(
    2727            'type' => 'object',
  • trunk/src/wp-includes/block-supports/custom-classname.php

    r54873 r56382  
    1616 */
    1717function wp_register_custom_classname_support( $block_type ) {
    18     $has_custom_classname_support = block_has_support( $block_type, array( 'customClassName' ), true );
     18    $has_custom_classname_support = block_has_support( $block_type, , true );
    1919
    2020    if ( $has_custom_classname_support ) {
     
    4343 */
    4444function wp_apply_custom_classname_support( $block_type, $block_attributes ) {
    45     $has_custom_classname_support = block_has_support( $block_type, array( 'customClassName' ), true );
     45    $has_custom_classname_support = block_has_support( $block_type, , true );
    4646    $attributes                   = array();
    4747    if ( $has_custom_classname_support ) {
  • trunk/src/wp-includes/block-supports/dimensions.php

    r55175 r56382  
    3030    }
    3131
    32     $has_dimensions_support = block_has_support( $block_type, array( 'dimensions' ), false );
     32    $has_dimensions_support = block_has_support( $block_type, , false );
    3333
    3434    if ( $has_dimensions_support ) {
  • trunk/src/wp-includes/block-supports/generated-classname.php

    r54874 r56382  
    5151function wp_apply_generated_classname_support( $block_type ) {
    5252    $attributes                      = array();
    53     $has_generated_classname_support = block_has_support( $block_type, array( 'className' ), true );
     53    $has_generated_classname_support = block_has_support( $block_type, , true );
    5454    if ( $has_generated_classname_support ) {
    5555        $block_classname = wp_get_block_default_classname( $block_type->name );
  • trunk/src/wp-includes/block-supports/layout.php

    r56055 r56382  
    203203 */
    204204function wp_register_layout_support( $block_type ) {
    205     $support_layout = block_has_support( $block_type, array( 'layout' ), false ) || block_has_support( $block_type, array( '__experimentalLayout' ), false );
     205    $support_layout = block_has_support( $block_type, , false );
    206206    if ( $support_layout ) {
    207207        if ( ! $block_type->attributes ) {
     
    549549function wp_render_layout_support_flag( $block_content, $block ) {
    550550    $block_type       = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] );
    551     $support_layout   = block_has_support( $block_type, array( 'layout' ), false ) || block_has_support( $block_type, array( '__experimentalLayout' ), false );
     551    $support_layout   = block_has_support( $block_type, , false );
    552552    $has_child_layout = isset( $block['attrs']['style']['layout']['selfStretch'] );
    553553
  • trunk/src/wp-includes/block-supports/position.php

    r55286 r56382  
    1616 */
    1717function wp_register_position_support( $block_type ) {
    18     $has_position_support = block_has_support( $block_type, array( 'position' ), false );
     18    $has_position_support = block_has_support( $block_type, , false );
    1919
    2020    // Set up attributes and styles within that if needed.
     
    4242function wp_render_position_support( $block_content, $block ) {
    4343    $block_type           = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] );
    44     $has_position_support = block_has_support( $block_type, array( 'position' ), false );
     44    $has_position_support = block_has_support( $block_type, , false );
    4545
    4646    if (
  • trunk/src/wp-includes/block-supports/settings.php

    r56146 r56382  
    4141    // return early if the block doesn't have support for settings.
    4242    $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] );
    43     if ( ! block_has_support( $block_type, array( '__experimentalSettings' ), false ) ) {
     43    if ( ! block_has_support( $block_type, , false ) ) {
    4444        return $block_content;
    4545    }
     
    7878    // Return early if the block has not support for descendent block styles.
    7979    $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] );
    80     if ( ! block_has_support( $block_type, array( '__experimentalSettings' ), false ) ) {
     80    if ( ! block_has_support( $block_type, , false ) ) {
    8181        return null;
    8282    }
  • trunk/src/wp-includes/block-supports/shadow.php

    r56046 r56382  
    1616 */
    1717function wp_register_shadow_support( $block_type ) {
    18     $has_shadow_support = block_has_support( $block_type, array( 'shadow' ), false );
     18    $has_shadow_support = block_has_support( $block_type, , false );
    1919
    2020    if ( ! $has_shadow_support ) {
     
    5151 */
    5252function wp_apply_shadow_support( $block_type, $block_attributes ) {
    53     $has_shadow_support = block_has_support( $block_type, array( 'shadow' ), false );
     53    $has_shadow_support = block_has_support( $block_type, , false );
    5454
    5555    if ( ! $has_shadow_support ) {
  • trunk/src/wp-includes/block-supports/spacing.php

    r54874 r56382  
    1919 */
    2020function wp_register_spacing_support( $block_type ) {
    21     $has_spacing_support = block_has_support( $block_type, array( 'spacing' ), false );
     21    $has_spacing_support = block_has_support( $block_type, , false );
    2222
    2323    // Setup attributes and styles within that if needed.
  • trunk/src/wp-includes/blocks.php

    r56244 r56382  
    12491249 *
    12501250 * @since 5.8.0
     1251
    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 ath 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
  • trunk/src/wp-includes/class-wp-theme-json.php

    r56345 r56382  
    12641264        if ( isset( $block_metadata['name'] ) ) {
    12651265            $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block_metadata['name'] );
    1266             if ( ! block_has_support( $block_type, array( 'layout' ), false ) && ! block_has_support( $block_type, array( '__experimentalLayout' ), false ) ) {
     1266            if ( ! block_has_support( $block_type, , false ) ) {
    12671267                return $block_rules;
    12681268            }
  • trunk/tests/phpunit/tests/blocks/wpBlock.php

    r55822 r56382  
    684684
    685685    /**
     686
     687
     688
     689
     690
     691
     692
     693
     694
     695
     696
     697
     698
     699
     700
     701
     702
     703
     704
     705
     706
     707
     708
     709
     710
     711
     712
     713
     714
     715
     716
     717
     718
     719
     720
     721
     722
     723
     724
     725
     726
     727
     728
     729
     730
     731
     732
     733
     734
     735
     736
     737
     738
     739
     740
     741
     742
     743
     744
     745
     746
     747
     748
     749
     750
     751
     752
     753
     754
     755
     756
     757
     758
     759
     760
    686761     * @ticket 51612
    687762     */
Note: See TracChangeset for help on using the changeset viewer.