diff --git a/src/wp-includes/media.php b/src/wp-includes/media.php index 69a6d53299..b5e477e7f7 100644 --- a/src/wp-includes/media.php +++ b/src/wp-includes/media.php @@ -2354,10 +2354,6 @@ add_shortcode( 'caption', 'img_caption_shortcode' ); * @return string HTML content to display the caption. */ function img_caption_shortcode( $attr, $content = '' ) { - if ( ! $attr ) { - $attr = array(); - } - // New-style shortcode with the caption inside the shortcode with the link and image tags. if ( ! isset( $attr['caption'] ) ) { if ( preg_match( '#((?:]+>\s*)?]+>(?:\s*)?)(.*)#is', $content, $matches ) ) { diff --git a/src/wp-includes/shortcodes.php b/src/wp-includes/shortcodes.php index cf88b2195f..bc7ade1a2f 100644 --- a/src/wp-includes/shortcodes.php +++ b/src/wp-includes/shortcodes.php @@ -600,11 +600,13 @@ function get_shortcode_atts_regex() { * retrieval of the attributes, since all attributes have to be known. * * @since 2.5.0 + * @since 6.5.0 The function now always returns an empty array, + * even if the original arguments string cannot be parsed or is empty. * * @param string $text Shortcode arguments list. - * @return array|string Array of attribute values keyed by attribute name. - * Returns empty array if there are no attributes. - * Returns the original arguments string if it cannot be parsed. + * @return array Array of attribute values keyed by attribute name. + * Returns empty array if there are no attributes + * or if the original arguments string cannot be parsed. */ function shortcode_parse_atts( $text ) { $atts = array(); @@ -635,8 +637,6 @@ function shortcode_parse_atts( $text ) { } } } - } else { - $atts = ltrim( $text ); } return $atts; diff --git a/tests/phpunit/tests/shortcode.php b/tests/phpunit/tests/shortcode.php index e8aa82c5a8..2c5b04d02e 100644 --- a/tests/phpunit/tests/shortcode.php +++ b/tests/phpunit/tests/shortcode.php @@ -105,9 +105,13 @@ class Tests_Shortcode extends WP_UnitTestCase { return $out; } + /** + * @ticket 59249 + */ public function test_noatts() { do_shortcode( '[test-shortcode-tag /]' ); - $this->assertSame( '', $this->atts ); + $this->assertIsArray( $this->atts ); + $this->assertEmpty( $this->atts ); $this->assertSame( 'test-shortcode-tag', $this->tagname ); } @@ -181,9 +185,13 @@ class Tests_Shortcode extends WP_UnitTestCase { $this->assertSame( 'test-shortcode-tag', $this->tagname ); } + /** + * @ticket 59249 + */ public function test_noatts_enclosing() { do_shortcode( '[test-shortcode-tag]content[/test-shortcode-tag]' ); - $this->assertSame( '', $this->atts ); + $this->assertIsArray( $this->atts ); + $this->assertEmpty( $this->atts ); $this->assertSame( 'content', $this->content ); $this->assertSame( 'test-shortcode-tag', $this->tagname ); } @@ -208,10 +216,14 @@ class Tests_Shortcode extends WP_UnitTestCase { $this->assertSame( 'test-shortcode-tag', $this->tagname ); } + /** + * @ticket 59249 + */ public function test_unclosed() { $out = do_shortcode( '[test-shortcode-tag]' ); $this->assertSame( '', $out ); - $this->assertSame( '', $this->atts ); + $this->assertIsArray( $this->atts ); + $this->assertEmpty( $this->atts ); $this->assertSame( 'test-shortcode-tag', $this->tagname ); } @@ -998,4 +1010,13 @@ EOF; ); $this->assertSame( 'test-shortcode-tag', $this->tagname ); } + + /** + * @ticket 59249 + */ + public function test_shortcode_parse_atts_empty() { + $out = shortcode_parse_atts( '' ); + $this->assertIsArray( $out, 'Return value is not an array' ); + $this->assertEmpty( $out, 'Returned array is not empty' ); + } }