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