Make WordPress Core


Ignore:
Timestamp:
02/12/2024 04:06:47 PM (15 months ago)
Author:
swissspidy
Message:

Shortcodes: Always return an array in shortcode_parse_atts().

Previously, shortcode_parse_atts() would return the input (an empty string) if a shortcode had no attributes, even though the documentation said otherwise.

Always returning an (empty) array reduces confusion and improves developer experience as the return value does not have to be manually checked in the shortcode itself.

Props: nicolefurlan, swissspidy, johnbillion, bedas.
Fixes #59249.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/shortcode.php

    r56548 r57597  
    106106    }
    107107
     108    /**
     109     * @ticket 59249
     110     */
    108111    public function test_noatts() {
    109112        do_shortcode( '[test-shortcode-tag /]' );
    110         $this->assertSame( '', $this->atts );
     113        $this->assertIsArray( $this->atts );
     114        $this->assertEmpty( $this->atts );
    111115        $this->assertSame( 'test-shortcode-tag', $this->tagname );
    112116    }
     
    182186    }
    183187
     188    /**
     189     * @ticket 59249
     190     */
    184191    public function test_noatts_enclosing() {
    185192        do_shortcode( '[test-shortcode-tag]content[/test-shortcode-tag]' );
    186         $this->assertSame( '', $this->atts );
     193        $this->assertIsArray( $this->atts );
     194        $this->assertEmpty( $this->atts );
    187195        $this->assertSame( 'content', $this->content );
    188196        $this->assertSame( 'test-shortcode-tag', $this->tagname );
     
    209217    }
    210218
     219    /**
     220     * @ticket 59249
     221     */
    211222    public function test_unclosed() {
    212223        $out = do_shortcode( '[test-shortcode-tag]' );
    213224        $this->assertSame( '', $out );
    214         $this->assertSame( '', $this->atts );
     225        $this->assertIsArray( $this->atts );
     226        $this->assertEmpty( $this->atts );
    215227        $this->assertSame( 'test-shortcode-tag', $this->tagname );
    216228    }
     
    9991011        $this->assertSame( 'test-shortcode-tag', $this->tagname );
    10001012    }
     1013
     1014    /**
     1015     * @ticket 59249
     1016     */
     1017    public function test_shortcode_parse_atts_empty() {
     1018        $out = shortcode_parse_atts( '' );
     1019        $this->assertIsArray( $out, 'Return value is not an array' );
     1020        $this->assertEmpty( $out, 'Returned array is not empty' );
     1021    }
    10011022}
Note: See TracChangeset for help on using the changeset viewer.