author | ymh <ymh.work@gmail.com> |
Mon, 14 Oct 2019 17:39:30 +0200 | |
changeset 7 | cf61fcea0001 |
parent 5 | 5e2f62d02dcd |
child 9 | 177826044cd9 |
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 |
* |
5 | 26 |
* @link https://codex.wordpress.org/Shortcode_API |
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 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
66 |
if ( '' == trim( $tag ) ) { |
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 ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
73 |
/* translators: 1: shortcode name, 2: space separated list of reserved characters */ |
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 |
*/ |
91 |
function remove_shortcode($tag) { |
|
92 |
global $shortcode_tags; |
|
93 |
||
94 |
unset($shortcode_tags[$tag]); |
|
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 ); |
0 | 147 |
if ( empty( $matches ) ) |
148 |
return false; |
|
149 |
||
150 |
foreach ( $matches as $shortcode ) { |
|
5 | 151 |
if ( $tag === $shortcode[2] ) { |
0 | 152 |
return true; |
5 | 153 |
} elseif ( ! empty( $shortcode[5] ) && has_shortcode( $shortcode[5], $tag ) ) { |
154 |
return true; |
|
155 |
} |
|
0 | 156 |
} |
157 |
} |
|
158 |
return false; |
|
159 |
} |
|
160 |
||
161 |
/** |
|
162 |
* Search content for shortcodes and filter shortcodes through their hooks. |
|
163 |
* |
|
164 |
* If there are no shortcode tags defined, then the content will be returned |
|
165 |
* without any filtering. This might cause issues when plugins are disabled but |
|
166 |
* the shortcode will still show up in the post or content. |
|
167 |
* |
|
5 | 168 |
* @since 2.5.0 |
0 | 169 |
* |
5 | 170 |
* @global array $shortcode_tags List of shortcode tags and their callback hooks. |
171 |
* |
|
172 |
* @param string $content Content to search for shortcodes. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
173 |
* @param bool $ignore_html When true, shortcodes inside HTML elements will be skipped. |
0 | 174 |
* @return string Content with shortcodes filtered out. |
175 |
*/ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
176 |
function do_shortcode( $content, $ignore_html = false ) { |
0 | 177 |
global $shortcode_tags; |
178 |
||
5 | 179 |
if ( false === strpos( $content, '[' ) ) { |
180 |
return $content; |
|
181 |
} |
|
182 |
||
0 | 183 |
if (empty($shortcode_tags) || !is_array($shortcode_tags)) |
184 |
return $content; |
|
185 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
186 |
// Find all registered tag names in $content. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
187 |
preg_match_all( '@\[([^<>&/\[\]\x00-\x20=]++)@', $content, $matches ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
188 |
$tagnames = array_intersect( array_keys( $shortcode_tags ), $matches[1] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
189 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
190 |
if ( empty( $tagnames ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
191 |
return $content; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
192 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
193 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
194 |
$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
|
195 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
196 |
$pattern = get_shortcode_regex( $tagnames ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
197 |
$content = preg_replace_callback( "/$pattern/", 'do_shortcode_tag', $content ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
198 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
199 |
// Always restore square braces so we don't break things like <!--[if IE ]> |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
200 |
$content = unescape_invalid_shortcodes( $content ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
201 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
202 |
return $content; |
0 | 203 |
} |
204 |
||
205 |
/** |
|
206 |
* Retrieve the shortcode regular expression for searching. |
|
207 |
* |
|
208 |
* The regular expression combines the shortcode tags in the regular expression |
|
209 |
* in a regex class. |
|
210 |
* |
|
211 |
* The regular expression contains 6 different sub matches to help with parsing. |
|
212 |
* |
|
213 |
* 1 - An extra [ to allow for escaping shortcodes with double [[]] |
|
214 |
* 2 - The shortcode name |
|
215 |
* 3 - The shortcode argument list |
|
216 |
* 4 - The self closing / |
|
217 |
* 5 - The content of a shortcode when it wraps some content. |
|
218 |
* 6 - An extra ] to allow for escaping shortcodes with double [[]] |
|
219 |
* |
|
5 | 220 |
* @since 2.5.0 |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
221 |
* @since 4.4.0 Added the `$tagnames` parameter. |
5 | 222 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
223 |
* @global array $shortcode_tags |
0 | 224 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
225 |
* @param array $tagnames Optional. List of shortcodes to find. Defaults to all registered shortcodes. |
0 | 226 |
* @return string The shortcode search regular expression |
227 |
*/ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
228 |
function get_shortcode_regex( $tagnames = null ) { |
0 | 229 |
global $shortcode_tags; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
230 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
231 |
if ( empty( $tagnames ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
232 |
$tagnames = array_keys( $shortcode_tags ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
233 |
} |
0 | 234 |
$tagregexp = join( '|', array_map('preg_quote', $tagnames) ); |
235 |
||
236 |
// WARNING! Do not change this regex without changing do_shortcode_tag() and strip_shortcode_tag() |
|
237 |
// Also, see shortcode_unautop() and shortcode.js. |
|
238 |
return |
|
239 |
'\\[' // Opening bracket |
|
240 |
. '(\\[?)' // 1: Optional second opening bracket for escaping shortcodes: [[tag]] |
|
241 |
. "($tagregexp)" // 2: Shortcode name |
|
242 |
. '(?![\\w-])' // Not followed by word character or hyphen |
|
243 |
. '(' // 3: Unroll the loop: Inside the opening shortcode tag |
|
244 |
. '[^\\]\\/]*' // Not a closing bracket or forward slash |
|
245 |
. '(?:' |
|
246 |
. '\\/(?!\\])' // A forward slash not followed by a closing bracket |
|
247 |
. '[^\\]\\/]*' // Not a closing bracket or forward slash |
|
248 |
. ')*?' |
|
249 |
. ')' |
|
250 |
. '(?:' |
|
251 |
. '(\\/)' // 4: Self closing tag ... |
|
252 |
. '\\]' // ... and closing bracket |
|
253 |
. '|' |
|
254 |
. '\\]' // Closing bracket |
|
255 |
. '(?:' |
|
256 |
. '(' // 5: Unroll the loop: Optionally, anything between the opening and closing shortcode tags |
|
257 |
. '[^\\[]*+' // Not an opening bracket |
|
258 |
. '(?:' |
|
259 |
. '\\[(?!\\/\\2\\])' // An opening bracket not followed by the closing shortcode tag |
|
260 |
. '[^\\[]*+' // Not an opening bracket |
|
261 |
. ')*+' |
|
262 |
. ')' |
|
263 |
. '\\[\\/\\2\\]' // Closing shortcode tag |
|
264 |
. ')?' |
|
265 |
. ')' |
|
266 |
. '(\\]?)'; // 6: Optional second closing brocket for escaping shortcodes: [[tag]] |
|
267 |
} |
|
268 |
||
269 |
/** |
|
270 |
* Regular Expression callable for do_shortcode() for calling shortcode hook. |
|
271 |
* @see get_shortcode_regex for details of the match array contents. |
|
272 |
* |
|
5 | 273 |
* @since 2.5.0 |
0 | 274 |
* @access private |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
275 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
276 |
* @global array $shortcode_tags |
0 | 277 |
* |
278 |
* @param array $m Regular expression match array |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
279 |
* @return string|false False on failure. |
0 | 280 |
*/ |
281 |
function do_shortcode_tag( $m ) { |
|
282 |
global $shortcode_tags; |
|
283 |
||
284 |
// allow [[foo]] syntax for escaping a tag |
|
285 |
if ( $m[1] == '[' && $m[6] == ']' ) { |
|
286 |
return substr($m[0], 1, -1); |
|
287 |
} |
|
288 |
||
289 |
$tag = $m[2]; |
|
290 |
$attr = shortcode_parse_atts( $m[3] ); |
|
291 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
292 |
if ( ! is_callable( $shortcode_tags[ $tag ] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
293 |
/* translators: %s: shortcode tag */ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
294 |
$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
|
295 |
_doing_it_wrong( __FUNCTION__, $message, '4.3.0' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
296 |
return $m[0]; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
297 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
298 |
|
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 |
* Filters whether to call a shortcode callback. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
301 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
302 |
* Passing a truthy value to the filter will effectively short-circuit the |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
303 |
* shortcode generation process, returning that value instead. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
304 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
305 |
* @since 4.7.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
306 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
307 |
* @param bool|string $return Short-circuit return value. Either false or the value to replace the shortcode with. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
308 |
* @param string $tag Shortcode name. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
309 |
* @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
|
310 |
* @param array $m Regular expression match array. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
311 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
312 |
$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
|
313 |
if ( false !== $return ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
314 |
return $return; |
0 | 315 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
316 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
317 |
$content = isset( $m[5] ) ? $m[5] : null; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
318 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
319 |
$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
|
320 |
|
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 |
* Filters the output created by a shortcode callback. |
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 |
* @since 4.7.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
325 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
326 |
* @param string $output Shortcode output. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
327 |
* @param string $tag Shortcode name. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
328 |
* @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
|
329 |
* @param array $m Regular expression match array. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
330 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
331 |
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
|
332 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
333 |
|
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 |
* 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
|
336 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
337 |
* 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
|
338 |
* 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
|
339 |
* 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
|
340 |
* 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
|
341 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
342 |
* @since 4.2.3 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
343 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
344 |
* @param string $content Content to search for shortcodes |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
345 |
* @param bool $ignore_html When true, all square braces inside elements will be encoded. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
346 |
* @param array $tagnames List of shortcodes to find. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
347 |
* @return string Content with shortcodes filtered out. |
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 |
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
|
350 |
// Normalize entities in unfiltered HTML before adding placeholders. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
351 |
$trans = array( '[' => '[', ']' => ']' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
352 |
$content = strtr( $content, $trans ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
353 |
$trans = 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 |
$pattern = get_shortcode_regex( $tagnames ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
356 |
$textarr = wp_html_split( $content ); |
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 |
foreach ( $textarr as &$element ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
359 |
if ( '' == $element || '<' !== $element[0] ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
360 |
continue; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
361 |
} |
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 |
$noopen = false === strpos( $element, '[' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
364 |
$noclose = false === strpos( $element, ']' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
365 |
if ( $noopen || $noclose ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
366 |
// This element does not contain shortcodes. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
367 |
if ( $noopen xor $noclose ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
368 |
// Need to encode stray [ or ] chars. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
369 |
$element = strtr( $element, $trans ); |
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 |
continue; |
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 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
374 |
if ( $ignore_html || '<!--' === substr( $element, 0, 4 ) || '<![CDATA[' === substr( $element, 0, 9 ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
375 |
// Encode all [ and ] chars. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
376 |
$element = strtr( $element, $trans ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
377 |
continue; |
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 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
380 |
$attributes = wp_kses_attr_parse( $element ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
381 |
if ( false === $attributes ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
382 |
// Some plugins are doing things like [name] <[email]>. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
383 |
if ( 1 === preg_match( '%^<\s*\[\[?[^\[\]]+\]%', $element ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
384 |
$element = preg_replace_callback( "/$pattern/", 'do_shortcode_tag', $element ); |
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 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
387 |
// Looks like we found some crazy unfiltered HTML. Skipping it for sanity. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
388 |
$element = strtr( $element, $trans ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
389 |
continue; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
390 |
} |
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 |
// Get element name |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
393 |
$front = array_shift( $attributes ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
394 |
$back = array_pop( $attributes ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
395 |
$matches = array(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
396 |
preg_match('%[a-zA-Z0-9]+%', $front, $matches); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
397 |
$elname = $matches[0]; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
398 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
399 |
// Look for shortcodes in each attribute separately. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
400 |
foreach ( $attributes as &$attr ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
401 |
$open = strpos( $attr, '[' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
402 |
$close = strpos( $attr, ']' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
403 |
if ( false === $open || false === $close ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
404 |
continue; // Go to next attribute. Square braces will be escaped at end of loop. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
405 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
406 |
$double = strpos( $attr, '"' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
407 |
$single = strpos( $attr, "'" ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
408 |
if ( ( false === $single || $open < $single ) && ( false === $double || $open < $double ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
409 |
// $attr like '[shortcode]' or 'name = [shortcode]' implies unfiltered_html. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
410 |
// In this specific situation we assume KSES did not run because the input |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
411 |
// was written by an administrator, so we should avoid changing the output |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
412 |
// and we do not need to run KSES here. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
413 |
$attr = preg_replace_callback( "/$pattern/", 'do_shortcode_tag', $attr ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
414 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
415 |
// $attr like 'name = "[shortcode]"' or "name = '[shortcode]'" |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
416 |
// We do not know if $content was unfiltered. Assume KSES ran before shortcodes. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
417 |
$count = 0; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
418 |
$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
|
419 |
if ( $count > 0 ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
420 |
// Sanitize the shortcode output using KSES. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
421 |
$new_attr = wp_kses_one_attr( $new_attr, $elname ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
422 |
if ( '' !== trim( $new_attr ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
423 |
// The shortcode is safe to use now. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
424 |
$attr = $new_attr; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
425 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
426 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
427 |
} |
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 |
$element = $front . implode( '', $attributes ) . $back; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
430 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
431 |
// Now encode any remaining [ or ] chars. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
432 |
$element = strtr( $element, $trans ); |
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 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
435 |
$content = implode( '', $textarr ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
436 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
437 |
return $content; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
438 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
439 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
440 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
441 |
* Remove placeholders added by do_shortcodes_in_html_tags(). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
442 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
443 |
* @since 4.2.3 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
444 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
445 |
* @param string $content Content to search for placeholders. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
446 |
* @return string Content with placeholders removed. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
447 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
448 |
function unescape_invalid_shortcodes( $content ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
449 |
// Clean up entire string, avoids re-parsing HTML. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
450 |
$trans = array( '[' => '[', ']' => ']' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
451 |
$content = strtr( $content, $trans ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
452 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
453 |
return $content; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
454 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
455 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
456 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
457 |
* Retrieve the shortcode attributes regex. |
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 |
* @since 4.4.0 |
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 |
* @return string The shortcode attribute regular expression |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
462 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
463 |
function get_shortcode_atts_regex() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
464 |
return '/([\w-]+)\s*=\s*"([^"]*)"(?:\s|$)|([\w-]+)\s*=\s*\'([^\']*)\'(?:\s|$)|([\w-]+)\s*=\s*([^\s\'"]+)(?:\s|$)|"([^"]*)"(?:\s|$)|\'([^\']*)\'(?:\s|$)|(\S+)(?:\s|$)/'; |
0 | 465 |
} |
466 |
||
467 |
/** |
|
468 |
* Retrieve all attributes from the shortcodes tag. |
|
469 |
* |
|
470 |
* The attributes list has the attribute name as the key and the value of the |
|
471 |
* attribute as the value in the key/value pair. This allows for easier |
|
472 |
* retrieval of the attributes, since all attributes have to be known. |
|
473 |
* |
|
5 | 474 |
* @since 2.5.0 |
0 | 475 |
* |
476 |
* @param string $text |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
477 |
* @return array|string List of attribute values. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
478 |
* Returns empty array if trim( $text ) == '""'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
479 |
* Returns empty string if trim( $text ) == ''. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
480 |
* All other matches are checked for not empty(). |
0 | 481 |
*/ |
482 |
function shortcode_parse_atts($text) { |
|
483 |
$atts = array(); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
484 |
$pattern = get_shortcode_atts_regex(); |
0 | 485 |
$text = preg_replace("/[\x{00a0}\x{200b}]+/u", " ", $text); |
486 |
if ( preg_match_all($pattern, $text, $match, PREG_SET_ORDER) ) { |
|
487 |
foreach ($match as $m) { |
|
488 |
if (!empty($m[1])) |
|
489 |
$atts[strtolower($m[1])] = stripcslashes($m[2]); |
|
490 |
elseif (!empty($m[3])) |
|
491 |
$atts[strtolower($m[3])] = stripcslashes($m[4]); |
|
492 |
elseif (!empty($m[5])) |
|
493 |
$atts[strtolower($m[5])] = stripcslashes($m[6]); |
|
5 | 494 |
elseif (isset($m[7]) && strlen($m[7])) |
0 | 495 |
$atts[] = stripcslashes($m[7]); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
496 |
elseif (isset($m[8]) && strlen($m[8])) |
0 | 497 |
$atts[] = stripcslashes($m[8]); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
498 |
elseif (isset($m[9])) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
499 |
$atts[] = stripcslashes($m[9]); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
500 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
501 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
502 |
// Reject any unclosed HTML elements |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
503 |
foreach( $atts as &$value ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
504 |
if ( false !== strpos( $value, '<' ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
505 |
if ( 1 !== preg_match( '/^[^<]*+(?:<[^>]*+>[^<]*+)*+$/', $value ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
506 |
$value = ''; |
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 |
} |
0 | 509 |
} |
510 |
} else { |
|
511 |
$atts = ltrim($text); |
|
512 |
} |
|
513 |
return $atts; |
|
514 |
} |
|
515 |
||
516 |
/** |
|
517 |
* Combine user attributes with known attributes and fill in defaults when needed. |
|
518 |
* |
|
519 |
* The pairs should be considered to be all of the attributes which are |
|
520 |
* supported by the caller and given as a list. The returned attributes will |
|
521 |
* only contain the attributes in the $pairs list. |
|
522 |
* |
|
523 |
* If the $atts list has unsupported attributes, then they will be ignored and |
|
524 |
* removed from the final returned list. |
|
525 |
* |
|
5 | 526 |
* @since 2.5.0 |
0 | 527 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
528 |
* @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
|
529 |
* @param array $atts User defined attributes in shortcode tag. |
0 | 530 |
* @param string $shortcode Optional. The name of the shortcode, provided for context to enable filtering |
531 |
* @return array Combined and filtered attribute list. |
|
532 |
*/ |
|
533 |
function shortcode_atts( $pairs, $atts, $shortcode = '' ) { |
|
534 |
$atts = (array)$atts; |
|
535 |
$out = array(); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
536 |
foreach ($pairs as $name => $default) { |
0 | 537 |
if ( array_key_exists($name, $atts) ) |
538 |
$out[$name] = $atts[$name]; |
|
539 |
else |
|
540 |
$out[$name] = $default; |
|
541 |
} |
|
542 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
543 |
* Filters a shortcode's default attributes. |
0 | 544 |
* |
545 |
* If the third parameter of the shortcode_atts() function is present then this filter is available. |
|
546 |
* The third parameter, $shortcode, is the name of the shortcode. |
|
547 |
* |
|
548 |
* @since 3.6.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
549 |
* @since 4.4.0 Added the `$shortcode` parameter. |
0 | 550 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
551 |
* @param array $out The output array of shortcode attributes. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
552 |
* @param array $pairs The supported attributes and their defaults. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
553 |
* @param array $atts The user defined shortcode attributes. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
554 |
* @param string $shortcode The shortcode name. |
0 | 555 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
556 |
if ( $shortcode ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
557 |
$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
|
558 |
} |
0 | 559 |
|
560 |
return $out; |
|
561 |
} |
|
562 |
||
563 |
/** |
|
564 |
* Remove all shortcode tags from the given content. |
|
565 |
* |
|
5 | 566 |
* @since 2.5.0 |
567 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
568 |
* @global array $shortcode_tags |
0 | 569 |
* |
570 |
* @param string $content Content to remove shortcode tags. |
|
571 |
* @return string Content without shortcode tags. |
|
572 |
*/ |
|
573 |
function strip_shortcodes( $content ) { |
|
574 |
global $shortcode_tags; |
|
575 |
||
5 | 576 |
if ( false === strpos( $content, '[' ) ) { |
577 |
return $content; |
|
578 |
} |
|
579 |
||
0 | 580 |
if (empty($shortcode_tags) || !is_array($shortcode_tags)) |
581 |
return $content; |
|
582 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
583 |
// Find all registered tag names in $content. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
584 |
preg_match_all( '@\[([^<>&/\[\]\x00-\x20=]++)@', $content, $matches ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
585 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
586 |
$tags_to_remove = array_keys( $shortcode_tags ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
587 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
588 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
589 |
* 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
|
590 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
591 |
* @since 4.7.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
592 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
593 |
* @param array $tag_array Array of shortcode tags to remove. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
594 |
* @param string $content Content shortcodes are being removed from. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
595 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
596 |
$tags_to_remove = apply_filters( 'strip_shortcodes_tagnames', $tags_to_remove, $content ); |
0 | 597 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
598 |
$tagnames = array_intersect( $tags_to_remove, $matches[1] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
599 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
600 |
if ( empty( $tagnames ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
601 |
return $content; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
602 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
603 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
604 |
$content = do_shortcodes_in_html_tags( $content, true, $tagnames ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
605 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
606 |
$pattern = get_shortcode_regex( $tagnames ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
607 |
$content = preg_replace_callback( "/$pattern/", 'strip_shortcode_tag', $content ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
608 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
609 |
// Always restore square braces so we don't break things like <!--[if IE ]> |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
610 |
$content = unescape_invalid_shortcodes( $content ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
611 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
612 |
return $content; |
0 | 613 |
} |
614 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
615 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
616 |
* 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
|
617 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
618 |
* @since 3.3.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
619 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
620 |
* @param array $m RegEx matches against post content. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
621 |
* @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
|
622 |
*/ |
0 | 623 |
function strip_shortcode_tag( $m ) { |
624 |
// allow [[foo]] syntax for escaping a tag |
|
625 |
if ( $m[1] == '[' && $m[6] == ']' ) { |
|
626 |
return substr($m[0], 1, -1); |
|
627 |
} |
|
628 |
||
629 |
return $m[1] . $m[6]; |
|
630 |
} |