]> scripts.mit.edu Git - autoinstalls/wordpress.git/blobdiff - wp-includes/shortcodes.php
WordPress 4.3-scripts
[autoinstalls/wordpress.git] / wp-includes / shortcodes.php
index 2661201bcc9a787522172af1bcee4094d010d1cb..c63958b12f5e78c4bf7368b0fd1448ff8818b3db 100644 (file)
@@ -81,16 +81,14 @@ $shortcode_tags = array();
  *
  * @since 2.5.0
  *
- * @uses $shortcode_tags
+ * @global array $shortcode_tags
  *
- * @param string $tag Shortcode tag to be searched in post content.
+ * @param string   $tag  Shortcode tag to be searched in post content.
  * @param callable $func Hook to run when shortcode is found.
  */
 function add_shortcode($tag, $func) {
        global $shortcode_tags;
-
-       if ( is_callable($func) )
-               $shortcode_tags[$tag] = $func;
+       $shortcode_tags[ $tag ] = $func;
 }
 
 /**
@@ -98,7 +96,7 @@ function add_shortcode($tag, $func) {
  *
  * @since 2.5.0
  *
- * @uses $shortcode_tags
+ * @global array $shortcode_tags
  *
  * @param string $tag Shortcode tag to remove hook for.
  */
@@ -117,7 +115,7 @@ function remove_shortcode($tag) {
  *
  * @since 2.5.0
  *
- * @uses $shortcode_tags
+ * @global array $shortcode_tags
  */
 function remove_all_shortcodes() {
        global $shortcode_tags;
@@ -210,10 +208,10 @@ function do_shortcode( $content, $ignore_html = false ) {
 
        $pattern = get_shortcode_regex();
        $content = preg_replace_callback( "/$pattern/s", 'do_shortcode_tag', $content );
-       
+
        // Always restore square braces so we don't break things like <!--[if IE ]>
        $content = unescape_invalid_shortcodes( $content );
-       
+
        return $content;
 }
 
@@ -234,7 +232,7 @@ function do_shortcode( $content, $ignore_html = false ) {
  *
  * @since 2.5.0
  *
- * @uses $shortcode_tags
+ * @global array $shortcode_tags
  *
  * @return string The shortcode search regular expression
  */
@@ -282,10 +280,11 @@ function get_shortcode_regex() {
  *
  * @since 2.5.0
  * @access private
- * @uses $shortcode_tags
+ *
+ * @global array $shortcode_tags
  *
  * @param array $m Regular expression match array
- * @return mixed False on failure.
+ * @return string|false False on failure.
  */
 function do_shortcode_tag( $m ) {
        global $shortcode_tags;
@@ -298,6 +297,12 @@ function do_shortcode_tag( $m ) {
        $tag = $m[2];
        $attr = shortcode_parse_atts( $m[3] );
 
+       if ( ! is_callable( $shortcode_tags[ $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];
+       }
+
        if ( isset( $m[5] ) ) {
                // enclosing tag - extra parameter
                return $m[1] . call_user_func( $shortcode_tags[$tag], $attr, $m[5], $tag ) . $m[6];
@@ -326,31 +331,12 @@ function do_shortcodes_in_html_tags( $content, $ignore_html ) {
        $trans = array( '&#91;' => '&#091;', '&#93;' => '&#093;' );
        $content = strtr( $content, $trans );
        $trans = array( '[' => '&#91;', ']' => '&#93;' );
-       
-       $pattern = get_shortcode_regex();
 
-       $comment_regex =
-                 '!'           // Start of comment, after the <.
-               . '(?:'         // Unroll the loop: Consume everything until --> is found.
-               .     '-(?!->)' // Dash not followed by end of comment.
-               .     '[^\-]*+' // Consume non-dashes.
-               . ')*+'         // Loop possessively.
-               . '(?:-->)?';   // End of comment. If not found, match all input.
-
-       $regex =
-                 '/('                   // Capture the entire match.
-               .     '<'                // Find start of element.
-               .     '(?(?=!--)'        // Is this a comment?
-               .         $comment_regex // Find end of comment.
-               .     '|'
-               .         '[^>]*>?'      // Find end of element. If not found, match all input.
-               .     ')'
-               . ')/s';
-
-       $textarr = preg_split( $regex, $content, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY );
+       $pattern = get_shortcode_regex();
+       $textarr = wp_html_split( $content );
 
        foreach ( $textarr as &$element ) {
-               if ( '<' !== $element[0] ) {
+               if ( '' == $element || '<' !== $element[0] ) {
                        continue;
                }
 
@@ -365,7 +351,7 @@ function do_shortcodes_in_html_tags( $content, $ignore_html ) {
                        continue;
                }
 
-               if ( $ignore_html || '<!--' === substr( $element, 0, 4 ) ) {
+               if ( $ignore_html || '<!--' === substr( $element, 0, 4 ) || '<![CDATA[' === substr( $element, 0, 9 ) ) {
                        // Encode all [ and ] chars.
                        $element = strtr( $element, $trans );
                        continue;
@@ -373,18 +359,23 @@ function do_shortcodes_in_html_tags( $content, $ignore_html ) {
 
                $attributes = wp_kses_attr_parse( $element );
                if ( false === $attributes ) {
+                       // Some plugins are doing things like [name] <[email]>.
+                       if ( 1 === preg_match( '%^<\s*\[\[?[^\[\]]+\]%', $element ) ) {
+                               $element = preg_replace_callback( "/$pattern/s", 'do_shortcode_tag', $element );
+                       }
+
                        // Looks like we found some crazy unfiltered HTML.  Skipping it for sanity.
                        $element = strtr( $element, $trans );
                        continue;
                }
-               
+
                // Get element name
                $front = array_shift( $attributes );
                $back = array_pop( $attributes );
                $matches = array();
                preg_match('%[a-zA-Z0-9]+%', $front, $matches);
                $elname = $matches[0];
-               
+
                // Look for shortcodes in each attribute separately.
                foreach ( $attributes as &$attr ) {
                        $open = strpos( $attr, '[' );
@@ -408,7 +399,7 @@ function do_shortcodes_in_html_tags( $content, $ignore_html ) {
                                if ( $count > 0 ) {
                                        // Sanitize the shortcode output using KSES.
                                        $new_attr = wp_kses_one_attr( $new_attr, $elname );
-                                       if ( '' !== $new_attr ) {
+                                       if ( '' !== trim( $new_attr ) ) {
                                                // The shortcode is safe to use now.
                                                $attr = $new_attr;
                                        }
@@ -416,13 +407,13 @@ function do_shortcodes_in_html_tags( $content, $ignore_html ) {
                        }
                }
                $element = $front . implode( '', $attributes ) . $back;
-               
+
                // Now encode any remaining [ or ] chars.
                $element = strtr( $element, $trans );
        }
-       
+
        $content = implode( '', $textarr );
-       
+
        return $content;
 }
 
@@ -438,7 +429,7 @@ function unescape_invalid_shortcodes( $content ) {
         // Clean up entire string, avoids re-parsing HTML.
         $trans = array( '&#91;' => '[', '&#93;' => ']' );
         $content = strtr( $content, $trans );
-        
+
         return $content;
 }
 
@@ -456,7 +447,7 @@ function unescape_invalid_shortcodes( $content ) {
  */
 function shortcode_parse_atts($text) {
        $atts = array();
-       $pattern = '/(\w+)\s*=\s*"([^"]*)"(?:\s|$)|(\w+)\s*=\s*\'([^\']*)\'(?:\s|$)|(\w+)\s*=\s*([^\s\'"]+)(?:\s|$)|"([^"]*)"(?:\s|$)|(\S+)(?:\s|$)/';
+       $pattern = '/([\w-]+)\s*=\s*"([^"]*)"(?:\s|$)|([\w-]+)\s*=\s*\'([^\']*)\'(?:\s|$)|([\w-]+)\s*=\s*([^\s\'"]+)(?:\s|$)|"([^"]*)"(?:\s|$)|(\S+)(?:\s|$)/';
        $text = preg_replace("/[\x{00a0}\x{200b}]+/u", " ", $text);
        if ( preg_match_all($pattern, $text, $match, PREG_SET_ORDER) ) {
                foreach ($match as $m) {
@@ -489,8 +480,8 @@ function shortcode_parse_atts($text) {
  *
  * @since 2.5.0
  *
- * @param array $pairs Entire list of supported attributes and their defaults.
- * @param array $atts User defined attributes in shortcode tag.
+ * @param array  $pairs     Entire list of supported attributes and their defaults.
+ * @param array  $atts      User defined attributes in shortcode tag.
  * @param string $shortcode Optional. The name of the shortcode, provided for context to enable filtering
  * @return array Combined and filtered attribute list.
  */
@@ -511,9 +502,9 @@ function shortcode_atts( $pairs, $atts, $shortcode = '' ) {
         *
         * @since 3.6.0
         *
-        * @param array $out The output array of shortcode attributes.
+        * @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 array $atts  The user defined shortcode attributes.
         */
        if ( $shortcode )
                $out = apply_filters( "shortcode_atts_{$shortcode}", $out, $pairs, $atts );
@@ -526,7 +517,7 @@ function shortcode_atts( $pairs, $atts, $shortcode = '' ) {
  *
  * @since 2.5.0
  *
- * @uses $shortcode_tags
+ * @global array $shortcode_tags
  *
  * @param string $content Content to remove shortcode tags.
  * @return string Content without shortcode tags.
@@ -548,10 +539,15 @@ function strip_shortcodes( $content ) {
 
        // Always restore square braces so we don't break things like <!--[if IE ]>
        $content = unescape_invalid_shortcodes( $content );
-       
+
        return $content;
 }
 
+/**
+ *
+ * @param array $m
+ * @return string|false
+ */
 function strip_shortcode_tag( $m ) {
        // allow [[foo]] syntax for escaping a tag
        if ( $m[1] == '[' && $m[6] == ']' ) {