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

/wp-includes/formatting.php

https://gitlab.com/jessehall/hudson_alpha
PHP | 4007 lines | 2377 code | 331 blank | 1299 comment | 330 complexity | cf4dd72771cc0d1693822a8358294c4c MD5 | raw file
Possible License(s): GPL-2.0

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

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

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