wp/wp-includes/shortcodes.php
changeset 16 a86126ab1dd4
parent 9 177826044cd9
child 18 be944660c56a
--- a/wp/wp-includes/shortcodes.php	Tue Oct 22 16:11:46 2019 +0200
+++ b/wp/wp-includes/shortcodes.php	Tue Dec 15 13:49:49 2020 +0100
@@ -23,7 +23,7 @@
  *
  *     $out = do_shortcode( $content );
  *
- * @link https://codex.wordpress.org/Shortcode_API
+ * @link https://developer.wordpress.org/plugins/shortcodes/
  *
  * @package WordPress
  * @subpackage Shortcodes
@@ -63,14 +63,14 @@
 function add_shortcode( $tag, $callback ) {
 	global $shortcode_tags;
 
-	if ( '' == trim( $tag ) ) {
+	if ( '' === trim( $tag ) ) {
 		$message = __( 'Invalid shortcode name: Empty name given.' );
 		_doing_it_wrong( __FUNCTION__, $message, '4.4.0' );
 		return;
 	}
 
 	if ( 0 !== preg_match( '@[<>&/\[\]\x00-\x20=]@', $tag ) ) {
-		/* translators: 1: shortcode name, 2: space separated list of reserved characters */
+		/* translators: 1: Shortcode name, 2: Space-separated list of reserved characters. */
 		$message = sprintf( __( 'Invalid shortcode name: %1$s. Do not use spaces or reserved characters: %2$s' ), $tag, '& / < > [ ] =' );
 		_doing_it_wrong( __FUNCTION__, $message, '4.4.0' );
 		return;
@@ -162,6 +162,24 @@
 /**
  * Search content for shortcodes and filter shortcodes through their hooks.
  *
+ * This function is an alias for do_shortcode().
+ *
+ * @since 5.4.0
+ *
+ * @see do_shortcode()
+ *
+ * @param string $content     Content to search for shortcodes.
+ * @param bool   $ignore_html When true, shortcodes inside HTML elements will be skipped.
+ *                            Default false.
+ * @return string Content with shortcodes filtered out.
+ */
+function apply_shortcodes( $content, $ignore_html = false ) {
+	return do_shortcode( $content, $ignore_html );
+}
+
+/**
+ * Search content for shortcodes and filter shortcodes through their hooks.
+ *
  * If there are no shortcode tags defined, then the content will be returned
  * without any filtering. This might cause issues when plugins are disabled but
  * the shortcode will still show up in the post or content.
@@ -170,8 +188,9 @@
  *
  * @global array $shortcode_tags List of shortcode tags and their callback hooks.
  *
- * @param string $content Content to search for shortcodes.
- * @param bool $ignore_html When true, shortcodes inside HTML elements will be skipped.
+ * @param string $content     Content to search for shortcodes.
+ * @param bool   $ignore_html When true, shortcodes inside HTML elements will be skipped.
+ *                            Default false.
  * @return string Content with shortcodes filtered out.
  */
 function do_shortcode( $content, $ignore_html = false ) {
@@ -198,7 +217,7 @@
 	$pattern = get_shortcode_regex( $tagnames );
 	$content = preg_replace_callback( "/$pattern/", 'do_shortcode_tag', $content );
 
-	// Always restore square braces so we don't break things like <!--[if IE ]>
+	// Always restore square braces so we don't break things like <!--[if IE ]>.
 	$content = unescape_invalid_shortcodes( $content );
 
 	return $content;
@@ -235,39 +254,38 @@
 	}
 	$tagregexp = join( '|', array_map( 'preg_quote', $tagnames ) );
 
-	// WARNING! Do not change this regex without changing do_shortcode_tag() and strip_shortcode_tag()
+	// WARNING! Do not change this regex without changing do_shortcode_tag() and strip_shortcode_tag().
 	// Also, see shortcode_unautop() and shortcode.js.
 
 	// phpcs:disable Squiz.Strings.ConcatenationSpacing.PaddingFound -- don't remove regex indentation
-	return
-		'\\['                                // Opening bracket
-		. '(\\[?)'                           // 1: Optional second opening bracket for escaping shortcodes: [[tag]]
-		. "($tagregexp)"                     // 2: Shortcode name
-		. '(?![\\w-])'                       // Not followed by word character or hyphen
-		. '('                                // 3: Unroll the loop: Inside the opening shortcode tag
-		.     '[^\\]\\/]*'                   // Not a closing bracket or forward slash
+	return '\\['                             // Opening bracket.
+		. '(\\[?)'                           // 1: Optional second opening bracket for escaping shortcodes: [[tag]].
+		. "($tagregexp)"                     // 2: Shortcode name.
+		. '(?![\\w-])'                       // Not followed by word character or hyphen.
+		. '('                                // 3: Unroll the loop: Inside the opening shortcode tag.
+		.     '[^\\]\\/]*'                   // Not a closing bracket or forward slash.
 		.     '(?:'
-		.         '\\/(?!\\])'               // A forward slash not followed by a closing bracket
-		.         '[^\\]\\/]*'               // Not a closing bracket or forward slash
+		.         '\\/(?!\\])'               // A forward slash not followed by a closing bracket.
+		.         '[^\\]\\/]*'               // Not a closing bracket or forward slash.
 		.     ')*?'
 		. ')'
 		. '(?:'
-		.     '(\\/)'                        // 4: Self closing tag ...
-		.     '\\]'                          // ... and closing bracket
+		.     '(\\/)'                        // 4: Self closing tag...
+		.     '\\]'                          // ...and closing bracket.
 		. '|'
-		.     '\\]'                          // Closing bracket
+		.     '\\]'                          // Closing bracket.
 		.     '(?:'
-		.         '('                        // 5: Unroll the loop: Optionally, anything between the opening and closing shortcode tags
-		.             '[^\\[]*+'             // Not an opening bracket
+		.         '('                        // 5: Unroll the loop: Optionally, anything between the opening and closing shortcode tags.
+		.             '[^\\[]*+'             // Not an opening bracket.
 		.             '(?:'
-		.                 '\\[(?!\\/\\2\\])' // An opening bracket not followed by the closing shortcode tag
-		.                 '[^\\[]*+'         // Not an opening bracket
+		.                 '\\[(?!\\/\\2\\])' // An opening bracket not followed by the closing shortcode tag.
+		.                 '[^\\[]*+'         // Not an opening bracket.
 		.             ')*+'
 		.         ')'
-		.         '\\[\\/\\2\\]'             // Closing shortcode tag
+		.         '\\[\\/\\2\\]'             // Closing shortcode tag.
 		.     ')?'
 		. ')'
-		. '(\\]?)';                          // 6: Optional second closing brocket for escaping shortcodes: [[tag]]
+		. '(\\]?)';                          // 6: Optional second closing brocket for escaping shortcodes: [[tag]].
 	// phpcs:enable
 }
 
@@ -287,8 +305,8 @@
 function do_shortcode_tag( $m ) {
 	global $shortcode_tags;
 
-	// allow [[foo]] syntax for escaping a tag
-	if ( $m[1] == '[' && $m[6] == ']' ) {
+	// Allow [[foo]] syntax for escaping a tag.
+	if ( '[' === $m[1] && ']' === $m[6] ) {
 		return substr( $m[0], 1, -1 );
 	}
 
@@ -296,7 +314,7 @@
 	$attr = shortcode_parse_atts( $m[3] );
 
 	if ( ! is_callable( $shortcode_tags[ $tag ] ) ) {
-		/* translators: %s: shortcode tag */
+		/* translators: %s: Shortcode tag. */
 		$message = sprintf( __( 'Attempting to parse a shortcode without a valid callback: %s' ), $tag );
 		_doing_it_wrong( __FUNCTION__, $message, '4.3.0' );
 		return $m[0];
@@ -305,12 +323,12 @@
 	/**
 	 * Filters whether to call a shortcode callback.
 	 *
-	 * Passing a truthy value to the filter will effectively short-circuit the
+	 * Returning a non-false value from filter will short-circuit the
 	 * shortcode generation process, returning that value instead.
 	 *
 	 * @since 4.7.0
 	 *
-	 * @param bool|string $return      Short-circuit return value. Either false or the value to replace the shortcode with.
+	 * @param false|string $return      Short-circuit return value. Either false or the value to replace the shortcode with.
 	 * @param string       $tag         Shortcode name.
 	 * @param array|string $attr        Shortcode attributes array or empty string.
 	 * @param array        $m           Regular expression match array.
@@ -347,9 +365,9 @@
  *
  * @since 4.2.3
  *
- * @param string $content Content to search for shortcodes
- * @param bool $ignore_html When true, all square braces inside elements will be encoded.
- * @param array $tagnames List of shortcodes to find.
+ * @param string $content     Content to search for shortcodes.
+ * @param bool   $ignore_html When true, all square braces inside elements will be encoded.
+ * @param array  $tagnames    List of shortcodes to find.
  * @return string Content with shortcodes filtered out.
  */
 function do_shortcodes_in_html_tags( $content, $ignore_html, $tagnames ) {
@@ -368,7 +386,7 @@
 	$textarr = wp_html_split( $content );
 
 	foreach ( $textarr as &$element ) {
-		if ( '' == $element || '<' !== $element[0] ) {
+		if ( '' === $element || '<' !== $element[0] ) {
 			continue;
 		}
 
@@ -377,14 +395,14 @@
 		if ( $noopen || $noclose ) {
 			// This element does not contain shortcodes.
 			if ( $noopen xor $noclose ) {
-				// Need to encode stray [ or ] chars.
+				// Need to encode stray '[' or ']' chars.
 				$element = strtr( $element, $trans );
 			}
 			continue;
 		}
 
 		if ( $ignore_html || '<!--' === substr( $element, 0, 4 ) || '<![CDATA[' === substr( $element, 0, 9 ) ) {
-			// Encode all [ and ] chars.
+			// Encode all '[' and ']' chars.
 			$element = strtr( $element, $trans );
 			continue;
 		}
@@ -396,12 +414,12 @@
 				$element = preg_replace_callback( "/$pattern/", 'do_shortcode_tag', $element );
 			}
 
-			// Looks like we found some crazy unfiltered HTML.  Skipping it for sanity.
+			// Looks like we found some crazy unfiltered HTML. Skipping it for sanity.
 			$element = strtr( $element, $trans );
 			continue;
 		}
 
-		// Get element name
+		// Get element name.
 		$front   = array_shift( $attributes );
 		$back    = array_pop( $attributes );
 		$matches = array();
@@ -413,18 +431,20 @@
 			$open  = strpos( $attr, '[' );
 			$close = strpos( $attr, ']' );
 			if ( false === $open || false === $close ) {
-				continue; // Go to next attribute.  Square braces will be escaped at end of loop.
+				continue; // Go to next attribute. Square braces will be escaped at end of loop.
 			}
 			$double = strpos( $attr, '"' );
 			$single = strpos( $attr, "'" );
 			if ( ( false === $single || $open < $single ) && ( false === $double || $open < $double ) ) {
-				// $attr like '[shortcode]' or 'name = [shortcode]' implies unfiltered_html.
-				// In this specific situation we assume KSES did not run because the input
-				// was written by an administrator, so we should avoid changing the output
-				// and we do not need to run KSES here.
+				/*
+				 * $attr like '[shortcode]' or 'name = [shortcode]' implies unfiltered_html.
+				 * In this specific situation we assume KSES did not run because the input
+				 * was written by an administrator, so we should avoid changing the output
+				 * and we do not need to run KSES here.
+				 */
 				$attr = preg_replace_callback( "/$pattern/", 'do_shortcode_tag', $attr );
 			} else {
-				// $attr like 'name = "[shortcode]"' or "name = '[shortcode]'"
+				// $attr like 'name = "[shortcode]"' or "name = '[shortcode]'".
 				// We do not know if $content was unfiltered. Assume KSES ran before shortcodes.
 				$count    = 0;
 				$new_attr = preg_replace_callback( "/$pattern/", 'do_shortcode_tag', $attr, -1, $count );
@@ -440,7 +460,7 @@
 		}
 		$element = $front . implode( '', $attributes ) . $back;
 
-		// Now encode any remaining [ or ] chars.
+		// Now encode any remaining '[' or ']' chars.
 		$element = strtr( $element, $trans );
 	}
 
@@ -491,8 +511,8 @@
  *
  * @param string $text
  * @return array|string List of attribute values.
- *                      Returns empty array if trim( $text ) == '""'.
- *                      Returns empty string if trim( $text ) == ''.
+ *                      Returns empty array if '""' === trim( $text ).
+ *                      Returns empty string if '' === trim( $text ).
  *                      All other matches are checked for not empty().
  */
 function shortcode_parse_atts( $text ) {
@@ -516,7 +536,7 @@
 			}
 		}
 
-		// 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 +547,7 @@
 	} else {
 		$atts = ltrim( $text );
 	}
+
 	return $atts;
 }
 
@@ -557,21 +578,22 @@
 			$out[ $name ] = $default;
 		}
 	}
