1 | <?php |
---|
2 | /** |
---|
3 | * Main Wordpress Formatting API. |
---|
4 | * |
---|
5 | * Handles many functions for formatting output. |
---|
6 | * |
---|
7 | * @package WordPress |
---|
8 | **/ |
---|
9 | |
---|
10 | /** |
---|
11 | * Replaces common plain text characters into formatted entities |
---|
12 | * |
---|
13 | * As an example, |
---|
14 | * <code> |
---|
15 | * 'cause today's effort makes it worth tomorrow's "holiday"... |
---|
16 | * </code> |
---|
17 | * Becomes: |
---|
18 | * <code> |
---|
19 | * ’cause today’s effort makes it worth tomorrow’s “holiday”… |
---|
20 | * </code> |
---|
21 | * Code within certain html blocks are skipped. |
---|
22 | * |
---|
23 | * @since 0.71 |
---|
24 | * @uses $wp_cockneyreplace Array of formatted entities for certain common phrases |
---|
25 | * |
---|
26 | * @param string $text The text to be formatted |
---|
27 | * @return string The string replaced with html entities |
---|
28 | */ |
---|
29 | function wptexturize($text) { |
---|
30 | global $wp_cockneyreplace; |
---|
31 | $output = ''; |
---|
32 | $curl = ''; |
---|
33 | $textarr = preg_split('/(<.*>|\[.*\])/Us', $text, -1, PREG_SPLIT_DELIM_CAPTURE); |
---|
34 | $stop = count($textarr); |
---|
35 | |
---|
36 | /* translators: opening curly quote */ |
---|
37 | $opening_quote = _x('“', 'opening curly quote'); |
---|
38 | /* translators: closing curly quote */ |
---|
39 | $closing_quote = _x('”', 'closing curly quote'); |
---|
40 | |
---|
41 | $no_texturize_tags = apply_filters('no_texturize_tags', array('pre', 'code', 'kbd', 'style', 'script', 'tt')); |
---|
42 | $no_texturize_shortcodes = apply_filters('no_texturize_shortcodes', array('code')); |
---|
43 | $no_texturize_tags_stack = array(); |
---|
44 | $no_texturize_shortcodes_stack = array(); |
---|
45 | |
---|
46 | // if a plugin has provided an autocorrect array, use it |
---|
47 | if ( isset($wp_cockneyreplace) ) { |
---|
48 | $cockney = array_keys($wp_cockneyreplace); |
---|
49 | $cockneyreplace = array_values($wp_cockneyreplace); |
---|
50 | } else { |
---|
51 | $cockney = array("'tain't","'twere","'twas","'tis","'twill","'til","'bout","'nuff","'round","'cause"); |
---|
52 | $cockneyreplace = array("’tain’t","’twere","’twas","’tis","’twill","’til","’bout","’nuff","’round","’cause"); |
---|
53 | } |
---|
54 | |
---|
55 | $static_characters = array_merge(array('---', ' -- ', '--', ' - ', 'xn–', '...', '``', '\'s', '\'\'', ' (tm)'), $cockney); |
---|
56 | $static_replacements = array_merge(array('—', ' — ', '–', ' – ', 'xn--', '…', $opening_quote, '’s', $closing_quote, ' ™'), $cockneyreplace); |
---|
57 | |
---|
58 | $dynamic_characters = array('/\'(\d\d(?:’|\')?s)/', '/(\s|\A|")\'/', '/(\d+)"/', '/(\d+)\'/', '/(\S)\'([^\'\s])/', '/(\s|\A)"(?!\s)/', '/"(\s|\S|\Z)/', '/\'([\s.]|\Z)/', '/(\d+)x(\d+)/'); |
---|
59 | $dynamic_replacements = array('’$1','$1‘', '$1″', '$1′', '$1’$2', '$1' . $opening_quote . '$2', $closing_quote . '$1', '’$1', '$1×$2'); |
---|
60 | |
---|
61 | for ( $i = 0; $i < $stop; $i++ ) { |
---|
62 | $curl = $textarr[$i]; |
---|
63 | |
---|
64 | if ( !empty($curl) && '<' != $curl{0} && '[' != $curl{0} |
---|
65 | && empty($no_texturize_shortcodes_stack) && empty($no_texturize_tags_stack)) { // If it's not a tag |
---|
66 | // static strings |
---|
67 | $curl = str_replace($static_characters, $static_replacements, $curl); |
---|
68 | // regular expressions |
---|
69 | $curl = preg_replace($dynamic_characters, $dynamic_replacements, $curl); |
---|
70 | } else { |
---|
71 | wptexturize_pushpop_element($curl, $no_texturize_tags_stack, $no_texturize_tags, '<', '>'); |
---|
72 | wptexturize_pushpop_element($curl, $no_texturize_shortcodes_stack, $no_texturize_shortcodes, '[', ']'); |
---|
73 | } |
---|
74 | |
---|
75 | $curl = preg_replace('/&([^#])(?![a-zA-Z1-4]{1,8};)/', '&$1', $curl); |
---|
76 | $output .= $curl; |
---|
77 | } |
---|
78 | |
---|
79 | return $output; |
---|
80 | } |
---|
81 | |
---|
82 | function wptexturize_pushpop_element($text, &$stack, $disabled_elements, $opening = '<', $closing = '>') { |
---|
83 | $o = preg_quote($opening); |
---|
84 | $c = preg_quote($closing); |
---|
85 | foreach($disabled_elements as $element) { |
---|
86 | if (preg_match('/^'.$o.$element.'\b/', $text)) array_push($stack, $element); |
---|
87 | if (preg_match('/^'.$o.'\/'.$element.$c.'/', $text)) { |
---|
88 | $last = array_pop($stack); |
---|
89 | // disable texturize until we find a closing tag of our type (e.g. <pre>) |
---|
90 | // even if there was invalid nesting before that |
---|
91 | // Example: in the case <pre>sadsadasd</code>"baba"</pre> "baba" won't be texturized |
---|
92 | if ($last != $element) array_push($stack, $last); |
---|
93 | } |
---|
94 | } |
---|
95 | } |
---|
96 | |
---|
97 | /** |
---|
98 | * Accepts matches array from preg_replace_callback in wpautop() or a string. |
---|
99 | * |
---|
100 | * Ensures that the contents of a <<pre>>...<</pre>> HTML block are not |
---|
101 | * converted into paragraphs or line-breaks. |
---|
102 | * |
---|
103 | * @since 1.2.0 |
---|
104 | * |
---|
105 | * @param array|string $matches The array or string |
---|
106 | * @return string The pre block without paragraph/line-break conversion. |
---|
107 | */ |
---|
108 | function clean_pre($matches) { |
---|
109 | if ( is_array($matches) ) |
---|
110 | $text = $matches[1] . $matches[2] . "</pre>"; |
---|
111 | else |
---|
112 | $text = $matches; |
---|
113 | |
---|
114 | $text = str_replace('<br />', '', $text); |
---|
115 | $text = str_replace('<p>', "\n", $text); |
---|
116 | $text = str_replace('</p>', '', $text); |
---|
117 | |
---|
118 | return $text; |
---|
119 | } |
---|
120 | |
---|
121 | /** |
---|
122 | * Replaces double line-breaks with paragraph elements. |
---|
123 | * |
---|
124 | * A group of regex replaces used to identify text formatted with newlines and |
---|
125 | * replace double line-breaks with HTML paragraph tags. The remaining |
---|
126 | * line-breaks after conversion become <<br />> tags, unless $br is set to '0' |
---|
127 | * or 'false'. |
---|
128 | * |
---|
129 | * @since 0.71 |
---|
130 | * |
---|
131 | * @param string $pee The text which has to be formatted. |
---|
132 | * @param int|bool $br Optional. If set, this will convert all remaining line-breaks after paragraphing. Default true. |
---|
133 | * @return string Text which has been converted into correct paragraph tags. |
---|
134 | */ |
---|
135 | function wpautop($pee, $br = 1) { |
---|
136 | if ( trim($pee) === '' ) |
---|
137 | return ''; |
---|
138 | $pee = $pee . "\n"; // just to make things a little easier, pad the end |
---|
139 | $pee = preg_replace('|<br />\s*<br />|', "\n\n", $pee); |
---|
140 | // Space things out a little |
---|
141 | $allblocks = '(?:table|thead|tfoot|caption|col|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|select|form|map|area|blockquote|address|math|style|input|p|h[1-6]|hr)'; |
---|
142 | $pee = preg_replace('!(<' . $allblocks . '[^>]*>)!', "\n$1", $pee); |
---|
143 | $pee = preg_replace('!(</' . $allblocks . '>)!', "$1\n\n", $pee); |
---|
144 | $pee = str_replace(array("\r\n", "\r"), "\n", $pee); // cross-platform newlines |
---|
145 | if ( strpos($pee, '<object') !== false ) { |
---|
146 | $pee = preg_replace('|\s*<param([^>]*)>\s*|', "<param$1>", $pee); // no pee inside object/embed |
---|
147 | $pee = preg_replace('|\s*</embed>\s*|', '</embed>', $pee); |
---|
148 | } |
---|
149 | $pee = preg_replace("/\n\n+/", "\n\n", $pee); // take care of duplicates |
---|
150 | // make paragraphs, including one at the end |
---|
151 | $pees = preg_split('/\n\s*\n/', $pee, -1, PREG_SPLIT_NO_EMPTY); |
---|
152 | $pee = ''; |
---|
153 | foreach ( $pees as $tinkle ) |
---|
154 | $pee .= '<p>' . trim($tinkle, "\n") . "</p>\n"; |
---|
155 | $pee = preg_replace('|<p>\s*</p>|', '', $pee); // under certain strange conditions it could create a P of entirely whitespace |
---|
156 | $pee = preg_replace('!<p>([^<]+)</(div|address|form)>!', "<p>$1</p></$2>", $pee); |
---|
157 | $pee = preg_replace('!<p>\s*(</?' . $allblocks . '[^>]*>)\s*</p>!', "$1", $pee); // don't pee all over a tag |
---|
158 | $pee = preg_replace("|<p>(<li.+?)</p>|", "$1", $pee); // problem with nested lists |
---|
159 | $pee = preg_replace('|<p><blockquote([^>]*)>|i', "<blockquote$1><p>", $pee); |
---|
160 | $pee = str_replace('</blockquote></p>', '</p></blockquote>', $pee); |
---|
161 | $pee = preg_replace('!<p>\s*(</?' . $allblocks . '[^>]*>)!', "$1", $pee); |
---|
162 | $pee = preg_replace('!(</?' . $allblocks . '[^>]*>)\s*</p>!', "$1", $pee); |
---|
163 | if ($br) { |
---|
164 | $pee = preg_replace_callback('/<(script|style).*?<\/\\1>/s', create_function('$matches', 'return str_replace("\n", "<WPPreserveNewline />", $matches[0]);'), $pee); |
---|
165 | $pee = preg_replace('|(?<!<br />)\s*\n|', "<br />\n", $pee); // optionally make line breaks |
---|
166 | $pee = str_replace('<WPPreserveNewline />', "\n", $pee); |
---|
167 | } |
---|
168 | $pee = preg_replace('!(</?' . $allblocks . '[^>]*>)\s*<br />!', "$1", $pee); |
---|
169 | $pee = preg_replace('!<br />(\s*</?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)[^>]*>)!', '$1', $pee); |
---|
170 | if (strpos($pee, '<pre') !== false) |
---|
171 | $pee = preg_replace_callback('!(<pre[^>]*>)(.*?)</pre>!is', 'clean_pre', $pee ); |
---|
172 | $pee = preg_replace( "|\n</p>$|", '</p>', $pee ); |
---|
173 | $pee = preg_replace('/<p>\s*?(' . get_shortcode_regex() . ')\s*<\/p>/s', '$1', $pee); // don't auto-p wrap shortcodes that stand alone |
---|
174 | |
---|
175 | return $pee; |
---|
176 | } |
---|
177 | |
---|
178 | /** |
---|
179 | * Checks to see if a string is utf8 encoded. |
---|
180 | * |
---|
181 | * NOTE: This function checks for 5-Byte sequences, UTF8 |
---|
182 | * has Bytes Sequences with a maximum length of 4. |
---|
183 | * |
---|
184 | * @author bmorel at ssi dot fr (modified) |
---|
185 | * @since 1.2.1 |
---|
186 | * |
---|
187 | * @param string $str The string to be checked |
---|
188 | * @return bool True if $str fits a UTF-8 model, false otherwise. |
---|
189 | */ |
---|
190 | function seems_utf8($str) { |
---|
191 | $length = strlen($str); |
---|
192 | for ($i=0; $i < $length; $i++) { |
---|
193 | $c = ord($str[$i]); |
---|
194 | if ($c < 0x80) $n = 0; # 0bbbbbbb |
---|
195 | elseif (($c & 0xE0) == 0xC0) $n=1; # 110bbbbb |
---|
196 | elseif (($c & 0xF0) == 0xE0) $n=2; # 1110bbbb |
---|
197 | elseif (($c & 0xF8) == 0xF0) $n=3; # 11110bbb |
---|
198 | elseif (($c & 0xFC) == 0xF8) $n=4; # 111110bb |
---|
199 | elseif (($c & 0xFE) == 0xFC) $n=5; # 1111110b |
---|
200 | else return false; # Does not match any model |
---|
201 | for ($j=0; $j<$n; $j++) { # n bytes matching 10bbbbbb follow ? |
---|
202 | if ((++$i == $length) || ((ord($str[$i]) & 0xC0) != 0x80)) |
---|
203 | return false; |
---|
204 | } |
---|
205 | } |
---|
206 | return true; |
---|
207 | } |
---|
208 | |
---|
209 | /** |
---|
210 | * Converts a number of special characters into their HTML entities. |
---|
211 | * |
---|
212 | * Specifically deals with: &, <, >, ", and '. |
---|
213 | * |
---|
214 | * $quote_style can be set to ENT_COMPAT to encode " to |
---|
215 | * ", or ENT_QUOTES to do both. Default is ENT_NOQUOTES where no quotes are encoded. |
---|
216 | * |
---|
217 | * @since 1.2.2 |
---|
218 | * |
---|
219 | * @param string $string The text which is to be encoded. |
---|
220 | * @param mixed $quote_style Optional. Converts double quotes if set to ENT_COMPAT, both single and double if set to ENT_QUOTES or none if set to ENT_NOQUOTES. Also compatible with old values; converting single quotes if set to 'single', double if set to 'double' or both if otherwise set. Default is ENT_NOQUOTES. |
---|
221 | * @param string $charset Optional. The character encoding of the string. Default is false. |
---|
222 | * @param boolean $double_encode Optional. Whether or not to encode existing html entities. Default is false. |
---|
223 | * @return string The encoded text with HTML entities. |
---|
224 | */ |
---|
225 | function _wp_specialchars( $string, $quote_style = ENT_NOQUOTES, $charset = false, $double_encode = false ) { |
---|
226 | $string = (string) $string; |
---|
227 | |
---|
228 | if ( 0 === strlen( $string ) ) { |
---|
229 | return ''; |
---|
230 | } |
---|
231 | |
---|
232 | // Don't bother if there are no specialchars - saves some processing |
---|
233 | if ( !preg_match( '/[&<>"\']/', $string ) ) { |
---|
234 | return $string; |
---|
235 | } |
---|
236 | |
---|
237 | // Account for the previous behaviour of the function when the $quote_style is not an accepted value |
---|
238 | if ( empty( $quote_style ) ) { |
---|
239 | $quote_style = ENT_NOQUOTES; |
---|
240 | } elseif ( !in_array( $quote_style, array( 0, 2, 3, 'single', 'double' ), true ) ) { |
---|
241 | $quote_style = ENT_QUOTES; |
---|
242 | } |
---|
243 | |
---|
244 | // Store the site charset as a static to avoid multiple calls to wp_load_alloptions() |
---|
245 | if ( !$charset ) { |
---|
246 | static $_charset; |
---|
247 | if ( !isset( $_charset ) ) { |
---|
248 | $alloptions = wp_load_alloptions(); |
---|
249 | $_charset = isset( $alloptions['blog_charset'] ) ? $alloptions['blog_charset'] : ''; |
---|
250 | } |
---|
251 | $charset = $_charset; |
---|
252 | } |
---|
253 | if ( in_array( $charset, array( 'utf8', 'utf-8', 'UTF8' ) ) ) { |
---|
254 | $charset = 'UTF-8'; |
---|
255 | } |
---|
256 | |
---|
257 | $_quote_style = $quote_style; |
---|
258 | |
---|
259 | if ( $quote_style === 'double' ) { |
---|
260 | $quote_style = ENT_COMPAT; |
---|
261 | $_quote_style = ENT_COMPAT; |
---|
262 | } elseif ( $quote_style === 'single' ) { |
---|
263 | $quote_style = ENT_NOQUOTES; |
---|
264 | } |
---|
265 | |
---|
266 | // Handle double encoding ourselves |
---|
267 | if ( !$double_encode ) { |
---|
268 | $string = wp_specialchars_decode( $string, $_quote_style ); |
---|
269 | $string = preg_replace( '/&(#?x?[0-9a-z]+);/i', '|wp_entity|$1|/wp_entity|', $string ); |
---|
270 | } |
---|
271 | |
---|
272 | $string = @htmlspecialchars( $string, $quote_style, $charset ); |
---|
273 | |
---|
274 | // Handle double encoding ourselves |
---|
275 | if ( !$double_encode ) { |
---|
276 | $string = str_replace( array( '|wp_entity|', '|/wp_entity|' ), array( '&', ';' ), $string ); |
---|
277 | } |
---|
278 | |
---|
279 | // Backwards compatibility |
---|
280 | if ( 'single' === $_quote_style ) { |
---|
281 | $string = str_replace( "'", ''', $string ); |
---|
282 | } |
---|
283 | |
---|
284 | return $string; |
---|
285 | } |
---|
286 | |
---|
287 | /** |
---|
288 | * Converts a number of HTML entities into their special characters. |
---|
289 | * |
---|
290 | * Specifically deals with: &, <, >, ", and '. |
---|
291 | * |
---|
292 | * $quote_style can be set to ENT_COMPAT to decode " entities, |
---|
293 | * or ENT_QUOTES to do both " and '. Default is ENT_NOQUOTES where no quotes are decoded. |
---|
294 | * |
---|
295 | * @since 2.8 |
---|
296 | * |
---|
297 | * @param string $string The text which is to be decoded. |
---|
298 | * @param mixed $quote_style Optional. Converts double quotes if set to ENT_COMPAT, both single and double if set to ENT_QUOTES or none if set to ENT_NOQUOTES. Also compatible with old _wp_specialchars() values; converting single quotes if set to 'single', double if set to 'double' or both if otherwise set. Default is ENT_NOQUOTES. |
---|
299 | * @return string The decoded text without HTML entities. |
---|
300 | */ |
---|
301 | function wp_specialchars_decode( $string, $quote_style = ENT_NOQUOTES ) { |
---|
302 | $string = (string) $string; |
---|
303 | |
---|
304 | if ( 0 === strlen( $string ) ) { |
---|
305 | return ''; |
---|
306 | } |
---|
307 | |
---|
308 | // Don't bother if there are no entities - saves a lot of processing |
---|
309 | if ( strpos( $string, '&' ) === false ) { |
---|
310 | return $string; |
---|
311 | } |
---|
312 | |
---|
313 | // Match the previous behaviour of _wp_specialchars() when the $quote_style is not an accepted value |
---|
314 | if ( empty( $quote_style ) ) { |
---|
315 | $quote_style = ENT_NOQUOTES; |
---|
316 | } elseif ( !in_array( $quote_style, array( 0, 2, 3, 'single', 'double' ), true ) ) { |
---|
317 | $quote_style = ENT_QUOTES; |
---|
318 | } |
---|
319 | |
---|
320 | // More complete than get_html_translation_table( HTML_SPECIALCHARS ) |
---|
321 | $single = array( ''' => '\'', ''' => '\'' ); |
---|
322 | $single_preg = array( '/�*39;/' => ''', '/�*27;/i' => ''' ); |
---|
323 | $double = array( '"' => '"', '"' => '"', '"' => '"' ); |
---|
324 | $double_preg = array( '/�*34;/' => '"', '/�*22;/i' => '"' ); |
---|
325 | $others = array( '<' => '<', '<' => '<', '>' => '>', '>' => '>', '&' => '&', '&' => '&', '&' => '&' ); |
---|
326 | $others_preg = array( '/�*60;/' => '<', '/�*62;/' => '>', '/�*38;/' => '&', '/�*26;/i' => '&' ); |
---|
327 | |
---|
328 | if ( $quote_style === ENT_QUOTES ) { |
---|
329 | $translation = array_merge( $single, $double, $others ); |
---|
330 | $translation_preg = array_merge( $single_preg, $double_preg, $others_preg ); |
---|
331 | } elseif ( $quote_style === ENT_COMPAT || $quote_style === 'double' ) { |
---|
332 | $translation = array_merge( $double, $others ); |
---|
333 | $translation_preg = array_merge( $double_preg, $others_preg ); |
---|
334 | } elseif ( $quote_style === 'single' ) { |
---|
335 | $translation = array_merge( $single, $others ); |
---|
336 | $translation_preg = array_merge( $single_preg, $others_preg ); |
---|
337 | } elseif ( $quote_style === ENT_NOQUOTES ) { |
---|
338 | $translation = $others; |
---|
339 | $translation_preg = $others_preg; |
---|
340 | } |
---|
341 | |
---|
342 | // Remove zero padding on numeric entities |
---|
343 | $string = preg_replace( array_keys( $translation_preg ), array_values( $translation_preg ), $string ); |
---|
344 | |
---|
345 | // Replace characters according to translation table |
---|
346 | return strtr( $string, $translation ); |
---|
347 | } |
---|
348 | |
---|
349 | /** |
---|
350 | * Checks for invalid UTF8 in a string. |
---|
351 | * |
---|
352 | * @since 2.8 |
---|
353 | * |
---|
354 | * @param string $string The text which is to be checked. |
---|
355 | * @param boolean $strip Optional. Whether to attempt to strip out invalid UTF8. Default is false. |
---|
356 | * @return string The checked text. |
---|
357 | */ |
---|
358 | function wp_check_invalid_utf8( $string, $strip = false ) { |
---|
359 | $string = (string) $string; |
---|
360 | |
---|
361 | if ( 0 === strlen( $string ) ) { |
---|
362 | return ''; |
---|
363 | } |
---|
364 | |
---|
365 | // Store the site charset as a static to avoid multiple calls to get_option() |
---|
366 | static $is_utf8; |
---|
367 | if ( !isset( $is_utf8 ) ) { |
---|
368 | $is_utf8 = in_array( get_option( 'blog_charset' ), array( 'utf8', 'utf-8', 'UTF8', 'UTF-8' ) ); |
---|
369 | } |
---|
370 | if ( !$is_utf8 ) { |
---|
371 | return $string; |
---|
372 | } |
---|
373 | |
---|
374 | // Check for support for utf8 in the installed PCRE library once and store the result in a static |
---|
375 | static $utf8_pcre; |
---|
376 | if ( !isset( $utf8_pcre ) ) { |
---|
377 | $utf8_pcre = @preg_match( '/^./u', 'a' ); |
---|
378 | } |
---|
379 | // We can't demand utf8 in the PCRE installation, so just return the string in those cases |
---|
380 | if ( !$utf8_pcre ) { |
---|
381 | return $string; |
---|
382 | } |
---|
383 | |
---|
384 | // preg_match fails when it encounters invalid UTF8 in $string |
---|
385 | if ( 1 === @preg_match( '/^./us', $string ) ) { |
---|
386 | return $string; |
---|
387 | } |
---|
388 | |
---|
389 | // Attempt to strip the bad chars if requested (not recommended) |
---|
390 | if ( $strip && function_exists( 'iconv' ) ) { |
---|
391 | return iconv( 'utf-8', 'utf-8', $string ); |
---|
392 | } |
---|
393 | |
---|
394 | return ''; |
---|
395 | } |
---|
396 | |
---|
397 | /** |
---|
398 | * Encode the Unicode values to be used in the URI. |
---|
399 | * |
---|
400 | * @since 1.5.0 |
---|
401 | * |
---|
402 | * @param string $utf8_string |
---|
403 | * @param int $length Max length of the string |
---|
404 | * @return string String with Unicode encoded for URI. |
---|
405 | */ |
---|
406 | function utf8_uri_encode( $utf8_string, $length = 0 ) { |
---|
407 | $unicode = ''; |
---|
408 | $values = array(); |
---|
409 | $num_octets = 1; |
---|
410 | $unicode_length = 0; |
---|
411 | |
---|
412 | $string_length = strlen( $utf8_string ); |
---|
413 | for ($i = 0; $i < $string_length; $i++ ) { |
---|
414 | |
---|
415 | $value = ord( $utf8_string[ $i ] ); |
---|
416 | |
---|
417 | if ( $value < 128 ) { |
---|
418 | if ( $length && ( $unicode_length >= $length ) ) |
---|
419 | break; |
---|
420 | $unicode .= chr($value); |
---|
421 | $unicode_length++; |
---|
422 | } else { |
---|
423 | if ( count( $values ) == 0 ) $num_octets = ( $value < 224 ) ? 2 : 3; |
---|
424 | |
---|
425 | $values[] = $value; |
---|
426 | |
---|
427 | if ( $length && ( $unicode_length + ($num_octets * 3) ) > $length ) |
---|
428 | break; |
---|
429 | if ( count( $values ) == $num_octets ) { |
---|
430 | if ($num_octets == 3) { |
---|
431 | $unicode .= '%' . dechex($values[0]) . '%' . dechex($values[1]) . '%' . dechex($values[2]); |
---|
432 | $unicode_length += 9; |
---|
433 | } else { |
---|
434 | $unicode .= '%' . dechex($values[0]) . '%' . dechex($values[1]); |
---|
435 | $unicode_length += 6; |
---|
436 | } |
---|
437 | |
---|
438 | $values = array(); |
---|
439 | $num_octets = 1; |
---|
440 | } |
---|
441 | } |
---|
442 | } |
---|
443 | |
---|
444 | return $unicode; |
---|
445 | } |
---|
446 | |
---|
447 | /** |
---|
448 | * Converts all accent characters to ASCII characters. |
---|
449 | * |
---|
450 | * If there are no accent characters, then the string given is just returned. |
---|
451 | * |
---|
452 | * @since 1.2.1 |
---|
453 | * |
---|
454 | * @param string $string Text that might have accent characters |
---|
455 | * @return string Filtered string with replaced "nice" characters. |
---|
456 | */ |
---|
457 | function remove_accents($string) { |
---|
458 | if ( !preg_match('/[\x80-\xff]/', $string) ) |
---|
459 | return $string; |
---|
460 | |
---|
461 | if (seems_utf8($string)) { |
---|
462 | $chars = array( |
---|
463 | // Decompositions for Latin-1 Supplement |
---|
464 | chr(195).chr(128) => 'A', chr(195).chr(129) => 'A', |
---|
465 | chr(195).chr(130) => 'A', chr(195).chr(131) => 'A', |
---|
466 | chr(195).chr(132) => 'A', chr(195).chr(133) => 'A', |
---|
467 | chr(195).chr(135) => 'C', chr(195).chr(136) => 'E', |
---|
468 | chr(195).chr(137) => 'E', chr(195).chr(138) => 'E', |
---|
469 | chr(195).chr(139) => 'E', chr(195).chr(140) => 'I', |
---|
470 | chr(195).chr(141) => 'I', chr(195).chr(142) => 'I', |
---|
471 | chr(195).chr(143) => 'I', chr(195).chr(145) => 'N', |
---|
472 | chr(195).chr(146) => 'O', chr(195).chr(147) => 'O', |
---|
473 | chr(195).chr(148) => 'O', chr(195).chr(149) => 'O', |
---|
474 | chr(195).chr(150) => 'O', chr(195).chr(153) => 'U', |
---|
475 | chr(195).chr(154) => 'U', chr(195).chr(155) => 'U', |
---|
476 | chr(195).chr(156) => 'U', chr(195).chr(157) => 'Y', |
---|
477 | chr(195).chr(159) => 's', chr(195).chr(160) => 'a', |
---|
478 | chr(195).chr(161) => 'a', chr(195).chr(162) => 'a', |
---|
479 | chr(195).chr(163) => 'a', chr(195).chr(164) => 'a', |
---|
480 | chr(195).chr(165) => 'a', chr(195).chr(167) => 'c', |
---|
481 | chr(195).chr(168) => 'e', chr(195).chr(169) => 'e', |
---|
482 | chr(195).chr(170) => 'e', chr(195).chr(171) => 'e', |
---|
483 | chr(195).chr(172) => 'i', chr(195).chr(173) => 'i', |
---|
484 | chr(195).chr(174) => 'i', chr(195).chr(175) => 'i', |
---|
485 | chr(195).chr(177) => 'n', chr(195).chr(178) => 'o', |
---|
486 | chr(195).chr(179) => 'o', chr(195).chr(180) => 'o', |
---|
487 | chr(195).chr(181) => 'o', chr(195).chr(182) => 'o', |
---|
488 | chr(195).chr(182) => 'o', chr(195).chr(185) => 'u', |
---|
489 | chr(195).chr(186) => 'u', chr(195).chr(187) => 'u', |
---|
490 | chr(195).chr(188) => 'u', chr(195).chr(189) => 'y', |
---|
491 | chr(195).chr(191) => 'y', |
---|
492 | // Decompositions for Latin Extended-A |
---|
493 | chr(196).chr(128) => 'A', chr(196).chr(129) => 'a', |
---|
494 | chr(196).chr(130) => 'A', chr(196).chr(131) => 'a', |
---|
495 | chr(196).chr(132) => 'A', chr(196).chr(133) => 'a', |
---|
496 | chr(196).chr(134) => 'C', chr(196).chr(135) => 'c', |
---|
497 | chr(196).chr(136) => 'C', chr(196).chr(137) => 'c', |
---|
498 | chr(196).chr(138) => 'C', chr(196).chr(139) => 'c', |
---|
499 | chr(196).chr(140) => 'C', chr(196).chr(141) => 'c', |
---|
500 | chr(196).chr(142) => 'D', chr(196).chr(143) => 'd', |
---|
501 | chr(196).chr(144) => 'D', chr(196).chr(145) => 'd', |
---|
502 | chr(196).chr(146) => 'E', chr(196).chr(147) => 'e', |
---|
503 | chr(196).chr(148) => 'E', chr(196).chr(149) => 'e', |
---|
504 | chr(196).chr(150) => 'E', chr(196).chr(151) => 'e', |
---|
505 | chr(196).chr(152) => 'E', chr(196).chr(153) => 'e', |
---|
506 | chr(196).chr(154) => 'E', chr(196).chr(155) => 'e', |
---|
507 | chr(196).chr(156) => 'G', chr(196).chr(157) => 'g', |
---|
508 | chr(196).chr(158) => 'G', chr(196).chr(159) => 'g', |
---|
509 | chr(196).chr(160) => 'G', chr(196).chr(161) => 'g', |
---|
510 | chr(196).chr(162) => 'G', chr(196).chr(163) => 'g', |
---|
511 | chr(196).chr(164) => 'H', chr(196).chr(165) => 'h', |
---|
512 | chr(196).chr(166) => 'H', chr(196).chr(167) => 'h', |
---|
513 | chr(196).chr(168) => 'I', chr(196).chr(169) => 'i', |
---|
514 | chr(196).chr(170) => 'I', chr(196).chr(171) => 'i', |
---|
515 | chr(196).chr(172) => 'I', chr(196).chr(173) => 'i', |
---|
516 | chr(196).chr(174) => 'I', chr(196).chr(175) => 'i', |
---|
517 | chr(196).chr(176) => 'I', chr(196).chr(177) => 'i', |
---|
518 | chr(196).chr(178) => 'IJ',chr(196).chr(179) => 'ij', |
---|
519 | chr(196).chr(180) => 'J', chr(196).chr(181) => 'j', |
---|
520 | chr(196).chr(182) => 'K', chr(196).chr(183) => 'k', |
---|
521 | chr(196).chr(184) => 'k', chr(196).chr(185) => 'L', |
---|
522 | chr(196).chr(186) => 'l', chr(196).chr(187) => 'L', |
---|
523 | chr(196).chr(188) => 'l', chr(196).chr(189) => 'L', |
---|
524 | chr(196).chr(190) => 'l', chr(196).chr(191) => 'L', |
---|
525 | chr(197).chr(128) => 'l', chr(197).chr(129) => 'L', |
---|
526 | chr(197).chr(130) => 'l', chr(197).chr(131) => 'N', |
---|
527 | chr(197).chr(132) => 'n', chr(197).chr(133) => 'N', |
---|
528 | chr(197).chr(134) => 'n', chr(197).chr(135) => 'N', |
---|
529 | chr(197).chr(136) => 'n', chr(197).chr(137) => 'N', |
---|
530 | chr(197).chr(138) => 'n', chr(197).chr(139) => 'N', |
---|
531 | chr(197).chr(140) => 'O', chr(197).chr(141) => 'o', |
---|
532 | chr(197).chr(142) => 'O', chr(197).chr(143) => 'o', |
---|
533 | chr(197).chr(144) => 'O', chr(197).chr(145) => 'o', |
---|
534 | chr(197).chr(146) => 'OE',chr(197).chr(147) => 'oe', |
---|
535 | chr(197).chr(148) => 'R',chr(197).chr(149) => 'r', |
---|
536 | chr(197).chr(150) => 'R',chr(197).chr(151) => 'r', |
---|
537 | chr(197).chr(152) => 'R',chr(197).chr(153) => 'r', |
---|
538 | chr(197).chr(154) => 'S',chr(197).chr(155) => 's', |
---|
539 | chr(197).chr(156) => 'S',chr(197).chr(157) => 's', |
---|
540 | chr(197).chr(158) => 'S',chr(197).chr(159) => 's', |
---|
541 | chr(197).chr(160) => 'S', chr(197).chr(161) => 's', |
---|
542 | chr(197).chr(162) => 'T', chr(197).chr(163) => 't', |
---|
543 | chr(197).chr(164) => 'T', chr(197).chr(165) => 't', |
---|
544 | chr(197).chr(166) => 'T', chr(197).chr(167) => 't', |
---|
545 | chr(197).chr(168) => 'U', chr(197).chr(169) => 'u', |
---|
546 | chr(197).chr(170) => 'U', chr(197).chr(171) => 'u', |
---|
547 | chr(197).chr(172) => 'U', chr(197).chr(173) => 'u', |
---|
548 | chr(197).chr(174) => 'U', chr(197).chr(175) => 'u', |
---|
549 | chr(197).chr(176) => 'U', chr(197).chr(177) => 'u', |
---|
550 | chr(197).chr(178) => 'U', chr(197).chr(179) => 'u', |
---|
551 | chr(197).chr(180) => 'W', chr(197).chr(181) => 'w', |
---|
552 | chr(197).chr(182) => 'Y', chr(197).chr(183) => 'y', |
---|
553 | chr(197).chr(184) => 'Y', chr(197).chr(185) => 'Z', |
---|
554 | chr(197).chr(186) => 'z', chr(197).chr(187) => 'Z', |
---|
555 | chr(197).chr(188) => 'z', chr(197).chr(189) => 'Z', |
---|
556 | chr(197).chr(190) => 'z', chr(197).chr(191) => 's', |
---|
557 | // Euro Sign |
---|
558 | chr(226).chr(130).chr(172) => 'E', |
---|
559 | // GBP (Pound) Sign |
---|
560 | chr(194).chr(163) => ''); |
---|
561 | |
---|
562 | $string = strtr($string, $chars); |
---|
563 | } else { |
---|
564 | // Assume ISO-8859-1 if not UTF-8 |
---|
565 | $chars['in'] = chr(128).chr(131).chr(138).chr(142).chr(154).chr(158) |
---|
566 | .chr(159).chr(162).chr(165).chr(181).chr(192).chr(193).chr(194) |
---|
567 | .chr(195).chr(196).chr(197).chr(199).chr(200).chr(201).chr(202) |
---|
568 | .chr(203).chr(204).chr(205).chr(206).chr(207).chr(209).chr(210) |
---|
569 | .chr(211).chr(212).chr(213).chr(214).chr(216).chr(217).chr(218) |
---|
570 | .chr(219).chr(220).chr(221).chr(224).chr(225).chr(226).chr(227) |
---|
571 | .chr(228).chr(229).chr(231).chr(232).chr(233).chr(234).chr(235) |
---|
572 | .chr(236).chr(237).chr(238).chr(239).chr(241).chr(242).chr(243) |
---|
573 | .chr(244).chr(245).chr(246).chr(248).chr(249).chr(250).chr(251) |
---|
574 | .chr(252).chr(253).chr(255); |
---|
575 | |
---|
576 | $chars['out'] = "EfSZszYcYuAAAAAACEEEEIIIINOOOOOOUUUUYaaaaaaceeeeiiiinoooooouuuuyy"; |
---|
577 | |
---|
578 | $string = strtr($string, $chars['in'], $chars['out']); |
---|
579 | $double_chars['in'] = array(chr(140), chr(156), chr(198), chr(208), chr(222), chr(223), chr(230), chr(240), chr(254)); |
---|
580 | $double_chars['out'] = array('OE', 'oe', 'AE', 'DH', 'TH', 'ss', 'ae', 'dh', 'th'); |
---|
581 | $string = str_replace($double_chars['in'], $double_chars['out'], $string); |
---|
582 | } |
---|
583 | |
---|
584 | return $string; |
---|
585 | } |
---|
586 | |
---|
587 | /** |
---|
588 | * Sanitizes a filename replacing whitespace with dashes |
---|
589 | * |
---|
590 | * Removes special characters that are illegal in filenames on certain |
---|
591 | * operating systems and special characters requiring special escaping |
---|
592 | * to manipulate at the command line. Replaces spaces and consecutive |
---|
593 | * dashes with a single dash. Trim period, dash and underscore from beginning |
---|
594 | * and end of filename. |
---|
595 | * |
---|
596 | * @since 2.1.0 |
---|
597 | * |
---|
598 | * @param string $filename The filename to be sanitized |
---|
599 | * @return string The sanitized filename |
---|
600 | */ |
---|
601 | function sanitize_file_name( $filename ) { |
---|
602 | $filename_raw = $filename; |
---|
603 | $special_chars = array("?", "[", "]", "/", "\\", "=", "<", ">", ":", ";", ",", "'", "\"", "&", "$", "#", "*", "(", ")", "|", "~", "`", "!", "{", "}"); |
---|
604 | $special_chars = apply_filters('sanitize_file_name_chars', $special_chars, $filename_raw); |
---|
605 | $filename = str_replace($special_chars, '', $filename); |
---|
606 | $filename = preg_replace('/[\s-]+/', '-', $filename); |
---|
607 | $filename = trim($filename, '.-_'); |
---|
608 | return apply_filters('sanitize_file_name', $filename, $filename_raw); |
---|
609 | } |
---|
610 | |
---|
611 | /** |
---|
612 | * Sanitize username stripping out unsafe characters. |
---|
613 | * |
---|
614 | * If $strict is true, only alphanumeric characters (as well as _, space, ., -, |
---|
615 | * @) are returned. |
---|
616 | * Removes tags, octets, entities, and if strict is enabled, will remove all |
---|
617 | * non-ASCII characters. After sanitizing, it passes the username, raw username |
---|
618 | * (the username in the parameter), and the strict parameter as parameters for |
---|
619 | * the filter. |
---|
620 | * |
---|
621 | * @since 2.0.0 |
---|
622 | * @uses apply_filters() Calls 'sanitize_user' hook on username, raw username, |
---|
623 | * and $strict parameter. |
---|
624 | * |
---|
625 | * @param string $username The username to be sanitized. |
---|
626 | * @param bool $strict If set limits $username to specific characters. Default false. |
---|
627 | * @return string The sanitized username, after passing through filters. |
---|
628 | */ |
---|
629 | function sanitize_user( $username, $strict = false ) { |
---|
630 | $raw_username = $username; |
---|
631 | $username = strip_tags($username); |
---|
632 | // Kill octets |
---|
633 | $username = preg_replace('|%([a-fA-F0-9][a-fA-F0-9])|', '', $username); |
---|
634 | $username = preg_replace('/&.+?;/', '', $username); // Kill entities |
---|
635 | |
---|
636 | // If strict, reduce to ASCII for max portability. |
---|
637 | if ( $strict ) |
---|
638 | $username = preg_replace('|[^a-z0-9 _.\-@]|i', '', $username); |
---|
639 | |
---|
640 | // Consolidate contiguous whitespace |
---|
641 | $username = preg_replace('|\s+|', ' ', $username); |
---|
642 | |
---|
643 | return apply_filters('sanitize_user', $username, $raw_username, $strict); |
---|
644 | } |
---|
645 | |
---|
646 | /** |
---|
647 | * Sanitizes title or use fallback title. |
---|
648 | * |
---|
649 | * Specifically, HTML and PHP tags are stripped. Further actions can be added |
---|
650 | * via the plugin API. If $title is empty and $fallback_title is set, the latter |
---|
651 | * will be used. |
---|
652 | * |
---|
653 | * @since 1.0.0 |
---|
654 | * |
---|
655 | * @param string $title The string to be sanitized. |
---|
656 | * @param string $fallback_title Optional. A title to use if $title is empty. |
---|
657 | * @return string The sanitized string. |
---|
658 | */ |
---|
659 | function sanitize_title($title, $fallback_title = '') { |
---|
660 | $raw_title = $title; |
---|
661 | $title = strip_tags($title); |
---|
662 | $title = apply_filters('sanitize_title', $title, $raw_title); |
---|
663 | |
---|
664 | if ( '' === $title || false === $title ) |
---|
665 | $title = $fallback_title; |
---|
666 | |
---|
667 | return $title; |
---|
668 | } |
---|
669 | |
---|
670 | /** |
---|
671 | * Sanitizes title, replacing whitespace with dashes. |
---|
672 | * |
---|
673 | * Limits the output to alphanumeric characters, underscore (_) and dash (-). |
---|
674 | * Whitespace becomes a dash. |
---|
675 | * |
---|
676 | * @since 1.2.0 |
---|
677 | * |
---|
678 | * @param string $title The title to be sanitized. |
---|
679 | * @return string The sanitized title. |
---|
680 | */ |
---|
681 | function sanitize_title_with_dashes($title) { |
---|
682 | $title = strip_tags($title); |
---|
683 | // Preserve escaped octets. |
---|
684 | $title = preg_replace('|%([a-fA-F0-9][a-fA-F0-9])|', '---$1---', $title); |
---|
685 | // Remove percent signs that are not part of an octet. |
---|
686 | $title = str_replace('%', '', $title); |
---|
687 | // Restore octets. |
---|
688 | $title = preg_replace('|---([a-fA-F0-9][a-fA-F0-9])---|', '%$1', $title); |
---|
689 | |
---|
690 | $title = remove_accents($title); |
---|
691 | if (seems_utf8($title)) { |
---|
692 | if (function_exists('mb_strtolower')) { |
---|
693 | $title = mb_strtolower($title, 'UTF-8'); |
---|
694 | } |
---|
695 | $title = utf8_uri_encode($title, 200); |
---|
696 | } |
---|
697 | |
---|
698 | $title = strtolower($title); |
---|
699 | $title = preg_replace('/&.+?;/', '', $title); // kill entities |
---|
700 | $title = str_replace('.', '-', $title); |
---|
701 | $title = preg_replace('/[^%a-z0-9 _-]/', '', $title); |
---|
702 | $title = preg_replace('/\s+/', '-', $title); |
---|
703 | $title = preg_replace('|-+|', '-', $title); |
---|
704 | $title = trim($title, '-'); |
---|
705 | |
---|
706 | return $title; |
---|
707 | } |
---|
708 | |
---|
709 | /** |
---|
710 | * Ensures a string is a valid SQL order by clause. |
---|
711 | * |
---|
712 | * Accepts one or more columns, with or without ASC/DESC, and also accepts |
---|
713 | * RAND(). |
---|
714 | * |
---|
715 | * @since 2.5.1 |
---|
716 | * |
---|
717 | * @param string $orderby Order by string to be checked. |
---|
718 | * @return string|false Returns the order by clause if it is a match, false otherwise. |
---|
719 | */ |
---|
720 | function sanitize_sql_orderby( $orderby ){ |
---|
721 | preg_match('/^\s*([a-z0-9_]+(\s+(ASC|DESC))?(\s*,\s*|\s*$))+|^\s*RAND\(\s*\)\s*$/i', $orderby, $obmatches); |
---|
722 | if ( !$obmatches ) |
---|
723 | return false; |
---|
724 | return $orderby; |
---|
725 | } |
---|
726 | |
---|
727 | /** |
---|
728 | * Santizes a html classname to ensure it only contains valid characters |
---|
729 | * |
---|
730 | * Strips the string down to A-Z,a-z,0-9,'-' if this results in an empty |
---|
731 | * string then it will return the alternative value supplied. |
---|
732 | * |
---|
733 | * @todo Expand to support the full range of CDATA that a class attribute can contain. |
---|
734 | * |
---|
735 | * @since 2.8.0 |
---|
736 | * |
---|
737 | * @param string $class The classname to be sanitized |
---|
738 | * @param string $fallback The value to return if the sanitization end's up as an empty string. |
---|
739 | * @return string The sanitized value |
---|
740 | */ |
---|
741 | function sanitize_html_class($class, $fallback){ |
---|
742 | //Strip out any % encoded octets |
---|
743 | $sanitized = preg_replace('|%[a-fA-F0-9][a-fA-F0-9]|', '', $class); |
---|
744 | |
---|
745 | //Limit to A-Z,a-z,0-9,'-' |
---|
746 | $sanitized = preg_replace('/[^A-Za-z0-9-]/', '', $sanitized); |
---|
747 | |
---|
748 | if ('' == $sanitized) |
---|
749 | $sanitized = $fallback; |
---|
750 | |
---|
751 | return apply_filters('sanitize_html_class',$sanitized, $class, $fallback); |
---|
752 | } |
---|
753 | |
---|
754 | /** |
---|
755 | * Converts a number of characters from a string. |
---|
756 | * |
---|
757 | * Metadata tags <<title>> and <<category>> are removed, <<br>> and <<hr>> are |
---|
758 | * converted into correct XHTML and Unicode characters are converted to the |
---|
759 | * valid range. |
---|
760 | * |
---|
761 | * @since 0.71 |
---|
762 | * |
---|
763 | * @param string $content String of characters to be converted. |
---|
764 | * @param string $deprecated Not used. |
---|
765 | * @return string Converted string. |
---|
766 | */ |
---|
767 | function convert_chars($content, $deprecated = '') { |
---|
768 | // Translation of invalid Unicode references range to valid range |
---|
769 | $wp_htmltranswinuni = array( |
---|
770 | '€' => '€', // the Euro sign |
---|
771 | '' => '', |
---|
772 | '‚' => '‚', // these are Windows CP1252 specific characters |
---|
773 | 'ƒ' => 'ƒ', // they would look weird on non-Windows browsers |
---|
774 | '„' => '„', |
---|
775 | '…' => '…', |
---|
776 | '†' => '†', |
---|
777 | '‡' => '‡', |
---|
778 | 'ˆ' => 'ˆ', |
---|
779 | '‰' => '‰', |
---|
780 | 'Š' => 'Š', |
---|
781 | '‹' => '‹', |
---|
782 | 'Œ' => 'Œ', |
---|
783 | '' => '', |
---|
784 | 'Ž' => 'ž', |
---|
785 | '' => '', |
---|
786 | '' => '', |
---|
787 | '‘' => '‘', |
---|
788 | '’' => '’', |
---|
789 | '“' => '“', |
---|
790 | '”' => '”', |
---|
791 | '•' => '•', |
---|
792 | '–' => '–', |
---|
793 | '—' => '—', |
---|
794 | '˜' => '˜', |
---|
795 | '™' => '™', |
---|
796 | 'š' => 'š', |
---|
797 | '›' => '›', |
---|
798 | 'œ' => 'œ', |
---|
799 | '' => '', |
---|
800 | 'ž' => '', |
---|
801 | 'Ÿ' => 'Ÿ' |
---|
802 | ); |
---|
803 | |
---|
804 | // Remove metadata tags |
---|
805 | $content = preg_replace('/<title>(.+?)<\/title>/','',$content); |
---|
806 | $content = preg_replace('/<category>(.+?)<\/category>/','',$content); |
---|
807 | |
---|
808 | // Converts lone & characters into & (a.k.a. &) |
---|
809 | $content = preg_replace('/&([^#])(?![a-z1-4]{1,8};)/i', '&$1', $content); |
---|
810 | |
---|
811 | // Fix Word pasting |
---|
812 | $content = strtr($content, $wp_htmltranswinuni); |
---|
813 | |
---|
814 | // Just a little XHTML help |
---|
815 | $content = str_replace('<br>', '<br />', $content); |
---|
816 | $content = str_replace('<hr>', '<hr />', $content); |
---|
817 | |
---|
818 | return $content; |
---|
819 | } |
---|
820 | |
---|
821 | /** |
---|
822 | * Callback used to change %uXXXX to &#YYY; syntax |
---|
823 | * |
---|
824 | * @since 2.8? |
---|
825 | * |
---|
826 | * @param array $matches Single Match |
---|
827 | * @return string An HTML entity |
---|
828 | */ |
---|
829 | function funky_javascript_callback($matches) { |
---|
830 | return "&#".base_convert($matches[1],16,10).";"; |
---|
831 | } |
---|
832 | |
---|
833 | /** |
---|
834 | * Fixes javascript bugs in browsers. |
---|
835 | * |
---|
836 | * Converts unicode characters to HTML numbered entities. |
---|
837 | * |
---|
838 | * @since 1.5.0 |
---|
839 | * @uses $is_macIE |
---|
840 | * @uses $is_winIE |
---|
841 | * |
---|
842 | * @param string $text Text to be made safe. |
---|
843 | * @return string Fixed text. |
---|
844 | */ |
---|
845 | function funky_javascript_fix($text) { |
---|
846 | // Fixes for browsers' javascript bugs |
---|
847 | global $is_macIE, $is_winIE; |
---|
848 | |
---|
849 | if ( $is_winIE || $is_macIE ) |
---|
850 | $text = preg_replace_callback("/\%u([0-9A-F]{4,4})/", |
---|
851 | "funky_javascript_callback", |
---|
852 | $text); |
---|
853 | |
---|
854 | return $text; |
---|
855 | } |
---|
856 | |
---|
857 | /** |
---|
858 | * Will only balance the tags if forced to and the option is set to balance tags. |
---|
859 | * |
---|
860 | * The option 'use_balanceTags' is used for whether the tags will be balanced. |
---|
861 | * Both the $force parameter and 'use_balanceTags' option will have to be true |
---|
862 | * before the tags will be balanced. |
---|
863 | * |
---|
864 | * @since 0.71 |
---|
865 | * |
---|
866 | * @param string $text Text to be balanced |
---|
867 | * @param bool $force Forces balancing, ignoring the value of the option. Default false. |
---|
868 | * @return string Balanced text |
---|
869 | */ |
---|
870 | function balanceTags( $text, $force = false ) { |
---|
871 | if ( !$force && get_option('use_balanceTags') == 0 ) |
---|
872 | return $text; |
---|
873 | return force_balance_tags( $text ); |
---|
874 | } |
---|
875 | |
---|
876 | /** |
---|
877 | * Balances tags of string using a modified stack. |
---|
878 | * |
---|
879 | * @since 2.0.4 |
---|
880 | * |
---|
881 | * @author Leonard Lin <leonard@acm.org> |
---|
882 | * @license GPL v2.0 |
---|
883 | * @copyright November 4, 2001 |
---|
884 | * @version 1.1 |
---|
885 | * @todo Make better - change loop condition to $text in 1.2 |
---|
886 | * @internal Modified by Scott Reilly (coffee2code) 02 Aug 2004 |
---|
887 | * 1.1 Fixed handling of append/stack pop order of end text |
---|
888 | * Added Cleaning Hooks |
---|
889 | * 1.0 First Version |
---|
890 | * |
---|
891 | * @param string $text Text to be balanced. |
---|
892 | * @return string Balanced text. |
---|
893 | */ |
---|
894 | function force_balance_tags( $text ) { |
---|
895 | $tagstack = array(); $stacksize = 0; $tagqueue = ''; $newtext = ''; |
---|
896 | $single_tags = array('br', 'hr', 'img', 'input'); //Known single-entity/self-closing tags |
---|
897 | $nestable_tags = array('blockquote', 'div', 'span'); //Tags that can be immediately nested within themselves |
---|
898 | |
---|
899 | # WP bug fix for comments - in case you REALLY meant to type '< !--' |
---|
900 | $text = str_replace('< !--', '< !--', $text); |
---|
901 | # WP bug fix for LOVE <3 (and other situations with '<' before a number) |
---|
902 | $text = preg_replace('#<([0-9]{1})#', '<$1', $text); |
---|
903 | |
---|
904 | while (preg_match("/<(\/?\w*)\s*([^>]*)>/",$text,$regex)) { |
---|
905 | $newtext .= $tagqueue; |
---|
906 | |
---|
907 | $i = strpos($text,$regex[0]); |
---|
908 | $l = strlen($regex[0]); |
---|
909 | |
---|
910 | // clear the shifter |
---|
911 | $tagqueue = ''; |
---|
912 | // Pop or Push |
---|
913 | if ( isset($regex[1][0]) && '/' == $regex[1][0] ) { // End Tag |
---|
914 | $tag = strtolower(substr($regex[1],1)); |
---|
915 | // if too many closing tags |
---|
916 | if($stacksize <= 0) { |
---|
917 | $tag = ''; |
---|
918 | //or close to be safe $tag = '/' . $tag; |
---|
919 | } |
---|
920 | // if stacktop value = tag close value then pop |
---|
921 | else if ($tagstack[$stacksize - 1] == $tag) { // found closing tag |
---|
922 | $tag = '</' . $tag . '>'; // Close Tag |
---|
923 | // Pop |
---|
924 | array_pop ($tagstack); |
---|
925 | $stacksize--; |
---|
926 | } else { // closing tag not at top, search for it |
---|
927 | for ($j=$stacksize-1;$j>=0;$j--) { |
---|
928 | if ($tagstack[$j] == $tag) { |
---|
929 | // add tag to tagqueue |
---|
930 | for ($k=$stacksize-1;$k>=$j;$k--){ |
---|
931 | $tagqueue .= '</' . array_pop ($tagstack) . '>'; |
---|
932 | $stacksize--; |
---|
933 | } |
---|
934 | break; |
---|
935 | } |
---|
936 | } |
---|
937 | $tag = ''; |
---|
938 | } |
---|
939 | } else { // Begin Tag |
---|
940 | $tag = strtolower($regex[1]); |
---|
941 | |
---|
942 | // Tag Cleaning |
---|
943 | |
---|
944 | // If self-closing or '', don't do anything. |
---|
945 | if((substr($regex[2],-1) == '/') || ($tag == '')) { |
---|
946 | } |
---|
947 | // ElseIf it's a known single-entity tag but it doesn't close itself, do so |
---|
948 | elseif ( in_array($tag, $single_tags) ) { |
---|
949 | $regex[2] .= '/'; |
---|
950 | } else { // Push the tag onto the stack |
---|
951 | // If the top of the stack is the same as the tag we want to push, close previous tag |
---|
952 | if (($stacksize > 0) && !in_array($tag, $nestable_tags) && ($tagstack[$stacksize - 1] == $tag)) { |
---|
953 | $tagqueue = '</' . array_pop ($tagstack) . '>'; |
---|
954 | $stacksize--; |
---|
955 | } |
---|
956 | $stacksize = array_push ($tagstack, $tag); |
---|
957 | } |
---|
958 | |
---|
959 | // Attributes |
---|
960 | $attributes = $regex[2]; |
---|
961 | if($attributes) { |
---|
962 | $attributes = ' '.$attributes; |
---|
963 | } |
---|
964 | $tag = '<'.$tag.$attributes.'>'; |
---|
965 | //If already queuing a close tag, then put this tag on, too |
---|
966 | if ($tagqueue) { |
---|
967 | $tagqueue .= $tag; |
---|
968 | $tag = ''; |
---|
969 | } |
---|
970 | } |
---|
971 | $newtext .= substr($text,0,$i) . $tag; |
---|
972 | $text = substr($text,$i+$l); |
---|
973 | } |
---|
974 | |
---|
975 | // Clear Tag Queue |
---|
976 | $newtext .= $tagqueue; |
---|
977 | |
---|
978 | // Add Remaining text |
---|
979 | $newtext .= $text; |
---|
980 | |
---|
981 | // Empty Stack |
---|
982 | while($x = array_pop($tagstack)) { |
---|
983 | $newtext .= '</' . $x . '>'; // Add remaining tags to close |
---|
984 | } |
---|
985 | |
---|
986 | // WP fix for the bug with HTML comments |
---|
987 | $newtext = str_replace("< !--","<!--",$newtext); |
---|
988 | $newtext = str_replace("< !--","< !--",$newtext); |
---|
989 | |
---|
990 | return $newtext; |
---|
991 | } |
---|
992 | |
---|
993 | /** |
---|
994 | * Acts on text which is about to be edited. |
---|
995 | * |
---|
996 | * Unless $richedit is set, it is simply a holder for the 'format_to_edit' |
---|
997 | * filter. If $richedit is set true htmlspecialchars() will be run on the |
---|
998 | * content, converting special characters to HTMl entities. |
---|
999 | * |
---|
1000 | * @since 0.71 |
---|
1001 | * |
---|
1002 | * @param string $content The text about to be edited. |
---|
1003 | * @param bool $richedit Whether or not the $content should pass through htmlspecialchars(). Default false. |
---|
1004 | * @return string The text after the filter (and possibly htmlspecialchars()) has been run. |
---|
1005 | */ |
---|
1006 | function format_to_edit($content, $richedit = false) { |
---|
1007 | $content = apply_filters('format_to_edit', $content); |
---|
1008 | if (! $richedit ) |
---|
1009 | $content = htmlspecialchars($content); |
---|
1010 | return $content; |
---|
1011 | } |
---|
1012 | |
---|
1013 | /** |
---|
1014 | * Holder for the 'format_to_post' filter. |
---|
1015 | * |
---|
1016 | * @since 0.71 |
---|
1017 | * |
---|
1018 | * @param string $content The text to pass through the filter. |
---|
1019 | * @return string Text returned from the 'format_to_post' filter. |
---|
1020 | */ |
---|
1021 | function format_to_post($content) { |
---|
1022 | $content = apply_filters('format_to_post', $content); |
---|
1023 | return $content; |
---|
1024 | } |
---|
1025 | |
---|
1026 | /** |
---|
1027 | * Add leading zeros when necessary. |
---|
1028 | * |
---|
1029 | * If you set the threshold to '4' and the number is '10', then you will get |
---|
1030 | * back '0010'. If you set the number to '4' and the number is '5000', then you |
---|
1031 | * will get back '5000'. |
---|
1032 | * |
---|
1033 | * Uses sprintf to append the amount of zeros based on the $threshold parameter |
---|
1034 | * and the size of the number. If the number is large enough, then no zeros will |
---|
1035 | * be appended. |
---|
1036 | * |
---|
1037 | * @since 0.71 |
---|
1038 | * |
---|
1039 | * @param mixed $number Number to append zeros to if not greater than threshold. |
---|
1040 | * @param int $threshold Digit places number needs to be to not have zeros added. |
---|
1041 | * @return string Adds leading zeros to number if needed. |
---|
1042 | */ |
---|
1043 | function zeroise($number, $threshold) { |
---|
1044 | return sprintf('%0'.$threshold.'s', $number); |
---|
1045 | } |
---|
1046 | |
---|
1047 | /** |
---|
1048 | * Adds backslashes before letters and before a number at the start of a string. |
---|
1049 | * |
---|
1050 | * @since 0.71 |
---|
1051 | * |
---|
1052 | * @param string $string Value to which backslashes will be added. |
---|
1053 | * @return string String with backslashes inserted. |
---|
1054 | */ |
---|
1055 | function backslashit($string) { |
---|
1056 | $string = preg_replace('/^([0-9])/', '\\\\\\\\\1', $string); |
---|
1057 | $string = preg_replace('/([a-z])/i', '\\\\\1', $string); |
---|
1058 | return $string; |
---|
1059 | } |
---|
1060 | |
---|
1061 | /** |
---|
1062 | * Appends a trailing slash. |
---|
1063 | * |
---|
1064 | * Will remove trailing slash if it exists already before adding a trailing |
---|
1065 | * slash. This prevents double slashing a string or path. |
---|
1066 | * |
---|
1067 | * The primary use of this is for paths and thus should be used for paths. It is |
---|
1068 | * not restricted to paths and offers no specific path support. |
---|
1069 | * |
---|
1070 | * @since 1.2.0 |
---|
1071 | * @uses untrailingslashit() Unslashes string if it was slashed already. |
---|
1072 | * |
---|
1073 | * @param string $string What to add the trailing slash to. |
---|
1074 | * @return string String with trailing slash added. |
---|
1075 | */ |
---|
1076 | function trailingslashit($string) { |
---|
1077 | return untrailingslashit($string) . '/'; |
---|
1078 | } |
---|
1079 | |
---|
1080 | /** |
---|
1081 | * Removes trailing slash if it exists. |
---|
1082 | * |
---|
1083 | * The primary use of this is for paths and thus should be used for paths. It is |
---|
1084 | * not restricted to paths and offers no specific path support. |
---|
1085 | * |
---|
1086 | * @since 2.2.0 |
---|
1087 | * |
---|
1088 | * @param string $string What to remove the trailing slash from. |
---|
1089 | * @return string String without the trailing slash. |
---|
1090 | */ |
---|
1091 | function untrailingslashit($string) { |
---|
1092 | return rtrim($string, '/'); |
---|
1093 | } |
---|
1094 | |
---|
1095 | /** |
---|
1096 | * Adds slashes to escape strings. |
---|
1097 | * |
---|
1098 | * Slashes will first be removed if magic_quotes_gpc is set, see {@link |
---|
1099 | * http://www.php.net/magic_quotes} for more details. |
---|
1100 | * |
---|
1101 | * @since 0.71 |
---|
1102 | * |
---|
1103 | * @param string $gpc The string returned from HTTP request data. |
---|
1104 | * @return string Returns a string escaped with slashes. |
---|
1105 | */ |
---|
1106 | function addslashes_gpc($gpc) { |
---|
1107 | global $wpdb; |
---|
1108 | |
---|
1109 | if (get_magic_quotes_gpc()) { |
---|
1110 | $gpc = stripslashes($gpc); |
---|
1111 | } |
---|
1112 | |
---|
1113 | return $wpdb->escape($gpc); |
---|
1114 | } |
---|
1115 | |
---|
1116 | /** |
---|
1117 | * Navigates through an array and removes slashes from the values. |
---|
1118 | * |
---|
1119 | * If an array is passed, the array_map() function causes a callback to pass the |
---|
1120 | * value back to the function. The slashes from this value will removed. |
---|
1121 | * |
---|
1122 | * @since 2.0.0 |
---|
1123 | * |
---|
1124 | * @param array|string $value The array or string to be striped. |
---|
1125 | * @return array|string Stripped array (or string in the callback). |
---|
1126 | */ |
---|
1127 | function stripslashes_deep($value) { |
---|
1128 | $value = is_array($value) ? array_map('stripslashes_deep', $value) : stripslashes($value); |
---|
1129 | return $value; |
---|
1130 | } |
---|
1131 | |
---|
1132 | /** |
---|
1133 | * Navigates through an array and encodes the values to be used in a URL. |
---|
1134 | * |
---|
1135 | * Uses a callback to pass the value of the array back to the function as a |
---|
1136 | * string. |
---|
1137 | * |
---|
1138 | * @since 2.2.0 |
---|
1139 | * |
---|
1140 | * @param array|string $value The array or string to be encoded. |
---|
1141 | * @return array|string $value The encoded array (or string from the callback). |
---|
1142 | */ |
---|
1143 | function urlencode_deep($value) { |
---|
1144 | $value = is_array($value) ? array_map('urlencode_deep', $value) : urlencode($value); |
---|
1145 | return $value; |
---|
1146 | } |
---|
1147 | |
---|
1148 | /** |
---|
1149 | * Converts email addresses characters to HTML entities to block spam bots. |
---|
1150 | * |
---|
1151 | * @since 0.71 |
---|
1152 | * |
---|
1153 | * @param string $emailaddy Email address. |
---|
1154 | * @param int $mailto Optional. Range from 0 to 1. Used for encoding. |
---|
1155 | * @return string Converted email address. |
---|
1156 | */ |
---|
1157 | function antispambot($emailaddy, $mailto=0) { |
---|
1158 | $emailNOSPAMaddy = ''; |
---|
1159 | srand ((float) microtime() * 1000000); |
---|
1160 | for ($i = 0; $i < strlen($emailaddy); $i = $i + 1) { |
---|
1161 | $j = floor(rand(0, 1+$mailto)); |
---|
1162 | if ($j==0) { |
---|
1163 | $emailNOSPAMaddy .= '&#'.ord(substr($emailaddy,$i,1)).';'; |
---|
1164 | } elseif ($j==1) { |
---|
1165 | $emailNOSPAMaddy .= substr($emailaddy,$i,1); |
---|
1166 | } elseif ($j==2) { |
---|
1167 | $emailNOSPAMaddy .= '%'.zeroise(dechex(ord(substr($emailaddy, $i, 1))), 2); |
---|
1168 | } |
---|
1169 | } |
---|
1170 | $emailNOSPAMaddy = str_replace('@','@',$emailNOSPAMaddy); |
---|
1171 | return $emailNOSPAMaddy; |
---|
1172 | } |
---|
1173 | |
---|
1174 | /** |
---|
1175 | * Callback to convert URI match to HTML A element. |
---|
1176 | * |
---|
1177 | * This function was backported from 2.5.0 to 2.3.2. Regex callback for {@link |
---|
1178 | * make_clickable()}. |
---|
1179 | * |
---|
1180 | * @since 2.3.2 |
---|
1181 | * @access private |
---|
1182 | * |
---|
1183 | * @param array $matches Single Regex Match. |
---|
1184 | * @return string HTML A element with URI address. |
---|
1185 | */ |
---|
1186 | function _make_url_clickable_cb($matches) { |
---|
1187 | $url = $matches[2]; |
---|
1188 | $url = esc_url($url); |
---|
1189 | if ( empty($url) ) |
---|
1190 | return $matches[0]; |
---|
1191 | return $matches[1] . "<a href=\"$url\" rel=\"nofollow\">$url</a>"; |
---|
1192 | } |
---|
1193 | |
---|
1194 | /** |
---|
1195 | * Callback to convert URL match to HTML A element. |
---|
1196 | * |
---|
1197 | * This function was backported from 2.5.0 to 2.3.2. Regex callback for {@link |
---|
1198 | * make_clickable()}. |
---|
1199 | * |
---|
1200 | * @since 2.3.2 |
---|
1201 | * @access private |
---|
1202 | * |
---|
1203 | * @param array $matches Single Regex Match. |
---|
1204 | * @return string HTML A element with URL address. |
---|
1205 | */ |
---|
1206 | function _make_web_ftp_clickable_cb($matches) { |
---|
1207 | $ret = ''; |
---|
1208 | $dest = $matches[2]; |
---|
1209 | $dest = 'http://' . $dest; |
---|
1210 | $dest = esc_url($dest); |
---|
1211 | if ( empty($dest) ) |
---|
1212 | return $matches[0]; |
---|
1213 | // removed trailing [,;:] from URL |
---|
1214 | if ( in_array(substr($dest, -1), array('.', ',', ';', ':')) === true ) { |
---|
1215 | $ret = substr($dest, -1); |
---|
1216 | $dest = substr($dest, 0, strlen($dest)-1); |
---|
1217 | } |
---|
1218 | return $matches[1] . "<a href=\"$dest\" rel=\"nofollow\">$dest</a>" . $ret; |
---|
1219 | } |
---|
1220 | |
---|
1221 | /** |
---|
1222 | * Callback to convert email address match to HTML A element. |
---|
1223 | * |
---|
1224 | * This function was backported from 2.5.0 to 2.3.2. Regex callback for {@link |
---|
1225 | * make_clickable()}. |
---|
1226 | * |
---|
1227 | * @since 2.3.2 |
---|
1228 | * @access private |
---|
1229 | * |
---|
1230 | * @param array $matches Single Regex Match. |
---|
1231 | * @return string HTML A element with email address. |
---|
1232 | */ |
---|
1233 | function _make_email_clickable_cb($matches) { |
---|
1234 | $email = $matches[2] . '@' . $matches[3]; |
---|
1235 | return $matches[1] . "<a href=\"mailto:$email\">$email</a>"; |
---|
1236 | } |
---|
1237 | |
---|
1238 | /** |
---|
1239 | * Convert plaintext URI to HTML links. |
---|
1240 | * |
---|
1241 | * Converts URI, www and ftp, and email addresses. Finishes by fixing links |
---|
1242 | * within links. |
---|
1243 | * |
---|
1244 | * @since 0.71 |
---|
1245 | * |
---|
1246 | * @param string $ret Content to convert URIs. |
---|
1247 | * @return string Content with converted URIs. |
---|
1248 | */ |
---|
1249 | function make_clickable($ret) { |
---|
1250 | $ret = ' ' . $ret; |
---|
1251 | // in testing, using arrays here was found to be faster |
---|
1252 | $ret = preg_replace_callback('#(?<=[\s>])(\()?([\w]+?://(?:[\w\\x80-\\xff\#$%&~/\-=?@\[\](+]|[.,;:](?![\s<])|(?(1)\)(?![\s<])|\)))+)#is', '_make_url_clickable_cb', $ret); |
---|
1253 | $ret = preg_replace_callback('#([\s>])((www|ftp)\.[\w\\x80-\\xff\#$%&~/.\-;:=,?@\[\]+]+)#is', '_make_web_ftp_clickable_cb', $ret); |
---|
1254 | $ret = preg_replace_callback('#([\s>])([.0-9a-z_+-]+)@(([0-9a-z-]+\.)+[0-9a-z]{2,})#i', '_make_email_clickable_cb', $ret); |
---|
1255 | // this one is not in an array because we need it to run last, for cleanup of accidental links within links |
---|
1256 | $ret = preg_replace("#(<a( [^>]+?>|>))<a [^>]+?>([^>]+?)</a></a>#i", "$1$3</a>", $ret); |
---|
1257 | $ret = trim($ret); |
---|
1258 | return $ret; |
---|
1259 | } |
---|
1260 | |
---|
1261 | /** |
---|
1262 | * Adds rel nofollow string to all HTML A elements in content. |
---|
1263 | * |
---|
1264 | * @since 1.5.0 |
---|
1265 | * |
---|
1266 | * @param string $text Content that may contain HTML A elements. |
---|
1267 | * @return string Converted content. |
---|
1268 | */ |
---|
1269 | function wp_rel_nofollow( $text ) { |
---|
1270 | global $wpdb; |
---|
1271 | // This is a pre save filter, so text is already escaped. |
---|
1272 | $text = stripslashes($text); |
---|
1273 | $r = array( |
---|
1274 | "|(<a\b[^>]*?\srel=['\"])(?![^'\"]*\bnofollow\b)|i" => "$1nofollow ", |
---|
1275 | "|<a\b(?![^>]*\srel=['\"])|i" => '<a rel="nofollow"' |
---|
1276 | ); |
---|
1277 | $text = preg_replace(array_keys($r), array_values($r), $text); |
---|
1278 | $text = $wpdb->escape($text); |
---|
1279 | return $text; |
---|
1280 | } |
---|
1281 | |
---|
1282 | /** |
---|
1283 | * Convert one smiley code to the icon graphic file equivalent. |
---|
1284 | * |
---|
1285 | * Looks up one smiley code in the $wpsmiliestrans global array and returns an |
---|
1286 | * <img> string for that smiley. |
---|
1287 | * |
---|
1288 | * @global array $wpsmiliestrans |
---|
1289 | * @since 2.8.0 |
---|
1290 | * |
---|
1291 | * @param string $smiley Smiley code to convert to image. |
---|
1292 | * @return string Image string for smiley. |
---|
1293 | */ |
---|
1294 | function translate_smiley($smiley) { |
---|
1295 | global $wpsmiliestrans; |
---|
1296 | |
---|
1297 | if (count($smiley) == 0) { |
---|
1298 | return ''; |
---|
1299 | } |
---|
1300 | |
---|
1301 | $siteurl = get_option( 'siteurl' ); |
---|
1302 | |
---|
1303 | $smiley = trim(reset($smiley)); |
---|
1304 | $img = $wpsmiliestrans[$smiley]; |
---|
1305 | $smiley_masked = esc_attr($smiley); |
---|
1306 | |
---|
1307 | return " <img src='$siteurl/wp-includes/images/smilies/$img' alt='$smiley_masked' class='wp-smiley' /> "; |
---|
1308 | } |
---|
1309 | |
---|
1310 | |
---|
1311 | /** |
---|
1312 | * Convert text equivalent of smilies to images. |
---|
1313 | * |
---|
1314 | * Will only convert smilies if the option 'use_smilies' is true and the global |
---|
1315 | * used in the function isn't empty. |
---|
1316 | * |
---|
1317 | * @since 0.71 |
---|
1318 | * @uses $wp_smiliessearch |
---|
1319 | * |
---|
1320 | * @param string $text Content to convert smilies from text. |
---|
1321 | * @return string Converted content with text smilies replaced with images. |
---|
1322 | */ |
---|
1323 | function convert_smilies($text) { |
---|
1324 | global $wp_smiliessearch; |
---|
1325 | $output = ''; |
---|
1326 | if ( get_option('use_smilies') && !empty($wp_smiliessearch) ) { |
---|
1327 | // HTML loop taken from texturize function, could possible be consolidated |
---|
1328 | $textarr = preg_split("/(<.*>)/U", $text, -1, PREG_SPLIT_DELIM_CAPTURE); // capture the tags as well as in between |
---|
1329 | $stop = count($textarr);// loop stuff |
---|
1330 | for ($i = 0; $i < $stop; $i++) { |
---|
1331 | $content = $textarr[$i]; |
---|
1332 | if ((strlen($content) > 0) && ('<' != $content{0})) { // If it's not a tag |
---|
1333 | $content = preg_replace_callback($wp_smiliessearch, 'translate_smiley', $content); |
---|
1334 | } |
---|
1335 | $output .= $content; |
---|
1336 | } |
---|
1337 | } else { |
---|
1338 | // return default text. |
---|
1339 | $output = $text; |
---|
1340 | } |
---|
1341 | return $output; |
---|
1342 | } |
---|
1343 | |
---|
1344 | /** |
---|
1345 | * Verifies that an email is valid. |
---|
1346 | * |
---|
1347 | * Does not grok i18n domains. Not RFC compliant. |
---|
1348 | * |
---|
1349 | * @since 0.71 |
---|
1350 | * |
---|
1351 | * @param string $email Email address to verify. |
---|
1352 | * @param boolean $check_dns Whether to check the DNS for the domain using checkdnsrr(). |
---|
1353 | * @return string|bool Either false or the valid email address. |
---|
1354 | */ |
---|
1355 | function is_email( $email, $check_dns = false ) { |
---|
1356 | // Test for the minimum length the email can be |
---|
1357 | if ( strlen( $email ) < 3 ) { |
---|
1358 | return apply_filters( 'is_email', false, $email, 'email_too_short' ); |
---|
1359 | } |
---|
1360 | |
---|
1361 | // Test for an @ character after the first position |
---|
1362 | if ( strpos( $email, '@', 1 ) === false ) { |
---|
1363 | return apply_filters( 'is_email', false, $email, 'email_no_at' ); |
---|
1364 | } |
---|
1365 | |
---|
1366 | // Split out the local and domain parts |
---|
1367 | list( $local, $domain ) = explode( '@', $email, 2 ); |
---|
1368 | |
---|
1369 | // LOCAL PART |
---|
1370 | // Test for invalid characters |
---|
1371 | if ( !preg_match( '/^[a-zA-Z0-9!#$%&\'*+\/=?^_`{|}~\.-]+$/', $local ) ) { |
---|
1372 | return apply_filters( 'is_email', false, $email, 'local_invalid_chars' ); |
---|
1373 | } |
---|
1374 | |
---|
1375 | // DOMAIN PART |
---|
1376 | // Test for sequences of periods |
---|
1377 | if ( preg_match( '/\.{2,}/', $domain ) ) { |
---|
1378 | return apply_filters( 'is_email', false, $email, 'domain_period_sequence' ); |
---|
1379 | } |
---|
1380 | |
---|
1381 | // Test for leading and trailing periods and whitespace |
---|
1382 | if ( trim( $domain, " \t\n\r\0\x0B." ) !== $domain ) { |
---|
1383 | return apply_filters( 'is_email', false, $email, 'domain_period_limits' ); |
---|
1384 | } |
---|
1385 | |
---|
1386 | // Split the domain into subs |
---|
1387 | $subs = explode( '.', $domain ); |
---|
1388 | |
---|
1389 | // Assume the domain will have at least two subs |
---|
1390 | if ( 2 > count( $subs ) ) { |
---|
1391 | return apply_filters( 'is_email', false, $email, 'domain_no_periods' ); |
---|
1392 | } |
---|
1393 | |
---|
1394 | // Loop through each sub |
---|
1395 | foreach ( $subs as $sub ) { |
---|
1396 | // Test for leading and trailing hyphens and whitespace |
---|
1397 | if ( trim( $sub, " \t\n\r\0\x0B-" ) !== $sub ) { |
---|
1398 | return apply_filters( 'is_email', false, $email, 'sub_hyphen_limits' ); |
---|
1399 | } |
---|
1400 | |
---|
1401 | // Test for invalid characters |
---|
1402 | if ( !preg_match('/^[a-z0-9-]+$/i', $sub ) ) { |
---|
1403 | return apply_filters( 'is_email', false, $email, 'sub_invalid_chars' ); |
---|
1404 | } |
---|
1405 | } |
---|
1406 | |
---|
1407 | // DNS |
---|
1408 | // Check the domain has a valid MX and A resource record |
---|
1409 | if ( $check_dns && function_exists( 'checkdnsrr' ) && !( checkdnsrr( $domain . '.', 'MX' ) || checkdnsrr( $domain . '.', 'A' ) ) ) { |
---|
1410 | return apply_filters( 'is_email', false, $email, 'dns_no_rr' ); |
---|
1411 | } |
---|
1412 | |
---|
1413 | // Congratulations your email made it! |
---|
1414 | return apply_filters( 'is_email', $email, $email, null ); |
---|
1415 | } |
---|
1416 | |
---|
1417 | /** |
---|
1418 | * Convert to ASCII from email subjects. |
---|
1419 | * |
---|
1420 | * @since 1.2.0 |
---|
1421 | * @usedby wp_mail() handles charsets in email subjects |
---|
1422 | * |
---|
1423 | * @param string $string Subject line |
---|
1424 | * @return string Converted string to ASCII |
---|
1425 | */ |
---|
1426 | function wp_iso_descrambler($string) { |
---|
1427 | /* this may only work with iso-8859-1, I'm afraid */ |
---|
1428 | if (!preg_match('#\=\?(.+)\?Q\?(.+)\?\=#i', $string, $matches)) { |
---|
1429 | return $string; |
---|
1430 | } else { |
---|
1431 | $subject = str_replace('_', ' ', $matches[2]); |
---|
1432 | $subject = preg_replace_callback('#\=([0-9a-f]{2})#i', create_function('$match', 'return chr(hexdec(strtolower($match[1])));'), $subject); |
---|
1433 | return $subject; |
---|
1434 | } |
---|
1435 | } |
---|
1436 | |
---|
1437 | /** |
---|
1438 | * Returns a date in the GMT equivalent. |
---|
1439 | * |
---|
1440 | * Requires and returns a date in the Y-m-d H:i:s format. Simply subtracts the |
---|
1441 | * value of the 'gmt_offset' option. |
---|
1442 | * |
---|
1443 | * @since 1.2.0 |
---|
1444 | * |
---|
1445 | * @uses get_option() to retrieve the the value of 'gmt_offset'. |
---|
1446 | * @param string $string The date to be converted. |
---|
1447 | * @return string GMT version of the date provided. |
---|
1448 | */ |
---|
1449 | function get_gmt_from_date($string) { |
---|
1450 | preg_match('#([0-9]{1,4})-([0-9]{1,2})-([0-9]{1,2}) ([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})#', $string, $matches); |
---|
1451 | $string_time = gmmktime($matches[4], $matches[5], $matches[6], $matches[2], $matches[3], $matches[1]); |
---|
1452 | $string_gmt = gmdate('Y-m-d H:i:s', $string_time - get_option('gmt_offset') * 3600); |
---|
1453 | return $string_gmt; |
---|
1454 | } |
---|
1455 | |
---|
1456 | /** |
---|
1457 | * Converts a GMT date into the correct format for the blog. |
---|
1458 | * |
---|
1459 | * Requires and returns in the Y-m-d H:i:s format. Simply adds the value of |
---|
1460 | * gmt_offset. |
---|
1461 | * |
---|
1462 | * @since 1.2.0 |
---|
1463 | * |
---|
1464 | * @param string $string The date to be converted. |
---|
1465 | * @return string Formatted date relative to the GMT offset. |
---|
1466 | */ |
---|
1467 | function get_date_from_gmt($string) { |
---|
1468 | preg_match('#([0-9]{1,4})-([0-9]{1,2})-([0-9]{1,2}) ([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})#', $string, $matches); |
---|
1469 | $string_time = gmmktime($matches[4], $matches[5], $matches[6], $matches[2], $matches[3], $matches[1]); |
---|
1470 | $string_localtime = gmdate('Y-m-d H:i:s', $string_time + get_option('gmt_offset')*3600); |
---|
1471 | return $string_localtime; |
---|
1472 | } |
---|
1473 | |
---|
1474 | /** |
---|
1475 | * Computes an offset in seconds from an iso8601 timezone. |
---|
1476 | * |
---|
1477 | * @since 1.5.0 |
---|
1478 | * |
---|
1479 | * @param string $timezone Either 'Z' for 0 offset or '±hhmm'. |
---|
1480 | * @return int|float The offset in seconds. |
---|
1481 | */ |
---|
1482 | function iso8601_timezone_to_offset($timezone) { |
---|
1483 | // $timezone is either 'Z' or '[+|-]hhmm' |
---|
1484 | if ($timezone == 'Z') { |
---|
1485 | $offset = 0; |
---|
1486 | } else { |
---|
1487 | $sign = (substr($timezone, 0, 1) == '+') ? 1 : -1; |
---|
1488 | $hours = intval(substr($timezone, 1, 2)); |
---|
1489 | $minutes = intval(substr($timezone, 3, 4)) / 60; |
---|
1490 | $offset = $sign * 3600 * ($hours + $minutes); |
---|
1491 | } |
---|
1492 | return $offset; |
---|
1493 | } |
---|
1494 | |
---|
1495 | /** |
---|
1496 | * Converts an iso8601 date to MySQL DateTime format used by post_date[_gmt]. |
---|
1497 | * |
---|
1498 | * @since 1.5.0 |
---|
1499 | * |
---|
1500 | * @param string $date_string Date and time in ISO 8601 format {@link http://en.wikipedia.org/wiki/ISO_8601}. |
---|
1501 | * @param string $timezone Optional. If set to GMT returns the time minus gmt_offset. Default is 'user'. |
---|
1502 | * @return string The date and time in MySQL DateTime format - Y-m-d H:i:s. |
---|
1503 | */ |
---|
1504 | function iso8601_to_datetime($date_string, $timezone = 'user') { |
---|
1505 | $timezone = strtolower($timezone); |
---|
1506 | |
---|
1507 | if ($timezone == 'gmt') { |
---|
1508 | |
---|
1509 | preg_match('#([0-9]{4})([0-9]{2})([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})(Z|[\+|\-][0-9]{2,4}){0,1}#', $date_string, $date_bits); |
---|
1510 | |
---|
1511 | if (!empty($date_bits[7])) { // we have a timezone, so let's compute an offset |
---|
1512 | $offset = iso8601_timezone_to_offset($date_bits[7]); |
---|
1513 | } else { // we don't have a timezone, so we assume user local timezone (not server's!) |
---|
1514 | $offset = 3600 * get_option('gmt_offset'); |
---|
1515 | } |
---|
1516 | |
---|
1517 | $timestamp = gmmktime($date_bits[4], $date_bits[5], $date_bits[6], $date_bits[2], $date_bits[3], $date_bits[1]); |
---|
1518 | $timestamp -= $offset; |
---|
1519 | |
---|
1520 | return gmdate('Y-m-d H:i:s', $timestamp); |
---|
1521 | |
---|
1522 | } else if ($timezone == 'user') { |
---|
1523 | return preg_replace('#([0-9]{4})([0-9]{2})([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})(Z|[\+|\-][0-9]{2,4}){0,1}#', '$1-$2-$3 $4:$5:$6', $date_string); |
---|
1524 | } |
---|
1525 | } |
---|
1526 | |
---|
1527 | /** |
---|
1528 | * Adds a element attributes to open links in new windows. |
---|
1529 | * |
---|
1530 | * Comment text in popup windows should be filtered through this. Right now it's |
---|
1531 | * a moderately dumb function, ideally it would detect whether a target or rel |
---|
1532 | * attribute was already there and adjust its actions accordingly. |
---|
1533 | * |
---|
1534 | * @since 0.71 |
---|
1535 | * |
---|
1536 | * @param string $text Content to replace links to open in a new window. |
---|
1537 | * @return string Content that has filtered links. |
---|
1538 | */ |
---|
1539 | function popuplinks($text) { |
---|
1540 | $text = preg_replace('/<a (.+?)>/i', "<a $1 target='_blank' rel='external'>", $text); |
---|
1541 | return $text; |
---|
1542 | } |
---|
1543 | |
---|
1544 | /** |
---|
1545 | * Strips out all characters that are not allowable in an email. |
---|
1546 | * |
---|
1547 | * @since 1.5.0 |
---|
1548 | * |
---|
1549 | * @param string $email Email address to filter. |
---|
1550 | * @return string Filtered email address. |
---|
1551 | */ |
---|
1552 | function sanitize_email( $email ) { |
---|
1553 | // Test for the minimum length the email can be |
---|
1554 | if ( strlen( $email ) < 3 ) { |
---|
1555 | return apply_filters( 'sanitize_email', '', $email, 'email_too_short' ); |
---|
1556 | } |
---|
1557 | |
---|
1558 | // Test for an @ character after the first position |
---|
1559 | if ( strpos( $email, '@', 1 ) === false ) { |
---|
1560 | return apply_filters( 'sanitize_email', '', $email, 'email_no_at' ); |
---|
1561 | } |
---|
1562 | |
---|
1563 | // Split out the local and domain parts |
---|
1564 | list( $local, $domain ) = explode( '@', $email, 2 ); |
---|
1565 | |
---|
1566 | // LOCAL PART |
---|
1567 | // Test for invalid characters |
---|
1568 | $local = preg_replace( '/[^a-zA-Z0-9!#$%&\'*+\/=?^_`{|}~\.-]/', '', $local ); |
---|
1569 | if ( '' === $local ) { |
---|
1570 | return apply_filters( 'sanitize_email', '', $email, 'local_invalid_chars' ); |
---|
1571 | } |
---|
1572 | |
---|
1573 | // DOMAIN PART |
---|
1574 | // Test for sequences of periods |
---|
1575 | $domain = preg_replace( '/\.{2,}/', '', $domain ); |
---|
1576 | if ( '' === $domain ) { |
---|
1577 | return apply_filters( 'sanitize_email', '', $email, 'domain_period_sequence' ); |
---|
1578 | } |
---|
1579 | |
---|
1580 | // Test for leading and trailing periods and whitespace |
---|
1581 | $domain = trim( $domain, " \t\n\r\0\x0B." ); |
---|
1582 | if ( '' === $domain ) { |
---|
1583 | return apply_filters( 'sanitize_email', '', $email, 'domain_period_limits' ); |
---|
1584 | } |
---|
1585 | |
---|
1586 | // Split the domain into subs |
---|
1587 | $subs = explode( '.', $domain ); |
---|
1588 | |
---|
1589 | // Assume the domain will have at least two subs |
---|
1590 | if ( 2 > count( $subs ) ) { |
---|
1591 | return apply_filters( 'sanitize_email', '', $email, 'domain_no_periods' ); |
---|
1592 | } |
---|
1593 | |
---|
1594 | // Create an array that will contain valid subs |
---|
1595 | $new_subs = array(); |
---|
1596 | |
---|
1597 | // Loop through each sub |
---|
1598 | foreach ( $subs as $sub ) { |
---|
1599 | // Test for leading and trailing hyphens |
---|
1600 | $sub = trim( $sub, " \t\n\r\0\x0B-" ); |
---|
1601 | |
---|
1602 | // Test for invalid characters |
---|
1603 | $sub = preg_replace( '/^[^a-z0-9-]+$/i', '', $sub ); |
---|
1604 | |
---|
1605 | // If there's anything left, add it to the valid subs |
---|
1606 | if ( '' !== $sub ) { |
---|
1607 | $new_subs[] = $sub; |
---|
1608 | } |
---|
1609 | } |
---|
1610 | |
---|
1611 | // If there aren't 2 or more valid subs |
---|
1612 | if ( 2 > count( $new_subs ) ) { |
---|
1613 | return apply_filters( 'sanitize_email', '', $email, 'domain_no_valid_subs' ); |
---|
1614 | } |
---|
1615 | |
---|
1616 | // Join valid subs into the new domain |
---|
1617 | $domain = join( '.', $new_subs ); |
---|
1618 | |
---|
1619 | // Put the email back together |
---|
1620 | $email = $local . '@' . $domain; |
---|
1621 | |
---|
1622 | // Congratulations your email made it! |
---|
1623 | return apply_filters( 'sanitize_email', $email, $email, null ); |
---|
1624 | } |
---|
1625 | |
---|
1626 | /** |
---|
1627 | * Determines the difference between two timestamps. |
---|
1628 | * |
---|
1629 | * The difference is returned in a human readable format such as "1 hour", |
---|
1630 | * "5 mins", "2 days". |
---|
1631 | * |
---|
1632 | * @since 1.5.0 |
---|
1633 | * |
---|
1634 | * @param int $from Unix timestamp from which the difference begins. |
---|
1635 | * @param int $to Optional. Unix timestamp to end the time difference. Default becomes time() if not set. |
---|
1636 | * @return string Human readable time difference. |
---|
1637 | */ |
---|
1638 | function human_time_diff( $from, $to = '' ) { |
---|
1639 | if ( empty($to) ) |
---|
1640 | $to = time(); |
---|
1641 | $diff = (int) abs($to - $from); |
---|
1642 | if ($diff <= 3600) { |
---|
1643 | $mins = round($diff / 60); |
---|
1644 | if ($mins <= 1) { |
---|
1645 | $mins = 1; |
---|
1646 | } |
---|
1647 | $since = sprintf(_n('%s min', '%s mins', $mins), $mins); |
---|
1648 | } else if (($diff <= 86400) && ($diff > 3600)) { |
---|
1649 | $hours = round($diff / 3600); |
---|
1650 | if ($hours <= 1) { |
---|
1651 | $hours = 1; |
---|
1652 | } |
---|
1653 | $since = sprintf(_n('%s hour', '%s hours', $hours), $hours); |
---|
1654 | } elseif ($diff >= 86400) { |
---|
1655 | $days = round($diff / 86400); |
---|
1656 | if ($days <= 1) { |
---|
1657 | $days = 1; |
---|
1658 | } |
---|
1659 | $since = sprintf(_n('%s day', '%s days', $days), $days); |
---|
1660 | } |
---|
1661 | return $since; |
---|
1662 | } |
---|
1663 | |
---|
1664 | /** |
---|
1665 | * Generates an excerpt from the content, if needed. |
---|
1666 | * |
---|
1667 | * The excerpt word amount will be 55 words and if the amount is greater than |
---|
1668 | * that, then the string '[...]' will be appended to the excerpt. If the string |
---|
1669 | * is less than 55 words, then the content will be returned as is. |
---|
1670 | * |
---|
1671 | * @since 1.5.0 |
---|
1672 | * |
---|
1673 | * @param string $text The exerpt. If set to empty an excerpt is generated. |
---|
1674 | * @return string The excerpt. |
---|
1675 | */ |
---|
1676 | function wp_trim_excerpt($text) { |
---|
1677 | $raw_excerpt = $text; |
---|
1678 | if ( '' == $text ) { |
---|
1679 | $text = get_the_content(''); |
---|
1680 | |
---|
1681 | $text = strip_shortcodes( $text ); |
---|
1682 | |
---|
1683 | $text = apply_filters('the_content', $text); |
---|
1684 | $text = str_replace(']]>', ']]>', $text); |
---|
1685 | $text = strip_tags($text); |
---|
1686 | $excerpt_length = apply_filters('excerpt_length', 55); |
---|
1687 | $words = explode(' ', $text, $excerpt_length + 1); |
---|
1688 | if (count($words) > $excerpt_length) { |
---|
1689 | array_pop($words); |
---|
1690 | array_push($words, '[...]'); |
---|
1691 | $text = implode(' ', $words); |
---|
1692 | } |
---|
1693 | } |
---|
1694 | return apply_filters('wp_trim_excerpt', $text, $raw_excerpt); |
---|
1695 | } |
---|
1696 | |
---|
1697 | /** |
---|
1698 | * Converts named entities into numbered entities. |
---|
1699 | * |
---|
1700 | * @since 1.5.1 |
---|
1701 | * |
---|
1702 | * @param string $text The text within which entities will be converted. |
---|
1703 | * @return string Text with converted entities. |
---|
1704 | */ |
---|
1705 | function ent2ncr($text) { |
---|
1706 | $to_ncr = array( |
---|
1707 | '"' => '"', |
---|
1708 | '&' => '&', |
---|
1709 | '⁄' => '/', |
---|
1710 | '<' => '<', |
---|
1711 | '>' => '>', |
---|
1712 | '|' => '|', |
---|
1713 | ' ' => ' ', |
---|
1714 | '¡' => '¡', |
---|
1715 | '¢' => '¢', |
---|
1716 | '£' => '£', |
---|
1717 | '¤' => '¤', |
---|
1718 | '¥' => '¥', |
---|
1719 | '¦' => '¦', |
---|
1720 | '&brkbar;' => '¦', |
---|
1721 | '§' => '§', |
---|
1722 | '¨' => '¨', |
---|
1723 | '¨' => '¨', |
---|
1724 | '©' => '©', |
---|
1725 | 'ª' => 'ª', |
---|
1726 | '«' => '«', |
---|
1727 | '¬' => '¬', |
---|
1728 | '­' => '­', |
---|
1729 | '®' => '®', |
---|
1730 | '¯' => '¯', |
---|
1731 | '&hibar;' => '¯', |
---|
1732 | '°' => '°', |
---|
1733 | '±' => '±', |
---|
1734 | '²' => '²', |
---|
1735 | '³' => '³', |
---|
1736 | '´' => '´', |
---|
1737 | 'µ' => 'µ', |
---|
1738 | '¶' => '¶', |
---|
1739 | '·' => '·', |
---|
1740 | '¸' => '¸', |
---|
1741 | '¹' => '¹', |
---|
1742 | 'º' => 'º', |
---|
1743 | '»' => '»', |
---|
1744 | '¼' => '¼', |
---|
1745 | '½' => '½', |
---|
1746 | '¾' => '¾', |
---|
1747 | '¿' => '¿', |
---|
1748 | 'À' => 'À', |
---|
1749 | 'Á' => 'Á', |
---|
1750 | 'Â' => 'Â', |
---|
1751 | 'Ã' => 'Ã', |
---|
1752 | 'Ä' => 'Ä', |
---|
1753 | 'Å' => 'Å', |
---|
1754 | 'Æ' => 'Æ', |
---|
1755 | 'Ç' => 'Ç', |
---|
1756 | 'È' => 'È', |
---|
1757 | 'É' => 'É', |
---|
1758 | 'Ê' => 'Ê', |
---|
1759 | 'Ë' => 'Ë', |
---|
1760 | 'Ì' => 'Ì', |
---|
1761 | 'Í' => 'Í', |
---|
1762 | 'Î' => 'Î', |
---|
1763 | 'Ï' => 'Ï', |
---|
1764 | 'Ð' => 'Ð', |
---|
1765 | 'Ñ' => 'Ñ', |
---|
1766 | 'Ò' => 'Ò', |
---|
1767 | 'Ó' => 'Ó', |
---|
1768 | 'Ô' => 'Ô', |
---|
1769 | 'Õ' => 'Õ', |
---|
1770 | 'Ö' => 'Ö', |
---|
1771 | '×' => '×', |
---|
1772 | 'Ø' => 'Ø', |
---|
1773 | 'Ù' => 'Ù', |
---|
1774 | 'Ú' => 'Ú', |
---|
1775 | 'Û' => 'Û', |
---|
1776 | 'Ü' => 'Ü', |
---|
1777 | 'Ý' => 'Ý', |
---|
1778 | 'Þ' => 'Þ', |
---|
1779 | 'ß' => 'ß', |
---|
1780 | 'à' => 'à', |
---|
1781 | 'á' => 'á', |
---|
1782 | 'â' => 'â', |
---|
1783 | 'ã' => 'ã', |
---|
1784 | 'ä' => 'ä', |
---|
1785 | 'å' => 'å', |
---|
1786 | 'æ' => 'æ', |
---|
1787 | 'ç' => 'ç', |
---|
1788 | 'è' => 'è', |
---|
1789 | 'é' => 'é', |
---|
1790 | 'ê' => 'ê', |
---|
1791 | 'ë' => 'ë', |
---|
1792 | 'ì' => 'ì', |
---|
1793 | 'í' => 'í', |
---|
1794 | 'î' => 'î', |
---|
1795 | 'ï' => 'ï', |
---|
1796 | 'ð' => 'ð', |
---|
1797 | 'ñ' => 'ñ', |
---|
1798 | 'ò' => 'ò', |
---|
1799 | 'ó' => 'ó', |
---|
1800 | 'ô' => 'ô', |
---|
1801 | 'õ' => 'õ', |
---|
1802 | 'ö' => 'ö', |
---|
1803 | '÷' => '÷', |
---|
1804 | 'ø' => 'ø', |
---|
1805 | 'ù' => 'ù', |
---|
1806 | 'ú' => 'ú', |
---|
1807 | 'û' => 'û', |
---|
1808 | 'ü' => 'ü', |
---|
1809 | 'ý' => 'ý', |
---|
1810 | 'þ' => 'þ', |
---|
1811 | 'ÿ' => 'ÿ', |
---|
1812 | 'Œ' => 'Œ', |
---|
1813 | 'œ' => 'œ', |
---|
1814 | 'Š' => 'Š', |
---|
1815 | 'š' => 'š', |
---|
1816 | 'Ÿ' => 'Ÿ', |
---|
1817 | 'ƒ' => 'ƒ', |
---|
1818 | 'ˆ' => 'ˆ', |
---|
1819 | '˜' => '˜', |
---|
1820 | 'Α' => 'Α', |
---|
1821 | 'Β' => 'Β', |
---|
1822 | 'Γ' => 'Γ', |
---|
1823 | 'Δ' => 'Δ', |
---|
1824 | 'Ε' => 'Ε', |
---|
1825 | 'Ζ' => 'Ζ', |
---|
1826 | 'Η' => 'Η', |
---|
1827 | 'Θ' => 'Θ', |
---|
1828 | 'Ι' => 'Ι', |
---|
1829 | 'Κ' => 'Κ', |
---|
1830 | 'Λ' => 'Λ', |
---|
1831 | 'Μ' => 'Μ', |
---|
1832 | 'Ν' => 'Ν', |
---|
1833 | 'Ξ' => 'Ξ', |
---|
1834 | 'Ο' => 'Ο', |
---|
1835 | 'Π' => 'Π', |
---|
1836 | 'Ρ' => 'Ρ', |
---|
1837 | 'Σ' => 'Σ', |
---|
1838 | 'Τ' => 'Τ', |
---|
1839 | 'Υ' => 'Υ', |
---|
1840 | 'Φ' => 'Φ', |
---|
1841 | 'Χ' => 'Χ', |
---|
1842 | 'Ψ' => 'Ψ', |
---|
1843 | 'Ω' => 'Ω', |
---|
1844 | 'α' => 'α', |
---|
1845 | 'β' => 'β', |
---|
1846 | 'γ' => 'γ', |
---|
1847 | 'δ' => 'δ', |
---|
1848 | 'ε' => 'ε', |
---|
1849 | 'ζ' => 'ζ', |
---|
1850 | 'η' => 'η', |
---|
1851 | 'θ' => 'θ', |
---|
1852 | 'ι' => 'ι', |
---|
1853 | 'κ' => 'κ', |
---|
1854 | 'λ' => 'λ', |
---|
1855 | 'μ' => 'μ', |
---|
1856 | 'ν' => 'ν', |
---|
1857 | 'ξ' => 'ξ', |
---|
1858 | 'ο' => 'ο', |
---|
1859 | 'π' => 'π', |
---|
1860 | 'ρ' => 'ρ', |
---|
1861 | 'ς' => 'ς', |
---|
1862 | 'σ' => 'σ', |
---|
1863 | 'τ' => 'τ', |
---|
1864 | 'υ' => 'υ', |
---|
1865 | 'φ' => 'φ', |
---|
1866 | 'χ' => 'χ', |
---|
1867 | 'ψ' => 'ψ', |
---|
1868 | 'ω' => 'ω', |
---|
1869 | 'ϑ' => 'ϑ', |
---|
1870 | 'ϒ' => 'ϒ', |
---|
1871 | 'ϖ' => 'ϖ', |
---|
1872 | ' ' => ' ', |
---|
1873 | ' ' => ' ', |
---|
1874 | ' ' => ' ', |
---|
1875 | '‌' => '‌', |
---|
1876 | '‍' => '‍', |
---|
1877 | '‎' => '‎', |
---|
1878 | '‏' => '‏', |
---|
1879 | '–' => '–', |
---|
1880 | '—' => '—', |
---|
1881 | '‘' => '‘', |
---|
1882 | '’' => '’', |
---|
1883 | '‚' => '‚', |
---|
1884 | '“' => '“', |
---|
1885 | '”' => '”', |
---|
1886 | '„' => '„', |
---|
1887 | '†' => '†', |
---|
1888 | '‡' => '‡', |
---|
1889 | '•' => '•', |
---|
1890 | '…' => '…', |
---|
1891 | '‰' => '‰', |
---|
1892 | '′' => '′', |
---|
1893 | '″' => '″', |
---|
1894 | '‹' => '‹', |
---|
1895 | '›' => '›', |
---|
1896 | '‾' => '‾', |
---|
1897 | '⁄' => '⁄', |
---|
1898 | '€' => '€', |
---|
1899 | 'ℑ' => 'ℑ', |
---|
1900 | '℘' => '℘', |
---|
1901 | 'ℜ' => 'ℜ', |
---|
1902 | '™' => '™', |
---|
1903 | 'ℵ' => 'ℵ', |
---|
1904 | '↵' => '↵', |
---|
1905 | '⇐' => '⇐', |
---|
1906 | '⇑' => '⇑', |
---|
1907 | '⇒' => '⇒', |
---|
1908 | '⇓' => '⇓', |
---|
1909 | '⇔' => '⇔', |
---|
1910 | '∀' => '∀', |
---|
1911 | '∂' => '∂', |
---|
1912 | '∃' => '∃', |
---|
1913 | '∅' => '∅', |
---|
1914 | '∇' => '∇', |
---|
1915 | '∈' => '∈', |
---|
1916 | '∉' => '∉', |
---|
1917 | '∋' => '∋', |
---|
1918 | '∏' => '∏', |
---|
1919 | '∑' => '∑', |
---|
1920 | '−' => '−', |
---|
1921 | '∗' => '∗', |
---|
1922 | '√' => '√', |
---|
1923 | '∝' => '∝', |
---|
1924 | '∞' => '∞', |
---|
1925 | '∠' => '∠', |
---|
1926 | '∧' => '∧', |
---|
1927 | '∨' => '∨', |
---|
1928 | '∩' => '∩', |
---|
1929 | '∪' => '∪', |
---|
1930 | '∫' => '∫', |
---|
1931 | '∴' => '∴', |
---|
1932 | '∼' => '∼', |
---|
1933 | '≅' => '≅', |
---|
1934 | '≈' => '≈', |
---|
1935 | '≠' => '≠', |
---|
1936 | '≡' => '≡', |
---|
1937 | '≤' => '≤', |
---|
1938 | '≥' => '≥', |
---|
1939 | '⊂' => '⊂', |
---|
1940 | '⊃' => '⊃', |
---|
1941 | '⊄' => '⊄', |
---|
1942 | '⊆' => '⊆', |
---|
1943 | '⊇' => '⊇', |
---|
1944 | '⊕' => '⊕', |
---|
1945 | '⊗' => '⊗', |
---|
1946 | '⊥' => '⊥', |
---|
1947 | '⋅' => '⋅', |
---|
1948 | '⌈' => '⌈', |
---|
1949 | '⌉' => '⌉', |
---|
1950 | '⌊' => '⌊', |
---|
1951 | '⌋' => '⌋', |
---|
1952 | '⟨' => '〈', |
---|
1953 | '⟩' => '〉', |
---|
1954 | '←' => '←', |
---|
1955 | '↑' => '↑', |
---|
1956 | '→' => '→', |
---|
1957 | '↓' => '↓', |
---|
1958 | '↔' => '↔', |
---|
1959 | '◊' => '◊', |
---|
1960 | '♠' => '♠', |
---|
1961 | '♣' => '♣', |
---|
1962 | '♥' => '♥', |
---|
1963 | '♦' => '♦' |
---|
1964 | ); |
---|
1965 | |
---|
1966 | return str_replace( array_keys($to_ncr), array_values($to_ncr), $text ); |
---|
1967 | } |
---|
1968 | |
---|
1969 | /** |
---|
1970 | * Formats text for the rich text editor. |
---|
1971 | * |
---|
1972 | * The filter 'richedit_pre' is applied here. If $text is empty the filter will |
---|
1973 | * be applied to an empty string. |
---|
1974 | * |
---|
1975 | * @since 2.0.0 |
---|
1976 | * |
---|
1977 | * @param string $text The text to be formatted. |
---|
1978 | * @return string The formatted text after filter is applied. |
---|
1979 | */ |
---|
1980 | function wp_richedit_pre($text) { |
---|
1981 | // Filtering a blank results in an annoying <br />\n |
---|
1982 | if ( empty($text) ) return apply_filters('richedit_pre', ''); |
---|
1983 | |
---|
1984 | $output = convert_chars($text); |
---|
1985 | $output = wpautop($output); |
---|
1986 | $output = htmlspecialchars($output, ENT_NOQUOTES); |
---|
1987 | |
---|
1988 | return apply_filters('richedit_pre', $output); |
---|
1989 | } |
---|
1990 | |
---|
1991 | /** |
---|
1992 | * Formats text for the HTML editor. |
---|
1993 | * |
---|
1994 | * Unless $output is empty it will pass through htmlspecialchars before the |
---|
1995 | * 'htmledit_pre' filter is applied. |
---|
1996 | * |
---|
1997 | * @since 2.5.0 |
---|
1998 | * |
---|
1999 | * @param string $output The text to be formatted. |
---|
2000 | * @return string Formatted text after filter applied. |
---|
2001 | */ |
---|
2002 | function wp_htmledit_pre($output) { |
---|
2003 | if ( !empty($output) ) |
---|
2004 | $output = htmlspecialchars($output, ENT_NOQUOTES); // convert only < > & |
---|
2005 | |
---|
2006 | return apply_filters('htmledit_pre', $output); |
---|
2007 | } |
---|
2008 | |
---|
2009 | /** |
---|
2010 | * Checks and cleans a URL. |
---|
2011 | * |
---|
2012 | * A number of characters are removed from the URL. If the URL is for displaying |
---|
2013 | * (the default behaviour) amperstands are also replaced. The 'esc_url' filter |
---|
2014 | * is applied to the returned cleaned URL. |
---|
2015 | * |
---|
2016 | * @since 1.2.0 |
---|
2017 | * @uses wp_kses_bad_protocol() To only permit protocols in the URL set |
---|
2018 | * via $protocols or the common ones set in the function. |
---|
2019 | * |
---|
2020 | * @param string $url The URL to be cleaned. |
---|
2021 | * @param array $protocols Optional. An array of acceptable protocols. |
---|
2022 | * Defaults to 'http', 'https', 'ftp', 'ftps', 'mailto', 'news', 'irc', 'gopher', 'nntp', 'feed', 'telnet' if not set. |
---|
2023 | * @param string $context Optional. How the URL will be used. Default is 'display'. |
---|
2024 | * @return string The cleaned $url after the 'cleaned_url' filter is applied. |
---|
2025 | */ |
---|
2026 | function clean_url( $url, $protocols = null, $context = 'display' ) { |
---|
2027 | $original_url = $url; |
---|
2028 | |
---|
2029 | if ('' == $url) return $url; |
---|
2030 | $url = preg_replace('|[^a-z0-9-~+_.?#=!&;,/:%@$\|*\'()\\x80-\\xff]|i', '', $url); |
---|
2031 | $strip = array('%0d', '%0a'); |
---|
2032 | $url = str_replace($strip, '', $url); |
---|
2033 | $url = str_replace(';//', '://', $url); |
---|
2034 | /* If the URL doesn't appear to contain a scheme, we |
---|
2035 | * presume it needs http:// appended (unless a relative |
---|
2036 | * link starting with / or a php file). |
---|
2037 | */ |
---|
2038 | if ( strpos($url, ':') === false && |
---|
2039 | substr( $url, 0, 1 ) != '/' && substr( $url, 0, 1 ) != '#' && !preg_match('/^[a-z0-9-]+?\.php/i', $url) ) |
---|
2040 | $url = 'http://' . $url; |
---|
2041 | |
---|
2042 | // Replace ampersands and single quotes only when displaying. |
---|
2043 | if ( 'display' == $context ) { |
---|
2044 | $url = preg_replace('/&([^#])(?![a-z]{2,8};)/', '&$1', $url); |
---|
2045 | $url = str_replace( "'", ''', $url ); |
---|
2046 | } |
---|
2047 | |
---|
2048 | if ( !is_array($protocols) ) |
---|
2049 | $protocols = array('http', 'https', 'ftp', 'ftps', 'mailto', 'news', 'irc', 'gopher', 'nntp', 'feed', 'telnet'); |
---|
2050 | if ( wp_kses_bad_protocol( $url, $protocols ) != $url ) |
---|
2051 | return ''; |
---|
2052 | |
---|
2053 | return apply_filters('clean_url', $url, $original_url, $context); |
---|
2054 | } |
---|
2055 | |
---|
2056 | /** |
---|
2057 | * Escapes data for use in a MySQL query |
---|
2058 | * |
---|
2059 | * This is just a handy shortcut for $wpdb->escape(), for completeness' sake |
---|
2060 | * |
---|
2061 | * @since 2.8.0 |
---|
2062 | * @param string $sql Unescaped SQL data |
---|
2063 | * @return string The cleaned $sql |
---|
2064 | */ |
---|
2065 | function esc_sql( $sql ) { |
---|
2066 | global $wpdb; |
---|
2067 | return $wpdb->escape( $sql ); |
---|
2068 | } |
---|
2069 | |
---|
2070 | |
---|
2071 | /** |
---|
2072 | * Checks and cleans a URL. |
---|
2073 | * |
---|
2074 | * A number of characters are removed from the URL. If the URL is for displaying |
---|
2075 | * (the default behaviour) amperstands are also replaced. The 'esc_url' filter |
---|
2076 | * is applied to the returned cleaned URL. |
---|
2077 | * |
---|
2078 | * @since 2.8.0 |
---|
2079 | * @uses esc_url() |
---|
2080 | * @uses wp_kses_bad_protocol() To only permit protocols in the URL set |
---|
2081 | * via $protocols or the common ones set in the function. |
---|
2082 | * |
---|
2083 | * @param string $url The URL to be cleaned. |
---|
2084 | * @param array $protocols Optional. An array of acceptable protocols. |
---|
2085 | * Defaults to 'http', 'https', 'ftp', 'ftps', 'mailto', 'news', 'irc', 'gopher', 'nntp', 'feed', 'telnet' if not set. |
---|
2086 | * @return string The cleaned $url after the 'cleaned_url' filter is applied. |
---|
2087 | */ |
---|
2088 | function esc_url( $url, $protocols = null ) { |
---|
2089 | return clean_url( $url, $protocols, 'display' ); |
---|
2090 | } |
---|
2091 | |
---|
2092 | /** |
---|
2093 | * Performs esc_url() for database usage. |
---|
2094 | * |
---|
2095 | * @see esc_url() |
---|
2096 | * @see esc_url() |
---|
2097 | * |
---|
2098 | * @since 2.8.0 |
---|
2099 | * |
---|
2100 | * @param string $url The URL to be cleaned. |
---|
2101 | * @param array $protocols An array of acceptable protocols. |
---|
2102 | * @return string The cleaned URL. |
---|
2103 | */ |
---|
2104 | function esc_url_raw( $url, $protocols = null ) { |
---|
2105 | return clean_url( $url, $protocols, 'db' ); |
---|
2106 | } |
---|
2107 | |
---|
2108 | /** |
---|
2109 | * Performs esc_url() for database or redirect usage. |
---|
2110 | * |
---|
2111 | * @see esc_url() |
---|
2112 | * @deprecated 2.8.0 |
---|
2113 | * |
---|
2114 | * @since 2.3.1 |
---|
2115 | * |
---|
2116 | * @param string $url The URL to be cleaned. |
---|
2117 | * @param array $protocols An array of acceptable protocols. |
---|
2118 | * @return string The cleaned URL. |
---|
2119 | */ |
---|
2120 | function sanitize_url( $url, $protocols = null ) { |
---|
2121 | return clean_url( $url, $protocols, 'db' ); |
---|
2122 | } |
---|
2123 | |
---|
2124 | /** |
---|
2125 | * Convert entities, while preserving already-encoded entities. |
---|
2126 | * |
---|
2127 | * @link http://www.php.net/htmlentities Borrowed from the PHP Manual user notes. |
---|
2128 | * |
---|
2129 | * @since 1.2.2 |
---|
2130 | * |
---|
2131 | * @param string $myHTML The text to be converted. |
---|
2132 | * @return string Converted text. |
---|
2133 | */ |
---|
2134 | function htmlentities2($myHTML) { |
---|
2135 | $translation_table = get_html_translation_table( HTML_ENTITIES, ENT_QUOTES ); |
---|
2136 | $translation_table[chr(38)] = '&'; |
---|
2137 | return preg_replace( "/&(?![A-Za-z]{0,4}\w{2,3};|#[0-9]{2,3};)/", "&", strtr($myHTML, $translation_table) ); |
---|
2138 | } |
---|
2139 | |
---|
2140 | /** |
---|
2141 | * Escape single quotes, specialchar double quotes, and fix line endings. |
---|
2142 | * |
---|
2143 | * The filter 'js_escape' is also applied here. |
---|
2144 | * |
---|
2145 | * @since 2.8.0 |
---|
2146 | * |
---|
2147 | * @param string $text The text to be escaped. |
---|
2148 | * @return string Escaped text. |
---|
2149 | */ |
---|
2150 | function esc_js( $text ) { |
---|
2151 | $safe_text = wp_check_invalid_utf8( $text ); |
---|
2152 | $safe_text = _wp_specialchars( $safe_text, ENT_COMPAT ); |
---|
2153 | $safe_text = preg_replace( '/&#(x)?0*(?(1)27|39);?/i', "'", stripslashes( $safe_text ) ); |
---|
2154 | $safe_text = preg_replace( "/\r?\n/", "\\n", addslashes( $safe_text ) ); |
---|
2155 | return apply_filters( 'js_escape', $safe_text, $text ); |
---|
2156 | } |
---|
2157 | |
---|
2158 | /** |
---|
2159 | * Escape single quotes, specialchar double quotes, and fix line endings. |
---|
2160 | * |
---|
2161 | * The filter 'js_escape' is also applied by esc_js() |
---|
2162 | * |
---|
2163 | * @since 2.0.4 |
---|
2164 | * |
---|
2165 | * @deprecated 2.8.0 |
---|
2166 | * @see esc_js() |
---|
2167 | * |
---|
2168 | * @param string $text The text to be escaped. |
---|
2169 | * @return string Escaped text. |
---|
2170 | */ |
---|
2171 | function js_escape( $text ) { |
---|
2172 | return esc_js( $text ); |
---|
2173 | } |
---|
2174 | |
---|
2175 | /** |
---|
2176 | * Escaping for HTML blocks. |
---|
2177 | * |
---|
2178 | * @since 2.8.0 |
---|
2179 | * |
---|
2180 | * @param string $text |
---|
2181 | * @return string |
---|
2182 | */ |
---|
2183 | function esc_html( $text ) { |
---|
2184 | $safe_text = wp_check_invalid_utf8( $text ); |
---|
2185 | $safe_text = _wp_specialchars( $safe_text, ENT_QUOTES ); |
---|
2186 | return apply_filters( 'esc_html', $safe_text, $text ); |
---|
2187 | return $text; |
---|
2188 | } |
---|
2189 | |
---|
2190 | /** |
---|
2191 | * Escaping for HTML blocks |
---|
2192 | * @deprecated 2.8.0 |
---|
2193 | * @see esc_html() |
---|
2194 | */ |
---|
2195 | function wp_specialchars( $string, $quote_style = ENT_NOQUOTES, $charset = false, $double_encode = false ) { |
---|
2196 | if ( func_num_args() > 1 ) { // Maintain backwards compat for people passing additional args |
---|
2197 | $args = func_get_args(); |
---|
2198 | return call_user_func_array( '_wp_specialchars', $args ); |
---|
2199 | } else { |
---|
2200 | return esc_html( $string ); |
---|
2201 | } |
---|
2202 | } |
---|
2203 | |
---|
2204 | /** |
---|
2205 | * Escaping for HTML attributes. |
---|
2206 | * |
---|
2207 | * @since 2.8.0 |
---|
2208 | * |
---|
2209 | * @param string $text |
---|
2210 | * @return string |
---|
2211 | */ |
---|
2212 | function esc_attr( $text ) { |
---|
2213 | $safe_text = wp_check_invalid_utf8( $text ); |
---|
2214 | $safe_text = _wp_specialchars( $safe_text, ENT_QUOTES ); |
---|
2215 | return apply_filters( 'attribute_escape', $safe_text, $text ); |
---|
2216 | } |
---|
2217 | |
---|
2218 | /** |
---|
2219 | * Escaping for HTML attributes. |
---|
2220 | * |
---|
2221 | * @since 2.0.6 |
---|
2222 | * |
---|
2223 | * @deprecated 2.8.0 |
---|
2224 | * @see esc_attr() |
---|
2225 | * |
---|
2226 | * @param string $text |
---|
2227 | * @return string |
---|
2228 | */ |
---|
2229 | function attribute_escape( $text ) { |
---|
2230 | return esc_attr( $text ); |
---|
2231 | } |
---|
2232 | |
---|
2233 | /** |
---|
2234 | * Escape a HTML tag name. |
---|
2235 | * |
---|
2236 | * @since 2.5.0 |
---|
2237 | * |
---|
2238 | * @param string $tag_name |
---|
2239 | * @return string |
---|
2240 | */ |
---|
2241 | function tag_escape($tag_name) { |
---|
2242 | $safe_tag = strtolower( preg_replace('/[^a-zA-Z_:]/', '', $tag_name) ); |
---|
2243 | return apply_filters('tag_escape', $safe_tag, $tag_name); |
---|
2244 | } |
---|
2245 | |
---|
2246 | /** |
---|
2247 | * Escapes text for SQL LIKE special characters % and _. |
---|
2248 | * |
---|
2249 | * @since 2.5.0 |
---|
2250 | * |
---|
2251 | * @param string $text The text to be escaped. |
---|
2252 | * @return string text, safe for inclusion in LIKE query. |
---|
2253 | */ |
---|
2254 | function like_escape($text) { |
---|
2255 | return str_replace(array("%", "_"), array("\\%", "\\_"), $text); |
---|
2256 | } |
---|
2257 | |
---|
2258 | /** |
---|
2259 | * Convert full URL paths to absolute paths. |
---|
2260 | * |
---|
2261 | * Removes the http or https protocols and the domain. Keeps the path '/' at the |
---|
2262 | * beginning, so it isn't a true relative link, but from the web root base. |
---|
2263 | * |
---|
2264 | * @since 2.1.0 |
---|
2265 | * |
---|
2266 | * @param string $link Full URL path. |
---|
2267 | * @return string Absolute path. |
---|
2268 | */ |
---|
2269 | function wp_make_link_relative( $link ) { |
---|
2270 | return preg_replace( '|https?://[^/]+(/.*)|i', '$1', $link ); |
---|
2271 | } |
---|
2272 | |
---|
2273 | /** |
---|
2274 | * Sanitises various option values based on the nature of the option. |
---|
2275 | * |
---|
2276 | * This is basically a switch statement which will pass $value through a number |
---|
2277 | * of functions depending on the $option. |
---|
2278 | * |
---|
2279 | * @since 2.0.5 |
---|
2280 | * |
---|
2281 | * @param string $option The name of the option. |
---|
2282 | * @param string $value The unsanitised value. |
---|
2283 | * @return string Sanitized value. |
---|
2284 | */ |
---|
2285 | function sanitize_option($option, $value) { |
---|
2286 | |
---|
2287 | switch ($option) { |
---|
2288 | case 'admin_email': |
---|
2289 | $value = sanitize_email($value); |
---|
2290 | break; |
---|
2291 | |
---|
2292 | case 'thumbnail_size_w': |
---|
2293 | case 'thumbnail_size_h': |
---|
2294 | case 'medium_size_w': |
---|
2295 | case 'medium_size_h': |
---|
2296 | case 'large_size_w': |
---|
2297 | case 'large_size_h': |
---|
2298 | case 'default_post_edit_rows': |
---|
2299 | case 'mailserver_port': |
---|
2300 | case 'comment_max_links': |
---|
2301 | case 'page_on_front': |
---|
2302 | case 'rss_excerpt_length': |
---|
2303 | case 'default_category': |
---|
2304 | case 'default_email_category': |
---|
2305 | case 'default_link_category': |
---|
2306 | case 'close_comments_days_old': |
---|
2307 | case 'comments_per_page': |
---|
2308 | case 'thread_comments_depth': |
---|
2309 | $value = abs((int) $value); |
---|
2310 | break; |
---|
2311 | |
---|
2312 | case 'posts_per_page': |
---|
2313 | case 'posts_per_rss': |
---|
2314 | $value = (int) $value; |
---|
2315 | if ( empty($value) ) $value = 1; |
---|
2316 | if ( $value < -1 ) $value = abs($value); |
---|
2317 | break; |
---|
2318 | |
---|
2319 | case 'default_ping_status': |
---|
2320 | case 'default_comment_status': |
---|
2321 | // Options that if not there have 0 value but need to be something like "closed" |
---|
2322 | if ( $value == '0' || $value == '') |
---|
2323 | $value = 'closed'; |
---|
2324 | break; |
---|
2325 | |
---|
2326 | case 'blogdescription': |
---|
2327 | case 'blogname': |
---|
2328 | $value = addslashes($value); |
---|
2329 | $value = wp_filter_post_kses( $value ); // calls stripslashes then addslashes |
---|
2330 | $value = stripslashes($value); |
---|
2331 | $value = esc_html( $value ); |
---|
2332 | break; |
---|
2333 | |
---|
2334 | case 'blog_charset': |
---|
2335 | $value = preg_replace('/[^a-zA-Z0-9_-]/', '', $value); // strips slashes |
---|
2336 | break; |
---|
2337 | |
---|
2338 | case 'date_format': |
---|
2339 | case 'time_format': |
---|
2340 | case 'mailserver_url': |
---|
2341 | case 'mailserver_login': |
---|
2342 | case 'mailserver_pass': |
---|
2343 | case 'ping_sites': |
---|
2344 | case 'upload_path': |
---|
2345 | $value = strip_tags($value); |
---|
2346 | $value = addslashes($value); |
---|
2347 | $value = wp_filter_kses($value); // calls stripslashes then addslashes |
---|
2348 | $value = stripslashes($value); |
---|
2349 | break; |
---|
2350 | |
---|
2351 | case 'gmt_offset': |
---|
2352 | $value = preg_replace('/[^0-9:.-]/', '', $value); // strips slashes |
---|
2353 | break; |
---|
2354 | |
---|
2355 | case 'siteurl': |
---|
2356 | case 'home': |
---|
2357 | $value = stripslashes($value); |
---|
2358 | $value = esc_url($value); |
---|
2359 | break; |
---|
2360 | default : |
---|
2361 | $value = apply_filters("sanitize_option_{$option}", $value, $option); |
---|
2362 | break; |
---|
2363 | } |
---|
2364 | |
---|
2365 | return $value; |
---|
2366 | } |
---|
2367 | |
---|
2368 | /** |
---|
2369 | * Parses a string into variables to be stored in an array. |
---|
2370 | * |
---|
2371 | * Uses {@link http://www.php.net/parse_str parse_str()} and stripslashes if |
---|
2372 | * {@link http://www.php.net/magic_quotes magic_quotes_gpc} is on. |
---|
2373 | * |
---|
2374 | * @since 2.2.1 |
---|
2375 | * @uses apply_filters() for the 'wp_parse_str' filter. |
---|
2376 | * |
---|
2377 | * @param string $string The string to be parsed. |
---|
2378 | * @param array $array Variables will be stored in this array. |
---|
2379 | */ |
---|
2380 | function wp_parse_str( $string, &$array ) { |
---|
2381 | parse_str( $string, $array ); |
---|
2382 | if ( get_magic_quotes_gpc() ) |
---|
2383 | $array = stripslashes_deep( $array ); |
---|
2384 | $array = apply_filters( 'wp_parse_str', $array ); |
---|
2385 | } |
---|
2386 | |
---|
2387 | /** |
---|
2388 | * Convert lone less than signs. |
---|
2389 | * |
---|
2390 | * KSES already converts lone greater than signs. |
---|
2391 | * |
---|
2392 | * @uses wp_pre_kses_less_than_callback in the callback function. |
---|
2393 | * @since 2.3.0 |
---|
2394 | * |
---|
2395 | * @param string $text Text to be converted. |
---|
2396 | * @return string Converted text. |
---|
2397 | */ |
---|
2398 | function wp_pre_kses_less_than( $text ) { |
---|
2399 | return preg_replace_callback('%<[^>]*?((?=<)|>|$)%', 'wp_pre_kses_less_than_callback', $text); |
---|
2400 | } |
---|
2401 | |
---|
2402 | /** |
---|
2403 | * Callback function used by preg_replace. |
---|
2404 | * |
---|
2405 | * @uses esc_html to format the $matches text. |
---|
2406 | * @since 2.3.0 |
---|
2407 | * |
---|
2408 | * @param array $matches Populated by matches to preg_replace. |
---|
2409 | * @return string The text returned after esc_html if needed. |
---|
2410 | */ |
---|
2411 | function wp_pre_kses_less_than_callback( $matches ) { |
---|
2412 | if ( false === strpos($matches[0], '>') ) |
---|
2413 | return esc_html($matches[0]); |
---|
2414 | return $matches[0]; |
---|
2415 | } |
---|
2416 | |
---|
2417 | /** |
---|
2418 | * WordPress implementation of PHP sprintf() with filters. |
---|
2419 | * |
---|
2420 | * @since 2.5.0 |
---|
2421 | * @link http://www.php.net/sprintf |
---|
2422 | * |
---|
2423 | * @param string $pattern The string which formatted args are inserted. |
---|
2424 | * @param mixed $args,... Arguments to be formatted into the $pattern string. |
---|
2425 | * @return string The formatted string. |
---|
2426 | */ |
---|
2427 | function wp_sprintf( $pattern ) { |
---|
2428 | $args = func_get_args( ); |
---|
2429 | $len = strlen($pattern); |
---|
2430 | $start = 0; |
---|
2431 | $result = ''; |
---|
2432 | $arg_index = 0; |
---|
2433 | while ( $len > $start ) { |
---|
2434 | // Last character: append and break |
---|
2435 | if ( strlen($pattern) - 1 == $start ) { |
---|
2436 | $result .= substr($pattern, -1); |
---|
2437 | break; |
---|
2438 | } |
---|
2439 | |
---|
2440 | // Literal %: append and continue |
---|
2441 | if ( substr($pattern, $start, 2) == '%%' ) { |
---|
2442 | $start += 2; |
---|
2443 | $result .= '%'; |
---|
2444 | continue; |
---|
2445 | } |
---|
2446 | |
---|
2447 | // Get fragment before next % |
---|
2448 | $end = strpos($pattern, '%', $start + 1); |
---|
2449 | if ( false === $end ) |
---|
2450 | $end = $len; |
---|
2451 | $fragment = substr($pattern, $start, $end - $start); |
---|
2452 | |
---|
2453 | // Fragment has a specifier |
---|
2454 | if ( $pattern{$start} == '%' ) { |
---|
2455 | // Find numbered arguments or take the next one in order |
---|
2456 | if ( preg_match('/^%(\d+)\$/', $fragment, $matches) ) { |
---|
2457 | $arg = isset($args[$matches[1]]) ? $args[$matches[1]] : ''; |
---|
2458 | $fragment = str_replace("%{$matches[1]}$", '%', $fragment); |
---|
2459 | } else { |
---|
2460 | ++$arg_index; |
---|
2461 | $arg = isset($args[$arg_index]) ? $args[$arg_index] : ''; |
---|
2462 | } |
---|
2463 | |
---|
2464 | // Apply filters OR sprintf |
---|
2465 | $_fragment = apply_filters( 'wp_sprintf', $fragment, $arg ); |
---|
2466 | if ( $_fragment != $fragment ) |
---|
2467 | $fragment = $_fragment; |
---|
2468 | else |
---|
2469 | $fragment = sprintf($fragment, strval($arg) ); |
---|
2470 | } |
---|
2471 | |
---|
2472 | // Append to result and move to next fragment |
---|
2473 | $result .= $fragment; |
---|
2474 | $start = $end; |
---|
2475 | } |
---|
2476 | return $result; |
---|
2477 | } |
---|
2478 | |
---|
2479 | /** |
---|
2480 | * Localize list items before the rest of the content. |
---|
2481 | * |
---|
2482 | * The '%l' must be at the first characters can then contain the rest of the |
---|
2483 | * content. The list items will have ', ', ', and', and ' and ' added depending |
---|
2484 | * on the amount of list items in the $args parameter. |
---|
2485 | * |
---|
2486 | * @since 2.5.0 |
---|
2487 | * |
---|
2488 | * @param string $pattern Content containing '%l' at the beginning. |
---|
2489 | * @param array $args List items to prepend to the content and replace '%l'. |
---|
2490 | * @return string Localized list items and rest of the content. |
---|
2491 | */ |
---|
2492 | function wp_sprintf_l($pattern, $args) { |
---|
2493 | // Not a match |
---|
2494 | if ( substr($pattern, 0, 2) != '%l' ) |
---|
2495 | return $pattern; |
---|
2496 | |
---|
2497 | // Nothing to work with |
---|
2498 | if ( empty($args) ) |
---|
2499 | return ''; |
---|
2500 | |
---|
2501 | // Translate and filter the delimiter set (avoid ampersands and entities here) |
---|
2502 | $l = apply_filters('wp_sprintf_l', array( |
---|
2503 | /* translators: used between list items, there is a space after the coma */ |
---|
2504 | 'between' => __(', '), |
---|
2505 | /* translators: used between list items, there is a space after the and */ |
---|
2506 | 'between_last_two' => __(', and '), |
---|
2507 | /* translators: used between only two list items, there is a space after the and */ |
---|
2508 | 'between_only_two' => __(' and '), |
---|
2509 | )); |
---|
2510 | |
---|
2511 | $args = (array) $args; |
---|
2512 | $result = array_shift($args); |
---|
2513 | if ( count($args) == 1 ) |
---|
2514 | $result .= $l['between_only_two'] . array_shift($args); |
---|
2515 | // Loop when more than two args |
---|
2516 | $i = count($args); |
---|
2517 | while ( $i ) { |
---|
2518 | $arg = array_shift($args); |
---|
2519 | $i--; |
---|
2520 | if ( 0 == $i ) |
---|
2521 | $result .= $l['between_last_two'] . $arg; |
---|
2522 | else |
---|
2523 | $result .= $l['between'] . $arg; |
---|
2524 | } |
---|
2525 | return $result . substr($pattern, 2); |
---|
2526 | } |
---|
2527 | |
---|
2528 | /** |
---|
2529 | * Safely extracts not more than the first $count characters from html string. |
---|
2530 | * |
---|
2531 | * UTF-8, tags and entities safe prefix extraction. Entities inside will *NOT* |
---|
2532 | * be counted as one character. For example & will be counted as 4, < as |
---|
2533 | * 3, etc. |
---|
2534 | * |
---|
2535 | * @since 2.5.0 |
---|
2536 | * |
---|
2537 | * @param integer $str String to get the excerpt from. |
---|
2538 | * @param integer $count Maximum number of characters to take. |
---|
2539 | * @return string The excerpt. |
---|
2540 | */ |
---|
2541 | function wp_html_excerpt( $str, $count ) { |
---|
2542 | $str = strip_tags( $str ); |
---|
2543 | $str = mb_substr( $str, 0, $count ); |
---|
2544 | // remove part of an entity at the end |
---|
2545 | $str = preg_replace( '/&[^;\s]{0,6}$/', '', $str ); |
---|
2546 | return $str; |
---|
2547 | } |
---|
2548 | |
---|
2549 | /** |
---|
2550 | * Add a Base url to relative links in passed content. |
---|
2551 | * |
---|
2552 | * By default it supports the 'src' and 'href' attributes. However this can be |
---|
2553 | * changed via the 3rd param. |
---|
2554 | * |
---|
2555 | * @since 2.7.0 |
---|
2556 | * |
---|
2557 | * @param string $content String to search for links in. |
---|
2558 | * @param string $base The base URL to prefix to links. |
---|
2559 | * @param array $attrs The attributes which should be processed. |
---|
2560 | * @return string The processed content. |
---|
2561 | */ |
---|
2562 | function links_add_base_url( $content, $base, $attrs = array('src', 'href') ) { |
---|
2563 | $attrs = implode('|', (array)$attrs); |
---|
2564 | return preg_replace_callback("!($attrs)=(['\"])(.+?)\\2!i", |
---|
2565 | create_function('$m', 'return _links_add_base($m, "' . $base . '");'), |
---|
2566 | $content); |
---|
2567 | } |
---|
2568 | |
---|
2569 | /** |
---|
2570 | * Callback to add a base url to relative links in passed content. |
---|
2571 | * |
---|
2572 | * @since 2.7.0 |
---|
2573 | * @access private |
---|
2574 | * |
---|
2575 | * @param string $m The matched link. |
---|
2576 | * @param string $base The base URL to prefix to links. |
---|
2577 | * @return string The processed link. |
---|
2578 | */ |
---|
2579 | function _links_add_base($m, $base) { |
---|
2580 | //1 = attribute name 2 = quotation mark 3 = URL |
---|
2581 | return $m[1] . '=' . $m[2] . |
---|
2582 | (strpos($m[3], 'http://') === false ? |
---|
2583 | path_join($base, $m[3]) : |
---|
2584 | $m[3]) |
---|
2585 | . $m[2]; |
---|
2586 | } |
---|
2587 | |
---|
2588 | /** |
---|
2589 | * Adds a Target attribute to all links in passed content. |
---|
2590 | * |
---|
2591 | * This function by default only applies to <a> tags, however this can be |
---|
2592 | * modified by the 3rd param. |
---|
2593 | * |
---|
2594 | * <b>NOTE:</b> Any current target attributed will be striped and replaced. |
---|
2595 | * |
---|
2596 | * @since 2.7.0 |
---|
2597 | * |
---|
2598 | * @param string $content String to search for links in. |
---|
2599 | * @param string $target The Target to add to the links. |
---|
2600 | * @param array $tags An array of tags to apply to. |
---|
2601 | * @return string The processed content. |
---|
2602 | */ |
---|
2603 | function links_add_target( $content, $target = '_blank', $tags = array('a') ) { |
---|
2604 | $tags = implode('|', (array)$tags); |
---|
2605 | return preg_replace_callback("!<($tags)(.+?)>!i", |
---|
2606 | create_function('$m', 'return _links_add_target($m, "' . $target . '");'), |
---|
2607 | $content); |
---|
2608 | } |
---|
2609 | /** |
---|
2610 | * Callback to add a target attribute to all links in passed content. |
---|
2611 | * |
---|
2612 | * @since 2.7.0 |
---|
2613 | * @access private |
---|
2614 | * |
---|
2615 | * @param string $m The matched link. |
---|
2616 | * @param string $target The Target to add to the links. |
---|
2617 | * @return string The processed link. |
---|
2618 | */ |
---|
2619 | function _links_add_target( $m, $target ) { |
---|
2620 | $tag = $m[1]; |
---|
2621 | $link = preg_replace('|(target=[\'"](.*?)[\'"])|i', '', $m[2]); |
---|
2622 | return '<' . $tag . $link . ' target="' . $target . '">'; |
---|
2623 | } |
---|
2624 | |
---|
2625 | // normalize EOL characters and strip duplicate whitespace |
---|
2626 | function normalize_whitespace( $str ) { |
---|
2627 | $str = trim($str); |
---|
2628 | $str = str_replace("\r", "\n", $str); |
---|
2629 | $str = preg_replace( array( '/\n+/', '/[ \t]+/' ), array( "\n", ' ' ), $str ); |
---|
2630 | return $str; |
---|
2631 | } |
---|
2632 | |
---|
2633 | ?> |
---|