PageRenderTime 81ms CodeModel.GetById 42ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-includes/formatting.php

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

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