wp/wp-includes/shortcodes.php
author ymh <ymh.work@gmail.com>
Tue, 09 Jun 2015 03:35:32 +0200
changeset 5 5e2f62d02dcd
parent 0 d970ebf37754
child 7 cf61fcea0001
permissions -rw-r--r--
upgrade wordpress + plugins
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
<?php
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
 * WordPress API for creating bbcode like tags or what WordPress calls
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
 * "shortcodes." The tag and attribute parsing or regular expression code is
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
 * based on the Textpattern tag parser.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
 * A few examples are below:
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
 * [shortcode /]
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
 * [shortcode foo="bar" baz="bing" /]
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
 * [shortcode foo="bar"]content[/shortcode]
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
 * Shortcode tags support attributes and enclosed content, but does not entirely
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
 * support inline shortcodes in other shortcodes. You will have to call the
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
 * shortcode parser in your function to account for that.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
 * {@internal
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
 * Please be aware that the above note was made during the beta of WordPress 2.6
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
 * and in the future may not be accurate. Please update the note when it is no
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
 * longer the case.}}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
 * To apply shortcode tags to content:
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    24
 *     $out = do_shortcode( $content );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    26
 * @link https://codex.wordpress.org/Shortcode_API
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
 * @subpackage Shortcodes
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    30
 * @since 2.5.0
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
 * Container for storing shortcode tags and their hook to call for the shortcode
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    36
 * @since 2.5.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    37
 *
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
 * @name $shortcode_tags
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
 * @var array
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
 * @global array $shortcode_tags
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    42
$shortcode_tags = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
 * Add hook for shortcode tag.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
 * There can only be one hook for each shortcode. Which means that if another
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
 * plugin has a similar shortcode, it will override yours or yours will override
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    49
 * theirs depending on which order the plugins are included and/or ran.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
 * Simplest example of a shortcode tag using the API:
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    53
 *     // [footag foo="bar"]
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    54
 *     function footag_func( $atts ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    55
 *         return "foo = {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    56
 *             $atts[foo]
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    57
 *         }";
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    58
 *     }
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    59
 *     add_shortcode( 'footag', 'footag_func' );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
 * Example with nice attribute defaults:
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    62
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    63
 *     // [bartag foo="bar"]
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    64
 *     function bartag_func( $atts ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    65
 *         $args = shortcode_atts( array(
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    66
 *             'foo' => 'no foo',
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    67
 *             'baz' => 'default baz',
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    68
 *         ), $atts );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    70
 *         return "foo = {$args['foo']}";
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    71
 *     }
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    72
 *     add_shortcode( 'bartag', 'bartag_func' );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    73
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
 * Example with enclosed content:
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    75
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    76
 *     // [baztag]content[/baztag]
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    77
 *     function baztag_func( $atts, $content = '' ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    78
 *         return "content = $content";
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    79
 *     }
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    80
 *     add_shortcode( 'baztag', 'baztag_func' );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    81
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    82
 * @since 2.5.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    83
 *
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    84
 * @uses $shortcode_tags
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    85
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    86
 * @param string $tag Shortcode tag to be searched in post content.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    87
 * @param callable $func Hook to run when shortcode is found.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    88
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    89
function add_shortcode($tag, $func) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    90
	global $shortcode_tags;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    91
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    92
	if ( is_callable($func) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    93
		$shortcode_tags[$tag] = $func;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    94
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    95
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    96
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    97
 * Removes hook for shortcode.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    98
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    99
 * @since 2.5.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   100
 *
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   101
 * @uses $shortcode_tags
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   102
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   103
 * @param string $tag Shortcode tag to remove hook for.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   104
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   105
function remove_shortcode($tag) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   106
	global $shortcode_tags;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   107
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   108
	unset($shortcode_tags[$tag]);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   109
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   110
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   111
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   112
 * Clear all shortcodes.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   113
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   114
 * This function is simple, it clears all of the shortcode tags by replacing the
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   115
 * shortcodes global by a empty array. This is actually a very efficient method
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   116
 * for removing all shortcodes.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   117
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   118
 * @since 2.5.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   119
 *
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   120
 * @uses $shortcode_tags
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   121
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   122
function remove_all_shortcodes() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   123
	global $shortcode_tags;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   124
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   125
	$shortcode_tags = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   126
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   127
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   128
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   129
 * Whether a registered shortcode exists named $tag
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   130
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   131
 * @since 3.6.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   132
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   133
 * @global array $shortcode_tags List of shortcode tags and their callback hooks.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   134
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   135
 * @param string $tag Shortcode tag to check.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   136
 * @return bool Whether the given shortcode exists.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   137
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   138
function shortcode_exists( $tag ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   139
	global $shortcode_tags;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   140
	return array_key_exists( $tag, $shortcode_tags );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   141
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   142
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   143
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   144
 * Whether the passed content contains the specified shortcode
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   145
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   146
 * @since 3.6.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   147
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   148
 * @global array $shortcode_tags
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   149
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   150
 * @param string $content Content to search for shortcodes.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   151
 * @param string $tag     Shortcode tag to check.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   152
 * @return bool Whether the passed content contains the given shortcode.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   153
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   154
function has_shortcode( $content, $tag ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   155
	if ( false === strpos( $content, '[' ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   156
		return false;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   157
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   158
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   159
	if ( shortcode_exists( $tag ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   160
		preg_match_all( '/' . get_shortcode_regex() . '/s', $content, $matches, PREG_SET_ORDER );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   161
		if ( empty( $matches ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   162
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   163
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   164
		foreach ( $matches as $shortcode ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   165
			if ( $tag === $shortcode[2] ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   166
				return true;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   167
			} elseif ( ! empty( $shortcode[5] ) && has_shortcode( $shortcode[5], $tag ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   168
				return true;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   169
			}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   170
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   171
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   172
	return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   173
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   174
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   175
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   176
 * Search content for shortcodes and filter shortcodes through their hooks.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   177
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   178
 * If there are no shortcode tags defined, then the content will be returned
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   179
 * without any filtering. This might cause issues when plugins are disabled but
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   180
 * the shortcode will still show up in the post or content.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   181
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   182
 * @since 2.5.0
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   183
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   184
 * @global array $shortcode_tags List of shortcode tags and their callback hooks.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   185
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   186
 * @param string $content Content to search for shortcodes.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   187
 * @return string Content with shortcodes filtered out.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   188
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   189
function do_shortcode($content) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   190
	global $shortcode_tags;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   191
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   192
	if ( false === strpos( $content, '[' ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   193
		return $content;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   194
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   195
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   196
	if (empty($shortcode_tags) || !is_array($shortcode_tags))
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   197
		return $content;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   198
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   199
	$pattern = get_shortcode_regex();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   200
	return preg_replace_callback( "/$pattern/s", 'do_shortcode_tag', $content );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   201
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   202
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   203
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   204
 * Retrieve the shortcode regular expression for searching.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   205
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   206
 * The regular expression combines the shortcode tags in the regular expression
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   207
 * in a regex class.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   208
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   209
 * The regular expression contains 6 different sub matches to help with parsing.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   210
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   211
 * 1 - An extra [ to allow for escaping shortcodes with double [[]]
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   212
 * 2 - The shortcode name
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   213
 * 3 - The shortcode argument list
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   214
 * 4 - The self closing /
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   215
 * 5 - The content of a shortcode when it wraps some content.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   216
 * 6 - An extra ] to allow for escaping shortcodes with double [[]]
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   217
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   218
 * @since 2.5.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   219
 *
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   220
 * @uses $shortcode_tags
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   221
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   222
 * @return string The shortcode search regular expression
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   223
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   224
function get_shortcode_regex() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   225
	global $shortcode_tags;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   226
	$tagnames = array_keys($shortcode_tags);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   227
	$tagregexp = join( '|', array_map('preg_quote', $tagnames) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   228
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   229
	// WARNING! Do not change this regex without changing do_shortcode_tag() and strip_shortcode_tag()
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   230
	// Also, see shortcode_unautop() and shortcode.js.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   231
	return
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   232
		  '\\['                              // Opening bracket
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   233
		. '(\\[?)'                           // 1: Optional second opening bracket for escaping shortcodes: [[tag]]
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   234
		. "($tagregexp)"                     // 2: Shortcode name
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   235
		. '(?![\\w-])'                       // Not followed by word character or hyphen
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   236
		. '('                                // 3: Unroll the loop: Inside the opening shortcode tag
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   237
		.     '[^\\]\\/]*'                   // Not a closing bracket or forward slash
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   238
		.     '(?:'
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   239
		.         '\\/(?!\\])'               // A forward slash not followed by a closing bracket
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   240
		.         '[^\\]\\/]*'               // Not a closing bracket or forward slash
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   241
		.     ')*?'
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   242
		. ')'
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   243
		. '(?:'
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   244
		.     '(\\/)'                        // 4: Self closing tag ...
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   245
		.     '\\]'                          // ... and closing bracket
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   246
		. '|'
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   247
		.     '\\]'                          // Closing bracket
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   248
		.     '(?:'
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   249
		.         '('                        // 5: Unroll the loop: Optionally, anything between the opening and closing shortcode tags
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   250
		.             '[^\\[]*+'             // Not an opening bracket
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   251
		.             '(?:'
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   252
		.                 '\\[(?!\\/\\2\\])' // An opening bracket not followed by the closing shortcode tag
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   253
		.                 '[^\\[]*+'         // Not an opening bracket
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   254
		.             ')*+'
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   255
		.         ')'
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   256
		.         '\\[\\/\\2\\]'             // Closing shortcode tag
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   257
		.     ')?'
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   258
		. ')'
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   259
		. '(\\]?)';                          // 6: Optional second closing brocket for escaping shortcodes: [[tag]]
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   260
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   261
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   262
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   263
 * Regular Expression callable for do_shortcode() for calling shortcode hook.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   264
 * @see get_shortcode_regex for details of the match array contents.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   265
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   266
 * @since 2.5.0
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   267
 * @access private
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   268
 * @uses $shortcode_tags
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   269
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   270
 * @param array $m Regular expression match array
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   271
 * @return mixed False on failure.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   272
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   273
function do_shortcode_tag( $m ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   274
	global $shortcode_tags;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   275
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   276
	// allow [[foo]] syntax for escaping a tag
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   277
	if ( $m[1] == '[' && $m[6] == ']' ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   278
		return substr($m[0], 1, -1);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   279
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   280
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   281
	$tag = $m[2];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   282
	$attr = shortcode_parse_atts( $m[3] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   283
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   284
	if ( isset( $m[5] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   285
		// enclosing tag - extra parameter
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   286
		return $m[1] . call_user_func( $shortcode_tags[$tag], $attr, $m[5], $tag ) . $m[6];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   287
	} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   288
		// self-closing tag
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   289
		return $m[1] . call_user_func( $shortcode_tags[$tag], $attr, null,  $tag ) . $m[6];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   290
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   291
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   292
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   293
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   294
 * Retrieve all attributes from the shortcodes tag.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   295
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   296
 * The attributes list has the attribute name as the key and the value of the
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   297
 * attribute as the value in the key/value pair. This allows for easier
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   298
 * retrieval of the attributes, since all attributes have to be known.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   299
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   300
 * @since 2.5.0
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   301
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   302
 * @param string $text
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   303
 * @return array List of attributes and their value.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   304
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   305
function shortcode_parse_atts($text) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   306
	$atts = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   307
	$pattern = '/(\w+)\s*=\s*"([^"]*)"(?:\s|$)|(\w+)\s*=\s*\'([^\']*)\'(?:\s|$)|(\w+)\s*=\s*([^\s\'"]+)(?:\s|$)|"([^"]*)"(?:\s|$)|(\S+)(?:\s|$)/';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   308
	$text = preg_replace("/[\x{00a0}\x{200b}]+/u", " ", $text);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   309
	if ( preg_match_all($pattern, $text, $match, PREG_SET_ORDER) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   310
		foreach ($match as $m) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   311
			if (!empty($m[1]))
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   312
				$atts[strtolower($m[1])] = stripcslashes($m[2]);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   313
			elseif (!empty($m[3]))
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   314
				$atts[strtolower($m[3])] = stripcslashes($m[4]);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   315
			elseif (!empty($m[5]))
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   316
				$atts[strtolower($m[5])] = stripcslashes($m[6]);
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   317
			elseif (isset($m[7]) && strlen($m[7]))
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   318
				$atts[] = stripcslashes($m[7]);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   319
			elseif (isset($m[8]))
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   320
				$atts[] = stripcslashes($m[8]);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   321
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   322
	} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   323
		$atts = ltrim($text);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   324
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   325
	return $atts;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   326
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   327
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   328
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   329
 * Combine user attributes with known attributes and fill in defaults when needed.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   330
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   331
 * The pairs should be considered to be all of the attributes which are
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   332
 * supported by the caller and given as a list. The returned attributes will
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   333
 * only contain the attributes in the $pairs list.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   334
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   335
 * If the $atts list has unsupported attributes, then they will be ignored and
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   336
 * removed from the final returned list.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   337
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   338
 * @since 2.5.0
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   339
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   340
 * @param array $pairs Entire list of supported attributes and their defaults.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   341
 * @param array $atts User defined attributes in shortcode tag.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   342
 * @param string $shortcode Optional. The name of the shortcode, provided for context to enable filtering
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   343
 * @return array Combined and filtered attribute list.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   344
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   345
function shortcode_atts( $pairs, $atts, $shortcode = '' ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   346
	$atts = (array)$atts;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   347
	$out = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   348
	foreach($pairs as $name => $default) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   349
		if ( array_key_exists($name, $atts) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   350
			$out[$name] = $atts[$name];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   351
		else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   352
			$out[$name] = $default;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   353
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   354
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   355
	 * Filter a shortcode's default attributes.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   356
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   357
	 * If the third parameter of the shortcode_atts() function is present then this filter is available.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   358
	 * The third parameter, $shortcode, is the name of the shortcode.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   359
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   360
	 * @since 3.6.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   361
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   362
	 * @param array $out The output array of shortcode attributes.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   363
	 * @param array $pairs The supported attributes and their defaults.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   364
	 * @param array $atts The user defined shortcode attributes.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   365
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   366
	if ( $shortcode )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   367
		$out = apply_filters( "shortcode_atts_{$shortcode}", $out, $pairs, $atts );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   368
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   369
	return $out;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   370
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   371
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   372
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   373
 * Remove all shortcode tags from the given content.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   374
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   375
 * @since 2.5.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   376
 *
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   377
 * @uses $shortcode_tags
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   378
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   379
 * @param string $content Content to remove shortcode tags.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   380
 * @return string Content without shortcode tags.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   381
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   382
function strip_shortcodes( $content ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   383
	global $shortcode_tags;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   384
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   385
	if ( false === strpos( $content, '[' ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   386
		return $content;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   387
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   388
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   389
	if (empty($shortcode_tags) || !is_array($shortcode_tags))
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   390
		return $content;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   391
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   392
	$pattern = get_shortcode_regex();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   393
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   394
	return preg_replace_callback( "/$pattern/s", 'strip_shortcode_tag', $content );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   395
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   396
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   397
function strip_shortcode_tag( $m ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   398
	// allow [[foo]] syntax for escaping a tag
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   399
	if ( $m[1] == '[' && $m[6] == ']' ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   400
		return substr($m[0], 1, -1);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   401
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   402
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   403
	return $m[1] . $m[6];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   404
}