Make WordPress Core

Changeset 56382


Ignore:
Timestamp:
08/10/2023 04:47:00 PM (11 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, 'align', 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, 'align', 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, '__experimentalBorder' ) && ! 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, 'customClassName', 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, 'customClassName', 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, 'dimensions', 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, 'className', 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, 'layout', false ) || block_has_support( $block_type, '__experimentalLayout', 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, 'layout', false ) || block_has_support( $block_type, '__experimentalLayout', 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, 'position', 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, 'position', 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, '__experimentalSettings', 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, '__experimentalSettings', 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, 'shadow', 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, 'shadow', 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, 'spacing', 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 * @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
  • 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, 'layout', false ) && ! block_has_support( $block_type, '__experimentalLayout', false ) ) {
    12671267                return $block_rules;
    12681268            }
  • trunk/tests/phpunit/tests/blocks/wpBlock.php

    r55822 r56382  
    684684
    685685    /**
     686     * @ticket 58532
     687     *
     688     * @dataProvider data_block_has_support_string
     689     *
     690     * @param array  $block_data Block data.
     691     * @param string $support    Support string to check.
     692     * @param bool   $expected   Expected result.
     693     */
     694    public function test_block_has_support_string( $block_data, $support, $expected, $message ) {
     695        $this->registry->register( 'core/example', $block_data );
     696        $block_type  = $this->registry->get_registered( 'core/example' );
     697        $has_support = block_has_support( $block_type, $support );
     698        $this->assertEquals( $expected, $has_support, $message );
     699    }
     700
     701    /**
     702     * Data provider for test_block_has_support_string
     703     */
     704    public function data_block_has_support_string() {
     705        return array(
     706            array(
     707                array(),
     708                'color',
     709                false,
     710                'Block with empty support array.',
     711            ),
     712            array(
     713                array(
     714                    'supports' => array(
     715                        'align'    => array( 'wide', 'full' ),
     716                        'fontSize' => true,
     717                        'color'    => array(
     718                            'link'     => true,
     719                            'gradient' => false,
     720                        ),
     721                    ),
     722                ),
     723                'align',
     724                true,
     725                'Feature present in support array.',
     726            ),
     727            array(
     728                array(
     729                    'supports' => array(
     730                        'align'    => array( 'wide', 'full' ),
     731                        'fontSize' => true,
     732                        'color'    => array(
     733                            'link'     => true,
     734                            'gradient' => false,
     735                        ),
     736                    ),
     737                ),
     738                'anchor',
     739                false,
     740                'Feature not present in support array.',
     741            ),
     742            array(
     743                array(
     744                    'supports' => array(
     745                        'align'    => array( 'wide', 'full' ),
     746                        'fontSize' => true,
     747                        'color'    => array(
     748                            'link'     => true,
     749                            'gradient' => false,
     750                        ),
     751                    ),
     752                ),
     753                array( 'align' ),
     754                true,
     755                'Feature present in support array, single element array.',
     756            ),
     757        );
     758    }
     759
     760    /**
    686761     * @ticket 51612
    687762     */
Note: See TracChangeset for help on using the changeset viewer.