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