From d30053446b41c3c1ba1f3a6a08d10dfb8d778ad2 Mon Sep 17 00:00:00 2001 From: whyisjake Date: Tue, 1 Oct 2019 03:42:58 +0000 Subject: [PATCH] Shortcodes: Improve handling from `shortcode_parse_attts()`. Ensure consistency between `shortcode_parse_attts()` when being used directly. Props mauteri, birgire, SergeyBiryukov, kadamwhite, whyisjake. Fixes #47863. Built from https://develop.svn.wordpress.org/trunk@46369 git-svn-id: http://core.svn.wordpress.org/trunk@46168 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/shortcodes.php | 14 +++++++++++--- wp-includes/version.php | 2 +- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/wp-includes/shortcodes.php b/wp-includes/shortcodes.php index ed042262cc..df29332215 100644 --- a/wp-includes/shortcodes.php +++ b/wp-includes/shortcodes.php @@ -488,8 +488,9 @@ function get_shortcode_atts_regex() { * retrieval of the attributes, since all attributes have to be known. * * @since 2.5.0 + * @since 5.3.0 Support of a full shortcode input. * - * @param string $text + * @param string $text Any single shortcode of any format or key/value pair string. * @return array|string List of attribute values. * Returns empty array if trim( $text ) == '""'. * Returns empty string if trim( $text ) == ''. @@ -498,7 +499,13 @@ function get_shortcode_atts_regex() { function shortcode_parse_atts( $text ) { $atts = array(); $pattern = get_shortcode_atts_regex(); - $text = preg_replace( "/[\x{00a0}\x{200b}]+/u", ' ', $text ); + $text = trim( preg_replace( "/[\x{00a0}\x{200b}]+/u", ' ', $text ) ); + + // Remove everything but attributes from shortcode. + if ( preg_match( '#^\[[\w-]+([^\]]*?)\/?\]#', $text, $matches ) ) { + $text = $matches[1]; + } + if ( preg_match_all( $pattern, $text, $match, PREG_SET_ORDER ) ) { foreach ( $match as $m ) { if ( ! empty( $m[1] ) ) { @@ -516,7 +523,7 @@ function shortcode_parse_atts( $text ) { } } - // Reject any unclosed HTML elements + // Reject any unclosed HTML elements. foreach ( $atts as &$value ) { if ( false !== strpos( $value, '<' ) ) { if ( 1 !== preg_match( '/^[^<]*+(?:<[^>]*+>[^<]*+)*+$/', $value ) ) { @@ -527,6 +534,7 @@ function shortcode_parse_atts( $text ) { } else { $atts = ltrim( $text ); } + return $atts; } diff --git a/wp-includes/version.php b/wp-includes/version.php index fc8f1635ce..fbd49b9ba7 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -13,7 +13,7 @@ * * @global string $wp_version */ -$wp_version = '5.3-beta2-46368'; +$wp_version = '5.3-beta2-46369'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.