author | ymh <ymh.work@gmail.com> |
Wed, 21 Sep 2022 18:19:35 +0200 | |
changeset 18 | be944660c56a |
parent 16 | a86126ab1dd4 |
child 21 | 48c4eec2b7e6 |
permissions | -rw-r--r-- |
0 | 1 |
<?php |
2 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3 |
* WordPress API for creating bbcode-like tags or what WordPress calls |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4 |
* "shortcodes". The tag and attribute parsing or regular expression code is |
0 | 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 |
* |
|
5 | 24 |
* $out = do_shortcode( $content ); |
0 | 25 |
* |
16 | 26 |
* @link https://developer.wordpress.org/plugins/shortcodes/ |
0 | 27 |
* |
28 |
* @package WordPress |
|
29 |
* @subpackage Shortcodes |
|
5 | 30 |
* @since 2.5.0 |
0 | 31 |
*/ |
32 |
||
33 |
/** |
|
34 |
* Container for storing shortcode tags and their hook to call for the shortcode |
|
35 |
* |
|
5 | 36 |
* @since 2.5.0 |
37 |
* |
|
0 | 38 |
* @name $shortcode_tags |
39 |
* @var array |
|
40 |
* @global array $shortcode_tags |
|
41 |
*/ |
|
42 |
$shortcode_tags = array(); |
|
43 |
||
44 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
45 |
* Adds a new shortcode. |
0 | 46 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
47 |
* Care should be taken through prefixing or other means to ensure that the |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
48 |
* shortcode tag being added is unique and will not conflict with other, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
49 |
* already-added shortcode tags. In the event of a duplicated tag, the tag |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
50 |
* loaded last will take precedence. |
0 | 51 |
* |
5 | 52 |
* @since 2.5.0 |
53 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
54 |
* @global array $shortcode_tags |
0 | 55 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
56 |
* @param string $tag Shortcode tag to be searched in post content. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
57 |
* @param callable $callback The callback function to run when the shortcode is found. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
58 |
* Every shortcode callback is passed three parameters by default, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
59 |
* including an array of attributes (`$atts`), the shortcode content |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
60 |
* or null if not set (`$content`), and finally the shortcode tag |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
61 |
* itself (`$shortcode_tag`), in that order. |
0 | 62 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
63 |
function add_shortcode( $tag, $callback ) { |
0 | 64 |
global $shortcode_tags; |
65 |
||
16 | 66 |
if ( '' === trim( $tag ) ) { |
18 | 67 |
_doing_it_wrong( |
68 |
__FUNCTION__, |
|
69 |
__( 'Invalid shortcode name: Empty name given.' ), |
|
70 |
'4.4.0' |
|
71 |
); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
72 |
return; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
73 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
74 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
75 |
if ( 0 !== preg_match( '@[<>&/\[\]\x00-\x20=]@', $tag ) ) { |
18 | 76 |
_doing_it_wrong( |
77 |
__FUNCTION__, |
|
78 |
sprintf( |
|
79 |
/* translators: 1: Shortcode name, 2: Space-separated list of reserved characters. */ |
|
80 |
__( 'Invalid shortcode name: %1$s. Do not use spaces or reserved characters: %2$s' ), |
|
81 |
$tag, |
|
82 |
'& / < > [ ] =' |
|
83 |
), |
|
84 |
'4.4.0' |
|
85 |
); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
86 |
return; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
87 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
88 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
89 |
$shortcode_tags[ $tag ] = $callback; |
0 | 90 |
} |
91 |
||
92 |
/** |
|
93 |
* Removes hook for shortcode. |
|
94 |
* |
|
5 | 95 |
* @since 2.5.0 |
96 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
97 |
* @global array $shortcode_tags |
0 | 98 |
* |
5 | 99 |
* @param string $tag Shortcode tag to remove hook for. |
0 | 100 |
*/ |
9 | 101 |
function remove_shortcode( $tag ) { |
0 | 102 |
global $shortcode_tags; |
103 |
||
9 | 104 |
unset( $shortcode_tags[ $tag ] ); |
0 | 105 |
} |
106 |
||
107 |
/** |
|
108 |
* Clear all shortcodes. |
|
109 |
* |
|
110 |
* This function is simple, it clears all of the shortcode tags by replacing the |
|
111 |
* shortcodes global by a empty array. This is actually a very efficient method |
|
112 |
* for removing all shortcodes. |
|
113 |
* |
|
5 | 114 |
* @since 2.5.0 |
115 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
116 |
* @global array $shortcode_tags |
0 | 117 |
*/ |
118 |
function remove_all_shortcodes() { |
|
119 |
global $shortcode_tags; |
|
120 |
||
121 |
$shortcode_tags = array(); |
|
122 |
} |
|
123 |
||
124 |
/** |
|
125 |
* Whether a registered shortcode exists named $tag |
|
126 |
* |
|
127 |
* @since 3.6.0 |
|
128 |
* |
|
5 | 129 |
* @global array $shortcode_tags List of shortcode tags and their callback hooks. |
130 |
* |
|
131 |
* @param string $tag Shortcode tag to check. |
|
132 |
* @return bool Whether the given shortcode exists. |
|
0 | 133 |
*/ |
134 |
function shortcode_exists( $tag ) { |
|
135 |
global $shortcode_tags; |
|
136 |
return array_key_exists( $tag, $shortcode_tags ); |
|
137 |
} |
|
138 |
||
139 |
/** |
|
140 |
* Whether the passed content contains the specified shortcode |
|
141 |
* |
|
142 |
* @since 3.6.0 |
|
143 |
* |
|
144 |
* @global array $shortcode_tags |
|
5 | 145 |
* |
146 |
* @param string $content Content to search for shortcodes. |
|
147 |
* @param string $tag Shortcode tag to check. |
|
148 |
* @return bool Whether the passed content contains the given shortcode. |
|
0 | 149 |
*/ |
150 |
function has_shortcode( $content, $tag ) { |
|
5 | 151 |
if ( false === strpos( $content, '[' ) ) { |
152 |
return false; |
|
153 |
} |
|
154 |
||
0 | 155 |
if ( shortcode_exists( $tag ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
156 |
preg_match_all( '/' . get_shortcode_regex() . '/', $content, $matches, PREG_SET_ORDER ); |
9 | 157 |
if ( empty( $matches ) ) { |
0 | 158 |
return false; |
9 | 159 |
} |
0 | 160 |
|
161 |
foreach ( $matches as $shortcode ) { |
|
5 | 162 |
if ( $tag === $shortcode[2] ) { |
0 | 163 |
return true; |
5 | 164 |
} elseif ( ! empty( $shortcode[5] ) && has_shortcode( $shortcode[5], $tag ) ) { |
165 |
return true; |
|
166 |
} |
|
0 | 167 |
} |
168 |
} |
|
169 |
return false; |
|
170 |
} |
|
171 |
||
172 |
/** |
|
173 |
* Search content for shortcodes and filter shortcodes through their hooks. |
|
174 |
* |
|
16 | 175 |
* This function is an alias for do_shortcode(). |
176 |
* |
|
177 |
* @since 5.4.0 |
|
178 |
* |
|
179 |
* @see do_shortcode() |
|
180 |
* |
|
181 |
* @param string $content Content to search for shortcodes. |
|
182 |
* @param bool $ignore_html When true, shortcodes inside HTML elements will be skipped. |
|
183 |
* Default false. |
|
184 |
* @return string Content with shortcodes filtered out. |
|
185 |
*/ |
|
186 |
function apply_shortcodes( $content, $ignore_html = false ) { |
|
187 |
return do_shortcode( $content, $ignore_html ); |
|
188 |
} |
|
189 |
||
190 |
/** |
|
191 |
* Search content for shortcodes and filter shortcodes through their hooks. |
|
192 |
* |
|
0 | 193 |
* If there are no shortcode tags defined, then the content will be returned |
194 |
* without any filtering. This might cause issues when plugins are disabled but |
|
195 |
* the shortcode will still show up in the post or content. |
|
196 |
* |
|
5 | 197 |
* @since 2.5.0 |
0 | 198 |
* |
5 | 199 |
* @global array $shortcode_tags List of shortcode tags and their callback hooks. |
200 |
* |
|
16 | 201 |
* @param string $content Content to search for shortcodes. |
202 |
* @param bool $ignore_html When true, shortcodes inside HTML elements will be skipped. |
|
203 |
* Default false. |
|
0 | 204 |
* @return string Content with shortcodes filtered out. |
205 |
*/ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
206 |
function do_shortcode( $content, $ignore_html = false ) { |
0 | 207 |
global $shortcode_tags; |
208 |
||
5 | 209 |
if ( false === strpos( $content, '[' ) ) { |
210 |
return $content; |
|
211 |
} |
|
212 |
||
9 | 213 |
if ( empty( $shortcode_tags ) || ! is_array( $shortcode_tags ) ) { |
0 | 214 |
return $content; |
9 | 215 |
} |
0 | 216 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
217 |
// Find all registered tag names in $content. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
218 |
preg_match_all( '@\[([^<>&/\[\]\x00-\x20=]++)@', $content, $matches ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
219 |
$tagnames = array_intersect( array_keys( $shortcode_tags ), $matches[1] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
220 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
221 |
if ( empty( $tagnames ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
222 |
return $content; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
223 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
224 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
225 |
$content = do_shortcodes_in_html_tags( $content, $ignore_html, $tagnames ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
226 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
227 |
$pattern = get_shortcode_regex( $tagnames ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
228 |
$content = preg_replace_callback( "/$pattern/", 'do_shortcode_tag', $content ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
229 |
|
16 | 230 |
// Always restore square braces so we don't break things like <!--[if IE ]>. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
231 |
$content = unescape_invalid_shortcodes( $content ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
232 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
233 |
return $content; |
0 | 234 |
} |
235 |
||
236 |
/** |
|
237 |
* Retrieve the shortcode regular expression for searching. |
|
238 |
* |
|
239 |
* The regular expression combines the shortcode tags in the regular expression |
|
240 |
* in a regex class. |
|
241 |
* |
|
242 |
* The regular expression contains 6 different sub matches to help with parsing. |
|
243 |
* |
|
244 |
* 1 - An extra [ to allow for escaping shortcodes with double [[]] |
|
245 |
* 2 - The shortcode name |
|
246 |
* 3 - The shortcode argument list |
|
247 |
* 4 - The self closing / |
|
248 |
* 5 - The content of a shortcode when it wraps some content. |
|
249 |
* 6 - An extra ] to allow for escaping shortcodes with double [[]] |
|
250 |
* |
|
5 | 251 |
* @since 2.5.0 |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
252 |
* @since 4.4.0 Added the `$tagnames` parameter. |
5 | 253 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
254 |
* @global array $shortcode_tags |
0 | 255 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
256 |
* @param array $tagnames Optional. List of shortcodes to find. Defaults to all registered shortcodes. |
0 | 257 |
* @return string The shortcode search regular expression |
258 |
*/ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
259 |
function get_shortcode_regex( $tagnames = null ) { |
0 | 260 |
global $shortcode_tags; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
261 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
262 |
if ( empty( $tagnames ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
263 |
$tagnames = array_keys( $shortcode_tags ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
264 |
} |
18 | 265 |
$tagregexp = implode( '|', array_map( 'preg_quote', $tagnames ) ); |
0 | 266 |
|
16 | 267 |
// WARNING! Do not change this regex without changing do_shortcode_tag() and strip_shortcode_tag(). |
0 | 268 |
// Also, see shortcode_unautop() and shortcode.js. |
9 | 269 |
|
270 |
// phpcs:disable Squiz.Strings.ConcatenationSpacing.PaddingFound -- don't remove regex indentation |
|
16 | 271 |
return '\\[' // Opening bracket. |
272 |
. '(\\[?)' // 1: Optional second opening bracket for escaping shortcodes: [[tag]]. |
|
273 |
. "($tagregexp)" // 2: Shortcode name. |
|
274 |
. '(?![\\w-])' // Not followed by word character or hyphen. |
|
275 |
. '(' // 3: Unroll the loop: Inside the opening shortcode tag. |
|
276 |
. '[^\\]\\/]*' // Not a closing bracket or forward slash. |
|
0 | 277 |
. '(?:' |
16 | 278 |
. '\\/(?!\\])' // A forward slash not followed by a closing bracket. |
279 |
. '[^\\]\\/]*' // Not a closing bracket or forward slash. |
|
0 | 280 |
. ')*?' |
281 |
. ')' |
|
282 |
. '(?:' |
|
16 | 283 |
. '(\\/)' // 4: Self closing tag... |
284 |
. '\\]' // ...and closing bracket. |
|
0 | 285 |
. '|' |
16 | 286 |
. '\\]' // Closing bracket. |
0 | 287 |
. '(?:' |
16 | 288 |
. '(' // 5: Unroll the loop: Optionally, anything between the opening and closing shortcode tags. |
289 |
. '[^\\[]*+' // Not an opening bracket. |
|
0 | 290 |
. '(?:' |
16 | 291 |
. '\\[(?!\\/\\2\\])' // An opening bracket not followed by the closing shortcode tag. |
292 |
. '[^\\[]*+' // Not an opening bracket. |
|
0 | 293 |
. ')*+' |
294 |
. ')' |
|
16 | 295 |
. '\\[\\/\\2\\]' // Closing shortcode tag. |
0 | 296 |
. ')?' |
297 |
. ')' |
|
16 | 298 |
. '(\\]?)'; // 6: Optional second closing brocket for escaping shortcodes: [[tag]]. |
9 | 299 |
// phpcs:enable |
0 | 300 |
} |
301 |
||
302 |
/** |
|
303 |
* Regular Expression callable for do_shortcode() for calling shortcode hook. |
|
9 | 304 |
* |
18 | 305 |
* @see get_shortcode_regex() for details of the match array contents. |
0 | 306 |
* |
5 | 307 |
* @since 2.5.0 |
0 | 308 |
* @access private |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
309 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
310 |
* @global array $shortcode_tags |
0 | 311 |
* |
18 | 312 |
* @param array $m Regular expression match array. |
313 |
* @return string|false Shortcode output on success, false on failure. |
|
0 | 314 |
*/ |
315 |
function do_shortcode_tag( $m ) { |
|
316 |
global $shortcode_tags; |
|
317 |
||
16 | 318 |
// Allow [[foo]] syntax for escaping a tag. |
319 |
if ( '[' === $m[1] && ']' === $m[6] ) { |
|
9 | 320 |
return substr( $m[0], 1, -1 ); |
0 | 321 |
} |
322 |
||
9 | 323 |
$tag = $m[2]; |
0 | 324 |
$attr = shortcode_parse_atts( $m[3] ); |
325 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
326 |
if ( ! is_callable( $shortcode_tags[ $tag ] ) ) { |
18 | 327 |
_doing_it_wrong( |
328 |
__FUNCTION__, |
|
329 |
/* translators: %s: Shortcode tag. */ |
|
330 |
sprintf( __( 'Attempting to parse a shortcode without a valid callback: %s' ), $tag ), |
|
331 |
'4.3.0' |
|
332 |
); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
333 |
return $m[0]; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
334 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
335 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
336 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
337 |
* Filters whether to call a shortcode callback. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
338 |
* |
16 | 339 |
* Returning a non-false value from filter will short-circuit the |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
340 |
* shortcode generation process, returning that value instead. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
341 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
342 |
* @since 4.7.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
343 |
* |
16 | 344 |
* @param false|string $return Short-circuit return value. Either false or the value to replace the shortcode with. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
345 |
* @param string $tag Shortcode name. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
346 |
* @param array|string $attr Shortcode attributes array or empty string. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
347 |
* @param array $m Regular expression match array. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
348 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
349 |
$return = apply_filters( 'pre_do_shortcode_tag', false, $tag, $attr, $m ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
350 |
if ( false !== $return ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
351 |
return $return; |
0 | 352 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
353 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
354 |
$content = isset( $m[5] ) ? $m[5] : null; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
355 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
356 |
$output = $m[1] . call_user_func( $shortcode_tags[ $tag ], $attr, $content, $tag ) . $m[6]; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
357 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
358 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
359 |
* Filters the output created by a shortcode callback. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
360 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
361 |
* @since 4.7.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
362 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
363 |
* @param string $output Shortcode output. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
364 |
* @param string $tag Shortcode name. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
365 |
* @param array|string $attr Shortcode attributes array or empty string. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
366 |
* @param array $m Regular expression match array. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
367 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
368 |
return apply_filters( 'do_shortcode_tag', $output, $tag, $attr, $m ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
369 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
370 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
371 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
372 |
* Search only inside HTML elements for shortcodes and process them. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
373 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
374 |
* Any [ or ] characters remaining inside elements will be HTML encoded |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
375 |
* to prevent interference with shortcodes that are outside the elements. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
376 |
* Assumes $content processed by KSES already. Users with unfiltered_html |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
377 |
* capability may get unexpected output if angle braces are nested in tags. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
378 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
379 |
* @since 4.2.3 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
380 |
* |
16 | 381 |
* @param string $content Content to search for shortcodes. |
382 |
* @param bool $ignore_html When true, all square braces inside elements will be encoded. |
|
383 |
* @param array $tagnames List of shortcodes to find. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
384 |
* @return string Content with shortcodes filtered out. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
385 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
386 |
function do_shortcodes_in_html_tags( $content, $ignore_html, $tagnames ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
387 |
// Normalize entities in unfiltered HTML before adding placeholders. |
9 | 388 |
$trans = array( |
389 |
'[' => '[', |
|
390 |
']' => ']', |
|
391 |
); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
392 |
$content = strtr( $content, $trans ); |
9 | 393 |
$trans = array( |
394 |
'[' => '[', |
|
395 |
']' => ']', |
|
396 |
); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
397 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
398 |
$pattern = get_shortcode_regex( $tagnames ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
399 |
$textarr = wp_html_split( $content ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
400 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
401 |
foreach ( $textarr as &$element ) { |
16 | 402 |
if ( '' === $element || '<' !== $element[0] ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
403 |
continue; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
404 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
405 |
|
9 | 406 |
$noopen = false === strpos( $element, '[' ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
407 |
$noclose = false === strpos( $element, ']' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
408 |
if ( $noopen || $noclose ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
409 |
// This element does not contain shortcodes. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
410 |
if ( $noopen xor $noclose ) { |
16 | 411 |
// Need to encode stray '[' or ']' chars. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
412 |
$element = strtr( $element, $trans ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
413 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
414 |
continue; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
415 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
416 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
417 |
if ( $ignore_html || '<!--' === substr( $element, 0, 4 ) || '<![CDATA[' === substr( $element, 0, 9 ) ) { |
16 | 418 |
// Encode all '[' and ']' chars. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
419 |
$element = strtr( $element, $trans ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
420 |
continue; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
421 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
422 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
423 |
$attributes = wp_kses_attr_parse( $element ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
424 |
if ( false === $attributes ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
425 |
// Some plugins are doing things like [name] <[email]>. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
426 |
if ( 1 === preg_match( '%^<\s*\[\[?[^\[\]]+\]%', $element ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
427 |
$element = preg_replace_callback( "/$pattern/", 'do_shortcode_tag', $element ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
428 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
429 |
|
16 | 430 |
// Looks like we found some crazy unfiltered HTML. Skipping it for sanity. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
431 |
$element = strtr( $element, $trans ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
432 |
continue; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
433 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
434 |
|
16 | 435 |
// Get element name. |
9 | 436 |
$front = array_shift( $attributes ); |
437 |
$back = array_pop( $attributes ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
438 |
$matches = array(); |
9 | 439 |
preg_match( '%[a-zA-Z0-9]+%', $front, $matches ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
440 |
$elname = $matches[0]; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
441 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
442 |
// Look for shortcodes in each attribute separately. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
443 |
foreach ( $attributes as &$attr ) { |
9 | 444 |
$open = strpos( $attr, '[' ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
445 |
$close = strpos( $attr, ']' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
446 |
if ( false === $open || false === $close ) { |
16 | 447 |
continue; // Go to next attribute. Square braces will be escaped at end of loop. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
448 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
449 |
$double = strpos( $attr, '"' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
450 |
$single = strpos( $attr, "'" ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
451 |
if ( ( false === $single || $open < $single ) && ( false === $double || $open < $double ) ) { |
16 | 452 |
/* |
453 |
* $attr like '[shortcode]' or 'name = [shortcode]' implies unfiltered_html. |
|
454 |
* In this specific situation we assume KSES did not run because the input |
|
455 |
* was written by an administrator, so we should avoid changing the output |
|
456 |
* and we do not need to run KSES here. |
|
457 |
*/ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
458 |
$attr = preg_replace_callback( "/$pattern/", 'do_shortcode_tag', $attr ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
459 |
} else { |
16 | 460 |
// $attr like 'name = "[shortcode]"' or "name = '[shortcode]'". |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
461 |
// We do not know if $content was unfiltered. Assume KSES ran before shortcodes. |
9 | 462 |
$count = 0; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
463 |
$new_attr = preg_replace_callback( "/$pattern/", 'do_shortcode_tag', $attr, -1, $count ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
464 |
if ( $count > 0 ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
465 |
// Sanitize the shortcode output using KSES. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
466 |
$new_attr = wp_kses_one_attr( $new_attr, $elname ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
467 |
if ( '' !== trim( $new_attr ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
468 |
// The shortcode is safe to use now. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
469 |
$attr = $new_attr; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
470 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
471 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
472 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
473 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
474 |
$element = $front . implode( '', $attributes ) . $back; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
475 |
|
16 | 476 |
// Now encode any remaining '[' or ']' chars. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
477 |
$element = strtr( $element, $trans ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
478 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
479 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
480 |
$content = implode( '', $textarr ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
481 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
482 |
return $content; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
483 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
484 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
485 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
486 |
* Remove placeholders added by do_shortcodes_in_html_tags(). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
487 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
488 |
* @since 4.2.3 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
489 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
490 |
* @param string $content Content to search for placeholders. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
491 |
* @return string Content with placeholders removed. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
492 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
493 |
function unescape_invalid_shortcodes( $content ) { |
9 | 494 |
// Clean up entire string, avoids re-parsing HTML. |
495 |
$trans = array( |
|
496 |
'[' => '[', |
|
497 |
']' => ']', |
|
498 |
); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
499 |
|
9 | 500 |
$content = strtr( $content, $trans ); |
501 |
||
502 |
return $content; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
503 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
504 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
505 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
506 |
* Retrieve the shortcode attributes regex. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
507 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
508 |
* @since 4.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
509 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
510 |
* @return string The shortcode attribute regular expression |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
511 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
512 |
function get_shortcode_atts_regex() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
513 |
return '/([\w-]+)\s*=\s*"([^"]*)"(?:\s|$)|([\w-]+)\s*=\s*\'([^\']*)\'(?:\s|$)|([\w-]+)\s*=\s*([^\s\'"]+)(?:\s|$)|"([^"]*)"(?:\s|$)|\'([^\']*)\'(?:\s|$)|(\S+)(?:\s|$)/'; |
0 | 514 |
} |
515 |
||
516 |
/** |
|
517 |
* Retrieve all attributes from the shortcodes tag. |
|
518 |
* |
|
519 |
* The attributes list has the attribute name as the key and the value of the |
|
520 |
* attribute as the value in the key/value pair. This allows for easier |
|
521 |
* retrieval of the attributes, since all attributes have to be known. |
|
522 |
* |
|
5 | 523 |
* @since 2.5.0 |
0 | 524 |
* |
525 |
* @param string $text |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
526 |
* @return array|string List of attribute values. |
16 | 527 |
* Returns empty array if '""' === trim( $text ). |
528 |
* Returns empty string if '' === trim( $text ). |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
529 |
* All other matches are checked for not empty(). |
0 | 530 |
*/ |
9 | 531 |
function shortcode_parse_atts( $text ) { |
532 |
$atts = array(); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
533 |
$pattern = get_shortcode_atts_regex(); |
9 | 534 |
$text = preg_replace( "/[\x{00a0}\x{200b}]+/u", ' ', $text ); |
535 |
if ( preg_match_all( $pattern, $text, $match, PREG_SET_ORDER ) ) { |
|
536 |
foreach ( $match as $m ) { |
|
537 |
if ( ! empty( $m[1] ) ) { |
|
538 |
$atts[ strtolower( $m[1] ) ] = stripcslashes( $m[2] ); |
|
539 |
} elseif ( ! empty( $m[3] ) ) { |
|
540 |
$atts[ strtolower( $m[3] ) ] = stripcslashes( $m[4] ); |
|
541 |
} elseif ( ! empty( $m[5] ) ) { |
|
542 |
$atts[ strtolower( $m[5] ) ] = stripcslashes( $m[6] ); |
|
543 |
} elseif ( isset( $m[7] ) && strlen( $m[7] ) ) { |
|
544 |
$atts[] = stripcslashes( $m[7] ); |
|
545 |
} elseif ( isset( $m[8] ) && strlen( $m[8] ) ) { |
|
546 |
$atts[] = stripcslashes( $m[8] ); |
|
547 |
} elseif ( isset( $m[9] ) ) { |
|
548 |
$atts[] = stripcslashes( $m[9] ); |
|
549 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
550 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
551 |
|
16 | 552 |
// Reject any unclosed HTML elements. |
9 | 553 |
foreach ( $atts as &$value ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
554 |
if ( false !== strpos( $value, '<' ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
555 |
if ( 1 !== preg_match( '/^[^<]*+(?:<[^>]*+>[^<]*+)*+$/', $value ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
556 |
$value = ''; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
557 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
558 |
} |
0 | 559 |
} |
560 |
} else { |
|
9 | 561 |
$atts = ltrim( $text ); |
0 | 562 |
} |
16 | 563 |
|
0 | 564 |
return $atts; |
565 |
} |
|
566 |
||
567 |
/** |
|
568 |
* Combine user attributes with known attributes and fill in defaults when needed. |
|
569 |
* |
|
570 |
* The pairs should be considered to be all of the attributes which are |
|
571 |
* supported by the caller and given as a list. The returned attributes will |
|
572 |
* only contain the attributes in the $pairs list. |
|
573 |
* |
|
574 |
* If the $atts list has unsupported attributes, then they will be ignored and |
|
575 |
* removed from the final returned list. |
|
576 |
* |
|
5 | 577 |
* @since 2.5.0 |
0 | 578 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
579 |
* @param array $pairs Entire list of supported attributes and their defaults. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
580 |
* @param array $atts User defined attributes in shortcode tag. |
0 | 581 |
* @param string $shortcode Optional. The name of the shortcode, provided for context to enable filtering |
582 |
* @return array Combined and filtered attribute list. |
|
583 |
*/ |
|
584 |
function shortcode_atts( $pairs, $atts, $shortcode = '' ) { |
|
9 | 585 |
$atts = (array) $atts; |
586 |
$out = array(); |
|
587 |
foreach ( $pairs as $name => $default ) { |
|
588 |
if ( array_key_exists( $name, $atts ) ) { |
|
589 |
$out[ $name ] = $atts[ $name ]; |
|
590 |
} else { |
|
591 |
$out[ $name ] = $default; |
|
592 |
} |
|
0 | 593 |
} |
16 | 594 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
595 |
if ( $shortcode ) { |
16 | 596 |
/** |
597 |
* Filters shortcode attributes. |
|
598 |
* |
|
599 |
* If the third parameter of the shortcode_atts() function is present then this filter is available. |
|
600 |
* The third parameter, $shortcode, is the name of the shortcode. |
|
601 |
* |
|
602 |
* @since 3.6.0 |
|
603 |
* @since 4.4.0 Added the `$shortcode` parameter. |
|
604 |
* |
|
605 |
* @param array $out The output array of shortcode attributes. |
|
606 |
* @param array $pairs The supported attributes and their defaults. |
|
607 |
* @param array $atts The user defined shortcode attributes. |
|
608 |
* @param string $shortcode The shortcode name. |
|
609 |
*/ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
610 |
$out = apply_filters( "shortcode_atts_{$shortcode}", $out, $pairs, $atts, $shortcode ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
611 |
} |
0 | 612 |
|
613 |
return $out; |
|
614 |
} |
|
615 |
||
616 |
/** |
|
617 |
* Remove all shortcode tags from the given content. |
|
618 |
* |
|
5 | 619 |
* @since 2.5.0 |
620 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
621 |
* @global array $shortcode_tags |
0 | 622 |
* |
623 |
* @param string $content Content to remove shortcode tags. |
|
624 |
* @return string Content without shortcode tags. |
|
625 |
*/ |
|
626 |
function strip_shortcodes( $content ) { |
|
627 |
global $shortcode_tags; |
|
628 |
||
5 | 629 |
if ( false === strpos( $content, '[' ) ) { |
630 |
return $content; |
|
631 |
} |
|
632 |
||
9 | 633 |
if ( empty( $shortcode_tags ) || ! is_array( $shortcode_tags ) ) { |
0 | 634 |
return $content; |
9 | 635 |
} |
0 | 636 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
637 |
// Find all registered tag names in $content. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
638 |
preg_match_all( '@\[([^<>&/\[\]\x00-\x20=]++)@', $content, $matches ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
639 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
640 |
$tags_to_remove = array_keys( $shortcode_tags ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
641 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
642 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
643 |
* Filters the list of shortcode tags to remove from the content. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
644 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
645 |
* @since 4.7.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
646 |
* |
9 | 647 |
* @param array $tags_to_remove Array of shortcode tags to remove. |
648 |
* @param string $content Content shortcodes are being removed from. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
649 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
650 |
$tags_to_remove = apply_filters( 'strip_shortcodes_tagnames', $tags_to_remove, $content ); |
0 | 651 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
652 |
$tagnames = array_intersect( $tags_to_remove, $matches[1] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
653 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
654 |
if ( empty( $tagnames ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
655 |
return $content; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
656 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
657 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
658 |
$content = do_shortcodes_in_html_tags( $content, true, $tagnames ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
659 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
660 |
$pattern = get_shortcode_regex( $tagnames ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
661 |
$content = preg_replace_callback( "/$pattern/", 'strip_shortcode_tag', $content ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
662 |
|
16 | 663 |
// Always restore square braces so we don't break things like <!--[if IE ]>. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
664 |
$content = unescape_invalid_shortcodes( $content ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
665 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
666 |
return $content; |
0 | 667 |
} |
668 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
669 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
670 |
* Strips a shortcode tag based on RegEx matches against post content. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
671 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
672 |
* @since 3.3.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
673 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
674 |
* @param array $m RegEx matches against post content. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
675 |
* @return string|false The content stripped of the tag, otherwise false. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
676 |
*/ |
0 | 677 |
function strip_shortcode_tag( $m ) { |
16 | 678 |
// Allow [[foo]] syntax for escaping a tag. |
679 |
if ( '[' === $m[1] && ']' === $m[6] ) { |
|
9 | 680 |
return substr( $m[0], 1, -1 ); |
0 | 681 |
} |
682 |
||
683 |
return $m[1] . $m[6]; |
|
684 |
} |