3 * Main WordPress Formatting API.
5 * Handles many functions for formatting output.
11 * Replaces common plain text characters into formatted entities
15 * 'cause today's effort makes it worth tomorrow's "holiday" ...
19 * ’cause today’s effort makes it worth tomorrow’s “holiday” …
21 * Code within certain html blocks are skipped.
24 * @uses $wp_cockneyreplace Array of formatted entities for certain common phrases
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
30 function 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;
35 // If there's nothing to do, just stop.
36 if ( empty( $text ) || false === $run_texturize ) {
40 // Set up static variables. Run once only.
41 if ( $reset || ! isset( $static_characters ) ) {
43 * Filter whether to skip running wptexturize().
45 * Passing false to the filter will effectively short-circuit wptexturize().
46 * returning the original text passed to the function instead.
48 * The filter runs only once, the first time wptexturize() is called.
54 * @param bool $run_texturize Whether to short-circuit wptexturize().
56 $run_texturize = apply_filters( 'run_wptexturize', $run_texturize );
57 if ( false === $run_texturize ) {
61 /* translators: opening curly double quote */
62 $opening_quote = _x( '“', 'opening curly double quote' );
63 /* translators: closing curly double quote */
64 $closing_quote = _x( '”', 'closing curly double quote' );
66 /* translators: apostrophe, for example in 'cause or can't */
67 $apos = _x( '’', 'apostrophe' );
69 /* translators: prime, for example in 9' (nine feet) */
70 $prime = _x( '′', 'prime' );
71 /* translators: double prime, for example in 9" (nine inches) */
72 $double_prime = _x( '″', 'double prime' );
74 /* translators: opening curly single quote */
75 $opening_single_quote = _x( '‘', 'opening curly single quote' );
76 /* translators: closing curly single quote */
77 $closing_single_quote = _x( '’', 'closing curly single quote' );
79 /* translators: en dash */
80 $en_dash = _x( '–', 'en dash' );
81 /* translators: em dash */
82 $em_dash = _x( '—', 'em dash' );
84 $default_no_texturize_tags = array('pre', 'code', 'kbd', 'style', 'script', 'tt');
85 $default_no_texturize_shortcodes = array('code');
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" );
95 $cockney = $cockneyreplace = array();
98 $static_characters = array_merge( array( '...', '``', '\'\'', ' (tm)' ), $cockney );
99 $static_replacements = array_merge( array( '…', $opening_quote, $closing_quote, ' ™' ), $cockneyreplace );
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() );
107 $spaces = wp_spaces_regexp();
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|[.,)}\-\]]|>|' . $spaces . ')/' ] = $apos . '$1' . $closing_single_quote;
113 if ( "'" !== $apos || '"' !== $closing_quote ) {
114 $dynamic[ '/\'(\d\d)"(?=\Z|[.,)}\-\]]|>|' . $spaces . ')/' ] = $apos . '$1' . $closing_quote;
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;
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;
127 // Single quote at start, or preceded by (, {, <, [, ", -, or spaces.
128 if ( "'" !== $opening_single_quote ) {
129 $dynamic[ '/(?<=\A|[([{"\-]|<|' . $spaces . ')\'/' ] = $opening_single_quote;
132 // Apostrophe in a word. No spaces, double apostrophes, or other punctuation.
133 if ( "'" !== $apos ) {
134 $dynamic[ '/(?<!' . $spaces . ')\'(?!\Z|[.,:;"\'(){}[\]\-]|&[lg]t;|' . $spaces . ')/' ] = $apos;
138 if ( "'" !== $prime ) {
139 $dynamic[ '/(?<=\d)\'/' ] = $prime;
142 // Single quotes followed by spaces or ending punctuation.
143 if ( "'" !== $closing_single_quote ) {
144 $dynamic[ '/\'(?=\Z|[.,)}\-\]]|>|' . $spaces . ')/' ] = $closing_single_quote;
147 $dynamic_characters['apos'] = array_keys( $dynamic );
148 $dynamic_replacements['apos'] = array_values( $dynamic );
151 // Quoted Numbers like "42"
152 if ( '"' !== $opening_quote && '"' !== $closing_quote ) {
153 $dynamic[ '/(?<=\A|' . $spaces . ')"(\d[.,\d]*)"/' ] = $opening_quote . '$1' . $closing_quote;
157 if ( '"' !== $double_prime ) {
158 $dynamic[ '/(?<=\d)"/' ] = $double_prime;
161 // Double quote at start, or preceded by (, {, <, [, -, or spaces, and not followed by spaces.
162 if ( '"' !== $opening_quote ) {
163 $dynamic[ '/(?<=\A|[([{\-]|<|' . $spaces . ')"(?!' . $spaces . ')/' ] = $opening_quote;
166 // Any remaining double quotes.
167 if ( '"' !== $closing_quote ) {
168 $dynamic[ '/"/' ] = $closing_quote;
171 $dynamic_characters['quote'] = array_keys( $dynamic );
172 $dynamic_replacements['quote'] = array_values( $dynamic );
176 $dynamic[ '/---/' ] = $em_dash;
177 $dynamic[ '/(?<=^|' . $spaces . ')--(?=$|' . $spaces . ')/' ] = $em_dash;
178 $dynamic[ '/(?<!xn)--/' ] = $en_dash;
179 $dynamic[ '/(?<=^|' . $spaces . ')-(?=$|' . $spaces . ')/' ] = $en_dash;
181 $dynamic_characters['dash'] = array_keys( $dynamic );
182 $dynamic_replacements['dash'] = array_values( $dynamic );
185 // Must do this every time in case plugins use these filters in a context sensitive manner
187 * Filter the list of HTML elements not to texturize.
191 * @param array $default_no_texturize_tags An array of HTML element names.
193 $no_texturize_tags = apply_filters( 'no_texturize_tags', $default_no_texturize_tags );
195 * Filter the list of shortcodes not to texturize.
199 * @param array $default_no_texturize_shortcodes An array of shortcode names.
201 $no_texturize_shortcodes = apply_filters( 'no_texturize_shortcodes', $default_no_texturize_shortcodes );
203 $no_texturize_tags_stack = array();
204 $no_texturize_shortcodes_stack = array();
206 // Look for shortcodes and HTML elements.
208 $tagnames = array_keys( $shortcode_tags );
209 $tagregexp = join( '|', array_map( 'preg_quote', $tagnames ) );
210 $tagregexp = "(?:$tagregexp)(?![\\w-])"; // Excerpt of get_shortcode_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.
221 '\[' // Find start of shortcode.
222 . '[\/\[]?' // Shortcodes may begin with [/ or [[
223 . $tagregexp // Only match registered shortcodes, because performance.
225 . '[^\[\]<>]+' // Shortcodes do not contain other shortcodes. Quantifier critical.
227 . '<[^\[\]>]*>' // HTML elements permitted. Prevents matching ] before >.
228 . ')*+' // Possessive critical.
229 . '\]' // Find end of shortcode.
230 . '\]?'; // Shortcodes may end with ]]
233 '/(' // Capture the entire match.
234 . '<' // Find start of element.
235 . '(?(?=!--)' // Is this a comment?
236 . $comment_regex // Find end of comment.
238 . '[^>]*>' // Find end of element.
241 . $shortcode_regex // Find shortcodes.
244 $textarr = preg_split( $regex, $text, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY );
246 foreach ( $textarr as &$curl ) {
247 // Only call _wptexturize_pushpop_element if $curl is a delimiter.
249 if ( '<' === $first && '<!--' === substr( $curl, 0, 4 ) ) {
250 // This is an HTML comment delimeter.
254 } elseif ( '<' === $first && '>' === substr( $curl, -1 ) ) {
255 // This is an HTML element delimiter.
257 _wptexturize_pushpop_element( $curl, $no_texturize_tags_stack, $no_texturize_tags );
259 } elseif ( '' === trim( $curl ) ) {
260 // This is a newline between delimiters. Performance improves when we check this.
264 } elseif ( '[' === $first && 1 === preg_match( '/^' . $shortcode_regex . '$/', $curl ) ) {
265 // This is a shortcode delimiter.
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 );
271 // Looks like an escaped shortcode.
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.
278 $curl = str_replace( $static_characters, $static_replacements, $curl );
280 if ( false !== strpos( $curl, "'" ) ) {
281 $curl = preg_replace( $dynamic_characters['apos'], $dynamic_replacements['apos'], $curl );
283 if ( false !== strpos( $curl, '"' ) ) {
284 $curl = preg_replace( $dynamic_characters['quote'], $dynamic_replacements['quote'], $curl );
286 if ( false !== strpos( $curl, '-' ) ) {
287 $curl = preg_replace( $dynamic_characters['dash'], $dynamic_replacements['dash'], $curl );
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×$2', $curl );
297 $text = implode( '', $textarr );
299 // Replace each & with & unless it already looks like an entity.
300 $text = preg_replace('/&(?!#(?:\d+|x[a-f0-9]+);|[a-z1-4]{1,8};)/i', '&', $text);
306 * Search for disabled element tags. Push element to stack on tag open and pop
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>.
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.
319 function _wptexturize_pushpop_element($text, &$stack, $disabled_elements) {
320 // Is it an opening tag or closing tag?
321 if ( '/' !== $text[1] ) {
324 } elseif ( 0 == count( $stack ) ) {
325 // Stack is empty. Just stop.
328 $opening_tag = false;
332 // Parse out the tag name.
333 $space = strpos( $text, ' ' );
334 if ( false === $space ) {
337 $space -= $name_offset;
339 $tag = substr( $text, $name_offset, $space );
341 // Handle disabled tags.
342 if ( in_array( $tag, $disabled_elements ) ) {
343 if ( $opening_tag ) {
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
348 * Example: in the case <pre>sadsadasd</code>"baba"</pre>
349 * "baba" won't be texturize
352 array_push( $stack, $tag );
353 } elseif ( end( $stack ) == $tag ) {
360 * Replaces double line-breaks with paragraph elements.
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'.
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.
373 function wpautop($pee, $br = true) {
376 if ( trim($pee) === '' )
379 // Just to make things a little easier, pad the end.
383 * Pre tags shouldn't be touched by autop.
384 * Replace pre tags with placeholders and bring them back after autop.
386 if ( strpos($pee, '<pre') !== false ) {
387 $pee_parts = explode( '</pre>', $pee );
388 $last_pee = array_pop($pee_parts);
392 foreach ( $pee_parts as $pee_part ) {
393 $start = strpos($pee_part, '<pre');
396 if ( $start === false ) {
401 $name = "<pre wp-pre-tag-$i></pre>";
402 $pre_tags[$name] = substr( $pee_part, $start ) . '</pre>';
404 $pee .= substr( $pee_part, 0, $start ) . $name;
410 // Change multiple <br>s into two line breaks, which will turn into paragraphs.
411 $pee = preg_replace('|<br />\s*<br />|', "\n\n", $pee);
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)';
415 // Add a single line break above block-level opening tags.
416 $pee = preg_replace('!(<' . $allblocks . '[^>]*>)!', "\n$1", $pee);
418 // Add a double line break below block-level closing tags.
419 $pee = preg_replace('!(</' . $allblocks . '>)!', "$1\n\n", $pee);
421 // Standardize newline characters to "\n".
422 $pee = str_replace(array("\r\n", "\r"), "\n", $pee);
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 );
431 * Collapse line breaks inside <object> elements, before <param> and <embed> elements
432 * so they don't get autop'd.
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 );
441 * Collapse line breaks inside <audio> and <video> elements,
442 * before and after <source> and <track> elements.
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 );
450 // Remove more than two contiguous line breaks.
451 $pee = preg_replace("/\n\n+/", "\n\n", $pee);
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);
456 // Reset $pee prior to rebuilding.
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";
464 // Under certain strange conditions it could create a P of entirely whitespace.
465 $pee = preg_replace('|<p>\s*</p>|', '', $pee);
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);
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);
473 // In some cases <li> may get wrapped in <p>, fix them.
474 $pee = preg_replace("|<p>(<li.+?)</p>|", "$1", $pee);
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);
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);
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);
486 // Optionally insert line breaks.
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);
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);
494 // Replace newline placeholders with newlines.
495 $pee = str_replace('<WPPreserveNewline />', "\n", $pee);
498 // If a <br /> tag is after an opening or closing block tag, remove it.
499 $pee = preg_replace('!(</?' . $allblocks . '[^>]*>)\s*<br />!', "$1", $pee);
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 );
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);
513 * Newline preservation help function for wpautop
518 * @param array $matches preg_replace_callback matches array
521 function _autop_newline_preservation_helper( $matches ) {
522 return str_replace("\n", "<WPPreserveNewline />", $matches[0]);
526 * Don't auto-p wrap shortcodes that stand alone
528 * Ensures that shortcodes are not wrapped in `<p>...</p>`.
532 * @param string $pee The content.
533 * @return string The filtered content.
535 function shortcode_unautop( $pee ) {
536 global $shortcode_tags;
538 if ( empty( $shortcode_tags ) || !is_array( $shortcode_tags ) ) {
542 $tagregexp = join( '|', array_map( 'preg_quote', array_keys( $shortcode_tags ) ) );
543 $spaces = wp_spaces_regexp();
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
556 . '\\/(?!\\])' // A forward slash not followed by a closing bracket
557 . '[^\\]\\/]*' // Not a closing bracket or forward slash
560 . '\\/\\]' // Self closing tag and closing bracket
562 . '\\]' // Closing bracket
563 . '(?:' // Unroll the loop: Optionally, anything between the opening and closing shortcode tags
564 . '[^\\[]*+' // Not an opening bracket
566 . '\\[(?!\\/\\2\\])' // An opening bracket not followed by the closing shortcode tag
567 . '[^\\[]*+' // Not an opening bracket
569 . '\\[\\/\\2\\]' // Closing shortcode tag
573 . '(?:' . $spaces . ')*+' // optional trailing whitespace
574 . '<\\/p>' // closing paragraph
577 return preg_replace( $pattern, '$1', $pee );
581 * Checks to see if a string is utf8 encoded.
583 * NOTE: This function checks for 5-Byte sequences, UTF8
584 * has Bytes Sequences with a maximum length of 4.
586 * @author bmorel at ssi dot fr (modified)
589 * @param string $str The string to be checked
590 * @return bool True if $str fits a UTF-8 model, false otherwise.
592 function seems_utf8($str) {
593 mbstring_binary_safe_encoding();
594 $length = strlen($str);
595 reset_mbstring_encoding();
596 for ($i=0; $i < $length; $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))
614 * Converts a number of special characters into their HTML entities.
616 * Specifically deals with: &, <, >, ", and '.
618 * $quote_style can be set to ENT_COMPAT to encode " to
619 * ", or ENT_QUOTES to do both. Default is ENT_NOQUOTES where no quotes are encoded.
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.
630 function _wp_specialchars( $string, $quote_style = ENT_NOQUOTES, $charset = false, $double_encode = false ) {
631 $string = (string) $string;
633 if ( 0 === strlen( $string ) )
636 // Don't bother if there are no specialchars - saves some processing
637 if ( ! preg_match( '/[&<>"\']/', $string ) )
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;
646 // Store the site charset as a static to avoid multiple calls to wp_load_alloptions()
649 if ( ! isset( $_charset ) ) {
650 $alloptions = wp_load_alloptions();
651 $_charset = isset( $alloptions['blog_charset'] ) ? $alloptions['blog_charset'] : '';
653 $charset = $_charset;
656 if ( in_array( $charset, array( 'utf8', 'utf-8', 'UTF8' ) ) )
659 $_quote_style = $quote_style;
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;
668 // Handle double encoding ourselves
669 if ( $double_encode ) {
670 $string = @htmlspecialchars( $string, $quote_style, $charset );
672 // Decode & into &
673 $string = wp_specialchars_decode( $string, $_quote_style );
675 // Guarantee every &entity; is valid or re-encode the &
676 $string = wp_kses_normalize_entities( $string );
678 // Now re-encode everything except &entity;
679 $string = preg_split( '/(&#?x?[0-9a-z]+;)/i', $string, -1, PREG_SPLIT_DELIM_CAPTURE );
681 for ( $i = 0, $c = count( $string ); $i < $c; $i += 2 ) {
682 $string[$i] = @htmlspecialchars( $string[$i], $quote_style, $charset );
684 $string = implode( '', $string );
687 // Backwards compatibility
688 if ( 'single' === $_quote_style )
689 $string = str_replace( "'", ''', $string );
695 * Converts a number of HTML entities into their special characters.
697 * Specifically deals with: &, <, >, ", and '.
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.
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.
708 function wp_specialchars_decode( $string, $quote_style = ENT_NOQUOTES ) {
709 $string = (string) $string;
711 if ( 0 === strlen( $string ) ) {
715 // Don't bother if there are no entities - saves a lot of processing
716 if ( strpos( $string, '&' ) === false ) {
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;
727 // More complete than get_html_translation_table( HTML_SPECIALCHARS )
728 $single = array( ''' => '\'', ''' => '\'' );
729 $single_preg = array( '/�*39;/' => ''', '/�*27;/i' => ''' );
730 $double = array( '"' => '"', '"' => '"', '"' => '"' );
731 $double_preg = array( '/�*34;/' => '"', '/�*22;/i' => '"' );
732 $others = array( '<' => '<', '<' => '<', '>' => '>', '>' => '>', '&' => '&', '&' => '&', '&' => '&' );
733 $others_preg = array( '/�*60;/' => '<', '/�*62;/' => '>', '/�*38;/' => '&', '/�*26;/i' => '&' );
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;
749 // Remove zero padding on numeric entities
750 $string = preg_replace( array_keys( $translation_preg ), array_values( $translation_preg ), $string );
752 // Replace characters according to translation table
753 return strtr( $string, $translation );
757 * Checks for invalid UTF8 in a string.
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.
765 function wp_check_invalid_utf8( $string, $strip = false ) {
766 $string = (string) $string;
768 if ( 0 === strlen( $string ) ) {
772 // Store the site charset as a static to avoid multiple calls to get_option()
774 if ( !isset( $is_utf8 ) ) {
775 $is_utf8 = in_array( get_option( 'blog_charset' ), array( 'utf8', 'utf-8', 'UTF8', 'UTF-8' ) );
781 // Check for support for utf8 in the installed PCRE library once and store the result in a static
783 if ( !isset( $utf8_pcre ) ) {
784 $utf8_pcre = @preg_match( '/^./u', 'a' );
786 // We can't demand utf8 in the PCRE installation, so just return the string in those cases
791 // preg_match fails when it encounters invalid UTF8 in $string
792 if ( 1 === @preg_match( '/^./us', $string ) ) {
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 );
805 * Encode the Unicode values to be used in the URI.
809 * @param string $utf8_string
810 * @param int $length Max length of the string
811 * @return string String with Unicode encoded for URI.
813 function utf8_uri_encode( $utf8_string, $length = 0 ) {
819 mbstring_binary_safe_encoding();
820 $string_length = strlen( $utf8_string );
821 reset_mbstring_encoding();
823 for ($i = 0; $i < $string_length; $i++ ) {
825 $value = ord( $utf8_string[ $i ] );
827 if ( $value < 128 ) {
828 if ( $length && ( $unicode_length >= $length ) )
830 $unicode .= chr($value);
833 if ( count( $values ) == 0 ) {
834 if ( $value < 224 ) {
836 } elseif ( $value < 240 ) {
845 if ( $length && ( $unicode_length + ($num_octets * 3) ) > $length )
847 if ( count( $values ) == $num_octets ) {
848 for ( $j = 0; $j < $num_octets; $j++ ) {
849 $unicode .= '%' . dechex( $values[ $j ] );
852 $unicode_length += $num_octets * 3;
864 * Converts all accent characters to ASCII characters.
866 * If there are no accent characters, then the string given is just returned.
870 * @param string $string Text that might have accent characters
871 * @return string Filtered string with replaced "nice" characters.
873 function remove_accents($string) {
874 if ( !preg_match('/[\x80-\xff]/', $string) )
877 if (seems_utf8($string)) {
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',
981 chr(226).chr(130).chr(172) => 'E',
983 chr(194).chr(163) => '',
984 // Vowels with diacritic (Vietnamese)
986 chr(198).chr(160) => 'O', chr(198).chr(161) => 'o',
987 chr(198).chr(175) => 'U', chr(198).chr(176) => 'u',
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',
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',
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',
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',
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',
1041 chr(199).chr(149) => 'U', chr(199).chr(150) => 'u',
1043 chr(199).chr(151) => 'U', chr(199).chr(152) => 'u',
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',
1051 chr(199).chr(155) => 'U', chr(199).chr(156) => 'u',
1054 // Used for locale-specific rules
1055 $locale = get_locale();
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';
1074 $string = strtr($string, $chars);
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);
1089 $chars['out'] = "EfSZszYcYuAAAAAACEEEEIIIINOOOOOOUUUUYaaaaaaceeeeiiiinoooooouuuuyy";
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);
1102 * Sanitizes a filename, replacing whitespace with dashes.
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.
1112 * @param string $filename The filename to be sanitized
1113 * @return string The sanitized filename
1115 function sanitize_file_name( $filename ) {
1116 $filename_raw = $filename;
1117 $special_chars = array("?", "[", "]", "/", "\\", "=", "<", ">", ":", ";", ",", "'", "\"", "&", "$", "#", "*", "(", ")", "|", "~", "`", "!", "{", "}", chr(0));
1119 * Filter the list of characters to remove from a filename.
1123 * @param array $special_chars Characters to remove.
1124 * @param string $filename_raw Filename as it was passed into sanitize_file_name().
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, '.-_' );
1133 // Split the filename into a base and extension[s]
1134 $parts = explode('.', $filename);
1136 // Return if only one extension
1137 if ( count( $parts ) <= 2 ) {
1139 * Filter a sanitized filename string.
1143 * @param string $filename Sanitized filename.
1144 * @param string $filename_raw The filename prior to sanitization.
1146 return apply_filters( 'sanitize_file_name', $filename, $filename_raw );
1149 // Process multiple extensions
1150 $filename = array_shift($parts);
1151 $extension = array_pop($parts);
1152 $mimes = get_allowed_mime_types();
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.
1158 foreach ( (array) $parts as $part) {
1159 $filename .= '.' . $part;
1161 if ( preg_match("/^[a-zA-Z]{2,5}\d?$/", $part) ) {
1163 foreach ( $mimes as $ext_preg => $mime_match ) {
1164 $ext_preg = '!^(' . $ext_preg . ')$!i';
1165 if ( preg_match( $ext_preg, $part ) ) {
1174 $filename .= '.' . $extension;
1175 /** This filter is documented in wp-includes/formatting.php */
1176 return apply_filters('sanitize_file_name', $filename, $filename_raw);
1180 * Sanitizes a username, stripping out unsafe characters.
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.
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.
1193 function sanitize_user( $username, $strict = false ) {
1194 $raw_username = $username;
1195 $username = wp_strip_all_tags( $username );
1196 $username = remove_accents( $username );
1198 $username = preg_replace( '|%([a-fA-F0-9][a-fA-F0-9])|', '', $username );
1199 $username = preg_replace( '/&.+?;/', '', $username ); // Kill entities
1201 // If strict, reduce to ASCII for max portability.
1203 $username = preg_replace( '|[^a-z0-9 _.\-@]|i', '', $username );
1205 $username = trim( $username );
1206 // Consolidate contiguous whitespace
1207 $username = preg_replace( '|\s+|', ' ', $username );
1210 * Filter a sanitized username string.
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.
1218 return apply_filters( 'sanitize_user', $username, $raw_username, $strict );
1222 * Sanitizes a string key.
1224 * Keys are used as internal identifiers. Lowercase alphanumeric characters, dashes and underscores are allowed.
1228 * @param string $key String key
1229 * @return string Sanitized key
1231 function sanitize_key( $key ) {
1233 $key = strtolower( $key );
1234 $key = preg_replace( '/[^a-z0-9_\-]/', '', $key );
1237 * Filter a sanitized key string.
1241 * @param string $key Sanitized key.
1242 * @param string $raw_key The key prior to sanitization.
1244 return apply_filters( 'sanitize_key', $key, $raw_key );
1248 * Sanitizes a title, or returns a fallback title.
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
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.
1261 function sanitize_title( $title, $fallback_title = '', $context = 'save' ) {
1262 $raw_title = $title;
1264 if ( 'save' == $context )
1265 $title = remove_accents($title);
1268 * Filter a sanitized title string.
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.
1276 $title = apply_filters( 'sanitize_title', $title, $raw_title, $context );
1278 if ( '' === $title || false === $title )
1279 $title = $fallback_title;
1285 * Sanitizes a title with the 'query' context.
1287 * Used for querying the database for a value from URL.
1291 * @param string $title The string to be sanitized.
1292 * @return string The sanitized string.
1294 function sanitize_title_for_query( $title ) {
1295 return sanitize_title( $title, '', 'query' );
1299 * Sanitizes a title, replacing whitespace and a few other characters with dashes.
1301 * Limits the output to alphanumeric characters, underscore (_) and dash (-).
1302 * Whitespace becomes a dash.
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.
1311 function 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);
1318 $title = preg_replace('|---([a-fA-F0-9][a-fA-F0-9])---|', '%$1', $title);
1320 if (seems_utf8($title)) {
1321 if (function_exists('mb_strtolower')) {
1322 $title = mb_strtolower($title, 'UTF-8');
1324 $title = utf8_uri_encode($title, 200);
1327 $title = strtolower($title);
1328 $title = preg_replace('/&.+?;/', '', $title); // kill entities
1329 $title = str_replace('.', '-', $title);
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 );
1335 // Strip these characters entirely
1336 $title = str_replace( array(
1340 '%c2%ab', '%c2%bb', '%e2%80%b9', '%e2%80%ba',
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',
1347 '%c2%b4', '%cb%8a', '%cc%81', '%cd%81',
1348 // grave accent, macron, caron
1349 '%cc%80', '%cc%84', '%cc%8c',
1352 // Convert times to x
1353 $title = str_replace( '%c3%97', 'x', $title );
1356 $title = preg_replace('/[^%a-z0-9 _-]/', '', $title);
1357 $title = preg_replace('/\s+/', '-', $title);
1358 $title = preg_replace('|-+|', '-', $title);
1359 $title = trim($title, '-');
1365 * Ensures a string is a valid SQL 'order by' clause.
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.
1370 * Also accepts 'RAND()'.
1374 * @param string $orderby Order by clause to be validated.
1375 * @return string|bool Returns $orderby if valid, false otherwise.
1377 function 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 ) ) {
1385 * Sanitizes an HTML classname to ensure it only contains valid characters.
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.
1390 * @todo Expand to support the full range of CDATA that a class attribute can contain.
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
1399 function sanitize_html_class( $class, $fallback = '' ) {
1400 //Strip out any % encoded octets
1401 $sanitized = preg_replace( '|%[a-fA-F0-9][a-fA-F0-9]|', '', $class );
1403 //Limit to A-Z,a-z,0-9,_,-
1404 $sanitized = preg_replace( '/[^A-Za-z0-9_-]/', '', $sanitized );
1406 if ( '' == $sanitized )
1407 $sanitized = $fallback;
1410 * Filter a sanitized HTML class string.
1414 * @param string $sanitized The sanitized HTML class.
1415 * @param string $class HTML class before sanitization.
1416 * @param string $fallback The fallback string.
1418 return apply_filters( 'sanitize_html_class', $sanitized, $class, $fallback );
1422 * Converts a number of characters from a string.
1424 * Metadata tags `<title>` and `<category>` are removed, `<br>` and `<hr>` are
1425 * converted into correct XHTML and Unicode characters are converted to the
1430 * @param string $content String of characters to be converted.
1431 * @param string $deprecated Not used.
1432 * @return string Converted string.
1434 function convert_chars($content, $deprecated = '') {
1435 if ( !empty( $deprecated ) )
1436 _deprecated_argument( __FUNCTION__, '0.71' );
1438 // Translation of invalid Unicode references range to valid range
1439 $wp_htmltranswinuni = array(
1440 '€' => '€', // the Euro sign
1442 '‚' => '‚', // these are Windows CP1252 specific characters
1443 'ƒ' => 'ƒ', // they would look weird on non-Windows browsers
1444 '„' => '„',
1445 '…' => '…',
1446 '†' => '†',
1447 '‡' => '‡',
1448 'ˆ' => 'ˆ',
1449 '‰' => '‰',
1450 'Š' => 'Š',
1451 '‹' => '‹',
1452 'Œ' => 'Œ',
1454 'Ž' => 'Ž',
1457 '‘' => '‘',
1458 '’' => '’',
1459 '“' => '“',
1460 '”' => '”',
1461 '•' => '•',
1462 '–' => '–',
1463 '—' => '—',
1464 '˜' => '˜',
1465 '™' => '™',
1466 'š' => 'š',
1467 '›' => '›',
1468 'œ' => 'œ',
1470 'ž' => 'ž',
1471 'Ÿ' => 'Ÿ'
1474 // Remove metadata tags
1475 $content = preg_replace('/<title>(.+?)<\/title>/','',$content);
1476 $content = preg_replace('/<category>(.+?)<\/category>/','',$content);
1478 // Converts lone & characters into & (a.k.a. &)
1479 $content = preg_replace('/&([^#])(?![a-z1-4]{1,8};)/i', '&$1', $content);
1482 $content = strtr($content, $wp_htmltranswinuni);
1484 // Just a little XHTML help
1485 $content = str_replace('<br>', '<br />', $content);
1486 $content = str_replace('<hr>', '<hr />', $content);
1492 * Balances tags if forced to, or if the 'use_balanceTags' option is set to true.
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
1500 function balanceTags( $text, $force = false ) {
1501 if ( $force || get_option('use_balanceTags') == 1 ) {
1502 return force_balance_tags( $text );
1509 * Balances tags of string using a modified stack.
1513 * @author Leonard Lin <leonard@acm.org>
1515 * @copyright November 4, 2001
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
1523 * @param string $text Text to be balanced.
1524 * @return string Balanced text.
1526 function force_balance_tags( $text ) {
1527 $tagstack = array();
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' );
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})#', '<$1', $text);
1541 while ( preg_match("/<(\/?[\w:]*)\s*([^>]*)>/", $text, $regex) ) {
1542 $newtext .= $tagqueue;
1544 $i = strpos($text, $regex[0]);
1545 $l = strlen($regex[0]);
1547 // clear the shifter
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 ) {
1555 // or close to be safe $tag = '/' . $tag;
1557 // if stacktop value = tag close value then pop
1558 elseif ( $tagstack[$stacksize - 1] == $tag ) { // found closing tag
1559 $tag = '</' . $tag . '>'; // Close Tag
1561 array_pop( $tagstack );
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 ) . '>';
1576 } else { // Begin Tag
1577 $tag = strtolower($regex[1]);
1581 // If it's an empty tag "< >", do nothing
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";
1592 // ElseIf it's a known single-entity tag but it doesn't close itself, do so
1593 elseif ( in_array($tag, $single_tags) ) {
1596 // Else it's not a single-entity tag
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 ) . '>';
1603 $stacksize = array_push( $tagstack, $tag );
1607 $attributes = $regex[2];
1608 if( ! empty( $attributes ) && $attributes[0] != '>' )
1609 $attributes = ' ' . $attributes;
1611 $tag = '<' . $tag . $attributes . '>';
1612 //If already queuing a close tag, then put this tag on, too
1613 if ( !empty($tagqueue) ) {
1618 $newtext .= substr($text, 0, $i) . $tag;
1619 $text = substr($text, $i + $l);
1623 $newtext .= $tagqueue;
1625 // Add Remaining text
1629 while( $x = array_pop($tagstack) )
1630 $newtext .= '</' . $x . '>'; // Add remaining tags to close
1632 // WP fix for the bug with HTML comments
1633 $newtext = str_replace("< !--","<!--",$newtext);
1634 $newtext = str_replace("< !--","< !--",$newtext);
1640 * Acts on text which is about to be edited.
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.
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.
1652 function format_to_edit( $content, $richedit = false ) {
1654 * Filter the text to be formatted for editing.
1658 * @param string $content The text, prior to formatting for editing.
1660 $content = apply_filters( 'format_to_edit', $content );
1662 $content = esc_textarea( $content );
1667 * Add leading zeros when necessary.
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'.
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
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.
1683 function zeroise($number, $threshold) {
1684 return sprintf('%0'.$threshold.'s', $number);
1688 * Adds backslashes before letters and before a number at the start of a string.
1692 * @param string $string Value to which backslashes will be added.
1693 * @return string String with backslashes inserted.
1695 function backslashit($string) {
1696 if ( isset( $string[0] ) && $string[0] >= '0' && $string[0] <= '9' )
1697 $string = '\\\\' . $string;
1698 return addcslashes( $string, 'A..Za..z' );
1702 * Appends a trailing slash.
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.
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.
1712 * @param string $string What to add the trailing slash to.
1713 * @return string String with trailing slash added.
1715 function trailingslashit( $string ) {
1716 return untrailingslashit( $string ) . '/';
1720 * Removes trailing forward slashes and backslashes if they exist.
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.
1727 * @param string $string What to remove the trailing slashes from.
1728 * @return string String without the trailing slashes.
1730 function untrailingslashit( $string ) {
1731 return rtrim( $string, '/\\' );
1735 * Adds slashes to escape strings.
1737 * Slashes will first be removed if magic_quotes_gpc is set, see {@link
1738 * http://www.php.net/magic_quotes} for more details.
1742 * @param string $gpc The string returned from HTTP request data.
1743 * @return string Returns a string escaped with slashes.
1745 function addslashes_gpc($gpc) {
1746 if ( get_magic_quotes_gpc() )
1747 $gpc = stripslashes($gpc);
1749 return wp_slash($gpc);
1753 * Navigates through an array and removes slashes from the values.
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.
1760 * @param mixed $value The value to be stripped.
1761 * @return mixed Stripped value.
1763 function 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 );
1771 } elseif ( is_string( $value ) ) {
1772 $value = stripslashes($value);
1779 * Navigates through an array and encodes the values to be used in a URL.
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).
1787 function urlencode_deep($value) {
1788 $value = is_array($value) ? array_map('urlencode_deep', $value) : urlencode($value);
1793 * Navigates through an array and raw encodes the values to be used in a URL.
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).
1800 function rawurlencode_deep( $value ) {
1801 return is_array( $value ) ? array_map( 'rawurlencode_deep', $value ) : rawurlencode( $value );
1805 * Converts email addresses characters to HTML entities to block spam bots.
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.
1813 function 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 );
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 );
1826 $email_no_spam_address = str_replace( '@', '@', $email_no_spam_address );
1828 return $email_no_spam_address;
1832 * Callback to convert URI match to HTML A element.
1834 * This function was backported from 2.5.0 to 2.3.2. Regex callback for {@link
1835 * make_clickable()}.
1840 * @param array $matches Single Regex Match.
1841 * @return string HTML A element with URI address.
1843 function _make_url_clickable_cb($matches) {
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];
1852 $suffix = $matches[3];
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, ')' ) );
1861 $url = esc_url($url);
1865 return $matches[1] . "<a href=\"$url\" rel=\"nofollow\">$url</a>" . $suffix;
1869 * Callback to convert URL match to HTML A element.
1871 * This function was backported from 2.5.0 to 2.3.2. Regex callback for {@link
1872 * make_clickable()}.
1877 * @param array $matches Single Regex Match.
1878 * @return string HTML A element with URL address.
1880 function _make_web_ftp_clickable_cb($matches) {
1882 $dest = $matches[2];
1883 $dest = 'http://' . $dest;
1884 $dest = esc_url($dest);
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);
1893 return $matches[1] . "<a href=\"$dest\" rel=\"nofollow\">$dest</a>$ret";
1897 * Callback to convert email address match to HTML A element.
1899 * This function was backported from 2.5.0 to 2.3.2. Regex callback for {@link
1900 * make_clickable()}.
1905 * @param array $matches Single Regex Match.
1906 * @return string HTML A element with email address.
1908 function _make_email_clickable_cb($matches) {
1909 $email = $matches[2] . '@' . $matches[3];
1910 return $matches[1] . "<a href=\"mailto:$email\">$email</a>";
1914 * Convert plaintext URI to HTML links.
1916 * Converts URI, www and ftp, and email addresses. Finishes by fixing links
1921 * @param string $text Content to convert URIs.
1922 * @return string Content with converted URIs.
1924 function make_clickable( $text ) {
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 ) {
1930 if ( preg_match( '|^<code[\s>]|i', $piece ) || preg_match( '|^<pre[\s>]|i', $piece ) )
1932 elseif ( ( '</code>' === strtolower( $piece ) || '</pre>' === strtolower( $piece ) ) && $nested_code_pre )
1935 if ( $nested_code_pre || empty( $piece ) || ( $piece[0] === '<' && ! preg_match( '|^<\s*[\w]{1,20}+://|', $piece ) ) ) {
1940 // Long strings might contain expensive edge cases ...
1941 if ( 10000 < strlen( $piece ) ) {
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.
1947 $r .= make_clickable( $chunk );
1951 $ret = " $piece "; // Pad with whitespace to simplify the regexes
1954 ([\\s(<.,;:!?]) # 1: Leading whitespace, or punctuation
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
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.
1968 $ret = preg_replace_callback( $url_clickable, '_make_url_clickable_cb', $ret );
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 );
1973 $ret = substr( $ret, 1, -1 ); // Remove our whitespace padding.
1978 // Cleanup of accidental links within links
1979 $r = preg_replace( '#(<a([ \r\n\t]+[^>]+?>|>))<a [^>]+?>([^>]+?)</a></a>#i', "$1$3</a>", $r );
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.
1989 * Joining the returned chunks with empty delimiters reconstructs the input string losslessly.
1991 * Input string must have no null characters (or eventual transformations on output chunks must not care about null characters)
1993 * _split_str_by_whitespace( "1234 67890 1234 67890a cd 1234 890 123456789 1234567890a 45678 1 3 5 7 90 ", 10 ) ==
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
2008 * @param string $string The string to split.
2009 * @param int $goal The desired chunk length.
2010 * @return array Numeric array of chunks.
2012 function _split_str_by_whitespace( $string, $goal ) {
2015 $string_nullspace = strtr( $string, "\r\n\t\v\f ", "\000\000\000\000\000\000" );
2017 while ( $goal < strlen( $string_nullspace ) ) {
2018 $pos = strrpos( substr( $string_nullspace, 0, $goal + 1 ), "\000" );
2020 if ( false === $pos ) {
2021 $pos = strpos( $string_nullspace, "\000", $goal + 1 );
2022 if ( false === $pos ) {
2027 $chunks[] = substr( $string, 0, $pos + 1 );
2028 $string = substr( $string, $pos + 1 );
2029 $string_nullspace = substr( $string_nullspace, $pos + 1 );
2033 $chunks[] = $string;
2040 * Adds rel nofollow string to all HTML A elements in content.
2044 * @param string $text Content that may contain HTML A elements.
2045 * @return string Converted content.
2047 function 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);
2056 * Callback to add rel=nofollow string to HTML A element.
2058 * Will remove already existing rel="nofollow" and rel='nofollow' from the
2059 * string to prevent from invalidating (X)HTML.
2063 * @param array $matches Single Match
2064 * @return string HTML A Element with rel nofollow.
2066 function 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\">";
2073 * Convert one smiley code to the icon graphic file equivalent.
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.
2079 * @global array $wpsmiliestrans
2082 * @param array $matches Single match. Smiley code to convert to image.
2083 * @return string Image string for smiley.
2085 function translate_smiley( $matches ) {
2086 global $wpsmiliestrans;
2088 if ( count( $matches ) == 0 )
2091 $smiley = trim( reset( $matches ) );
2092 $img = $wpsmiliestrans[ $smiley ];
2095 $ext = preg_match( '/\.([^.]+)$/', $img, $matches ) ? strtolower( $matches[1] ) : false;
2096 $image_exts = array( 'jpg', 'jpeg', 'jpe', 'gif', 'png' );
2098 // Don't convert smilies that aren't images - they're probably emoji.
2099 if ( ! in_array( $ext, $image_exts ) ) {
2104 * Filter the Smiley image URL before it's used in the image element.
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().
2112 $src_url = apply_filters( 'smilies_src', includes_url( "images/smilies/$img" ), $img, site_url() );
2114 return sprintf( '<img src="%s" alt="%s" class="wp-smiley" style="height: 1em; max-height: 1em;" />', esc_url( $src_url ), esc_attr( $smiley ) );
2118 * Convert text equivalent of smilies to images.
2120 * Will only convert smilies if the option 'use_smilies' is true and the global
2121 * used in the function isn't empty.
2124 * @uses $wp_smiliessearch
2126 * @param string $text Content to convert smilies from text.
2127 * @return string Converted content with text smilies replaced with images.
2129 function convert_smilies( $text ) {
2130 global $wp_smiliessearch;
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
2137 // Ignore proessing of specific tags
2138 $tags_to_ignore = 'code|pre|style|script|textarea';
2139 $ignore_block_element = '';
2141 for ( $i = 0; $i < $stop; $i++ ) {
2142 $content = $textarr[$i];
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];
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 );
2154 // did we exit ignore block
2155 if ( '' != $ignore_block_element && '</' . $ignore_block_element . '>' == $content ) {
2156 $ignore_block_element = '';
2159 $output .= $content;
2162 // return default text.
2169 * Verifies that an email is valid.
2171 * Does not grok i18n domains. Not RFC compliant.
2175 * @param string $email Email address to verify.
2176 * @param boolean $deprecated Deprecated.
2177 * @return string|bool Either false or the valid email address.
2179 function is_email( $email, $deprecated = false ) {
2180 if ( ! empty( $deprecated ) )
2181 _deprecated_argument( __FUNCTION__, '3.0' );
2183 // Test for the minimum length the email can be
2184 if ( strlen( $email ) < 3 ) {
2186 * Filter whether an email address is valid.
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.
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.
2199 return apply_filters( 'is_email', false, $email, 'email_too_short' );
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' );
2208 // Split out the local and domain parts
2209 list( $local, $domain ) = explode( '@', $email, 2 );
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' );
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' );
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' );
2231 // Split the domain into subs
2232 $subs = explode( '.', $domain );
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' );
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' );
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' );
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 );
2261 * Convert to ASCII from email subjects.
2265 * @param string $string Subject line
2266 * @return string Converted string to ASCII
2268 function wp_iso_descrambler($string) {
2269 /* this may only work with iso-8859-1, I'm afraid */
2270 if (!preg_match('#\=\?(.+)\?Q\?(.+)\?\=#i', $string, $matches)) {
2273 $subject = str_replace('_', ' ', $matches[2]);
2274 $subject = preg_replace_callback('#\=([0-9a-f]{2})#i', '_wp_iso_convert', $subject);
2280 * Helper function to convert hex encoded chars to ASCII
2285 * @param array $match The preg_replace_callback matches array
2286 * @return string Converted chars
2288 function _wp_iso_convert( $match ) {
2289 return chr( hexdec( strtolower( $match[1] ) ) );
2293 * Returns a date in the GMT equivalent.
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.
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.
2306 function get_gmt_from_date( $string, $format = 'Y-m-d H:i:s' ) {
2307 $tz = get_option( 'timezone_string' );
2309 $datetime = date_create( $string, new DateTimeZone( $tz ) );
2311 return gmdate( $format, 0 );
2312 $datetime->setTimezone( new DateTimeZone( 'UTC' ) );
2313 $string_gmt = $datetime->format( $format );
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 );
2324 * Converts a GMT date into the correct format for the blog.
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
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.
2337 function get_date_from_gmt( $string, $format = 'Y-m-d H:i:s' ) {
2338 $tz = get_option( 'timezone_string' );
2340 $datetime = date_create( $string, new DateTimeZone( 'UTC' ) );
2342 return date( $format, 0 );
2343 $datetime->setTimezone( new DateTimeZone( $tz ) );
2344 $string_localtime = $datetime->format( $format );
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 );
2351 return $string_localtime;
2355 * Computes an offset in seconds from an iso8601 timezone.
2359 * @param string $timezone Either 'Z' for 0 offset or '±hhmm'.
2360 * @return int|float The offset in seconds.
2362 function iso8601_timezone_to_offset($timezone) {
2363 // $timezone is either 'Z' or '[+|-]hhmm'
2364 if ($timezone == 'Z') {
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);
2376 * Converts an iso8601 date to MySQL DateTime format used by post_date[_gmt].
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.
2384 function iso8601_to_datetime($date_string, $timezone = 'user') {
2385 $timezone = strtolower($timezone);
2387 if ($timezone == 'gmt') {
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);
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');
2397 $timestamp = gmmktime($date_bits[4], $date_bits[5], $date_bits[6], $date_bits[2], $date_bits[3], $date_bits[1]);
2398 $timestamp -= $offset;
2400 return gmdate('Y-m-d H:i:s', $timestamp);
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);
2408 * Adds a element attributes to open links in new windows.
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.
2416 * @param string $text Content to replace links to open in a new window.
2417 * @return string Content that has filtered links.
2419 function popuplinks($text) {
2420 $text = preg_replace('/<a (.+?)>/i', "<a $1 target='_blank' rel='external'>", $text);
2425 * Strips out all characters that are not allowable in an email.
2429 * @param string $email Email address to filter.
2430 * @return string Filtered email address.
2432 function sanitize_email( $email ) {
2433 // Test for the minimum length the email can be
2434 if ( strlen( $email ) < 3 ) {
2436 * Filter a sanitized email address.
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.
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.
2448 return apply_filters( 'sanitize_email', '', $email, 'email_too_short' );
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' );
2457 // Split out the local and domain parts
2458 list( $local, $domain ) = explode( '@', $email, 2 );
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' );
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' );
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' );
2483 // Split the domain into subs
2484 $subs = explode( '.', $domain );
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' );
2492 // Create an array that will contain valid subs
2493 $new_subs = array();
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-" );
2500 // Test for invalid characters
2501 $sub = preg_replace( '/[^a-z0-9-]+/i', '', $sub );
2503 // If there's anything left, add it to the valid subs
2504 if ( '' !== $sub ) {
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' );
2515 // Join valid subs into the new domain
2516 $domain = join( '.', $new_subs );
2518 // Put the email back together
2519 $email = $local . '@' . $domain;
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 );
2527 * Determines the difference between two timestamps.
2529 * The difference is returned in a human readable format such as "1 hour",
2530 * "5 mins", "2 days".
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.
2538 function human_time_diff( $from, $to = '' ) {
2539 if ( empty( $to ) ) {
2543 $diff = (int) abs( $to - $from );
2545 if ( $diff < HOUR_IN_SECONDS ) {
2546 $mins = round( $diff / MINUTE_IN_SECONDS );
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 );
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 );
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 );
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 ) );
2570 $since = sprintf( _n( '%s month', '%s months', $months ), $months );
2571 } elseif ( $diff >= YEAR_IN_SECONDS ) {
2572 $years = round( $diff / YEAR_IN_SECONDS );
2575 $since = sprintf( _n( '%s year', '%s years', $years ), $years );
2579 * Filter the human readable difference between two timestamps.
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.
2588 return apply_filters( 'human_time_diff', $since, $diff, $from, $to );
2592 * Generates an excerpt from the content, if needed.
2594 * The excerpt word amount will be 55 words and if the amount is greater than
2595 * that, then the string ' […]' will be appended to the excerpt. If the string
2596 * is less than 55 words, then the content will be returned as is.
2598 * The 55 word limit can be modified by plugins/themes using the excerpt_length filter
2599 * The ' […]' string can be modified by plugins/themes using the excerpt_more filter
2603 * @param string $text Optional. The excerpt. If set to empty, an excerpt is generated.
2604 * @return string The excerpt.
2606 function wp_trim_excerpt($text = '') {
2607 $raw_excerpt = $text;
2608 if ( '' == $text ) {
2609 $text = get_the_content('');
2611 $text = strip_shortcodes( $text );
2613 /** This filter is documented in wp-includes/post-template.php */
2614 $text = apply_filters( 'the_content', $text );
2615 $text = str_replace(']]>', ']]>', $text);
2618 * Filter the number of words in an excerpt.
2622 * @param int $number The number of words. Default 55.
2624 $excerpt_length = apply_filters( 'excerpt_length', 55 );
2626 * Filter the string in the "more" link displayed after a trimmed excerpt.
2630 * @param string $more_string The string shown within the more link.
2632 $excerpt_more = apply_filters( 'excerpt_more', ' ' . '[…]' );
2633 $text = wp_trim_words( $text, $excerpt_length, $excerpt_more );
2636 * Filter the trimmed excerpt string.
2640 * @param string $text The trimmed text.
2641 * @param string $raw_excerpt The text prior to trimming.
2643 return apply_filters( 'wp_trim_excerpt', $text, $raw_excerpt );
2647 * Trims text to a certain number of words.
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.
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 '…'.
2658 * @return string Trimmed text.
2660 function wp_trim_words( $text, $num_words = 55, $more = null ) {
2661 if ( null === $more )
2662 $more = __( '…' );
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 );
2673 $words_array = preg_split( "/[\n\r\t ]+/", $text, $num_words + 1, PREG_SPLIT_NO_EMPTY );
2676 if ( count( $words_array ) > $num_words ) {
2677 array_pop( $words_array );
2678 $text = implode( $sep, $words_array );
2679 $text = $text . $more;
2681 $text = implode( $sep, $words_array );
2684 * Filter the text content after words have been trimmed.
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. ….
2691 * @param string $original_text The text before it was trimmed.
2693 return apply_filters( 'wp_trim_words', $text, $num_words, $more, $original_text );
2697 * Converts named entities into numbered entities.
2701 * @param string $text The text within which entities will be converted.
2702 * @return string Text with converted entities.
2704 function ent2ncr($text) {
2707 * Filter text before named entities are converted into numbered entities.
2709 * A non-null string must be returned for the filter to be evaluated.
2713 * @param null $converted_text The text to be converted. Default null.
2714 * @param string $text The text prior to entity conversion.
2716 $filtered = apply_filters( 'pre_ent2ncr', null, $text );
2717 if( null !== $filtered )
2721 '"' => '"',
2726 ' ' => ' ',
2727 '¡' => '¡',
2728 '¢' => '¢',
2729 '£' => '£',
2730 '¤' => '¤',
2731 '¥' => '¥',
2732 '¦' => '¦',
2733 '&brkbar;' => '¦',
2734 '§' => '§',
2735 '¨' => '¨',
2736 '¨' => '¨',
2737 '©' => '©',
2738 'ª' => 'ª',
2739 '«' => '«',
2740 '¬' => '¬',
2741 '­' => '­',
2742 '®' => '®',
2743 '¯' => '¯',
2744 '&hibar;' => '¯',
2745 '°' => '°',
2746 '±' => '±',
2747 '²' => '²',
2748 '³' => '³',
2749 '´' => '´',
2750 'µ' => 'µ',
2751 '¶' => '¶',
2752 '·' => '·',
2753 '¸' => '¸',
2754 '¹' => '¹',
2755 'º' => 'º',
2756 '»' => '»',
2757 '¼' => '¼',
2758 '½' => '½',
2759 '¾' => '¾',
2760 '¿' => '¿',
2761 'À' => 'À',
2762 'Á' => 'Á',
2763 'Â' => 'Â',
2764 'Ã' => 'Ã',
2765 'Ä' => 'Ä',
2766 'Å' => 'Å',
2767 'Æ' => 'Æ',
2768 'Ç' => 'Ç',
2769 'È' => 'È',
2770 'É' => 'É',
2771 'Ê' => 'Ê',
2772 'Ë' => 'Ë',
2773 'Ì' => 'Ì',
2774 'Í' => 'Í',
2775 'Î' => 'Î',
2776 'Ï' => 'Ï',
2777 'Ð' => 'Ð',
2778 'Ñ' => 'Ñ',
2779 'Ò' => 'Ò',
2780 'Ó' => 'Ó',
2781 'Ô' => 'Ô',
2782 'Õ' => 'Õ',
2783 'Ö' => 'Ö',
2784 '×' => '×',
2785 'Ø' => 'Ø',
2786 'Ù' => 'Ù',
2787 'Ú' => 'Ú',
2788 'Û' => 'Û',
2789 'Ü' => 'Ü',
2790 'Ý' => 'Ý',
2791 'Þ' => 'Þ',
2792 'ß' => 'ß',
2793 'à' => 'à',
2794 'á' => 'á',
2795 'â' => 'â',
2796 'ã' => 'ã',
2797 'ä' => 'ä',
2798 'å' => 'å',
2799 'æ' => 'æ',
2800 'ç' => 'ç',
2801 'è' => 'è',
2802 'é' => 'é',
2803 'ê' => 'ê',
2804 'ë' => 'ë',
2805 'ì' => 'ì',
2806 'í' => 'í',
2807 'î' => 'î',
2808 'ï' => 'ï',
2809 'ð' => 'ð',
2810 'ñ' => 'ñ',
2811 'ò' => 'ò',
2812 'ó' => 'ó',
2813 'ô' => 'ô',
2814 'õ' => 'õ',
2815 'ö' => 'ö',
2816 '÷' => '÷',
2817 'ø' => 'ø',
2818 'ù' => 'ù',
2819 'ú' => 'ú',
2820 'û' => 'û',
2821 'ü' => 'ü',
2822 'ý' => 'ý',
2823 'þ' => 'þ',
2824 'ÿ' => 'ÿ',
2825 'Œ' => 'Œ',
2826 'œ' => 'œ',
2827 'Š' => 'Š',
2828 'š' => 'š',
2829 'Ÿ' => 'Ÿ',
2830 'ƒ' => 'ƒ',
2831 'ˆ' => 'ˆ',
2832 '˜' => '˜',
2833 'Α' => 'Α',
2834 'Β' => 'Β',
2835 'Γ' => 'Γ',
2836 'Δ' => 'Δ',
2837 'Ε' => 'Ε',
2838 'Ζ' => 'Ζ',
2839 'Η' => 'Η',
2840 'Θ' => 'Θ',
2841 'Ι' => 'Ι',
2842 'Κ' => 'Κ',
2843 'Λ' => 'Λ',
2847 'Ο' => 'Ο',
2849 'Ρ' => 'Ρ',
2850 'Σ' => 'Σ',
2851 'Τ' => 'Τ',
2852 'Υ' => 'Υ',
2853 'Φ' => 'Φ',
2854 'Χ' => 'Χ',
2855 'Ψ' => 'Ψ',
2856 'Ω' => 'Ω',
2857 'α' => 'α',
2858 'β' => 'β',
2859 'γ' => 'γ',
2860 'δ' => 'δ',
2861 'ε' => 'ε',
2862 'ζ' => 'ζ',
2863 'η' => 'η',
2864 'θ' => 'θ',
2865 'ι' => 'ι',
2866 'κ' => 'κ',
2867 'λ' => 'λ',
2871 'ο' => 'ο',
2873 'ρ' => 'ρ',
2874 'ς' => 'ς',
2875 'σ' => 'σ',
2876 'τ' => 'τ',
2877 'υ' => 'υ',
2878 'φ' => 'φ',
2879 'χ' => 'χ',
2880 'ψ' => 'ψ',
2881 'ω' => 'ω',
2882 'ϑ' => 'ϑ',
2883 'ϒ' => 'ϒ',
2884 'ϖ' => 'ϖ',
2885 ' ' => ' ',
2886 ' ' => ' ',
2887 ' ' => ' ',
2888 '‌' => '‌',
2889 '‍' => '‍',
2890 '‎' => '‎',
2891 '‏' => '‏',
2892 '–' => '–',
2893 '—' => '—',
2894 '‘' => '‘',
2895 '’' => '’',
2896 '‚' => '‚',
2897 '“' => '“',
2898 '”' => '”',
2899 '„' => '„',
2900 '†' => '†',
2901 '‡' => '‡',
2902 '•' => '•',
2903 '…' => '…',
2904 '‰' => '‰',
2905 '′' => '′',
2906 '″' => '″',
2907 '‹' => '‹',
2908 '›' => '›',
2909 '‾' => '‾',
2910 '⁄' => '⁄',
2911 '€' => '€',
2912 'ℑ' => 'ℑ',
2913 '℘' => '℘',
2914 'ℜ' => 'ℜ',
2915 '™' => '™',
2916 'ℵ' => 'ℵ',
2917 '↵' => '↵',
2918 '⇐' => '⇐',
2919 '⇑' => '⇑',
2920 '⇒' => '⇒',
2921 '⇓' => '⇓',
2922 '⇔' => '⇔',
2923 '∀' => '∀',
2924 '∂' => '∂',
2925 '∃' => '∃',
2926 '∅' => '∅',
2927 '∇' => '∇',
2928 '∈' => '∈',
2929 '∉' => '∉',
2930 '∋' => '∋',
2931 '∏' => '∏',
2932 '∑' => '∑',
2933 '−' => '−',
2934 '∗' => '∗',
2935 '√' => '√',
2936 '∝' => '∝',
2937 '∞' => '∞',
2938 '∠' => '∠',
2939 '∧' => '∧',
2940 '∨' => '∨',
2941 '∩' => '∩',
2942 '∪' => '∪',
2943 '∫' => '∫',
2944 '∴' => '∴',
2945 '∼' => '∼',
2946 '≅' => '≅',
2947 '≈' => '≈',
2948 '≠' => '≠',
2949 '≡' => '≡',
2950 '≤' => '≤',
2951 '≥' => '≥',
2952 '⊂' => '⊂',
2953 '⊃' => '⊃',
2954 '⊄' => '⊄',
2955 '⊆' => '⊆',
2956 '⊇' => '⊇',
2957 '⊕' => '⊕',
2958 '⊗' => '⊗',
2959 '⊥' => '⊥',
2960 '⋅' => '⋅',
2961 '⌈' => '⌈',
2962 '⌉' => '⌉',
2963 '⌊' => '⌊',
2964 '⌋' => '⌋',
2965 '⟨' => '〈',
2966 '⟩' => '〉',
2967 '←' => '←',
2968 '↑' => '↑',
2969 '→' => '→',
2970 '↓' => '↓',
2971 '↔' => '↔',
2972 '◊' => '◊',
2973 '♠' => '♠',
2974 '♣' => '♣',
2975 '♥' => '♥',
2976 '♦' => '♦'
2979 return str_replace( array_keys($to_ncr), array_values($to_ncr), $text );
2983 * Formats text for the rich text editor.
2985 * The filter 'richedit_pre' is applied here. If $text is empty the filter will
2986 * be applied to an empty string.
2990 * @param string $text The text to be formatted.
2991 * @return string The formatted text after filter is applied.
2993 function wp_richedit_pre($text) {
2994 if ( empty( $text ) ) {
2996 * Filter text returned for the rich text editor.
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.
3002 * If a non-empty string is passed, the filter is evaluated on the wp_richedit_pre()
3003 * return after being formatted.
3007 * @param string $output Text for the rich text editor.
3009 return apply_filters( 'richedit_pre', '' );
3012 $output = convert_chars($text);
3013 $output = wpautop($output);
3014 $output = htmlspecialchars($output, ENT_NOQUOTES, get_option( 'blog_charset' ) );
3016 /** This filter is documented in wp-includes/formatting.php */
3017 return apply_filters( 'richedit_pre', $output );
3021 * Formats text for the HTML editor.
3023 * Unless $output is empty it will pass through htmlspecialchars before the
3024 * 'htmledit_pre' filter is applied.
3028 * @param string $output The text to be formatted.
3029 * @return string Formatted text after filter applied.
3031 function wp_htmledit_pre($output) {
3032 if ( !empty($output) )
3033 $output = htmlspecialchars($output, ENT_NOQUOTES, get_option( 'blog_charset' ) ); // convert only < > &
3036 * Filter the text before it is formatted for the HTML editor.
3040 * @param string $output The HTML-formatted text.
3042 return apply_filters( 'htmledit_pre', $output );
3046 * Perform a deep string replace operation to ensure the values in $search are no longer present
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
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.
3059 function _deep_replace( $search, $subject ) {
3060 $subject = (string) $subject;
3064 $subject = str_replace( $search, '', $subject, $count );
3071 * Escapes data for use in a MySQL query.
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.
3078 * @param string|array $data Unescaped data
3079 * @return string|array Escaped data
3081 function esc_sql( $data ) {
3083 return $wpdb->_escape( $data );
3087 * Checks and cleans a URL.
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.
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.
3101 function esc_url( $url, $protocols = null, $_context = 'display' ) {
3102 $original_url = $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).
3114 if ( strpos($url, ':') === false && ! in_array( $url[0], array( '/', '#', '?' ) ) &&
3115 ! preg_match('/^[a-z0-9-]+?\.php/i', $url) )
3116 $url = 'http://' . $url;
3118 // Replace ampersands and single quotes only when displaying.
3119 if ( 'display' == $_context ) {
3120 $url = wp_kses_normalize_entities( $url );
3121 $url = str_replace( '&', '&', $url );
3122 $url = str_replace( "'", ''', $url );
3125 if ( '/' === $url[0] ) {
3126 $good_protocol_url = $url;
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 ) )
3136 * Filter a string cleaned and escaped for output as a URL.
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.
3144 return apply_filters( 'clean_url', $good_protocol_url, $original_url, $_context );
3148 * Performs esc_url() for database usage.
3152 * @param string $url The URL to be cleaned.
3153 * @param array $protocols An array of acceptable protocols.
3154 * @return string The cleaned URL.
3156 function esc_url_raw( $url, $protocols = null ) {
3157 return esc_url( $url, $protocols, 'db' );
3161 * Convert entities, while preserving already-encoded entities.
3163 * @link http://www.php.net/htmlentities Borrowed from the PHP Manual user notes.
3167 * @param string $myHTML The text to be converted.
3168 * @return string Converted text.
3170 function 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};)/", "&", strtr($myHTML, $translation_table) );
3177 * Escape single quotes, htmlspecialchar " < > &, and fix line endings.
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.
3185 * @param string $text The text to be escaped.
3186 * @return string Escaped text.
3188 function 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 ) );
3195 * Filter a string cleaned and escaped for output in JavaScript.
3197 * Text passed to esc_js() is stripped of invalid or special characters,
3198 * and properly slashed for output.
3202 * @param string $safe_text The text after it has been escaped.
3203 * @param string $text The text prior to being escaped.
3205 return apply_filters( 'js_escape', $safe_text, $text );
3209 * Escaping for HTML blocks.
3213 * @param string $text
3216 function esc_html( $text ) {
3217 $safe_text = wp_check_invalid_utf8( $text );
3218 $safe_text = _wp_specialchars( $safe_text, ENT_QUOTES );
3220 * Filter a string cleaned and escaped for output in HTML.
3222 * Text passed to esc_html() is stripped of invalid or special characters
3227 * @param string $safe_text The text after it has been escaped.
3228 * @param string $text The text prior to being escaped.
3230 return apply_filters( 'esc_html', $safe_text, $text );
3234 * Escaping for HTML attributes.
3238 * @param string $text
3241 function esc_attr( $text ) {
3242 $safe_text = wp_check_invalid_utf8( $text );
3243 $safe_text = _wp_specialchars( $safe_text, ENT_QUOTES );
3245 * Filter a string cleaned and escaped for output in an HTML attribute.
3247 * Text passed to esc_attr() is stripped of invalid or special characters
3252 * @param string $safe_text The text after it has been escaped.
3253 * @param string $text The text prior to being escaped.
3255 return apply_filters( 'attribute_escape', $safe_text, $text );
3259 * Escaping for textarea values.
3263 * @param string $text
3266 function esc_textarea( $text ) {
3267 $safe_text = htmlspecialchars( $text, ENT_QUOTES, get_option( 'blog_charset' ) );
3269 * Filter a string cleaned and escaped for output in a textarea element.
3273 * @param string $safe_text The text after it has been escaped.
3274 * @param string $text The text prior to being escaped.
3276 return apply_filters( 'esc_textarea', $safe_text, $text );
3280 * Escape an HTML tag name.
3284 * @param string $tag_name
3287 function tag_escape($tag_name) {
3288 $safe_tag = strtolower( preg_replace('/[^a-zA-Z0-9_:]/', '', $tag_name) );
3290 * Filter a string cleaned and escaped for output as an HTML tag.
3294 * @param string $safe_tag The tag name after it has been escaped.
3295 * @param string $tag_name The text before it was escaped.
3297 return apply_filters( 'tag_escape', $safe_tag, $tag_name );
3301 * Convert full URL paths to absolute paths.
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.
3307 * @since 4.1.0 Support was added for relative URLs.
3309 * @param string $link Full URL path.
3310 * @return string Absolute path.
3312 function wp_make_link_relative( $link ) {
3313 return preg_replace( '|^(https?:)?//[^/]+(/.*)|i', '$2', $link );
3317 * Sanitises various option values based on the nature of the option.
3319 * This is basically a switch statement which will pass $value through a number
3320 * of functions depending on the $option.
3324 * @param string $option The name of the option.
3325 * @param string $value The unsanitised value.
3326 * @return string Sanitized value.
3328 function sanitize_option($option, $value) {
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.' ) );
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 );
3365 case 'posts_per_page':
3366 case 'posts_per_rss':
3367 $value = (int) $value;
3368 if ( empty($value) )
3371 $value = abs($value);
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 == '')
3381 case 'blogdescription':
3383 $value = $wpdb->strip_invalid_text_for_column( $wpdb->options, 'option_value', $value );
3384 $value = wp_kses_post( $value );
3385 $value = esc_html( $value );
3388 case 'blog_charset':
3389 $value = preg_replace('/[^a-zA-Z0-9_-]/', '', $value); // strips slashes
3393 // This is the value if the settings checkbox is not checked on POST. Don't rely on this.
3394 if ( null === $value )
3397 $value = intval( $value );
3402 case 'mailserver_url':
3403 case 'mailserver_login':
3404 case 'mailserver_pass':
3406 $value = $wpdb->strip_invalid_text_for_column( $wpdb->options, 'option_value', $value );
3407 $value = strip_tags( $value );
3408 $value = wp_kses_data( $value );
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 );
3419 $value = preg_replace('/[^0-9:.-]/', '', $value); // strips slashes
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);
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.'));
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);
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.'));
3445 $allowed = get_available_languages();
3446 if ( ! is_multisite() && defined( 'WPLANG' ) && '' !== WPLANG && 'en_US' !== WPLANG ) {
3447 $allowed[] = WPLANG;
3449 if ( ! in_array( $value, $allowed ) && ! empty( $value ) ) {
3450 $value = get_option( $option );
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 );
3459 $value = array_values( array_filter( array_map( 'trim', $value ) ) );
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 );
3471 $domains = array_values( array_filter( array_map( 'trim', $value ) ) );
3474 foreach ( $domains as $domain ) {
3475 if ( ! preg_match( '/(--|\.\.)/', $domain ) && preg_match( '|^([a-zA-Z0-9-\.])+$|', $domain ) )
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.') );
3491 case 'permalink_structure':
3492 case 'category_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 );
3499 case 'default_role' :
3500 if ( ! get_role( $value ) && get_role( 'subscriber' ) )
3501 $value = 'subscriber';
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 );
3515 * Filter an option value following sanitization.
3519 * @param string $value The sanitized option value.
3520 * @param string $option The option name.
3522 $value = apply_filters( "sanitize_option_{$option}", $value, $option );
3528 * Parses a string into variables to be stored in an array.
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.
3535 * @param string $string The string to be parsed.
3536 * @param array $array Variables will be stored in this array.
3538 function wp_parse_str( $string, &$array ) {
3539 parse_str( $string, $array );
3540 if ( get_magic_quotes_gpc() )
3541 $array = stripslashes_deep( $array );
3543 * Filter the array of variables derived from a parsed string.
3547 * @param array $array The array populated with variables.
3549 $array = apply_filters( 'wp_parse_str', $array );
3553 * Convert lone less than signs.
3555 * KSES already converts lone greater than signs.
3559 * @param string $text Text to be converted.
3560 * @return string Converted text.
3562 function wp_pre_kses_less_than( $text ) {
3563 return preg_replace_callback('%<[^>]*?((?=<)|>|$)%', 'wp_pre_kses_less_than_callback', $text);
3567 * Callback function used by preg_replace.
3571 * @param array $matches Populated by matches to preg_replace.
3572 * @return string The text returned after esc_html if needed.
3574 function wp_pre_kses_less_than_callback( $matches ) {
3575 if ( false === strpos($matches[0], '>') )
3576 return esc_html($matches[0]);
3581 * WordPress implementation of PHP sprintf() with filters.
3584 * @link http://www.php.net/sprintf
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.
3590 function wp_sprintf( $pattern ) {
3591 $args = func_get_args();
3592 $len = strlen($pattern);
3596 while ( $len > $start ) {
3597 // Last character: append and break
3598 if ( strlen($pattern) - 1 == $start ) {
3599 $result .= substr($pattern, -1);
3603 // Literal %: append and continue
3604 if ( substr($pattern, $start, 2) == '%%' ) {
3610 // Get fragment before next %
3611 $end = strpos($pattern, '%', $start + 1);
3612 if ( false === $end )
3614 $fragment = substr($pattern, $start, $end - $start);
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);
3624 $arg = isset($args[$arg_index]) ? $args[$arg_index] : '';
3628 * Filter a fragment from the pattern passed to wp_sprintf().
3630 * If the fragment is unchanged, then sprintf() will be run on the fragment.
3634 * @param string $fragment A fragment from the pattern.
3635 * @param string $arg The argument.
3637 $_fragment = apply_filters( 'wp_sprintf', $fragment, $arg );
3638 if ( $_fragment != $fragment )
3639 $fragment = $_fragment;
3641 $fragment = sprintf($fragment, strval($arg) );
3644 // Append to result and move to next fragment
3645 $result .= $fragment;
3652 * Localize list items before the rest of the content.
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.
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.
3664 function wp_sprintf_l($pattern, $args) {
3666 if ( substr($pattern, 0, 2) != '%l' )
3669 // Nothing to work with
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.
3678 * Please note: Ampersands and entities should be avoided here.
3682 * @param array $delimiters An array of translated delimiters.
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'), '', '' ),
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
3700 $arg = array_shift($args);
3703 $result .= $l['between_last_two'] . $arg;
3705 $result .= $l['between'] . $arg;
3707 return $result . substr($pattern, 2);
3711 * Safely extracts not more than the first $count characters from html string.
3713 * UTF-8, tags and entities safe prefix extraction. Entities inside will *NOT*
3714 * be counted as one character. For example & will be counted as 4, < as
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.
3724 function wp_html_excerpt( $str, $count, $more = null ) {
3725 if ( null === $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;
3737 * Add a Base url to relative links in passed content.
3739 * By default it supports the 'src' and 'href' attributes. However this can be
3740 * changed via the 3rd param.
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.
3749 function 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 );
3757 * Callback to add a base url to relative links in passed content.
3762 * @param string $m The matched link.
3763 * @return string The processed link.
3765 function _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() ) ?
3771 WP_HTTP::make_absolute_url( $m[3], $_links_add_base )
3777 * Adds a Target attribute to all links in passed content.
3779 * This function by default only applies to `<a>` tags, however this can be
3780 * modified by the 3rd param.
3782 * *NOTE:* Any current target attributed will be stripped and replaced.
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.
3791 function 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 );
3799 * Callback to add a target attribute to all links in passed content.
3804 * @param string $m The matched link.
3805 * @return string The processed link.
3807 function _links_add_target( $m ) {
3808 global $_links_add_target;
3810 $link = preg_replace('|( target=([\'"])(.*?)\2)|i', '', $m[2]);
3811 return '<' . $tag . $link . ' target="' . esc_attr( $_links_add_target ) . '">';
3815 * Normalize EOL characters and strip duplicate whitespace.
3819 * @param string $str The string to normalize.
3820 * @return string The normalized string.
3822 function normalize_whitespace( $str ) {
3823 $str = trim( $str );
3824 $str = str_replace( "\r", "\n", $str );
3825 $str = preg_replace( array( '/\n+/', '/[ \t]+/' ), array( "\n", ' ' ), $str );
3830 * Properly strip all HTML tags including script and style
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 ''
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.
3842 function wp_strip_all_tags($string, $remove_breaks = false) {
3843 $string = preg_replace( '@<(script|style)[^>]*?>.*?</\\1>@si', '', $string );
3844 $string = strip_tags($string);
3846 if ( $remove_breaks )
3847 $string = preg_replace('/[\r\n\t ]+/', ' ', $string);
3849 return trim( $string );
3853 * Sanitize a string from user input or from the db
3855 * check for invalid UTF-8,
3856 * Convert single < characters to entity,
3858 * remove line breaks, tabs and extra white space,
3863 * @param string $str
3866 function sanitize_text_field($str) {
3867 $filtered = wp_check_invalid_utf8( $str );
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 );
3874 $filtered = trim( preg_replace('/[\r\n\t ]+/', ' ', $filtered) );
3878 while ( preg_match('/%[a-f0-9]{2}/i', $filtered, $match) ) {
3879 $filtered = str_replace($match[0], '', $filtered);
3884 // Strip out the whitespace that may now exist after removing the octets.
3885 $filtered = trim( preg_replace('/ +/', ' ', $filtered) );
3889 * Filter a sanitized text field string.
3893 * @param string $filtered The sanitized string.
3894 * @param string $str The string prior to being sanitized.
3896 return apply_filters( 'sanitize_text_field', $filtered, $str );
3900 * i18n friendly version of basename()
3904 * @param string $path A path.
3905 * @param string $suffix If the filename ends in suffix this will also be cut off.
3908 function wp_basename( $path, $suffix = '' ) {
3909 return urldecode( basename( str_replace( array( '%2F', '%5C' ), '/', urlencode( $path ) ), $suffix ) );
3913 * Forever eliminate "Wordpress" from the planet (or at least the little bit we can influence).
3915 * Violating our coding standards for a good function name.
3919 function 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( '“', 'opening curly double quote' );
3929 array( ' Wordpress', '‘Wordpress', $dblq . 'Wordpress', '>Wordpress', '(Wordpress' ),
3930 array( ' WordPress', '‘WordPress', $dblq . 'WordPress', '>WordPress', '(WordPress' ),
3936 * Sanitize a mime type
3940 * @param string $mime_type Mime type
3941 * @return string Sanitized mime type
3943 function sanitize_mime_type( $mime_type ) {
3944 $sani_mime_type = preg_replace( '/[^-+*.a-zA-Z0-9\/]/', '', $mime_type );
3946 * Filter a mime type following sanitization.
3950 * @param string $sani_mime_type The sanitized mime type.
3951 * @param string $mime_type The mime type prior to sanitization.
3953 return apply_filters( 'sanitize_mime_type', $sani_mime_type, $mime_type );
3957 * Sanitize space or carriage return separated URLs that are used to send trackbacks.
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.
3964 function 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] );
3970 $urls_to_ping = array_map( 'esc_url_raw', $urls_to_ping );
3971 $urls_to_ping = implode( "\n", $urls_to_ping );
3973 * Filter a list of trackback URLs following sanitization.
3975 * The string returned here consists of a space or carriage return-delimited list
3976 * of trackback URLs.
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.
3983 return apply_filters( 'sanitize_trackback_urls', $urls_to_ping, $to_ping );
3987 * Add slashes to a string or array of strings.
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.
3994 * @param string|array $value String or array of strings to slash.
3995 * @return string|array Slashed $value
3997 function 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 );
4003 $value[$k] = addslashes( $v );
4007 $value = addslashes( $value );
4014 * Remove slashes from a string or array of strings.
4016 * This should be used to remove slashes from data passed to core API that
4017 * expects data to be unslashed.
4021 * @param string|array $value String or array of strings to unslash.
4022 * @return string|array Unslashed $value
4024 function wp_unslash( $value ) {
4025 return stripslashes_deep( $value );
4029 * Extract and return the first URL from passed content.
4033 * @param string $content A string which might contain a URL.
4034 * @return string The found URL.
4036 function get_url_in_content( $content ) {
4037 if ( empty( $content ) ) {
4041 if ( preg_match( '/<a\s[^>]*?href=([\'"])(.+?)\1/is', $content, $matches ) ) {
4042 return esc_url_raw( $matches[2] );
4049 * Returns the regexp for common whitespace characters.
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.
4057 * @return string The spaces regexp.
4059 function wp_spaces_regexp() {
4062 if ( empty( $spaces ) ) {
4064 * Filter the regexp for common whitespace characters.
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.
4073 * @param string $spaces Regexp pattern for matching common whitespace characters.
4075 $spaces = apply_filters( 'wp_spaces_regexp', '[\r\n\t ]|\xC2\xA0| ' );
4082 * Print the important emoji-related styles.
4086 function print_emoji_styles() {
4087 static $printed = false;
4095 <style type="text/css">
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;
4112 function print_emoji_detection_script() {
4114 static $printed = false;
4124 * Filter the URL where emoji images are hosted.
4128 * @param string The emoji base URL.
4130 'baseUrl' => apply_filters( 'emoji_url', set_url_scheme( '//s.w.org/images/core/emoji/72x72/' ) ),
4133 * Filter the extension of the emoji files.
4137 * @param string The emoji extension. Default .png.
4139 'ext' => apply_filters( 'emoji_ext', '.png' ),
4142 $version = 'ver=' . $wp_version;
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' ),
4153 <script type="text/javascript">
4154 window._wpemojiSettings = <?php echo wp_json_encode( $settings ); ?>;
4155 <?php readfile( ABSPATH . WPINC . "/js/wp-emoji-loader.js" ); ?>
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' ),
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.
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.
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,g;c.supports={simple:d("simple"),flag:d("flag")},c.DOMReady=!1,c.readyCallback=function(){c.DOMReady=!0},c.supports.simple&&c.supports.flag||(g=function(){c.readyCallback()},b.addEventListener?(b.addEventListener("DOMContentLoaded",g,!1),a.addEventListener("load",g,!1)):(a.attachEvent("onload",g),b.attachEvent("onreadystatechange",function(){"complete"===b.readyState&&c.readyCallback()})),f=c.source||{},f.concatemoji?e(f.concatemoji):f.wpemoji&&f.twemoji&&(e(f.twemoji),e(f.wpemoji)))}(window,document,window._wpemojiSettings);
4184 * Convert any 4 byte emoji in a string to their equivalent HTML entity.
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.
4189 * This allows us to store emoji in a DB using the utf8 character set.
4193 * @param string $content The content to encode.
4194 * @return string The encoded content.
4196 function wp_encode_emoji( $content ) {
4197 if ( function_exists( 'mb_convert_encoding' ) ) {
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
4209 if ( preg_match_all( $regex, $content, $matches ) ) {
4210 if ( ! empty( $matches[1] ) ) {
4211 foreach( $matches[1] as $emoji ) {
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.
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 );
4231 * Convert emoji to a static img element.
4235 * @param string $text The content to encode.
4236 * @return string The encoded content.
4238 function wp_staticize_emoji( $text ) {
4239 $text = wp_encode_emoji( $text );
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/' ) );
4244 /** This filter is documented in wp-includes/formatting.php */
4245 $ext = apply_filters( 'emoji_ext', '.png' );
4249 * HTML loop taken from smiley function, which was taken from texturize function.
4250 * It'll never be consolidated.
4252 * First, capture the tags as well as in between.
4254 $textarr = preg_split( '/(<.*>)/U', $text, -1, PREG_SPLIT_DELIM_CAPTURE );
4255 $stop = count( $textarr );
4257 // Ignore processing of specific tags.
4258 $tags_to_ignore = 'code|pre|style|script|textarea';
4259 $ignore_block_element = '';
4261 for ( $i = 0; $i < $stop; $i++ ) {
4262 $content = $textarr[$i];
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];
4269 // If it's not a tag and not in ignore block.
4270 if ( '' == $ignore_block_element && strlen( $content ) > 0 && '<' != $content[0] ) {
4272 if ( preg_match_all( '/(DZ(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 );
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 ) );
4280 $content = str_replace( $flag, $entity, $content );
4285 // Loosely match the Emoji Unicode range.
4286 $regex = '/(&#x[2-3][0-9a-f]{3};|[1-6][0-9a-f]{2};)/';
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 ) );
4295 $content = str_replace( $emoji, $entity, $content );
4301 // Did we exit ignore block.
4302 if ( '' != $ignore_block_element && '</' . $ignore_block_element . '>' == $content ) {
4303 $ignore_block_element = '';
4306 $output .= $content;
4313 * Convert emoji in emails into static images.
4317 * @param array $mail The email data array.
4318 * @return array The email data array, with emoji in the message staticized.
4320 function wp_staticize_emoji_for_email( $mail ) {
4321 if ( ! isset( $mail['message'] ) ) {
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.
4333 if ( isset( $mail['headers'] ) ) {
4334 if ( is_array( $mail['headers'] ) ) {
4335 $headers = $mail['headers'];
4337 $headers = explode( "\n", str_replace( "\r\n", "\n", $mail['headers'] ) );
4341 foreach ( $headers as $header ) {
4342 if ( strpos($header, ':') === false ) {
4346 // Explode them out.
4347 list( $name, $content ) = explode( ':', trim( $header ), 2 );
4350 $name = trim( $name );
4351 $content = trim( $content );
4353 if ( 'content-type' === strtolower( $name ) ) {
4354 if ( strpos( $content, ';' ) !== false ) {
4355 list( $type, $charset ) = explode( ';', $content );
4356 $content_type = trim( $type );
4358 $content_type = trim( $content );
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';
4369 /** This filter is documented in wp-includes/pluggable.php */
4370 $content_type = apply_filters( 'wp_mail_content_type', $content_type );
4372 if ( 'text/html' === $content_type ) {
4373 $mail['message'] = wp_staticize_emoji( $mail['message'] );