*
* @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;
}
/**
*
* @since 2.5.0
*
- * @uses $shortcode_tags
+ * @global array $shortcode_tags
*
* @param string $tag Shortcode tag to remove hook for.
*/
*
* @since 2.5.0
*
- * @uses $shortcode_tags
+ * @global array $shortcode_tags
*/
function remove_all_shortcodes() {
global $shortcode_tags;
$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;
}
*
* @since 2.5.0
*
- * @uses $shortcode_tags
+ * @global array $shortcode_tags
*
* @return string The shortcode search regular expression
*/
*
* @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;
$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];
$trans = array( '[' => '[', ']' => ']' );
$content = strtr( $content, $trans );
$trans = array( '[' => '[', ']' => ']' );
-
- $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;
}
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;
$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, '[' );
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;
}
}
}
$element = $front . implode( '', $attributes ) . $back;
-
+
// Now encode any remaining [ or ] chars.
$element = strtr( $element, $trans );
}
-
+
$content = implode( '', $textarr );
-
+
return $content;
}
// Clean up entire string, avoids re-parsing HTML.
$trans = array( '[' => '[', ']' => ']' );
$content = strtr( $content, $trans );
-
+
return $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) {
*
* @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.
*/
*
* @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 );
*
* @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.
// 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] == ']' ) {