PageRenderTime 70ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-includes/formatting.php

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

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