PageRenderTime 64ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 2ms

/wp-includes/formatting.php

https://bitbucket.org/Thane2376/death-edge.ru
PHP | 3994 lines | 2366 code | 329 blank | 1299 comment | 330 complexity | f7d3cfcabaa821265be4326e1373f41b MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0, LGPL-3.0, AGPL-1.0

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

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