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.
Built from https://develop.svn.wordpress.org/trunk@57597


git-svn-id: http://core.svn.wordpress.org/trunk@57098 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Pascal Birchler 2024-02-12 16:08:10 +00:00
parent 64b5e4c51a
commit 10b96420a5
3 changed files with 6 additions and 10 deletions

View File

@ -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( '#((?:<a [^>]+>\s*)?<img [^>]+>(?:\s*</a>)?)(.*)#is', $content, $matches ) ) {

View File

@ -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;

View File

@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '6.5-alpha-57596';
$wp_version = '6.5-alpha-57597';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.