~canonical-sysadmins/wordpress/4.7.2

« back to all changes in this revision

Viewing changes to wp-includes/shortcodes.php

  • Committer: Jacek Nykis
  • Date: 2015-01-05 16:17:05 UTC
  • Revision ID: jacek.nykis@canonical.com-20150105161705-w544l1h5mcg7u4w9
Initial commit

Show diffs side-by-side

added added

removed removed

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