-	/**
-	 * Filters a shortcode's default attributes.
-	 *
-	 * If the third parameter of the shortcode_atts() function is present then this filter is available.
-	 * The third parameter, $shortcode, is the name of the shortcode.
-	 *
-	 * @since 3.6.0
-	 * @since 4.4.0 Added the `$shortcode` parameter.
-	 *
-	 * @param array  $out       The output array of shortcode attributes.
-	 * @param array  $pairs     The supported attributes and their defaults.
-	 * @param array  $atts      The user defined shortcode attributes.
-	 * @param string $shortcode The shortcode name.
-	 */
+
 	if ( $shortcode ) {
+		/**
+		 * Filters shortcode attributes.
+		 *
+		 * If the third parameter of the shortcode_atts() function is present then this filter is available.
+		 * The third parameter, $shortcode, is the name of the shortcode.
+		 *
+		 * @since 3.6.0
+		 * @since 4.4.0 Added the `$shortcode` parameter.
+		 *
+		 * @param array  $out       The output array of shortcode attributes.
+		 * @param array  $pairs     The supported attributes and their defaults.
+		 * @param array  $atts      The user defined shortcode attributes.
+		 * @param string $shortcode The shortcode name.
+		 */
 		$out = apply_filters( "shortcode_atts_{$shortcode}", $out, $pairs, $atts, $shortcode );
 	}
 
@@ -625,7 +647,7 @@
 	$pattern = get_shortcode_regex( $tagnames );
 	$content = preg_replace_callback( "/$pattern/", 'strip_shortcode_tag', $content );
 
-	// Always restore square braces so we don't break things like <!--[if IE ]>
+	// Always restore square braces so we don't break things like <!--[if IE ]>.
 	$content = unescape_invalid_shortcodes( $content );
 
 	return $content;
@@ -640,8 +662,8 @@
  * @return string|false The content stripped of the tag, otherwise false.
  */
 function strip_shortcode_tag( $m ) {
-	// allow [[foo]] syntax for escaping a tag
-	if ( $m[1] == '[' && $m[6] == ']' ) {
+	// Allow [[foo]] syntax for escaping a tag.
+	if ( '[' === $m[1] && ']' === $m[6] ) {
 		return substr( $m[0], 1, -1 );
 	}