PageRenderTime 68ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/APP/wp-includes/formatting.php

https://bitbucket.org/AFelipeTrujillo/goblog
PHP | 3827 lines | 2273 code | 302 blank | 1252 comment | 310 complexity | 19afd3e550fd94adeadaee57dab15718 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1

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

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