PageRenderTime 69ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-includes/formatting.php

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

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