Make WordPress Core

Ticket #32080: formatting.php

File formatting.php, 141.3 KB (added by mihai2u, 11 years ago)

Tested formatting.php file

Line 
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 *
15 *     'cause today's effort makes it worth tomorrow's "holiday" ...
16 *
17 * Becomes:
18 *
19 *     &#8217;cause today&#8217;s effort makes it worth tomorrow&#8217;s &#8220;holiday&#8221; &#8230;
20 *
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 * @param bool $reset Set to true for unit testing. Translated patterns will reset.
28 * @return string The string replaced with html entities
29 */
30function wptexturize($text, $reset = false) {
31        global $wp_cockneyreplace, $shortcode_tags;
32        static $static_characters, $static_replacements, $dynamic_characters, $dynamic_replacements,
33                $default_no_texturize_tags, $default_no_texturize_shortcodes, $run_texturize = true;
34
35        // If there's nothing to do, just stop.
36        if ( empty( $text ) || false === $run_texturize ) {
37                return $text;
38        }
39
40        // Set up static variables. Run once only.
41        if ( $reset || ! isset( $static_characters ) ) {
42                /**
43                 * Filter whether to skip running wptexturize().
44                 *
45                 * Passing false to the filter will effectively short-circuit wptexturize().
46                 * returning the original text passed to the function instead.
47                 *
48                 * The filter runs only once, the first time wptexturize() is called.
49                 *
50                 * @since 4.0.0
51                 *
52                 * @see wptexturize()
53                 *
54                 * @param bool $run_texturize Whether to short-circuit wptexturize().
55                 */
56                $run_texturize = apply_filters( 'run_wptexturize', $run_texturize );
57                if ( false === $run_texturize ) {
58                        return $text;
59                }
60
61                /* translators: opening curly double quote */
62                $opening_quote = _x( '&#8220;', 'opening curly double quote' );
63                /* translators: closing curly double quote */
64                $closing_quote = _x( '&#8221;', 'closing curly double quote' );
65
66                /* translators: apostrophe, for example in 'cause or can't */
67                $apos = _x( '&#8217;', 'apostrophe' );
68
69                /* translators: prime, for example in 9' (nine feet) */
70                $prime = _x( '&#8242;', 'prime' );
71                /* translators: double prime, for example in 9" (nine inches) */
72                $double_prime = _x( '&#8243;', 'double prime' );
73
74                /* translators: opening curly single quote */
75                $opening_single_quote = _x( '&#8216;', 'opening curly single quote' );
76                /* translators: closing curly single quote */
77                $closing_single_quote = _x( '&#8217;', 'closing curly single quote' );
78
79                /* translators: en dash */
80                $en_dash = _x( '&#8211;', 'en dash' );
81                /* translators: em dash */
82                $em_dash = _x( '&#8212;', 'em dash' );
83
84                $default_no_texturize_tags = array('pre', 'code', 'kbd', 'style', 'script', 'tt');
85                $default_no_texturize_shortcodes = array('code');
86
87                // if a plugin has provided an autocorrect array, use it
88                if ( isset($wp_cockneyreplace) ) {
89                        $cockney = array_keys( $wp_cockneyreplace );
90                        $cockneyreplace = array_values( $wp_cockneyreplace );
91                } elseif ( "'" != $apos ) { // Only bother if we're doing a replacement.
92                        $cockney = array( "'tain't", "'twere", "'twas", "'tis", "'twill", "'til", "'bout", "'nuff", "'round", "'cause", "'em" );
93                        $cockneyreplace = array( $apos . "tain" . $apos . "t", $apos . "twere", $apos . "twas", $apos . "tis", $apos . "twill", $apos . "til", $apos . "bout", $apos . "nuff", $apos . "round", $apos . "cause", $apos . "em" );
94                } else {
95                        $cockney = $cockneyreplace = array();
96                }
97
98                $static_characters = array_merge( array( '...', '``', '\'\'', ' (tm)' ), $cockney );
99                $static_replacements = array_merge( array( '&#8230;', $opening_quote, $closing_quote, ' &#8482;' ), $cockneyreplace );
100
101
102                // Pattern-based replacements of characters.
103                // Sort the remaining patterns into several arrays for performance tuning.
104                $dynamic_characters = array( 'apos' => array(), 'quote' => array(), 'dash' => array() );
105                $dynamic_replacements = array( 'apos' => array(), 'quote' => array(), 'dash' => array() );
106                $dynamic = array();
107                $spaces = wp_spaces_regexp();
108
109                // '99' and '99" are ambiguous among other patterns; assume it's an abbreviated year at the end of a quotation.
110                if ( "'" !== $apos || "'" !== $closing_single_quote ) {
111                        $dynamic[ '/\'(\d\d)\'(?=\Z|[.,)}\-\]]|&gt;|' . $spaces . ')/' ] = $apos . '$1' . $closing_single_quote;
112                }
113                if ( "'" !== $apos || '"' !== $closing_quote ) {
114                        $dynamic[ '/\'(\d\d)"(?=\Z|[.,)}\-\]]|&gt;|' . $spaces . ')/' ] = $apos . '$1' . $closing_quote;
115                }
116
117                // '99 '99s '99's (apostrophe)  But never '9 or '99% or '999 or '99.0.
118                if ( "'" !== $apos ) {
119                        $dynamic[ '/\'(?=\d\d(?:\Z|(?![%\d]|[.,]\d)))/' ] = $apos;
120                }
121
122                // Quoted Numbers like '0.42'
123                if ( "'" !== $opening_single_quote && "'" !== $closing_single_quote ) {
124                        $dynamic[ '/(?<=\A|' . $spaces . ')\'(\d[.,\d]*)\'/' ] = $opening_single_quote . '$1' . $closing_single_quote;
125                }
126
127                // Single quote at start, or preceded by (, {, <, [, ", -, or spaces.
128                if ( "'" !== $opening_single_quote ) {
129                        $dynamic[ '/(?<=\A|[([{"\-]|&lt;|' . $spaces . ')\'/' ] = $opening_single_quote;
130                }
131
132                // Apostrophe in a word.  No spaces, double apostrophes, or other punctuation.
133                if ( "'" !== $apos ) {
134                        $dynamic[ '/(?<!' . $spaces . ')\'(?!\Z|[.,:;"\'(){}[\]\-]|&[lg]t;|' . $spaces . ')/' ] = $apos;
135                }
136
137                // 9' (prime)
138                if ( "'" !== $prime ) {
139                        $dynamic[ '/(?<=\d)\'/' ] = $prime;
140                }
141
142                // Single quotes followed by spaces or ending punctuation.
143                if ( "'" !== $closing_single_quote ) {
144                        $dynamic[ '/\'(?=\Z|[.,)}\-\]]|&gt;|' . $spaces . ')/' ] = $closing_single_quote;
145                }
146
147                $dynamic_characters['apos'] = array_keys( $dynamic );
148                $dynamic_replacements['apos'] = array_values( $dynamic );
149                $dynamic = array();
150
151                // Quoted Numbers like "42"
152                if ( '"' !== $opening_quote && '"' !== $closing_quote ) {
153                        $dynamic[ '/(?<=\A|' . $spaces . ')"(\d[.,\d]*)"/' ] = $opening_quote . '$1' . $closing_quote;
154                }
155
156                // 9" (double prime)
157                if ( '"' !== $double_prime ) {
158                        $dynamic[ '/(?<=\d)"/' ] = $double_prime;
159                }
160
161                // Double quote at start, or preceded by (, {, <, [, -, or spaces, and not followed by spaces.
162                if ( '"' !== $opening_quote ) {
163                        $dynamic[ '/(?<=\A|[([{\-]|&lt;|' . $spaces . ')"(?!' . $spaces . ')/' ] = $opening_quote;
164                }
165
166                // Any remaining double quotes.
167                if ( '"' !== $closing_quote ) {
168                        $dynamic[ '/"/' ] = $closing_quote;
169                }
170
171                $dynamic_characters['quote'] = array_keys( $dynamic );
172                $dynamic_replacements['quote'] = array_values( $dynamic );
173                $dynamic = array();
174
175                // Dashes and spaces
176                $dynamic[ '/---/' ] = $em_dash;
177                $dynamic[ '/(?<=^|' . $spaces . ')--(?=$|' . $spaces . ')/' ] = $em_dash;
178                $dynamic[ '/(?<!xn)--/' ] = $en_dash;
179                $dynamic[ '/(?<=^|' . $spaces . ')-(?=$|' . $spaces . ')/' ] = $en_dash;
180
181                $dynamic_characters['dash'] = array_keys( $dynamic );
182                $dynamic_replacements['dash'] = array_values( $dynamic );
183        }
184
185        // Must do this every time in case plugins use these filters in a context sensitive manner
186        /**
187         * Filter the list of HTML elements not to texturize.
188         *
189         * @since 2.8.0
190         *
191         * @param array $default_no_texturize_tags An array of HTML element names.
192         */
193        $no_texturize_tags = apply_filters( 'no_texturize_tags', $default_no_texturize_tags );
194        /**
195         * Filter the list of shortcodes not to texturize.
196         *
197         * @since 2.8.0
198         *
199         * @param array $default_no_texturize_shortcodes An array of shortcode names.
200         */
201        $no_texturize_shortcodes = apply_filters( 'no_texturize_shortcodes', $default_no_texturize_shortcodes );
202
203        $no_texturize_tags_stack = array();
204        $no_texturize_shortcodes_stack = array();
205
206        // Look for shortcodes and HTML elements.
207
208        $tagnames = array_keys( $shortcode_tags );
209        $tagregexp = join( '|', array_map( 'preg_quote', $tagnames ) );
210        $tagregexp = "(?:$tagregexp)(?![\\w-])"; // Excerpt of get_shortcode_regex().
211
212        $comment_regex =
213                  '!'           // Start of comment, after the <.
214                . '(?:'         // Unroll the loop: Consume everything until --> is found.
215                .     '-(?!->)' // Dash not followed by end of comment.
216                .     '[^\-]*+' // Consume non-dashes.
217                . ')*+'         // Loop possessively.
218                . '(?:-->)?';   // End of comment. If not found, match all input.
219
220        $shortcode_regex =
221                  '\['              // Find start of shortcode.
222                . '[\/\[]?'         // Shortcodes may begin with [/ or [[
223                . $tagregexp        // Only match registered shortcodes, because performance.
224                . '(?:'
225                .     '[^\[\]<>]+'  // Shortcodes do not contain other shortcodes. Quantifier critical.
226                . '|'
227                .     '<[^\[\]>]*>' // HTML elements permitted. Prevents matching ] before >.
228                . ')*+'             // Possessive critical.
229                . '\]'              // Find end of shortcode.
230                . '\]?';            // Shortcodes may end with ]]
231
232        $regex =
233                  '/('                   // Capture the entire match.
234                .     '<'                // Find start of element.
235                .     '(?(?=!--)'        // Is this a comment?
236                .         $comment_regex // Find end of comment.
237                .     '|'
238                .         '[^>]*>'       // Find end of element.
239                .     ')'
240                . '|'
241                .     $shortcode_regex   // Find shortcodes.
242                . ')/s';
243
244        $textarr = preg_split( $regex, $text, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY );
245
246        foreach ( $textarr as &$curl ) {
247                // Only call _wptexturize_pushpop_element if $curl is a delimiter.
248                $first = $curl[0];
249                if ( '<' === $first && '<!--' === substr( $curl, 0, 4 ) ) {
250                        // This is an HTML comment delimeter.
251
252                        continue;
253
254                } elseif ( '<' === $first && '>' === substr( $curl, -1 ) ) {
255                        // This is an HTML element delimiter.
256
257                        _wptexturize_pushpop_element( $curl, $no_texturize_tags_stack, $no_texturize_tags );
258
259                } elseif ( '' === trim( $curl ) ) {
260                        // This is a newline between delimiters.  Performance improves when we check this.
261
262                        continue;
263
264                } elseif ( '[' === $first && 1 === preg_match( '/^' . $shortcode_regex . '$/', $curl ) ) {
265                        // This is a shortcode delimiter.
266
267                        if ( '[[' !== substr( $curl, 0, 2 ) && ']]' !== substr( $curl, -2 ) ) {
268                                // Looks like a normal shortcode.
269                                _wptexturize_pushpop_element( $curl, $no_texturize_shortcodes_stack, $no_texturize_shortcodes );
270                        } else {
271                                // Looks like an escaped shortcode.
272                                continue;
273                        }
274
275                } elseif ( empty( $no_texturize_shortcodes_stack ) && empty( $no_texturize_tags_stack ) ) {
276                        // This is neither a delimiter, nor is this content inside of no_texturize pairs.  Do texturize.
277
278                        $curl = str_replace( $static_characters, $static_replacements, $curl );
279
280                        if ( false !== strpos( $curl, "'" ) ) {
281                                $curl = preg_replace( $dynamic_characters['apos'], $dynamic_replacements['apos'], $curl );
282                        }
283                        if ( false !== strpos( $curl, '"' ) ) {
284                                $curl = preg_replace( $dynamic_characters['quote'], $dynamic_replacements['quote'], $curl );
285                        }
286                        if ( false !== strpos( $curl, '-' ) ) {
287                                $curl = preg_replace( $dynamic_characters['dash'], $dynamic_replacements['dash'], $curl );
288                        }
289
290                        // 9x9 (times), but never 0x9999
291                        if ( 1 === preg_match( '/(?<=\d)x\d/', $curl ) ) {
292                                // Searching for a digit is 10 times more expensive than for the x, so we avoid doing this one!
293                                $curl = preg_replace( '/\b(\d(?(?<=0)[\d\.,]+|[\d\.,]*))x(\d[\d\.,]*)\b/', '$1&#215;$2', $curl );
294                        }
295                }
296        }
297        $text = implode( '', $textarr );
298
299        // Replace each & with &#038; unless it already looks like an entity.
300        $text = preg_replace('/&(?!#(?:\d+|x[a-f0-9]+);|[a-z1-4]{1,8};)/i', '&#038;', $text);
301
302        return $text;
303}
304
305/**
306 * Search for disabled element tags. Push element to stack on tag open and pop
307 * on tag close.
308 *
309 * Assumes first char of $text is tag opening and last char is tag closing.
310 * Assumes second char of $text is optionally '/' to indicate closing as in </html>.
311 *
312 * @since 2.9.0
313 * @access private
314 *
315 * @param string $text Text to check. Must be a tag like `<html>` or `[shortcode]`.
316 * @param array $stack List of open tag elements.
317 * @param array $disabled_elements The tag names to match against. Spaces are not allowed in tag names.
318 */
319function _wptexturize_pushpop_element($text, &$stack, $disabled_elements) {
320        // Is it an opening tag or closing tag?
321        if ( '/' !== $text[1] ) {
322                $opening_tag = true;
323                $name_offset = 1;
324        } elseif ( 0 == count( $stack ) ) {
325                // Stack is empty. Just stop.
326                return;
327        } else {
328                $opening_tag = false;
329                $name_offset = 2;
330        }
331
332        // Parse out the tag name.
333        $space = strpos( $text, ' ' );
334        if ( false === $space ) {
335                $space = -1;
336        } else {
337                $space -= $name_offset;
338        }
339        $tag = substr( $text, $name_offset, $space );
340
341        // Handle disabled tags.
342        if ( in_array( $tag, $disabled_elements ) ) {
343                if ( $opening_tag ) {
344                        /*
345                         * This disables texturize until we find a closing tag of our type
346                         * (e.g. <pre>) even if there was invalid nesting before that
347                         *
348                         * Example: in the case <pre>sadsadasd</code>"baba"</pre>
349                         *          "baba" won't be texturize
350                         */
351
352                        array_push( $stack, $tag );
353                } elseif ( end( $stack ) == $tag ) {
354                        array_pop( $stack );
355                }
356        }
357}
358
359/**
360 * Replaces double line-breaks with paragraph elements.
361 *
362 * A group of regex replaces used to identify text formatted with newlines and
363 * replace double line-breaks with HTML paragraph tags. The remaining line-breaks
364 * after conversion become <<br />> tags, unless $br is set to '0' or 'false'.
365 *
366 * @since 0.71
367 *
368 * @param string $pee The text which has to be formatted.
369 * @param bool   $br  Optional. If set, this will convert all remaining line-breaks
370 *                    after paragraphing. Default true.
371 * @return string Text which has been converted into correct paragraph tags.
372 */
373function wpautop($pee, $br = true) {
374        $pre_tags = array();
375
376        if ( trim($pee) === '' )
377                return '';
378
379        // Just to make things a little easier, pad the end.
380        $pee = $pee . "\n";
381
382        /*
383         * Pre tags shouldn't be touched by autop.
384         * Replace pre tags with placeholders and bring them back after autop.
385         */
386        if ( strpos($pee, '<pre') !== false ) {
387                $pee_parts = explode( '</pre>', $pee );
388                $last_pee = array_pop($pee_parts);
389                $pee = '';
390                $i = 0;
391
392                foreach ( $pee_parts as $pee_part ) {
393                        $start = strpos($pee_part, '<pre');
394
395                        // Malformed html?
396                        if ( $start === false ) {
397                                $pee .= $pee_part;
398                                continue;
399                        }
400
401                        $name = "<pre wp-pre-tag-$i></pre>";
402                        $pre_tags[$name] = substr( $pee_part, $start ) . '</pre>';
403
404                        $pee .= substr( $pee_part, 0, $start ) . $name;
405                        $i++;
406                }
407
408                $pee .= $last_pee;
409        }
410        // Change multiple <br>s into two line breaks, which will turn into paragraphs.
411        $pee = preg_replace('|<br />\s*<br />|', "\n\n", $pee);
412
413        $allblocks = '(?:table|thead|tfoot|caption|col|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|form|map|area|blockquote|address|math|style|p|h[1-6]|hr|fieldset|legend|section|article|aside|hgroup|header|footer|nav|figure|figcaption|details|menu|summary)';
414
415        // Add a single line break above block-level opening tags.
416        $pee = preg_replace('!(<' . $allblocks . '[^>]*>)!', "\n$1", $pee);
417
418        // Add a double line break below block-level closing tags.
419        $pee = preg_replace('!(</' . $allblocks . '>)!', "$1\n\n", $pee);
420
421        // Standardize newline characters to "\n".
422        $pee = str_replace(array("\r\n", "\r"), "\n", $pee);
423
424        // Collapse line breaks before and after <option> elements so they don't get autop'd.
425        if ( strpos( $pee, '<option' ) !== false ) {
426                $pee = preg_replace( '|\s*<option|', '<option', $pee );
427                $pee = preg_replace( '|</option>\s*|', '</option>', $pee );
428        }
429
430        /*
431         * Collapse line breaks inside <object> elements, before <param> and <embed> elements
432         * so they don't get autop'd.
433         */
434        if ( strpos( $pee, '</object>' ) !== false ) {
435                $pee = preg_replace( '|(<object[^>]*>)\s*|', '$1', $pee );
436                $pee = preg_replace( '|\s*</object>|', '</object>', $pee );
437                $pee = preg_replace( '%\s*(</?(?:param|embed)[^>]*>)\s*%', '$1', $pee );
438        }
439
440        /*
441         * Collapse line breaks inside <audio> and <video> elements,
442         * before and after <source> and <track> elements.
443         */
444        if ( strpos( $pee, '<source' ) !== false || strpos( $pee, '<track' ) !== false ) {
445                $pee = preg_replace( '%([<\[](?:audio|video)[^>\]]*[>\]])\s*%', '$1', $pee );
446                $pee = preg_replace( '%\s*([<\[]/(?:audio|video)[>\]])%', '$1', $pee );
447                $pee = preg_replace( '%\s*(<(?:source|track)[^>]*>)\s*%', '$1', $pee );
448        }
449
450        // Remove more than two contiguous line breaks.
451        $pee = preg_replace("/\n\n+/", "\n\n", $pee);
452
453        // Split up the contents into an array of strings, separated by double line breaks.
454        $pees = preg_split('/\n\s*\n/', $pee, -1, PREG_SPLIT_NO_EMPTY);
455
456        // Reset $pee prior to rebuilding.
457        $pee = '';
458
459        // Rebuild the content as a string, wrapping every bit with a <p>.
460        foreach ( $pees as $tinkle ) {
461                $pee .= '<p>' . trim($tinkle, "\n") . "</p>\n";
462        }
463
464        // Under certain strange conditions it could create a P of entirely whitespace.
465        $pee = preg_replace('|<p>\s*</p>|', '', $pee);
466
467        // Add a closing <p> inside <div>, <address>, or <form> tag if missing.
468        $pee = preg_replace('!<p>([^<]+)</(div|address|form)>!', "<p>$1</p></$2>", $pee);
469
470        // If an opening or closing block element tag is wrapped in a <p>, unwrap it.
471        $pee = preg_replace('!<p>\s*(</?' . $allblocks . '[^>]*>)\s*</p>!', "$1", $pee);
472
473        // In some cases <li> may get wrapped in <p>, fix them.
474        $pee = preg_replace("|<p>(<li.+?)</p>|", "$1", $pee);
475
476        // If a <blockquote> is wrapped with a <p>, move it inside the <blockquote>.
477        $pee = preg_replace('|<p><blockquote([^>]*)>|i', "<blockquote$1><p>", $pee);
478        $pee = str_replace('</blockquote></p>', '</p></blockquote>', $pee);
479
480        // If an opening or closing block element tag is preceded by an opening <p> tag, remove it.
481        $pee = preg_replace('!<p>\s*(</?' . $allblocks . '[^>]*>)!', "$1", $pee);
482
483        // If an opening or closing block element tag is followed by a closing <p> tag, remove it.
484        $pee = preg_replace('!(</?' . $allblocks . '[^>]*>)\s*</p>!', "$1", $pee);
485
486        // Optionally insert line breaks.
487        if ( $br ) {
488                // Replace newlines that shouldn't be touched with a placeholder.
489                $pee = preg_replace_callback('/<(script|style).*?<\/\\1>/s', '_autop_newline_preservation_helper', $pee);
490
491                // Replace any new line characters that aren't preceded by a <br /> with a <br />.
492                $pee = preg_replace('|(?<!<br />)\s*\n|', "<br />\n", $pee);
493
494                // Replace newline placeholders with newlines.
495                $pee = str_replace('<WPPreserveNewline />', "\n", $pee);
496        }
497
498        // If a <br /> tag is after an opening or closing block tag, remove it.
499        $pee = preg_replace('!(</?' . $allblocks . '[^>]*>)\s*<br />!', "$1", $pee);
500
501        // If a <br /> tag is before a subset of opening or closing block tags, remove it.
502        $pee = preg_replace('!<br />(\s*</?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)[^>]*>)!', '$1', $pee);
503        $pee = preg_replace( "|\n</p>$|", '</p>', $pee );
504
505        // Replace placeholder <pre> tags with their original content.
506        if ( !empty($pre_tags) )
507                $pee = str_replace(array_keys($pre_tags), array_values($pre_tags), $pee);
508
509        return $pee;
510}
511
512/**
513 * Newline preservation help function for wpautop
514 *
515 * @since 3.1.0
516 * @access private
517 *
518 * @param array $matches preg_replace_callback matches array
519 * @return string
520 */
521function _autop_newline_preservation_helper( $matches ) {
522        return str_replace("\n", "<WPPreserveNewline />", $matches[0]);
523}
524
525/**
526 * Don't auto-p wrap shortcodes that stand alone
527 *
528 * Ensures that shortcodes are not wrapped in `<p>...</p>`.
529 *
530 * @since 2.9.0
531 *
532 * @param string $pee The content.
533 * @return string The filtered content.
534 */
535function shortcode_unautop( $pee ) {
536        global $shortcode_tags;
537
538        if ( empty( $shortcode_tags ) || !is_array( $shortcode_tags ) ) {
539                return $pee;
540        }
541
542        $tagregexp = join( '|', array_map( 'preg_quote', array_keys( $shortcode_tags ) ) );
543        $spaces = wp_spaces_regexp();
544
545        $pattern =
546                  '/'
547                . '<p>'                              // Opening paragraph
548                . '(?:' . $spaces . ')*+'            // Optional leading whitespace
549                . '('                                // 1: The shortcode
550                .     '\\['                          // Opening bracket
551                .     "($tagregexp)"                 // 2: Shortcode name
552                .     '(?![\\w-])'                   // Not followed by word character or hyphen
553                                                     // Unroll the loop: Inside the opening shortcode tag
554                .     '[^\\]\\/]*'                   // Not a closing bracket or forward slash
555                .     '(?:'
556                .         '\\/(?!\\])'               // A forward slash not followed by a closing bracket
557                .         '[^\\]\\/]*'               // Not a closing bracket or forward slash
558                .     ')*?'
559                .     '(?:'
560                .         '\\/\\]'                   // Self closing tag and closing bracket
561                .     '|'
562                .         '\\]'                      // Closing bracket
563                .         '(?:'                      // Unroll the loop: Optionally, anything between the opening and closing shortcode tags
564                .             '[^\\[]*+'             // Not an opening bracket
565                .             '(?:'
566                .                 '\\[(?!\\/\\2\\])' // An opening bracket not followed by the closing shortcode tag
567                .                 '[^\\[]*+'         // Not an opening bracket
568                .             ')*+'
569                .             '\\[\\/\\2\\]'         // Closing shortcode tag
570                .         ')?'
571                .     ')'
572                . ')'
573                . '(?:' . $spaces . ')*+'            // optional trailing whitespace
574                . '<\\/p>'                           // closing paragraph
575                . '/s';
576
577        return preg_replace( $pattern, '$1', $pee );
578}
579
580/**
581 * Checks to see if a string is utf8 encoded.
582 *
583 * NOTE: This function checks for 5-Byte sequences, UTF8
584 *       has Bytes Sequences with a maximum length of 4.
585 *
586 * @author bmorel at ssi dot fr (modified)
587 * @since 1.2.1
588 *
589 * @param string $str The string to be checked
590 * @return bool True if $str fits a UTF-8 model, false otherwise.
591 */
592function seems_utf8($str) {
593        mbstring_binary_safe_encoding();
594        $length = strlen($str);
595        reset_mbstring_encoding();
596        for ($i=0; $i < $length; $i++) {
597                $c = ord($str[$i]);
598                if ($c < 0x80) $n = 0; // 0bbbbbbb
599                elseif (($c & 0xE0) == 0xC0) $n=1; // 110bbbbb
600                elseif (($c & 0xF0) == 0xE0) $n=2; // 1110bbbb
601                elseif (($c & 0xF8) == 0xF0) $n=3; // 11110bbb
602                elseif (($c & 0xFC) == 0xF8) $n=4; // 111110bb
603                elseif (($c & 0xFE) == 0xFC) $n=5; // 1111110b
604                else return false; // Does not match any model
605                for ($j=0; $j<$n; $j++) { // n bytes matching 10bbbbbb follow ?
606                        if ((++$i == $length) || ((ord($str[$i]) & 0xC0) != 0x80))
607                                return false;
608                }
609        }
610        return true;
611}
612
613/**
614 * Converts a number of special characters into their HTML entities.
615 *
616 * Specifically deals with: &, <, >, ", and '.
617 *
618 * $quote_style can be set to ENT_COMPAT to encode " to
619 * &quot;, or ENT_QUOTES to do both. Default is ENT_NOQUOTES where no quotes are encoded.
620 *
621 * @since 1.2.2
622 * @access private
623 *
624 * @param string $string The text which is to be encoded.
625 * @param int $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.
626 * @param string $charset Optional. The character encoding of the string. Default is false.
627 * @param boolean $double_encode Optional. Whether to encode existing html entities. Default is false.
628 * @return string The encoded text with HTML entities.
629 */
630function _wp_specialchars( $string, $quote_style = ENT_NOQUOTES, $charset = false, $double_encode = false ) {
631        $string = (string) $string;
632
633        if ( 0 === strlen( $string ) )
634                return '';
635
636        // Don't bother if there are no specialchars - saves some processing
637        if ( ! preg_match( '/[&<>"\']/', $string ) )
638                return $string;
639
640        // Account for the previous behaviour of the function when the $quote_style is not an accepted value
641        if ( empty( $quote_style ) )
642                $quote_style = ENT_NOQUOTES;
643        elseif ( ! in_array( $quote_style, array( 0, 2, 3, 'single', 'double' ), true ) )
644                $quote_style = ENT_QUOTES;
645
646        // Store the site charset as a static to avoid multiple calls to wp_load_alloptions()
647        if ( ! $charset ) {
648                static $_charset;
649                if ( ! isset( $_charset ) ) {
650                        $alloptions = wp_load_alloptions();
651                        $_charset = isset( $alloptions['blog_charset'] ) ? $alloptions['blog_charset'] : '';
652                }
653                $charset = $_charset;
654        }
655
656        if ( in_array( $charset, array( 'utf8', 'utf-8', 'UTF8' ) ) )
657                $charset = 'UTF-8';
658
659        $_quote_style = $quote_style;
660
661        if ( $quote_style === 'double' ) {
662                $quote_style = ENT_COMPAT;
663                $_quote_style = ENT_COMPAT;
664        } elseif ( $quote_style === 'single' ) {
665                $quote_style = ENT_NOQUOTES;
666        }
667
668        // Handle double encoding ourselves
669        if ( $double_encode ) {
670                $string = @htmlspecialchars( $string, $quote_style, $charset );
671        } else {
672                // Decode &amp; into &
673                $string = wp_specialchars_decode( $string, $_quote_style );
674
675                // Guarantee every &entity; is valid or re-encode the &
676                $string = wp_kses_normalize_entities( $string );
677
678                // Now re-encode everything except &entity;
679                $string = preg_split( '/(&#?x?[0-9a-z]+;)/i', $string, -1, PREG_SPLIT_DELIM_CAPTURE );
680
681                for ( $i = 0, $c = count( $string ); $i < $c; $i += 2 ) {
682                        $string[$i] = @htmlspecialchars( $string[$i], $quote_style, $charset );
683                }
684                $string = implode( '', $string );
685        }
686
687        // Backwards compatibility
688        if ( 'single' === $_quote_style )
689                $string = str_replace( "'", '&#039;', $string );
690
691        return $string;
692}
693
694/**
695 * Converts a number of HTML entities into their special characters.
696 *
697 * Specifically deals with: &, <, >, ", and '.
698 *
699 * $quote_style can be set to ENT_COMPAT to decode " entities,
700 * or ENT_QUOTES to do both " and '. Default is ENT_NOQUOTES where no quotes are decoded.
701 *
702 * @since 2.8.0
703 *
704 * @param string $string The text which is to be decoded.
705 * @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.
706 * @return string The decoded text without HTML entities.
707 */
708function wp_specialchars_decode( $string, $quote_style = ENT_NOQUOTES ) {
709        $string = (string) $string;
710
711        if ( 0 === strlen( $string ) ) {
712                return '';
713        }
714
715        // Don't bother if there are no entities - saves a lot of processing
716        if ( strpos( $string, '&' ) === false ) {
717                return $string;
718        }
719
720        // Match the previous behaviour of _wp_specialchars() when the $quote_style is not an accepted value
721        if ( empty( $quote_style ) ) {
722                $quote_style = ENT_NOQUOTES;
723        } elseif ( !in_array( $quote_style, array( 0, 2, 3, 'single', 'double' ), true ) ) {
724                $quote_style = ENT_QUOTES;
725        }
726
727        // More complete than get_html_translation_table( HTML_SPECIALCHARS )
728        $single = array( '&#039;'  => '\'', '&#x27;' => '\'' );
729        $single_preg = array( '/&#0*39;/'  => '&#039;', '/&#x0*27;/i' => '&#x27;' );
730        $double = array( '&quot;' => '"', '&#034;'  => '"', '&#x22;' => '"' );
731        $double_preg = array( '/&#0*34;/'  => '&#034;', '/&#x0*22;/i' => '&#x22;' );
732        $others = array( '&lt;'   => '<', '&#060;'  => '<', '&gt;'   => '>', '&#062;'  => '>', '&amp;'  => '&', '&#038;'  => '&', '&#x26;' => '&' );
733        $others_preg = array( '/&#0*60;/'  => '&#060;', '/&#0*62;/'  => '&#062;', '/&#0*38;/'  => '&#038;', '/&#x0*26;/i' => '&#x26;' );
734
735        if ( $quote_style === ENT_QUOTES ) {
736                $translation = array_merge( $single, $double, $others );
737                $translation_preg = array_merge( $single_preg, $double_preg, $others_preg );
738        } elseif ( $quote_style === ENT_COMPAT || $quote_style === 'double' ) {
739                $translation = array_merge( $double, $others );
740                $translation_preg = array_merge( $double_preg, $others_preg );
741        } elseif ( $quote_style === 'single' ) {
742                $translation = array_merge( $single, $others );
743                $translation_preg = array_merge( $single_preg, $others_preg );
744        } elseif ( $quote_style === ENT_NOQUOTES ) {
745                $translation = $others;
746                $translation_preg = $others_preg;
747        }
748
749        // Remove zero padding on numeric entities
750        $string = preg_replace( array_keys( $translation_preg ), array_values( $translation_preg ), $string );
751
752        // Replace characters according to translation table
753        return strtr( $string, $translation );
754}
755
756/**
757 * Checks for invalid UTF8 in a string.
758 *
759 * @since 2.8.0
760 *
761 * @param string $string The text which is to be checked.
762 * @param boolean $strip Optional. Whether to attempt to strip out invalid UTF8. Default is false.
763 * @return string The checked text.
764 */
765function wp_check_invalid_utf8( $string, $strip = false ) {
766        $string = (string) $string;
767
768        if ( 0 === strlen( $string ) ) {
769                return '';
770        }
771
772        // Store the site charset as a static to avoid multiple calls to get_option()
773        static $is_utf8;
774        if ( !isset( $is_utf8 ) ) {
775                $is_utf8 = in_array( get_option( 'blog_charset' ), array( 'utf8', 'utf-8', 'UTF8', 'UTF-8' ) );
776        }
777        if ( !$is_utf8 ) {
778                return $string;
779        }
780
781        // Check for support for utf8 in the installed PCRE library once and store the result in a static
782        static $utf8_pcre;
783        if ( !isset( $utf8_pcre ) ) {
784                $utf8_pcre = @preg_match( '/^./u', 'a' );
785        }
786        // We can't demand utf8 in the PCRE installation, so just return the string in those cases
787        if ( !$utf8_pcre ) {
788                return $string;
789        }
790
791        // preg_match fails when it encounters invalid UTF8 in $string
792        if ( 1 === @preg_match( '/^./us', $string ) ) {
793                return $string;
794        }
795
796        // Attempt to strip the bad chars if requested (not recommended)
797        if ( $strip && function_exists( 'iconv' ) ) {
798                return iconv( 'utf-8', 'utf-8', $string );
799        }
800
801        return '';
802}
803
804/**
805 * Encode the Unicode values to be used in the URI.
806 *
807 * @since 1.5.0
808 *
809 * @param string $utf8_string
810 * @param int $length Max length of the string
811 * @return string String with Unicode encoded for URI.
812 */
813function utf8_uri_encode( $utf8_string, $length = 0 ) {
814        $unicode = '';
815        $values = array();
816        $num_octets = 1;
817        $unicode_length = 0;
818
819        mbstring_binary_safe_encoding();
820        $string_length = strlen( $utf8_string );
821        reset_mbstring_encoding();
822
823        for ($i = 0; $i < $string_length; $i++ ) {
824
825                $value = ord( $utf8_string[ $i ] );
826
827                if ( $value < 128 ) {
828                        if ( $length && ( $unicode_length >= $length ) )
829                                break;
830                        $unicode .= chr($value);
831                        $unicode_length++;
832                } else {
833                        if ( count( $values ) == 0 ) {
834                                if ( $value < 224 ) {
835                                        $num_octets = 2;
836                                } elseif ( $value < 240 ) {
837                                        $num_octets = 3;
838                                } else {
839                                        $num_octets = 4;
840                                }
841                        }
842
843                        $values[] = $value;
844
845                        if ( $length && ( $unicode_length + ($num_octets * 3) ) > $length )
846                                break;
847                        if ( count( $values ) == $num_octets ) {
848                                for ( $j = 0; $j < $num_octets; $j++ ) {
849                                        $unicode .= '%' . dechex( $values[ $j ] );
850                                }
851
852                                $unicode_length += $num_octets * 3;
853
854                                $values = array();
855                                $num_octets = 1;
856                        }
857                }
858        }
859
860        return $unicode;
861}
862
863/**
864 * Converts all accent characters to ASCII characters.
865 *
866 * If there are no accent characters, then the string given is just returned.
867 *
868 * @since 1.2.1
869 *
870 * @param string $string Text that might have accent characters
871 * @return string Filtered string with replaced "nice" characters.
872 */
873function remove_accents($string) {
874        if ( !preg_match('/[\x80-\xff]/', $string) )
875                return $string;
876
877        if (seems_utf8($string)) {
878                $chars = array(
879                // Decompositions for Latin-1 Supplement
880                chr(194).chr(170) => 'a', chr(194).chr(186) => 'o',
881                chr(195).chr(128) => 'A', chr(195).chr(129) => 'A',
882                chr(195).chr(130) => 'A', chr(195).chr(131) => 'A',
883                chr(195).chr(132) => 'A', chr(195).chr(133) => 'A',
884                chr(195).chr(134) => 'AE',chr(195).chr(135) => 'C',
885                chr(195).chr(136) => 'E', chr(195).chr(137) => 'E',
886                chr(195).chr(138) => 'E', chr(195).chr(139) => 'E',
887                chr(195).chr(140) => 'I', chr(195).chr(141) => 'I',
888                chr(195).chr(142) => 'I', chr(195).chr(143) => 'I',
889                chr(195).chr(144) => 'D', chr(195).chr(145) => 'N',
890                chr(195).chr(146) => 'O', chr(195).chr(147) => 'O',
891                chr(195).chr(148) => 'O', chr(195).chr(149) => 'O',
892                chr(195).chr(150) => 'O', chr(195).chr(153) => 'U',
893                chr(195).chr(154) => 'U', chr(195).chr(155) => 'U',
894                chr(195).chr(156) => 'U', chr(195).chr(157) => 'Y',
895                chr(195).chr(158) => 'TH',chr(195).chr(159) => 's',
896                chr(195).chr(160) => 'a', chr(195).chr(161) => 'a',
897                chr(195).chr(162) => 'a', chr(195).chr(163) => 'a',
898                chr(195).chr(164) => 'a', chr(195).chr(165) => 'a',
899                chr(195).chr(166) => 'ae',chr(195).chr(167) => 'c',
900                chr(195).chr(168) => 'e', chr(195).chr(169) => 'e',
901                chr(195).chr(170) => 'e', chr(195).chr(171) => 'e',
902                chr(195).chr(172) => 'i', chr(195).chr(173) => 'i',
903                chr(195).chr(174) => 'i', chr(195).chr(175) => 'i',
904                chr(195).chr(176) => 'd', chr(195).chr(177) => 'n',
905                chr(195).chr(178) => 'o', chr(195).chr(179) => 'o',
906                chr(195).chr(180) => 'o', chr(195).chr(181) => 'o',
907                chr(195).chr(182) => 'o', chr(195).chr(184) => 'o',
908                chr(195).chr(185) => 'u', chr(195).chr(186) => 'u',
909                chr(195).chr(187) => 'u', chr(195).chr(188) => 'u',
910                chr(195).chr(189) => 'y', chr(195).chr(190) => 'th',
911                chr(195).chr(191) => 'y', chr(195).chr(152) => 'O',
912                // Decompositions for Latin Extended-A
913                chr(196).chr(128) => 'A', chr(196).chr(129) => 'a',
914                chr(196).chr(130) => 'A', chr(196).chr(131) => 'a',
915                chr(196).chr(132) => 'A', chr(196).chr(133) => 'a',
916                chr(196).chr(134) => 'C', chr(196).chr(135) => 'c',
917                chr(196).chr(136) => 'C', chr(196).chr(137) => 'c',
918                chr(196).chr(138) => 'C', chr(196).chr(139) => 'c',
919                chr(196).chr(140) => 'C', chr(196).chr(141) => 'c',
920                chr(196).chr(142) => 'D', chr(196).chr(143) => 'd',
921                chr(196).chr(144) => 'D', chr(196).chr(145) => 'd',
922                chr(196).chr(146) => 'E', chr(196).chr(147) => 'e',
923                chr(196).chr(148) => 'E', chr(196).chr(149) => 'e',
924                chr(196).chr(150) => 'E', chr(196).chr(151) => 'e',
925                chr(196).chr(152) => 'E', chr(196).chr(153) => 'e',
926                chr(196).chr(154) => 'E', chr(196).chr(155) => 'e',
927                chr(196).chr(156) => 'G', chr(196).chr(157) => 'g',
928                chr(196).chr(158) => 'G', chr(196).chr(159) => 'g',
929                chr(196).chr(160) => 'G', chr(196).chr(161) => 'g',
930                chr(196).chr(162) => 'G', chr(196).chr(163) => 'g',
931                chr(196).chr(164) => 'H', chr(196).chr(165) => 'h',
932                chr(196).chr(166) => 'H', chr(196).chr(167) => 'h',
933                chr(196).chr(168) => 'I', chr(196).chr(169) => 'i',
934                chr(196).chr(170) => 'I', chr(196).chr(171) => 'i',
935                chr(196).chr(172) => 'I', chr(196).chr(173) => 'i',
936                chr(196).chr(174) => 'I', chr(196).chr(175) => 'i',
937                chr(196).chr(176) => 'I', chr(196).chr(177) => 'i',
938                chr(196).chr(178) => 'IJ',chr(196).chr(179) => 'ij',
939                chr(196).chr(180) => 'J', chr(196).chr(181) => 'j',
940                chr(196).chr(182) => 'K', chr(196).chr(183) => 'k',
941                chr(196).chr(184) => 'k', chr(196).chr(185) => 'L',
942                chr(196).chr(186) => 'l', chr(196).chr(187) => 'L',
943                chr(196).chr(188) => 'l', chr(196).chr(189) => 'L',
944                chr(196).chr(190) => 'l', chr(196).chr(191) => 'L',
945                chr(197).chr(128) => 'l', chr(197).chr(129) => 'L',
946                chr(197).chr(130) => 'l', chr(197).chr(131) => 'N',
947                chr(197).chr(132) => 'n', chr(197).chr(133) => 'N',
948                chr(197).chr(134) => 'n', chr(197).chr(135) => 'N',
949                chr(197).chr(136) => 'n', chr(197).chr(137) => 'N',
950                chr(197).chr(138) => 'n', chr(197).chr(139) => 'N',
951                chr(197).chr(140) => 'O', chr(197).chr(141) => 'o',
952                chr(197).chr(142) => 'O', chr(197).chr(143) => 'o',
953                chr(197).chr(144) => 'O', chr(197).chr(145) => 'o',
954                chr(197).chr(146) => 'OE',chr(197).chr(147) => 'oe',
955                chr(197).chr(148) => 'R',chr(197).chr(149) => 'r',
956                chr(197).chr(150) => 'R',chr(197).chr(151) => 'r',
957                chr(197).chr(152) => 'R',chr(197).chr(153) => 'r',
958                chr(197).chr(154) => 'S',chr(197).chr(155) => 's',
959                chr(197).chr(156) => 'S',chr(197).chr(157) => 's',
960                chr(197).chr(158) => 'S',chr(197).chr(159) => 's',
961                chr(197).chr(160) => 'S', chr(197).chr(161) => 's',
962                chr(197).chr(162) => 'T', chr(197).chr(163) => 't',
963                chr(197).chr(164) => 'T', chr(197).chr(165) => 't',
964                chr(197).chr(166) => 'T', chr(197).chr(167) => 't',
965                chr(197).chr(168) => 'U', chr(197).chr(169) => 'u',
966                chr(197).chr(170) => 'U', chr(197).chr(171) => 'u',
967                chr(197).chr(172) => 'U', chr(197).chr(173) => 'u',
968                chr(197).chr(174) => 'U', chr(197).chr(175) => 'u',
969                chr(197).chr(176) => 'U', chr(197).chr(177) => 'u',
970                chr(197).chr(178) => 'U', chr(197).chr(179) => 'u',
971                chr(197).chr(180) => 'W', chr(197).chr(181) => 'w',
972                chr(197).chr(182) => 'Y', chr(197).chr(183) => 'y',
973                chr(197).chr(184) => 'Y', chr(197).chr(185) => 'Z',
974                chr(197).chr(186) => 'z', chr(197).chr(187) => 'Z',
975                chr(197).chr(188) => 'z', chr(197).chr(189) => 'Z',
976                chr(197).chr(190) => 'z', chr(197).chr(191) => 's',
977                // Decompositions for Latin Extended-B
978                chr(200).chr(152) => 'S', chr(200).chr(153) => 's',
979                chr(200).chr(154) => 'T', chr(200).chr(155) => 't',
980                // Euro Sign
981                chr(226).chr(130).chr(172) => 'E',
982                // GBP (Pound) Sign
983                chr(194).chr(163) => '',
984                // Vowels with diacritic (Vietnamese)
985                // unmarked
986                chr(198).chr(160) => 'O', chr(198).chr(161) => 'o',
987                chr(198).chr(175) => 'U', chr(198).chr(176) => 'u',
988                // grave accent
989                chr(225).chr(186).chr(166) => 'A', chr(225).chr(186).chr(167) => 'a',
990                chr(225).chr(186).chr(176) => 'A', chr(225).chr(186).chr(177) => 'a',
991                chr(225).chr(187).chr(128) => 'E', chr(225).chr(187).chr(129) => 'e',
992                chr(225).chr(187).chr(146) => 'O', chr(225).chr(187).chr(147) => 'o',
993                chr(225).chr(187).chr(156) => 'O', chr(225).chr(187).chr(157) => 'o',
994                chr(225).chr(187).chr(170) => 'U', chr(225).chr(187).chr(171) => 'u',
995                chr(225).chr(187).chr(178) => 'Y', chr(225).chr(187).chr(179) => 'y',
996                // hook
997                chr(225).chr(186).chr(162) => 'A', chr(225).chr(186).chr(163) => 'a',
998                chr(225).chr(186).chr(168) => 'A', chr(225).chr(186).chr(169) => 'a',
999                chr(225).chr(186).chr(178) => 'A', chr(225).chr(186).chr(179) => 'a',
1000                chr(225).chr(186).chr(186) => 'E', chr(225).chr(186).chr(187) => 'e',
1001                chr(225).chr(187).chr(130) => 'E', chr(225).chr(187).chr(131) => 'e',
1002                chr(225).chr(187).chr(136) => 'I', chr(225).chr(187).chr(137) => 'i',
1003                chr(225).chr(187).chr(142) => 'O', chr(225).chr(187).chr(143) => 'o',
1004                chr(225).chr(187).chr(148) => 'O', chr(225).chr(187).chr(149) => 'o',
1005                chr(225).chr(187).chr(158) => 'O', chr(225).chr(187).chr(159) => 'o',
1006                chr(225).chr(187).chr(166) => 'U', chr(225).chr(187).chr(167) => 'u',
1007                chr(225).chr(187).chr(172) => 'U', chr(225).chr(187).chr(173) => 'u',
1008                chr(225).chr(187).chr(182) => 'Y', chr(225).chr(187).chr(183) => 'y',
1009                // tilde
1010                chr(225).chr(186).chr(170) => 'A', chr(225).chr(186).chr(171) => 'a',
1011                chr(225).chr(186).chr(180) => 'A', chr(225).chr(186).chr(181) => 'a',
1012                chr(225).chr(186).chr(188) => 'E', chr(225).chr(186).chr(189) => 'e',
1013                chr(225).chr(187).chr(132) => 'E', chr(225).chr(187).chr(133) => 'e',
1014                chr(225).chr(187).chr(150) => 'O', chr(225).chr(187).chr(151) => 'o',
1015                chr(225).chr(187).chr(160) => 'O', chr(225).chr(187).chr(161) => 'o',
1016                chr(225).chr(187).chr(174) => 'U', chr(225).chr(187).chr(175) => 'u',
1017                chr(225).chr(187).chr(184) => 'Y', chr(225).chr(187).chr(185) => 'y',
1018                // acute accent
1019                chr(225).chr(186).chr(164) => 'A', chr(225).chr(186).chr(165) => 'a',
1020                chr(225).chr(186).chr(174) => 'A', chr(225).chr(186).chr(175) => 'a',
1021                chr(225).chr(186).chr(190) => 'E', chr(225).chr(186).chr(191) => 'e',
1022                chr(225).chr(187).chr(144) => 'O', chr(225).chr(187).chr(145) => 'o',
1023                chr(225).chr(187).chr(154) => 'O', chr(225).chr(187).chr(155) => 'o',
1024                chr(225).chr(187).chr(168) => 'U', chr(225).chr(187).chr(169) => 'u',
1025                // dot below
1026                chr(225).chr(186).chr(160) => 'A', chr(225).chr(186).chr(161) => 'a',
1027                chr(225).chr(186).chr(172) => 'A', chr(225).chr(186).chr(173) => 'a',
1028                chr(225).chr(186).chr(182) => 'A', chr(225).chr(186).chr(183) => 'a',
1029                chr(225).chr(186).chr(184) => 'E', chr(225).chr(186).chr(185) => 'e',
1030                chr(225).chr(187).chr(134) => 'E', chr(225).chr(187).chr(135) => 'e',
1031                chr(225).chr(187).chr(138) => 'I', chr(225).chr(187).chr(139) => 'i',
1032                chr(225).chr(187).chr(140) => 'O', chr(225).chr(187).chr(141) => 'o',
1033                chr(225).chr(187).chr(152) => 'O', chr(225).chr(187).chr(153) => 'o',
1034                chr(225).chr(187).chr(162) => 'O', chr(225).chr(187).chr(163) => 'o',
1035                chr(225).chr(187).chr(164) => 'U', chr(225).chr(187).chr(165) => 'u',
1036                chr(225).chr(187).chr(176) => 'U', chr(225).chr(187).chr(177) => 'u',
1037                chr(225).chr(187).chr(180) => 'Y', chr(225).chr(187).chr(181) => 'y',
1038                // Vowels with diacritic (Chinese, Hanyu Pinyin)
1039                chr(201).chr(145) => 'a',
1040                // macron
1041                chr(199).chr(149) => 'U', chr(199).chr(150) => 'u',
1042                // acute accent
1043                chr(199).chr(151) => 'U', chr(199).chr(152) => 'u',
1044                // caron
1045                chr(199).chr(141) => 'A', chr(199).chr(142) => 'a',
1046                chr(199).chr(143) => 'I', chr(199).chr(144) => 'i',
1047                chr(199).chr(145) => 'O', chr(199).chr(146) => 'o',
1048                chr(199).chr(147) => 'U', chr(199).chr(148) => 'u',
1049                chr(199).chr(153) => 'U', chr(199).chr(154) => 'u',
1050                // grave accent
1051                chr(199).chr(155) => 'U', chr(199).chr(156) => 'u',
1052                );
1053
1054                // Used for locale-specific rules
1055                $locale = get_locale();
1056
1057                if ( 'de_DE' == $locale ) {
1058                        $chars[ chr(195).chr(132) ] = 'Ae';
1059                        $chars[ chr(195).chr(164) ] = 'ae';
1060                        $chars[ chr(195).chr(150) ] = 'Oe';
1061                        $chars[ chr(195).chr(182) ] = 'oe';
1062                        $chars[ chr(195).chr(156) ] = 'Ue';
1063                        $chars[ chr(195).chr(188) ] = 'ue';
1064                        $chars[ chr(195).chr(159) ] = 'ss';
1065                } elseif ( 'da_DK' === $locale ) {
1066                        $chars[ chr(195).chr(134) ] = 'Ae';
1067                        $chars[ chr(195).chr(166) ] = 'ae';
1068                        $chars[ chr(195).chr(152) ] = 'Oe';
1069                        $chars[ chr(195).chr(184) ] = 'oe';
1070                        $chars[ chr(195).chr(133) ] = 'Aa';
1071                        $chars[ chr(195).chr(165) ] = 'aa';
1072                }
1073
1074                $string = strtr($string, $chars);
1075        } else {
1076                $chars = array();
1077                // Assume ISO-8859-1 if not UTF-8
1078                $chars['in'] = chr(128).chr(131).chr(138).chr(142).chr(154).chr(158)
1079                        .chr(159).chr(162).chr(165).chr(181).chr(192).chr(193).chr(194)
1080                        .chr(195).chr(196).chr(197).chr(199).chr(200).chr(201).chr(202)
1081                        .chr(203).chr(204).chr(205).chr(206).chr(207).chr(209).chr(210)
1082                        .chr(211).chr(212).chr(213).chr(214).chr(216).chr(217).chr(218)
1083                        .chr(219).chr(220).chr(221).chr(224).chr(225).chr(226).chr(227)
1084                        .chr(228).chr(229).chr(231).chr(232).chr(233).chr(234).chr(235)
1085                        .chr(236).chr(237).chr(238).chr(239).chr(241).chr(242).chr(243)
1086                        .chr(244).chr(245).chr(246).chr(248).chr(249).chr(250).chr(251)
1087                        .chr(252).chr(253).chr(255);
1088
1089                $chars['out'] = "EfSZszYcYuAAAAAACEEEEIIIINOOOOOOUUUUYaaaaaaceeeeiiiinoooooouuuuyy";
1090
1091                $string = strtr($string, $chars['in'], $chars['out']);
1092                $double_chars = array();
1093                $double_chars['in'] = array(chr(140), chr(156), chr(198), chr(208), chr(222), chr(223), chr(230), chr(240), chr(254));
1094                $double_chars['out'] = array('OE', 'oe', 'AE', 'DH', 'TH', 'ss', 'ae', 'dh', 'th');
1095                $string = str_replace($double_chars['in'], $double_chars['out'], $string);
1096        }
1097
1098        return $string;
1099}
1100
1101/**
1102 * Sanitizes a filename, replacing whitespace with dashes.
1103 *
1104 * Removes special characters that are illegal in filenames on certain
1105 * operating systems and special characters requiring special escaping
1106 * to manipulate at the command line. Replaces spaces and consecutive
1107 * dashes with a single dash. Trims period, dash and underscore from beginning
1108 * and end of filename.
1109 *
1110 * @since 2.1.0
1111 *
1112 * @param string $filename The filename to be sanitized
1113 * @return string The sanitized filename
1114 */
1115function sanitize_file_name( $filename ) {
1116        $filename_raw = $filename;
1117        $special_chars = array("?", "[", "]", "/", "\\", "=", "<", ">", ":", ";", ",", "'", "\"", "&", "$", "#", "*", "(", ")", "|", "~", "`", "!", "{", "}", chr(0));
1118        /**
1119         * Filter the list of characters to remove from a filename.
1120         *
1121         * @since 2.8.0
1122         *
1123         * @param array  $special_chars Characters to remove.
1124         * @param string $filename_raw  Filename as it was passed into sanitize_file_name().
1125         */
1126        $special_chars = apply_filters( 'sanitize_file_name_chars', $special_chars, $filename_raw );
1127        $filename = preg_replace( "#\x{00a0}#siu", ' ', $filename );
1128        $filename = str_replace( $special_chars, '', $filename );
1129        $filename = str_replace( array( '%20', '+' ), '-', $filename );
1130        $filename = preg_replace( '/[\r\n\t -]+/', '-', $filename );
1131        $filename = trim( $filename, '.-_' );
1132
1133        // Split the filename into a base and extension[s]
1134        $parts = explode('.', $filename);
1135
1136        // Return if only one extension
1137        if ( count( $parts ) <= 2 ) {
1138                /**
1139                 * Filter a sanitized filename string.
1140                 *
1141                 * @since 2.8.0
1142                 *
1143                 * @param string $filename     Sanitized filename.
1144                 * @param string $filename_raw The filename prior to sanitization.
1145                 */
1146                return apply_filters( 'sanitize_file_name', $filename, $filename_raw );
1147        }
1148
1149        // Process multiple extensions
1150        $filename = array_shift($parts);
1151        $extension = array_pop($parts);
1152        $mimes = get_allowed_mime_types();
1153
1154        /*
1155         * Loop over any intermediate extensions. Postfix them with a trailing underscore
1156         * if they are a 2 - 5 character long alpha string not in the extension whitelist.
1157         */
1158        foreach ( (array) $parts as $part) {
1159                $filename .= '.' . $part;
1160
1161                if ( preg_match("/^[a-zA-Z]{2,5}\d?$/", $part) ) {
1162                        $allowed = false;
1163                        foreach ( $mimes as $ext_preg => $mime_match ) {
1164                                $ext_preg = '!^(' . $ext_preg . ')$!i';
1165                                if ( preg_match( $ext_preg, $part ) ) {
1166                                        $allowed = true;
1167                                        break;
1168                                }
1169                        }
1170                        if ( !$allowed )
1171                                $filename .= '_';
1172                }
1173        }
1174        $filename .= '.' . $extension;
1175        /** This filter is documented in wp-includes/formatting.php */
1176        return apply_filters('sanitize_file_name', $filename, $filename_raw);
1177}
1178
1179/**
1180 * Sanitizes a username, stripping out unsafe characters.
1181 *
1182 * Removes tags, octets, entities, and if strict is enabled, will only keep
1183 * alphanumeric, _, space, ., -, @. After sanitizing, it passes the username,
1184 * raw username (the username in the parameter), and the value of $strict as
1185 * parameters for the 'sanitize_user' filter.
1186 *
1187 * @since 2.0.0
1188 *
1189 * @param string $username The username to be sanitized.
1190 * @param bool $strict If set limits $username to specific characters. Default false.
1191 * @return string The sanitized username, after passing through filters.
1192 */
1193function sanitize_user( $username, $strict = false ) {
1194        $raw_username = $username;
1195        $username = wp_strip_all_tags( $username );
1196        $username = remove_accents( $username );
1197        // Kill octets
1198        $username = preg_replace( '|%([a-fA-F0-9][a-fA-F0-9])|', '', $username );
1199        $username = preg_replace( '/&.+?;/', '', $username ); // Kill entities
1200
1201        // If strict, reduce to ASCII for max portability.
1202        if ( $strict )
1203                $username = preg_replace( '|[^a-z0-9 _.\-@]|i', '', $username );
1204
1205        $username = trim( $username );
1206        // Consolidate contiguous whitespace
1207        $username = preg_replace( '|\s+|', ' ', $username );
1208
1209        /**
1210         * Filter a sanitized username string.
1211         *
1212         * @since 2.0.1
1213         *
1214         * @param string $username     Sanitized username.
1215         * @param string $raw_username The username prior to sanitization.
1216         * @param bool   $strict       Whether to limit the sanitization to specific characters. Default false.
1217         */
1218        return apply_filters( 'sanitize_user', $username, $raw_username, $strict );
1219}
1220
1221/**
1222 * Sanitizes a string key.
1223 *
1224 * Keys are used as internal identifiers. Lowercase alphanumeric characters, dashes and underscores are allowed.
1225 *
1226 * @since 3.0.0
1227 *
1228 * @param string $key String key
1229 * @return string Sanitized key
1230 */
1231function sanitize_key( $key ) {
1232        $raw_key = $key;
1233        $key = strtolower( $key );
1234        $key = preg_replace( '/[^a-z0-9_\-]/', '', $key );
1235
1236        /**
1237         * Filter a sanitized key string.
1238         *
1239         * @since 3.0.0
1240         *
1241         * @param string $key     Sanitized key.
1242         * @param string $raw_key The key prior to sanitization.
1243         */
1244        return apply_filters( 'sanitize_key', $key, $raw_key );
1245}
1246
1247/**
1248 * Sanitizes a title, or returns a fallback title.
1249 *
1250 * Specifically, HTML and PHP tags are stripped. Further actions can be added
1251 * via the plugin API. If $title is empty and $fallback_title is set, the latter
1252 * will be used.
1253 *
1254 * @since 1.0.0
1255 *
1256 * @param string $title The string to be sanitized.
1257 * @param string $fallback_title Optional. A title to use if $title is empty.
1258 * @param string $context Optional. The operation for which the string is sanitized
1259 * @return string The sanitized string.
1260 */
1261function sanitize_title( $title, $fallback_title = '', $context = 'save' ) {
1262        $raw_title = $title;
1263
1264        if ( 'save' == $context )
1265                $title = remove_accents($title);
1266
1267        /**
1268         * Filter a sanitized title string.
1269         *
1270         * @since 1.2.0
1271         *
1272         * @param string $title     Sanitized title.
1273         * @param string $raw_title The title prior to sanitization.
1274         * @param string $context   The context for which the title is being sanitized.
1275         */
1276        $title = apply_filters( 'sanitize_title', $title, $raw_title, $context );
1277
1278        if ( '' === $title || false === $title )
1279                $title = $fallback_title;
1280
1281        return $title;
1282}
1283
1284/**
1285 * Sanitizes a title with the 'query' context.
1286 *
1287 * Used for querying the database for a value from URL.
1288 *
1289 * @since 3.1.0
1290 *
1291 * @param string $title The string to be sanitized.
1292 * @return string The sanitized string.
1293 */
1294function sanitize_title_for_query( $title ) {
1295        return sanitize_title( $title, '', 'query' );
1296}
1297
1298/**
1299 * Sanitizes a title, replacing whitespace and a few other characters with dashes.
1300 *
1301 * Limits the output to alphanumeric characters, underscore (_) and dash (-).
1302 * Whitespace becomes a dash.
1303 *
1304 * @since 1.2.0
1305 *
1306 * @param string $title The title to be sanitized.
1307 * @param string $raw_title Optional. Not used.
1308 * @param string $context Optional. The operation for which the string is sanitized.
1309 * @return string The sanitized title.
1310 */
1311function sanitize_title_with_dashes( $title, $raw_title = '', $context = 'display' ) {
1312        $title = strip_tags($title);
1313        // Preserve escaped octets.
1314        $title = preg_replace('|%([a-fA-F0-9][a-fA-F0-9])|', '---$1---', $title);
1315        // Remove percent signs that are not part of an octet.
1316        $title = str_replace('%', '', $title);
1317        // Restore octets.
1318        $title = preg_replace('|---([a-fA-F0-9][a-fA-F0-9])---|', '%$1', $title);
1319
1320        if (seems_utf8($title)) {
1321                if (function_exists('mb_strtolower')) {
1322                        $title = mb_strtolower($title, 'UTF-8');
1323                }
1324                $title = utf8_uri_encode($title, 200);
1325        }
1326
1327        $title = strtolower($title);
1328        $title = preg_replace('/&.+?;/', '', $title); // kill entities
1329        $title = str_replace('.', '-', $title);
1330
1331        if ( 'save' == $context ) {
1332                // Convert nbsp, ndash and mdash to hyphens
1333                $title = str_replace( array( '%c2%a0', '%e2%80%93', '%e2%80%94' ), '-', $title );
1334
1335                // Strip these characters entirely
1336                $title = str_replace( array(
1337                        // iexcl and iquest
1338                        '%c2%a1', '%c2%bf',
1339                        // angle quotes
1340                        '%c2%ab', '%c2%bb', '%e2%80%b9', '%e2%80%ba',
1341                        // curly quotes
1342                        '%e2%80%98', '%e2%80%99', '%e2%80%9c', '%e2%80%9d',
1343                        '%e2%80%9a', '%e2%80%9b', '%e2%80%9e', '%e2%80%9f',
1344                        // copy, reg, deg, hellip and trade
1345                        '%c2%a9', '%c2%ae', '%c2%b0', '%e2%80%a6', '%e2%84%a2',
1346                        // acute accents
1347                        '%c2%b4', '%cb%8a', '%cc%81', '%cd%81',
1348                        // grave accent, macron, caron
1349                        '%cc%80', '%cc%84', '%cc%8c',
1350                ), '', $title );
1351
1352                // Convert times to x
1353                $title = str_replace( '%c3%97', 'x', $title );
1354        }
1355
1356        $title = preg_replace('/[^%a-z0-9 _-]/', '', $title);
1357        $title = preg_replace('/\s+/', '-', $title);
1358        $title = preg_replace('|-+|', '-', $title);
1359        $title = trim($title, '-');
1360
1361        return $title;
1362}
1363
1364/**
1365 * Ensures a string is a valid SQL 'order by' clause.
1366 *
1367 * Accepts one or more columns, with or without a sort order (ASC / DESC).
1368 * e.g. 'column_1', 'column_1, column_2', 'column_1 ASC, column_2 DESC' etc.
1369 *
1370 * Also accepts 'RAND()'.
1371 *
1372 * @since 2.5.1
1373 *
1374 * @param string $orderby Order by clause to be validated.
1375 * @return string|bool Returns $orderby if valid, false otherwise.
1376 */
1377function sanitize_sql_orderby( $orderby ) {
1378        if ( preg_match( '/^\s*(([a-z0-9_]+|`[a-z0-9_]+`)(\s+(ASC|DESC))?\s*(,\s*(?=[a-z0-9_`])|$))+$/i', $orderby ) || preg_match( '/^\s*RAND\(\s*\)\s*$/i', $orderby ) ) {
1379                return $orderby;
1380        }
1381        return false;
1382}
1383
1384/**
1385 * Sanitizes an HTML classname to ensure it only contains valid characters.
1386 *
1387 * Strips the string down to A-Z,a-z,0-9,_,-. If this results in an empty
1388 * string then it will return the alternative value supplied.
1389 *
1390 * @todo Expand to support the full range of CDATA that a class attribute can contain.
1391 *
1392 * @since 2.8.0
1393 *
1394 * @param string $class The classname to be sanitized
1395 * @param string $fallback Optional. The value to return if the sanitization ends up as an empty string.
1396 *      Defaults to an empty string.
1397 * @return string The sanitized value
1398 */
1399function sanitize_html_class( $class, $fallback = '' ) {
1400        //Strip out any % encoded octets
1401        $sanitized = preg_replace( '|%[a-fA-F0-9][a-fA-F0-9]|', '', $class );
1402
1403        //Limit to A-Z,a-z,0-9,_,-
1404        $sanitized = preg_replace( '/[^A-Za-z0-9_-]/', '', $sanitized );
1405
1406        if ( '' == $sanitized )
1407                $sanitized = $fallback;
1408
1409        /**
1410         * Filter a sanitized HTML class string.
1411         *
1412         * @since 2.8.0
1413         *
1414         * @param string $sanitized The sanitized HTML class.
1415         * @param string $class     HTML class before sanitization.
1416         * @param string $fallback  The fallback string.
1417         */
1418        return apply_filters( 'sanitize_html_class', $sanitized, $class, $fallback );
1419}
1420
1421/**
1422 * Converts a number of characters from a string.
1423 *
1424 * Metadata tags `<title>` and `<category>` are removed, `<br>` and `<hr>` are
1425 * converted into correct XHTML and Unicode characters are converted to the
1426 * valid range.
1427 *
1428 * @since 0.71
1429 *
1430 * @param string $content String of characters to be converted.
1431 * @param string $deprecated Not used.
1432 * @return string Converted string.
1433 */
1434function convert_chars($content, $deprecated = '') {
1435        if ( !empty( $deprecated ) )
1436                _deprecated_argument( __FUNCTION__, '0.71' );
1437
1438        // Translation of invalid Unicode references range to valid range
1439        $wp_htmltranswinuni = array(
1440        '&#128;' => '&#8364;', // the Euro sign
1441        '&#129;' => '',
1442        '&#130;' => '&#8218;', // these are Windows CP1252 specific characters
1443        '&#131;' => '&#402;',  // they would look weird on non-Windows browsers
1444        '&#132;' => '&#8222;',
1445        '&#133;' => '&#8230;',
1446        '&#134;' => '&#8224;',
1447        '&#135;' => '&#8225;',
1448        '&#136;' => '&#710;',
1449        '&#137;' => '&#8240;',
1450        '&#138;' => '&#352;',
1451        '&#139;' => '&#8249;',
1452        '&#140;' => '&#338;',
1453        '&#141;' => '',
1454        '&#142;' => '&#381;',
1455        '&#143;' => '',
1456        '&#144;' => '',
1457        '&#145;' => '&#8216;',
1458        '&#146;' => '&#8217;',
1459        '&#147;' => '&#8220;',
1460        '&#148;' => '&#8221;',
1461        '&#149;' => '&#8226;',
1462        '&#150;' => '&#8211;',
1463        '&#151;' => '&#8212;',
1464        '&#152;' => '&#732;',
1465        '&#153;' => '&#8482;',
1466        '&#154;' => '&#353;',
1467        '&#155;' => '&#8250;',
1468        '&#156;' => '&#339;',
1469        '&#157;' => '',
1470        '&#158;' => '&#382;',
1471        '&#159;' => '&#376;'
1472        );
1473
1474        // Remove metadata tags
1475        $content = preg_replace('/<title>(.+?)<\/title>/','',$content);
1476        $content = preg_replace('/<category>(.+?)<\/category>/','',$content);
1477
1478        // Converts lone & characters into &#38; (a.k.a. &amp;)
1479        $content = preg_replace('/&([^#])(?![a-z1-4]{1,8};)/i', '&#038;$1', $content);
1480
1481        // Fix Word pasting
1482        $content = strtr($content, $wp_htmltranswinuni);
1483
1484        // Just a little XHTML help
1485        $content = str_replace('<br>', '<br />', $content);
1486        $content = str_replace('<hr>', '<hr />', $content);
1487
1488        return $content;
1489}
1490
1491/**
1492 * Balances tags if forced to, or if the 'use_balanceTags' option is set to true.
1493 *
1494 * @since 0.71
1495 *
1496 * @param string $text Text to be balanced
1497 * @param bool $force If true, forces balancing, ignoring the value of the option. Default false.
1498 * @return string Balanced text
1499 */
1500function balanceTags( $text, $force = false ) {
1501        if ( $force || get_option('use_balanceTags') == 1 ) {
1502                return force_balance_tags( $text );
1503        } else {
1504                return $text;
1505        }
1506}
1507
1508/**
1509 * Balances tags of string using a modified stack.
1510 *
1511 * @since 2.0.4
1512 *
1513 * @author Leonard Lin <leonard@acm.org>
1514 * @license GPL
1515 * @copyright November 4, 2001
1516 * @version 1.1
1517 * @todo Make better - change loop condition to $text in 1.2
1518 * @internal Modified by Scott Reilly (coffee2code) 02 Aug 2004
1519 *              1.1  Fixed handling of append/stack pop order of end text
1520 *                       Added Cleaning Hooks
1521 *              1.0  First Version
1522 *
1523 * @param string $text Text to be balanced.
1524 * @return string Balanced text.
1525 */
1526function force_balance_tags( $text ) {
1527        $tagstack = array();
1528        $stacksize = 0;
1529        $tagqueue = '';
1530        $newtext = '';
1531        // Known single-entity/self-closing tags
1532        $single_tags = array( 'area', 'base', 'basefont', 'br', 'col', 'command', 'embed', 'frame', 'hr', 'img', 'input', 'isindex', 'link', 'meta', 'param', 'source' );
1533        // Tags that can be immediately nested within themselves
1534        $nestable_tags = array( 'blockquote', 'div', 'object', 'q', 'span' );
1535
1536        // WP bug fix for comments - in case you REALLY meant to type '< !--'
1537        $text = str_replace('< !--', '<    !--', $text);
1538        // WP bug fix for LOVE <3 (and other situations with '<' before a number)
1539        $text = preg_replace('#<([0-9]{1})#', '&lt;$1', $text);
1540
1541        while ( preg_match("/<(\/?[\w:]*)\s*([^>]*)>/", $text, $regex) ) {
1542                $newtext .= $tagqueue;
1543
1544                $i = strpos($text, $regex[0]);
1545                $l = strlen($regex[0]);
1546
1547                // clear the shifter
1548                $tagqueue = '';
1549                // Pop or Push
1550                if ( isset($regex[1][0]) && '/' == $regex[1][0] ) { // End Tag
1551                        $tag = strtolower(substr($regex[1],1));
1552                        // if too many closing tags
1553                        if( $stacksize <= 0 ) {
1554                                $tag = '';
1555                                // or close to be safe $tag = '/' . $tag;
1556                        }
1557                        // if stacktop value = tag close value then pop
1558                        elseif ( $tagstack[$stacksize - 1] == $tag ) { // found closing tag
1559                                $tag = '</' . $tag . '>'; // Close Tag
1560                                // Pop
1561                                array_pop( $tagstack );
1562                                $stacksize--;
1563                        } else { // closing tag not at top, search for it
1564                                for ( $j = $stacksize-1; $j >= 0; $j-- ) {
1565                                        if ( $tagstack[$j] == $tag ) {
1566                                        // add tag to tagqueue
1567                                                for ( $k = $stacksize-1; $k >= $j; $k--) {
1568                                                        $tagqueue .= '</' . array_pop( $tagstack ) . '>';
1569                                                        $stacksize--;
1570                                                }
1571                                                break;
1572                                        }
1573                                }
1574                                $tag = '';
1575                        }
1576                } else { // Begin Tag
1577                        $tag = strtolower($regex[1]);
1578
1579                        // Tag Cleaning
1580
1581                        // If it's an empty tag "< >", do nothing
1582                        if ( '' == $tag ) {
1583                                // do nothing
1584                        }
1585                        // ElseIf it presents itself as a self-closing tag...
1586                        elseif ( substr( $regex[2], -1 ) == '/' ) {
1587                                // ...but it isn't a known single-entity self-closing tag, then don't let it be treated as such and
1588                                // immediately close it with a closing tag (the tag will encapsulate no text as a result)
1589                                if ( ! in_array( $tag, $single_tags ) )
1590                                        $regex[2] = trim( substr( $regex[2], 0, -1 ) ) . "></$tag";
1591                        }
1592                        // ElseIf it's a known single-entity tag but it doesn't close itself, do so
1593                        elseif ( in_array($tag, $single_tags) ) {
1594                                $regex[2] .= '/';
1595                        }
1596                        // Else it's not a single-entity tag
1597                        else {
1598                                // If the top of the stack is the same as the tag we want to push, close previous tag
1599                                if ( $stacksize > 0 && !in_array($tag, $nestable_tags) && $tagstack[$stacksize - 1] == $tag ) {
1600                                        $tagqueue = '</' . array_pop( $tagstack ) . '>';
1601                                        $stacksize--;
1602                                }
1603                                $stacksize = array_push( $tagstack, $tag );
1604                        }
1605
1606                        // Attributes
1607                        $attributes = $regex[2];
1608                        if( ! empty( $attributes ) && $attributes[0] != '>' )
1609                                $attributes = ' ' . $attributes;
1610
1611                        $tag = '<' . $tag . $attributes . '>';
1612                        //If already queuing a close tag, then put this tag on, too
1613                        if ( !empty($tagqueue) ) {
1614                                $tagqueue .= $tag;
1615                                $tag = '';
1616                        }
1617                }
1618                $newtext .= substr($text, 0, $i) . $tag;
1619                $text = substr($text, $i + $l);
1620        }
1621
1622        // Clear Tag Queue
1623        $newtext .= $tagqueue;
1624
1625        // Add Remaining text
1626        $newtext .= $text;
1627
1628        // Empty Stack
1629        while( $x = array_pop($tagstack) )
1630                $newtext .= '</' . $x . '>'; // Add remaining tags to close
1631
1632        // WP fix for the bug with HTML comments
1633        $newtext = str_replace("< !--","<!--",$newtext);
1634        $newtext = str_replace("<    !--","< !--",$newtext);
1635
1636        return $newtext;
1637}
1638
1639/**
1640 * Acts on text which is about to be edited.
1641 *
1642 * The $content is run through esc_textarea(), which uses htmlspecialchars()
1643 * to convert special characters to HTML entities. If $richedit is set to true,
1644 * it is simply a holder for the 'format_to_edit' filter.
1645 *
1646 * @since 0.71
1647 *
1648 * @param string $content The text about to be edited.
1649 * @param bool $richedit Whether the $content should not pass through htmlspecialchars(). Default false (meaning it will be passed).
1650 * @return string The text after the filter (and possibly htmlspecialchars()) has been run.
1651 */
1652function format_to_edit( $content, $richedit = false ) {
1653        /**
1654         * Filter the text to be formatted for editing.
1655         *
1656         * @since 1.2.0
1657         *
1658         * @param string $content The text, prior to formatting for editing.
1659         */
1660        $content = apply_filters( 'format_to_edit', $content );
1661        if ( ! $richedit )
1662                $content = esc_textarea( $content );
1663        return $content;
1664}
1665
1666/**
1667 * Add leading zeros when necessary.
1668 *
1669 * If you set the threshold to '4' and the number is '10', then you will get
1670 * back '0010'. If you set the threshold to '4' and the number is '5000', then you
1671 * will get back '5000'.
1672 *
1673 * Uses sprintf to append the amount of zeros based on the $threshold parameter
1674 * and the size of the number. If the number is large enough, then no zeros will
1675 * be appended.
1676 *
1677 * @since 0.71
1678 *
1679 * @param mixed $number Number to append zeros to if not greater than threshold.
1680 * @param int $threshold Digit places number needs to be to not have zeros added.
1681 * @return string Adds leading zeros to number if needed.
1682 */
1683function zeroise($number, $threshold) {
1684        return sprintf('%0'.$threshold.'s', $number);
1685}
1686
1687/**
1688 * Adds backslashes before letters and before a number at the start of a string.
1689 *
1690 * @since 0.71
1691 *
1692 * @param string $string Value to which backslashes will be added.
1693 * @return string String with backslashes inserted.
1694 */
1695function backslashit($string) {
1696        if ( isset( $string[0] ) && $string[0] >= '0' && $string[0] <= '9' )
1697                $string = '\\\\' . $string;
1698        return addcslashes( $string, 'A..Za..z' );
1699}
1700
1701/**
1702 * Appends a trailing slash.
1703 *
1704 * Will remove trailing forward and backslashes if it exists already before adding
1705 * a trailing forward slash. This prevents double slashing a string or path.
1706 *
1707 * The primary use of this is for paths and thus should be used for paths. It is
1708 * not restricted to paths and offers no specific path support.
1709 *
1710 * @since 1.2.0
1711 *
1712 * @param string $string What to add the trailing slash to.
1713 * @return string String with trailing slash added.
1714 */
1715function trailingslashit( $string ) {
1716        return untrailingslashit( $string ) . '/';
1717}
1718
1719/**
1720 * Removes trailing forward slashes and backslashes if they exist.
1721 *
1722 * The primary use of this is for paths and thus should be used for paths. It is
1723 * not restricted to paths and offers no specific path support.
1724 *
1725 * @since 2.2.0
1726 *
1727 * @param string $string What to remove the trailing slashes from.
1728 * @return string String without the trailing slashes.
1729 */
1730function untrailingslashit( $string ) {
1731        return rtrim( $string, '/\\' );
1732}
1733
1734/**
1735 * Adds slashes to escape strings.
1736 *
1737 * Slashes will first be removed if magic_quotes_gpc is set, see {@link
1738 * http://www.php.net/magic_quotes} for more details.
1739 *
1740 * @since 0.71
1741 *
1742 * @param string $gpc The string returned from HTTP request data.
1743 * @return string Returns a string escaped with slashes.
1744 */
1745function addslashes_gpc($gpc) {
1746        if ( get_magic_quotes_gpc() )
1747                $gpc = stripslashes($gpc);
1748
1749        return wp_slash($gpc);
1750}
1751
1752/**
1753 * Navigates through an array and removes slashes from the values.
1754 *
1755 * If an array is passed, the array_map() function causes a callback to pass the
1756 * value back to the function. The slashes from this value will removed.
1757 *
1758 * @since 2.0.0
1759 *
1760 * @param mixed $value The value to be stripped.
1761 * @return mixed Stripped value.
1762 */
1763function stripslashes_deep($value) {
1764        if ( is_array($value) ) {
1765                $value = array_map('stripslashes_deep', $value);
1766        } elseif ( is_object($value) ) {
1767                $vars = get_object_vars( $value );
1768                foreach ($vars as $key=>$data) {
1769                        $value->{$key} = stripslashes_deep( $data );
1770                }
1771        } elseif ( is_string( $value ) ) {
1772                $value = stripslashes($value);
1773        }
1774
1775        return $value;
1776}
1777
1778/**
1779 * Navigates through an array and encodes the values to be used in a URL.
1780 *
1781 *
1782 * @since 2.2.0
1783 *
1784 * @param array|string $value The array or string to be encoded.
1785 * @return array|string $value The encoded array (or string from the callback).
1786 */
1787function urlencode_deep($value) {
1788        $value = is_array($value) ? array_map('urlencode_deep', $value) : urlencode($value);
1789        return $value;
1790}
1791
1792/**
1793 * Navigates through an array and raw encodes the values to be used in a URL.
1794 *
1795 * @since 3.4.0
1796 *
1797 * @param array|string $value The array or string to be encoded.
1798 * @return array|string $value The encoded array (or string from the callback).
1799 */
1800function rawurlencode_deep( $value ) {
1801        return is_array( $value ) ? array_map( 'rawurlencode_deep', $value ) : rawurlencode( $value );
1802}
1803
1804/**
1805 * Converts email addresses characters to HTML entities to block spam bots.
1806 *
1807 * @since 0.71
1808 *
1809 * @param string $email_address Email address.
1810 * @param int $hex_encoding Optional. Set to 1 to enable hex encoding.
1811 * @return string Converted email address.
1812 */
1813function antispambot( $email_address, $hex_encoding = 0 ) {
1814        $email_no_spam_address = '';
1815        for ( $i = 0, $len = strlen( $email_address ); $i < $len; $i++ ) {
1816                $j = rand( 0, 1 + $hex_encoding );
1817                if ( $j == 0 ) {
1818                        $email_no_spam_address .= '&#' . ord( $email_address[$i] ) . ';';
1819                } elseif ( $j == 1 ) {
1820                        $email_no_spam_address .= $email_address[$i];
1821                } elseif ( $j == 2 ) {
1822                        $email_no_spam_address .= '%' . zeroise( dechex( ord( $email_address[$i] ) ), 2 );
1823                }
1824        }
1825
1826        $email_no_spam_address = str_replace( '@', '&#64;', $email_no_spam_address );
1827
1828        return $email_no_spam_address;
1829}
1830
1831/**
1832 * Callback to convert URI match to HTML A element.
1833 *
1834 * This function was backported from 2.5.0 to 2.3.2. Regex callback for {@link
1835 * make_clickable()}.
1836 *
1837 * @since 2.3.2
1838 * @access private
1839 *
1840 * @param array $matches Single Regex Match.
1841 * @return string HTML A element with URI address.
1842 */
1843function _make_url_clickable_cb($matches) {
1844        $url = $matches[2];
1845
1846        if ( ')' == $matches[3] && strpos( $url, '(' ) ) {
1847                // If the trailing character is a closing parethesis, and the URL has an opening parenthesis in it, add the closing parenthesis to the URL.
1848                // Then we can let the parenthesis balancer do its thing below.
1849                $url .= $matches[3];
1850                $suffix = '';
1851        } else {
1852                $suffix = $matches[3];
1853        }
1854
1855        // Include parentheses in the URL only if paired
1856        while ( substr_count( $url, '(' ) < substr_count( $url, ')' ) ) {
1857                $suffix = strrchr( $url, ')' ) . $suffix;
1858                $url = substr( $url, 0, strrpos( $url, ')' ) );
1859        }
1860
1861        $url = esc_url($url);
1862        if ( empty($url) )
1863                return $matches[0];
1864
1865        return $matches[1] . "<a href=\"$url\" rel=\"nofollow\">$url</a>" . $suffix;
1866}
1867
1868/**
1869 * Callback to convert URL match to HTML A element.
1870 *
1871 * This function was backported from 2.5.0 to 2.3.2. Regex callback for {@link
1872 * make_clickable()}.
1873 *
1874 * @since 2.3.2
1875 * @access private
1876 *
1877 * @param array $matches Single Regex Match.
1878 * @return string HTML A element with URL address.
1879 */
1880function _make_web_ftp_clickable_cb($matches) {
1881        $ret = '';
1882        $dest = $matches[2];
1883        $dest = 'http://' . $dest;
1884        $dest = esc_url($dest);
1885        if ( empty($dest) )
1886                return $matches[0];
1887
1888        // removed trailing [.,;:)] from URL
1889        if ( in_array( substr($dest, -1), array('.', ',', ';', ':', ')') ) === true ) {
1890                $ret = substr($dest, -1);
1891                $dest = substr($dest, 0, strlen($dest)-1);
1892        }
1893        return $matches[1] . "<a href=\"$dest\" rel=\"nofollow\">$dest</a>$ret";
1894}
1895
1896/**
1897 * Callback to convert email address match to HTML A element.
1898 *
1899 * This function was backported from 2.5.0 to 2.3.2. Regex callback for {@link
1900 * make_clickable()}.
1901 *
1902 * @since 2.3.2
1903 * @access private
1904 *
1905 * @param array $matches Single Regex Match.
1906 * @return string HTML A element with email address.
1907 */
1908function _make_email_clickable_cb($matches) {
1909        $email = $matches[2] . '@' . $matches[3];
1910        return $matches[1] . "<a href=\"mailto:$email\">$email</a>";
1911}
1912
1913/**
1914 * Convert plaintext URI to HTML links.
1915 *
1916 * Converts URI, www and ftp, and email addresses. Finishes by fixing links
1917 * within links.
1918 *
1919 * @since 0.71
1920 *
1921 * @param string $text Content to convert URIs.
1922 * @return string Content with converted URIs.
1923 */
1924function make_clickable( $text ) {
1925        $r = '';
1926        $textarr = preg_split( '/(<[^<>]+>)/', $text, -1, PREG_SPLIT_DELIM_CAPTURE ); // split out HTML tags
1927        $nested_code_pre = 0; // Keep track of how many levels link is nested inside <pre> or <code>
1928        foreach ( $textarr as $piece ) {
1929
1930                if ( preg_match( '|^<code[\s>]|i', $piece ) || preg_match( '|^<pre[\s>]|i', $piece ) )
1931                        $nested_code_pre++;
1932                elseif ( ( '</code>' === strtolower( $piece ) || '</pre>' === strtolower( $piece ) ) && $nested_code_pre )
1933                        $nested_code_pre--;
1934
1935                if ( $nested_code_pre || empty( $piece ) || ( $piece[0] === '<' && ! preg_match( '|^<\s*[\w]{1,20}+://|', $piece ) ) ) {
1936                        $r .= $piece;
1937                        continue;
1938                }
1939
1940                // Long strings might contain expensive edge cases ...
1941                if ( 10000 < strlen( $piece ) ) {
1942                        // ... break it up
1943                        foreach ( _split_str_by_whitespace( $piece, 2100 ) as $chunk ) { // 2100: Extra room for scheme and leading and trailing paretheses
1944                                if ( 2101 < strlen( $chunk ) ) {
1945                                        $r .= $chunk; // Too big, no whitespace: bail.
1946                                } else {
1947                                        $r .= make_clickable( $chunk );
1948                                }
1949                        }
1950                } else {
1951                        $ret = " $piece "; // Pad with whitespace to simplify the regexes
1952
1953                        $url_clickable = '~
1954                                ([\\s(<.,;:!?])                                        # 1: Leading whitespace, or punctuation
1955                                (                                                      # 2: URL
1956                                        [\\w]{1,20}+://                                # Scheme and hier-part prefix
1957                                        (?=\S{1,2000}\s)                               # Limit to URLs less than about 2000 characters long
1958                                        [\\w\\x80-\\xff#%\\~/@\\[\\]*(+=&$-]*+         # Non-punctuation URL character
1959                                        (?:                                            # Unroll the Loop: Only allow puctuation URL character if followed by a non-punctuation URL character
1960                                                [\'.,;:!?)]                            # Punctuation URL character
1961                                                [\\w\\x80-\\xff#%\\~/@\\[\\]*(+=&$-]++ # Non-punctuation URL character
1962                                        )*
1963                                )
1964                                (\)?)                                                  # 3: Trailing closing parenthesis (for parethesis balancing post processing)
1965                        ~xS'; // The regex is a non-anchored pattern and does not have a single fixed starting character.
1966                              // Tell PCRE to spend more time optimizing since, when used on a page load, it will probably be used several times.
1967
1968                        $ret = preg_replace_callback( $url_clickable, '_make_url_clickable_cb', $ret );
1969
1970                        $ret = preg_replace_callback( '#([\s>])((www|ftp)\.[\w\\x80-\\xff\#$%&~/.\-;:=,?@\[\]+]+)#is', '_make_web_ftp_clickable_cb', $ret );
1971                        $ret = preg_replace_callback( '#([\s>])([.0-9a-z_+-]+)@(([0-9a-z-]+\.)+[0-9a-z]{2,})#i', '_make_email_clickable_cb', $ret );
1972
1973                        $ret = substr( $ret, 1, -1 ); // Remove our whitespace padding.
1974                        $r .= $ret;
1975                }
1976        }
1977
1978        // Cleanup of accidental links within links
1979        $r = preg_replace( '#(<a([ \r\n\t]+[^>]+?>|>))<a [^>]+?>([^>]+?)</a></a>#i', "$1$3</a>", $r );
1980        return $r;
1981}
1982
1983/**
1984 * Breaks a string into chunks by splitting at whitespace characters.
1985 * The length of each returned chunk is as close to the specified length goal as possible,
1986 * with the caveat that each chunk includes its trailing delimiter.
1987 * Chunks longer than the goal are guaranteed to not have any inner whitespace.
1988 *
1989 * Joining the returned chunks with empty delimiters reconstructs the input string losslessly.
1990 *
1991 * Input string must have no null characters (or eventual transformations on output chunks must not care about null characters)
1992 *
1993 *     _split_str_by_whitespace( "1234 67890 1234 67890a cd 1234   890 123456789 1234567890a    45678   1 3 5 7 90 ", 10 ) ==
1994 *     array (
1995 *         0 => '1234 67890 ',  // 11 characters: Perfect split
1996 *         1 => '1234 ',        //  5 characters: '1234 67890a' was too long
1997 *         2 => '67890a cd ',   // 10 characters: '67890a cd 1234' was too long
1998 *         3 => '1234   890 ',  // 11 characters: Perfect split
1999 *         4 => '123456789 ',   // 10 characters: '123456789 1234567890a' was too long
2000 *         5 => '1234567890a ', // 12 characters: Too long, but no inner whitespace on which to split
2001 *         6 => '   45678   ',  // 11 characters: Perfect split
2002 *         7 => '1 3 5 7 90 ',  // 11 characters: End of $string
2003 *     );
2004 *
2005 * @since 3.4.0
2006 * @access private
2007 *
2008 * @param string $string The string to split.
2009 * @param int $goal The desired chunk length.
2010 * @return array Numeric array of chunks.
2011 */
2012function _split_str_by_whitespace( $string, $goal ) {
2013        $chunks = array();
2014
2015        $string_nullspace = strtr( $string, "\r\n\t\v\f ", "\000\000\000\000\000\000" );
2016
2017        while ( $goal < strlen( $string_nullspace ) ) {
2018                $pos = strrpos( substr( $string_nullspace, 0, $goal + 1 ), "\000" );
2019
2020                if ( false === $pos ) {
2021                        $pos = strpos( $string_nullspace, "\000", $goal + 1 );
2022                        if ( false === $pos ) {
2023                                break;
2024                        }
2025                }
2026
2027                $chunks[] = substr( $string, 0, $pos + 1 );
2028                $string = substr( $string, $pos + 1 );
2029                $string_nullspace = substr( $string_nullspace, $pos + 1 );
2030        }
2031
2032        if ( $string ) {
2033                $chunks[] = $string;
2034        }
2035
2036        return $chunks;
2037}
2038
2039/**
2040 * Adds rel nofollow string to all HTML A elements in content.
2041 *
2042 * @since 1.5.0
2043 *
2044 * @param string $text Content that may contain HTML A elements.
2045 * @return string Converted content.
2046 */
2047function wp_rel_nofollow( $text ) {
2048        // This is a pre save filter, so text is already escaped.
2049        $text = stripslashes($text);
2050        $text = preg_replace_callback('|<a (.+?)>|i', 'wp_rel_nofollow_callback', $text);
2051        $text = wp_slash($text);
2052        return $text;
2053}
2054
2055/**
2056 * Callback to add rel=nofollow string to HTML A element.
2057 *
2058 * Will remove already existing rel="nofollow" and rel='nofollow' from the
2059 * string to prevent from invalidating (X)HTML.
2060 *
2061 * @since 2.3.0
2062 *
2063 * @param array $matches Single Match
2064 * @return string HTML A Element with rel nofollow.
2065 */
2066function wp_rel_nofollow_callback( $matches ) {
2067        $text = $matches[1];
2068        $text = str_replace(array(' rel="nofollow"', " rel='nofollow'"), '', $text);
2069        return "<a $text rel=\"nofollow\">";
2070}
2071
2072/**
2073 * Convert one smiley code to the icon graphic file equivalent.
2074 *
2075 * Callback handler for {@link convert_smilies()}.
2076 * Looks up one smiley code in the $wpsmiliestrans global array and returns an
2077 * `<img>` string for that smiley.
2078 *
2079 * @global array $wpsmiliestrans
2080 * @since 2.8.0
2081 *
2082 * @param array $matches Single match. Smiley code to convert to image.
2083 * @return string Image string for smiley.
2084 */
2085function translate_smiley( $matches ) {
2086        global $wpsmiliestrans;
2087
2088        if ( count( $matches ) == 0 )
2089                return '';
2090
2091        $smiley = trim( reset( $matches ) );
2092        $img = $wpsmiliestrans[ $smiley ];
2093
2094        $matches = array();
2095        $ext = preg_match( '/\.([^.]+)$/', $img, $matches ) ? strtolower( $matches[1] ) : false;
2096        $image_exts = array( 'jpg', 'jpeg', 'jpe', 'gif', 'png' );
2097
2098        // Don't convert smilies that aren't images - they're probably emoji.
2099        if ( ! in_array( $ext, $image_exts ) ) {
2100                return $img;
2101        }
2102
2103        /**
2104         * Filter the Smiley image URL before it's used in the image element.
2105         *
2106         * @since 2.9.0
2107         *
2108         * @param string $smiley_url URL for the smiley image.
2109         * @param string $img        Filename for the smiley image.
2110         * @param string $site_url   Site URL, as returned by site_url().
2111         */
2112        $src_url = apply_filters( 'smilies_src', includes_url( "images/smilies/$img" ), $img, site_url() );
2113
2114        return sprintf( '<img src="%s" alt="%s" class="wp-smiley" style="height: 1em; max-height: 1em;" />', esc_url( $src_url ), esc_attr( $smiley ) );
2115}
2116
2117/**
2118 * Convert text equivalent of smilies to images.
2119 *
2120 * Will only convert smilies if the option 'use_smilies' is true and the global
2121 * used in the function isn't empty.
2122 *
2123 * @since 0.71
2124 * @uses $wp_smiliessearch
2125 *
2126 * @param string $text Content to convert smilies from text.
2127 * @return string Converted content with text smilies replaced with images.
2128 */
2129function convert_smilies( $text ) {
2130        global $wp_smiliessearch;
2131        $output = '';
2132        if ( get_option( 'use_smilies' ) && ! empty( $wp_smiliessearch ) ) {
2133                // HTML loop taken from texturize function, could possible be consolidated
2134                $textarr = preg_split( '/(<.*>)/U', $text, -1, PREG_SPLIT_DELIM_CAPTURE ); // capture the tags as well as in between
2135                $stop = count( $textarr );// loop stuff
2136
2137                // Ignore proessing of specific tags
2138                $tags_to_ignore = 'code|pre|style|script|textarea';
2139                $ignore_block_element = '';
2140
2141                for ( $i = 0; $i < $stop; $i++ ) {
2142                        $content = $textarr[$i];
2143
2144                        // If we're in an ignore block, wait until we find its closing tag
2145                        if ( '' == $ignore_block_element && preg_match( '/^<(' . $tags_to_ignore . ')>/', $content, $matches ) )  {
2146                                $ignore_block_element = $matches[1];
2147                        }
2148
2149                        // If it's not a tag and not in ignore block
2150                        if ( '' ==  $ignore_block_element && strlen( $content ) > 0 && '<' != $content[0] ) {
2151                                $content = preg_replace_callback( $wp_smiliessearch, 'translate_smiley', $content );
2152                        }
2153
2154                        // did we exit ignore block
2155                        if ( '' != $ignore_block_element && '</' . $ignore_block_element . '>' == $content )  {
2156                                $ignore_block_element = '';
2157                        }
2158
2159                        $output .= $content;
2160                }
2161        } else {
2162                // return default text.
2163                $output = $text;
2164        }
2165        return $output;
2166}
2167
2168/**
2169 * Verifies that an email is valid.
2170 *
2171 * Does not grok i18n domains. Not RFC compliant.
2172 *
2173 * @since 0.71
2174 *
2175 * @param string $email Email address to verify.
2176 * @param boolean $deprecated Deprecated.
2177 * @return string|bool Either false or the valid email address.
2178 */
2179function is_email( $email, $deprecated = false ) {
2180        if ( ! empty( $deprecated ) )
2181                _deprecated_argument( __FUNCTION__, '3.0' );
2182
2183        // Test for the minimum length the email can be
2184        if ( strlen( $email ) < 3 ) {
2185                /**
2186                 * Filter whether an email address is valid.
2187                 *
2188                 * This filter is evaluated under several different contexts, such as 'email_too_short',
2189                 * 'email_no_at', 'local_invalid_chars', 'domain_period_sequence', 'domain_period_limits',
2190                 * 'domain_no_periods', 'sub_hyphen_limits', 'sub_invalid_chars', or no specific context.
2191                 *
2192                 * @since 2.8.0
2193                 *
2194                 * @param bool   $is_email Whether the email address has passed the is_email() checks. Default false.
2195                 * @param string $email    The email address being checked.
2196                 * @param string $message  An explanatory message to the user.
2197                 * @param string $context  Context under which the email was tested.
2198                 */
2199                return apply_filters( 'is_email', false, $email, 'email_too_short' );
2200        }
2201
2202        // Test for an @ character after the first position
2203        if ( strpos( $email, '@', 1 ) === false ) {
2204                /** This filter is documented in wp-includes/formatting.php */
2205                return apply_filters( 'is_email', false, $email, 'email_no_at' );
2206        }
2207
2208        // Split out the local and domain parts
2209        list( $local, $domain ) = explode( '@', $email, 2 );
2210
2211        // LOCAL PART
2212        // Test for invalid characters
2213        if ( !preg_match( '/^[a-zA-Z0-9!#$%&\'*+\/=?^_`{|}~\.-]+$/', $local ) ) {
2214                /** This filter is documented in wp-includes/formatting.php */
2215                return apply_filters( 'is_email', false, $email, 'local_invalid_chars' );
2216        }
2217
2218        // DOMAIN PART
2219        // Test for sequences of periods
2220        if ( preg_match( '/\.{2,}/', $domain ) ) {
2221                /** This filter is documented in wp-includes/formatting.php */
2222                return apply_filters( 'is_email', false, $email, 'domain_period_sequence' );
2223        }
2224
2225        // Test for leading and trailing periods and whitespace
2226        if ( trim( $domain, " \t\n\r\0\x0B." ) !== $domain ) {
2227                /** This filter is documented in wp-includes/formatting.php */
2228                return apply_filters( 'is_email', false, $email, 'domain_period_limits' );
2229        }
2230
2231        // Split the domain into subs
2232        $subs = explode( '.', $domain );
2233
2234        // Assume the domain will have at least two subs
2235        if ( 2 > count( $subs ) ) {
2236                /** This filter is documented in wp-includes/formatting.php */
2237                return apply_filters( 'is_email', false, $email, 'domain_no_periods' );
2238        }
2239
2240        // Loop through each sub
2241        foreach ( $subs as $sub ) {
2242                // Test for leading and trailing hyphens and whitespace
2243                if ( trim( $sub, " \t\n\r\0\x0B-" ) !== $sub ) {
2244                        /** This filter is documented in wp-includes/formatting.php */
2245                        return apply_filters( 'is_email', false, $email, 'sub_hyphen_limits' );
2246                }
2247
2248                // Test for invalid characters
2249                if ( !preg_match('/^[a-z0-9-]+$/i', $sub ) ) {
2250                        /** This filter is documented in wp-includes/formatting.php */
2251                        return apply_filters( 'is_email', false, $email, 'sub_invalid_chars' );
2252                }
2253        }
2254
2255        // Congratulations your email made it!
2256        /** This filter is documented in wp-includes/formatting.php */
2257        return apply_filters( 'is_email', $email, $email, null );
2258}
2259
2260/**
2261 * Convert to ASCII from email subjects.
2262 *
2263 * @since 1.2.0
2264 *
2265 * @param string $string Subject line
2266 * @return string Converted string to ASCII
2267 */
2268function wp_iso_descrambler($string) {
2269        /* this may only work with iso-8859-1, I'm afraid */
2270        if (!preg_match('#\=\?(.+)\?Q\?(.+)\?\=#i', $string, $matches)) {
2271                return $string;
2272        } else {
2273                $subject = str_replace('_', ' ', $matches[2]);
2274                $subject = preg_replace_callback('#\=([0-9a-f]{2})#i', '_wp_iso_convert', $subject);
2275                return $subject;
2276        }
2277}
2278
2279/**
2280 * Helper function to convert hex encoded chars to ASCII
2281 *
2282 * @since 3.1.0
2283 * @access private
2284 *
2285 * @param array $match The preg_replace_callback matches array
2286 * @return string Converted chars
2287 */
2288function _wp_iso_convert( $match ) {
2289        return chr( hexdec( strtolower( $match[1] ) ) );
2290}
2291
2292/**
2293 * Returns a date in the GMT equivalent.
2294 *
2295 * Requires and returns a date in the Y-m-d H:i:s format. If there is a
2296 * timezone_string available, the date is assumed to be in that timezone,
2297 * otherwise it simply subtracts the value of the 'gmt_offset' option. Return
2298 * format can be overridden using the $format parameter.
2299 *
2300 * @since 1.2.0
2301 *
2302 * @param string $string The date to be converted.
2303 * @param string $format The format string for the returned date (default is Y-m-d H:i:s)
2304 * @return string GMT version of the date provided.
2305 */
2306function get_gmt_from_date( $string, $format = 'Y-m-d H:i:s' ) {
2307        $tz = get_option( 'timezone_string' );
2308        if ( $tz ) {
2309                $datetime = date_create( $string, new DateTimeZone( $tz ) );
2310                if ( ! $datetime )
2311                        return gmdate( $format, 0 );
2312                $datetime->setTimezone( new DateTimeZone( 'UTC' ) );
2313                $string_gmt = $datetime->format( $format );
2314        } else {
2315                if ( ! 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 ) )
2316                        return gmdate( $format, 0 );
2317                $string_time = gmmktime( $matches[4], $matches[5], $matches[6], $matches[2], $matches[3], $matches[1] );
2318                $string_gmt = gmdate( $format, $string_time - get_option( 'gmt_offset' ) * HOUR_IN_SECONDS );
2319        }
2320        return $string_gmt;
2321}
2322
2323/**
2324 * Converts a GMT date into the correct format for the blog.
2325 *
2326 * Requires and returns a date in the Y-m-d H:i:s format. If there is a
2327 * timezone_string available, the returned date is in that timezone, otherwise
2328 * it simply adds the value of gmt_offset. Return format can be overridden
2329 * using the $format parameter
2330 *
2331 * @since 1.2.0
2332 *
2333 * @param string $string The date to be converted.
2334 * @param string $format The format string for the returned date (default is Y-m-d H:i:s)
2335 * @return string Formatted date relative to the timezone / GMT offset.
2336 */
2337function get_date_from_gmt( $string, $format = 'Y-m-d H:i:s' ) {
2338        $tz = get_option( 'timezone_string' );
2339        if ( $tz ) {
2340                $datetime = date_create( $string, new DateTimeZone( 'UTC' ) );
2341                if ( ! $datetime )
2342                        return date( $format, 0 );
2343                $datetime->setTimezone( new DateTimeZone( $tz ) );
2344                $string_localtime = $datetime->format( $format );
2345        } else {
2346                if ( ! 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) )
2347                        return date( $format, 0 );
2348                $string_time = gmmktime( $matches[4], $matches[5], $matches[6], $matches[2], $matches[3], $matches[1] );
2349                $string_localtime = gmdate( $format, $string_time + get_option( 'gmt_offset' ) * HOUR_IN_SECONDS );
2350        }
2351        return $string_localtime;
2352}
2353
2354/**
2355 * Computes an offset in seconds from an iso8601 timezone.
2356 *
2357 * @since 1.5.0
2358 *
2359 * @param string $timezone Either 'Z' for 0 offset or '±hhmm'.
2360 * @return int|float The offset in seconds.
2361 */
2362function iso8601_timezone_to_offset($timezone) {
2363        // $timezone is either 'Z' or '[+|-]hhmm'
2364        if ($timezone == 'Z') {
2365                $offset = 0;
2366        } else {
2367                $sign    = (substr($timezone, 0, 1) == '+') ? 1 : -1;
2368                $hours   = intval(substr($timezone, 1, 2));
2369                $minutes = intval(substr($timezone, 3, 4)) / 60;
2370                $offset  = $sign * HOUR_IN_SECONDS * ($hours + $minutes);
2371        }
2372        return $offset;
2373}
2374
2375/**
2376 * Converts an iso8601 date to MySQL DateTime format used by post_date[_gmt].
2377 *
2378 * @since 1.5.0
2379 *
2380 * @param string $date_string Date and time in ISO 8601 format {@link http://en.wikipedia.org/wiki/ISO_8601}.
2381 * @param string $timezone Optional. If set to GMT returns the time minus gmt_offset. Default is 'user'.
2382 * @return string The date and time in MySQL DateTime format - Y-m-d H:i:s.
2383 */
2384function iso8601_to_datetime($date_string, $timezone = 'user') {
2385        $timezone = strtolower($timezone);
2386
2387        if ($timezone == 'gmt') {
2388
2389                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);
2390
2391                if (!empty($date_bits[7])) { // we have a timezone, so let's compute an offset
2392                        $offset = iso8601_timezone_to_offset($date_bits[7]);
2393                } else { // we don't have a timezone, so we assume user local timezone (not server's!)
2394                        $offset = HOUR_IN_SECONDS * get_option('gmt_offset');
2395                }
2396
2397                $timestamp = gmmktime($date_bits[4], $date_bits[5], $date_bits[6], $date_bits[2], $date_bits[3], $date_bits[1]);
2398                $timestamp -= $offset;
2399
2400                return gmdate('Y-m-d H:i:s', $timestamp);
2401
2402        } elseif ($timezone == 'user') {
2403                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);
2404        }
2405}
2406
2407/**
2408 * Adds a element attributes to open links in new windows.
2409 *
2410 * Comment text in popup windows should be filtered through this. Right now it's
2411 * a moderately dumb function, ideally it would detect whether a target or rel
2412 * attribute was already there and adjust its actions accordingly.
2413 *
2414 * @since 0.71
2415 *
2416 * @param string $text Content to replace links to open in a new window.
2417 * @return string Content that has filtered links.
2418 */
2419function popuplinks($text) {
2420        $text = preg_replace('/<a (.+?)>/i', "<a $1 target='_blank' rel='external'>", $text);
2421        return $text;
2422}
2423
2424/**
2425 * Strips out all characters that are not allowable in an email.
2426 *
2427 * @since 1.5.0
2428 *
2429 * @param string $email Email address to filter.
2430 * @return string Filtered email address.
2431 */
2432function sanitize_email( $email ) {
2433        // Test for the minimum length the email can be
2434        if ( strlen( $email ) < 3 ) {
2435                /**
2436                 * Filter a sanitized email address.
2437                 *
2438                 * This filter is evaluated under several contexts, including 'email_too_short',
2439                 * 'email_no_at', 'local_invalid_chars', 'domain_period_sequence', 'domain_period_limits',
2440                 * 'domain_no_periods', 'domain_no_valid_subs', or no context.
2441                 *
2442                 * @since 2.8.0
2443                 *
2444                 * @param string $email   The sanitized email address.
2445                 * @param string $email   The email address, as provided to sanitize_email().
2446                 * @param string $message A message to pass to the user.
2447                 */
2448                return apply_filters( 'sanitize_email', '', $email, 'email_too_short' );
2449        }
2450
2451        // Test for an @ character after the first position
2452        if ( strpos( $email, '@', 1 ) === false ) {
2453                /** This filter is documented in wp-includes/formatting.php */
2454                return apply_filters( 'sanitize_email', '', $email, 'email_no_at' );
2455        }
2456
2457        // Split out the local and domain parts
2458        list( $local, $domain ) = explode( '@', $email, 2 );
2459
2460        // LOCAL PART
2461        // Test for invalid characters
2462        $local = preg_replace( '/[^a-zA-Z0-9!#$%&\'*+\/=?^_`{|}~\.-]/', '', $local );
2463        if ( '' === $local ) {
2464                /** This filter is documented in wp-includes/formatting.php */
2465                return apply_filters( 'sanitize_email', '', $email, 'local_invalid_chars' );
2466        }
2467
2468        // DOMAIN PART
2469        // Test for sequences of periods
2470        $domain = preg_replace( '/\.{2,}/', '', $domain );
2471        if ( '' === $domain ) {
2472                /** This filter is documented in wp-includes/formatting.php */
2473                return apply_filters( 'sanitize_email', '', $email, 'domain_period_sequence' );
2474        }
2475
2476        // Test for leading and trailing periods and whitespace
2477        $domain = trim( $domain, " \t\n\r\0\x0B." );
2478        if ( '' === $domain ) {
2479                /** This filter is documented in wp-includes/formatting.php */
2480                return apply_filters( 'sanitize_email', '', $email, 'domain_period_limits' );
2481        }
2482
2483        // Split the domain into subs
2484        $subs = explode( '.', $domain );
2485
2486        // Assume the domain will have at least two subs
2487        if ( 2 > count( $subs ) ) {
2488                /** This filter is documented in wp-includes/formatting.php */
2489                return apply_filters( 'sanitize_email', '', $email, 'domain_no_periods' );
2490        }
2491
2492        // Create an array that will contain valid subs
2493        $new_subs = array();
2494
2495        // Loop through each sub
2496        foreach ( $subs as $sub ) {
2497                // Test for leading and trailing hyphens
2498                $sub = trim( $sub, " \t\n\r\0\x0B-" );
2499
2500                // Test for invalid characters
2501                $sub = preg_replace( '/[^a-z0-9-]+/i', '', $sub );
2502
2503                // If there's anything left, add it to the valid subs
2504                if ( '' !== $sub ) {
2505                        $new_subs[] = $sub;
2506                }
2507        }
2508
2509        // If there aren't 2 or more valid subs
2510        if ( 2 > count( $new_subs ) ) {
2511                /** This filter is documented in wp-includes/formatting.php */
2512                return apply_filters( 'sanitize_email', '', $email, 'domain_no_valid_subs' );
2513        }
2514
2515        // Join valid subs into the new domain
2516        $domain = join( '.', $new_subs );
2517
2518        // Put the email back together
2519        $email = $local . '@' . $domain;
2520
2521        // Congratulations your email made it!
2522        /** This filter is documented in wp-includes/formatting.php */
2523        return apply_filters( 'sanitize_email', $email, $email, null );
2524}
2525
2526/**
2527 * Determines the difference between two timestamps.
2528 *
2529 * The difference is returned in a human readable format such as "1 hour",
2530 * "5 mins", "2 days".
2531 *
2532 * @since 1.5.0
2533 *
2534 * @param int $from Unix timestamp from which the difference begins.
2535 * @param int $to Optional. Unix timestamp to end the time difference. Default becomes time() if not set.
2536 * @return string Human readable time difference.
2537 */
2538function human_time_diff( $from, $to = '' ) {
2539        if ( empty( $to ) ) {
2540                $to = time();
2541        }
2542
2543        $diff = (int) abs( $to - $from );
2544
2545        if ( $diff < HOUR_IN_SECONDS ) {
2546                $mins = round( $diff / MINUTE_IN_SECONDS );
2547                if ( $mins <= 1 )
2548                        $mins = 1;
2549                /* translators: min=minute */
2550                $since = sprintf( _n( '%s min', '%s mins', $mins ), $mins );
2551        } elseif ( $diff < DAY_IN_SECONDS && $diff >= HOUR_IN_SECONDS ) {
2552                $hours = round( $diff / HOUR_IN_SECONDS );
2553                if ( $hours <= 1 )
2554                        $hours = 1;
2555                $since = sprintf( _n( '%s hour', '%s hours', $hours ), $hours );
2556        } elseif ( $diff < WEEK_IN_SECONDS && $diff >= DAY_IN_SECONDS ) {
2557                $days = round( $diff / DAY_IN_SECONDS );
2558                if ( $days <= 1 )
2559                        $days = 1;
2560                $since = sprintf( _n( '%s day', '%s days', $days ), $days );
2561        } elseif ( $diff < 30 * DAY_IN_SECONDS && $diff >= WEEK_IN_SECONDS ) {
2562                $weeks = round( $diff / WEEK_IN_SECONDS );
2563                if ( $weeks <= 1 )
2564                        $weeks = 1;
2565                $since = sprintf( _n( '%s week', '%s weeks', $weeks ), $weeks );
2566        } elseif ( $diff < YEAR_IN_SECONDS && $diff >= 30 * DAY_IN_SECONDS ) {
2567                $months = round( $diff / ( 30 * DAY_IN_SECONDS ) );
2568                if ( $months <= 1 )
2569                        $months = 1;
2570                $since = sprintf( _n( '%s month', '%s months', $months ), $months );
2571        } elseif ( $diff >= YEAR_IN_SECONDS ) {
2572                $years = round( $diff / YEAR_IN_SECONDS );
2573                if ( $years <= 1 )
2574                        $years = 1;
2575                $since = sprintf( _n( '%s year', '%s years', $years ), $years );
2576        }
2577
2578        /**
2579         * Filter the human readable difference between two timestamps.
2580         *
2581         * @since 4.0.0
2582         *
2583         * @param string $since The difference in human readable text.
2584         * @param int    $diff  The difference in seconds.
2585         * @param int    $from  Unix timestamp from which the difference begins.
2586         * @param int    $to    Unix timestamp to end the time difference.
2587         */
2588        return apply_filters( 'human_time_diff', $since, $diff, $from, $to );
2589}
2590
2591/**
2592 * Generates an excerpt from the content, if needed.
2593 *
2594 * The excerpt word amount will be 55 words and if the amount is greater than
2595 * that, then the string ' [&hellip;]' will be appended to the excerpt. If the string
2596 * is less than 55 words, then the content will be returned as is.
2597 *
2598 * The 55 word limit can be modified by plugins/themes using the excerpt_length filter
2599 * The ' [&hellip;]' string can be modified by plugins/themes using the excerpt_more filter
2600 *
2601 * @since 1.5.0
2602 *
2603 * @param string $text Optional. The excerpt. If set to empty, an excerpt is generated.
2604 * @return string The excerpt.
2605 */
2606function wp_trim_excerpt($text = '') {
2607        $raw_excerpt = $text;
2608        if ( '' == $text ) {
2609                $text = get_the_content('');
2610
2611                $text = strip_shortcodes( $text );
2612
2613                /** This filter is documented in wp-includes/post-template.php */
2614                $text = apply_filters( 'the_content', $text );
2615                $text = str_replace(']]>', ']]&gt;', $text);
2616
2617                /**
2618                 * Filter the number of words in an excerpt.
2619                 *
2620                 * @since 2.7.0
2621                 *
2622                 * @param int $number The number of words. Default 55.
2623                 */
2624                $excerpt_length = apply_filters( 'excerpt_length', 55 );
2625                /**
2626                 * Filter the string in the "more" link displayed after a trimmed excerpt.
2627                 *
2628                 * @since 2.9.0
2629                 *
2630                 * @param string $more_string The string shown within the more link.
2631                 */
2632                $excerpt_more = apply_filters( 'excerpt_more', ' ' . '[&hellip;]' );
2633                $text = wp_trim_words( $text, $excerpt_length, $excerpt_more );
2634        }
2635        /**
2636         * Filter the trimmed excerpt string.
2637         *
2638         * @since 2.8.0
2639         *
2640         * @param string $text        The trimmed text.
2641         * @param string $raw_excerpt The text prior to trimming.
2642         */
2643        return apply_filters( 'wp_trim_excerpt', $text, $raw_excerpt );
2644}
2645
2646/**
2647 * Trims text to a certain number of words.
2648 *
2649 * This function is localized. For languages that count 'words' by the individual
2650 * character (such as East Asian languages), the $num_words argument will apply
2651 * to the number of individual characters.
2652 *
2653 * @since 3.3.0
2654 *
2655 * @param string $text Text to trim.
2656 * @param int $num_words Number of words. Default 55.
2657 * @param string $more Optional. What to append if $text needs to be trimmed. Default '&hellip;'.
2658 * @return string Trimmed text.
2659 */
2660function wp_trim_words( $text, $num_words = 55, $more = null ) {
2661        if ( null === $more )
2662                $more = __( '&hellip;' );
2663        $original_text = $text;
2664        $text = wp_strip_all_tags( $text );
2665        /* translators: If your word count is based on single characters (East Asian characters),
2666           enter 'characters'. Otherwise, enter 'words'. Do not translate into your own language. */
2667        if ( 'characters' == _x( 'words', 'word count: words or characters?' ) && preg_match( '/^utf\-?8$/i', get_option( 'blog_charset' ) ) ) {
2668                $text = trim( preg_replace( "/[\n\r\t ]+/", ' ', $text ), ' ' );
2669                preg_match_all( '/./u', $text, $words_array );
2670                $words_array = array_slice( $words_array[0], 0, $num_words + 1 );
2671                $sep = '';
2672        } else {
2673                $words_array = preg_split( "/[\n\r\t ]+/", $text, $num_words + 1, PREG_SPLIT_NO_EMPTY );
2674                $sep = ' ';
2675        }
2676        if ( count( $words_array ) > $num_words ) {
2677                array_pop( $words_array );
2678                $text = implode( $sep, $words_array );
2679                $text = $text . $more;
2680        } else {
2681                $text = implode( $sep, $words_array );
2682        }
2683        /**
2684         * Filter the text content after words have been trimmed.
2685         *
2686         * @since 3.3.0
2687         *
2688         * @param string $text          The trimmed text.
2689         * @param int    $num_words     The number of words to trim the text to. Default 5.
2690         * @param string $more          An optional string to append to the end of the trimmed text, e.g. &hellip;.
2691         * @param string $original_text The text before it was trimmed.
2692         */
2693        return apply_filters( 'wp_trim_words', $text, $num_words, $more, $original_text );
2694}
2695
2696/**
2697 * Converts named entities into numbered entities.
2698 *
2699 * @since 1.5.1
2700 *
2701 * @param string $text The text within which entities will be converted.
2702 * @return string Text with converted entities.
2703 */
2704function ent2ncr($text) {
2705
2706        /**
2707         * Filter text before named entities are converted into numbered entities.
2708         *
2709         * A non-null string must be returned for the filter to be evaluated.
2710         *
2711         * @since 3.3.0
2712         *
2713         * @param null   $converted_text The text to be converted. Default null.
2714         * @param string $text           The text prior to entity conversion.
2715         */
2716        $filtered = apply_filters( 'pre_ent2ncr', null, $text );
2717        if( null !== $filtered )
2718                return $filtered;
2719
2720        $to_ncr = array(
2721                '&quot;' => '&#34;',
2722                '&amp;' => '&#38;',
2723                '&lt;' => '&#60;',
2724                '&gt;' => '&#62;',
2725                '|' => '&#124;',
2726                '&nbsp;' => '&#160;',
2727                '&iexcl;' => '&#161;',
2728                '&cent;' => '&#162;',
2729                '&pound;' => '&#163;',
2730                '&curren;' => '&#164;',
2731                '&yen;' => '&#165;',
2732                '&brvbar;' => '&#166;',
2733                '&brkbar;' => '&#166;',
2734                '&sect;' => '&#167;',
2735                '&uml;' => '&#168;',
2736                '&die;' => '&#168;',
2737                '&copy;' => '&#169;',
2738                '&ordf;' => '&#170;',
2739                '&laquo;' => '&#171;',
2740                '&not;' => '&#172;',
2741                '&shy;' => '&#173;',
2742                '&reg;' => '&#174;',
2743                '&macr;' => '&#175;',
2744                '&hibar;' => '&#175;',
2745                '&deg;' => '&#176;',
2746                '&plusmn;' => '&#177;',
2747                '&sup2;' => '&#178;',
2748                '&sup3;' => '&#179;',
2749                '&acute;' => '&#180;',
2750                '&micro;' => '&#181;',
2751                '&para;' => '&#182;',
2752                '&middot;' => '&#183;',
2753                '&cedil;' => '&#184;',
2754                '&sup1;' => '&#185;',
2755                '&ordm;' => '&#186;',
2756                '&raquo;' => '&#187;',
2757                '&frac14;' => '&#188;',
2758                '&frac12;' => '&#189;',
2759                '&frac34;' => '&#190;',
2760                '&iquest;' => '&#191;',
2761                '&Agrave;' => '&#192;',
2762                '&Aacute;' => '&#193;',
2763                '&Acirc;' => '&#194;',
2764                '&Atilde;' => '&#195;',
2765                '&Auml;' => '&#196;',
2766                '&Aring;' => '&#197;',
2767                '&AElig;' => '&#198;',
2768                '&Ccedil;' => '&#199;',
2769                '&Egrave;' => '&#200;',
2770                '&Eacute;' => '&#201;',
2771                '&Ecirc;' => '&#202;',
2772                '&Euml;' => '&#203;',
2773                '&Igrave;' => '&#204;',
2774                '&Iacute;' => '&#205;',
2775                '&Icirc;' => '&#206;',
2776                '&Iuml;' => '&#207;',
2777                '&ETH;' => '&#208;',
2778                '&Ntilde;' => '&#209;',
2779                '&Ograve;' => '&#210;',
2780                '&Oacute;' => '&#211;',
2781                '&Ocirc;' => '&#212;',
2782                '&Otilde;' => '&#213;',
2783                '&Ouml;' => '&#214;',
2784                '&times;' => '&#215;',
2785                '&Oslash;' => '&#216;',
2786                '&Ugrave;' => '&#217;',
2787                '&Uacute;' => '&#218;',
2788                '&Ucirc;' => '&#219;',
2789                '&Uuml;' => '&#220;',
2790                '&Yacute;' => '&#221;',
2791                '&THORN;' => '&#222;',
2792                '&szlig;' => '&#223;',
2793                '&agrave;' => '&#224;',
2794                '&aacute;' => '&#225;',
2795                '&acirc;' => '&#226;',
2796                '&atilde;' => '&#227;',
2797                '&auml;' => '&#228;',
2798                '&aring;' => '&#229;',
2799                '&aelig;' => '&#230;',
2800                '&ccedil;' => '&#231;',
2801                '&egrave;' => '&#232;',
2802                '&eacute;' => '&#233;',
2803                '&ecirc;' => '&#234;',
2804                '&euml;' => '&#235;',
2805                '&igrave;' => '&#236;',
2806                '&iacute;' => '&#237;',
2807                '&icirc;' => '&#238;',
2808                '&iuml;' => '&#239;',
2809                '&eth;' => '&#240;',
2810                '&ntilde;' => '&#241;',
2811                '&ograve;' => '&#242;',
2812                '&oacute;' => '&#243;',
2813                '&ocirc;' => '&#244;',
2814                '&otilde;' => '&#245;',
2815                '&ouml;' => '&#246;',
2816                '&divide;' => '&#247;',
2817                '&oslash;' => '&#248;',
2818                '&ugrave;' => '&#249;',
2819                '&uacute;' => '&#250;',
2820                '&ucirc;' => '&#251;',
2821                '&uuml;' => '&#252;',
2822                '&yacute;' => '&#253;',
2823                '&thorn;' => '&#254;',
2824                '&yuml;' => '&#255;',
2825                '&OElig;' => '&#338;',
2826                '&oelig;' => '&#339;',
2827                '&Scaron;' => '&#352;',
2828                '&scaron;' => '&#353;',
2829                '&Yuml;' => '&#376;',
2830                '&fnof;' => '&#402;',
2831                '&circ;' => '&#710;',
2832                '&tilde;' => '&#732;',
2833                '&Alpha;' => '&#913;',
2834                '&Beta;' => '&#914;',
2835                '&Gamma;' => '&#915;',
2836                '&Delta;' => '&#916;',
2837                '&Epsilon;' => '&#917;',
2838                '&Zeta;' => '&#918;',
2839                '&Eta;' => '&#919;',
2840                '&Theta;' => '&#920;',
2841                '&Iota;' => '&#921;',
2842                '&Kappa;' => '&#922;',
2843                '&Lambda;' => '&#923;',
2844                '&Mu;' => '&#924;',
2845                '&Nu;' => '&#925;',
2846                '&Xi;' => '&#926;',
2847                '&Omicron;' => '&#927;',
2848                '&Pi;' => '&#928;',
2849                '&Rho;' => '&#929;',
2850                '&Sigma;' => '&#931;',
2851                '&Tau;' => '&#932;',
2852                '&Upsilon;' => '&#933;',
2853                '&Phi;' => '&#934;',
2854                '&Chi;' => '&#935;',
2855                '&Psi;' => '&#936;',
2856                '&Omega;' => '&#937;',
2857                '&alpha;' => '&#945;',
2858                '&beta;' => '&#946;',
2859                '&gamma;' => '&#947;',
2860                '&delta;' => '&#948;',
2861                '&epsilon;' => '&#949;',
2862                '&zeta;' => '&#950;',
2863                '&eta;' => '&#951;',
2864                '&theta;' => '&#952;',
2865                '&iota;' => '&#953;',
2866                '&kappa;' => '&#954;',
2867                '&lambda;' => '&#955;',
2868                '&mu;' => '&#956;',
2869                '&nu;' => '&#957;',
2870                '&xi;' => '&#958;',
2871                '&omicron;' => '&#959;',
2872                '&pi;' => '&#960;',
2873                '&rho;' => '&#961;',
2874                '&sigmaf;' => '&#962;',
2875                '&sigma;' => '&#963;',
2876                '&tau;' => '&#964;',
2877                '&upsilon;' => '&#965;',
2878                '&phi;' => '&#966;',
2879                '&chi;' => '&#967;',
2880                '&psi;' => '&#968;',
2881                '&omega;' => '&#969;',
2882                '&thetasym;' => '&#977;',
2883                '&upsih;' => '&#978;',
2884                '&piv;' => '&#982;',
2885                '&ensp;' => '&#8194;',
2886                '&emsp;' => '&#8195;',
2887                '&thinsp;' => '&#8201;',
2888                '&zwnj;' => '&#8204;',
2889                '&zwj;' => '&#8205;',
2890                '&lrm;' => '&#8206;',
2891                '&rlm;' => '&#8207;',
2892                '&ndash;' => '&#8211;',
2893                '&mdash;' => '&#8212;',
2894                '&lsquo;' => '&#8216;',
2895                '&rsquo;' => '&#8217;',
2896                '&sbquo;' => '&#8218;',
2897                '&ldquo;' => '&#8220;',
2898                '&rdquo;' => '&#8221;',
2899                '&bdquo;' => '&#8222;',
2900                '&dagger;' => '&#8224;',
2901                '&Dagger;' => '&#8225;',
2902                '&bull;' => '&#8226;',
2903                '&hellip;' => '&#8230;',
2904                '&permil;' => '&#8240;',
2905                '&prime;' => '&#8242;',
2906                '&Prime;' => '&#8243;',
2907                '&lsaquo;' => '&#8249;',
2908                '&rsaquo;' => '&#8250;',
2909                '&oline;' => '&#8254;',
2910                '&frasl;' => '&#8260;',
2911                '&euro;' => '&#8364;',
2912                '&image;' => '&#8465;',
2913                '&weierp;' => '&#8472;',
2914                '&real;' => '&#8476;',
2915                '&trade;' => '&#8482;',
2916                '&alefsym;' => '&#8501;',
2917                '&crarr;' => '&#8629;',
2918                '&lArr;' => '&#8656;',
2919                '&uArr;' => '&#8657;',
2920                '&rArr;' => '&#8658;',
2921                '&dArr;' => '&#8659;',
2922                '&hArr;' => '&#8660;',
2923                '&forall;' => '&#8704;',
2924                '&part;' => '&#8706;',
2925                '&exist;' => '&#8707;',
2926                '&empty;' => '&#8709;',
2927                '&nabla;' => '&#8711;',
2928                '&isin;' => '&#8712;',
2929                '&notin;' => '&#8713;',
2930                '&ni;' => '&#8715;',
2931                '&prod;' => '&#8719;',
2932                '&sum;' => '&#8721;',
2933                '&minus;' => '&#8722;',
2934                '&lowast;' => '&#8727;',
2935                '&radic;' => '&#8730;',
2936                '&prop;' => '&#8733;',
2937                '&infin;' => '&#8734;',
2938                '&ang;' => '&#8736;',
2939                '&and;' => '&#8743;',
2940                '&or;' => '&#8744;',
2941                '&cap;' => '&#8745;',
2942                '&cup;' => '&#8746;',
2943                '&int;' => '&#8747;',
2944                '&there4;' => '&#8756;',
2945                '&sim;' => '&#8764;',
2946                '&cong;' => '&#8773;',
2947                '&asymp;' => '&#8776;',
2948                '&ne;' => '&#8800;',
2949                '&equiv;' => '&#8801;',
2950                '&le;' => '&#8804;',
2951                '&ge;' => '&#8805;',
2952                '&sub;' => '&#8834;',
2953                '&sup;' => '&#8835;',
2954                '&nsub;' => '&#8836;',
2955                '&sube;' => '&#8838;',
2956                '&supe;' => '&#8839;',
2957                '&oplus;' => '&#8853;',
2958                '&otimes;' => '&#8855;',
2959                '&perp;' => '&#8869;',
2960                '&sdot;' => '&#8901;',
2961                '&lceil;' => '&#8968;',
2962                '&rceil;' => '&#8969;',
2963                '&lfloor;' => '&#8970;',
2964                '&rfloor;' => '&#8971;',
2965                '&lang;' => '&#9001;',
2966                '&rang;' => '&#9002;',
2967                '&larr;' => '&#8592;',
2968                '&uarr;' => '&#8593;',
2969                '&rarr;' => '&#8594;',
2970                '&darr;' => '&#8595;',
2971                '&harr;' => '&#8596;',
2972                '&loz;' => '&#9674;',
2973                '&spades;' => '&#9824;',
2974                '&clubs;' => '&#9827;',
2975                '&hearts;' => '&#9829;',
2976                '&diams;' => '&#9830;'
2977        );
2978
2979        return str_replace( array_keys($to_ncr), array_values($to_ncr), $text );
2980}
2981
2982/**
2983 * Formats text for the rich text editor.
2984 *
2985 * The filter 'richedit_pre' is applied here. If $text is empty the filter will
2986 * be applied to an empty string.
2987 *
2988 * @since 2.0.0
2989 *
2990 * @param string $text The text to be formatted.
2991 * @return string The formatted text after filter is applied.
2992 */
2993function wp_richedit_pre($text) {
2994        if ( empty( $text ) ) {
2995                /**
2996                 * Filter text returned for the rich text editor.
2997                 *
2998                 * This filter is first evaluated, and the value returned, if an empty string
2999                 * is passed to wp_richedit_pre(). If an empty string is passed, it results
3000                 * in a break tag and line feed.
3001                 *
3002                 * If a non-empty string is passed, the filter is evaluated on the wp_richedit_pre()
3003                 * return after being formatted.
3004                 *
3005                 * @since 2.0.0
3006                 *
3007                 * @param string $output Text for the rich text editor.
3008                 */
3009                return apply_filters( 'richedit_pre', '' );
3010        }
3011
3012        $output = convert_chars($text);
3013        $output = wpautop($output);
3014        $output = htmlspecialchars($output, ENT_NOQUOTES, get_option( 'blog_charset' ) );
3015
3016        /** This filter is documented in wp-includes/formatting.php */
3017        return apply_filters( 'richedit_pre', $output );
3018}
3019
3020/**
3021 * Formats text for the HTML editor.
3022 *
3023 * Unless $output is empty it will pass through htmlspecialchars before the
3024 * 'htmledit_pre' filter is applied.
3025 *
3026 * @since 2.5.0
3027 *
3028 * @param string $output The text to be formatted.
3029 * @return string Formatted text after filter applied.
3030 */
3031function wp_htmledit_pre($output) {
3032        if ( !empty($output) )
3033                $output = htmlspecialchars($output, ENT_NOQUOTES, get_option( 'blog_charset' ) ); // convert only < > &
3034
3035        /**
3036         * Filter the text before it is formatted for the HTML editor.
3037         *
3038         * @since 2.5.0
3039         *
3040         * @param string $output The HTML-formatted text.
3041         */
3042        return apply_filters( 'htmledit_pre', $output );
3043}
3044
3045/**
3046 * Perform a deep string replace operation to ensure the values in $search are no longer present
3047 *
3048 * Repeats the replacement operation until it no longer replaces anything so as to remove "nested" values
3049 * e.g. $subject = '%0%0%0DDD', $search ='%0D', $result ='' rather than the '%0%0DD' that
3050 * str_replace would return
3051 *
3052 * @since 2.8.1
3053 * @access private
3054 *
3055 * @param string|array $search The value being searched for, otherwise known as the needle. An array may be used to designate multiple needles.
3056 * @param string $subject The string being searched and replaced on, otherwise known as the haystack.
3057 * @return string The string with the replaced svalues.
3058 */
3059function _deep_replace( $search, $subject ) {
3060        $subject = (string) $subject;
3061
3062        $count = 1;
3063        while ( $count ) {
3064                $subject = str_replace( $search, '', $subject, $count );
3065        }
3066
3067        return $subject;
3068}
3069
3070/**
3071 * Escapes data for use in a MySQL query.
3072 *
3073 * Usually you should prepare queries using wpdb::prepare().
3074 * Sometimes, spot-escaping is required or useful. One example
3075 * is preparing an array for use in an IN clause.
3076 *
3077 * @since 2.8.0
3078 * @param string|array $data Unescaped data
3079 * @return string|array Escaped data
3080 */
3081function esc_sql( $data ) {
3082        global $wpdb;
3083        return $wpdb->_escape( $data );
3084}
3085
3086/**
3087 * Checks and cleans a URL.
3088 *
3089 * A number of characters are removed from the URL. If the URL is for displaying
3090 * (the default behaviour) ampersands are also replaced. The 'clean_url' filter
3091 * is applied to the returned cleaned URL.
3092 *
3093 * @since 2.8.0
3094 *
3095 * @param string $url The URL to be cleaned.
3096 * @param array $protocols Optional. An array of acceptable protocols.
3097 *              Defaults to 'http', 'https', 'ftp', 'ftps', 'mailto', 'news', 'irc', 'gopher', 'nntp', 'feed', 'telnet', 'mms', 'rtsp', 'svn' if not set.
3098 * @param string $_context Private. Use esc_url_raw() for database usage.
3099 * @return string The cleaned $url after the 'clean_url' filter is applied.
3100 */
3101function esc_url( $url, $protocols = null, $_context = 'display' ) {
3102        $original_url = $url;
3103
3104        if ( '' == $url )
3105                return $url;
3106        $url = preg_replace('|[^a-z0-9-~+_.?#=!&;,/:%@$\|*\'()\\x80-\\xff]|i', '', $url);
3107        $strip = array('%0d', '%0a', '%0D', '%0A');
3108        $url = _deep_replace($strip, $url);
3109        $url = str_replace(';//', '://', $url);
3110        /* If the URL doesn't appear to contain a scheme, we
3111         * presume it needs http:// appended (unless a relative
3112         * link starting with /, # or ? or a php file).
3113         */
3114        if ( strpos($url, ':') === false && ! in_array( $url[0], array( '/', '#', '?' ) ) &&
3115                ! preg_match('/^[a-z0-9-]+?\.php/i', $url) )
3116                $url = 'http://' . $url;
3117
3118        // Replace ampersands and single quotes only when displaying.
3119        if ( 'display' == $_context ) {
3120                $url = wp_kses_normalize_entities( $url );
3121                $url = str_replace( '&amp;', '&#038;', $url );
3122                $url = str_replace( "'", '&#039;', $url );
3123        }
3124
3125        if ( '/' === $url[0] ) {
3126                $good_protocol_url = $url;
3127        } else {
3128                if ( ! is_array( $protocols ) )
3129                        $protocols = wp_allowed_protocols();
3130                $good_protocol_url = wp_kses_bad_protocol( $url, $protocols );
3131                if ( strtolower( $good_protocol_url ) != strtolower( $url ) )
3132                        return '';
3133        }
3134
3135        /**
3136         * Filter a string cleaned and escaped for output as a URL.
3137         *
3138         * @since 2.3.0
3139         *
3140         * @param string $good_protocol_url The cleaned URL to be returned.
3141         * @param string $original_url      The URL prior to cleaning.
3142         * @param string $_context          If 'display', replace ampersands and single quotes only.
3143         */
3144        return apply_filters( 'clean_url', $good_protocol_url, $original_url, $_context );
3145}
3146
3147/**
3148 * Performs esc_url() for database usage.
3149 *
3150 * @since 2.8.0
3151 *
3152 * @param string $url The URL to be cleaned.
3153 * @param array $protocols An array of acceptable protocols.
3154 * @return string The cleaned URL.
3155 */
3156function esc_url_raw( $url, $protocols = null ) {
3157        return esc_url( $url, $protocols, 'db' );
3158}
3159
3160/**
3161 * Convert entities, while preserving already-encoded entities.
3162 *
3163 * @link http://www.php.net/htmlentities Borrowed from the PHP Manual user notes.
3164 *
3165 * @since 1.2.2
3166 *
3167 * @param string $myHTML The text to be converted.
3168 * @return string Converted text.
3169 */
3170function htmlentities2($myHTML) {
3171        $translation_table = get_html_translation_table( HTML_ENTITIES, ENT_QUOTES );
3172        $translation_table[chr(38)] = '&';
3173        return preg_replace( "/&(?![A-Za-z]{0,4}\w{2,3};|#[0-9]{2,3};)/", "&amp;", strtr($myHTML, $translation_table) );
3174}
3175
3176/**
3177 * Escape single quotes, htmlspecialchar " < > &, and fix line endings.
3178 *
3179 * Escapes text strings for echoing in JS. It is intended to be used for inline JS
3180 * (in a tag attribute, for example onclick="..."). Note that the strings have to
3181 * be in single quotes. The filter 'js_escape' is also applied here.
3182 *
3183 * @since 2.8.0
3184 *
3185 * @param string $text The text to be escaped.
3186 * @return string Escaped text.
3187 */
3188function esc_js( $text ) {
3189        $safe_text = wp_check_invalid_utf8( $text );
3190        $safe_text = _wp_specialchars( $safe_text, ENT_COMPAT );
3191        $safe_text = preg_replace( '/&#(x)?0*(?(1)27|39);?/i', "'", stripslashes( $safe_text ) );
3192        $safe_text = str_replace( "\r", '', $safe_text );
3193        $safe_text = str_replace( "\n", '\\n', addslashes( $safe_text ) );
3194        /**
3195         * Filter a string cleaned and escaped for output in JavaScript.
3196         *
3197         * Text passed to esc_js() is stripped of invalid or special characters,
3198         * and properly slashed for output.
3199         *
3200         * @since 2.0.6
3201         *
3202         * @param string $safe_text The text after it has been escaped.
3203         * @param string $text      The text prior to being escaped.
3204         */
3205        return apply_filters( 'js_escape', $safe_text, $text );
3206}
3207
3208/**
3209 * Escaping for HTML blocks.
3210 *
3211 * @since 2.8.0
3212 *
3213 * @param string $text
3214 * @return string
3215 */
3216function esc_html( $text ) {
3217        $safe_text = wp_check_invalid_utf8( $text );
3218        $safe_text = _wp_specialchars( $safe_text, ENT_QUOTES );
3219        /**
3220         * Filter a string cleaned and escaped for output in HTML.
3221         *
3222         * Text passed to esc_html() is stripped of invalid or special characters
3223         * before output.
3224         *
3225         * @since 2.8.0
3226         *
3227         * @param string $safe_text The text after it has been escaped.
3228         * @param string $text      The text prior to being escaped.
3229         */
3230        return apply_filters( 'esc_html', $safe_text, $text );
3231}
3232
3233/**
3234 * Escaping for HTML attributes.
3235 *
3236 * @since 2.8.0
3237 *
3238 * @param string $text
3239 * @return string
3240 */
3241function esc_attr( $text ) {
3242        $safe_text = wp_check_invalid_utf8( $text );
3243        $safe_text = _wp_specialchars( $safe_text, ENT_QUOTES );
3244        /**
3245         * Filter a string cleaned and escaped for output in an HTML attribute.
3246         *
3247         * Text passed to esc_attr() is stripped of invalid or special characters
3248         * before output.
3249         *
3250         * @since 2.0.6
3251         *
3252         * @param string $safe_text The text after it has been escaped.
3253         * @param string $text      The text prior to being escaped.
3254         */
3255        return apply_filters( 'attribute_escape', $safe_text, $text );
3256}
3257
3258/**
3259 * Escaping for textarea values.
3260 *
3261 * @since 3.1.0
3262 *
3263 * @param string $text
3264 * @return string
3265 */
3266function esc_textarea( $text ) {
3267        $safe_text = htmlspecialchars( $text, ENT_QUOTES, get_option( 'blog_charset' ) );
3268        /**
3269         * Filter a string cleaned and escaped for output in a textarea element.
3270         *
3271         * @since 3.1.0
3272         *
3273         * @param string $safe_text The text after it has been escaped.
3274         * @param string $text      The text prior to being escaped.
3275         */
3276        return apply_filters( 'esc_textarea', $safe_text, $text );
3277}
3278
3279/**
3280 * Escape an HTML tag name.
3281 *
3282 * @since 2.5.0
3283 *
3284 * @param string $tag_name
3285 * @return string
3286 */
3287function tag_escape($tag_name) {
3288        $safe_tag = strtolower( preg_replace('/[^a-zA-Z0-9_:]/', '', $tag_name) );
3289        /**
3290         * Filter a string cleaned and escaped for output as an HTML tag.
3291         *
3292         * @since 2.8.0
3293         *
3294         * @param string $safe_tag The tag name after it has been escaped.
3295         * @param string $tag_name The text before it was escaped.
3296         */
3297        return apply_filters( 'tag_escape', $safe_tag, $tag_name );
3298}
3299
3300/**
3301 * Convert full URL paths to absolute paths.
3302 *
3303 * Removes the http or https protocols and the domain. Keeps the path '/' at the
3304 * beginning, so it isn't a true relative link, but from the web root base.
3305 *
3306 * @since 2.1.0
3307 * @since 4.1.0 Support was added for relative URLs.
3308 *
3309 * @param string $link Full URL path.
3310 * @return string Absolute path.
3311 */
3312function wp_make_link_relative( $link ) {
3313        return preg_replace( '|^(https?:)?//[^/]+(/.*)|i', '$2', $link );
3314}
3315
3316/**
3317 * Sanitises various option values based on the nature of the option.
3318 *
3319 * This is basically a switch statement which will pass $value through a number
3320 * of functions depending on the $option.
3321 *
3322 * @since 2.0.5
3323 *
3324 * @param string $option The name of the option.
3325 * @param string $value The unsanitised value.
3326 * @return string Sanitized value.
3327 */
3328function sanitize_option($option, $value) {
3329        global $wpdb;
3330
3331        switch ( $option ) {
3332                case 'admin_email' :
3333                case 'new_admin_email' :
3334                        $value = $wpdb->strip_invalid_text_for_column( $wpdb->options, 'option_value', $value );
3335                        $value = sanitize_email( $value );
3336                        if ( ! is_email( $value ) ) {
3337                                $value = get_option( $option ); // Resets option to stored value in the case of failed sanitization
3338                                if ( function_exists( 'add_settings_error' ) )
3339                                        add_settings_error( $option, 'invalid_admin_email', __( 'The email address entered did not appear to be a valid email address. Please enter a valid email address.' ) );
3340                        }
3341                        break;
3342
3343                case 'thumbnail_size_w':
3344                case 'thumbnail_size_h':
3345                case 'medium_size_w':
3346                case 'medium_size_h':
3347                case 'large_size_w':
3348                case 'large_size_h':
3349                case 'mailserver_port':
3350                case 'comment_max_links':
3351                case 'page_on_front':
3352                case 'page_for_posts':
3353                case 'rss_excerpt_length':
3354                case 'default_category':
3355                case 'default_email_category':
3356                case 'default_link_category':
3357                case 'close_comments_days_old':
3358                case 'comments_per_page':
3359                case 'thread_comments_depth':
3360                case 'users_can_register':
3361                case 'start_of_week':
3362                        $value = absint( $value );
3363                        break;
3364
3365                case 'posts_per_page':
3366                case 'posts_per_rss':
3367                        $value = (int) $value;
3368                        if ( empty($value) )
3369                                $value = 1;
3370                        if ( $value < -1 )
3371                                $value = abs($value);
3372                        break;
3373
3374                case 'default_ping_status':
3375                case 'default_comment_status':
3376                        // Options that if not there have 0 value but need to be something like "closed"
3377                        if ( $value == '0' || $value == '')
3378                                $value = 'closed';
3379                        break;
3380
3381                case 'blogdescription':
3382                case 'blogname':
3383                        $value = $wpdb->strip_invalid_text_for_column( $wpdb->options, 'option_value', $value );
3384                        $value = wp_kses_post( $value );
3385                        $value = esc_html( $value );
3386                        break;
3387
3388                case 'blog_charset':
3389                        $value = preg_replace('/[^a-zA-Z0-9_-]/', '', $value); // strips slashes
3390                        break;
3391
3392                case 'blog_public':
3393                        // This is the value if the settings checkbox is not checked on POST. Don't rely on this.
3394                        if ( null === $value )
3395                                $value = 1;
3396                        else
3397                                $value = intval( $value );
3398                        break;
3399
3400                case 'date_format':
3401                case 'time_format':
3402                case 'mailserver_url':
3403                case 'mailserver_login':
3404                case 'mailserver_pass':
3405                case 'upload_path':
3406                        $value = $wpdb->strip_invalid_text_for_column( $wpdb->options, 'option_value', $value );
3407                        $value = strip_tags( $value );
3408                        $value = wp_kses_data( $value );
3409                        break;
3410
3411                case 'ping_sites':
3412                        $value = explode( "\n", $value );
3413                        $value = array_filter( array_map( 'trim', $value ) );
3414                        $value = array_filter( array_map( 'esc_url_raw', $value ) );
3415                        $value = implode( "\n", $value );
3416                        break;
3417
3418                case 'gmt_offset':
3419                        $value = preg_replace('/[^0-9:.-]/', '', $value); // strips slashes
3420                        break;
3421
3422                case 'siteurl':
3423                        $value = $wpdb->strip_invalid_text_for_column( $wpdb->options, 'option_value', $value );
3424                        if ( (bool)preg_match( '#http(s?)://(.+)#i', $value) ) {
3425                                $value = esc_url_raw($value);
3426                        } else {
3427                                $value = get_option( $option ); // Resets option to stored value in the case of failed sanitization
3428                                if ( function_exists('add_settings_error') )
3429                                        add_settings_error('siteurl', 'invalid_siteurl', __('The WordPress address you entered did not appear to be a valid URL. Please enter a valid URL.'));
3430                        }
3431                        break;
3432
3433                case 'home':
3434                        $value = $wpdb->strip_invalid_text_for_column( $wpdb->options, 'option_value', $value );
3435                        if ( (bool)preg_match( '#http(s?)://(.+)#i', $value) ) {
3436                                $value = esc_url_raw($value);
3437                        } else {
3438                                $value = get_option( $option ); // Resets option to stored value in the case of failed sanitization
3439                                if ( function_exists('add_settings_error') )
3440                                        add_settings_error('home', 'invalid_home', __('The Site address you entered did not appear to be a valid URL. Please enter a valid URL.'));
3441                        }
3442                        break;
3443
3444                case 'WPLANG':
3445                        $allowed = get_available_languages();
3446                        if ( ! is_multisite() && defined( 'WPLANG' ) && '' !== WPLANG && 'en_US' !== WPLANG ) {
3447                                $allowed[] = WPLANG;
3448                        }
3449                        if ( ! in_array( $value, $allowed ) && ! empty( $value ) ) {
3450                                $value = get_option( $option );
3451                        }
3452                        break;
3453
3454                case 'illegal_names':
3455                        $value = $wpdb->strip_invalid_text_for_column( $wpdb->options, 'option_value', $value );
3456                        if ( ! is_array( $value ) )
3457                                $value = explode( ' ', $value );
3458
3459                        $value = array_values( array_filter( array_map( 'trim', $value ) ) );
3460
3461                        if ( ! $value )
3462                                $value = '';
3463                        break;
3464
3465                case 'limited_email_domains':
3466                case 'banned_email_domains':
3467                        $value = $wpdb->strip_invalid_text_for_column( $wpdb->options, 'option_value', $value );
3468                        if ( ! is_array( $value ) )
3469                                $value = explode( "\n", $value );
3470
3471                        $domains = array_values( array_filter( array_map( 'trim', $value ) ) );
3472                        $value = array();
3473
3474                        foreach ( $domains as $domain ) {
3475                                if ( ! preg_match( '/(--|\.\.)/', $domain ) && preg_match( '|^([a-zA-Z0-9-\.])+$|', $domain ) )
3476                                        $value[] = $domain;
3477                        }
3478                        if ( ! $value )
3479                                $value = '';
3480                        break;
3481
3482                case 'timezone_string':
3483                        $allowed_zones = timezone_identifiers_list();
3484                        if ( ! in_array( $value, $allowed_zones ) && ! empty( $value ) ) {
3485                                $value = get_option( $option ); // Resets option to stored value in the case of failed sanitization
3486                                if ( function_exists('add_settings_error') )
3487                                        add_settings_error('timezone_string', 'invalid_timezone_string', __('The timezone you have entered is not valid. Please select a valid timezone.') );
3488                        }
3489                        break;
3490
3491                case 'permalink_structure':
3492                case 'category_base':
3493                case 'tag_base':
3494                        $value = $wpdb->strip_invalid_text_for_column( $wpdb->options, 'option_value', $value );
3495                        $value = esc_url_raw( $value );
3496                        $value = str_replace( 'http://', '', $value );
3497                        break;
3498
3499                case 'default_role' :
3500                        if ( ! get_role( $value ) && get_role( 'subscriber' ) )
3501                                $value = 'subscriber';
3502                        break;
3503
3504                case 'moderation_keys':
3505                case 'blacklist_keys':
3506                        $value = $wpdb->strip_invalid_text_for_column( $wpdb->options, 'option_value', $value );
3507                        $value = explode( "\n", $value );
3508                        $value = array_filter( array_map( 'trim', $value ) );
3509                        $value = array_unique( $value );
3510                        $value = implode( "\n", $value );
3511                        break;
3512        }
3513
3514        /**
3515         * Filter an option value following sanitization.
3516         *
3517         * @since 2.3.0
3518         *
3519         * @param string $value  The sanitized option value.
3520         * @param string $option The option name.
3521         */
3522        $value = apply_filters( "sanitize_option_{$option}", $value, $option );
3523
3524        return $value;
3525}
3526
3527/**
3528 * Parses a string into variables to be stored in an array.
3529 *
3530 * Uses {@link http://www.php.net/parse_str parse_str()} and stripslashes if
3531 * {@link http://www.php.net/magic_quotes magic_quotes_gpc} is on.
3532 *
3533 * @since 2.2.1
3534 *
3535 * @param string $string The string to be parsed.
3536 * @param array $array Variables will be stored in this array.
3537 */
3538function wp_parse_str( $string, &$array ) {
3539        parse_str( $string, $array );
3540        if ( get_magic_quotes_gpc() )
3541                $array = stripslashes_deep( $array );
3542        /**
3543         * Filter the array of variables derived from a parsed string.
3544         *
3545         * @since 2.3.0
3546         *
3547         * @param array $array The array populated with variables.
3548         */
3549        $array = apply_filters( 'wp_parse_str', $array );
3550}
3551
3552/**
3553 * Convert lone less than signs.
3554 *
3555 * KSES already converts lone greater than signs.
3556 *
3557 * @since 2.3.0
3558 *
3559 * @param string $text Text to be converted.
3560 * @return string Converted text.
3561 */
3562function wp_pre_kses_less_than( $text ) {
3563        return preg_replace_callback('%<[^>]*?((?=<)|>|$)%', 'wp_pre_kses_less_than_callback', $text);
3564}
3565
3566/**
3567 * Callback function used by preg_replace.
3568 *
3569 * @since 2.3.0
3570 *
3571 * @param array $matches Populated by matches to preg_replace.
3572 * @return string The text returned after esc_html if needed.
3573 */
3574function wp_pre_kses_less_than_callback( $matches ) {
3575        if ( false === strpos($matches[0], '>') )
3576                return esc_html($matches[0]);
3577        return $matches[0];
3578}
3579
3580/**
3581 * WordPress implementation of PHP sprintf() with filters.
3582 *
3583 * @since 2.5.0
3584 * @link http://www.php.net/sprintf
3585 *
3586 * @param string $pattern The string which formatted args are inserted.
3587 * @param mixed  $args ,... Arguments to be formatted into the $pattern string.
3588 * @return string The formatted string.
3589 */
3590function wp_sprintf( $pattern ) {
3591        $args = func_get_args();
3592        $len = strlen($pattern);
3593        $start = 0;
3594        $result = '';
3595        $arg_index = 0;
3596        while ( $len > $start ) {
3597                // Last character: append and break
3598                if ( strlen($pattern) - 1 == $start ) {
3599                        $result .= substr($pattern, -1);
3600                        break;
3601                }
3602
3603                // Literal %: append and continue
3604                if ( substr($pattern, $start, 2) == '%%' ) {
3605                        $start += 2;
3606                        $result .= '%';
3607                        continue;
3608                }
3609
3610                // Get fragment before next %
3611                $end = strpos($pattern, '%', $start + 1);
3612                if ( false === $end )
3613                        $end = $len;
3614                $fragment = substr($pattern, $start, $end - $start);
3615
3616                // Fragment has a specifier
3617                if ( $pattern[$start] == '%' ) {
3618                        // Find numbered arguments or take the next one in order
3619                        if ( preg_match('/^%(\d+)\$/', $fragment, $matches) ) {
3620                                $arg = isset($args[$matches[1]]) ? $args[$matches[1]] : '';
3621                                $fragment = str_replace("%{$matches[1]}$", '%', $fragment);
3622                        } else {
3623                                ++$arg_index;
3624                                $arg = isset($args[$arg_index]) ? $args[$arg_index] : '';
3625                        }
3626
3627                        /**
3628                         * Filter a fragment from the pattern passed to wp_sprintf().
3629                         *
3630                         * If the fragment is unchanged, then sprintf() will be run on the fragment.
3631                         *
3632                         * @since 2.5.0
3633                         *
3634                         * @param string $fragment A fragment from the pattern.
3635                         * @param string $arg      The argument.
3636                         */
3637                        $_fragment = apply_filters( 'wp_sprintf', $fragment, $arg );
3638                        if ( $_fragment != $fragment )
3639                                $fragment = $_fragment;
3640                        else
3641                                $fragment = sprintf($fragment, strval($arg) );
3642                }
3643
3644                // Append to result and move to next fragment
3645                $result .= $fragment;
3646                $start = $end;
3647        }
3648        return $result;
3649}
3650
3651/**
3652 * Localize list items before the rest of the content.
3653 *
3654 * The '%l' must be at the first characters can then contain the rest of the
3655 * content. The list items will have ', ', ', and', and ' and ' added depending
3656 * on the amount of list items in the $args parameter.
3657 *
3658 * @since 2.5.0
3659 *
3660 * @param string $pattern Content containing '%l' at the beginning.
3661 * @param array $args List items to prepend to the content and replace '%l'.
3662 * @return string Localized list items and rest of the content.
3663 */
3664function wp_sprintf_l($pattern, $args) {
3665        // Not a match
3666        if ( substr($pattern, 0, 2) != '%l' )
3667                return $pattern;
3668
3669        // Nothing to work with
3670        if ( empty($args) )
3671                return '';
3672
3673        /**
3674         * Filter the translated delimiters used by wp_sprintf_l().
3675         * Placeholders (%s) are included to assist translators and then
3676         * removed before the array of strings reaches the filter.
3677         *
3678         * Please note: Ampersands and entities should be avoided here.
3679         *
3680         * @since 2.5.0
3681         *
3682         * @param array $delimiters An array of translated delimiters.
3683         */
3684        $l = apply_filters( 'wp_sprintf_l', array(
3685                /* translators: used to join items in a list with more than 2 items */
3686                'between'          => sprintf( __('%s, %s'), '', '' ),
3687                /* translators: used to join last two items in a list with more than 2 times */
3688                'between_last_two' => sprintf( __('%s, and %s'), '', '' ),
3689                /* translators: used to join items in a list with only 2 items */
3690                'between_only_two' => sprintf( __('%s and %s'), '', '' ),
3691        ) );
3692
3693        $args = (array) $args;
3694        $result = array_shift($args);
3695        if ( count($args) == 1 )
3696                $result .= $l['between_only_two'] . array_shift($args);
3697        // Loop when more than two args
3698        $i = count($args);
3699        while ( $i ) {
3700                $arg = array_shift($args);
3701                $i--;
3702                if ( 0 == $i )
3703                        $result .= $l['between_last_two'] . $arg;
3704                else
3705                        $result .= $l['between'] . $arg;
3706        }
3707        return $result . substr($pattern, 2);
3708}
3709
3710/**
3711 * Safely extracts not more than the first $count characters from html string.
3712 *
3713 * UTF-8, tags and entities safe prefix extraction. Entities inside will *NOT*
3714 * be counted as one character. For example &amp; will be counted as 4, &lt; as
3715 * 3, etc.
3716 *
3717 * @since 2.5.0
3718 *
3719 * @param string $str String to get the excerpt from.
3720 * @param integer $count Maximum number of characters to take.
3721 * @param string $more Optional. What to append if $str needs to be trimmed. Defaults to empty string.
3722 * @return string The excerpt.
3723 */
3724function wp_html_excerpt( $str, $count, $more = null ) {
3725        if ( null === $more )
3726                $more = '';
3727        $str = wp_strip_all_tags( $str, true );
3728        $excerpt = mb_substr( $str, 0, $count );
3729        // remove part of an entity at the end
3730        $excerpt = preg_replace( '/&[^;\s]{0,6}$/', '', $excerpt );
3731        if ( $str != $excerpt )
3732                $excerpt = trim( $excerpt ) . $more;
3733        return $excerpt;
3734}
3735
3736/**
3737 * Add a Base url to relative links in passed content.
3738 *
3739 * By default it supports the 'src' and 'href' attributes. However this can be
3740 * changed via the 3rd param.
3741 *
3742 * @since 2.7.0
3743 *
3744 * @param string $content String to search for links in.
3745 * @param string $base The base URL to prefix to links.
3746 * @param array $attrs The attributes which should be processed.
3747 * @return string The processed content.
3748 */
3749function links_add_base_url( $content, $base, $attrs = array('src', 'href') ) {
3750        global $_links_add_base;
3751        $_links_add_base = $base;
3752        $attrs = implode('|', (array)$attrs);
3753        return preg_replace_callback( "!($attrs)=(['\"])(.+?)\\2!i", '_links_add_base', $content );
3754}
3755
3756/**
3757 * Callback to add a base url to relative links in passed content.
3758 *
3759 * @since 2.7.0
3760 * @access private
3761 *
3762 * @param string $m The matched link.
3763 * @return string The processed link.
3764 */
3765function _links_add_base($m) {
3766        global $_links_add_base;
3767        //1 = attribute name  2 = quotation mark  3 = URL
3768        return $m[1] . '=' . $m[2] .
3769                ( preg_match( '#^(\w{1,20}):#', $m[3], $protocol ) && in_array( $protocol[1], wp_allowed_protocols() ) ?
3770                        $m[3] :
3771                        WP_HTTP::make_absolute_url( $m[3], $_links_add_base )
3772                )
3773                . $m[2];
3774}
3775
3776/**
3777 * Adds a Target attribute to all links in passed content.
3778 *
3779 * This function by default only applies to `<a>` tags, however this can be
3780 * modified by the 3rd param.
3781 *
3782 * *NOTE:* Any current target attributed will be stripped and replaced.
3783 *
3784 * @since 2.7.0
3785 *
3786 * @param string $content String to search for links in.
3787 * @param string $target The Target to add to the links.
3788 * @param array $tags An array of tags to apply to.
3789 * @return string The processed content.
3790 */
3791function links_add_target( $content, $target = '_blank', $tags = array('a') ) {
3792        global $_links_add_target;
3793        $_links_add_target = $target;
3794        $tags = implode('|', (array)$tags);
3795        return preg_replace_callback( "!<($tags)([^>]*)>!i", '_links_add_target', $content );
3796}
3797
3798/**
3799 * Callback to add a target attribute to all links in passed content.
3800 *
3801 * @since 2.7.0
3802 * @access private
3803 *
3804 * @param string $m The matched link.
3805 * @return string The processed link.
3806 */
3807function _links_add_target( $m ) {
3808        global $_links_add_target;
3809        $tag = $m[1];
3810        $link = preg_replace('|( target=([\'"])(.*?)\2)|i', '', $m[2]);
3811        return '<' . $tag . $link . ' target="' . esc_attr( $_links_add_target ) . '">';
3812}
3813
3814/**
3815 * Normalize EOL characters and strip duplicate whitespace.
3816 *
3817 * @since 2.7.0
3818 *
3819 * @param string $str The string to normalize.
3820 * @return string The normalized string.
3821 */
3822function normalize_whitespace( $str ) {
3823        $str  = trim( $str );
3824        $str  = str_replace( "\r", "\n", $str );
3825        $str  = preg_replace( array( '/\n+/', '/[ \t]+/' ), array( "\n", ' ' ), $str );
3826        return $str;
3827}
3828
3829/**
3830 * Properly strip all HTML tags including script and style
3831 *
3832 * This differs from strip_tags() because it removes the contents of
3833 * the `<script>` and `<style>` tags. E.g. `strip_tags( '<script>something</script>' )`
3834 * will return 'something'. wp_strip_all_tags will return ''
3835 *
3836 * @since 2.9.0
3837 *
3838 * @param string $string String containing HTML tags
3839 * @param bool $remove_breaks optional Whether to remove left over line breaks and white space chars
3840 * @return string The processed string.
3841 */
3842function wp_strip_all_tags($string, $remove_breaks = false) {
3843        $string = preg_replace( '@<(script|style)[^>]*?>.*?</\\1>@si', '', $string );
3844        $string = strip_tags($string);
3845
3846        if ( $remove_breaks )
3847                $string = preg_replace('/[\r\n\t ]+/', ' ', $string);
3848
3849        return trim( $string );
3850}
3851
3852/**
3853 * Sanitize a string from user input or from the db
3854 *
3855 * check for invalid UTF-8,
3856 * Convert single < characters to entity,
3857 * strip all tags,
3858 * remove line breaks, tabs and extra white space,
3859 * strip octets.
3860 *
3861 * @since 2.9.0
3862 *
3863 * @param string $str
3864 * @return string
3865 */
3866function sanitize_text_field($str) {
3867        $filtered = wp_check_invalid_utf8( $str );
3868
3869        if ( strpos($filtered, '<') !== false ) {
3870                $filtered = wp_pre_kses_less_than( $filtered );
3871                // This will strip extra whitespace for us.
3872                $filtered = wp_strip_all_tags( $filtered, true );
3873        } else {
3874                $filtered = trim( preg_replace('/[\r\n\t ]+/', ' ', $filtered) );
3875        }
3876
3877        $found = false;
3878        while ( preg_match('/%[a-f0-9]{2}/i', $filtered, $match) ) {
3879                $filtered = str_replace($match[0], '', $filtered);
3880                $found = true;
3881        }
3882
3883        if ( $found ) {
3884                // Strip out the whitespace that may now exist after removing the octets.
3885                $filtered = trim( preg_replace('/ +/', ' ', $filtered) );
3886        }
3887
3888        /**
3889         * Filter a sanitized text field string.
3890         *
3891         * @since 2.9.0
3892         *
3893         * @param string $filtered The sanitized string.
3894         * @param string $str      The string prior to being sanitized.
3895         */
3896        return apply_filters( 'sanitize_text_field', $filtered, $str );
3897}
3898
3899/**
3900 * i18n friendly version of basename()
3901 *
3902 * @since 3.1.0
3903 *
3904 * @param string $path A path.
3905 * @param string $suffix If the filename ends in suffix this will also be cut off.
3906 * @return string
3907 */
3908function wp_basename( $path, $suffix = '' ) {
3909        return urldecode( basename( str_replace( array( '%2F', '%5C' ), '/', urlencode( $path ) ), $suffix ) );
3910}
3911
3912/**
3913 * Forever eliminate "Wordpress" from the planet (or at least the little bit we can influence).
3914 *
3915 * Violating our coding standards for a good function name.
3916 *
3917 * @since 3.0.0
3918 */
3919function capital_P_dangit( $text ) {
3920        // Simple replacement for titles
3921        $current_filter = current_filter();
3922        if ( 'the_title' === $current_filter || 'wp_title' === $current_filter )
3923                return str_replace( 'Wordpress', 'WordPress', $text );
3924        // Still here? Use the more judicious replacement
3925        static $dblq = false;
3926        if ( false === $dblq )
3927                $dblq = _x( '&#8220;', 'opening curly double quote' );
3928        return str_replace(
3929                array( ' Wordpress', '&#8216;Wordpress', $dblq . 'Wordpress', '>Wordpress', '(Wordpress' ),
3930                array( ' WordPress', '&#8216;WordPress', $dblq . 'WordPress', '>WordPress', '(WordPress' ),
3931        $text );
3932
3933}
3934
3935/**
3936 * Sanitize a mime type
3937 *
3938 * @since 3.1.3
3939 *
3940 * @param string $mime_type Mime type
3941 * @return string Sanitized mime type
3942 */
3943function sanitize_mime_type( $mime_type ) {
3944        $sani_mime_type = preg_replace( '/[^-+*.a-zA-Z0-9\/]/', '', $mime_type );
3945        /**
3946         * Filter a mime type following sanitization.
3947         *
3948         * @since 3.1.3
3949         *
3950         * @param string $sani_mime_type The sanitized mime type.
3951         * @param string $mime_type      The mime type prior to sanitization.
3952         */
3953        return apply_filters( 'sanitize_mime_type', $sani_mime_type, $mime_type );
3954}
3955
3956/**
3957 * Sanitize space or carriage return separated URLs that are used to send trackbacks.
3958 *
3959 * @since 3.4.0
3960 *
3961 * @param string $to_ping Space or carriage return separated URLs
3962 * @return string URLs starting with the http or https protocol, separated by a carriage return.
3963 */
3964function sanitize_trackback_urls( $to_ping ) {
3965        $urls_to_ping = preg_split( '/[\r\n\t ]/', trim( $to_ping ), -1, PREG_SPLIT_NO_EMPTY );
3966        foreach ( $urls_to_ping as $k => $url ) {
3967                if ( !preg_match( '#^https?://.#i', $url ) )
3968                        unset( $urls_to_ping[$k] );
3969        }
3970        $urls_to_ping = array_map( 'esc_url_raw', $urls_to_ping );
3971        $urls_to_ping = implode( "\n", $urls_to_ping );
3972        /**
3973         * Filter a list of trackback URLs following sanitization.
3974         *
3975         * The string returned here consists of a space or carriage return-delimited list
3976         * of trackback URLs.
3977         *
3978         * @since 3.4.0
3979         *
3980         * @param string $urls_to_ping Sanitized space or carriage return separated URLs.
3981         * @param string $to_ping      Space or carriage return separated URLs before sanitization.
3982         */
3983        return apply_filters( 'sanitize_trackback_urls', $urls_to_ping, $to_ping );
3984}
3985
3986/**
3987 * Add slashes to a string or array of strings.
3988 *
3989 * This should be used when preparing data for core API that expects slashed data.
3990 * This should not be used to escape data going directly into an SQL query.
3991 *
3992 * @since 3.6.0
3993 *
3994 * @param string|array $value String or array of strings to slash.
3995 * @return string|array Slashed $value
3996 */
3997function wp_slash( $value ) {
3998        if ( is_array( $value ) ) {
3999                foreach ( $value as $k => $v ) {
4000                        if ( is_array( $v ) ) {
4001                                $value[$k] = wp_slash( $v );
4002                        } else {
4003                                $value[$k] = addslashes( $v );
4004                        }
4005                }
4006        } else {
4007                $value = addslashes( $value );
4008        }
4009
4010        return $value;
4011}
4012
4013/**
4014 * Remove slashes from a string or array of strings.
4015 *
4016 * This should be used to remove slashes from data passed to core API that
4017 * expects data to be unslashed.
4018 *
4019 * @since 3.6.0
4020 *
4021 * @param string|array $value String or array of strings to unslash.
4022 * @return string|array Unslashed $value
4023 */
4024function wp_unslash( $value ) {
4025        return stripslashes_deep( $value );
4026}
4027
4028/**
4029 * Extract and return the first URL from passed content.
4030 *
4031 * @since 3.6.0
4032 *
4033 * @param string $content A string which might contain a URL.
4034 * @return string The found URL.
4035 */
4036function get_url_in_content( $content ) {
4037        if ( empty( $content ) ) {
4038                return false;
4039        }
4040
4041        if ( preg_match( '/<a\s[^>]*?href=([\'"])(.+?)\1/is', $content, $matches ) ) {
4042                return esc_url_raw( $matches[2] );
4043        }
4044
4045        return false;
4046}
4047
4048/**
4049 * Returns the regexp for common whitespace characters.
4050 *
4051 * By default, spaces include new lines, tabs, nbsp entities, and the UTF-8 nbsp.
4052 * This is designed to replace the PCRE \s sequence.  In ticket #22692, that
4053 * sequence was found to be unreliable due to random inclusion of the A0 byte.
4054 *
4055 * @since 4.0.0
4056 *
4057 * @return string The spaces regexp.
4058 */
4059function wp_spaces_regexp() {
4060        static $spaces;
4061
4062        if ( empty( $spaces ) ) {
4063                /**
4064                 * Filter the regexp for common whitespace characters.
4065                 *
4066                 * This string is substituted for the \s sequence as needed in regular
4067                 * expressions. For websites not written in English, different characters
4068                 * may represent whitespace. For websites not encoded in UTF-8, the 0xC2 0xA0
4069                 * sequence may not be in use.
4070                 *
4071                 * @since 4.0.0
4072                 *
4073                 * @param string $spaces Regexp pattern for matching common whitespace characters.
4074                 */
4075                $spaces = apply_filters( 'wp_spaces_regexp', '[\r\n\t ]|\xC2\xA0|&nbsp;' );
4076        }
4077
4078        return $spaces;
4079}
4080
4081/**
4082 * Print the important emoji-related styles.
4083 *
4084 * @since 4.2.0
4085 */
4086function print_emoji_styles() {
4087        static $printed = false;
4088
4089        if ( $printed || !( get_option( 'use_smilies' ) ) ) {
4090                return;
4091        }
4092
4093        $printed = true;
4094?>
4095<style type="text/css">
4096img.wp-smiley,
4097img.emoji {
4098        display: inline !important;
4099        border: none !important;
4100        box-shadow: none !important;
4101        height: 1em !important;
4102        width: 1em !important;
4103        margin: 0 .07em !important;
4104        vertical-align: -0.1em !important;
4105        background: none !important;
4106        padding: 0 !important;
4107}
4108</style>
4109<?php
4110}
4111
4112function print_emoji_detection_script() {
4113        global $wp_version;
4114        static $printed = false;
4115
4116        if ( $printed || !( get_option( 'use_smilies' ) ) ) {
4117                return;
4118        }
4119
4120        $printed = true;
4121
4122        $settings = array(
4123                /**
4124                 * Filter the URL where emoji images are hosted.
4125                 *
4126                 * @since 4.2.0
4127                 *
4128                 * @param string The emoji base URL.
4129                 */
4130                'baseUrl' => apply_filters( 'emoji_url', set_url_scheme( '//s.w.org/images/core/emoji/72x72/' ) ),
4131
4132                /**
4133                 * Filter the extension of the emoji files.
4134                 *
4135                 * @since 4.2.0
4136                 *
4137                 * @param string The emoji extension. Default .png.
4138                 */
4139                'ext' => apply_filters( 'emoji_ext', '.png' ),
4140        );
4141
4142        $version = 'ver=' . $wp_version;
4143
4144        if ( SCRIPT_DEBUG ) {
4145                $settings['source'] = array(
4146                        /** This filter is documented in wp-includes/class.wp-scripts.php */
4147                        'wpemoji' => apply_filters( 'script_loader_src', includes_url( "js/wp-emoji.js?$version" ), 'wpemoji' ),
4148                        /** This filter is documented in wp-includes/class.wp-scripts.php */
4149                        'twemoji' => apply_filters( 'script_loader_src', includes_url( "js/twemoji.js?$version" ), 'twemoji' ),
4150                );
4151
4152                ?>
4153                <script type="text/javascript">
4154                        window._wpemojiSettings = <?php echo wp_json_encode( $settings ); ?>;
4155                        <?php readfile( ABSPATH . WPINC . "/js/wp-emoji-loader.js" ); ?>
4156                </script>
4157                <?php
4158        } else {
4159                $settings['source'] = array(
4160                        /** This filter is documented in wp-includes/class.wp-scripts.php */
4161                        'concatemoji' => apply_filters( 'script_loader_src', includes_url( "js/wp-emoji-release.min.js?$version" ), 'concatemoji' ),
4162                );
4163
4164                /*
4165                 * If you're looking at a src version of this file, you'll see an "include"
4166                 * statement below. This is used by the `grunt build` process to directly
4167                 * include a minified version of wp-emoji-loader.js, instead of using the
4168                 * readfile() method from above.
4169                 *
4170                 * If you're looking at a build version of this file, you'll see a string of
4171                 * minified JavaScript. If you need to debug it, please turn on SCRIPT_DEBUG
4172                 * and edit wp-emoji-loader.js directly.
4173                 */
4174                ?>
4175                <script type="text/javascript">
4176                        window._wpemojiSettings = <?php echo wp_json_encode( $settings ); ?>;
4177                        !function(a,b,c){function d(a){var c=b.createElement("canvas"),d=c.getContext&&c.getContext("2d");return d&&d.fillText?(d.textBaseline="top",d.font="600 32px Arial","flag"===a?(d.fillText(String.fromCharCode(55356,56812,55356,56807),0,0),c.toDataURL().length>3e3):(d.fillText(String.fromCharCode(55357,56835),0,0),0!==d.getImageData(16,16,1,1).data[0])):!1}function e(a){var c=b.createElement("script");c.src=a,c.type="text/javascript",b.getElementsByTagName("head")[0].appendChild(c)}var f;c.supports={simple:d("simple"),flag:d("flag")},c.supports.simple&&c.supports.flag||(f=c.source||{},f.concatemoji?e(f.concatemoji):f.wpemoji&&f.twemoji&&(e(f.twemoji),e(f.wpemoji)))}(window,document,window._wpemojiSettings);
4178                </script>
4179                <?php
4180        }
4181}
4182
4183/**
4184 * Convert any 4 byte emoji in a string to their equivalent HTML entity.
4185 *
4186 * Currently, only Unicode 7 emoji are supported. Skin tone modifiers are allowed,
4187 * all other Unicode 8 emoji will be added when the spec is finalised.
4188 *
4189 * This allows us to store emoji in a DB using the utf8 character set.
4190 *
4191 * @since 4.2.0
4192 *
4193 * @param string $content The content to encode.
4194 * @return string The encoded content.
4195 */
4196function wp_encode_emoji( $content ) {
4197        if ( function_exists( 'mb_convert_encoding' ) ) {
4198                $regex = '/(
4199                     \x23\xE2\x83\xA3               # Digits
4200                     [\x30-\x39]\xE2\x83\xA3
4201                   | \xF0\x9F[\x85-\x88][\xA6-\xBF] # Enclosed characters
4202                   | \xF0\x9F[\x8C-\x97][\x80-\xBF] # Misc
4203                   | \xF0\x9F\x98[\x80-\xBF]        # Smilies
4204                   | \xF0\x9F\x99[\x80-\x8F]
4205                   | \xF0\x9F\x9A[\x80-\xBF]        # Transport and map symbols
4206                )/x';
4207
4208                $matches = array();
4209                if ( preg_match_all( $regex, $content, $matches ) ) {
4210                        if ( ! empty( $matches[1] ) ) {
4211                                foreach( $matches[1] as $emoji ) {
4212                                        /*
4213                                         * UTF-32's hex encoding is the same as HTML's hex encoding.
4214                                         * So, by converting the emoji from UTF-8 to UTF-32, we magically
4215                                         * get the correct hex encoding.
4216                                         */
4217                                        $unpacked = unpack( 'H*', mb_convert_encoding( $emoji, 'UTF-32', 'UTF-8' ) );
4218                                        if ( isset( $unpacked[1] ) ) {
4219                                                $entity = '&#x' . ltrim( $unpacked[1], '0' ) . ';';
4220                                                $content = str_replace( $emoji, $entity, $content );
4221                                        }
4222                                }
4223                        }
4224                }
4225        }
4226
4227        return $content;
4228}
4229
4230/**
4231 * Convert emoji to a static img element.
4232 *
4233 * @since 4.2.0
4234 *
4235 * @param string $text The content to encode.
4236 * @return string The encoded content.
4237 */
4238function wp_staticize_emoji( $text ) {
4239        $text = wp_encode_emoji( $text );
4240
4241        /** This filter is documented in wp-includes/formatting.php */
4242        $cdn_url = apply_filters( 'emoji_url', set_url_scheme( '//s.w.org/images/core/emoji/72x72/' ) );
4243
4244        /** This filter is documented in wp-includes/formatting.php */
4245        $ext = apply_filters( 'emoji_ext', '.png' );
4246
4247        $output = '';
4248        /*
4249         * HTML loop taken from smiley function, which was taken from texturize function.
4250         * It'll never be consolidated.
4251         *
4252         * First, capture the tags as well as in between.
4253         */
4254        $textarr = preg_split( '/(<.*>)/U', $text, -1, PREG_SPLIT_DELIM_CAPTURE );
4255        $stop = count( $textarr );
4256
4257        // Ignore processing of specific tags.
4258        $tags_to_ignore = 'code|pre|style|script|textarea';
4259        $ignore_block_element = '';
4260
4261        for ( $i = 0; $i < $stop; $i++ ) {
4262                $content = $textarr[$i];
4263
4264                // If we're in an ignore block, wait until we find its closing tag.
4265                if ( '' == $ignore_block_element && preg_match( '/^<(' . $tags_to_ignore . ')>/', $content, $matches ) )  {
4266                        $ignore_block_element = $matches[1];
4267                }
4268
4269                // If it's not a tag and not in ignore block.
4270                if ( '' ==  $ignore_block_element && strlen( $content ) > 0 && '<' != $content[0] ) {
4271                        $matches = array();
4272                        if ( preg_match_all( '/(&#x1f1(e[6-9a-f]|f[0-9a-f]);){2}/', $content, $matches ) ) {
4273                                if ( ! empty( $matches[0] ) ) {
4274                                        foreach ( $matches[0] as $flag ) {
4275                                                $chars = str_replace( array( '&#x', ';'), '', $flag );
4276
4277                                                list( $char1, $char2 ) = str_split( $chars, 5 );
4278                                                $entity = sprintf( '<img src="%s" alt="%s" class="wp-smiley" style="height: 1em; max-height: 1em;" />', $cdn_url . $char1 . '-' . $char2 . $ext, html_entity_decode( $flag ) );
4279
4280                                                $content = str_replace( $flag, $entity, $content );
4281                                        }
4282                                }
4283                        }
4284
4285                        // Loosely match the Emoji Unicode range.
4286                        $regex = '/(&#x[2-3][0-9a-f]{3};|&#x1f[1-6][0-9a-f]{2};)/';
4287
4288                        $matches = array();
4289                        if ( preg_match_all( $regex, $content, $matches ) ) {
4290                                if ( ! empty( $matches[1] ) ) {
4291                                        foreach ( $matches[1] as $emoji ) {
4292                                                $char = str_replace( array( '&#x', ';'), '', $emoji );
4293                                                $entity = sprintf( '<img src="%s" alt="%s" class="wp-smiley" style="height: 1em; max-height: 1em;" />', $cdn_url . $char . $ext, html_entity_decode( $emoji ) );
4294
4295                                                $content = str_replace( $emoji, $entity, $content );
4296                                        }
4297                                }
4298                        }
4299                }
4300
4301                // Did we exit ignore block.
4302                if ( '' != $ignore_block_element && '</' . $ignore_block_element . '>' == $content )  {
4303                        $ignore_block_element = '';
4304                }
4305
4306                $output .= $content;
4307        }
4308
4309        return $output;
4310}
4311
4312/**
4313 * Convert emoji in emails into static images.
4314 *
4315 * @since 4.2.0
4316 *
4317 * @param array $mail The email data array.
4318 * @return array The email data array, with emoji in the message staticized.
4319 */
4320function wp_staticize_emoji_for_email( $mail ) {
4321        if ( ! isset( $mail['message'] ) ) {
4322                return $mail;
4323        }
4324
4325        /*
4326         * We can only transform the emoji into images if it's a text/html email.
4327         * To do that, here's a cut down version of the same process that happens
4328         * in wp_mail() - get the Content-Type from the headers, if there is one,
4329         * then pass it through the wp_mail_content_type filter, in case a plugin
4330         * is handling changing the Content-Type.
4331         */
4332        $headers = array();
4333        if ( isset( $mail['headers'] ) ) {
4334                if ( is_array( $mail['headers'] ) ) {
4335                        $headers = $mail['headers'];
4336                } else {
4337                        $headers = explode( "\n", str_replace( "\r\n", "\n", $mail['headers'] ) );
4338                }
4339        }
4340
4341        foreach ( $headers as $header ) {
4342                if ( strpos($header, ':') === false ) {
4343                        continue;
4344                }
4345
4346                // Explode them out.
4347                list( $name, $content ) = explode( ':', trim( $header ), 2 );
4348
4349                // Cleanup crew.
4350                $name    = trim( $name    );
4351                $content = trim( $content );
4352
4353                if ( 'content-type' === strtolower( $name ) ) {
4354                        if ( strpos( $content, ';' ) !== false ) {
4355                                list( $type, $charset ) = explode( ';', $content );
4356                                $content_type = trim( $type );
4357                        } else {
4358                                $content_type = trim( $content );
4359                        }
4360                        break;
4361                }
4362        }
4363
4364        // Set Content-Type if we don't have a content-type from the input headers.
4365        if ( ! isset( $content_type ) ) {
4366                $content_type = 'text/plain';
4367        }
4368
4369        /** This filter is documented in wp-includes/pluggable.php */
4370        $content_type = apply_filters( 'wp_mail_content_type', $content_type );
4371
4372        if ( 'text/html' === $content_type ) {
4373                $mail['message'] = wp_staticize_emoji( $mail['message'] );
4374        }
4375
4376        return $mail;
4377}