diff -r 490d5cc509ed -r cf61fcea0001 wp/wp-includes/shortcodes.php --- a/wp/wp-includes/shortcodes.php Tue Jun 09 11:14:17 2015 +0000 +++ b/wp/wp-includes/shortcodes.php Mon Oct 14 17:39:30 2019 +0200 @@ -1,7 +1,7 @@ 'no foo', - * 'baz' => 'default baz', - * ), $atts ); - * - * return "foo = {$args['foo']}"; - * } - * add_shortcode( 'bartag', 'bartag_func' ); - * - * Example with enclosed content: - * - * // [baztag]content[/baztag] - * function baztag_func( $atts, $content = '' ) { - * return "content = $content"; - * } - * add_shortcode( 'baztag', 'baztag_func' ); + * Care should be taken through prefixing or other means to ensure that the + * shortcode tag being added is unique and will not conflict with other, + * already-added shortcode tags. In the event of a duplicated tag, the tag + * loaded last will take precedence. * * @since 2.5.0 * - * @uses $shortcode_tags + * @global array $shortcode_tags * - * @param string $tag Shortcode tag to be searched in post content. - * @param callable $func Hook to run when shortcode is found. + * @param string $tag Shortcode tag to be searched in post content. + * @param callable $callback The callback function to run when the shortcode is found. + * Every shortcode callback is passed three parameters by default, + * including an array of attributes (`$atts`), the shortcode content + * or null if not set (`$content`), and finally the shortcode tag + * itself (`$shortcode_tag`), in that order. */ -function add_shortcode($tag, $func) { +function add_shortcode( $tag, $callback ) { 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: 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; + } + + $shortcode_tags[ $tag ] = $callback; } /** @@ -98,7 +84,7 @@ * * @since 2.5.0 * - * @uses $shortcode_tags + * @global array $shortcode_tags * * @param string $tag Shortcode tag to remove hook for. */ @@ -117,7 +103,7 @@ * * @since 2.5.0 * - * @uses $shortcode_tags + * @global array $shortcode_tags */ function remove_all_shortcodes() { global $shortcode_tags; @@ -157,7 +143,7 @@ } 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; @@ -184,9 +170,10 @@ * @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. * @return string Content with shortcodes filtered out. */ -function do_shortcode($content) { +function do_shortcode( $content, $ignore_html = false ) { global $shortcode_tags; if ( false === strpos( $content, '[' ) ) { @@ -196,8 +183,23 @@ if (empty($shortcode_tags) || !is_array($shortcode_tags)) return $content; - $pattern = get_shortcode_regex(); - return preg_replace_callback( "/$pattern/s", 'do_shortcode_tag', $content ); + // Find all registered tag names in $content. + preg_match_all( '@\[([^<>&/\[\]\x00-\x20=]++)@', $content, $matches ); + $tagnames = array_intersect( array_keys( $shortcode_tags ), $matches[1] ); + + if ( empty( $tagnames ) ) { + return $content; + } + + $content = do_shortcodes_in_html_tags( $content, $ignore_html, $tagnames ); + + $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