-
+ E072ADF866FC855BA28C779883F404DE3EBD15A8F0F69E8CFF2100360BEEB96619F7F1FF0656DA29BB0FEB1381159A2D1B75DA1A5F91D8D954F0F5ED36F7DB0F
mp-wp/wp-includes/shortcodes.php
(0 . 0)(1 . 283)
151088 <?php
151089 /**
151090 * WordPress API for creating bbcode like tags or what WordPress calls
151091 * "shortcodes." The tag and attribute parsing or regular expression code is
151092 * based on the Textpattern tag parser.
151093 *
151094 * A few examples are below:
151095 *
151096 * [shortcode /]
151097 * [shortcode foo="bar" baz="bing" /]
151098 * [shortcode foo="bar"]content[/shortcode]
151099 *
151100 * Shortcode tags support attributes and enclosed content, but does not entirely
151101 * support inline shortcodes in other shortcodes. You will have to call the
151102 * shortcode parser in your function to account for that.
151103 *
151104 * {@internal
151105 * Please be aware that the above note was made during the beta of WordPress 2.6
151106 * and in the future may not be accurate. Please update the note when it is no
151107 * longer the case.}}
151108 *
151109 * To apply shortcode tags to content:
151110 *
151111 * <code>
151112 * $out = do_shortcode($content);
151113 * </code>
151114 *
151115 * @link http://codex.wordpress.org/Shortcode_API
151116 *
151117 * @package WordPress
151118 * @subpackage Shortcodes
151119 * @since 2.5
151120 */
151121
151122 /**
151123 * Container for storing shortcode tags and their hook to call for the shortcode
151124 *
151125 * @since 2.5
151126 * @name $shortcode_tags
151127 * @var array
151128 * @global array $shortcode_tags
151129 */
151130 $shortcode_tags = array();
151131
151132 /**
151133 * Add hook for shortcode tag.
151134 *
151135 * There can only be one hook for each shortcode. Which means that if another
151136 * plugin has a similar shortcode, it will override yours or yours will override
151137 * theirs depending on which order the plugins are included and/or ran.
151138 *
151139 * Simplest example of a shortcode tag using the API:
151140 *
151141 * <code>
151142 * // [footag foo="bar"]
151143 * function footag_func($atts) {
151144 * return "foo = {$atts[foo]}";
151145 * }
151146 * add_shortcode('footag', 'footag_func');
151147 * </code>
151148 *
151149 * Example with nice attribute defaults:
151150 *
151151 * <code>
151152 * // [bartag foo="bar"]
151153 * function bartag_func($atts) {
151154 * extract(shortcode_atts(array(
151155 * 'foo' => 'no foo',
151156 * 'baz' => 'default baz',
151157 * ), $atts));
151158 *
151159 * return "foo = {$foo}";
151160 * }
151161 * add_shortcode('bartag', 'bartag_func');
151162 * </code>
151163 *
151164 * Example with enclosed content:
151165 *
151166 * <code>
151167 * // [baztag]content[/baztag]
151168 * function baztag_func($atts, $content='') {
151169 * return "content = $content";
151170 * }
151171 * add_shortcode('baztag', 'baztag_func');
151172 * </code>
151173 *
151174 * @since 2.5
151175 * @uses $shortcode_tags
151176 *
151177 * @param string $tag Shortcode tag to be searched in post content.
151178 * @param callable $func Hook to run when shortcode is found.
151179 */
151180 function add_shortcode($tag, $func) {
151181 global $shortcode_tags;
151182
151183 if ( is_callable($func) )
151184 $shortcode_tags[$tag] = $func;
151185 }
151186
151187 /**
151188 * Removes hook for shortcode.
151189 *
151190 * @since 2.5
151191 * @uses $shortcode_tags
151192 *
151193 * @param string $tag shortcode tag to remove hook for.
151194 */
151195 function remove_shortcode($tag) {
151196 global $shortcode_tags;
151197
151198 unset($shortcode_tags[$tag]);
151199 }
151200
151201 /**
151202 * Clear all shortcodes.
151203 *
151204 * This function is simple, it clears all of the shortcode tags by replacing the
151205 * shortcodes global by a empty array. This is actually a very efficient method
151206 * for removing all shortcodes.
151207 *
151208 * @since 2.5
151209 * @uses $shortcode_tags
151210 */
151211 function remove_all_shortcodes() {
151212 global $shortcode_tags;
151213
151214 $shortcode_tags = array();
151215 }
151216
151217 /**
151218 * Search content for shortcodes and filter shortcodes through their hooks.
151219 *
151220 * If there are no shortcode tags defined, then the content will be returned
151221 * without any filtering. This might cause issues when plugins are disabled but
151222 * the shortcode will still show up in the post or content.
151223 *
151224 * @since 2.5
151225 * @uses $shortcode_tags
151226 * @uses get_shortcode_regex() Gets the search pattern for searching shortcodes.
151227 *
151228 * @param string $content Content to search for shortcodes
151229 * @return string Content with shortcodes filtered out.
151230 */
151231 function do_shortcode($content) {
151232 global $shortcode_tags;
151233
151234 if (empty($shortcode_tags) || !is_array($shortcode_tags))
151235 return $content;
151236
151237 $pattern = get_shortcode_regex();
151238 return preg_replace_callback('/'.$pattern.'/s', 'do_shortcode_tag', $content);
151239 }
151240
151241 /**
151242 * Retrieve the shortcode regular expression for searching.
151243 *
151244 * The regular expression combines the shortcode tags in the regular expression
151245 * in a regex class.
151246 *
151247 * @since 2.5
151248 * @uses $shortcode_tags
151249 *
151250 * @return string The shortcode search regular expression
151251 */
151252 function get_shortcode_regex() {
151253 global $shortcode_tags;
151254 $tagnames = array_keys($shortcode_tags);
151255 $tagregexp = join( '|', array_map('preg_quote', $tagnames) );
151256
151257 return '\[('.$tagregexp.')\b(.*?)(?:(\/))?\](?:(.+?)\[\/\1\])?';
151258 }
151259
151260 /**
151261 * Regular Expression callable for do_shortcode() for calling shortcode hook.
151262 *
151263 * @since 2.5
151264 * @access private
151265 * @uses $shortcode_tags
151266 *
151267 * @param array $m Regular expression match array
151268 * @return mixed False on failure.
151269 */
151270 function do_shortcode_tag($m) {
151271 global $shortcode_tags;
151272
151273 $tag = $m[1];
151274 $attr = shortcode_parse_atts($m[2]);
151275
151276 if ( isset($m[4]) ) {
151277 // enclosing tag - extra parameter
151278 return call_user_func($shortcode_tags[$tag], $attr, $m[4], $tag);
151279 } else {
151280 // self-closing tag
151281 return call_user_func($shortcode_tags[$tag], $attr, NULL, $tag);
151282 }
151283 }
151284
151285 /**
151286 * Retrieve all attributes from the shortcodes tag.
151287 *
151288 * The attributes list has the attribute name as the key and the value of the
151289 * attribute as the value in the key/value pair. This allows for easier
151290 * retrieval of the attributes, since all attributes have to be known.
151291 *
151292 * @since 2.5
151293 *
151294 * @param string $text
151295 * @return array List of attributes and their value.
151296 */
151297 function shortcode_parse_atts($text) {
151298 $atts = array();
151299 $pattern = '/(\w+)\s*=\s*"([^"]*)"(?:\s|$)|(\w+)\s*=\s*\'([^\']*)\'(?:\s|$)|(\w+)\s*=\s*([^\s\'"]+)(?:\s|$)|"([^"]*)"(?:\s|$)|(\S+)(?:\s|$)/';
151300 $text = preg_replace("/[\x{00a0}\x{200b}]+/u", " ", $text);
151301 if ( preg_match_all($pattern, $text, $match, PREG_SET_ORDER) ) {
151302 foreach ($match as $m) {
151303 if (!empty($m[1]))
151304 $atts[strtolower($m[1])] = stripcslashes($m[2]);
151305 elseif (!empty($m[3]))
151306 $atts[strtolower($m[3])] = stripcslashes($m[4]);
151307 elseif (!empty($m[5]))
151308 $atts[strtolower($m[5])] = stripcslashes($m[6]);
151309 elseif (isset($m[7]) and strlen($m[7]))
151310 $atts[] = stripcslashes($m[7]);
151311 elseif (isset($m[8]))
151312 $atts[] = stripcslashes($m[8]);
151313 }
151314 } else {
151315 $atts = ltrim($text);
151316 }
151317 return $atts;
151318 }
151319
151320 /**
151321 * Combine user attributes with known attributes and fill in defaults when needed.
151322 *
151323 * The pairs should be considered to be all of the attributes which are
151324 * supported by the caller and given as a list. The returned attributes will
151325 * only contain the attributes in the $pairs list.
151326 *
151327 * If the $atts list has unsupported attributes, then they will be ignored and
151328 * removed from the final returned list.
151329 *
151330 * @since 2.5
151331 *
151332 * @param array $pairs Entire list of supported attributes and their defaults.
151333 * @param array $atts User defined attributes in shortcode tag.
151334 * @return array Combined and filtered attribute list.
151335 */
151336 function shortcode_atts($pairs, $atts) {
151337 $atts = (array)$atts;
151338 $out = array();
151339 foreach($pairs as $name => $default) {
151340 if ( array_key_exists($name, $atts) )
151341 $out[$name] = $atts[$name];
151342 else
151343 $out[$name] = $default;
151344 }
151345 return $out;
151346 }
151347
151348 /**
151349 * Remove all shortcode tags from the given content.
151350 *
151351 * @since 2.5
151352 * @uses $shortcode_tags
151353 *
151354 * @param string $content Content to remove shortcode tags.
151355 * @return string Content without shortcode tags.
151356 */
151357 function strip_shortcodes( $content ) {
151358 global $shortcode_tags;
151359
151360 if (empty($shortcode_tags) || !is_array($shortcode_tags))
151361 return $content;
151362
151363 $pattern = get_shortcode_regex();
151364
151365 return preg_replace('/'.$pattern.'/s', '', $content);
151366 }
151367
151368 add_filter('the_content', 'do_shortcode', 11); // AFTER wpautop()
151369
151370 ?>