PageRenderTime 184ms CodeModel.GetById 16ms app.highlight 54ms RepoModel.GetById 2ms app.codeStats 1ms

/wp-includes/formatting.php

https://bitbucket.org/acipriani/madeinapulia.com
PHP | 4005 lines | 2383 code | 331 blank | 1291 comment | 333 complexity | d8091ddba1ba8e1019c8dd48caed66cc MD5 | raw file

Large files files are truncated, but you can click here to view the full file

   1<?php
   2/**
   3 * Main WordPress Formatting API.
   4 *
   5 * Handles many functions for formatting output.
   6 *
   7 * @package WordPress
   8 */
   9
  10/**
  11 * Replaces common plain text characters into formatted entities
  12 *
  13 * As an example,
  14 *
  15 *     'cause today's effort makes it worth tomorrow's "holiday" ...
  16 *
  17 * Becomes:
  18 *
  19 *     &#8217;cause today&#8217;s effort makes it worth tomorrow&#8217;s &#8220;holiday&#8221; &#8230;
  20 *
  21 * Code within certain html blocks are skipped.
  22 *
  23 * @since 0.71
  24 * @uses $wp_cockneyreplace Array of formatted entities for certain common phrases
  25 *
  26 * @param string $text The text to be formatted
  27 * @param bool $reset Set to true for unit testing. Translated patterns will reset.
  28 * @return string The string replaced with html entities
  29 */
  30function wptexturize($text, $reset = false) {
  31	global $wp_cockneyreplace, $shortcode_tags;
  32	static $static_characters, $static_replacements, $dynamic_characters, $dynamic_replacements,
  33		$default_no_texturize_tags, $default_no_texturize_shortcodes, $run_texturize = true;
  34
  35	// If there's nothing to do, just stop.
  36	if ( empty( $text ) || false === $run_texturize ) {
  37		return $text;
  38	}
  39
  40	// Set up static variables. Run once only.
  41	if ( $reset || ! isset( $static_characters ) ) {
  42		/**
  43		 * Filter whether to skip running wptexturize().
  44		 *
  45		 * Passing false to the filter will effectively short-circuit wptexturize().
  46		 * returning the original text passed to the function instead.
  47		 *
  48		 * The filter runs only once, the first time wptexturize() is called.
  49		 *
  50		 * @since 4.0.0
  51		 *
  52		 * @see wptexturize()
  53		 *
  54		 * @param bool $run_texturize Whether to short-circuit wptexturize().
  55		 */
  56		$run_texturize = apply_filters( 'run_wptexturize', $run_texturize );
  57		if ( false === $run_texturize ) {
  58			return $text;
  59		}
  60
  61		/* translators: opening curly double quote */
  62		$opening_quote = _x( '&#8220;', 'opening curly double quote' );
  63		/* translators: closing curly double quote */
  64		$closing_quote = _x( '&#8221;', 'closing curly double quote' );
  65
  66		/* translators: apostrophe, for example in 'cause or can't */
  67		$apos = _x( '&#8217;', 'apostrophe' );
  68
  69		/* translators: prime, for example in 9' (nine feet) */
  70		$prime = _x( '&#8242;', 'prime' );
  71		/* translators: double prime, for example in 9" (nine inches) */
  72		$double_prime = _x( '&#8243;', 'double prime' );
  73
  74		/* translators: opening curly single quote */
  75		$opening_single_quote = _x( '&#8216;', 'opening curly single quote' );
  76		/* translators: closing curly single quote */
  77		$closing_single_quote = _x( '&#8217;', 'closing curly single quote' );
  78
  79		/* translators: en dash */
  80		$en_dash = _x( '&#8211;', 'en dash' );
  81		/* translators: em dash */
  82		$em_dash = _x( '&#8212;', 'em dash' );
  83
  84		$default_no_texturize_tags = array('pre', 'code', 'kbd', 'style', 'script', 'tt');
  85		$default_no_texturize_shortcodes = array('code');
  86
  87		// if a plugin has provided an autocorrect array, use it
  88		if ( isset($wp_cockneyreplace) ) {
  89			$cockney = array_keys($wp_cockneyreplace);
  90			$cockneyreplace = array_values($wp_cockneyreplace);
  91		} elseif ( "'" != $apos ) { // Only bother if we're doing a replacement.
  92			$cockney = array( "'tain't", "'twere", "'twas", "'tis", "'twill", "'til", "'bout", "'nuff", "'round", "'cause" );
  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" );
  94		} else {
  95			$cockney = $cockneyreplace = array();
  96		}
  97
  98		$static_characters = array_merge( array( '...', '``', '\'\'', ' (tm)' ), $cockney );
  99		$static_replacements = array_merge( array( '&#8230;', $opening_quote, $closing_quote, ' &#8482;' ), $cockneyreplace );
 100
 101
 102		// Pattern-based replacements of characters.
 103		// Sort the remaining patterns into several arrays for performance tuning.
 104		$dynamic_characters = array( 'apos' => array(), 'quote' => array(), 'dash' => array() );
 105		$dynamic_replacements = array( 'apos' => array(), 'quote' => array(), 'dash' => array() );
 106		$dynamic = array();
 107		$spaces = wp_spaces_regexp();
 108
 109		// '99' and '99" are ambiguous among other patterns; assume it's an abbreviated year at the end of a quotation.
 110		if ( "'" !== $apos || "'" !== $closing_single_quote ) {
 111			$dynamic[ '/\'(\d\d)\'(?=\Z|[.,)}\-\]]|&gt;|' . $spaces . ')/' ] = $apos . '$1' . $closing_single_quote;
 112		}
 113		if ( "'" !== $apos || '"' !== $closing_quote ) {
 114			$dynamic[ '/\'(\d\d)"(?=\Z|[.,)}\-\]]|&gt;|' . $spaces . ')/' ] = $apos . '$1' . $closing_quote;
 115		}
 116
 117		// '99 '99s '99's (apostrophe)  But never '9 or '99% or '999 or '99.0.
 118		if ( "'" !== $apos ) {
 119			$dynamic[ '/\'(?=\d\d(?:\Z|(?![%\d]|[.,]\d)))/' ] = $apos;
 120		}
 121
 122		// Quoted Numbers like '0.42'
 123		if ( "'" !== $opening_single_quote && "'" !== $closing_single_quote ) {
 124			$dynamic[ '/(?<=\A|' . $spaces . ')\'(\d[.,\d]*)\'/' ] = $opening_single_quote . '$1' . $closing_single_quote;
 125		}
 126
 127		// Single quote at start, or preceded by (, {, <, [, ", -, or spaces.
 128		if ( "'" !== $opening_single_quote ) {
 129			$dynamic[ '/(?<=\A|[([{"\-]|&lt;|' . $spaces . ')\'/' ] = $opening_single_quote;
 130		}
 131
 132		// Apostrophe in a word.  No spaces, double apostrophes, or other punctuation.
 133		if ( "'" !== $apos ) {
 134			$dynamic[ '/(?<!' . $spaces . ')\'(?!\Z|[.,:;"\'(){}[\]\-]|&[lg]t;|' . $spaces . ')/' ] = $apos;
 135		}
 136
 137		// 9' (prime)
 138		if ( "'" !== $prime ) {
 139			$dynamic[ '/(?<=\d)\'/' ] = $prime;
 140		}
 141
 142		// Single quotes followed by spaces or ending punctuation.
 143		if ( "'" !== $closing_single_quote ) {
 144			$dynamic[ '/\'(?=\Z|[.,)}\-\]]|&gt;|' . $spaces . ')/' ] = $closing_single_quote;
 145		}
 146
 147		$dynamic_characters['apos'] = array_keys( $dynamic );
 148		$dynamic_replacements['apos'] = array_values( $dynamic );
 149		$dynamic = array();
 150
 151		// Quoted Numbers like "42"
 152		if ( '"' !== $opening_quote && '"' !== $closing_quote ) {
 153			$dynamic[ '/(?<=\A|' . $spaces . ')"(\d[.,\d]*)"/' ] = $opening_quote . '$1' . $closing_quote;
 154		}
 155
 156		// 9" (double prime)
 157		if ( '"' !== $double_prime ) {
 158			$dynamic[ '/(?<=\d)"/' ] = $double_prime;
 159		}
 160
 161		// Double quote at start, or preceded by (, {, <, [, -, or spaces, and not followed by spaces.
 162		if ( '"' !== $opening_quote ) {
 163			$dynamic[ '/(?<=\A|[([{\-]|&lt;|' . $spaces . ')"(?!' . $spaces . ')/' ] = $opening_quote;
 164		}
 165
 166		// Any remaining double quotes.
 167		if ( '"' !== $closing_quote ) {
 168			$dynamic[ '/"/' ] = $closing_quote;
 169		}
 170
 171		$dynamic_characters['quote'] = array_keys( $dynamic );
 172		$dynamic_replacements['quote'] = array_values( $dynamic );
 173		$dynamic = array();
 174
 175		// Dashes and spaces
 176		$dynamic[ '/---/' ] = $em_dash;
 177		$dynamic[ '/(?<=' . $spaces . ')--(?=' . $spaces . ')/' ] = $em_dash;
 178		$dynamic[ '/(?<!xn)--/' ] = $en_dash;
 179		$dynamic[ '/(?<=' . $spaces . ')-(?=' . $spaces . ')/' ] = $en_dash;
 180
 181		$dynamic_characters['dash'] = array_keys( $dynamic );
 182		$dynamic_replacements['dash'] = array_values( $dynamic );
 183	}
 184
 185	// Must do this every time in case plugins use these filters in a context sensitive manner
 186	/**
 187	 * Filter the list of HTML elements not to texturize.
 188	 *
 189	 * @since 2.8.0
 190	 *
 191	 * @param array $default_no_texturize_tags An array of HTML element names.
 192	 */
 193	$no_texturize_tags = apply_filters( 'no_texturize_tags', $default_no_texturize_tags );
 194	/**
 195	 * Filter the list of shortcodes not to texturize.
 196	 *
 197	 * @since 2.8.0
 198	 *
 199	 * @param array $default_no_texturize_shortcodes An array of shortcode names.
 200	 */
 201	$no_texturize_shortcodes = apply_filters( 'no_texturize_shortcodes', $default_no_texturize_shortcodes );
 202
 203	$no_texturize_tags_stack = array();
 204	$no_texturize_shortcodes_stack = array();
 205
 206	// Look for shortcodes and HTML elements.
 207
 208	$tagnames = array_keys( $shortcode_tags );
 209	$tagregexp = join( '|', array_map( 'preg_quote', $tagnames ) );
 210	$tagregexp = "(?:$tagregexp)(?![\\w-])"; // Excerpt of get_shortcode_regex().
 211
 212	$comment_regex =
 213		  '!'           // Start of comment, after the <.
 214		. '(?:'         // Unroll the loop: Consume everything until --> is found.
 215		.     '-(?!->)' // Dash not followed by end of comment.
 216		.     '[^\-]*+' // Consume non-dashes.
 217		. ')*+'         // Loop possessively.
 218		. '(?:-->)?';   // End of comment. If not found, match all input.
 219
 220	$shortcode_regex =
 221		  '\['              // Find start of shortcode.
 222		. '[\/\[]?'         // Shortcodes may begin with [/ or [[
 223		. $tagregexp        // Only match registered shortcodes, because performance.
 224		. '(?:'
 225		.     '[^\[\]<>]+'  // Shortcodes do not contain other shortcodes. Quantifier critical.
 226		. '|'
 227		.     '<[^\[\]>]*>' // HTML elements permitted. Prevents matching ] before >.
 228		. ')*+'             // Possessive critical.
 229		. '\]'              // Find end of shortcode.
 230		. '\]?';            // Shortcodes may end with ]]
 231
 232	$regex =
 233		  '/('                   // Capture the entire match.
 234		.     '<'                // Find start of element.
 235		.     '(?(?=!--)'        // Is this a comment?
 236		.         $comment_regex // Find end of comment.
 237		.     '|'
 238		.         '[^>]+>'       // Find end of element.
 239		.     ')'
 240		. '|'
 241		.     $shortcode_regex   // Find shortcodes.
 242		. ')/s';
 243
 244	$textarr = preg_split( $regex, $text, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY );
 245
 246	foreach ( $textarr as &$curl ) {
 247		// Only call _wptexturize_pushpop_element if $curl is a delimiter.
 248		$first = $curl[0];
 249		if ( '<' === $first && '<!--' === substr( $curl, 0, 4 ) ) {
 250			// This is an HTML comment delimeter.
 251
 252			continue;
 253
 254		} elseif ( '<' === $first && '>' === substr( $curl, -1 ) ) {
 255			// This is an HTML element delimiter.
 256
 257			_wptexturize_pushpop_element( $curl, $no_texturize_tags_stack, $no_texturize_tags );
 258
 259		} elseif ( '' === trim( $curl ) ) {
 260			// This is a newline between delimiters.  Performance improves when we check this.
 261
 262			continue;
 263
 264		} elseif ( '[' === $first && 1 === preg_match( '/^' . $shortcode_regex . '$/', $curl ) ) {
 265			// This is a shortcode delimiter.
 266
 267			if ( '[[' !== substr( $curl, 0, 2 ) && ']]' !== substr( $curl, -2 ) ) {
 268				// Looks like a normal shortcode.
 269				_wptexturize_pushpop_element( $curl, $no_texturize_shortcodes_stack, $no_texturize_shortcodes );
 270			} else {
 271				// Looks like an escaped shortcode.
 272				continue;
 273			}
 274
 275		} elseif ( empty( $no_texturize_shortcodes_stack ) && empty( $no_texturize_tags_stack ) ) {
 276			// This is neither a delimiter, nor is this content inside of no_texturize pairs.  Do texturize.
 277
 278			$curl = str_replace( $static_characters, $static_replacements, $curl );
 279
 280			if ( false !== strpos( $curl, "'" ) ) {
 281				$curl = preg_replace( $dynamic_characters['apos'], $dynamic_replacements['apos'], $curl );
 282			}
 283			if ( false !== strpos( $curl, '"' ) ) {
 284				$curl = preg_replace( $dynamic_characters['quote'], $dynamic_replacements['quote'], $curl );
 285			}
 286			if ( false !== strpos( $curl, '-' ) ) {
 287				$curl = preg_replace( $dynamic_characters['dash'], $dynamic_replacements['dash'], $curl );
 288			}
 289
 290			// 9x9 (times), but never 0x9999
 291			if ( 1 === preg_match( '/(?<=\d)x\d/', $curl ) ) {
 292				// Searching for a digit is 10 times more expensive than for the x, so we avoid doing this one!
 293				$curl = preg_replace( '/\b(\d(?(?<=0)[\d\.,]+|[\d\.,]*))x(\d[\d\.,]*)\b/', '$1&#215;$2', $curl );
 294			}
 295		}
 296	}
 297	$text = implode( '', $textarr );
 298
 299	// Replace each & with &#038; unless it already looks like an entity.
 300	$text = preg_replace('/&(?!#(?:\d+|x[a-f0-9]+);|[a-z1-4]{1,8};)/i', '&#038;', $text);
 301
 302	return $text;
 303}
 304
 305/**
 306 * Search for disabled element tags. Push element to stack on tag open and pop
 307 * on tag close.
 308 *
 309 * Assumes first char of $text is tag opening and last char is tag closing.
 310 * Assumes second char of $text is optionally '/' to indicate closing as in </html>.
 311 *
 312 * @since 2.9.0
 313 * @access private
 314 *
 315 * @param string $text Text to check. Must be a tag like `<html>` or `[shortcode]`.
 316 * @param array $stack List of open tag elements.
 317 * @param array $disabled_elements The tag names to match against. Spaces are not allowed in tag names.
 318 */
 319function _wptexturize_pushpop_element($text, &$stack, $disabled_elements) {
 320	// Is it an opening tag or closing tag?
 321	if ( '/' !== $text[1] ) {
 322		$opening_tag = true;
 323		$name_offset = 1;
 324	} elseif ( 0 == count( $stack ) ) {
 325		// Stack is empty. Just stop.
 326		return;
 327	} else {
 328		$opening_tag = false;
 329		$name_offset = 2;
 330	}
 331
 332	// Parse out the tag name.
 333	$space = strpos( $text, ' ' );
 334	if ( false === $space ) {
 335		$space = -1;
 336	} else {
 337		$space -= $name_offset;
 338	}
 339	$tag = substr( $text, $name_offset, $space );
 340
 341	// Handle disabled tags.
 342	if ( in_array( $tag, $disabled_elements ) ) {
 343		if ( $opening_tag ) {
 344			/*
 345			 * This disables texturize until we find a closing tag of our type
 346			 * (e.g. <pre>) even if there was invalid nesting before that
 347			 *
 348			 * Example: in the case <pre>sadsadasd</code>"baba"</pre>
 349			 *          "baba" won't be texturize
 350			 */
 351
 352			array_push( $stack, $tag );
 353		} elseif ( end( $stack ) == $tag ) {
 354			array_pop( $stack );
 355		}
 356	}
 357}
 358
 359/**
 360 * Replaces double line-breaks with paragraph elements.
 361 *
 362 * A group of regex replaces used to identify text formatted with newlines and
 363 * replace double line-breaks with HTML paragraph tags. The remaining
 364 * line-breaks after conversion become <<br />> tags, unless $br is set to '0'
 365 * or 'false'.
 366 *
 367 * @since 0.71
 368 *
 369 * @param string $pee The text which has to be formatted.
 370 * @param bool $br Optional. If set, this will convert all remaining line-breaks after paragraphing. Default true.
 371 * @return string Text which has been converted into correct paragraph tags.
 372 */
 373function wpautop($pee, $br = true) {
 374	$pre_tags = array();
 375
 376	if ( trim($pee) === '' )
 377		return '';
 378
 379	$pee = $pee . "\n"; // just to make things a little easier, pad the end
 380
 381	if ( strpos($pee, '<pre') !== false ) {
 382		$pee_parts = explode( '</pre>', $pee );
 383		$last_pee = array_pop($pee_parts);
 384		$pee = '';
 385		$i = 0;
 386
 387		foreach ( $pee_parts as $pee_part ) {
 388			$start = strpos($pee_part, '<pre');
 389
 390			// Malformed html?
 391			if ( $start === false ) {
 392				$pee .= $pee_part;
 393				continue;
 394			}
 395
 396			$name = "<pre wp-pre-tag-$i></pre>";
 397			$pre_tags[$name] = substr( $pee_part, $start ) . '</pre>';
 398
 399			$pee .= substr( $pee_part, 0, $start ) . $name;
 400			$i++;
 401		}
 402
 403		$pee .= $last_pee;
 404	}
 405
 406	$pee = preg_replace('|<br />\s*<br />|', "\n\n", $pee);
 407	// Space things out a little
 408	$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|details|menu|summary)';
 409	$pee = preg_replace('!(<' . $allblocks . '[^>]*>)!', "\n$1", $pee);
 410	$pee = preg_replace('!(</' . $allblocks . '>)!', "$1\n\n", $pee);
 411	$pee = str_replace(array("\r\n", "\r"), "\n", $pee); // cross-platform newlines
 412
 413	if ( strpos( $pee, '<option' ) !== false ) {
 414		// no P/BR around option
 415		$pee = preg_replace( '|\s*<option|', '<option', $pee );
 416		$pee = preg_replace( '|</option>\s*|', '</option>', $pee );
 417	}
 418
 419	if ( strpos( $pee, '</object>' ) !== false ) {
 420		// no P/BR around param and embed
 421		$pee = preg_replace( '|(<object[^>]*>)\s*|', '$1', $pee );
 422		$pee = preg_replace( '|\s*</object>|', '</object>', $pee );
 423		$pee = preg_replace( '%\s*(</?(?:param|embed)[^>]*>)\s*%', '$1', $pee );
 424	}
 425
 426	if ( strpos( $pee, '<source' ) !== false || strpos( $pee, '<track' ) !== false ) {
 427		// no P/BR around source and track
 428		$pee = preg_replace( '%([<\[](?:audio|video)[^>\]]*[>\]])\s*%', '$1', $pee );
 429		$pee = preg_replace( '%\s*([<\[]/(?:audio|video)[>\]])%', '$1', $pee );
 430		$pee = preg_replace( '%\s*(<(?:source|track)[^>]*>)\s*%', '$1', $pee );
 431	}
 432
 433	$pee = preg_replace("/\n\n+/", "\n\n", $pee); // take care of duplicates
 434	// make paragraphs, including one at the end
 435	$pees = preg_split('/\n\s*\n/', $pee, -1, PREG_SPLIT_NO_EMPTY);
 436	$pee = '';
 437
 438	foreach ( $pees as $tinkle ) {
 439		$pee .= '<p>' . trim($tinkle, "\n") . "</p>\n";
 440	}
 441
 442	$pee = preg_replace('|<p>\s*</p>|', '', $pee); // under certain strange conditions it could create a P of entirely whitespace
 443	$pee = preg_replace('!<p>([^<]+)</(div|address|form)>!', "<p>$1</p></$2>", $pee);
 444	$pee = preg_replace('!<p>\s*(</?' . $allblocks . '[^>]*>)\s*</p>!', "$1", $pee); // don't pee all over a tag
 445	$pee = preg_replace("|<p>(<li.+?)</p>|", "$1", $pee); // problem with nested lists
 446	$pee = preg_replace('|<p><blockquote([^>]*)>|i', "<blockquote$1><p>", $pee);
 447	$pee = str_replace('</blockquote></p>', '</p></blockquote>', $pee);
 448	$pee = preg_replace('!<p>\s*(</?' . $allblocks . '[^>]*>)!', "$1", $pee);
 449	$pee = preg_replace('!(</?' . $allblocks . '[^>]*>)\s*</p>!', "$1", $pee);
 450
 451	if ( $br ) {
 452		$pee = preg_replace_callback('/<(script|style).*?<\/\\1>/s', '_autop_newline_preservation_helper', $pee);
 453		$pee = preg_replace('|(?<!<br />)\s*\n|', "<br />\n", $pee); // optionally make line breaks
 454		$pee = str_replace('<WPPreserveNewline />', "\n", $pee);
 455	}
 456
 457	$pee = preg_replace('!(</?' . $allblocks . '[^>]*>)\s*<br />!', "$1", $pee);
 458	$pee = preg_replace('!<br />(\s*</?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)[^>]*>)!', '$1', $pee);
 459	$pee = preg_replace( "|\n</p>$|", '</p>', $pee );
 460
 461	if ( !empty($pre_tags) )
 462		$pee = str_replace(array_keys($pre_tags), array_values($pre_tags), $pee);
 463
 464	return $pee;
 465}
 466
 467/**
 468 * Newline preservation help function for wpautop
 469 *
 470 * @since 3.1.0
 471 * @access private
 472 *
 473 * @param array $matches preg_replace_callback matches array
 474 * @return string
 475 */
 476function _autop_newline_preservation_helper( $matches ) {
 477	return str_replace("\n", "<WPPreserveNewline />", $matches[0]);
 478}
 479
 480/**
 481 * Don't auto-p wrap shortcodes that stand alone
 482 *
 483 * Ensures that shortcodes are not wrapped in `<p>...</p>`.
 484 *
 485 * @since 2.9.0
 486 *
 487 * @param string $pee The content.
 488 * @return string The filtered content.
 489 */
 490function shortcode_unautop( $pee ) {
 491	global $shortcode_tags;
 492
 493	if ( empty( $shortcode_tags ) || !is_array( $shortcode_tags ) ) {
 494		return $pee;
 495	}
 496
 497	$tagregexp = join( '|', array_map( 'preg_quote', array_keys( $shortcode_tags ) ) );
 498	$spaces = wp_spaces_regexp();
 499
 500	$pattern =
 501		  '/'
 502		. '<p>'                              // Opening paragraph
 503		. '(?:' . $spaces . ')*+'            // Optional leading whitespace
 504		. '('                                // 1: The shortcode
 505		.     '\\['                          // Opening bracket
 506		.     "($tagregexp)"                 // 2: Shortcode name
 507		.     '(?![\\w-])'                   // Not followed by word character or hyphen
 508		                                     // Unroll the loop: Inside the opening shortcode tag
 509		.     '[^\\]\\/]*'                   // Not a closing bracket or forward slash
 510		.     '(?:'
 511		.         '\\/(?!\\])'               // A forward slash not followed by a closing bracket
 512		.         '[^\\]\\/]*'               // Not a closing bracket or forward slash
 513		.     ')*?'
 514		.     '(?:'
 515		.         '\\/\\]'                   // Self closing tag and closing bracket
 516		.     '|'
 517		.         '\\]'                      // Closing bracket
 518		.         '(?:'                      // Unroll the loop: Optionally, anything between the opening and closing shortcode tags
 519		.             '[^\\[]*+'             // Not an opening bracket
 520		.             '(?:'
 521		.                 '\\[(?!\\/\\2\\])' // An opening bracket not followed by the closing shortcode tag
 522		.                 '[^\\[]*+'         // Not an opening bracket
 523		.             ')*+'
 524		.             '\\[\\/\\2\\]'         // Closing shortcode tag
 525		.         ')?'
 526		.     ')'
 527		. ')'
 528		. '(?:' . $spaces . ')*+'            // optional trailing whitespace
 529		. '<\\/p>'                           // closing paragraph
 530		. '/s';
 531
 532	return preg_replace( $pattern, '$1', $pee );
 533}
 534
 535/**
 536 * Checks to see if a string is utf8 encoded.
 537 *
 538 * NOTE: This function checks for 5-Byte sequences, UTF8
 539 *       has Bytes Sequences with a maximum length of 4.
 540 *
 541 * @author bmorel at ssi dot fr (modified)
 542 * @since 1.2.1
 543 *
 544 * @param string $str The string to be checked
 545 * @return bool True if $str fits a UTF-8 model, false otherwise.
 546 */
 547function seems_utf8($str) {
 548	mbstring_binary_safe_encoding();
 549	$length = strlen($str);
 550	reset_mbstring_encoding();
 551	for ($i=0; $i < $length; $i++) {
 552		$c = ord($str[$i]);
 553		if ($c < 0x80) $n = 0; # 0bbbbbbb
 554		elseif (($c & 0xE0) == 0xC0) $n=1; # 110bbbbb
 555		elseif (($c & 0xF0) == 0xE0) $n=2; # 1110bbbb
 556		elseif (($c & 0xF8) == 0xF0) $n=3; # 11110bbb
 557		elseif (($c & 0xFC) == 0xF8) $n=4; # 111110bb
 558		elseif (($c & 0xFE) == 0xFC) $n=5; # 1111110b
 559		else return false; # Does not match any model
 560		for ($j=0; $j<$n; $j++) { # n bytes matching 10bbbbbb follow ?
 561			if ((++$i == $length) || ((ord($str[$i]) & 0xC0) != 0x80))
 562				return false;
 563		}
 564	}
 565	return true;
 566}
 567
 568/**
 569 * Converts a number of special characters into their HTML entities.
 570 *
 571 * Specifically deals with: &, <, >, ", and '.
 572 *
 573 * $quote_style can be set to ENT_COMPAT to encode " to
 574 * &quot;, or ENT_QUOTES to do both. Default is ENT_NOQUOTES where no quotes are encoded.
 575 *
 576 * @since 1.2.2
 577 * @access private
 578 *
 579 * @param string $string The text which is to be encoded.
 580 * @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.
 581 * @param string $charset Optional. The character encoding of the string. Default is false.
 582 * @param boolean $double_encode Optional. Whether to encode existing html entities. Default is false.
 583 * @return string The encoded text with HTML entities.
 584 */
 585function _wp_specialchars( $string, $quote_style = ENT_NOQUOTES, $charset = false, $double_encode = false ) {
 586	$string = (string) $string;
 587
 588	if ( 0 === strlen( $string ) )
 589		return '';
 590
 591	// Don't bother if there are no specialchars - saves some processing
 592	if ( ! preg_match( '/[&<>"\']/', $string ) )
 593		return $string;
 594
 595	// Account for the previous behaviour of the function when the $quote_style is not an accepted value
 596	if ( empty( $quote_style ) )
 597		$quote_style = ENT_NOQUOTES;
 598	elseif ( ! in_array( $quote_style, array( 0, 2, 3, 'single', 'double' ), true ) )
 599		$quote_style = ENT_QUOTES;
 600
 601	// Store the site charset as a static to avoid multiple calls to wp_load_alloptions()
 602	if ( ! $charset ) {
 603		static $_charset;
 604		if ( ! isset( $_charset ) ) {
 605			$alloptions = wp_load_alloptions();
 606			$_charset = isset( $alloptions['blog_charset'] ) ? $alloptions['blog_charset'] : '';
 607		}
 608		$charset = $_charset;
 609	}
 610
 611	if ( in_array( $charset, array( 'utf8', 'utf-8', 'UTF8' ) ) )
 612		$charset = 'UTF-8';
 613
 614	$_quote_style = $quote_style;
 615
 616	if ( $quote_style === 'double' ) {
 617		$quote_style = ENT_COMPAT;
 618		$_quote_style = ENT_COMPAT;
 619	} elseif ( $quote_style === 'single' ) {
 620		$quote_style = ENT_NOQUOTES;
 621	}
 622
 623	// Handle double encoding ourselves
 624	if ( $double_encode ) {
 625		$string = @htmlspecialchars( $string, $quote_style, $charset );
 626	} else {
 627		// Decode &amp; into &
 628		$string = wp_specialchars_decode( $string, $_quote_style );
 629
 630		// Guarantee every &entity; is valid or re-encode the &
 631		$string = wp_kses_normalize_entities( $string );
 632
 633		// Now re-encode everything except &entity;
 634		$string = preg_split( '/(&#?x?[0-9a-z]+;)/i', $string, -1, PREG_SPLIT_DELIM_CAPTURE );
 635
 636		for ( $i = 0; $i < count( $string ); $i += 2 )
 637			$string[$i] = @htmlspecialchars( $string[$i], $quote_style, $charset );
 638
 639		$string = implode( '', $string );
 640	}
 641
 642	// Backwards compatibility
 643	if ( 'single' === $_quote_style )
 644		$string = str_replace( "'", '&#039;', $string );
 645
 646	return $string;
 647}
 648
 649/**
 650 * Converts a number of HTML entities into their special characters.
 651 *
 652 * Specifically deals with: &, <, >, ", and '.
 653 *
 654 * $quote_style can be set to ENT_COMPAT to decode " entities,
 655 * or ENT_QUOTES to do both " and '. Default is ENT_NOQUOTES where no quotes are decoded.
 656 *
 657 * @since 2.8.0
 658 *
 659 * @param string $string The text which is to be decoded.
 660 * @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.
 661 * @return string The decoded text without HTML entities.
 662 */
 663function wp_specialchars_decode( $string, $quote_style = ENT_NOQUOTES ) {
 664	$string = (string) $string;
 665
 666	if ( 0 === strlen( $string ) ) {
 667		return '';
 668	}
 669
 670	// Don't bother if there are no entities - saves a lot of processing
 671	if ( strpos( $string, '&' ) === false ) {
 672		return $string;
 673	}
 674
 675	// Match the previous behaviour of _wp_specialchars() when the $quote_style is not an accepted value
 676	if ( empty( $quote_style ) ) {
 677		$quote_style = ENT_NOQUOTES;
 678	} elseif ( !in_array( $quote_style, array( 0, 2, 3, 'single', 'double' ), true ) ) {
 679		$quote_style = ENT_QUOTES;
 680	}
 681
 682	// More complete than get_html_translation_table( HTML_SPECIALCHARS )
 683	$single = array( '&#039;'  => '\'', '&#x27;' => '\'' );
 684	$single_preg = array( '/&#0*39;/'  => '&#039;', '/&#x0*27;/i' => '&#x27;' );
 685	$double = array( '&quot;' => '"', '&#034;'  => '"', '&#x22;' => '"' );
 686	$double_preg = array( '/&#0*34;/'  => '&#034;', '/&#x0*22;/i' => '&#x22;' );
 687	$others = array( '&lt;'   => '<', '&#060;'  => '<', '&gt;'   => '>', '&#062;'  => '>', '&amp;'  => '&', '&#038;'  => '&', '&#x26;' => '&' );
 688	$others_preg = array( '/&#0*60;/'  => '&#060;', '/&#0*62;/'  => '&#062;', '/&#0*38;/'  => '&#038;', '/&#x0*26;/i' => '&#x26;' );
 689
 690	if ( $quote_style === ENT_QUOTES ) {
 691		$translation = array_merge( $single, $double, $others );
 692		$translation_preg = array_merge( $single_preg, $double_preg, $others_preg );
 693	} elseif ( $quote_style === ENT_COMPAT || $quote_style === 'double' ) {
 694		$translation = array_merge( $double, $others );
 695		$translation_preg = array_merge( $double_preg, $others_preg );
 696	} elseif ( $quote_style === 'single' ) {
 697		$translation = array_merge( $single, $others );
 698		$translation_preg = array_merge( $single_preg, $others_preg );
 699	} elseif ( $quote_style === ENT_NOQUOTES ) {
 700		$translation = $others;
 701		$translation_preg = $others_preg;
 702	}
 703
 704	// Remove zero padding on numeric entities
 705	$string = preg_replace( array_keys( $translation_preg ), array_values( $translation_preg ), $string );
 706
 707	// Replace characters according to translation table
 708	return strtr( $string, $translation );
 709}
 710
 711/**
 712 * Checks for invalid UTF8 in a string.
 713 *
 714 * @since 2.8.0
 715 *
 716 * @param string $string The text which is to be checked.
 717 * @param boolean $strip Optional. Whether to attempt to strip out invalid UTF8. Default is false.
 718 * @return string The checked text.
 719 */
 720function wp_check_invalid_utf8( $string, $strip = false ) {
 721	$string = (string) $string;
 722
 723	if ( 0 === strlen( $string ) ) {
 724		return '';
 725	}
 726
 727	// Store the site charset as a static to avoid multiple calls to get_option()
 728	static $is_utf8;
 729	if ( !isset( $is_utf8 ) ) {
 730		$is_utf8 = in_array( get_option( 'blog_charset' ), array( 'utf8', 'utf-8', 'UTF8', 'UTF-8' ) );
 731	}
 732	if ( !$is_utf8 ) {
 733		return $string;
 734	}
 735
 736	// Check for support for utf8 in the installed PCRE library once and store the result in a static
 737	static $utf8_pcre;
 738	if ( !isset( $utf8_pcre ) ) {
 739		$utf8_pcre = @preg_match( '/^./u', 'a' );
 740	}
 741	// We can't demand utf8 in the PCRE installation, so just return the string in those cases
 742	if ( !$utf8_pcre ) {
 743		return $string;
 744	}
 745
 746	// preg_match fails when it encounters invalid UTF8 in $string
 747	if ( 1 === @preg_match( '/^./us', $string ) ) {
 748		return $string;
 749	}
 750
 751	// Attempt to strip the bad chars if requested (not recommended)
 752	if ( $strip && function_exists( 'iconv' ) ) {
 753		return iconv( 'utf-8', 'utf-8', $string );
 754	}
 755
 756	return '';
 757}
 758
 759/**
 760 * Encode the Unicode values to be used in the URI.
 761 *
 762 * @since 1.5.0
 763 *
 764 * @param string $utf8_string
 765 * @param int $length Max length of the string
 766 * @return string String with Unicode encoded for URI.
 767 */
 768function utf8_uri_encode( $utf8_string, $length = 0 ) {
 769	$unicode = '';
 770	$values = array();
 771	$num_octets = 1;
 772	$unicode_length = 0;
 773
 774	mbstring_binary_safe_encoding();
 775	$string_length = strlen( $utf8_string );
 776	reset_mbstring_encoding();
 777
 778	for ($i = 0; $i < $string_length; $i++ ) {
 779
 780		$value = ord( $utf8_string[ $i ] );
 781
 782		if ( $value < 128 ) {
 783			if ( $length && ( $unicode_length >= $length ) )
 784				break;
 785			$unicode .= chr($value);
 786			$unicode_length++;
 787		} else {
 788			if ( count( $values ) == 0 ) $num_octets = ( $value < 224 ) ? 2 : 3;
 789
 790			$values[] = $value;
 791
 792			if ( $length && ( $unicode_length + ($num_octets * 3) ) > $length )
 793				break;
 794			if ( count( $values ) == $num_octets ) {
 795				if ($num_octets == 3) {
 796					$unicode .= '%' . dechex($values[0]) . '%' . dechex($values[1]) . '%' . dechex($values[2]);
 797					$unicode_length += 9;
 798				} else {
 799					$unicode .= '%' . dechex($values[0]) . '%' . dechex($values[1]);
 800					$unicode_length += 6;
 801				}
 802
 803				$values = array();
 804				$num_octets = 1;
 805			}
 806		}
 807	}
 808
 809	return $unicode;
 810}
 811
 812/**
 813 * Converts all accent characters to ASCII characters.
 814 *
 815 * If there are no accent characters, then the string given is just returned.
 816 *
 817 * @since 1.2.1
 818 *
 819 * @param string $string Text that might have accent characters
 820 * @return string Filtered string with replaced "nice" characters.
 821 */
 822function remove_accents($string) {
 823	if ( !preg_match('/[\x80-\xff]/', $string) )
 824		return $string;
 825
 826	if (seems_utf8($string)) {
 827		$chars = array(
 828		// Decompositions for Latin-1 Supplement
 829		chr(194).chr(170) => 'a', chr(194).chr(186) => 'o',
 830		chr(195).chr(128) => 'A', chr(195).chr(129) => 'A',
 831		chr(195).chr(130) => 'A', chr(195).chr(131) => 'A',
 832		chr(195).chr(132) => 'A', chr(195).chr(133) => 'A',
 833		chr(195).chr(134) => 'AE',chr(195).chr(135) => 'C',
 834		chr(195).chr(136) => 'E', chr(195).chr(137) => 'E',
 835		chr(195).chr(138) => 'E', chr(195).chr(139) => 'E',
 836		chr(195).chr(140) => 'I', chr(195).chr(141) => 'I',
 837		chr(195).chr(142) => 'I', chr(195).chr(143) => 'I',
 838		chr(195).chr(144) => 'D', chr(195).chr(145) => 'N',
 839		chr(195).chr(146) => 'O', chr(195).chr(147) => 'O',
 840		chr(195).chr(148) => 'O', chr(195).chr(149) => 'O',
 841		chr(195).chr(150) => 'O', chr(195).chr(153) => 'U',
 842		chr(195).chr(154) => 'U', chr(195).chr(155) => 'U',
 843		chr(195).chr(156) => 'U', chr(195).chr(157) => 'Y',
 844		chr(195).chr(158) => 'TH',chr(195).chr(159) => 's',
 845		chr(195).chr(160) => 'a', chr(195).chr(161) => 'a',
 846		chr(195).chr(162) => 'a', chr(195).chr(163) => 'a',
 847		chr(195).chr(164) => 'a', chr(195).chr(165) => 'a',
 848		chr(195).chr(166) => 'ae',chr(195).chr(167) => 'c',
 849		chr(195).chr(168) => 'e', chr(195).chr(169) => 'e',
 850		chr(195).chr(170) => 'e', chr(195).chr(171) => 'e',
 851		chr(195).chr(172) => 'i', chr(195).chr(173) => 'i',
 852		chr(195).chr(174) => 'i', chr(195).chr(175) => 'i',
 853		chr(195).chr(176) => 'd', chr(195).chr(177) => 'n',
 854		chr(195).chr(178) => 'o', chr(195).chr(179) => 'o',
 855		chr(195).chr(180) => 'o', chr(195).chr(181) => 'o',
 856		chr(195).chr(182) => 'o', chr(195).chr(184) => 'o',
 857		chr(195).chr(185) => 'u', chr(195).chr(186) => 'u',
 858		chr(195).chr(187) => 'u', chr(195).chr(188) => 'u',
 859		chr(195).chr(189) => 'y', chr(195).chr(190) => 'th',
 860		chr(195).chr(191) => 'y', chr(195).chr(152) => 'O',
 861		// Decompositions for Latin Extended-A
 862		chr(196).chr(128) => 'A', chr(196).chr(129) => 'a',
 863		chr(196).chr(130) => 'A', chr(196).chr(131) => 'a',
 864		chr(196).chr(132) => 'A', chr(196).chr(133) => 'a',
 865		chr(196).chr(134) => 'C', chr(196).chr(135) => 'c',
 866		chr(196).chr(136) => 'C', chr(196).chr(137) => 'c',
 867		chr(196).chr(138) => 'C', chr(196).chr(139) => 'c',
 868		chr(196).chr(140) => 'C', chr(196).chr(141) => 'c',
 869		chr(196).chr(142) => 'D', chr(196).chr(143) => 'd',
 870		chr(196).chr(144) => 'D', chr(196).chr(145) => 'd',
 871		chr(196).chr(146) => 'E', chr(196).chr(147) => 'e',
 872		chr(196).chr(148) => 'E', chr(196).chr(149) => 'e',
 873		chr(196).chr(150) => 'E', chr(196).chr(151) => 'e',
 874		chr(196).chr(152) => 'E', chr(196).chr(153) => 'e',
 875		chr(196).chr(154) => 'E', chr(196).chr(155) => 'e',
 876		chr(196).chr(156) => 'G', chr(196).chr(157) => 'g',
 877		chr(196).chr(158) => 'G', chr(196).chr(159) => 'g',
 878		chr(196).chr(160) => 'G', chr(196).chr(161) => 'g',
 879		chr(196).chr(162) => 'G', chr(196).chr(163) => 'g',
 880		chr(196).chr(164) => 'H', chr(196).chr(165) => 'h',
 881		chr(196).chr(166) => 'H', chr(196).chr(167) => 'h',
 882		chr(196).chr(168) => 'I', chr(196).chr(169) => 'i',
 883		chr(196).chr(170) => 'I', chr(196).chr(171) => 'i',
 884		chr(196).chr(172) => 'I', chr(196).chr(173) => 'i',
 885		chr(196).chr(174) => 'I', chr(196).chr(175) => 'i',
 886		chr(196).chr(176) => 'I', chr(196).chr(177) => 'i',
 887		chr(196).chr(178) => 'IJ',chr(196).chr(179) => 'ij',
 888		chr(196).chr(180) => 'J', chr(196).chr(181) => 'j',
 889		chr(196).chr(182) => 'K', chr(196).chr(183) => 'k',
 890		chr(196).chr(184) => 'k', chr(196).chr(185) => 'L',
 891		chr(196).chr(186) => 'l', chr(196).chr(187) => 'L',
 892		chr(196).chr(188) => 'l', chr(196).chr(189) => 'L',
 893		chr(196).chr(190) => 'l', chr(196).chr(191) => 'L',
 894		chr(197).chr(128) => 'l', chr(197).chr(129) => 'L',
 895		chr(197).chr(130) => 'l', chr(197).chr(131) => 'N',
 896		chr(197).chr(132) => 'n', chr(197).chr(133) => 'N',
 897		chr(197).chr(134) => 'n', chr(197).chr(135) => 'N',
 898		chr(197).chr(136) => 'n', chr(197).chr(137) => 'N',
 899		chr(197).chr(138) => 'n', chr(197).chr(139) => 'N',
 900		chr(197).chr(140) => 'O', chr(197).chr(141) => 'o',
 901		chr(197).chr(142) => 'O', chr(197).chr(143) => 'o',
 902		chr(197).chr(144) => 'O', chr(197).chr(145) => 'o',
 903		chr(197).chr(146) => 'OE',chr(197).chr(147) => 'oe',
 904		chr(197).chr(148) => 'R',chr(197).chr(149) => 'r',
 905		chr(197).chr(150) => 'R',chr(197).chr(151) => 'r',
 906		chr(197).chr(152) => 'R',chr(197).chr(153) => 'r',
 907		chr(197).chr(154) => 'S',chr(197).chr(155) => 's',
 908		chr(197).chr(156) => 'S',chr(197).chr(157) => 's',
 909		chr(197).chr(158) => 'S',chr(197).chr(159) => 's',
 910		chr(197).chr(160) => 'S', chr(197).chr(161) => 's',
 911		chr(197).chr(162) => 'T', chr(197).chr(163) => 't',
 912		chr(197).chr(164) => 'T', chr(197).chr(165) => 't',
 913		chr(197).chr(166) => 'T', chr(197).chr(167) => 't',
 914		chr(197).chr(168) => 'U', chr(197).chr(169) => 'u',
 915		chr(197).chr(170) => 'U', chr(197).chr(171) => 'u',
 916		chr(197).chr(172) => 'U', chr(197).chr(173) => 'u',
 917		chr(197).chr(174) => 'U', chr(197).chr(175) => 'u',
 918		chr(197).chr(176) => 'U', chr(197).chr(177) => 'u',
 919		chr(197).chr(178) => 'U', chr(197).chr(179) => 'u',
 920		chr(197).chr(180) => 'W', chr(197).chr(181) => 'w',
 921		chr(197).chr(182) => 'Y', chr(197).chr(183) => 'y',
 922		chr(197).chr(184) => 'Y', chr(197).chr(185) => 'Z',
 923		chr(197).chr(186) => 'z', chr(197).chr(187) => 'Z',
 924		chr(197).chr(188) => 'z', chr(197).chr(189) => 'Z',
 925		chr(197).chr(190) => 'z', chr(197).chr(191) => 's',
 926		// Decompositions for Latin Extended-B
 927		chr(200).chr(152) => 'S', chr(200).chr(153) => 's',
 928		chr(200).chr(154) => 'T', chr(200).chr(155) => 't',
 929		// Euro Sign
 930		chr(226).chr(130).chr(172) => 'E',
 931		// GBP (Pound) Sign
 932		chr(194).chr(163) => '',
 933		// Vowels with diacritic (Vietnamese)
 934		// unmarked
 935		chr(198).chr(160) => 'O', chr(198).chr(161) => 'o',
 936		chr(198).chr(175) => 'U', chr(198).chr(176) => 'u',
 937		// grave accent
 938		chr(225).chr(186).chr(166) => 'A', chr(225).chr(186).chr(167) => 'a',
 939		chr(225).chr(186).chr(176) => 'A', chr(225).chr(186).chr(177) => 'a',
 940		chr(225).chr(187).chr(128) => 'E', chr(225).chr(187).chr(129) => 'e',
 941		chr(225).chr(187).chr(146) => 'O', chr(225).chr(187).chr(147) => 'o',
 942		chr(225).chr(187).chr(156) => 'O', chr(225).chr(187).chr(157) => 'o',
 943		chr(225).chr(187).chr(170) => 'U', chr(225).chr(187).chr(171) => 'u',
 944		chr(225).chr(187).chr(178) => 'Y', chr(225).chr(187).chr(179) => 'y',
 945		// hook
 946		chr(225).chr(186).chr(162) => 'A', chr(225).chr(186).chr(163) => 'a',
 947		chr(225).chr(186).chr(168) => 'A', chr(225).chr(186).chr(169) => 'a',
 948		chr(225).chr(186).chr(178) => 'A', chr(225).chr(186).chr(179) => 'a',
 949		chr(225).chr(186).chr(186) => 'E', chr(225).chr(186).chr(187) => 'e',
 950		chr(225).chr(187).chr(130) => 'E', chr(225).chr(187).chr(131) => 'e',
 951		chr(225).chr(187).chr(136) => 'I', chr(225).chr(187).chr(137) => 'i',
 952		chr(225).chr(187).chr(142) => 'O', chr(225).chr(187).chr(143) => 'o',
 953		chr(225).chr(187).chr(148) => 'O', chr(225).chr(187).chr(149) => 'o',
 954		chr(225).chr(187).chr(158) => 'O', chr(225).chr(187).chr(159) => 'o',
 955		chr(225).chr(187).chr(166) => 'U', chr(225).chr(187).chr(167) => 'u',
 956		chr(225).chr(187).chr(172) => 'U', chr(225).chr(187).chr(173) => 'u',
 957		chr(225).chr(187).chr(182) => 'Y', chr(225).chr(187).chr(183) => 'y',
 958		// tilde
 959		chr(225).chr(186).chr(170) => 'A', chr(225).chr(186).chr(171) => 'a',
 960		chr(225).chr(186).chr(180) => 'A', chr(225).chr(186).chr(181) => 'a',
 961		chr(225).chr(186).chr(188) => 'E', chr(225).chr(186).chr(189) => 'e',
 962		chr(225).chr(187).chr(132) => 'E', chr(225).chr(187).chr(133) => 'e',
 963		chr(225).chr(187).chr(150) => 'O', chr(225).chr(187).chr(151) => 'o',
 964		chr(225).chr(187).chr(160) => 'O', chr(225).chr(187).chr(161) => 'o',
 965		chr(225).chr(187).chr(174) => 'U', chr(225).chr(187).chr(175) => 'u',
 966		chr(225).chr(187).chr(184) => 'Y', chr(225).chr(187).chr(185) => 'y',
 967		// acute accent
 968		chr(225).chr(186).chr(164) => 'A', chr(225).chr(186).chr(165) => 'a',
 969		chr(225).chr(186).chr(174) => 'A', chr(225).chr(186).chr(175) => 'a',
 970		chr(225).chr(186).chr(190) => 'E', chr(225).chr(186).chr(191) => 'e',
 971		chr(225).chr(187).chr(144) => 'O', chr(225).chr(187).chr(145) => 'o',
 972		chr(225).chr(187).chr(154) => 'O', chr(225).chr(187).chr(155) => 'o',
 973		chr(225).chr(187).chr(168) => 'U', chr(225).chr(187).chr(169) => 'u',
 974		// dot below
 975		chr(225).chr(186).chr(160) => 'A', chr(225).chr(186).chr(161) => 'a',
 976		chr(225).chr(186).chr(172) => 'A', chr(225).chr(186).chr(173) => 'a',
 977		chr(225).chr(186).chr(182) => 'A', chr(225).chr(186).chr(183) => 'a',
 978		chr(225).chr(186).chr(184) => 'E', chr(225).chr(186).chr(185) => 'e',
 979		chr(225).chr(187).chr(134) => 'E', chr(225).chr(187).chr(135) => 'e',
 980		chr(225).chr(187).chr(138) => 'I', chr(225).chr(187).chr(139) => 'i',
 981		chr(225).chr(187).chr(140) => 'O', chr(225).chr(187).chr(141) => 'o',
 982		chr(225).chr(187).chr(152) => 'O', chr(225).chr(187).chr(153) => 'o',
 983		chr(225).chr(187).chr(162) => 'O', chr(225).chr(187).chr(163) => 'o',
 984		chr(225).chr(187).chr(164) => 'U', chr(225).chr(187).chr(165) => 'u',
 985		chr(225).chr(187).chr(176) => 'U', chr(225).chr(187).chr(177) => 'u',
 986		chr(225).chr(187).chr(180) => 'Y', chr(225).chr(187).chr(181) => 'y',
 987		// Vowels with diacritic (Chinese, Hanyu Pinyin)
 988		chr(201).chr(145) => 'a',
 989		// macron
 990		chr(199).chr(149) => 'U', chr(199).chr(150) => 'u',
 991		// acute accent
 992		chr(199).chr(151) => 'U', chr(199).chr(152) => 'u',
 993		// caron
 994		chr(199).chr(141) => 'A', chr(199).chr(142) => 'a',
 995		chr(199).chr(143) => 'I', chr(199).chr(144) => 'i',
 996		chr(199).chr(145) => 'O', chr(199).chr(146) => 'o',
 997		chr(199).chr(147) => 'U', chr(199).chr(148) => 'u',
 998		chr(199).chr(153) => 'U', chr(199).chr(154) => 'u',
 999		// grave accent
1000		chr(199).chr(155) => 'U', chr(199).chr(156) => 'u',
1001		);
1002
1003		// Used for locale-specific rules
1004		$locale = get_locale();
1005
1006		if ( 'de_DE' == $locale ) {
1007			$chars[ chr(195).chr(132) ] = 'Ae';
1008			$chars[ chr(195).chr(164) ] = 'ae';
1009			$chars[ chr(195).chr(150) ] = 'Oe';
1010			$chars[ chr(195).chr(182) ] = 'oe';
1011			$chars[ chr(195).chr(156) ] = 'Ue';
1012			$chars[ chr(195).chr(188) ] = 'ue';
1013			$chars[ chr(195).chr(159) ] = 'ss';
1014		} elseif ( 'da_DK' === $locale ) {
1015			$chars[ chr(195).chr(134) ] = 'Ae';
1016 			$chars[ chr(195).chr(166) ] = 'ae';
1017			$chars[ chr(195).chr(152) ] = 'Oe';
1018			$chars[ chr(195).chr(184) ] = 'oe';
1019			$chars[ chr(195).chr(133) ] = 'Aa';
1020			$chars[ chr(195).chr(165) ] = 'aa';
1021		}
1022
1023		$string = strtr($string, $chars);
1024	} else {
1025		// Assume ISO-8859-1 if not UTF-8
1026		$chars['in'] = chr(128).chr(131).chr(138).chr(142).chr(154).chr(158)
1027			.chr(159).chr(162).chr(165).chr(181).chr(192).chr(193).chr(194)
1028			.chr(195).chr(196).chr(197).chr(199).chr(200).chr(201).chr(202)
1029			.chr(203).chr(204).chr(205).chr(206).chr(207).chr(209).chr(210)
1030			.chr(211).chr(212).chr(213).chr(214).chr(216).chr(217).chr(218)
1031			.chr(219).chr(220).chr(221).chr(224).chr(225).chr(226).chr(227)
1032			.chr(228).chr(229).chr(231).chr(232).chr(233).chr(234).chr(235)
1033			.chr(236).chr(237).chr(238).chr(239).chr(241).chr(242).chr(243)
1034			.chr(244).chr(245).chr(246).chr(248).chr(249).chr(250).chr(251)
1035			.chr(252).chr(253).chr(255);
1036
1037		$chars['out'] = "EfSZszYcYuAAAAAACEEEEIIIINOOOOOOUUUUYaaaaaaceeeeiiiinoooooouuuuyy";
1038
1039		$string = strtr($string, $chars['in'], $chars['out']);
1040		$double_chars['in'] = array(chr(140), chr(156), chr(198), chr(208), chr(222), chr(223), chr(230), chr(240), chr(254));
1041		$double_chars['out'] = array('OE', 'oe', 'AE', 'DH', 'TH', 'ss', 'ae', 'dh', 'th');
1042		$string = str_replace($double_chars['in'], $double_chars['out'], $string);
1043	}
1044
1045	return $string;
1046}
1047
1048/**
1049 * Sanitizes a filename, replacing whitespace with dashes.
1050 *
1051 * Removes special characters that are illegal in filenames on certain
1052 * operating systems and special characters requiring special escaping
1053 * to manipulate at the command line. Replaces spaces and consecutive
1054 * dashes with a single dash. Trims period, dash and underscore from beginning
1055 * and end of filename.
1056 *
1057 * @since 2.1.0
1058 *
1059 * @param string $filename The filename to be sanitized
1060 * @return string The sanitized filename
1061 */
1062function sanitize_file_name( $filename ) {
1063	$filename_raw = $filename;
1064	$special_chars = array("?", "[", "]", "/", "\\", "=", "<", ">", ":", ";", ",", "'", "\"", "&", "$", "#", "*", "(", ")", "|", "~", "`", "!", "{", "}", chr(0));
1065	/**
1066	 * Filter the list of characters to remove from a filename.
1067	 *
1068	 * @since 2.8.0
1069	 *
1070	 * @param array  $special_chars Characters to remove.
1071	 * @param string $filename_raw  Filename as it was passed into sanitize_file_name().
1072	 */
1073	$special_chars = apply_filters( 'sanitize_file_name_chars', $special_chars, $filename_raw );
1074	$filename = preg_replace( "#\x{00a0}#siu", ' ', $filename );
1075	$filename = str_replace( $special_chars, '', $filename );
1076	$filename = str_replace( array( '%20', '+' ), '-', $filename );
1077	$filename = preg_replace( '/[\r\n\t -]+/', '-', $filename );
1078	$filename = trim( $filename, '.-_' );
1079
1080	// Split the filename into a base and extension[s]
1081	$parts = explode('.', $filename);
1082
1083	// Return if only one extension
1084	if ( count( $parts ) <= 2 ) {
1085		/**
1086		 * Filter a sanitized filename string.
1087		 *
1088		 * @since 2.8.0
1089		 *
1090		 * @param string $filename     Sanitized filename.
1091		 * @param string $filename_raw The filename prior to sanitization.
1092		 */
1093		return apply_filters( 'sanitize_file_name', $filename, $filename_raw );
1094	}
1095
1096	// Process multiple extensions
1097	$filename = array_shift($parts);
1098	$extension = array_pop($parts);
1099	$mimes = get_allowed_mime_types();
1100
1101	/*
1102	 * Loop over any intermediate extensions. Postfix them with a trailing underscore
1103	 * if they are a 2 - 5 character long alpha string not in the extension whitelist.
1104	 */
1105	foreach ( (array) $parts as $part) {
1106		$filename .= '.' . $part;
1107
1108		if ( preg_match("/^[a-zA-Z]{2,5}\d?$/", $part) ) {
1109			$allowed = false;
1110			foreach ( $mimes as $ext_preg => $mime_match ) {
1111				$ext_preg = '!^(' . $ext_preg . ')$!i';
1112				if ( preg_match( $ext_preg, $part ) ) {
1113					$allowed = true;
1114					break;
1115				}
1116			}
1117			if ( !$allowed )
1118				$filename .= '_';
1119		}
1120	}
1121	$filename .= '.' . $extension;
1122	/** This filter is documented in wp-includes/formatting.php */
1123	return apply_filters('sanitize_file_name', $filename, $filename_raw);
1124}
1125
1126/**
1127 * Sanitizes a username, stripping out unsafe characters.
1128 *
1129 * Removes tags, octets, entities, and if strict is enabled, will only keep
1130 * alphanumeric, _, space, ., -, @. After sanitizing, it passes the username,
1131 * raw username (the username in the parameter), and the value of $strict as
1132 * parameters for the 'sanitize_user' filter.
1133 *
1134 * @since 2.0.0
1135 *
1136 * @param string $username The username to be sanitized.
1137 * @param bool $strict If set limits $username to specific characters. Default false.
1138 * @return string The sanitized username, after passing through filters.
1139 */
1140function sanitize_user( $username, $strict = false ) {
1141	$raw_username = $username;
1142	$username = wp_strip_all_tags( $username );
1143	$username = remove_accents( $username );
1144	// Kill octets
1145	$username = preg_replace( '|%([a-fA-F0-9][a-fA-F0-9])|', '', $username );
1146	$username = preg_replace( '/&.+?;/', '', $username ); // Kill entities
1147
1148	// If strict, reduce to ASCII for max portability.
1149	if ( $strict )
1150		$username = preg_replace( '|[^a-z0-9 _.\-@]|i', '', $username );
1151
1152	$username = trim( $username );
1153	// Consolidate contiguous whitespace
1154	$username = preg_replace( '|\s+|', ' ', $username );
1155
1156	/**
1157	 * Filter a sanitized username string.
1158	 *
1159	 * @since 2.0.1
1160	 *
1161	 * @param string $username     Sanitized username.
1162	 * @param string $raw_username The username prior to sanitization.
1163	 * @param bool   $strict       Whether to limit the sanitization to specific characters. Default false.
1164	 */
1165	return apply_filters( 'sanitize_user', $username, $raw_username, $strict );
1166}
1167
1168/**
1169 * Sanitizes a string key.
1170 *
1171 * Keys are used as internal identifiers. Lowercase alphanumeric characters, dashes and underscores are allowed.
1172 *
1173 * @since 3.0.0
1174 *
1175 * @param string $key String key
1176 * @return string Sanitized key
1177 */
1178function sanitize_key( $key ) {
1179	$raw_key = $key;
1180	$key = strtolower( $key );
1181	$key = preg_replace( '/[^a-z0-9_\-]/', '', $key );
1182
1183	/**
1184	 * Filter a sanitized key string.
1185	 *
1186	 * @since 3.0.0
1187	 *
1188	 * @param string $key     Sanitized key.
1189	 * @param string $raw_key The key prior to sanitization.
1190	 */
1191	return apply_filters( 'sanitize_key', $key, $raw_key );
1192}
1193
1194/**
1195 * Sanitizes a title, or returns a fallback title.
1196 *
1197 * Specifically, HTML and PHP tags are stripped. Further actions can be added
1198 * via the plugin API. If $title is empty and $fallback_title is set, the latter
1199 * will be used.
1200 *
1201 * @since 1.0.0
1202 *
1203 * @param string $title The string to be sanitized.
1204 * @param string $fallback_title Optional. A title to use if $title is empty.
1205 * @param string $context Optional. The operation for which the string is sanitized
1206 * @return string The sanitized string.
1207 */
1208function sanitize_title( $title, $fallback_title = '', $context = 'save' ) {
1209	$raw_title = $title;
1210
1211	if ( 'save' == $context )
1212		$title = remove_accents($title);
1213
1214	/**
1215	 * Filter a sanitized title string.
1216	 *
1217	 * @since 1.2.0
1218	 *
1219	 * @param string $title     Sanitized title.
1220	 * @param string $raw_title The title prior to sanitization.
1221	 * @param string $context   The context for which the title is being sanitized.
1222	 */
1223	$title = apply_filters( 'sanitize_title', $title, $raw_title, $context );
1224
1225	if ( '' === $title || false === $title )
1226		$title = $fallback_title;
1227
1228	return $title;
1229}
1230
1231/**
1232 * Sanitizes a title with the 'query' context.
1233 *
1234 * Used for querying the database for a value from URL.
1235 *
1236 * @since 3.1.0
1237 *
1238 * @param string $title The string to be sanitized.
1239 * @return string The sanitized string.
1240 */
1241function sanitize_title_for_query( $title ) {
1242	return sanitize_title( $title, '', 'query' );
1243}
1244
1245/**
1246 * Sanitizes a title, replacing whitespace and a few other characters with dashes.
1247 *
1248 * Limits the output to alphanumeric characters, underscore (_) and dash (-).
1249 * Whitespace becomes a dash.
1250 *
1251 * @since 1.2.0
1252 *
1253 * @param string $title The title to be sanitized.
1254 * @param string $raw_title Optional. Not used.
1255 * @param string $context Optional. The operation for which the string is sanitized.
1256 * @return string The sanitized title.
1257 */
1258function sanitize_title_with_dashes( $title, $raw_title = '', $context = 'display' ) {
1259	$title = strip_tags($title);
1260	// Preserve escaped octets.
1261	$title = preg_replace('|%([a-fA-F0-9][a-fA-F0-9])|', '---$1---', $title);
1262	// Remove percent signs that are not part of an octet.
1263	$title = str_replace('%', '', $title);
1264	// Restore octets.
1265	$title = preg_replace('|---([a-fA-F0-9][a-fA-F0-9])---|', '%$1', $title);
1266
1267	if (seems_utf8($title)) {
1268		if (function_exists('mb_strtolower')) {
1269			$title = mb_strtolower($title, 'UTF-8');
1270		}
1271		$title = utf8_uri_encode($title, 200);
1272	}
1273
1274	$title = strtolower($title);
1275	$title = preg_replace('/&.+?;/', '', $title); // kill entities
1276	$title = str_replace('.', '-', $title);
1277
1278	if ( 'save' == $context ) {
1279		// Convert nbsp, ndash and mdash to hyphens
1280		$title = str_replace( array( '%c2%a0', '%e2%80%93', '%e2%80%94' ), '-', $title );
1281
1282		// Strip these characters entirely
1283		$title = str_replace( array(
1284			// iexcl and iquest
1285			'%c2%a1', '%c2%bf',
1286			// angle quotes
1287			'%c2%ab', '%c2%bb', '%e2%80%b9', '%e2%80%ba',
1288			// curly quotes
1289			'%e2%80%98', '%e2%80%99', '%e2%80%9c', '%e2%80%9d',
1290			'%e2%80%9a', '%e2%80%9b', '%e2%80%9e', '%e2%80%9f',
1291			// copy, reg, deg, hellip and trade
1292			'%c2%a9', '%c2%ae', '%c2%b0', '%e2%80%a6', '%e2%84%a2',
1293			// acute accents
1294			'%c2%b4', '%cb%8a', '%cc%81', '%cd%81',
1295			// grave accent, macron, caron
1296			'%cc%80', '%cc%84', '%cc%8c',
1297		), '', $title );
1298
1299		// Convert times to x
1300		$title = str_replace( '%c3%97', 'x', $title );
1301	}
1302
1303	$title = preg_replace('/[^%a-z0-9 _-]/', '', $title);
1304	$title = preg_replace('/\s+/', '-', $title);
1305	$title = preg_replace('|-+|', '-', $title);
1306	$title = trim($title, '-');
1307
1308	return $title;
1309}
1310
1311/**
1312 * Ensures a string is a valid SQL order by clause.
1313 *
1314 * Accepts one or more columns, with or without ASC/DESC, and also accepts
1315 * RAND().
1316 *
1317 * @since 2.5.1
1318 *
1319 * @param string $orderby Order by string to be checked.
1320 * @return false|string Returns the order by clause if it is a match, false otherwise.
1321 */
1322function sanitize_sql_orderby( $orderby ){
1323	preg_match('/^\s*([a-z0-9_]+(\s+(ASC|DESC))?(\s*,\s*|\s*$))+|^\s*RAND\(\s*\)\s*$/i', $orderby, $obmatches);
1324	if ( !$obmatches )
1325		return false;
1326	return $orderby;
1327}
1328
1329/**
1330 * Sanitizes an HTML classname to ensure it only contains valid characters.
1331 *
1332 * Strips the string down to A-Z,a-z,0-9,_,-. If this results in an empty
1333 * string then it will return the alternative value supplied.
1334 *
1335 * @todo Expand to support the full range of CDATA that a class attribute can contain.
1336 *
1337 * @since 2.8.0
1338 *
1339 * 

Large files files are truncated, but you can click here to view the full file