X-Git-Url: https://scripts.mit.edu/gitweb/autoinstalls/wordpress.git/blobdiff_plain/38ac4bc40322ecdc4052db4263466573e01fa51f..refs/tags/wordpress-4.4:/wp-includes/shortcodes.php?ds=sidebyside diff --git a/wp-includes/shortcodes.php b/wp-includes/shortcodes.php index 2661201b..22f33c32 100644 --- a/wp-includes/shortcodes.php +++ b/wp-includes/shortcodes.php @@ -81,16 +81,28 @@ $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; + 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: %s: shortcode name */ + $message = sprintf( __( 'Invalid shortcode name: %s. Do not use spaces or reserved characters: & / < > [ ]' ), $tag ); + _doing_it_wrong( __FUNCTION__, $message, '4.4.0' ); + return; + } + + $shortcode_tags[ $tag ] = $func; } /** @@ -98,7 +110,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 +129,7 @@ function remove_shortcode($tag) { * * @since 2.5.0 * - * @uses $shortcode_tags + * @global array $shortcode_tags */ function remove_all_shortcodes() { global $shortcode_tags; @@ -157,7 +169,7 @@ function has_shortcode( $content, $tag ) { } if ( shortcode_exists( $tag ) ) { - preg_match_all( '/' . get_shortcode_regex() . '/s', $content, $matches, PREG_SET_ORDER ); + preg_match_all( '/' . get_shortcode_regex() . '/', $content, $matches, PREG_SET_ORDER ); if ( empty( $matches ) ) return false; @@ -197,23 +209,22 @@ function do_shortcode( $content, $ignore_html = false ) { if (empty($shortcode_tags) || !is_array($shortcode_tags)) return $content; - $tagnames = array_keys($shortcode_tags); - $tagregexp = join( '|', array_map('preg_quote', $tagnames) ); - $pattern = "/\\[($tagregexp)/s"; + // Find all registered tag names in $content. + preg_match_all( '@\[([^<>&/\[\]\x00-\x20]++)@', $content, $matches ); + $tagnames = array_intersect( array_keys( $shortcode_tags ), $matches[1] ); - if ( 1 !== preg_match( $pattern, $content ) ) { - // Avoids parsing HTML when there are no shortcodes or embeds anyway. + if ( empty( $tagnames ) ) { return $content; } - $content = do_shortcodes_in_html_tags( $content, $ignore_html ); + $content = do_shortcodes_in_html_tags( $content, $ignore_html, $tagnames ); + + $pattern = get_shortcode_regex( $tagnames ); + $content = preg_replace_callback( "/$pattern/", 'do_shortcode_tag', $content ); - $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 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( $tagnames ); + $textarr = wp_html_split( $content ); foreach ( $textarr as &$element ) { - if ( '<' !== $element[0] ) { + if ( '' == $element || '<' !== $element[0] ) { continue; } @@ -365,7 +370,7 @@ function do_shortcodes_in_html_tags( $content, $ignore_html ) { continue; } - if ( $ignore_html || '