PageRenderTime 64ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/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
  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. * @param string $deprecated Not used.
  1194. * @return string Converted string.
  1195. */
  1196. function convert_chars($content, $deprecated = '') {
  1197. if ( !empty( $deprecated ) )
  1198. _deprecated_argument( __FUNCTION__, '0.71' );
  1199. // Translation of invalid Unicode references range to valid range
  1200. $wp_htmltranswinuni = array(
  1201. '&#128;' => '&#8364;', // the Euro sign
  1202. '&#129;' => '',
  1203. '&#130;' => '&#8218;', // these are Windows CP1252 specific characters
  1204. '&#131;' => '&#402;', // they would look weird on non-Windows browsers
  1205. '&#132;' => '&#8222;',
  1206. '&#133;' => '&#8230;',
  1207. '&#134;' => '&#8224;',
  1208. '&#135;' => '&#8225;',
  1209. '&#136;' => '&#710;',
  1210. '&#137;' => '&#8240;',
  1211. '&#138;' => '&#352;',
  1212. '&#139;' => '&#8249;',
  1213. '&#140;' => '&#338;',
  1214. '&#141;' => '',
  1215. '&#142;' => '&#381;',
  1216. '&#143;' => '',
  1217. '&#144;' => '',
  1218. '&#145;' => '&#8216;',
  1219. '&#146;' => '&#8217;',
  1220. '&#147;' => '&#8220;',
  1221. '&#148;' => '&#8221;',
  1222. '&#149;' => '&#8226;',
  1223. '&#150;' => '&#8211;',
  1224. '&#151;' => '&#8212;',
  1225. '&#152;' => '&#732;',
  1226. '&#153;' => '&#8482;',
  1227. '&#154;' => '&#353;',
  1228. '&#155;' => '&#8250;',
  1229. '&#156;' => '&#339;',
  1230. '&#157;' => '',
  1231. '&#158;' => '&#382;',
  1232. '&#159;' => '&#376;'
  1233. );
  1234. // Remove metadata tags
  1235. $content = preg_replace('/<title>(.+?)<\/title>/','',$content);
  1236. $content = preg_replace('/<category>(.+?)<\/category>/','',$content);
  1237. // Converts lone & characters into &#38; (a.k.a. &amp;)
  1238. $content = preg_replace('/&([^#])(?![a-z1-4]{1,8};)/i', '&#038;$1', $content);
  1239. // Fix Word pasting
  1240. $content = strtr($content, $wp_htmltranswinuni);
  1241. // Just a little XHTML help
  1242. $content = str_replace('<br>', '<br />', $content);
  1243. $content = str_replace('<hr>', '<hr />', $content);
  1244. return $content;
  1245. }
  1246. /**
  1247. * Balances tags if forced to, or if the 'use_balanceTags' option is set to true.
  1248. *
  1249. * @since 0.71
  1250. *
  1251. * @param string $text Text to be balanced
  1252. * @param bool $force If true, forces balancing, ignoring the value of the option. Default false.
  1253. * @return string Balanced text
  1254. */
  1255. function balanceTags( $text, $force = false ) {
  1256. if ( $force || get_option('use_balanceTags') == 1 ) {
  1257. return force_balance_tags( $text );
  1258. } else {
  1259. return $text;
  1260. }
  1261. }
  1262. /**
  1263. * Balances tags of string using a modified stack.
  1264. *
  1265. * @since 2.0.4
  1266. *
  1267. * @author Leonard Lin <leonard@acm.org>
  1268. * @license GPL
  1269. * @copyright November 4, 2001
  1270. * @version 1.1
  1271. * @todo Make better - change loop condition to $text in 1.2
  1272. * @internal Modified by Scott Reilly (coffee2code) 02 Aug 2004
  1273. * 1.1 Fixed handling of append/stack pop order of end text
  1274. * Added Cleaning Hooks
  1275. * 1.0 First Version
  1276. *
  1277. * @param string $text Text to be balanced.
  1278. * @return string Balanced text.
  1279. */
  1280. function force_balance_tags( $text ) {
  1281. $tagstack = array();
  1282. $stacksize = 0;
  1283. $tagqueue = '';
  1284. $newtext = '';
  1285. // Known single-entity/self-closing tags
  1286. $single_tags = array( 'area', 'base', 'basefont', 'br', 'col', 'command', 'embed', 'frame', 'hr', 'img', 'input', 'isindex', 'link', 'meta', 'param', 'source' );
  1287. // Tags that can be immediately nested within themselves
  1288. $nestable_tags = array( 'blockquote', 'div', 'object', 'q', 'span' );
  1289. // WP bug fix for comments - in case you REALLY meant to type '< !--'
  1290. $text = str_replace('< !--', '< !--', $text);
  1291. // WP bug fix for LOVE <3 (and other situations with '<' before a number)
  1292. $text = preg_replace('#<([0-9]{1})#', '&lt;$1', $text);
  1293. while ( preg_match("/<(\/?[\w:]*)\s*([^>]*)>/", $text, $regex) ) {
  1294. $newtext .= $tagqueue;
  1295. $i = strpos($text, $regex[0]);
  1296. $l = strlen($regex[0]);
  1297. // clear the shifter
  1298. $tagqueue = '';
  1299. // Pop or Push
  1300. if ( isset($regex[1][0]) && '/' == $regex[1][0] ) { // End Tag
  1301. $tag = strtolower(substr($regex[1],1));
  1302. // if too many closing tags
  1303. if( $stacksize <= 0 ) {
  1304. $tag = '';
  1305. // or close to be safe $tag = '/' . $tag;
  1306. }
  1307. // if stacktop value = tag close value then pop
  1308. else if ( $tagstack[$stacksize - 1] == $tag ) { // found closing tag
  1309. $tag = '</' . $tag . '>'; // Close Tag
  1310. // Pop
  1311. array_pop( $tagstack );
  1312. $stacksize--;
  1313. } else { // closing tag not at top, search for it
  1314. for ( $j = $stacksize-1; $j >= 0; $j-- ) {
  1315. if ( $tagstack[$j] == $tag ) {
  1316. // add tag to tagqueue
  1317. for ( $k = $stacksize-1; $k >= $j; $k--) {
  1318. $tagqueue .= '</' . array_pop( $tagstack ) . '>';
  1319. $stacksize--;
  1320. }
  1321. break;
  1322. }
  1323. }
  1324. $tag = '';
  1325. }
  1326. } else { // Begin Tag
  1327. $tag = strtolower($regex[1]);
  1328. // Tag Cleaning
  1329. // If it's an empty tag "< >", do nothing
  1330. if ( '' == $tag ) {
  1331. // do nothing
  1332. }
  1333. // ElseIf it presents itself as a self-closing tag...
  1334. elseif ( substr( $regex[2], -1 ) == '/' ) {
  1335. // ...but it isn't a known single-entity self-closing tag, then don't let it be treated as such and
  1336. // immediately close it with a closing tag (the tag will encapsulate no text as a result)
  1337. if ( ! in_array( $tag, $single_tags ) )
  1338. $regex[2] = trim( substr( $regex[2], 0, -1 ) ) . "></$tag";
  1339. }
  1340. // ElseIf it's a known single-entity tag but it doesn't close itself, do so
  1341. elseif ( in_array($tag, $single_tags) ) {
  1342. $regex[2] .= '/';
  1343. }
  1344. // Else it's not a single-entity tag
  1345. else {
  1346. // If the top of the stack is the same as the tag we want to push, close previous tag
  1347. if ( $stacksize > 0 && !in_array($tag, $nestable_tags) && $tagstack[$stacksize - 1] == $tag ) {
  1348. $tagqueue = '</' . array_pop( $tagstack ) . '>';
  1349. $stacksize--;
  1350. }
  1351. $stacksize = array_push( $tagstack, $tag );
  1352. }
  1353. // Attributes
  1354. $attributes = $regex[2];
  1355. if( ! empty( $attributes ) && $attributes[0] != '>' )
  1356. $attributes = ' ' . $attributes;
  1357. $tag = '<' . $tag . $attributes . '>';
  1358. //If already queuing a close tag, then put this tag on, too
  1359. if ( !empty($tagqueue) ) {
  1360. $tagqueue .= $tag;
  1361. $tag = '';
  1362. }
  1363. }
  1364. $newtext .= substr($text, 0, $i) . $tag;
  1365. $text = substr($text, $i + $l);
  1366. }
  1367. // Clear Tag Queue
  1368. $newtext .= $tagqueue;
  1369. // Add Remaining text
  1370. $newtext .= $text;
  1371. // Empty Stack
  1372. while( $x = array_pop($tagstack) )
  1373. $newtext .= '</' . $x . '>'; // Add remaining tags to close
  1374. // WP fix for the bug with HTML comments
  1375. $newtext = str_replace("< !--","<!--",$newtext);
  1376. $newtext = str_replace("< !--","< !--",$newtext);
  1377. return $newtext;
  1378. }
  1379. /**
  1380. * Acts on text which is about to be edited.
  1381. *
  1382. * The $content is run through esc_textarea(), which uses htmlspecialchars()
  1383. * to convert special characters to HTML entities. If $richedit is set to true,
  1384. * it is simply a holder for the 'format_to_edit' filter.
  1385. *
  1386. * @since 0.71
  1387. *
  1388. * @param string $content The text about to be edited.
  1389. * @param bool $richedit Whether the $content should not pass through htmlspecialchars(). Default false (meaning it will be passed).
  1390. * @return string The text after the filter (and possibly htmlspecialchars()) has been run.
  1391. */
  1392. function format_to_edit( $content, $richedit = false ) {
  1393. /**
  1394. * Filter the text to be formatted for editing.
  1395. *
  1396. * @since 1.2.0
  1397. *
  1398. * @param string $content The text, prior to formatting for editing.
  1399. */
  1400. $content = apply_filters( 'format_to_edit', $content );
  1401. if ( ! $richedit )
  1402. $content = esc_textarea( $content );
  1403. return $content;
  1404. }
  1405. /**
  1406. * Add leading zeros when necessary.
  1407. *
  1408. * If you set the threshold to '4' and the number is '10', then you will get
  1409. * back '0010'. If you set the threshold to '4' and the number is '5000', then you
  1410. * will get back '5000'.
  1411. *
  1412. * Uses sprintf to append the amount of zeros based on the $threshold parameter
  1413. * and the size of the number. If the number is large enough, then no zeros will
  1414. * be appended.
  1415. *
  1416. * @since 0.71
  1417. *
  1418. * @param mixed $number Number to append zeros to if not greater than threshold.
  1419. * @param int $threshold Digit places number needs to be to not have zeros added.
  1420. * @return string Adds leading zeros to number if needed.
  1421. */
  1422. function zeroise($number, $threshold) {
  1423. return sprintf('%0'.$threshold.'s', $number);
  1424. }
  1425. /**
  1426. * Adds backslashes before letters and before a number at the start of a string.
  1427. *
  1428. * @since 0.71
  1429. *
  1430. * @param string $string Value to which backslashes will be added.
  1431. * @return string String with backslashes inserted.
  1432. */
  1433. function backslashit($string) {
  1434. if ( isset( $string[0] ) && $string[0] >= '0' && $string[0] <= '9' )
  1435. $string = '\\\\' . $string;
  1436. return addcslashes( $string, 'A..Za..z' );
  1437. }
  1438. /**
  1439. * Appends a trailing slash.
  1440. *
  1441. * Will remove trailing forward and backslashes if it exists already before adding
  1442. * a trailing forward slash. This prevents double slashing a string or path.
  1443. *
  1444. * The primary use of this is for paths and thus should be used for paths. It is
  1445. * not restricted to paths and offers no specific path support.
  1446. *
  1447. * @since 1.2.0
  1448. *
  1449. * @param string $string What to add the trailing slash to.
  1450. * @return string String with trailing slash added.
  1451. */
  1452. function trailingslashit( $string ) {
  1453. return untrailingslashit( $string ) . '/';
  1454. }
  1455. /**
  1456. * Removes trailing forward slashes and backslashes if they exist.
  1457. *
  1458. * The primary use of this is for paths and thus should be used for paths. It is
  1459. * not restricted to paths and offers no specific path support.
  1460. *
  1461. * @since 2.2.0
  1462. *
  1463. * @param string $string What to remove the trailing slashes from.
  1464. * @return string String without the trailing slashes.
  1465. */
  1466. function untrailingslashit( $string ) {
  1467. return rtrim( $string, '/\\' );
  1468. }
  1469. /**
  1470. * Adds slashes to escape strings.
  1471. *
  1472. * Slashes will first be removed if magic_quotes_gpc is set, see {@link
  1473. * http://www.php.net/magic_quotes} for more details.
  1474. *
  1475. * @since 0.71
  1476. *
  1477. * @param string $gpc The string returned from HTTP request data.
  1478. * @return string Returns a string escaped with slashes.
  1479. */
  1480. function addslashes_gpc($gpc) {
  1481. if ( get_magic_quotes_gpc() )
  1482. $gpc = stripslashes($gpc);
  1483. return wp_slash($gpc);
  1484. }
  1485. /**
  1486. * Navigates through an array and removes slashes from the values.
  1487. *
  1488. * If an array is passed, the array_map() function causes a callback to pass the
  1489. * value back to the function. The slashes from this value will removed.
  1490. *
  1491. * @since 2.0.0
  1492. *
  1493. * @param mixed $value The value to be stripped.
  1494. * @return mixed Stripped value.
  1495. */
  1496. function stripslashes_deep($value) {
  1497. if ( is_array($value) ) {
  1498. $value = array_map('stripslashes_deep', $value);
  1499. } elseif ( is_object($value) ) {
  1500. $vars = get_object_vars( $value );
  1501. foreach ($vars as $key=>$data) {
  1502. $value->{$key} = stripslashes_deep( $data );
  1503. }
  1504. } elseif ( is_string( $value ) ) {
  1505. $value = stripslashes($value);
  1506. }
  1507. return $value;
  1508. }
  1509. /**
  1510. * Navigates through an array and encodes the values to be used in a URL.
  1511. *
  1512. *
  1513. * @since 2.2.0
  1514. *
  1515. * @param array|string $value The array or string to be encoded.
  1516. * @return array|string $value The encoded array (or string from the callback).
  1517. */
  1518. function urlencode_deep($value) {
  1519. $value = is_array($value) ? array_map('urlencode_deep', $value) : urlencode($value);
  1520. return $value;
  1521. }
  1522. /**
  1523. * Navigates through an array and raw encodes the values to be used in a URL.
  1524. *
  1525. * @since 3.4.0
  1526. *
  1527. * @param array|string $value The array or string to be encoded.
  1528. * @return array|string $value The encoded array (or string from the callback).
  1529. */
  1530. function rawurlencode_deep( $value ) {
  1531. return is_array( $value ) ? array_map( 'rawurlencode_deep', $value ) : rawurlencode( $value );
  1532. }
  1533. /**
  1534. * Converts email addresses characters to HTML entities to block spam bots.
  1535. *
  1536. * @since 0.71
  1537. *
  1538. * @param string $email_address Email address.
  1539. * @param int $hex_encoding Optional. Set to 1 to enable hex encoding.
  1540. * @return string Converted email address.
  1541. */
  1542. function antispambot( $email_address, $hex_encoding = 0 ) {
  1543. $email_no_spam_address = '';
  1544. for ( $i = 0; $i < strlen( $email_address ); $i++ ) {
  1545. $j = rand( 0, 1 + $hex_encoding );
  1546. if ( $j == 0 ) {
  1547. $email_no_spam_address .= '&#' . ord( $email_address[$i] ) . ';';
  1548. } elseif ( $j == 1 ) {
  1549. $email_no_spam_address .= $email_address[$i];
  1550. } elseif ( $j == 2 ) {
  1551. $email_no_spam_address .= '%' . zeroise( dechex( ord( $email_address[$i] ) ), 2 );
  1552. }
  1553. }
  1554. $email_no_spam_address = str_replace( '@', '&#64;', $email_no_spam_address );
  1555. return $email_no_spam_address;
  1556. }
  1557. /**
  1558. * Callback to convert URI match to HTML A element.
  1559. *
  1560. * This function was backported from 2.5.0 to 2.3.2. Regex callback for {@link
  1561. * make_clickable()}.
  1562. *
  1563. * @since 2.3.2
  1564. * @access private
  1565. *
  1566. * @param array $matches Single Regex Match.
  1567. * @return string HTML A element with URI address.
  1568. */
  1569. function _make_url_clickable_cb($matches) {
  1570. $url = $matches[2];
  1571. if ( ')' == $matches[3] && strpos( $url, '(' ) ) {
  1572. // If the trailing character is a closing parethesis, and the URL has an opening parenthesis in it, add the closing parenthesis to the URL.
  1573. // Then we can let the parenthesis balancer do its thing below.
  1574. $url .= $matches[3];
  1575. $suffix = '';
  1576. } else {
  1577. $suffix = $matches[3];
  1578. }
  1579. // Include parentheses in the URL only if paired
  1580. while ( substr_count( $url, '(' ) < substr_count( $url, ')' ) ) {
  1581. $suffix = strrchr( $url, ')' ) . $suffix;
  1582. $url = substr( $url, 0, strrpos( $url, ')' ) );
  1583. }
  1584. $url = esc_url($url);
  1585. if ( empty($url) )
  1586. return $matches[0];
  1587. return $matches[1] . "<a href=\"$url\" rel=\"nofollow\">$url</a>" . $suffix;
  1588. }
  1589. /**
  1590. * Callback to convert URL match to HTML A element.
  1591. *
  1592. * This function was backported from 2.5.0 to 2.3.2. Regex callback for {@link
  1593. * make_clickable()}.
  1594. *
  1595. * @since 2.3.2
  1596. * @access private
  1597. *
  1598. * @param array $matches Single Regex Match.
  1599. * @return string HTML A element with URL address.
  1600. */
  1601. function _make_web_ftp_clickable_cb($matches) {
  1602. $ret = '';
  1603. $dest = $matches[2];
  1604. $dest = 'http://' . $dest;
  1605. $dest = esc_url($dest);
  1606. if ( empty($dest) )
  1607. return $matches[0];
  1608. // removed trailing [.,;:)] from URL
  1609. if ( in_array( substr($dest, -1), array('.', ',', ';', ':', ')') ) === true ) {
  1610. $ret = substr($dest, -1);
  1611. $dest = substr($dest, 0, strlen($dest)-1);
  1612. }
  1613. return $matches[1] . "<a href=\"$dest\" rel=\"nofollow\">$dest</a>$ret";
  1614. }
  1615. /**
  1616. * Callback to convert email address match to HTML A element.
  1617. *
  1618. * This function was backported from 2.5.0 to 2.3.2. Regex callback for {@link
  1619. * make_clickable()}.
  1620. *
  1621. * @since 2.3.2
  1622. * @access private
  1623. *
  1624. * @param array $matches Single Regex Match.
  1625. * @return string HTML A element with email address.
  1626. */
  1627. function _make_email_clickable_cb($matches) {
  1628. $email = $matches[2] . '@' . $matches[3];
  1629. return $matches[1] . "<a href=\"mailto:$email\">$email</a>";
  1630. }
  1631. /**
  1632. * Convert plaintext URI to HTML links.
  1633. *
  1634. * Converts URI, www and ftp, and email addresses. Finishes by fixing links
  1635. * within links.
  1636. *
  1637. * @since 0.71
  1638. *
  1639. * @param string $text Content to convert URIs.
  1640. * @return string Content with converted URIs.
  1641. */
  1642. function make_clickable( $text ) {
  1643. $r = '';
  1644. $textarr = preg_split( '/(<[^<>]+>)/', $text, -1, PREG_SPLIT_DELIM_CAPTURE ); // split out HTML tags
  1645. $nested_code_pre = 0; // Keep track of how many levels link is nested inside <pre> or <code>
  1646. foreach ( $textarr as $piece ) {
  1647. if ( preg_match( '|^<code[\s>]|i', $piece ) || preg_match( '|^<pre[\s>]|i', $piece ) )
  1648. $nested_code_pre++;
  1649. elseif ( ( '</code>' === strtolower( $piece ) || '</pre>' === strtolower( $piece ) ) && $nested_code_pre )
  1650. $nested_code_pre--;
  1651. if ( $nested_code_pre || empty( $piece ) || ( $piece[0] === '<' && ! preg_match( '|^<\s*[\w]{1,20}+://|', $piece ) ) ) {
  1652. $r .= $piece;
  1653. continue;
  1654. }
  1655. // Long strings might contain expensive edge cases ...
  1656. if ( 10000 < strlen( $piece ) ) {
  1657. // ... break it up
  1658. foreach ( _split_str_by_whitespace( $piece, 2100 ) as $chunk ) { // 2100: Extra room for scheme and leading and trailing paretheses
  1659. if ( 2101 < strlen( $chunk ) ) {
  1660. $r .= $chunk; // Too big, no whitespace: bail.
  1661. } else {
  1662. $r .= make_clickable( $chunk );
  1663. }
  1664. }
  1665. } else {
  1666. $ret = " $piece "; // Pad with whitespace to simplify the regexes
  1667. $url_clickable = '~
  1668. ([\\s(<.,;:!?]) # 1: Leading whitespace, or punctuation
  1669. ( # 2: URL
  1670. [\\w]{1,20}+:// # Scheme and hier-part prefix
  1671. (?=\S{1,2000}\s) # Limit to URLs less than about 2000 characters long
  1672. [\\w\\x80-\\xff#%\\~/@\\[\\]*(+=&$-]*+ # Non-punctuation URL character
  1673. (?: # Unroll the Loop: Only allow puctuation URL character if followed by a non-punctuation URL character
  1674. [\'.,;:!?)] # Punctuation URL character
  1675. [\\w\\x80-\\xff#%\\~/@\\[\\]*(+=&$-]++ # Non-punctuation URL character
  1676. )*
  1677. )
  1678. (\)?) # 3: Trailing closing parenthesis (for parethesis balancing post processing)
  1679. ~xS'; // The regex is a non-anchored pattern and does not have a single fixed starting character.
  1680. // Tell PCRE to spend more time optimizing since, when used on a page load, it will probably be used several times.
  1681. $ret = preg_replace_callback( $url_clickable, '_make_url_clickable_cb', $ret );
  1682. $ret = preg_replace_callback( '#([\s>])((www|ftp)\.[\w\\x80-\\xff\#$%&~/.\-;:=,?@\[\]+]+)#is', '_make_web_ftp_clickable_cb', $ret );
  1683. $ret = preg_replace_callback( '#([\s>])([.0-9a-z_+-]+)@(([0-9a-z-]+\.)+[0-9a-z]{2,})#i', '_make_email_clickable_cb', $ret );
  1684. $ret = substr( $ret, 1, -1 ); // Remove our whitespace padding.
  1685. $r .= $ret;
  1686. }
  1687. }
  1688. // Cleanup of accidental links within links
  1689. $r = preg_replace( '#(<a([ \r\n\t]+[^>]+?>|>))<a [^>]+?>([^>]+?)</a></a>#i', "$1$3</a>", $r );
  1690. return $r;
  1691. }
  1692. /**
  1693. * Breaks a string into chunks by splitting at whitespace characters.
  1694. * The length of each returned chunk is as close to the specified length goal as possible,
  1695. * with the caveat that each chunk includes its trailing delimiter.
  1696. * Chunks longer than the goal are guaranteed to not have any inner whitespace.
  1697. *
  1698. * Joining the returned chunks with empty delimiters reconstructs the input string losslessly.
  1699. *
  1700. * Input string must have no null characters (or eventual transformations on output chunks must not care about null characters)
  1701. *
  1702. * <code>
  1703. * _split_str_by_whitespace( "1234 67890 1234 67890a cd 1234 890 123456789 1234567890a 45678 1 3 5 7 90 ", 10 ) ==
  1704. * array (
  1705. * 0 => '1234 67890 ', // 11 characters: Perfect split
  1706. * 1 => '1234 ', // 5 characters: '1234 67890a' was too long
  1707. * 2 => '67890a cd ', // 10 characters: '67890a cd 1234' was too long
  1708. * 3 => '1234 890 ', // 11 characters: Perfect split
  1709. * 4 => '123456789 ', // 10 characters: '123456789 1234567890a' was too long
  1710. * 5 => '1234567890a ', // 12 characters: Too long, but no inner whitespace on which to split
  1711. * 6 => ' 45678 ', // 11 characters: Perfect split
  1712. * 7 => '1 3 5 7 9', // 9 characters: End of $string
  1713. * );
  1714. * </code>
  1715. *
  1716. * @since 3.4.0
  1717. * @access private
  1718. *
  1719. * @param string $string The string to split.
  1720. * @param int $goal The desired chunk length.
  1721. * @return array Numeric array of chunks.
  1722. */
  1723. function _split_str_by_whitespace( $string, $goal ) {
  1724. $chunks = array();
  1725. $string_nullspace = strtr( $string, "\r\n\t\v\f ", "\000\000\000\000\000\000" );
  1726. while ( $goal < strlen( $string_nullspace ) ) {
  1727. $pos = strrpos( substr( $string_nullspace, 0, $goal + 1 ), "\000" );
  1728. if ( false === $pos ) {
  1729. $pos = strpos( $string_nullspace, "\000", $goal + 1 );
  1730. if ( false === $pos ) {
  1731. break;
  1732. }
  1733. }
  1734. $chunks[] = substr( $string, 0, $pos + 1 );
  1735. $string = substr( $string, $pos + 1 );
  1736. $string_nullspace = substr( $string_nullspace, $pos + 1 );
  1737. }
  1738. if ( $string ) {
  1739. $chunks[] = $string;
  1740. }
  1741. return $chunks;
  1742. }
  1743. /**
  1744. * Adds rel nofollow string to all HTML A elements in content.
  1745. *
  1746. * @since 1.5.0
  1747. *
  1748. * @param string $text Content that may contain HTML A elements.
  1749. * @return string Converted content.
  1750. */
  1751. function wp_rel_nofollow( $text ) {
  1752. // This is a pre save filter, so text is already escaped.
  1753. $text = stripslashes($text);
  1754. $text = preg_replace_callback('|<a (.+?)>|i', 'wp_rel_nofollow_callback', $text);
  1755. $text = wp_slash($text);
  1756. return $text;
  1757. }
  1758. /**
  1759. * Callback to add rel=nofollow string to HTML A element.
  1760. *
  1761. * Will remove already existing rel="nofollow" and rel='nofollow' from the
  1762. * string to prevent from invalidating (X)HTML.
  1763. *
  1764. * @since 2.3.0
  1765. *
  1766. * @param array $matches Single Match
  1767. * @return string HTML A Element with rel nofollow.
  1768. */
  1769. function wp_rel_nofollow_callback( $matches ) {
  1770. $text = $matches[1];
  1771. $text = str_replace(array(' rel="nofollow"', " rel='nofollow'"), '', $text);
  1772. return "<a $text rel=\"nofollow\">";
  1773. }
  1774. /**
  1775. * Convert one smiley code to the icon graphic file equivalent.
  1776. *
  1777. * Callback handler for {@link convert_smilies()}.
  1778. * Looks up one smiley code in the $wpsmiliestrans global array and returns an
  1779. * <img> string for that smiley.
  1780. *
  1781. * @global array $wpsmiliestrans
  1782. * @since 2.8.0
  1783. *
  1784. * @param array $matches Single match. Smiley code to convert to image.
  1785. * @return string Image string for smiley.
  1786. */
  1787. function translate_smiley( $matches ) {
  1788. global $wpsmiliestrans;
  1789. if ( count( $matches ) == 0 )
  1790. return '';
  1791. $smiley = trim( reset( $matches ) );
  1792. $img = $wpsmiliestrans[ $smiley ];
  1793. /**
  1794. * Filter the Smiley image URL before it's used in the image element.
  1795. *
  1796. * @since 2.9.0
  1797. *
  1798. * @param string $smiley_url URL for the smiley image.
  1799. * @param string $img Filename for the smiley image.
  1800. * @param string $site_url Site URL, as returned by site_url().
  1801. */
  1802. $src_url = apply_filters( 'smilies_src', includes_url( "images/smilies/$img" ), $img, site_url() );
  1803. return sprintf( '<img src="%s" alt="%s" class="wp-smiley" />', esc_url( $src_url ), esc_attr( $smiley ) );
  1804. }
  1805. /**
  1806. * Convert text equivalent of smilies to images.
  1807. *
  1808. * Will only convert smilies if the option 'use_smilies' is true and the global
  1809. * used in the function isn't empty.
  1810. *
  1811. * @since 0.71
  1812. * @uses $wp_smiliessearch
  1813. *
  1814. * @param string $text Content to convert smilies from text.
  1815. * @return string Converted content with text smilies replaced with images.
  1816. */
  1817. function convert_smilies( $text ) {
  1818. global $wp_smiliessearch;
  1819. $output = '';
  1820. if ( get_option( 'use_smilies' ) && ! empty( $wp_smiliessearch ) ) {
  1821. // HTML loop taken from texturize function, could possible be consolidated
  1822. $textarr = preg_split( '/(<.*>)/U', $text, -1, PREG_SPLIT_DELIM_CAPTURE ); // capture the tags as well as in between
  1823. $stop = count( $textarr );// loop stuff
  1824. // Ignore proessing of specific tags
  1825. $tags_to_ignore = 'code|pre|style|script|textarea';
  1826. $ignore_block_element = '';
  1827. for ( $i = 0; $i < $stop; $i++ ) {
  1828. $content = $textarr[$i];
  1829. // If we're in an ignore block, wait until we find its closing tag
  1830. if ( '' == $ignore_block_element && preg_match( '/^<(' . $tags_to_ignore . ')>/', $content, $matches ) ) {
  1831. $ignore_block_element = $matches[1];
  1832. }
  1833. // If it's not a tag and not in ignore block
  1834. if ( '' == $ignore_block_element && strlen( $content ) > 0 && '<' != $content[0] ) {
  1835. $content = preg_replace_callback( $wp_smiliessearch, 'translate_smiley', $content );
  1836. }
  1837. // did we exit ignore block
  1838. if ( '' != $ignore_block_element && '</' . $ignore_block_element . '>' == $content ) {
  1839. $ignore_block_element = '';
  1840. }
  1841. $output .= $content;
  1842. }
  1843. } else {
  1844. // return default text.
  1845. $output = $text;
  1846. }
  1847. return $output;
  1848. }
  1849. /**
  1850. * Verifies that an email is valid.
  1851. *
  1852. * Does not grok i18n domains. Not RFC compliant.
  1853. *
  1854. * @since 0.71
  1855. *
  1856. * @param string $email Email address to verify.
  1857. * @param boolean $deprecated Deprecated.
  1858. * @return string|bool Either false or the valid email address.
  1859. */
  1860. function is_email( $email, $deprecated = false ) {
  1861. if ( ! empty( $deprecated ) )
  1862. _deprecated_argument( __FUNCTION__, '3.0' );
  1863. // Test for the minimum length the email can be
  1864. if ( strlen( $email ) < 3 ) {
  1865. /**
  1866. * Filter whether an email address is valid.
  1867. *
  1868. * This filter is evaluated under several different contexts, such as 'email_too_short',
  1869. * 'email_no_at', 'local_invalid_chars', 'domain_period_sequence', 'domain_period_limits',
  1870. * 'domain_no_periods', 'sub_hyphen_limits', 'sub_invalid_chars', or no specific context.
  1871. *
  1872. * @since 2.8.0
  1873. *
  1874. * @param bool $is_email Whether the email address has passed the is_email() checks. Default false.
  1875. * @param string $email The email address being checked.
  1876. * @param string $message An explanatory message to the user.
  1877. * @param string $context Context under which the email was tested.
  1878. */
  1879. return apply_filters( 'is_email', false, $email, 'email_too_short' );
  1880. }
  1881. // Test for an @ character after the first position
  1882. if ( strpos( $email, '@', 1 ) === false ) {
  1883. /** This filter is documented in wp-includes/formatting.php */
  1884. return apply_filters( 'is_email', false, $email, 'email_no_at' );
  1885. }
  1886. // Split out the local and domain parts
  1887. list( $local, $domain ) = explode( '@', $email, 2 );
  1888. // LOCAL PART
  1889. // Test for invalid characters
  1890. if ( !preg_match( '/^[a-zA-Z0-9!#$%&\'*+\/=?^_`{|}~\.-]+$/', $local ) ) {
  1891. /** This filter is documented in wp-includes/formatting.php */
  1892. return apply_filters( 'is_email', false, $email, 'local_invalid_chars' );
  1893. }
  1894. // DOMAIN PART
  1895. // Test for sequences of periods
  1896. if ( preg_match( '/\.{2,}/', $domain ) ) {
  1897. /** This filter is documented in wp-includes/formatting.php */
  1898. return apply_filters( 'is_email', false, $email, 'domain_period_sequence' );
  1899. }
  1900. // Test for leading and trailing periods and whitespace
  1901. if ( trim( $domain, " \t\n\r\0\x0B." ) !== $domain ) {
  1902. /** This filter is documented in wp-includes/formatting.php */
  1903. return apply_filters( 'is_email', false, $email, 'domain_period_limits' );
  1904. }
  1905. // Split the domain into subs
  1906. $subs = explode( '.', $domain );
  1907. // Assume the domain will have at least two subs
  1908. if ( 2 > count( $subs ) ) {
  1909. /** This filter is documented in wp-includes/formatting.php */
  1910. return apply_filters( 'is_email', false, $email, 'domain_no_periods' );
  1911. }
  1912. // Loop through each sub
  1913. foreach ( $subs as $sub ) {
  1914. // Test for leading and trailing hyphens and whitespace
  1915. if ( trim( $sub, " \t\n\r\0\x0B-" ) !== $sub ) {
  1916. /** This filter is documented in wp-includes/formatting.php */
  1917. return apply_filters( 'is_email', false, $email, 'sub_hyphen_limits' );
  1918. }
  1919. // Test for invalid characters
  1920. if ( !preg_match('/^[a-z0-9-]+$/i', $sub ) ) {
  1921. /** This filter is documented in wp-includes/formatting.php */
  1922. return apply_filters( 'is_email', false, $email, 'sub_invalid_chars' );
  1923. }
  1924. }
  1925. // Congratulations your email made it!
  1926. /** This filter is documented in wp-includes/formatting.php */
  1927. return apply_filters( 'is_email', $email, $email, null );
  1928. }
  1929. /**
  1930. * Convert to ASCII from email subjects.
  1931. *
  1932. * @since 1.2.0
  1933. *
  1934. * @param string $string Subject line
  1935. * @return string Converted string to ASCII
  1936. */
  1937. function wp_iso_descrambler($string) {
  1938. /* this may only work with iso-8859-1, I'm afraid */
  1939. if (!preg_match('#\=\?(.+)\?Q\?(.+)\?\=#i', $string, $matches)) {
  1940. return $string;
  1941. } else {
  1942. $subject = str_replace('_', ' ', $matches[2]);
  1943. $subject = preg_replace_callback('#\=([0-9a-f]{2})#i', '_wp_iso_convert', $subject);
  1944. return $subject;
  1945. }
  1946. }
  1947. /**
  1948. * Helper function to convert hex encoded chars to ASCII
  1949. *
  1950. * @since 3.1.0
  1951. * @access private
  1952. *
  1953. * @param array $match The preg_replace_callback matches array
  1954. * @return array Converted chars
  1955. */
  1956. function _wp_iso_convert( $match ) {
  1957. return chr( hexdec( strtolower( $match[1] ) ) );
  1958. }
  1959. /**
  1960. * Returns a date in the GMT equivalent.
  1961. *
  1962. * Requires and returns a date in the Y-m-d H:i:s format. If there is a
  1963. * timezone_string available, the date is assumed to be in that timezone,
  1964. * otherwise it simply subtracts the value of the 'gmt_offset' option. Return
  1965. * format can be overridden using the $format parameter.
  1966. *
  1967. * @since 1.2.0
  1968. *
  1969. * @uses get_option() to retrieve the value of 'gmt_offset'.
  1970. * @param string $string The date to be converted.
  1971. * @param string $format The format string for the returned date (default is Y-m-d H:i:s)
  1972. * @return string GMT version of the date provided.
  1973. */
  1974. function get_gmt_from_date( $string, $format = 'Y-m-d H:i:s' ) {
  1975. $tz = get_option( 'timezone_string' );
  1976. if ( $tz ) {
  1977. $datetime = date_create( $string, new DateTimeZone( $tz ) );
  1978. if ( ! $datetime )
  1979. return gmdate( $format, 0 );
  1980. $datetime->setTimezone( new DateTimeZone( 'UTC' ) );
  1981. $string_gmt = $datetime->format( $format );
  1982. } else {
  1983. if ( ! preg_match( '#([0-9]{1,4})-([0-9]{1,2})-([0-9]{1,2}) ([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})#', $string, $matches ) )
  1984. return gmdate( $format, 0 );
  1985. $string_time = gmmktime( $matches[4], $matches[5], $matches[6], $matches[2], $matches[3], $matches[1] );
  1986. $string_gmt = gmdate( $format, $string_time - get_option( 'gmt_offset' ) * HOUR_IN_SECONDS );
  1987. }
  1988. return $string_gmt;
  1989. }
  1990. /**
  1991. * Converts a GMT date into the correct format for the blog.
  1992. *
  1993. * Requires and returns a date in the Y-m-d H:i:s format. If there is a
  1994. * timezone_string available, the returned date is in that timezone, otherwise
  1995. * it simply adds the value of gmt_offset. Return format can be overridden
  1996. * using the $format parameter
  1997. *
  1998. * @since 1.2.0
  1999. *
  2000. * @param string $string The date to be converted.
  2001. * @param string $format The format string for the returned date (default is Y-m-d H:i:s)
  2002. * @return string Formatted date relative to the timezone / GMT offset.
  2003. */
  2004. function get_date_from_gmt( $string, $format = 'Y-m-d H:i:s' ) {
  2005. $tz = get_option( 'timezone_string' );
  2006. if ( $tz ) {
  2007. $datetime = date_create( $string, new DateTimeZone( 'UTC' ) );
  2008. if ( ! $datetime )
  2009. return date( $format, 0 );
  2010. $datetime->setTimezone( new DateTimeZone( $tz ) );
  2011. $string_localtime = $datetime->format( $format );
  2012. } else {
  2013. if ( ! preg_match('#([0-9]{1,4})-([0-9]{1,2})-([0-9]{1,2}) ([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})#', $string, $matches) )
  2014. return date( $format, 0 );
  2015. $string_time = gmmktime( $matches[4], $matches[5], $matches[6], $matches[2], $matches[3], $matches[1] );
  2016. $string_localtime = gmdate( $format, $string_time + get_option( 'gmt_offset' ) * HOUR_IN_SECONDS );
  2017. }
  2018. return $string_localtime;
  2019. }
  2020. /**
  2021. * Computes an offset in seconds from an iso8601 timezone.
  2022. *
  2023. * @since 1.5.0
  2024. *
  2025. * @param string $timezone Either 'Z' for 0 offset or 'Âąhhmm'.
  2026. * @return int|float The offset in seconds.
  2027. */
  2028. function iso8601_timezone_to_offset($timezone) {
  2029. // $timezone is either 'Z' or '[+|-]hhmm'
  2030. if ($timezone == 'Z') {
  2031. $offset = 0;
  2032. } else {
  2033. $sign = (substr($timezone, 0, 1) == '+') ? 1 : -1;
  2034. $hours = intval(substr($timezone, 1, 2));
  2035. $minutes = intval(substr($timezone, 3, 4)) / 60;
  2036. $offset = $sign * HOUR_IN_SECONDS * ($hours + $minutes);
  2037. }
  2038. return $offset;
  2039. }
  2040. /**
  2041. * Converts an iso8601 date to MySQL DateTime format used by post_date[_gmt].
  2042. *
  2043. * @since 1.5.0
  2044. *
  2045. * @param string $date_string Date and time in ISO 8601 format {@link http://en.wikipedia.org/wiki/ISO_8601}.
  2046. * @param string $timezone Optional. If set to GMT returns the time minus gmt_offset. Default is 'user'.
  2047. * @return string The date and time in MySQL DateTime format - Y-m-d H:i:s.
  2048. */
  2049. function iso8601_to_datetime($date_string, $timezone = 'user') {
  2050. $timezone = strtolower($timezone);
  2051. if ($timezone == 'gmt') {
  2052. preg_match('#([0-9]{4})([0-9]{2})([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})(Z|[\+|\-][0-9]{2,4}){0,1}#', $date_string, $date_bits);
  2053. if (!empty($date_bits[7])) { // we have a timezone, so let's compute an offset
  2054. $offset = iso8601_timezone_to_offset($date_bits[7]);
  2055. } else { // we don't have a timezone, so we assume user local timezone (not server's!)
  2056. $offset = HOUR_IN_SECONDS * get_option('gmt_offset');
  2057. }
  2058. $timestamp = gmmktime($date_bits[4], $date_bits[5], $date_bits[6], $date_bits[2], $date_bits[3], $date_bits[1]);
  2059. $timestamp -= $offset;
  2060. return gmdate('Y-m-d H:i:s', $timestamp);
  2061. } else if ($timezone == 'user') {
  2062. return preg_replace('#([0-9]{4})([0-9]{2})([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})(Z|[\+|\-][0-9]{2,4}){0,1}#', '$1-$2-$3 $4:$5:$6', $date_string);
  2063. }
  2064. }
  2065. /**
  2066. * Adds a element attributes to open links in new windows.
  2067. *
  2068. * Comment text in popup windows should be filtered through this. Right now it's
  2069. * a moderately dumb function, ideally it would detect whether a target or rel
  2070. * attribute was already there and adjust its actions accordingly.
  2071. *
  2072. * @since 0.71
  2073. *
  2074. * @param string $text Content to replace links to open in a new window.
  2075. * @return string Content that has filtered links.
  2076. */
  2077. function popuplinks($text) {
  2078. $text = preg_replace('/<a (.+?)>/i', "<a $1 target='_blank' rel='external'>", $text);
  2079. return $text;
  2080. }
  2081. /**
  2082. * Strips out all characters that are not allowable in an email.
  2083. *
  2084. * @since 1.5.0
  2085. *
  2086. * @param string $email Email address to filter.
  2087. * @return string Filtered email address.
  2088. */
  2089. function sanitize_email( $email ) {
  2090. // Test for the minimum length the email can be
  2091. if ( strlen( $email ) < 3 ) {
  2092. /**
  2093. * Filter a sanitized email address.
  2094. *
  2095. * This filter is evaluated under several contexts, including 'email_too_short',
  2096. * 'email_no_at', 'local_invalid_chars', 'domain_period_sequence', 'domain_period_limits',
  2097. * 'domain_no_periods', 'domain_no_valid_subs', or no context.
  2098. *
  2099. * @since 2.8.0
  2100. *
  2101. * @param string $email The sanitized email address.
  2102. * @param string $email The email address, as provided to sanitize_email().
  2103. * @param string $message A message to pass to the user.
  2104. */
  2105. return apply_filters( 'sanitize_email', '', $email, 'email_too_short' );
  2106. }
  2107. // Test for an @ character after the first position
  2108. if ( strpos( $email, '@', 1 ) === false ) {
  2109. /** This filter is documented in wp-includes/formatting.php */
  2110. return apply_filters( 'sanitize_email', '', $email, 'email_no_at' );
  2111. }
  2112. // Split out the local and domain parts
  2113. list( $local, $domain ) = explode( '@', $email, 2 );
  2114. // LOCAL PART
  2115. // Test for invalid characters
  2116. $local = preg_replace( '/[^a-zA-Z0-9!#$%&\'*+\/=?^_`{|}~\.-]/', '', $local );
  2117. if ( '' === $local ) {
  2118. /** This filter is documented in wp-includes/formatting.php */
  2119. return apply_filters( 'sanitize_email', '', $email, 'local_invalid_chars' );
  2120. }
  2121. // DOMAIN PART
  2122. // Test for sequences of periods
  2123. $domain = preg_replace( '/\.{2,}/', '', $domain );
  2124. if ( '' === $domain ) {
  2125. /** This filter is documented in wp-includes/formatting.php */
  2126. return apply_filters( 'sanitize_email', '', $email, 'domain_period_sequence' );
  2127. }
  2128. // Test for leading and trailing periods and whitespace
  2129. $domain = trim( $domain, " \t\n\r\0\x0B." );
  2130. if ( '' === $domain ) {
  2131. /** This filter is documented in wp-includes/formatting.php */
  2132. return apply_filters( 'sanitize_email', '', $email, 'domain_period_limits' );
  2133. }
  2134. // Split the domain into subs
  2135. $subs = explode( '.', $domain );
  2136. // Assume the domain will have at least two subs
  2137. if ( 2 > count( $subs ) ) {
  2138. /** This filter is documented in wp-includes/formatting.php */
  2139. return apply_filters( 'sanitize_email', '', $email, 'domain_no_periods' );
  2140. }
  2141. // Create an array that will contain valid subs
  2142. $new_subs = array();
  2143. // Loop through each sub
  2144. foreach ( $subs as $sub ) {
  2145. // Test for leading and trailing hyphens
  2146. $sub = trim( $sub, " \t\n\r\0\x0B-" );
  2147. // Test for invalid characters
  2148. $sub = preg_replace( '/[^a-z0-9-]+/i', '', $sub );
  2149. // If there's anything left, add it to the valid subs
  2150. if ( '' !== $sub ) {
  2151. $new_subs[] = $sub;
  2152. }
  2153. }
  2154. // If there aren't 2 or more valid subs
  2155. if ( 2 > count( $new_subs ) ) {
  2156. /** This filter is documented in wp-includes/formatting.php */
  2157. return apply_filters( 'sanitize_email', '', $email, 'domain_no_valid_subs' );
  2158. }
  2159. // Join valid subs into the new domain
  2160. $domain = join( '.', $new_subs );
  2161. // Put the email back together
  2162. $email = $local . '@' . $domain;
  2163. // Congratulations your email made it!
  2164. /** This filter is documented in wp-includes/formatting.php */
  2165. return apply_filters( 'sanitize_email', $email, $email, null );
  2166. }
  2167. /**
  2168. * Determines the difference between two timestamps.
  2169. *
  2170. * The difference is returned in a human readable format such as "1 hour",
  2171. * "5 mins", "2 days".
  2172. *
  2173. * @since 1.5.0
  2174. *
  2175. * @param int $from Unix timestamp from which the difference begins.
  2176. * @param int $to Optional. Unix timestamp to end the time difference. Default becomes time() if not set.
  2177. * @return string Human readable time difference.
  2178. */
  2179. function human_time_diff( $from, $to = '' ) {
  2180. if ( empty( $to ) ) {
  2181. $to = time();
  2182. }
  2183. $diff = (int) abs( $to - $from );
  2184. if ( $diff < HOUR_IN_SECONDS ) {
  2185. $mins = round( $diff / MINUTE_IN_SECONDS );
  2186. if ( $mins <= 1 )
  2187. $mins = 1;
  2188. /* translators: min=minute */
  2189. $since = sprintf( _n( '%s min', '%s mins', $mins ), $mins );
  2190. } elseif ( $diff < DAY_IN_SECONDS && $diff >= HOUR_IN_SECONDS ) {
  2191. $hours = round( $diff / HOUR_IN_SECONDS );
  2192. if ( $hours <= 1 )
  2193. $hours = 1;
  2194. $since = sprintf( _n( '%s hour', '%s hours', $hours ), $hours );
  2195. } elseif ( $diff < WEEK_IN_SECONDS && $diff >= DAY_IN_SECONDS ) {
  2196. $days = round( $diff / DAY_IN_SECONDS );
  2197. if ( $days <= 1 )
  2198. $days = 1;
  2199. $since = sprintf( _n( '%s day', '%s days', $days ), $days );
  2200. } elseif ( $diff < 30 * DAY_IN_SECONDS && $diff >= WEEK_IN_SECONDS ) {
  2201. $weeks = round( $diff / WEEK_IN_SECONDS );
  2202. if ( $weeks <= 1 )
  2203. $weeks = 1;
  2204. $since = sprintf( _n( '%s week', '%s weeks', $weeks ), $weeks );
  2205. } elseif ( $diff < YEAR_IN_SECONDS && $diff >= 30 * DAY_IN_SECONDS ) {
  2206. $months = round( $diff / ( 30 * DAY_IN_SECONDS ) );
  2207. if ( $months <= 1 )
  2208. $months = 1;
  2209. $since = sprintf( _n( '%s month', '%s months', $months ), $months );
  2210. } elseif ( $diff >= YEAR_IN_SECONDS ) {
  2211. $years = round( $diff / YEAR_IN_SECONDS );
  2212. if ( $years <= 1 )
  2213. $years = 1;
  2214. $since = sprintf( _n( '%s year', '%s years', $years ), $years );
  2215. }
  2216. /**
  2217. * Filter the human readable difference between two timestamps.
  2218. *
  2219. * @since 4.0.0
  2220. *
  2221. * @param string $since The difference in human readable text.
  2222. * @param int $diff The difference in seconds.
  2223. * @param int $from Unix timestamp from which the difference begins.
  2224. * @param int $to Unix timestamp to end the time difference.
  2225. */
  2226. return apply_filters( 'human_time_diff', $since, $diff, $from, $to );
  2227. }
  2228. /**
  2229. * Generates an excerpt from the content, if needed.
  2230. *
  2231. * The excerpt word amount will be 55 words and if the amount is greater than
  2232. * that, then the string ' [&hellip;]' will be appended to the excerpt. If the string
  2233. * is less than 55 words, then the content will be returned as is.
  2234. *
  2235. * The 55 word limit can be modified by plugins/themes using the excerpt_length filter
  2236. * The ' [&hellip;]' string can be modified by plugins/themes using the excerpt_more filter
  2237. *
  2238. * @since 1.5.0
  2239. *
  2240. * @param string $text Optional. The excerpt. If set to empty, an excerpt is generated.
  2241. * @return string The excerpt.
  2242. */
  2243. function wp_trim_excerpt($text = '') {
  2244. $raw_excerpt = $text;
  2245. if ( '' == $text ) {
  2246. $text = get_the_content('');
  2247. $text = strip_shortcodes( $text );
  2248. /** This filter is documented in wp-includes/post-template.php */
  2249. $text = apply_filters( 'the_content', $text );
  2250. $text = str_replace(']]>', ']]&gt;', $text);
  2251. /**
  2252. * Filter the number of words in an excerpt.
  2253. *
  2254. * @since 2.7.0
  2255. *
  2256. * @param int $number The number of words. Default 55.
  2257. */
  2258. $excerpt_length = apply_filters( 'excerpt_length', 55 );
  2259. /**
  2260. * Filter the string in the "more" link displayed after a trimmed excerpt.
  2261. *
  2262. * @since 2.9.0
  2263. *
  2264. * @param string $more_string The string shown within the more link.
  2265. */
  2266. $excerpt_more = apply_filters( 'excerpt_more', ' ' . '[&hellip;]' );
  2267. $text = wp_trim_words( $text, $excerpt_length, $excerpt_more );
  2268. }
  2269. /**
  2270. * Filter the trimmed excerpt string.
  2271. *
  2272. * @since 2.8.0
  2273. *
  2274. * @param string $text The trimmed text.
  2275. * @param string $raw_excerpt The text prior to trimming.
  2276. */
  2277. return apply_filters( 'wp_trim_excerpt', $text, $raw_excerpt );
  2278. }
  2279. /**
  2280. * Trims text to a certain number of words.
  2281. *
  2282. * This function is localized. For languages that count 'words' by the individual
  2283. * character (such as East Asian languages), the $num_words argument will apply
  2284. * to the number of individual characters.
  2285. *
  2286. * @since 3.3.0
  2287. *
  2288. * @param string $text Text to trim.
  2289. * @param int $num_words Number of words. Default 55.
  2290. * @param string $more Optional. What to append if $text needs to be trimmed. Default '&hellip;'.
  2291. * @return string Trimmed text.
  2292. */
  2293. function wp_trim_words( $text, $num_words = 55, $more = null ) {
  2294. if ( null === $more )
  2295. $more = __( '&hellip;' );
  2296. $original_text = $text;
  2297. $text = wp_strip_all_tags( $text );
  2298. /* translators: If your word count is based on single characters (East Asian characters),
  2299. enter 'characters'. Otherwise, enter 'words'. Do not translate into your own language. */
  2300. if ( 'characters' == _x( 'words', 'word count: words or characters?' ) && preg_match( '/^utf\-?8$/i', get_option( 'blog_charset' ) ) ) {
  2301. $text = trim( preg_replace( "/[\n\r\t ]+/", ' ', $text ), ' ' );
  2302. preg_match_all( '/./u', $text, $words_array );
  2303. $words_array = array_slice( $words_array[0], 0, $num_words + 1 );
  2304. $sep = '';
  2305. } else {
  2306. $words_array = preg_split( "/[\n\r\t ]+/", $text, $num_words + 1, PREG_SPLIT_NO_EMPTY );
  2307. $sep = ' ';
  2308. }
  2309. if ( count( $words_array ) > $num_words ) {
  2310. array_pop( $words_array );
  2311. $text = implode( $sep, $words_array );
  2312. $text = $text . $more;
  2313. } else {
  2314. $text = implode( $sep, $words_array );
  2315. }
  2316. /**
  2317. * Filter the text content after words have been trimmed.
  2318. *
  2319. * @since 3.3.0
  2320. *
  2321. * @param string $text The trimmed text.
  2322. * @param int $num_words The number of words to trim the text to. Default 5.
  2323. * @param string $more An optional string to append to the end of the trimmed text, e.g. &hellip;.
  2324. * @param string $original_text The text before it was trimmed.
  2325. */
  2326. return apply_filters( 'wp_trim_words', $text, $num_words, $more, $original_text );
  2327. }
  2328. /**
  2329. * Converts named entities into numbered entities.
  2330. *
  2331. * @since 1.5.1
  2332. *
  2333. * @param string $text The text within which entities will be converted.
  2334. * @return string Text with converted entities.
  2335. */
  2336. function ent2ncr($text) {
  2337. /**
  2338. * Filter text before named entities are converted into numbered entities.
  2339. *
  2340. * A non-null string must be returned for the filter to be evaluated.
  2341. *
  2342. * @since 3.3.0
  2343. *
  2344. * @param null $converted_text The text to be converted. Default null.
  2345. * @param string $text The text prior to entity conversion.
  2346. */
  2347. $filtered = apply_filters( 'pre_ent2ncr', null, $text );
  2348. if( null !== $filtered )
  2349. return $filtered;
  2350. $to_ncr = array(
  2351. '&quot;' => '&#34;',
  2352. '&amp;' => '&#38;',
  2353. '&lt;' => '&#60;',
  2354. '&gt;' => '&#62;',
  2355. '|' => '&#124;',
  2356. '&nbsp;' => '&#160;',
  2357. '&iexcl;' => '&#161;',
  2358. '&cent;' => '&#162;',
  2359. '&pound;' => '&#163;',
  2360. '&curren;' => '&#164;',
  2361. '&yen;' => '&#165;',
  2362. '&brvbar;' => '&#166;',
  2363. '&brkbar;' => '&#166;',
  2364. '&sect;' => '&#167;',
  2365. '&uml;' => '&#168;',
  2366. '&die;' => '&#168;',
  2367. '&copy;' => '&#169;',
  2368. '&ordf;' => '&#170;',
  2369. '&laquo;' => '&#171;',
  2370. '&not;' => '&#172;',
  2371. '&shy;' => '&#173;',
  2372. '&reg;' => '&#174;',
  2373. '&macr;' => '&#175;',
  2374. '&hibar;' => '&#175;',
  2375. '&deg;' => '&#176;',
  2376. '&plusmn;' => '&#177;',
  2377. '&sup2;' => '&#178;',
  2378. '&sup3;' => '&#179;',
  2379. '&acute;' => '&#180;',
  2380. '&micro;' => '&#181;',
  2381. '&para;' => '&#182;',
  2382. '&middot;' => '&#183;',
  2383. '&cedil;' => '&#184;',
  2384. '&sup1;' => '&#185;',
  2385. '&ordm;' => '&#186;',
  2386. '&raquo;' => '&#187;',
  2387. '&frac14;' => '&#188;',
  2388. '&frac12;' => '&#189;',
  2389. '&frac34;' => '&#190;',
  2390. '&iquest;' => '&#191;',
  2391. '&Agrave;' => '&#192;',
  2392. '&Aacute;' => '&#193;',
  2393. '&Acirc;' => '&#194;',
  2394. '&Atilde;' => '&#195;',
  2395. '&Auml;' => '&#196;',
  2396. '&Aring;' => '&#197;',
  2397. '&AElig;' => '&#198;',
  2398. '&Ccedil;' => '&#199;',
  2399. '&Egrave;' => '&#200;',
  2400. '&Eacute;' => '&#201;',
  2401. '&Ecirc;' => '&#202;',
  2402. '&Euml;' => '&#203;',
  2403. '&Igrave;' => '&#204;',
  2404. '&Iacute;' => '&#205;',
  2405. '&Icirc;' => '&#206;',
  2406. '&Iuml;' => '&#207;',
  2407. '&ETH;' => '&#208;',
  2408. '&Ntilde;' => '&#209;',
  2409. '&Ograve;' => '&#210;',
  2410. '&Oacute;' => '&#211;',
  2411. '&Ocirc;' => '&#212;',
  2412. '&Otilde;' => '&#213;',
  2413. '&Ouml;' => '&#214;',
  2414. '&times;' => '&#215;',
  2415. '&Oslash;' => '&#216;',
  2416. '&Ugrave;' => '&#217;',
  2417. '&Uacute;' => '&#218;',
  2418. '&Ucirc;' => '&#219;',
  2419. '&Uuml;' => '&#220;',
  2420. '&Yacute;' => '&#221;',
  2421. '&THORN;' => '&#222;',
  2422. '&szlig;' => '&#223;',
  2423. '&agrave;' => '&#224;',
  2424. '&aacute;' => '&#225;',
  2425. '&acirc;' => '&#226;',
  2426. '&atilde;' => '&#227;',
  2427. '&auml;' => '&#228;',
  2428. '&aring;' => '&#229;',
  2429. '&aelig;' => '&#230;',
  2430. '&ccedil;' => '&#231;',
  2431. '&egrave;' => '&#232;',
  2432. '&eacute;' => '&#233;',
  2433. '&ecirc;' => '&#234;',
  2434. '&euml;' => '&#235;',
  2435. '&igrave;' => '&#236;',
  2436. '&iacute;' => '&#237;',
  2437. '&icirc;' => '&#238;',
  2438. '&iuml;' => '&#239;',
  2439. '&eth;' => '&#240;',
  2440. '&ntilde;' => '&#241;',
  2441. '&ograve;' => '&#242;',
  2442. '&oacute;' => '&#243;',
  2443. '&ocirc;' => '&#244;',
  2444. '&otilde;' => '&#245;',
  2445. '&ouml;' => '&#246;',
  2446. '&divide;' => '&#247;',
  2447. '&oslash;' => '&#248;',
  2448. '&ugrave;' => '&#249;',
  2449. '&uacute;' => '&#250;',
  2450. '&ucirc;' => '&#251;',
  2451. '&uuml;' => '&#252;',
  2452. '&yacute;' => '&#253;',
  2453. '&thorn;' => '&#254;',
  2454. '&yuml;' => '&#255;',
  2455. '&OElig;' => '&#338;',
  2456. '&oelig;' => '&#339;',
  2457. '&Scaron;' => '&#352;',
  2458. '&scaron;' => '&#353;',
  2459. '&Yuml;' => '&#376;',
  2460. '&fnof;' => '&#402;',
  2461. '&circ;' => '&#710;',
  2462. '&tilde;' => '&#732;',
  2463. '&Alpha;' => '&#913;',
  2464. '&Beta;' => '&#914;',
  2465. '&Gamma;' => '&#915;',
  2466. '&Delta;' => '&#916;',
  2467. '&Epsilon;' => '&#917;',
  2468. '&Zeta;' => '&#918;',
  2469. '&Eta;' => '&#919;',
  2470. '&Theta;' => '&#920;',
  2471. '&Iota;' => '&#921;',
  2472. '&Kappa;' => '&#922;',
  2473. '&Lambda;' => '&#923;',
  2474. '&Mu;' => '&#924;',
  2475. '&Nu;' => '&#925;',
  2476. '&Xi;' => '&#926;',
  2477. '&Omicron;' => '&#927;',
  2478. '&Pi;' => '&#928;',
  2479. '&Rho;' => '&#929;',
  2480. '&Sigma;' => '&#931;',
  2481. '&Tau;' => '&#932;',
  2482. '&Upsilon;' => '&#933;',
  2483. '&Phi;' => '&#934;',
  2484. '&Chi;' => '&#935;',
  2485. '&Psi;' => '&#936;',
  2486. '&Omega;' => '&#937;',
  2487. '&alpha;' => '&#945;',
  2488. '&beta;' => '&#946;',
  2489. '&gamma;' => '&#947;',
  2490. '&delta;' => '&#948;',
  2491. '&epsilon;' => '&#949;',
  2492. '&zeta;' => '&#950;',
  2493. '&eta;' => '&#951;',
  2494. '&theta;' => '&#952;',
  2495. '&iota;' => '&#953;',
  2496. '&kappa;' => '&#954;',
  2497. '&lambda;' => '&#955;',
  2498. '&mu;' => '&#956;',
  2499. '&nu;' => '&#957;',
  2500. '&xi;' => '&#958;',
  2501. '&omicron;' => '&#959;',
  2502. '&pi;' => '&#960;',
  2503. '&rho;' => '&#961;',
  2504. '&sigmaf;' => '&#962;',
  2505. '&sigma;' => '&#963;',
  2506. '&tau;' => '&#964;',
  2507. '&upsilon;' => '&#965;',
  2508. '&phi;' => '&#966;',
  2509. '&chi;' => '&#967;',
  2510. '&psi;' => '&#968;',
  2511. '&omega;' => '&#969;',
  2512. '&thetasym;' => '&#977;',
  2513. '&upsih;' => '&#978;',
  2514. '&piv;' => '&#982;',
  2515. '&ensp;' => '&#8194;',
  2516. '&emsp;' => '&#8195;',
  2517. '&thinsp;' => '&#8201;',
  2518. '&zwnj;' => '&#8204;',
  2519. '&zwj;' => '&#8205;',
  2520. '&lrm;' => '&#8206;',
  2521. '&rlm;' => '&#8207;',
  2522. '&ndash;' => '&#8211;',
  2523. '&mdash;' => '&#8212;',
  2524. '&lsquo;' => '&#8216;',
  2525. '&rsquo;' => '&#8217;',
  2526. '&sbquo;' => '&#8218;',
  2527. '&ldquo;' => '&#8220;',
  2528. '&rdquo;' => '&#8221;',
  2529. '&bdquo;' => '&#8222;',
  2530. '&dagger;' => '&#8224;',
  2531. '&Dagger;' => '&#8225;',
  2532. '&bull;' => '&#8226;',
  2533. '&hellip;' => '&#8230;',
  2534. '&permil;' => '&#8240;',
  2535. '&prime;' => '&#8242;',
  2536. '&Prime;' => '&#8243;',
  2537. '&lsaquo;' => '&#8249;',
  2538. '&rsaquo;' => '&#8250;',
  2539. '&oline;' => '&#8254;',
  2540. '&frasl;' => '&#8260;',
  2541. '&euro;' => '&#8364;',
  2542. '&image;' => '&#8465;',
  2543. '&weierp;' => '&#8472;',
  2544. '&real;' => '&#8476;',
  2545. '&trade;' => '&#8482;',
  2546. '&alefsym;' => '&#8501;',
  2547. '&crarr;' => '&#8629;',
  2548. '&lArr;' => '&#8656;',
  2549. '&uArr;' => '&#8657;',
  2550. '&rArr;' => '&#8658;',
  2551. '&dArr;' => '&#8659;',
  2552. '&hArr;' => '&#8660;',
  2553. '&forall;' => '&#8704;',
  2554. '&part;' => '&#8706;',
  2555. '&exist;' => '&#8707;',
  2556. '&empty;' => '&#8709;',
  2557. '&nabla;' => '&#8711;',
  2558. '&isin;' => '&#8712;',
  2559. '&notin;' => '&#8713;',
  2560. '&ni;' => '&#8715;',
  2561. '&prod;' => '&#8719;',
  2562. '&sum;' => '&#8721;',
  2563. '&minus;' => '&#8722;',
  2564. '&lowast;' => '&#8727;',
  2565. '&radic;' => '&#8730;',
  2566. '&prop;' => '&#8733;',
  2567. '&infin;' => '&#8734;',
  2568. '&ang;' => '&#8736;',
  2569. '&and;' => '&#8743;',
  2570. '&or;' => '&#8744;',
  2571. '&cap;' => '&#8745;',
  2572. '&cup;' => '&#8746;',
  2573. '&int;' => '&#8747;',
  2574. '&there4;' => '&#8756;',
  2575. '&sim;' => '&#8764;',
  2576. '&cong;' => '&#8773;',
  2577. '&asymp;' => '&#8776;',
  2578. '&ne;' => '&#8800;',
  2579. '&equiv;' => '&#8801;',
  2580. '&le;' => '&#8804;',
  2581. '&ge;' => '&#8805;',
  2582. '&sub;' => '&#8834;',
  2583. '&sup;' => '&#8835;',
  2584. '&nsub;' => '&#8836;',
  2585. '&sube;' => '&#8838;',
  2586. '&supe;' => '&#8839;',
  2587. '&oplus;' => '&#8853;',
  2588. '&otimes;' => '&#8855;',
  2589. '&perp;' => '&#8869;',
  2590. '&sdot;' => '&#8901;',
  2591. '&lceil;' => '&#8968;',
  2592. '&rceil;' => '&#8969;',
  2593. '&lfloor;' => '&#8970;',
  2594. '&rfloor;' => '&#8971;',
  2595. '&lang;' => '&#9001;',
  2596. '&rang;' => '&#9002;',
  2597. '&larr;' => '&#8592;',
  2598. '&uarr;' => '&#8593;',
  2599. '&rarr;' => '&#8594;',
  2600. '&darr;' => '&#8595;',
  2601. '&harr;' => '&#8596;',
  2602. '&loz;' => '&#9674;',
  2603. '&spades;' => '&#9824;',
  2604. '&clubs;' => '&#9827;',
  2605. '&hearts;' => '&#9829;',
  2606. '&diams;' => '&#9830;'
  2607. );
  2608. return str_replace( array_keys($to_ncr), array_values($to_ncr), $text );
  2609. }
  2610. /**
  2611. * Formats text for the rich text editor.
  2612. *
  2613. * The filter 'richedit_pre' is applied here. If $text is empty the filter will
  2614. * be applied to an empty string.
  2615. *
  2616. * @since 2.0.0
  2617. *
  2618. * @param string $text The text to be formatted.
  2619. * @return string The formatted text after filter is applied.
  2620. */
  2621. function wp_richedit_pre($text) {
  2622. if ( empty( $text ) ) {
  2623. /**
  2624. * Filter text returned for the rich text editor.
  2625. *
  2626. * This filter is first evaluated, and the value returned, if an empty string
  2627. * is passed to wp_richedit_pre(). If an empty string is passed, it results
  2628. * in a break tag and line feed.
  2629. *
  2630. * If a non-empty string is passed, the filter is evaluated on the wp_richedit_pre()
  2631. * return after being formatted.
  2632. *
  2633. * @since 2.0.0
  2634. *
  2635. * @param string $output Text for the rich text editor.
  2636. */
  2637. return apply_filters( 'richedit_pre', '' );
  2638. }
  2639. $output = convert_chars($text);
  2640. $output = wpautop($output);
  2641. $output = htmlspecialchars($output, ENT_NOQUOTES, get_option( 'blog_charset' ) );
  2642. /** This filter is documented in wp-includes/formatting.php */
  2643. return apply_filters( 'richedit_pre', $output );
  2644. }
  2645. /**
  2646. * Formats text for the HTML editor.
  2647. *
  2648. * Unless $output is empty it will pass through htmlspecialchars before the
  2649. * 'htmledit_pre' filter is applied.
  2650. *
  2651. * @since 2.5.0
  2652. *
  2653. * @param string $output The text to be formatted.
  2654. * @return string Formatted text after filter applied.
  2655. */
  2656. function wp_htmledit_pre($output) {
  2657. if ( !empty($output) )
  2658. $output = htmlspecialchars($output, ENT_NOQUOTES, get_option( 'blog_charset' ) ); // convert only < > &
  2659. /**
  2660. * Filter the text before it is formatted for the HTML editor.
  2661. *
  2662. * @since 2.5.0
  2663. *
  2664. * @param string $output The HTML-formatted text.
  2665. */
  2666. return apply_filters( 'htmledit_pre', $output );
  2667. }
  2668. /**
  2669. * Perform a deep string replace operation to ensure the values in $search are no longer present
  2670. *
  2671. * Repeats the replacement operation until it no longer replaces anything so as to remove "nested" values
  2672. * e.g. $subject = '%0%0%0DDD', $search ='%0D', $result ='' rather than the '%0%0DD' that
  2673. * str_replace would return
  2674. *
  2675. * @since 2.8.1
  2676. * @access private
  2677. *
  2678. * @param string|array $search The value being searched for, otherwise known as the needle. An array may be used to designate multiple needles.
  2679. * @param string $subject The string being searched and replaced on, otherwise known as the haystack.
  2680. * @return string The string with the replaced svalues.
  2681. */
  2682. function _deep_replace( $search, $subject ) {
  2683. $subject = (string) $subject;
  2684. $count = 1;
  2685. while ( $count ) {
  2686. $subject = str_replace( $search, '', $subject, $count );
  2687. }
  2688. return $subject;
  2689. }
  2690. /**
  2691. * Escapes data for use in a MySQL query.
  2692. *
  2693. * Usually you should prepare queries using wpdb::prepare().
  2694. * Sometimes, spot-escaping is required or useful. One example
  2695. * is preparing an array for use in an IN clause.
  2696. *
  2697. * @since 2.8.0
  2698. * @param string|array $data Unescaped data
  2699. * @return string|array Escaped data
  2700. */
  2701. function esc_sql( $data ) {
  2702. global $wpdb;
  2703. return $wpdb->_escape( $data );
  2704. }
  2705. /**
  2706. * Checks and cleans a URL.
  2707. *
  2708. * A number of characters are removed from the URL. If the URL is for displaying
  2709. * (the default behaviour) ampersands are also replaced. The 'clean_url' filter
  2710. * is applied to the returned cleaned URL.
  2711. *
  2712. * @since 2.8.0
  2713. * @uses wp_kses_bad_protocol() To only permit protocols in the URL set
  2714. * via $protocols or the common ones set in the function.
  2715. *
  2716. * @param string $url The URL to be cleaned.
  2717. * @param array $protocols Optional. An array of acceptable protocols.
  2718. * Defaults to 'http', 'https', 'ftp', 'ftps', 'mailto', 'news', 'irc', 'gopher', 'nntp', 'feed', 'telnet', 'mms', 'rtsp', 'svn' if not set.
  2719. * @param string $_context Private. Use esc_url_raw() for database usage.
  2720. * @return string The cleaned $url after the 'clean_url' filter is applied.
  2721. */
  2722. function esc_url( $url, $protocols = null, $_context = 'display' ) {
  2723. $original_url = $url;
  2724. if ( '' == $url )
  2725. return $url;
  2726. $url = preg_replace('|[^a-z0-9-~+_.?#=!&;,/:%@$\|*\'()\\x80-\\xff]|i', '', $url);
  2727. $strip = array('%0d', '%0a', '%0D', '%0A');
  2728. $url = _deep_replace($strip, $url);
  2729. $url = str_replace(';//', '://', $url);
  2730. /* If the URL doesn't appear to contain a scheme, we
  2731. * presume it needs http:// appended (unless a relative
  2732. * link starting with /, # or ? or a php file).
  2733. */
  2734. if ( strpos($url, ':') === false && ! in_array( $url[0], array( '/', '#', '?' ) ) &&
  2735. ! preg_match('/^[a-z0-9-]+?\.php/i', $url) )
  2736. $url = 'http://' . $url;
  2737. // Replace ampersands and single quotes only when displaying.
  2738. if ( 'display' == $_context ) {
  2739. $url = wp_kses_normalize_entities( $url );
  2740. $url = str_replace( '&amp;', '&#038;', $url );
  2741. $url = str_replace( "'", '&#039;', $url );
  2742. }
  2743. if ( '/' === $url[0] ) {
  2744. $good_protocol_url = $url;
  2745. } else {
  2746. if ( ! is_array( $protocols ) )
  2747. $protocols = wp_allowed_protocols();
  2748. $good_protocol_url = wp_kses_bad_protocol( $url, $protocols );
  2749. if ( strtolower( $good_protocol_url ) != strtolower( $url ) )
  2750. return '';
  2751. }
  2752. /**
  2753. * Filter a string cleaned and escaped for output as a URL.
  2754. *
  2755. * @since 2.3.0
  2756. *
  2757. * @param string $good_protocol_url The cleaned URL to be returned.
  2758. * @param string $original_url The URL prior to cleaning.
  2759. * @param string $_context If 'display', replace ampersands and single quotes only.
  2760. */
  2761. return apply_filters( 'clean_url', $good_protocol_url, $original_url, $_context );
  2762. }
  2763. /**
  2764. * Performs esc_url() for database usage.
  2765. *
  2766. * @since 2.8.0
  2767. * @uses esc_url()
  2768. *
  2769. * @param string $url The URL to be cleaned.
  2770. * @param array $protocols An array of acceptable protocols.
  2771. * @return string The cleaned URL.
  2772. */
  2773. function esc_url_raw( $url, $protocols = null ) {
  2774. return esc_url( $url, $protocols, 'db' );
  2775. }
  2776. /**
  2777. * Convert entities, while preserving already-encoded entities.
  2778. *
  2779. * @link http://www.php.net/htmlentities Borrowed from the PHP Manual user notes.
  2780. *
  2781. * @since 1.2.2
  2782. *
  2783. * @param string $myHTML The text to be converted.
  2784. * @return string Converted text.
  2785. */
  2786. function htmlentities2($myHTML) {
  2787. $translation_table = get_html_translation_table( HTML_ENTITIES, ENT_QUOTES );
  2788. $translation_table[chr(38)] = '&';
  2789. return preg_replace( "/&(?![A-Za-z]{0,4}\w{2,3};|#[0-9]{2,3};)/", "&amp;", strtr($myHTML, $translation_table) );
  2790. }
  2791. /**
  2792. * Escape single quotes, htmlspecialchar " < > &, and fix line endings.
  2793. *
  2794. * Escapes text strings for echoing in JS. It is intended to be used for inline JS
  2795. * (in a tag attribute, for example onclick="..."). Note that the strings have to
  2796. * be in single quotes. The filter 'js_escape' is also applied here.
  2797. *
  2798. * @since 2.8.0
  2799. *
  2800. * @param string $text The text to be escaped.
  2801. * @return string Escaped text.
  2802. */
  2803. function esc_js( $text ) {
  2804. $safe_text = wp_check_invalid_utf8( $text );
  2805. $safe_text = _wp_specialchars( $safe_text, ENT_COMPAT );
  2806. $safe_text = preg_replace( '/&#(x)?0*(?(1)27|39);?/i', "'", stripslashes( $safe_text ) );
  2807. $safe_text = str_replace( "\r", '', $safe_text );
  2808. $safe_text = str_replace( "\n", '\\n', addslashes( $safe_text ) );
  2809. /**
  2810. * Filter a string cleaned and escaped for output in JavaScript.
  2811. *
  2812. * Text passed to esc_js() is stripped of invalid or special characters,
  2813. * and properly slashed for output.
  2814. *
  2815. * @since 2.0.6
  2816. *
  2817. * @param string $safe_text The text after it has been escaped.
  2818. * @param string $text The text prior to being escaped.
  2819. */
  2820. return apply_filters( 'js_escape', $safe_text, $text );
  2821. }
  2822. /**
  2823. * Escaping for HTML blocks.
  2824. *
  2825. * @since 2.8.0
  2826. *
  2827. * @param string $text
  2828. * @return string
  2829. */
  2830. function esc_html( $text ) {
  2831. $safe_text = wp_check_invalid_utf8( $text );
  2832. $safe_text = _wp_specialchars( $safe_text, ENT_QUOTES );
  2833. /**
  2834. * Filter a string cleaned and escaped for output in HTML.
  2835. *
  2836. * Text passed to esc_html() is stripped of invalid or special characters
  2837. * before output.
  2838. *
  2839. * @since 2.8.0
  2840. *
  2841. * @param string $safe_text The text after it has been escaped.
  2842. * @param string $text The text prior to being escaped.
  2843. */
  2844. return apply_filters( 'esc_html', $safe_text, $text );
  2845. }
  2846. /**
  2847. * Escaping for HTML attributes.
  2848. *
  2849. * @since 2.8.0
  2850. *
  2851. * @param string $text
  2852. * @return string
  2853. */
  2854. function esc_attr( $text ) {
  2855. $safe_text = wp_check_invalid_utf8( $text );
  2856. $safe_text = _wp_specialchars( $safe_text, ENT_QUOTES );
  2857. /**
  2858. * Filter a string cleaned and escaped for output in an HTML attribute.
  2859. *
  2860. * Text passed to esc_attr() is stripped of invalid or special characters
  2861. * before output.
  2862. *
  2863. * @since 2.0.6
  2864. *
  2865. * @param string $safe_text The text after it has been escaped.
  2866. * @param string $text The text prior to being escaped.
  2867. */
  2868. return apply_filters( 'attribute_escape', $safe_text, $text );
  2869. }
  2870. /**
  2871. * Escaping for textarea values.
  2872. *
  2873. * @since 3.1.0
  2874. *
  2875. * @param string $text
  2876. * @return string
  2877. */
  2878. function esc_textarea( $text ) {
  2879. $safe_text = htmlspecialchars( $text, ENT_QUOTES, get_option( 'blog_charset' ) );
  2880. /**
  2881. * Filter a string cleaned and escaped for output in a textarea element.
  2882. *
  2883. * @since 3.1.0
  2884. *
  2885. * @param string $safe_text The text after it has been escaped.
  2886. * @param string $text The text prior to being escaped.
  2887. */
  2888. return apply_filters( 'esc_textarea', $safe_text, $text );
  2889. }
  2890. /**
  2891. * Escape an HTML tag name.
  2892. *
  2893. * @since 2.5.0
  2894. *
  2895. * @param string $tag_name
  2896. * @return string
  2897. */
  2898. function tag_escape($tag_name) {
  2899. $safe_tag = strtolower( preg_replace('/[^a-zA-Z0-9_:]/', '', $tag_name) );
  2900. /**
  2901. * Filter a string cleaned and escaped for output as an HTML tag.
  2902. *
  2903. * @since 2.8.0
  2904. *
  2905. * @param string $safe_tag The tag name after it has been escaped.
  2906. * @param string $tag_name The text before it was escaped.
  2907. */
  2908. return apply_filters( 'tag_escape', $safe_tag, $tag_name );
  2909. }
  2910. /**
  2911. * Convert full URL paths to absolute paths.
  2912. *
  2913. * Removes the http or https protocols and the domain. Keeps the path '/' at the
  2914. * beginning, so it isn't a true relative link, but from the web root base.
  2915. *
  2916. * @since 2.1.0
  2917. *
  2918. * @param string $link Full URL path.
  2919. * @return string Absolute path.
  2920. */
  2921. function wp_make_link_relative( $link ) {
  2922. return preg_replace( '|https?://[^/]+(/.*)|i', '$1', $link );
  2923. }
  2924. /**
  2925. * Sanitises various option values based on the nature of the option.
  2926. *
  2927. * This is basically a switch statement which will pass $value through a number
  2928. * of functions depending on the $option.
  2929. *
  2930. * @since 2.0.5
  2931. *
  2932. * @param string $option The name of the option.
  2933. * @param string $value The unsanitised value.
  2934. * @return string Sanitized value.
  2935. */
  2936. function sanitize_option($option, $value) {
  2937. switch ( $option ) {
  2938. case 'admin_email' :
  2939. case 'new_admin_email' :
  2940. $value = sanitize_email( $value );
  2941. if ( ! is_email( $value ) ) {
  2942. $value = get_option( $option ); // Resets option to stored value in the case of failed sanitization
  2943. if ( function_exists( 'add_settings_error' ) )
  2944. add_settings_error( $option, 'invalid_admin_email', __( 'The email address entered did not appear to be a valid email address. Please enter a valid email address.' ) );
  2945. }
  2946. break;
  2947. case 'thumbnail_size_w':
  2948. case 'thumbnail_size_h':
  2949. case 'medium_size_w':
  2950. case 'medium_size_h':
  2951. case 'large_size_w':
  2952. case 'large_size_h':
  2953. case 'mailserver_port':
  2954. case 'comment_max_links':
  2955. case 'page_on_front':
  2956. case 'page_for_posts':
  2957. case 'rss_excerpt_length':
  2958. case 'default_category':
  2959. case 'default_email_category':
  2960. case 'default_link_category':
  2961. case 'close_comments_days_old':
  2962. case 'comments_per_page':
  2963. case 'thread_comments_depth':
  2964. case 'users_can_register':
  2965. case 'start_of_week':
  2966. $value = absint( $value );
  2967. break;
  2968. case 'posts_per_page':
  2969. case 'posts_per_rss':
  2970. $value = (int) $value;
  2971. if ( empty($value) )
  2972. $value = 1;
  2973. if ( $value < -1 )
  2974. $value = abs($value);
  2975. break;
  2976. case 'default_ping_status':
  2977. case 'default_comment_status':
  2978. // Options that if not there have 0 value but need to be something like "closed"
  2979. if ( $value == '0' || $value == '')
  2980. $value = 'closed';
  2981. break;
  2982. case 'blogdescription':
  2983. case 'blogname':
  2984. $value = wp_kses_post( $value );
  2985. $value = esc_html( $value );
  2986. break;
  2987. case 'blog_charset':
  2988. $value = preg_replace('/[^a-zA-Z0-9_-]/', '', $value); // strips slashes
  2989. break;
  2990. case 'blog_public':
  2991. // This is the value if the settings checkbox is not checked on POST. Don't rely on this.
  2992. if ( null === $value )
  2993. $value = 1;
  2994. else
  2995. $value = intval( $value );
  2996. break;
  2997. case 'date_format':
  2998. case 'time_format':
  2999. case 'mailserver_url':
  3000. case 'mailserver_login':
  3001. case 'mailserver_pass':
  3002. case 'upload_path':
  3003. $value = strip_tags( $value );
  3004. $value = wp_kses_data( $value );
  3005. break;
  3006. case 'ping_sites':
  3007. $value = explode( "\n", $value );
  3008. $value = array_filter( array_map( 'trim', $value ) );
  3009. $value = array_filter( array_map( 'esc_url_raw', $value ) );
  3010. $value = implode( "\n", $value );
  3011. break;
  3012. case 'gmt_offset':
  3013. $value = preg_replace('/[^0-9:.-]/', '', $value); // strips slashes
  3014. break;
  3015. case 'siteurl':
  3016. if ( (bool)preg_match( '#http(s?)://(.+)#i', $value) ) {
  3017. $value = esc_url_raw($value);
  3018. } else {
  3019. $value = get_option( $option ); // Resets option to stored value in the case of failed sanitization
  3020. if ( function_exists('add_settings_error') )
  3021. add_settings_error('siteurl', 'invalid_siteurl', __('The WordPress address you entered did not appear to be a valid URL. Please enter a valid URL.'));
  3022. }
  3023. break;
  3024. case 'home':
  3025. if ( (bool)preg_match( '#http(s?)://(.+)#i', $value) ) {
  3026. $value = esc_url_raw($value);
  3027. } else {
  3028. $value = get_option( $option ); // Resets option to stored value in the case of failed sanitization
  3029. if ( function_exists('add_settings_error') )
  3030. add_settings_error('home', 'invalid_home', __('The Site address you entered did not appear to be a valid URL. Please enter a valid URL.'));
  3031. }
  3032. break;
  3033. case 'WPLANG':
  3034. $allowed = get_available_languages();
  3035. if ( ! in_array( $value, $allowed ) && ! empty( $value ) )
  3036. $value = get_option( $option );
  3037. break;
  3038. case 'illegal_names':
  3039. if ( ! is_array( $value ) )
  3040. $value = explode( ' ', $value );
  3041. $value = array_values( array_filter( array_map( 'trim', $value ) ) );
  3042. if ( ! $value )
  3043. $value = '';
  3044. break;
  3045. case 'limited_email_domains':
  3046. case 'banned_email_domains':
  3047. if ( ! is_array( $value ) )
  3048. $value = explode( "\n", $value );
  3049. $domains = array_values( array_filter( array_map( 'trim', $value ) ) );
  3050. $value = array();
  3051. foreach ( $domains as $domain ) {
  3052. if ( ! preg_match( '/(--|\.\.)/', $domain ) && preg_match( '|^([a-zA-Z0-9-\.])+$|', $domain ) )
  3053. $value[] = $domain;
  3054. }
  3055. if ( ! $value )
  3056. $value = '';
  3057. break;
  3058. case 'timezone_string':
  3059. $allowed_zones = timezone_identifiers_list();
  3060. if ( ! in_array( $value, $allowed_zones ) && ! empty( $value ) ) {
  3061. $value = get_option( $option ); // Resets option to stored value in the case of failed sanitization
  3062. if ( function_exists('add_settings_error') )
  3063. add_settings_error('timezone_string', 'invalid_timezone_string', __('The timezone you have entered is not valid. Please select a valid timezone.') );
  3064. }
  3065. break;
  3066. case 'permalink_structure':
  3067. case 'category_base':
  3068. case 'tag_base':
  3069. $value = esc_url_raw( $value );
  3070. $value = str_replace( 'http://', '', $value );
  3071. break;
  3072. case 'default_role' :
  3073. if ( ! get_role( $value ) && get_role( 'subscriber' ) )
  3074. $value = 'subscriber';
  3075. break;
  3076. case 'moderation_keys':
  3077. case 'blacklist_keys':
  3078. $value = explode( "\n", $value );
  3079. $value = array_filter( array_map( 'trim', $value ) );
  3080. $value = array_unique( $value );
  3081. $value = implode( "\n", $value );
  3082. break;
  3083. }
  3084. /**
  3085. * Filter an option value following sanitization.
  3086. *
  3087. * @since 2.3.0
  3088. *
  3089. * @param string $value The sanitized option value.
  3090. * @param string $option The option name.
  3091. */
  3092. $value = apply_filters( "sanitize_option_{$option}", $value, $option );
  3093. return $value;
  3094. }
  3095. /**
  3096. * Parses a string into variables to be stored in an array.
  3097. *
  3098. * Uses {@link http://www.php.net/parse_str parse_str()} and stripslashes if
  3099. * {@link http://www.php.net/magic_quotes magic_quotes_gpc} is on.
  3100. *
  3101. * @since 2.2.1
  3102. *
  3103. * @param string $string The string to be parsed.
  3104. * @param array $array Variables will be stored in this array.
  3105. */
  3106. function wp_parse_str( $string, &$array ) {
  3107. parse_str( $string, $array );
  3108. if ( get_magic_quotes_gpc() )
  3109. $array = stripslashes_deep( $array );
  3110. /**
  3111. * Filter the array of variables derived from a parsed string.
  3112. *
  3113. * @since 2.3.0
  3114. *
  3115. * @param array $array The array populated with variables.
  3116. */
  3117. $array = apply_filters( 'wp_parse_str', $array );
  3118. }
  3119. /**
  3120. * Convert lone less than signs.
  3121. *
  3122. * KSES already converts lone greater than signs.
  3123. *
  3124. * @uses wp_pre_kses_less_than_callback in the callback function.
  3125. * @since 2.3.0
  3126. *
  3127. * @param string $text Text to be converted.
  3128. * @return string Converted text.
  3129. */
  3130. function wp_pre_kses_less_than( $text ) {
  3131. return preg_replace_callback('%<[^>]*?((?=<)|>|$)%', 'wp_pre_kses_less_than_callback', $text);
  3132. }
  3133. /**
  3134. * Callback function used by preg_replace.
  3135. *
  3136. * @uses esc_html to format the $matches text.
  3137. * @since 2.3.0
  3138. *
  3139. * @param array $matches Populated by matches to preg_replace.
  3140. * @return string The text returned after esc_html if needed.
  3141. */
  3142. function wp_pre_kses_less_than_callback( $matches ) {
  3143. if ( false === strpos($matches[0], '>') )
  3144. return esc_html($matches[0]);
  3145. return $matches[0];
  3146. }
  3147. /**
  3148. * WordPress implementation of PHP sprintf() with filters.
  3149. *
  3150. * @since 2.5.0
  3151. * @link http://www.php.net/sprintf
  3152. *
  3153. * @param string $pattern The string which formatted args are inserted.
  3154. * @param mixed $args,... Arguments to be formatted into the $pattern string.
  3155. * @return string The formatted string.
  3156. */
  3157. function wp_sprintf( $pattern ) {
  3158. $args = func_get_args();
  3159. $len = strlen($pattern);
  3160. $start = 0;
  3161. $result = '';
  3162. $arg_index = 0;
  3163. while ( $len > $start ) {
  3164. // Last character: append and break
  3165. if ( strlen($pattern) - 1 == $start ) {
  3166. $result .= substr($pattern, -1);
  3167. break;
  3168. }
  3169. // Literal %: append and continue
  3170. if ( substr($pattern, $start, 2) == '%%' ) {
  3171. $start += 2;
  3172. $result .= '%';
  3173. continue;
  3174. }
  3175. // Get fragment before next %
  3176. $end = strpos($pattern, '%', $start + 1);
  3177. if ( false === $end )
  3178. $end = $len;
  3179. $fragment = substr($pattern, $start, $end - $start);
  3180. // Fragment has a specifier
  3181. if ( $pattern[$start] == '%' ) {
  3182. // Find numbered arguments or take the next one in order
  3183. if ( preg_match('/^%(\d+)\$/', $fragment, $matches) ) {
  3184. $arg = isset($args[$matches[1]]) ? $args[$matches[1]] : '';
  3185. $fragment = str_replace("%{$matches[1]}$", '%', $fragment);
  3186. } else {
  3187. ++$arg_index;
  3188. $arg = isset($args[$arg_index]) ? $args[$arg_index] : '';
  3189. }
  3190. /**
  3191. * Filter a fragment from the pattern passed to wp_sprintf().
  3192. *
  3193. * If the fragment is unchanged, then sprintf() will be run on the fragment.
  3194. *
  3195. * @since 2.5.0
  3196. *
  3197. * @param string $fragment A fragment from the pattern.
  3198. * @param string $arg The argument.
  3199. */
  3200. $_fragment = apply_filters( 'wp_sprintf', $fragment, $arg );
  3201. if ( $_fragment != $fragment )
  3202. $fragment = $_fragment;
  3203. else
  3204. $fragment = sprintf($fragment, strval($arg) );
  3205. }
  3206. // Append to result and move to next fragment
  3207. $result .= $fragment;
  3208. $start = $end;
  3209. }
  3210. return $result;
  3211. }
  3212. /**
  3213. * Localize list items before the rest of the content.
  3214. *
  3215. * The '%l' must be at the first characters can then contain the rest of the
  3216. * content. The list items will have ', ', ', and', and ' and ' added depending
  3217. * on the amount of list items in the $args parameter.
  3218. *
  3219. * @since 2.5.0
  3220. *
  3221. * @param string $pattern Content containing '%l' at the beginning.
  3222. * @param array $args List items to prepend to the content and replace '%l'.
  3223. * @return string Localized list items and rest of the content.
  3224. */
  3225. function wp_sprintf_l($pattern, $args) {
  3226. // Not a match
  3227. if ( substr($pattern, 0, 2) != '%l' )
  3228. return $pattern;
  3229. // Nothing to work with
  3230. if ( empty($args) )
  3231. return '';
  3232. /**
  3233. * Filter the translated delimiters used by wp_sprintf_l().
  3234. * Placeholders (%s) are included to assist translators and then
  3235. * removed before the array of strings reaches the filter.
  3236. *
  3237. * Please note: Ampersands and entities should be avoided here.
  3238. *
  3239. * @since 2.5.0
  3240. *
  3241. * @param array $delimiters An array of translated delimiters.
  3242. */
  3243. $l = apply_filters( 'wp_sprintf_l', array(
  3244. /* translators: used to join items in a list with more than 2 items */
  3245. 'between' => sprintf( __('%s, %s'), '', '' ),
  3246. /* translators: used to join last two items in a list with more than 2 times */
  3247. 'between_last_two' => sprintf( __('%s, and %s'), '', '' ),
  3248. /* translators: used to join items in a list with only 2 items */
  3249. 'between_only_two' => sprintf( __('%s and %s'), '', '' ),
  3250. ) );
  3251. $args = (array) $args;
  3252. $result = array_shift($args);
  3253. if ( count($args) == 1 )
  3254. $result .= $l['between_only_two'] . array_shift($args);
  3255. // Loop when more than two args
  3256. $i = count($args);
  3257. while ( $i ) {
  3258. $arg = array_shift($args);
  3259. $i--;
  3260. if ( 0 == $i )
  3261. $result .= $l['between_last_two'] . $arg;
  3262. else
  3263. $result .= $l['between'] . $arg;
  3264. }
  3265. return $result . substr($pattern, 2);
  3266. }
  3267. /**
  3268. * Safely extracts not more than the first $count characters from html string.
  3269. *
  3270. * UTF-8, tags and entities safe prefix extraction. Entities inside will *NOT*
  3271. * be counted as one character. For example &amp; will be counted as 4, &lt; as
  3272. * 3, etc.
  3273. *
  3274. * @since 2.5.0
  3275. *
  3276. * @param string $str String to get the excerpt from.
  3277. * @param integer $count Maximum number of characters to take.
  3278. * @param string $more Optional. What to append if $str needs to be trimmed. Defaults to empty string.
  3279. * @return string The excerpt.
  3280. */
  3281. function wp_html_excerpt( $str, $count, $more = null ) {
  3282. if ( null === $more )
  3283. $more = '';
  3284. $str = wp_strip_all_tags( $str, true );
  3285. $excerpt = mb_substr( $str, 0, $count );
  3286. // remove part of an entity at the end
  3287. $excerpt = preg_replace( '/&[^;\s]{0,6}$/', '', $excerpt );
  3288. if ( $str != $excerpt )
  3289. $excerpt = trim( $excerpt ) . $more;
  3290. return $excerpt;
  3291. }
  3292. /**
  3293. * Add a Base url to relative links in passed content.
  3294. *
  3295. * By default it supports the 'src' and 'href' attributes. However this can be
  3296. * changed via the 3rd param.
  3297. *
  3298. * @since 2.7.0
  3299. *
  3300. * @param string $content String to search for links in.
  3301. * @param string $base The base URL to prefix to links.
  3302. * @param array $attrs The attributes which should be processed.
  3303. * @return string The processed content.
  3304. */
  3305. function links_add_base_url( $content, $base, $attrs = array('src', 'href') ) {
  3306. global $_links_add_base;
  3307. $_links_add_base = $base;
  3308. $attrs = implode('|', (array)$attrs);
  3309. return preg_replace_callback( "!($attrs)=(['\"])(.+?)\\2!i", '_links_add_base', $content );
  3310. }
  3311. /**
  3312. * Callback to add a base url to relative links in passed content.
  3313. *
  3314. * @since 2.7.0
  3315. * @access private
  3316. *
  3317. * @param string $m The matched link.
  3318. * @return string The processed link.
  3319. */
  3320. function _links_add_base($m) {
  3321. global $_links_add_base;
  3322. //1 = attribute name 2 = quotation mark 3 = URL
  3323. return $m[1] . '=' . $m[2] .
  3324. ( preg_match( '#^(\w{1,20}):#', $m[3], $protocol ) && in_array( $protocol[1], wp_allowed_protocols() ) ?
  3325. $m[3] :
  3326. path_join( $_links_add_base, $m[3] ) )
  3327. . $m[2];
  3328. }
  3329. /**
  3330. * Adds a Target attribute to all links in passed content.
  3331. *
  3332. * This function by default only applies to <a> tags, however this can be
  3333. * modified by the 3rd param.
  3334. *
  3335. * <b>NOTE:</b> Any current target attributed will be stripped and replaced.
  3336. *
  3337. * @since 2.7.0
  3338. *
  3339. * @param string $content String to search for links in.
  3340. * @param string $target The Target to add to the links.
  3341. * @param array $tags An array of tags to apply to.
  3342. * @return string The processed content.
  3343. */
  3344. function links_add_target( $content, $target = '_blank', $tags = array('a') ) {
  3345. global $_links_add_target;
  3346. $_links_add_target = $target;
  3347. $tags = implode('|', (array)$tags);
  3348. return preg_replace_callback( "!<($tags)([^>]*)>!i", '_links_add_target', $content );
  3349. }
  3350. /**
  3351. * Callback to add a target attribute to all links in passed content.
  3352. *
  3353. * @since 2.7.0
  3354. * @access private
  3355. *
  3356. * @param string $m The matched link.
  3357. * @return string The processed link.
  3358. */
  3359. function _links_add_target( $m ) {
  3360. global $_links_add_target;
  3361. $tag = $m[1];
  3362. $link = preg_replace('|( target=([\'"])(.*?)\2)|i', '', $m[2]);
  3363. return '<' . $tag . $link . ' target="' . esc_attr( $_links_add_target ) . '">';
  3364. }
  3365. /**
  3366. * Normalize EOL characters and strip duplicate whitespace.
  3367. *
  3368. * @since 2.7.0
  3369. *
  3370. * @param string $str The string to normalize.
  3371. * @return string The normalized string.
  3372. */
  3373. function normalize_whitespace( $str ) {
  3374. $str = trim( $str );
  3375. $str = str_replace( "\r", "\n", $str );
  3376. $str = preg_replace( array( '/\n+/', '/[ \t]+/' ), array( "\n", ' ' ), $str );
  3377. return $str;
  3378. }
  3379. /**
  3380. * Properly strip all HTML tags including script and style
  3381. *
  3382. * This differs from strip_tags() because it removes the contents of
  3383. * the <script> and <style> tags. E.g. strip_tags( '<script>something</script>' )
  3384. * will return 'something'. wp_strip_all_tags will return ''
  3385. *
  3386. * @since 2.9.0
  3387. *
  3388. * @param string $string String containing HTML tags
  3389. * @param bool $remove_breaks optional Whether to remove left over line breaks and white space chars
  3390. * @return string The processed string.
  3391. */
  3392. function wp_strip_all_tags($string, $remove_breaks = false) {
  3393. $string = preg_replace( '@<(script|style)[^>]*?>.*?</\\1>@si', '', $string );
  3394. $string = strip_tags($string);
  3395. if ( $remove_breaks )
  3396. $string = preg_replace('/[\r\n\t ]+/', ' ', $string);
  3397. return trim( $string );
  3398. }
  3399. /**
  3400. * Sanitize a string from user input or from the db
  3401. *
  3402. * check for invalid UTF-8,
  3403. * Convert single < characters to entity,
  3404. * strip all tags,
  3405. * remove line breaks, tabs and extra white space,
  3406. * strip octets.
  3407. *
  3408. * @since 2.9.0
  3409. *
  3410. * @param string $str
  3411. * @return string
  3412. */
  3413. function sanitize_text_field($str) {
  3414. $filtered = wp_check_invalid_utf8( $str );
  3415. if ( strpos($filtered, '<') !== false ) {
  3416. $filtered = wp_pre_kses_less_than( $filtered );
  3417. // This will strip extra whitespace for us.
  3418. $filtered = wp_strip_all_tags( $filtered, true );
  3419. } else {
  3420. $filtered = trim( preg_replace('/[\r\n\t ]+/', ' ', $filtered) );
  3421. }
  3422. $found = false;
  3423. while ( preg_match('/%[a-f0-9]{2}/i', $filtered, $match) ) {
  3424. $filtered = str_replace($match[0], '', $filtered);
  3425. $found = true;
  3426. }
  3427. if ( $found ) {
  3428. // Strip out the whitespace that may now exist after removing the octets.
  3429. $filtered = trim( preg_replace('/ +/', ' ', $filtered) );
  3430. }
  3431. /**
  3432. * Filter a sanitized text field string.
  3433. *
  3434. * @since 2.9.0
  3435. *
  3436. * @param string $filtered The sanitized string.
  3437. * @param string $str The string prior to being sanitized.
  3438. */
  3439. return apply_filters( 'sanitize_text_field', $filtered, $str );
  3440. }
  3441. /**
  3442. * i18n friendly version of basename()
  3443. *
  3444. * @since 3.1.0
  3445. *
  3446. * @param string $path A path.
  3447. * @param string $suffix If the filename ends in suffix this will also be cut off.
  3448. * @return string
  3449. */
  3450. function wp_basename( $path, $suffix = '' ) {
  3451. return urldecode( basename( str_replace( array( '%2F', '%5C' ), '/', urlencode( $path ) ), $suffix ) );
  3452. }
  3453. /**
  3454. * Forever eliminate "Wordpress" from the planet (or at least the little bit we can influence).
  3455. *
  3456. * Violating our coding standards for a good function name.
  3457. *
  3458. * @since 3.0.0
  3459. */
  3460. function capital_P_dangit( $text ) {
  3461. // Simple replacement for titles
  3462. $current_filter = current_filter();
  3463. if ( 'the_title' === $current_filter || 'wp_title' === $current_filter )
  3464. return str_replace( 'Wordpress', 'WordPress', $text );
  3465. // Still here? Use the more judicious replacement
  3466. static $dblq = false;
  3467. if ( false === $dblq )
  3468. $dblq = _x( '&#8220;', 'opening curly double quote' );
  3469. return str_replace(
  3470. array( ' Wordpress', '&#8216;Wordpress', $dblq . 'Wordpress', '>Wordpress', '(Wordpress' ),
  3471. array( ' WordPress', '&#8216;WordPress', $dblq . 'WordPress', '>WordPress', '(WordPress' ),
  3472. $text );
  3473. }
  3474. /**
  3475. * Sanitize a mime type
  3476. *
  3477. * @since 3.1.3
  3478. *
  3479. * @param string $mime_type Mime type
  3480. * @return string Sanitized mime type
  3481. */
  3482. function sanitize_mime_type( $mime_type ) {
  3483. $sani_mime_type = preg_replace( '/[^-+*.a-zA-Z0-9\/]/', '', $mime_type );
  3484. /**
  3485. * Filter a mime type following sanitization.
  3486. *
  3487. * @since 3.1.3
  3488. *
  3489. * @param string $sani_mime_type The sanitized mime type.
  3490. * @param string $mime_type The mime type prior to sanitization.
  3491. */
  3492. return apply_filters( 'sanitize_mime_type', $sani_mime_type, $mime_type );
  3493. }
  3494. /**
  3495. * Sanitize space or carriage return separated URLs that are used to send trackbacks.
  3496. *
  3497. * @since 3.4.0
  3498. *
  3499. * @param string $to_ping Space or carriage return separated URLs
  3500. * @return string URLs starting with the http or https protocol, separated by a carriage return.
  3501. */
  3502. function sanitize_trackback_urls( $to_ping ) {
  3503. $urls_to_ping = preg_split( '/[\r\n\t ]/', trim( $to_ping ), -1, PREG_SPLIT_NO_EMPTY );
  3504. foreach ( $urls_to_ping as $k => $url ) {
  3505. if ( !preg_match( '#^https?://.#i', $url ) )
  3506. unset( $urls_to_ping[$k] );
  3507. }
  3508. $urls_to_ping = array_map( 'esc_url_raw', $urls_to_ping );
  3509. $urls_to_ping = implode( "\n", $urls_to_ping );
  3510. /**
  3511. * Filter a list of trackback URLs following sanitization.
  3512. *
  3513. * The string returned here consists of a space or carriage return-delimited list
  3514. * of trackback URLs.
  3515. *
  3516. * @since 3.4.0
  3517. *
  3518. * @param string $urls_to_ping Sanitized space or carriage return separated URLs.
  3519. * @param string $to_ping Space or carriage return separated URLs before sanitization.
  3520. */
  3521. return apply_filters( 'sanitize_trackback_urls', $urls_to_ping, $to_ping );
  3522. }
  3523. /**
  3524. * Add slashes to a string or array of strings.
  3525. *
  3526. * This should be used when preparing data for core API that expects slashed data.
  3527. * This should not be used to escape data going directly into an SQL query.
  3528. *
  3529. * @since 3.6.0
  3530. *
  3531. * @param string|array $value String or array of strings to slash.
  3532. * @return string|array Slashed $value
  3533. */
  3534. function wp_slash( $value ) {
  3535. if ( is_array( $value ) ) {
  3536. foreach ( $value as $k => $v ) {
  3537. if ( is_array( $v ) ) {
  3538. $value[$k] = wp_slash( $v );
  3539. } else {
  3540. $value[$k] = addslashes( $v );
  3541. }
  3542. }
  3543. } else {
  3544. $value = addslashes( $value );
  3545. }
  3546. return $value;
  3547. }
  3548. /**
  3549. * Remove slashes from a string or array of strings.
  3550. *
  3551. * This should be used to remove slashes from data passed to core API that
  3552. * expects data to be unslashed.
  3553. *
  3554. * @since 3.6.0
  3555. *
  3556. * @param string|array $value String or array of strings to unslash.
  3557. * @return string|array Unslashed $value
  3558. */
  3559. function wp_unslash( $value ) {
  3560. return stripslashes_deep( $value );
  3561. }
  3562. /**
  3563. * Extract and return the first URL from passed content.
  3564. *
  3565. * @since 3.6.0
  3566. *
  3567. * @param string $content A string which might contain a URL.
  3568. * @return string The found URL.
  3569. */
  3570. function get_url_in_content( $content ) {
  3571. if ( empty( $content ) ) {
  3572. return false;
  3573. }
  3574. if ( preg_match( '/<a\s[^>]*?href=([\'"])(.+?)\1/is', $content, $matches ) ) {
  3575. return esc_url_raw( $matches[2] );
  3576. }
  3577. return false;
  3578. }
  3579. /**
  3580. * Returns the regexp for common whitespace characters.
  3581. *
  3582. * By default, spaces include new lines, tabs, nbsp entities, and the UTF-8 nbsp.
  3583. * This is designed to replace the PCRE \s sequence. In ticket #22692, that
  3584. * sequence was found to be unreliable due to random inclusion of the A0 byte.
  3585. *
  3586. * @since 4.0.0
  3587. *
  3588. * @return string The spaces regexp.
  3589. */
  3590. function wp_spaces_regexp() {
  3591. static $spaces;
  3592. if ( empty( $spaces ) ) {
  3593. /**
  3594. * Filter the regexp for common whitespace characters.
  3595. *
  3596. * This string is substituted for the \s sequence as needed in regular
  3597. * expressions. For websites not written in English, different characters
  3598. * may represent whitespace. For websites not encoded in UTF-8, the 0xC2 0xA0
  3599. * sequence may not be in use.
  3600. *
  3601. * @since 4.0.0
  3602. *
  3603. * @param string $spaces Regexp pattern for matching common whitespace characters.
  3604. */
  3605. $spaces = apply_filters( 'wp_spaces_regexp', '[\r\n\t ]|\xC2\xA0|&nbsp;' );
  3606. }
  3607. return $spaces;
  3608. }