Make WordPress Core

Changeset 57597

Timestamp:
02/12/2024 04:06:47 PM (6 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.

Location:
trunk
Files:
3 edited

Legend:

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

    r57524 r57597  
    23552355 */
    23562356function img_caption_shortcode( $attr, $content = '' ) {
    2357     if ( ! $attr ) {
    2358         $attr = array();
    2359     }
    2360 
    23612357    // New-style shortcode with the caption inside the shortcode with the link and image tags.
    23622358    if ( ! isset( $attr['caption'] ) ) {
  • trunk/src/wp-includes/shortcodes.php

    r57584 r57597  
    601601 *
    602602 * @since 2.5.0
     603
     604
    603605 *
    604606 * @param string $text Shortcode arguments list.
    605  * @return array|string Array of attribute values keyed by attribute name.
    606  *                      Returns empty array if there are no attributes.
    607  *                      Returns the original arguments string if it cannot be parsed.
     607 * @return array Array of attribute values keyed by attribute name.
     608 *               
     609 *                cannot be parsed.
    608610 */
    609611function shortcode_parse_atts( $text ) {
     
    636638            }
    637639        }
    638     } else {
    639         $atts = ltrim( $text );
    640640    }
    641641
  • trunk/tests/phpunit/tests/shortcode.php

    r56548 r57597  
    106106    }
    107107
     108
     109
     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
     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
     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
     1016
     1017
     1018
     1019
     1020
     1021
    10011022}
Note: See TracChangeset for help on using the changeset viewer.