X-Git-Url: https://scripts.mit.edu/gitweb/autoinstalls/wordpress.git/blobdiff_plain/5aa86a9053fb0fa15846bb60aac2fb8fdfff524a..fa11948979fd6a4ea5705dc613b239699a459db3:/wp-includes/shortcodes.php diff --git a/wp-includes/shortcodes.php b/wp-includes/shortcodes.php index 1cb36137..dafb3a14 100644 --- a/wp-includes/shortcodes.php +++ b/wp-includes/shortcodes.php @@ -127,6 +127,43 @@ function remove_all_shortcodes() { $shortcode_tags = array(); } +/** + * Whether a registered shortcode exists named $tag + * + * @since 3.6.0 + * + * @global array $shortcode_tags + * @param string $tag + * @return boolean + */ +function shortcode_exists( $tag ) { + global $shortcode_tags; + return array_key_exists( $tag, $shortcode_tags ); +} + +/** + * Whether the passed content contains the specified shortcode + * + * @since 3.6.0 + * + * @global array $shortcode_tags + * @param string $tag + * @return boolean + */ +function has_shortcode( $content, $tag ) { + if ( shortcode_exists( $tag ) ) { + preg_match_all( '/' . get_shortcode_regex() . '/s', $content, $matches, PREG_SET_ORDER ); + if ( empty( $matches ) ) + return false; + + foreach ( $matches as $shortcode ) { + if ( $tag === $shortcode[2] ) + return true; + } + } + return false; +} + /** * Search content for shortcodes and filter shortcodes through their hooks. * @@ -177,11 +214,12 @@ function get_shortcode_regex() { $tagregexp = join( '|', array_map('preg_quote', $tagnames) ); // WARNING! Do not change this regex without changing do_shortcode_tag() and strip_shortcode_tag() + // Also, see shortcode_unautop() and shortcode.js. return '\\[' // Opening bracket . '(\\[?)' // 1: Optional second opening bracket for escaping shortcodes: [[tag]] . "($tagregexp)" // 2: Shortcode name - . '\\b' // Word boundary + . '(?![\\w-])' // Not followed by word character or hyphen . '(' // 3: Unroll the loop: Inside the opening shortcode tag . '[^\\]\\/]*' // Not a closing bracket or forward slash . '(?:' @@ -235,7 +273,7 @@ function do_shortcode_tag( $m ) { return $m[1] . call_user_func( $shortcode_tags[$tag], $attr, $m[5], $tag ) . $m[6]; } else { // self-closing tag - return $m[1] . call_user_func( $shortcode_tags[$tag], $attr, NULL, $tag ) . $m[6]; + return $m[1] . call_user_func( $shortcode_tags[$tag], $attr, null, $tag ) . $m[6]; } } @@ -288,9 +326,10 @@ function shortcode_parse_atts($text) { * * @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. */ -function shortcode_atts($pairs, $atts) { +function shortcode_atts( $pairs, $atts, $shortcode = '' ) { $atts = (array)$atts; $out = array(); foreach($pairs as $name => $default) { @@ -299,6 +338,21 @@ function shortcode_atts($pairs, $atts) { else $out[$name] = $default; } + /** + * Filter 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 + * + * @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. + */ + if ( $shortcode ) + $out = apply_filters( "shortcode_atts_{$shortcode}", $out, $pairs, $atts ); + return $out; } @@ -332,5 +386,3 @@ function strip_shortcode_tag( $m ) { } add_filter('the_content', 'do_shortcode', 11); // AFTER wpautop() - -?>