Make WordPress Core


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