PageRenderTime 1491ms CodeModel.GetById 41ms RepoModel.GetById 1ms app.codeStats 0ms

/gp-includes/backpress/functions.formatting.php

https://bitbucket.org/moodsdesign-ondemand/reglot
PHP | 2274 lines | 1448 code | 184 blank | 642 comment | 190 complexity | e554c3d1a3c4ab62bc7d0ba715f52c5a MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. // Last sync [WP20951]
  3. /**
  4. * Main BackPress Formatting API.based on wp-includes/formatting.php
  5. *
  6. * Handles many functions for formatting output.
  7. * Excluded functions are indicated in comments
  8. *
  9. * @package BackPress
  10. **/
  11. if ( !function_exists( 'wptexturize' ) ) :
  12. /**
  13. * Replaces common plain text characters into formatted entities
  14. *
  15. * As an example,
  16. * <code>
  17. * 'cause today's effort makes it worth tomorrow's "holiday"...
  18. * </code>
  19. * Becomes:
  20. * <code>
  21. * &#8217;cause today&#8217;s effort makes it worth tomorrow&#8217;s &#8220;holiday&#8221;&#8230;
  22. * </code>
  23. * Code within certain html blocks are skipped.
  24. *
  25. * @since 0.71
  26. * @uses $wp_cockneyreplace Array of formatted entities for certain common phrases
  27. *
  28. * @param string $text The text to be formatted
  29. * @return string The string replaced with html entities
  30. */
  31. function wptexturize($text) {
  32. global $wp_cockneyreplace;
  33. static $static_characters, $static_replacements, $dynamic_characters, $dynamic_replacements,
  34. $default_no_texturize_tags, $default_no_texturize_shortcodes;
  35. // No need to set up these static variables more than once
  36. if ( ! isset( $static_characters ) ) {
  37. /* translators: opening curly double quote */
  38. $opening_quote = _x( '&#8220;', 'opening curly double quote' );
  39. /* translators: closing curly double quote */
  40. $closing_quote = _x( '&#8221;', 'closing curly double quote' );
  41. /* translators: apostrophe, for example in 'cause or can't */
  42. $apos = _x( '&#8217;', 'apostrophe' );
  43. /* translators: prime, for example in 9' (nine feet) */
  44. $prime = _x( '&#8242;', 'prime' );
  45. /* translators: double prime, for example in 9" (nine inches) */
  46. $double_prime = _x( '&#8243;', 'double prime' );
  47. /* translators: opening curly single quote */
  48. $opening_single_quote = _x( '&#8216;', 'opening curly single quote' );
  49. /* translators: closing curly single quote */
  50. $closing_single_quote = _x( '&#8217;', 'closing curly single quote' );
  51. /* translators: en dash */
  52. $en_dash = _x( '&#8211;', 'en dash' );
  53. /* translators: em dash */
  54. $em_dash = _x( '&#8212;', 'em dash' );
  55. $default_no_texturize_tags = array('pre', 'code', 'kbd', 'style', 'script', 'tt');
  56. $default_no_texturize_shortcodes = array('code');
  57. // if a plugin has provided an autocorrect array, use it
  58. if ( isset($wp_cockneyreplace) ) {
  59. $cockney = array_keys($wp_cockneyreplace);
  60. $cockneyreplace = array_values($wp_cockneyreplace);
  61. } elseif ( "'" != $apos ) { // Only bother if we're doing a replacement.
  62. $cockney = array( "'tain't", "'twere", "'twas", "'tis", "'twill", "'til", "'bout", "'nuff", "'round", "'cause" );
  63. $cockneyreplace = array( $apos . "tain" . $apos . "t", $apos . "twere", $apos . "twas", $apos . "tis", $apos . "twill", $apos . "til", $apos . "bout", $apos . "nuff", $apos . "round", $apos . "cause" );
  64. } else {
  65. $cockney = $cockneyreplace = array();
  66. }
  67. $static_characters = array_merge( array( '---', ' -- ', '--', ' - ', 'xn&#8211;', '...', '``', '\'\'', ' (tm)' ), $cockney );
  68. $static_replacements = array_merge( array( $em_dash, ' ' . $em_dash . ' ', $en_dash, ' ' . $en_dash . ' ', 'xn--', '&#8230;', $opening_quote, $closing_quote, ' &#8482;' ), $cockneyreplace );
  69. $dynamic = array();
  70. if ( "'" != $apos ) {
  71. $dynamic[ '/\'(\d\d(?:&#8217;|\')?s)/' ] = $apos . '$1'; // '99's
  72. $dynamic[ '/\'(\d)/' ] = $apos . '$1'; // '99
  73. }
  74. if ( "'" != $opening_single_quote )
  75. $dynamic[ '/(\s|\A|[([{<]|")\'/' ] = '$1' . $opening_single_quote; // opening single quote, even after (, {, <, [
  76. if ( '"' != $double_prime )
  77. $dynamic[ '/(\d)"/' ] = '$1' . $double_prime; // 9" (double prime)
  78. if ( "'" != $prime )
  79. $dynamic[ '/(\d)\'/' ] = '$1' . $prime; // 9' (prime)
  80. if ( "'" != $apos )
  81. $dynamic[ '/(\S)\'([^\'\s])/' ] = '$1' . $apos . '$2'; // apostrophe in a word
  82. if ( '"' != $opening_quote )
  83. $dynamic[ '/(\s|\A|[([{<])"(?!\s)/' ] = '$1' . $opening_quote . '$2'; // opening double quote, even after (, {, <, [
  84. if ( '"' != $closing_quote )
  85. $dynamic[ '/"(\s|\S|\Z)/' ] = $closing_quote . '$1'; // closing double quote
  86. if ( "'" != $closing_single_quote )
  87. $dynamic[ '/\'([\s.]|\Z)/' ] = $closing_single_quote . '$1'; // closing single quote
  88. $dynamic[ '/\b(\d+)x(\d+)\b/' ] = '$1&#215;$2'; // 9x9 (times)
  89. $dynamic_characters = array_keys( $dynamic );
  90. $dynamic_replacements = array_values( $dynamic );
  91. }
  92. // Transform into regexp sub-expression used in _wptexturize_pushpop_element
  93. // Must do this everytime in case plugins use these filters in a context sensitive manner
  94. $no_texturize_tags = '(' . implode('|', apply_filters('no_texturize_tags', $default_no_texturize_tags) ) . ')';
  95. $no_texturize_shortcodes = '(' . implode('|', apply_filters('no_texturize_shortcodes', $default_no_texturize_shortcodes) ) . ')';
  96. $no_texturize_tags_stack = array();
  97. $no_texturize_shortcodes_stack = array();
  98. $textarr = preg_split('/(<.*>|\[.*\])/Us', $text, -1, PREG_SPLIT_DELIM_CAPTURE);
  99. foreach ( $textarr as &$curl ) {
  100. if ( empty( $curl ) )
  101. continue;
  102. // Only call _wptexturize_pushpop_element if first char is correct tag opening
  103. $first = $curl[0];
  104. if ( '<' === $first ) {
  105. _wptexturize_pushpop_element($curl, $no_texturize_tags_stack, $no_texturize_tags, '<', '>');
  106. } elseif ( '[' === $first ) {
  107. _wptexturize_pushpop_element($curl, $no_texturize_shortcodes_stack, $no_texturize_shortcodes, '[', ']');
  108. } elseif ( empty($no_texturize_shortcodes_stack) && empty($no_texturize_tags_stack) ) {
  109. // This is not a tag, nor is the texturization disabled static strings
  110. $curl = str_replace($static_characters, $static_replacements, $curl);
  111. // regular expressions
  112. $curl = preg_replace($dynamic_characters, $dynamic_replacements, $curl);
  113. }
  114. $curl = preg_replace('/&([^#])(?![a-zA-Z1-4]{1,8};)/', '&#038;$1', $curl);
  115. }
  116. return implode( '', $textarr );
  117. }
  118. endif;
  119. if ( !function_exists( '_wptexturize_pushpop_element' ) ) :
  120. /**
  121. * Search for disabled element tags. Push element to stack on tag open and pop
  122. * on tag close. Assumes first character of $text is tag opening.
  123. *
  124. * @access private
  125. * @since 2.9.0
  126. *
  127. * @param string $text Text to check. First character is assumed to be $opening
  128. * @param array $stack Array used as stack of opened tag elements
  129. * @param string $disabled_elements Tags to match against formatted as regexp sub-expression
  130. * @param string $opening Tag opening character, assumed to be 1 character long
  131. * @param string $opening Tag closing character
  132. * @return object
  133. */
  134. function _wptexturize_pushpop_element($text, &$stack, $disabled_elements, $opening = '<', $closing = '>') {
  135. // Check if it is a closing tag -- otherwise assume opening tag
  136. if (strncmp($opening . '/', $text, 2)) {
  137. // Opening? Check $text+1 against disabled elements
  138. if (preg_match('/^' . $disabled_elements . '\b/', substr($text, 1), $matches)) {
  139. /*
  140. * This disables texturize until we find a closing tag of our type
  141. * (e.g. <pre>) even if there was invalid nesting before that
  142. *
  143. * Example: in the case <pre>sadsadasd</code>"baba"</pre>
  144. * "baba" won't be texturize
  145. */
  146. array_push($stack, $matches[1]);
  147. }
  148. } else {
  149. // Closing? Check $text+2 against disabled elements
  150. $c = preg_quote($closing, '/');
  151. if (preg_match('/^' . $disabled_elements . $c . '/', substr($text, 2), $matches)) {
  152. $last = array_pop($stack);
  153. // Make sure it matches the opening tag
  154. if ($last != $matches[1])
  155. array_push($stack, $last);
  156. }
  157. }
  158. }
  159. endif;
  160. // @todo: Deprecated in WP 3.4 can probably remove as was only used by autop and we never had that [WP20307]
  161. if ( !function_exists( 'clean_pre' ) ) :
  162. /**
  163. * Accepts matches array from preg_replace_callback in wpautop() or a string.
  164. *
  165. * Ensures that the contents of a <<pre>>...<</pre>> HTML block are not
  166. * converted into paragraphs or line-breaks.
  167. *
  168. * @since 1.2.0
  169. *
  170. * @param array|string $matches The array or string
  171. * @return string The pre block without paragraph/line-break conversion.
  172. */
  173. function clean_pre($matches) {
  174. if ( is_array($matches) )
  175. $text = $matches[1] . $matches[2] . "</pre>";
  176. else
  177. $text = $matches;
  178. $text = str_replace('<br />', '', $text);
  179. $text = str_replace('<p>', "\n", $text);
  180. $text = str_replace('</p>', '', $text);
  181. return $text;
  182. }
  183. endif;
  184. // ! function wpautop()
  185. // ! function _autop_newline_preservation_helper()
  186. // ! function shortcode_unautop()
  187. if ( !function_exists('seems_utf8') ) :
  188. /**
  189. * Checks to see if a string is utf8 encoded.
  190. *
  191. * NOTE: This function checks for 5-Byte sequences, UTF8
  192. * has Bytes Sequences with a maximum length of 4.
  193. *
  194. * @author bmorel at ssi dot fr (modified)
  195. * @since 1.2.1
  196. *
  197. * @param string $str The string to be checked
  198. * @return bool True if $str fits a UTF-8 model, false otherwise.
  199. */
  200. function seems_utf8($str) {
  201. $length = strlen($str);
  202. for ($i=0; $i < $length; $i++) {
  203. $c = ord($str[$i]);
  204. if ($c < 0x80) $n = 0; # 0bbbbbbb
  205. elseif (($c & 0xE0) == 0xC0) $n=1; # 110bbbbb
  206. elseif (($c & 0xF0) == 0xE0) $n=2; # 1110bbbb
  207. elseif (($c & 0xF8) == 0xF0) $n=3; # 11110bbb
  208. elseif (($c & 0xFC) == 0xF8) $n=4; # 111110bb
  209. elseif (($c & 0xFE) == 0xFC) $n=5; # 1111110b
  210. else return false; # Does not match any model
  211. for ($j=0; $j<$n; $j++) { # n bytes matching 10bbbbbb follow ?
  212. if ((++$i == $length) || ((ord($str[$i]) & 0xC0) != 0x80))
  213. return false;
  214. }
  215. }
  216. return true;
  217. }
  218. endif;
  219. if ( !function_exists('_wp_specialchars') ) :
  220. /**
  221. * Converts a number of special characters into their HTML entities.
  222. *
  223. * Specifically deals with: &, <, >, ", and '.
  224. *
  225. * $quote_style can be set to ENT_COMPAT to encode " to
  226. * &quot;, or ENT_QUOTES to do both. Default is ENT_NOQUOTES where no quotes are encoded.
  227. *
  228. * @since 1.2.2
  229. *
  230. * @param string $string The text which is to be encoded.
  231. * @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.
  232. * @param string $charset Optional. The character encoding of the string. Default is false.
  233. * @param boolean $double_encode Optional. Whether to encode existing html entities. Default is false.
  234. * @return string The encoded text with HTML entities.
  235. */
  236. function _wp_specialchars( $string, $quote_style = ENT_NOQUOTES, $charset = false, $double_encode = false ) {
  237. $string = (string) $string;
  238. if ( 0 === strlen( $string ) )
  239. return '';
  240. // Don't bother if there are no specialchars - saves some processing
  241. if ( ! preg_match( '/[&<>"\']/', $string ) )
  242. return $string;
  243. // Account for the previous behaviour of the function when the $quote_style is not an accepted value
  244. if ( empty( $quote_style ) )
  245. $quote_style = ENT_NOQUOTES;
  246. elseif ( ! in_array( $quote_style, array( 0, 2, 3, 'single', 'double' ), true ) )
  247. $quote_style = ENT_QUOTES;
  248. // Store the site charset as a static to avoid multiple calls to backpress_get_option()
  249. if ( !$charset ) {
  250. static $_charset;
  251. if ( !isset( $_charset ) ) {
  252. $_charset = backpress_get_option( 'charset' );
  253. }
  254. $charset = $_charset;
  255. }
  256. if ( in_array( $charset, array( 'utf8', 'utf-8', 'UTF8' ) ) )
  257. $charset = 'UTF-8';
  258. $_quote_style = $quote_style;
  259. if ( $quote_style === 'double' ) {
  260. $quote_style = ENT_COMPAT;
  261. $_quote_style = ENT_COMPAT;
  262. } elseif ( $quote_style === 'single' ) {
  263. $quote_style = ENT_NOQUOTES;
  264. }
  265. // Handle double encoding ourselves
  266. if ( $double_encode ) {
  267. $string = @htmlspecialchars( $string, $quote_style, $charset );
  268. } else {
  269. // Decode &amp; into &
  270. $string = wp_specialchars_decode( $string, $_quote_style );
  271. // Guarantee every &entity; is valid or re-encode the &
  272. $string = wp_kses_normalize_entities( $string );
  273. // Now re-encode everything except &entity;
  274. $string = preg_split( '/(&#?x?[0-9a-z]+;)/i', $string, -1, PREG_SPLIT_DELIM_CAPTURE );
  275. for ( $i = 0; $i < count( $string ); $i += 2 )
  276. $string[$i] = @htmlspecialchars( $string[$i], $quote_style, $charset );
  277. $string = implode( '', $string );
  278. }
  279. // Backwards compatibility
  280. if ( 'single' === $_quote_style )
  281. $string = str_replace( "'", '&#039;', $string );
  282. return $string;
  283. }
  284. endif;
  285. if ( !function_exists( 'wp_specialchars_decode' ) ) :
  286. /**
  287. * Converts a number of HTML entities into their special characters.
  288. *
  289. * Specifically deals with: &, <, >, ", and '.
  290. *
  291. * $quote_style can be set to ENT_COMPAT to decode " entities,
  292. * or ENT_QUOTES to do both " and '. Default is ENT_NOQUOTES where no quotes are decoded.
  293. *
  294. * @since 2.8
  295. *
  296. * @param string $string The text which is to be decoded.
  297. * @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.
  298. * @return string The decoded text without HTML entities.
  299. */
  300. function wp_specialchars_decode( $string, $quote_style = ENT_NOQUOTES ) {
  301. $string = (string) $string;
  302. if ( 0 === strlen( $string ) ) {
  303. return '';
  304. }
  305. // Don't bother if there are no entities - saves a lot of processing
  306. if ( strpos( $string, '&' ) === false ) {
  307. return $string;
  308. }
  309. // Match the previous behaviour of _wp_specialchars() when the $quote_style is not an accepted value
  310. if ( empty( $quote_style ) ) {
  311. $quote_style = ENT_NOQUOTES;
  312. } elseif ( !in_array( $quote_style, array( 0, 2, 3, 'single', 'double' ), true ) ) {
  313. $quote_style = ENT_QUOTES;
  314. }
  315. // More complete than get_html_translation_table( HTML_SPECIALCHARS )
  316. $single = array( '&#039;' => '\'', '&#x27;' => '\'' );
  317. $single_preg = array( '/&#0*39;/' => '&#039;', '/&#x0*27;/i' => '&#x27;' );
  318. $double = array( '&quot;' => '"', '&#034;' => '"', '&#x22;' => '"' );
  319. $double_preg = array( '/&#0*34;/' => '&#034;', '/&#x0*22;/i' => '&#x22;' );
  320. $others = array( '&lt;' => '<', '&#060;' => '<', '&gt;' => '>', '&#062;' => '>', '&amp;' => '&', '&#038;' => '&', '&#x26;' => '&' );
  321. $others_preg = array( '/&#0*60;/' => '&#060;', '/&#0*62;/' => '&#062;', '/&#0*38;/' => '&#038;', '/&#x0*26;/i' => '&#x26;' );
  322. if ( $quote_style === ENT_QUOTES ) {
  323. $translation = array_merge( $single, $double, $others );
  324. $translation_preg = array_merge( $single_preg, $double_preg, $others_preg );
  325. } elseif ( $quote_style === ENT_COMPAT || $quote_style === 'double' ) {
  326. $translation = array_merge( $double, $others );
  327. $translation_preg = array_merge( $double_preg, $others_preg );
  328. } elseif ( $quote_style === 'single' ) {
  329. $translation = array_merge( $single, $others );
  330. $translation_preg = array_merge( $single_preg, $others_preg );
  331. } elseif ( $quote_style === ENT_NOQUOTES ) {
  332. $translation = $others;
  333. $translation_preg = $others_preg;
  334. }
  335. // Remove zero padding on numeric entities
  336. $string = preg_replace( array_keys( $translation_preg ), array_values( $translation_preg ), $string );
  337. // Replace characters according to translation table
  338. return strtr( $string, $translation );
  339. }
  340. endif;
  341. if ( !function_exists( 'wp_check_invalid_utf8' ) ) :
  342. /**
  343. * Checks for invalid UTF8 in a string.
  344. *
  345. * @since 2.8
  346. *
  347. * @param string $string The text which is to be checked.
  348. * @param boolean $strip Optional. Whether to attempt to strip out invalid UTF8. Default is false.
  349. * @return string The checked text.
  350. */
  351. function wp_check_invalid_utf8( $string, $strip = false ) {
  352. $string = (string) $string;
  353. if ( 0 === strlen( $string ) ) {
  354. return '';
  355. }
  356. // Store the site charset as a static to avoid multiple calls to backpress_get_option()
  357. static $is_utf8;
  358. if ( !isset( $is_utf8 ) ) {
  359. $is_utf8 = in_array( backpress_get_option( 'charset' ), array( 'utf8', 'utf-8', 'UTF8', 'UTF-8' ) );
  360. }
  361. if ( !$is_utf8 ) {
  362. return $string;
  363. }
  364. // Check for support for utf8 in the installed PCRE library once and store the result in a static
  365. static $utf8_pcre;
  366. if ( !isset( $utf8_pcre ) ) {
  367. $utf8_pcre = @preg_match( '/^./u', 'a' );
  368. }
  369. // We can't demand utf8 in the PCRE installation, so just return the string in those cases
  370. if ( !$utf8_pcre ) {
  371. return $string;
  372. }
  373. // preg_match fails when it encounters invalid UTF8 in $string
  374. if ( 1 === @preg_match( '/^./us', $string ) ) {
  375. return $string;
  376. }
  377. // Attempt to strip the bad chars if requested (not recommended)
  378. if ( $strip && function_exists( 'iconv' ) ) {
  379. return iconv( 'utf-8', 'utf-8', $string );
  380. }
  381. return '';
  382. }
  383. endif;
  384. if ( !function_exists('utf8_uri_encode') ) :
  385. /**
  386. * Encode the Unicode values to be used in the URI.
  387. *
  388. * @since 1.5.0
  389. *
  390. * @param string $utf8_string
  391. * @param int $length Max length of the string
  392. * @return string String with Unicode encoded for URI.
  393. */
  394. function utf8_uri_encode( $utf8_string, $length = 0 ) {
  395. $unicode = '';
  396. $values = array();
  397. $num_octets = 1;
  398. $unicode_length = 0;
  399. $string_length = strlen( $utf8_string );
  400. for ($i = 0; $i < $string_length; $i++ ) {
  401. $value = ord( $utf8_string[ $i ] );
  402. if ( $value < 128 ) {
  403. if ( $length && ( $unicode_length >= $length ) )
  404. break;
  405. $unicode .= chr($value);
  406. $unicode_length++;
  407. } else {
  408. if ( count( $values ) == 0 ) $num_octets = ( $value < 224 ) ? 2 : 3;
  409. $values[] = $value;
  410. if ( $length && ( $unicode_length + ($num_octets * 3) ) > $length )
  411. break;
  412. if ( count( $values ) == $num_octets ) {
  413. if ($num_octets == 3) {
  414. $unicode .= '%' . dechex($values[0]) . '%' . dechex($values[1]) . '%' . dechex($values[2]);
  415. $unicode_length += 9;
  416. } else {
  417. $unicode .= '%' . dechex($values[0]) . '%' . dechex($values[1]);
  418. $unicode_length += 6;
  419. }
  420. $values = array();
  421. $num_octets = 1;
  422. }
  423. }
  424. }
  425. return $unicode;
  426. }
  427. endif;
  428. if ( !function_exists('remove_accents') ) :
  429. /**
  430. * Converts all accent characters to ASCII characters.
  431. *
  432. * If there are no accent characters, then the string given is just returned.
  433. *
  434. * @since 1.2.1
  435. *
  436. * @param string $string Text that might have accent characters
  437. * @return string Filtered string with replaced "nice" characters.
  438. */
  439. function remove_accents($string) {
  440. if ( !preg_match('/[\x80-\xff]/', $string) )
  441. return $string;
  442. if (seems_utf8($string)) {
  443. $chars = array(
  444. // Decompositions for Latin-1 Supplement
  445. chr(194).chr(170) => 'a', chr(194).chr(186) => 'o',
  446. chr(195).chr(128) => 'A', chr(195).chr(129) => 'A',
  447. chr(195).chr(130) => 'A', chr(195).chr(131) => 'A',
  448. chr(195).chr(132) => 'A', chr(195).chr(133) => 'A',
  449. chr(195).chr(134) => 'AE',chr(195).chr(135) => 'C',
  450. chr(195).chr(136) => 'E', chr(195).chr(137) => 'E',
  451. chr(195).chr(138) => 'E', chr(195).chr(139) => 'E',
  452. chr(195).chr(140) => 'I', chr(195).chr(141) => 'I',
  453. chr(195).chr(142) => 'I', chr(195).chr(143) => 'I',
  454. chr(195).chr(144) => 'D', chr(195).chr(145) => 'N',
  455. chr(195).chr(146) => 'O', chr(195).chr(147) => 'O',
  456. chr(195).chr(148) => 'O', chr(195).chr(149) => 'O',
  457. chr(195).chr(150) => 'O', chr(195).chr(153) => 'U',
  458. chr(195).chr(154) => 'U', chr(195).chr(155) => 'U',
  459. chr(195).chr(156) => 'U', chr(195).chr(157) => 'Y',
  460. chr(195).chr(158) => 'TH',chr(195).chr(159) => 's',
  461. chr(195).chr(160) => 'a', chr(195).chr(161) => 'a',
  462. chr(195).chr(162) => 'a', chr(195).chr(163) => 'a',
  463. chr(195).chr(164) => 'a', chr(195).chr(165) => 'a',
  464. chr(195).chr(166) => 'ae',chr(195).chr(167) => 'c',
  465. chr(195).chr(168) => 'e', chr(195).chr(169) => 'e',
  466. chr(195).chr(170) => 'e', chr(195).chr(171) => 'e',
  467. chr(195).chr(172) => 'i', chr(195).chr(173) => 'i',
  468. chr(195).chr(174) => 'i', chr(195).chr(175) => 'i',
  469. chr(195).chr(176) => 'd', chr(195).chr(177) => 'n',
  470. chr(195).chr(178) => 'o', chr(195).chr(179) => 'o',
  471. chr(195).chr(180) => 'o', chr(195).chr(181) => 'o',
  472. chr(195).chr(182) => 'o', chr(195).chr(184) => 'o',
  473. chr(195).chr(185) => 'u', chr(195).chr(186) => 'u',
  474. chr(195).chr(187) => 'u', chr(195).chr(188) => 'u',
  475. chr(195).chr(189) => 'y', chr(195).chr(190) => 'th',
  476. chr(195).chr(191) => 'y', chr(195).chr(152) => 'O',
  477. // Decompositions for Latin Extended-A
  478. chr(196).chr(128) => 'A', chr(196).chr(129) => 'a',
  479. chr(196).chr(130) => 'A', chr(196).chr(131) => 'a',
  480. chr(196).chr(132) => 'A', chr(196).chr(133) => 'a',
  481. chr(196).chr(134) => 'C', chr(196).chr(135) => 'c',
  482. chr(196).chr(136) => 'C', chr(196).chr(137) => 'c',
  483. chr(196).chr(138) => 'C', chr(196).chr(139) => 'c',
  484. chr(196).chr(140) => 'C', chr(196).chr(141) => 'c',
  485. chr(196).chr(142) => 'D', chr(196).chr(143) => 'd',
  486. chr(196).chr(144) => 'D', chr(196).chr(145) => 'd',
  487. chr(196).chr(146) => 'E', chr(196).chr(147) => 'e',
  488. chr(196).chr(148) => 'E', chr(196).chr(149) => 'e',
  489. chr(196).chr(150) => 'E', chr(196).chr(151) => 'e',
  490. chr(196).chr(152) => 'E', chr(196).chr(153) => 'e',
  491. chr(196).chr(154) => 'E', chr(196).chr(155) => 'e',
  492. chr(196).chr(156) => 'G', chr(196).chr(157) => 'g',
  493. chr(196).chr(158) => 'G', chr(196).chr(159) => 'g',
  494. chr(196).chr(160) => 'G', chr(196).chr(161) => 'g',
  495. chr(196).chr(162) => 'G', chr(196).chr(163) => 'g',
  496. chr(196).chr(164) => 'H', chr(196).chr(165) => 'h',
  497. chr(196).chr(166) => 'H', chr(196).chr(167) => 'h',
  498. chr(196).chr(168) => 'I', chr(196).chr(169) => 'i',
  499. chr(196).chr(170) => 'I', chr(196).chr(171) => 'i',
  500. chr(196).chr(172) => 'I', chr(196).chr(173) => 'i',
  501. chr(196).chr(174) => 'I', chr(196).chr(175) => 'i',
  502. chr(196).chr(176) => 'I', chr(196).chr(177) => 'i',
  503. chr(196).chr(178) => 'IJ',chr(196).chr(179) => 'ij',
  504. chr(196).chr(180) => 'J', chr(196).chr(181) => 'j',
  505. chr(196).chr(182) => 'K', chr(196).chr(183) => 'k',
  506. chr(196).chr(184) => 'k', chr(196).chr(185) => 'L',
  507. chr(196).chr(186) => 'l', chr(196).chr(187) => 'L',
  508. chr(196).chr(188) => 'l', chr(196).chr(189) => 'L',
  509. chr(196).chr(190) => 'l', chr(196).chr(191) => 'L',
  510. chr(197).chr(128) => 'l', chr(197).chr(129) => 'L',
  511. chr(197).chr(130) => 'l', chr(197).chr(131) => 'N',
  512. chr(197).chr(132) => 'n', chr(197).chr(133) => 'N',
  513. chr(197).chr(134) => 'n', chr(197).chr(135) => 'N',
  514. chr(197).chr(136) => 'n', chr(197).chr(137) => 'N',
  515. chr(197).chr(138) => 'n', chr(197).chr(139) => 'N',
  516. chr(197).chr(140) => 'O', chr(197).chr(141) => 'o',
  517. chr(197).chr(142) => 'O', chr(197).chr(143) => 'o',
  518. chr(197).chr(144) => 'O', chr(197).chr(145) => 'o',
  519. chr(197).chr(146) => 'OE',chr(197).chr(147) => 'oe',
  520. chr(197).chr(148) => 'R',chr(197).chr(149) => 'r',
  521. chr(197).chr(150) => 'R',chr(197).chr(151) => 'r',
  522. chr(197).chr(152) => 'R',chr(197).chr(153) => 'r',
  523. chr(197).chr(154) => 'S',chr(197).chr(155) => 's',
  524. chr(197).chr(156) => 'S',chr(197).chr(157) => 's',
  525. chr(197).chr(158) => 'S',chr(197).chr(159) => 's',
  526. chr(197).chr(160) => 'S', chr(197).chr(161) => 's',
  527. chr(197).chr(162) => 'T', chr(197).chr(163) => 't',
  528. chr(197).chr(164) => 'T', chr(197).chr(165) => 't',
  529. chr(197).chr(166) => 'T', chr(197).chr(167) => 't',
  530. chr(197).chr(168) => 'U', chr(197).chr(169) => 'u',
  531. chr(197).chr(170) => 'U', chr(197).chr(171) => 'u',
  532. chr(197).chr(172) => 'U', chr(197).chr(173) => 'u',
  533. chr(197).chr(174) => 'U', chr(197).chr(175) => 'u',
  534. chr(197).chr(176) => 'U', chr(197).chr(177) => 'u',
  535. chr(197).chr(178) => 'U', chr(197).chr(179) => 'u',
  536. chr(197).chr(180) => 'W', chr(197).chr(181) => 'w',
  537. chr(197).chr(182) => 'Y', chr(197).chr(183) => 'y',
  538. chr(197).chr(184) => 'Y', chr(197).chr(185) => 'Z',
  539. chr(197).chr(186) => 'z', chr(197).chr(187) => 'Z',
  540. chr(197).chr(188) => 'z', chr(197).chr(189) => 'Z',
  541. chr(197).chr(190) => 'z', chr(197).chr(191) => 's',
  542. // Decompositions for Latin Extended-B
  543. chr(200).chr(152) => 'S', chr(200).chr(153) => 's',
  544. chr(200).chr(154) => 'T', chr(200).chr(155) => 't',
  545. // Euro Sign
  546. chr(226).chr(130).chr(172) => 'E',
  547. // GBP (Pound) Sign
  548. chr(194).chr(163) => '',
  549. // Vowels with diacritic (Vietnamese)
  550. // unmarked
  551. chr(198).chr(160) => 'O', chr(198).chr(161) => 'o',
  552. chr(198).chr(175) => 'U', chr(198).chr(176) => 'u',
  553. // grave accent
  554. chr(225).chr(186).chr(166) => 'A', chr(225).chr(186).chr(167) => 'a',
  555. chr(225).chr(186).chr(176) => 'A', chr(225).chr(186).chr(177) => 'a',
  556. chr(225).chr(187).chr(128) => 'E', chr(225).chr(187).chr(129) => 'e',
  557. chr(225).chr(187).chr(146) => 'O', chr(225).chr(187).chr(147) => 'o',
  558. chr(225).chr(187).chr(156) => 'O', chr(225).chr(187).chr(157) => 'o',
  559. chr(225).chr(187).chr(170) => 'U', chr(225).chr(187).chr(171) => 'u',
  560. chr(225).chr(187).chr(178) => 'Y', chr(225).chr(187).chr(179) => 'y',
  561. // hook
  562. chr(225).chr(186).chr(162) => 'A', chr(225).chr(186).chr(163) => 'a',
  563. chr(225).chr(186).chr(168) => 'A', chr(225).chr(186).chr(169) => 'a',
  564. chr(225).chr(186).chr(178) => 'A', chr(225).chr(186).chr(179) => 'a',
  565. chr(225).chr(186).chr(186) => 'E', chr(225).chr(186).chr(187) => 'e',
  566. chr(225).chr(187).chr(130) => 'E', chr(225).chr(187).chr(131) => 'e',
  567. chr(225).chr(187).chr(136) => 'I', chr(225).chr(187).chr(137) => 'i',
  568. chr(225).chr(187).chr(142) => 'O', chr(225).chr(187).chr(143) => 'o',
  569. chr(225).chr(187).chr(148) => 'O', chr(225).chr(187).chr(149) => 'o',
  570. chr(225).chr(187).chr(158) => 'O', chr(225).chr(187).chr(159) => 'o',
  571. chr(225).chr(187).chr(166) => 'U', chr(225).chr(187).chr(167) => 'u',
  572. chr(225).chr(187).chr(172) => 'U', chr(225).chr(187).chr(173) => 'u',
  573. chr(225).chr(187).chr(182) => 'Y', chr(225).chr(187).chr(183) => 'y',
  574. // tilde
  575. chr(225).chr(186).chr(170) => 'A', chr(225).chr(186).chr(171) => 'a',
  576. chr(225).chr(186).chr(180) => 'A', chr(225).chr(186).chr(181) => 'a',
  577. chr(225).chr(186).chr(188) => 'E', chr(225).chr(186).chr(189) => 'e',
  578. chr(225).chr(187).chr(132) => 'E', chr(225).chr(187).chr(133) => 'e',
  579. chr(225).chr(187).chr(150) => 'O', chr(225).chr(187).chr(151) => 'o',
  580. chr(225).chr(187).chr(160) => 'O', chr(225).chr(187).chr(161) => 'o',
  581. chr(225).chr(187).chr(174) => 'U', chr(225).chr(187).chr(175) => 'u',
  582. chr(225).chr(187).chr(184) => 'Y', chr(225).chr(187).chr(185) => 'y',
  583. // acute accent
  584. chr(225).chr(186).chr(164) => 'A', chr(225).chr(186).chr(165) => 'a',
  585. chr(225).chr(186).chr(174) => 'A', chr(225).chr(186).chr(175) => 'a',
  586. chr(225).chr(186).chr(190) => 'E', chr(225).chr(186).chr(191) => 'e',
  587. chr(225).chr(187).chr(144) => 'O', chr(225).chr(187).chr(145) => 'o',
  588. chr(225).chr(187).chr(154) => 'O', chr(225).chr(187).chr(155) => 'o',
  589. chr(225).chr(187).chr(168) => 'U', chr(225).chr(187).chr(169) => 'u',
  590. // dot below
  591. chr(225).chr(186).chr(160) => 'A', chr(225).chr(186).chr(161) => 'a',
  592. chr(225).chr(186).chr(172) => 'A', chr(225).chr(186).chr(173) => 'a',
  593. chr(225).chr(186).chr(182) => 'A', chr(225).chr(186).chr(183) => 'a',
  594. chr(225).chr(186).chr(184) => 'E', chr(225).chr(186).chr(185) => 'e',
  595. chr(225).chr(187).chr(134) => 'E', chr(225).chr(187).chr(135) => 'e',
  596. chr(225).chr(187).chr(138) => 'I', chr(225).chr(187).chr(139) => 'i',
  597. chr(225).chr(187).chr(140) => 'O', chr(225).chr(187).chr(141) => 'o',
  598. chr(225).chr(187).chr(152) => 'O', chr(225).chr(187).chr(153) => 'o',
  599. chr(225).chr(187).chr(162) => 'O', chr(225).chr(187).chr(163) => 'o',
  600. chr(225).chr(187).chr(164) => 'U', chr(225).chr(187).chr(165) => 'u',
  601. chr(225).chr(187).chr(176) => 'U', chr(225).chr(187).chr(177) => 'u',
  602. chr(225).chr(187).chr(180) => 'Y', chr(225).chr(187).chr(181) => 'y',
  603. );
  604. $string = strtr($string, $chars);
  605. } else {
  606. // Assume ISO-8859-1 if not UTF-8
  607. $chars['in'] = chr(128).chr(131).chr(138).chr(142).chr(154).chr(158)
  608. .chr(159).chr(162).chr(165).chr(181).chr(192).chr(193).chr(194)
  609. .chr(195).chr(196).chr(197).chr(199).chr(200).chr(201).chr(202)
  610. .chr(203).chr(204).chr(205).chr(206).chr(207).chr(209).chr(210)
  611. .chr(211).chr(212).chr(213).chr(214).chr(216).chr(217).chr(218)
  612. .chr(219).chr(220).chr(221).chr(224).chr(225).chr(226).chr(227)
  613. .chr(228).chr(229).chr(231).chr(232).chr(233).chr(234).chr(235)
  614. .chr(236).chr(237).chr(238).chr(239).chr(241).chr(242).chr(243)
  615. .chr(244).chr(245).chr(246).chr(248).chr(249).chr(250).chr(251)
  616. .chr(252).chr(253).chr(255);
  617. $chars['out'] = "EfSZszYcYuAAAAAACEEEEIIIINOOOOOOUUUUYaaaaaaceeeeiiiinoooooouuuuyy";
  618. $string = strtr($string, $chars['in'], $chars['out']);
  619. $double_chars['in'] = array(chr(140), chr(156), chr(198), chr(208), chr(222), chr(223), chr(230), chr(240), chr(254));
  620. $double_chars['out'] = array('OE', 'oe', 'AE', 'DH', 'TH', 'ss', 'ae', 'dh', 'th');
  621. $string = str_replace($double_chars['in'], $double_chars['out'], $string);
  622. }
  623. return $string;
  624. }
  625. endif;
  626. // ! function sanitize_file_name()
  627. if ( !function_exists('sanitize_user') ) :
  628. /**
  629. * Sanitize username stripping out unsafe characters.
  630. *
  631. * Removes tags, octets, entities, and if strict is enabled, will only keep
  632. * alphanumeric, _, space, ., -, @. After sanitizing, it passes the username,
  633. * raw username (the username in the parameter), and the value of $strict as
  634. * parameters for the 'sanitize_user' filter.
  635. *
  636. * @since 2.0.0
  637. * @uses apply_filters() Calls 'sanitize_user' hook on username, raw username,
  638. * and $strict parameter.
  639. *
  640. * @param string $username The username to be sanitized.
  641. * @param bool $strict If set limits $username to specific characters. Default false.
  642. * @return string The sanitized username, after passing through filters.
  643. */
  644. function sanitize_user( $username, $strict = false ) {
  645. $raw_username = $username;
  646. $username = wp_strip_all_tags( $username );
  647. $username = remove_accents( $username );
  648. // Kill octets
  649. $username = preg_replace( '|%([a-fA-F0-9][a-fA-F0-9])|', '', $username );
  650. $username = preg_replace( '/&.+?;/', '', $username ); // Kill entities
  651. // If strict, reduce to ASCII for max portability.
  652. if ( $strict )
  653. $username = preg_replace( '|[^a-z0-9 _.\-@]|i', '', $username );
  654. $username = trim( $username );
  655. // Consolidate contiguous whitespace
  656. $username = preg_replace( '|\s+|', ' ', $username );
  657. return apply_filters( 'sanitize_user', $username, $raw_username, $strict );
  658. }
  659. endif;
  660. if ( !function_exists('sanitize_key') ) :
  661. /**
  662. * Sanitize a string key.
  663. *
  664. * Keys are used as internal identifiers. Lowercase alphanumeric characters, dashes and underscores are allowed.
  665. *
  666. * @since 3.0.0
  667. *
  668. * @param string $key String key
  669. * @return string Sanitized key
  670. */
  671. function sanitize_key( $key ) {
  672. $raw_key = $key;
  673. $key = strtolower( $key );
  674. $key = preg_replace( '/[^a-z0-9_\-]/', '', $key );
  675. return apply_filters( 'sanitize_key', $key, $raw_key );
  676. }
  677. endif;
  678. if ( !function_exists('sanitize_title') ) :
  679. /**
  680. * Sanitizes title or use fallback title.
  681. *
  682. * Specifically, HTML and PHP tags are stripped. Further actions can be added
  683. * via the plugin API. If $title is empty and $fallback_title is set, the latter
  684. * will be used.
  685. *
  686. * @since 1.0.0
  687. *
  688. * @param string $title The string to be sanitized.
  689. * @param string $fallback_title Optional. A title to use if $title is empty.
  690. * @param string $context Optional. The operation for which the string is sanitized
  691. * @return string The sanitized string.
  692. */
  693. function sanitize_title($title, $fallback_title = '', $context = 'save') {
  694. $raw_title = $title;
  695. if ( 'save' == $context )
  696. $title = remove_accents($title);
  697. $title = apply_filters('sanitize_title', $title, $raw_title, $context);
  698. if ( '' === $title || false === $title )
  699. $title = $fallback_title;
  700. return $title;
  701. }
  702. endif;
  703. //! function sanitize_title_for_query
  704. if ( !function_exists('sanitize_title_with_dashes') ) :
  705. /**
  706. * Sanitizes title, replacing whitespace and a few other characters with dashes.
  707. *
  708. * Limits the output to alphanumeric characters, underscore (_) and dash (-).
  709. * Whitespace becomes a dash.
  710. *
  711. * @since 1.2.0
  712. *
  713. * @param string $title The title to be sanitized.
  714. * @param string $raw_title Optional. Not used.
  715. * @param string $context Optional. The operation for which the string is sanitized.
  716. * @return string The sanitized title.
  717. */
  718. function sanitize_title_with_dashes($title, $raw_title = '', $context = 'display') {
  719. $title = strip_tags($title);
  720. // Preserve escaped octets.
  721. $title = preg_replace('|%([a-fA-F0-9][a-fA-F0-9])|', '---$1---', $title);
  722. // Remove percent signs that are not part of an octet.
  723. $title = str_replace('%', '', $title);
  724. // Restore octets.
  725. $title = preg_replace('|---([a-fA-F0-9][a-fA-F0-9])---|', '%$1', $title);
  726. if (seems_utf8($title)) {
  727. if (function_exists('mb_strtolower')) {
  728. $title = mb_strtolower($title, 'UTF-8');
  729. }
  730. $title = utf8_uri_encode($title, 200);
  731. }
  732. $title = strtolower($title);
  733. $title = preg_replace('/&.+?;/', '', $title); // kill entities
  734. $title = str_replace('.', '-', $title);
  735. if ( 'save' == $context ) {
  736. // Convert nbsp, ndash and mdash to hyphens
  737. $title = str_replace( array( '%c2%a0', '%e2%80%93', '%e2%80%94' ), '-', $title );
  738. // Strip these characters entirely
  739. $title = str_replace( array(
  740. // iexcl and iquest
  741. '%c2%a1', '%c2%bf',
  742. // angle quotes
  743. '%c2%ab', '%c2%bb', '%e2%80%b9', '%e2%80%ba',
  744. // curly quotes
  745. '%e2%80%98', '%e2%80%99', '%e2%80%9c', '%e2%80%9d',
  746. '%e2%80%9a', '%e2%80%9b', '%e2%80%9e', '%e2%80%9f',
  747. // copy, reg, deg, hellip and trade
  748. '%c2%a9', '%c2%ae', '%c2%b0', '%e2%80%a6', '%e2%84%a2',
  749. ), '', $title );
  750. // Convert times to x
  751. $title = str_replace( '%c3%97', 'x', $title );
  752. }
  753. $title = preg_replace('/[^%a-z0-9 _-]/', '', $title);
  754. $title = preg_replace('/\s+/', '-', $title);
  755. $title = preg_replace('|-+|', '-', $title);
  756. $title = trim($title, '-');
  757. return $title;
  758. }
  759. endif;
  760. // ! function sanitize_sql_orderby()
  761. // ! function sanitize_html_class()
  762. // ! function convert_chars()
  763. // ! function balanceTags()
  764. if ( !function_exists( 'force_balance_tags' ) ) :
  765. /**
  766. * Balances tags of string using a modified stack.
  767. *
  768. * @since 2.0.4
  769. *
  770. * @author Leonard Lin <leonard@acm.org>
  771. * @license GPL
  772. * @copyright November 4, 2001
  773. * @version 1.1
  774. * @todo Make better - change loop condition to $text in 1.2
  775. * @internal Modified by Scott Reilly (coffee2code) 02 Aug 2004
  776. * 1.1 Fixed handling of append/stack pop order of end text
  777. * Added Cleaning Hooks
  778. * 1.0 First Version
  779. *
  780. * @param string $text Text to be balanced.
  781. * @return string Balanced text.
  782. */
  783. function force_balance_tags( $text ) {
  784. $tagstack = array();
  785. $stacksize = 0;
  786. $tagqueue = '';
  787. $newtext = '';
  788. $single_tags = array( 'br', 'hr', 'img', 'input' ); // Known single-entity/self-closing tags
  789. $nestable_tags = array( 'blockquote', 'div', 'span', 'q' ); // Tags that can be immediately nested within themselves
  790. // WP bug fix for comments - in case you REALLY meant to type '< !--'
  791. $text = str_replace('< !--', '< !--', $text);
  792. // WP bug fix for LOVE <3 (and other situations with '<' before a number)
  793. $text = preg_replace('#<([0-9]{1})#', '&lt;$1', $text);
  794. while ( preg_match("/<(\/?[\w:]*)\s*([^>]*)>/", $text, $regex) ) {
  795. $newtext .= $tagqueue;
  796. $i = strpos($text, $regex[0]);
  797. $l = strlen($regex[0]);
  798. // clear the shifter
  799. $tagqueue = '';
  800. // Pop or Push
  801. if ( isset($regex[1][0]) && '/' == $regex[1][0] ) { // End Tag
  802. $tag = strtolower(substr($regex[1],1));
  803. // if too many closing tags
  804. if( $stacksize <= 0 ) {
  805. $tag = '';
  806. // or close to be safe $tag = '/' . $tag;
  807. }
  808. // if stacktop value = tag close value then pop
  809. else if ( $tagstack[$stacksize - 1] == $tag ) { // found closing tag
  810. $tag = '</' . $tag . '>'; // Close Tag
  811. // Pop
  812. array_pop( $tagstack );
  813. $stacksize--;
  814. } else { // closing tag not at top, search for it
  815. for ( $j = $stacksize-1; $j >= 0; $j-- ) {
  816. if ( $tagstack[$j] == $tag ) {
  817. // add tag to tagqueue
  818. for ( $k = $stacksize-1; $k >= $j; $k--) {
  819. $tagqueue .= '</' . array_pop( $tagstack ) . '>';
  820. $stacksize--;
  821. }
  822. break;
  823. }
  824. }
  825. $tag = '';
  826. }
  827. } else { // Begin Tag
  828. $tag = strtolower($regex[1]);
  829. // Tag Cleaning
  830. // If self-closing or '', don't do anything.
  831. if ( substr($regex[2],-1) == '/' || $tag == '' ) {
  832. // do nothing
  833. }
  834. // ElseIf it's a known single-entity tag but it doesn't close itself, do so
  835. elseif ( in_array($tag, $single_tags) ) {
  836. $regex[2] .= '/';
  837. } else { // Push the tag onto the stack
  838. // If the top of the stack is the same as the tag we want to push, close previous tag
  839. if ( $stacksize > 0 && !in_array($tag, $nestable_tags) && $tagstack[$stacksize - 1] == $tag ) {
  840. $tagqueue = '</' . array_pop ($tagstack) . '>';
  841. $stacksize--;
  842. }
  843. $stacksize = array_push ($tagstack, $tag);
  844. }
  845. // Attributes
  846. $attributes = $regex[2];
  847. if( !empty($attributes) )
  848. $attributes = ' '.$attributes;
  849. $tag = '<' . $tag . $attributes . '>';
  850. //If already queuing a close tag, then put this tag on, too
  851. if ( !empty($tagqueue) ) {
  852. $tagqueue .= $tag;
  853. $tag = '';
  854. }
  855. }
  856. $newtext .= substr($text, 0, $i) . $tag;
  857. $text = substr($text, $i + $l);
  858. }
  859. // Clear Tag Queue
  860. $newtext .= $tagqueue;
  861. // Add Remaining text
  862. $newtext .= $text;
  863. // Empty Stack
  864. while( $x = array_pop($tagstack) )
  865. $newtext .= '</' . $x . '>'; // Add remaining tags to close
  866. // WP fix for the bug with HTML comments
  867. $newtext = str_replace("< !--","<!--",$newtext);
  868. $newtext = str_replace("< !--","< !--",$newtext);
  869. return $newtext;
  870. }
  871. endif;
  872. if ( !function_exists('format_to_edit') ) :
  873. /**
  874. * Acts on text which is about to be edited.
  875. *
  876. * The $content is run through esc_textarea(), which uses htmlspecialchars()
  877. * to convert special characters to HTML entities. If $richedit is set to true,
  878. * it is simply a holder for the 'format_to_edit' filter.
  879. *
  880. * @since 0.71
  881. *
  882. * @param string $content The text about to be edited.
  883. * @param bool $richedit Whether the $content should not pass through htmlspecialchars(). Default false (meaning it will be passed).
  884. * @return string The text after the filter (and possibly htmlspecialchars()) has been run.
  885. */
  886. function format_to_edit( $content, $richedit = false ) {
  887. $content = apply_filters( 'format_to_edit', $content );
  888. if ( ! $richedit )
  889. $content = esc_textarea( $content );
  890. return $content;
  891. }
  892. endif;
  893. // !format_to_post()
  894. if ( !function_exists( 'zeroise' ) ) :
  895. /**
  896. * Add leading zeros when necessary.
  897. *
  898. * If you set the threshold to '4' and the number is '10', then you will get
  899. * back '0010'. If you set the threshold to '4' and the number is '5000', then you
  900. * will get back '5000'.
  901. *
  902. * Uses sprintf to append the amount of zeros based on the $threshold parameter
  903. * and the size of the number. If the number is large enough, then no zeros will
  904. * be appended.
  905. *
  906. * @since 0.71
  907. *
  908. * @param mixed $number Number to append zeros to if not greater than threshold.
  909. * @param int $threshold Digit places number needs to be to not have zeros added.
  910. * @return string Adds leading zeros to number if needed.
  911. */
  912. function zeroise($number, $threshold) {
  913. return sprintf('%0'.$threshold.'s', $number);
  914. }
  915. endif;
  916. if ( !function_exists( 'backslashit' ) ) :
  917. /**
  918. * Adds backslashes before letters and before a number at the start of a string.
  919. *
  920. * @since 0.71
  921. *
  922. * @param string $string Value to which backslashes will be added.
  923. * @return string String with backslashes inserted.
  924. */
  925. function backslashit($string) {
  926. $string = preg_replace('/^([0-9])/', '\\\\\\\\\1', $string);
  927. $string = preg_replace('/([a-z])/i', '\\\\\1', $string);
  928. return $string;
  929. }
  930. endif;
  931. if ( !function_exists( 'trailingslashit' ) ) :
  932. /**
  933. * Appends a trailing slash.
  934. *
  935. * Will remove trailing slash if it exists already before adding a trailing
  936. * slash. This prevents double slashing a string or path.
  937. *
  938. * The primary use of this is for paths and thus should be used for paths. It is
  939. * not restricted to paths and offers no specific path support.
  940. *
  941. * @since 1.2.0
  942. * @uses untrailingslashit() Unslashes string if it was slashed already.
  943. *
  944. * @param string $string What to add the trailing slash to.
  945. * @return string String with trailing slash added.
  946. */
  947. function trailingslashit($string) {
  948. return untrailingslashit($string) . '/';
  949. }
  950. endif;
  951. if ( !function_exists( 'untrailingslashit' ) ) :
  952. /**
  953. * Removes trailing slash if it exists.
  954. *
  955. * The primary use of this is for paths and thus should be used for paths. It is
  956. * not restricted to paths and offers no specific path support.
  957. *
  958. * @since 2.2.0
  959. *
  960. * @param string $string What to remove the trailing slash from.
  961. * @return string String without the trailing slash.
  962. */
  963. function untrailingslashit($string) {
  964. return rtrim($string, '/');
  965. }
  966. endif;
  967. // ! function addslashes_gpc()
  968. if ( !function_exists('stripslashes_deep') ) :
  969. /**
  970. * Navigates through an array and removes slashes from the values.
  971. *
  972. * If an array is passed, the array_map() function causes a callback to pass the
  973. * value back to the function. The slashes from this value will removed.
  974. *
  975. * @since 2.0.0
  976. *
  977. * @param array|string $value The array or string to be stripped.
  978. * @return array|string Stripped array (or string in the callback).
  979. */
  980. function stripslashes_deep($value) {
  981. if ( is_array($value) ) {
  982. $value = array_map('stripslashes_deep', $value);
  983. } elseif ( is_object($value) ) {
  984. $vars = get_object_vars( $value );
  985. foreach ($vars as $key=>$data) {
  986. $value->{$key} = stripslashes_deep( $data );
  987. }
  988. } else {
  989. $value = stripslashes($value);
  990. }
  991. return $value;
  992. }
  993. endif;
  994. if ( !function_exists( 'rawurlencode_deep' ) ) :
  995. /**
  996. * Navigates through an array and raw encodes the values to be used in a URL.
  997. *
  998. * @since 3.4.0
  999. *
  1000. * @param array|string $value The array or string to be encoded.
  1001. * @return array|string $value The encoded array (or string from the callback).
  1002. */
  1003. function rawurlencode_deep( $value ) {
  1004. return is_array( $value ) ? array_map( 'rawurlencode_deep', $value ) : rawurlencode( $value );
  1005. }
  1006. endif;
  1007. if ( !function_exists( 'urlencode_deep' ) ) :
  1008. /**
  1009. * Navigates through an array and encodes the values to be used in a URL.
  1010. *
  1011. * Uses a callback to pass the value of the array back to the function as a
  1012. * string.
  1013. *
  1014. * @since 2.2.0
  1015. *
  1016. * @param array|string $value The array or string to be encoded.
  1017. * @return array|string $value The encoded array (or string from the callback).
  1018. */
  1019. function urlencode_deep($value) {
  1020. $value = is_array($value) ? array_map('urlencode_deep', $value) : urlencode($value);
  1021. return $value;
  1022. }
  1023. endif;
  1024. // ! function antispambot()
  1025. if ( !function_exists( '_make_url_clickable_cb' ) ) :
  1026. /**
  1027. * Callback to convert URI match to HTML A element.
  1028. *
  1029. * This function was backported from 2.5.0 to 2.3.2. Regex callback for {@link
  1030. * make_clickable()}.
  1031. *
  1032. * @since 2.3.2
  1033. * @access private
  1034. *
  1035. * @param array $matches Single Regex Match.
  1036. * @return string HTML A element with URI address.
  1037. */
  1038. function _make_url_clickable_cb($matches) {
  1039. $url = $matches[2];
  1040. if ( ')' == $matches[3] && strpos( $url, '(' ) ) {
  1041. // If the trailing character is a closing parethesis, and the URL has an opening parenthesis in it, add the closing parenthesis to the URL.
  1042. // Then we can let the parenthesis balancer do its thing below.
  1043. $url .= $matches[3];
  1044. $suffix = '';
  1045. } else {
  1046. $suffix = $matches[3];
  1047. }
  1048. // Include parentheses in the URL only if paired
  1049. while ( substr_count( $url, '(' ) < substr_count( $url, ')' ) ) {
  1050. $suffix = strrchr( $url, ')' ) . $suffix;
  1051. $url = substr( $url, 0, strrpos( $url, ')' ) );
  1052. }
  1053. $url = esc_url($url);
  1054. if ( empty($url) )
  1055. return $matches[0];
  1056. return $matches[1] . "<a href=\"$url\" rel=\"nofollow\">$url</a>" . $suffix;
  1057. }
  1058. endif;
  1059. if ( !function_exists( '_make_web_ftp_clickable_cb' ) ) :
  1060. /**
  1061. * Callback to convert URL match to HTML A element.
  1062. *
  1063. * This function was backported from 2.5.0 to 2.3.2. Regex callback for {@link
  1064. * make_clickable()}.
  1065. *
  1066. * @since 2.3.2
  1067. * @access private
  1068. *
  1069. * @param array $matches Single Regex Match.
  1070. * @return string HTML A element with URL address.
  1071. */
  1072. function _make_web_ftp_clickable_cb($matches) {
  1073. $ret = '';
  1074. $dest = $matches[2];
  1075. $dest = 'http://' . $dest;
  1076. $dest = esc_url($dest);
  1077. if ( empty($dest) )
  1078. return $matches[0];
  1079. // removed trailing [.,;:)] from URL
  1080. if ( in_array( substr($dest, -1), array('.', ',', ';', ':', ')') ) === true ) {
  1081. $ret = substr($dest, -1);
  1082. $dest = substr($dest, 0, strlen($dest)-1);
  1083. }
  1084. return $matches[1] . "<a href=\"$dest\" rel=\"nofollow\">$dest</a>$ret";
  1085. }
  1086. endif;
  1087. if ( !function_exists( '_make_email_clickable_cb' ) ) :
  1088. /**
  1089. * Callback to convert email address match to HTML A element.
  1090. *
  1091. * This function was backported from 2.5.0 to 2.3.2. Regex callback for {@link
  1092. * make_clickable()}.
  1093. *
  1094. * @since 2.3.2
  1095. * @access private
  1096. *
  1097. * @param array $matches Single Regex Match.
  1098. * @return string HTML A element with email address.
  1099. */
  1100. function _make_email_clickable_cb($matches) {
  1101. $email = $matches[2] . '@' . $matches[3];
  1102. return $matches[1] . "<a href=\"mailto:$email\">$email</a>";
  1103. }
  1104. endif;
  1105. if ( !function_exists( 'make_clickable' ) ) :
  1106. /**
  1107. * Convert plaintext URI to HTML links.
  1108. *
  1109. * Converts URI, www and ftp, and email addresses. Finishes by fixing links
  1110. * within links.
  1111. *
  1112. * @since 0.71
  1113. *
  1114. * @param string $text Content to convert URIs.
  1115. * @return string Content with converted URIs.
  1116. */
  1117. function make_clickable( $text ) {
  1118. $r = '';
  1119. $textarr = preg_split( '/(<[^<>]+>)/', $text, -1, PREG_SPLIT_DELIM_CAPTURE ); // split out HTML tags
  1120. foreach ( $textarr as $piece ) {
  1121. if ( empty( $piece ) || ( $piece[0] == '<' && ! preg_match('|^<\s*[\w]{1,20}+://|', $piece) ) ) {
  1122. $r .= $piece;
  1123. continue;
  1124. }
  1125. // Long strings might contain expensive edge cases ...
  1126. if ( 10000 < strlen( $piece ) ) {
  1127. // ... break it up
  1128. foreach ( _split_str_by_whitespace( $piece, 2100 ) as $chunk ) { // 2100: Extra room for scheme and leading and trailing paretheses
  1129. if ( 2101 < strlen( $chunk ) ) {
  1130. $r .= $chunk; // Too big, no whitespace: bail.
  1131. } else {
  1132. $r .= make_clickable( $chunk );
  1133. }
  1134. }
  1135. } else {
  1136. $ret = " $piece "; // Pad with whitespace to simplify the regexes
  1137. $url_clickable = '~
  1138. ([\\s(<.,;:!?]) # 1: Leading whitespace, or punctuation
  1139. ( # 2: URL
  1140. [\\w]{1,20}+:// # Scheme and hier-part prefix
  1141. (?=\S{1,2000}\s) # Limit to URLs less than about 2000 characters long
  1142. [\\w\\x80-\\xff#%\\~/@\\[\\]*(+=&$-]*+ # Non-punctuation URL character
  1143. (?: # Unroll the Loop: Only allow puctuation URL character if followed by a non-punctuation URL character
  1144. [\'.,;:!?)] # Punctuation URL character
  1145. [\\w\\x80-\\xff#%\\~/@\\[\\]*(+=&$-]++ # Non-punctuation URL character
  1146. )*
  1147. )
  1148. (\)?) # 3: Trailing closing parenthesis (for parethesis balancing post processing)
  1149. ~xS'; // The regex is a non-anchored pattern and does not have a single fixed starting character.
  1150. // Tell PCRE to spend more time optimizing since, when used on a page load, it will probably be used several times.
  1151. $ret = preg_replace_callback( $url_clickable, '_make_url_clickable_cb', $ret );
  1152. $ret = preg_replace_callback( '#([\s>])((www|ftp)\.[\w\\x80-\\xff\#$%&~/.\-;:=,?@\[\]+]+)#is', '_make_web_ftp_clickable_cb', $ret );
  1153. $ret = preg_replace_callback( '#([\s>])([.0-9a-z_+-]+)@(([0-9a-z-]+\.)+[0-9a-z]{2,})#i', '_make_email_clickable_cb', $ret );
  1154. $ret = substr( $ret, 1, -1 ); // Remove our whitespace padding.
  1155. $r .= $ret;
  1156. }
  1157. }
  1158. // Cleanup of accidental links within links
  1159. $r = preg_replace( '#(<a( [^>]+?>|>))<a [^>]+?>([^>]+?)</a></a>#i', "$1$3</a>", $r );
  1160. return $r;
  1161. }
  1162. endif;
  1163. if ( !function_exists('_split_str_by_whitespace') ) :
  1164. /**
  1165. * Breaks a string into chunks by splitting at whitespace characters.
  1166. * The length of each returned chunk is as close to the specified length goal as possible,
  1167. * with the caveat that each chunk includes its trailing delimiter.
  1168. * Chunks longer than the goal are guaranteed to not have any inner whitespace.
  1169. *
  1170. * Joining the returned chunks with empty delimiters reconstructs the input string losslessly.
  1171. *
  1172. * Input string must have no null characters (or eventual transformations on output chunks must not care about null characters)
  1173. *
  1174. * <code>
  1175. * _split_str_by_whitespace( "1234 67890 1234 67890a cd 1234 890 123456789 1234567890a 45678 1 3 5 7 90 ", 10 ) ==
  1176. * array (
  1177. * 0 => '1234 67890 ', // 11 characters: Perfect split
  1178. * 1 => '1234 ', // 5 characters: '1234 67890a' was too long
  1179. * 2 => '67890a cd ', // 10 characters: '67890a cd 1234' was too long
  1180. * 3 => '1234 890 ', // 11 characters: Perfect split
  1181. * 4 => '123456789 ', // 10 characters: '123456789 1234567890a' was too long
  1182. * 5 => '1234567890a ', // 12 characters: Too long, but no inner whitespace on which to split
  1183. * 6 => ' 45678 ', // 11 characters: Perfect split
  1184. * 7 => '1 3 5 7 9', // 9 characters: End of $string
  1185. * );
  1186. * </code>
  1187. *
  1188. * @since 3.4.0
  1189. * @access private
  1190. *
  1191. * @param string $string The string to split
  1192. * @param int $goal The desired chunk length.
  1193. * @return array Numeric array of chunks.
  1194. */
  1195. function _split_str_by_whitespace( $string, $goal ) {
  1196. $chunks = array();
  1197. $string_nullspace = strtr( $string, "\r\n\t\v\f ", "\000\000\000\000\000\000" );
  1198. while ( $goal < strlen( $string_nullspace ) ) {
  1199. $pos = strrpos( substr( $string_nullspace, 0, $goal + 1 ), "\000" );
  1200. if ( false === $pos ) {
  1201. $pos = strpos( $string_nullspace, "\000", $goal + 1 );
  1202. if ( false === $pos ) {
  1203. break;
  1204. }
  1205. }
  1206. $chunks[] = substr( $string, 0, $pos + 1 );
  1207. $string = substr( $string, $pos + 1 );
  1208. $string_nullspace = substr( $string_nullspace, $pos + 1 );
  1209. }
  1210. if ( $string ) {
  1211. $chunks[] = $string;
  1212. }
  1213. return $chunks;
  1214. }
  1215. endif;
  1216. // ! function wp_rel_nofollow()
  1217. // ! function wp_rel_nofollow_callback()
  1218. // ! function translate_smiley()
  1219. // ! function convert_smilies()
  1220. if ( !function_exists('is_email') ) :
  1221. /**
  1222. * Verifies that an email is valid.
  1223. *
  1224. * Does not grok i18n domains. Not RFC compliant.
  1225. *
  1226. * @since 0.71
  1227. *
  1228. * @param string $email Email address to verify.
  1229. * @param boolean $deprecated Deprecated.
  1230. * @return string|bool Either false or the valid email address.
  1231. */
  1232. function is_email( $email, $deprecated = false ) {
  1233. // Test for the minimum length the email can be
  1234. if ( strlen( $email ) < 3 ) {
  1235. return apply_filters( 'is_email', false, $email, 'email_too_short' );
  1236. }
  1237. // Test for an @ character after the first position
  1238. if ( strpos( $email, '@', 1 ) === false ) {
  1239. return apply_filters( 'is_email', false, $email, 'email_no_at' );
  1240. }
  1241. // Split out the local and domain parts
  1242. list( $local, $domain ) = explode( '@', $email, 2 );
  1243. // LOCAL PART
  1244. // Test for invalid characters
  1245. if ( !preg_match( '/^[a-zA-Z0-9!#$%&\'*+\/=?^_`{|}~\.-]+$/', $local ) ) {
  1246. return apply_filters( 'is_email', false, $email, 'local_invalid_chars' );
  1247. }
  1248. // DOMAIN PART
  1249. // Test for sequences of periods
  1250. if ( preg_match( '/\.{2,}/', $domain ) ) {
  1251. return apply_filters( 'is_email', false, $email, 'domain_period_sequence' );
  1252. }
  1253. // Test for leading and trailing periods and whitespace
  1254. if ( trim( $domain, " \t\n\r\0\x0B." ) !== $domain ) {
  1255. return apply_filters( 'is_email', false, $email, 'domain_period_limits' );
  1256. }
  1257. // Split the domain into subs
  1258. $subs = explode( '.', $domain );
  1259. // Assume the domain will have at least two subs
  1260. if ( 2 > count( $subs ) ) {
  1261. return apply_filters( 'is_email', false, $email, 'domain_no_periods' );
  1262. }
  1263. // Loop through each sub
  1264. foreach ( $subs as $sub ) {
  1265. // Test for leading and trailing hyphens and whitespace
  1266. if ( trim( $sub, " \t\n\r\0\x0B-" ) !== $sub ) {
  1267. return apply_filters( 'is_email', false, $email, 'sub_hyphen_limits' );
  1268. }
  1269. // Test for invalid characters
  1270. if ( !preg_match('/^[a-z0-9-]+$/i', $sub ) ) {
  1271. return apply_filters( 'is_email', false, $email, 'sub_invalid_chars' );
  1272. }
  1273. }
  1274. // Congratulations your email made it!
  1275. return apply_filters( 'is_email', $email, $email, null );
  1276. }
  1277. endif;
  1278. // ! function wp_iso_descrambler()
  1279. // ! function _wp_iso_convert()
  1280. // ! function get_gmt_from_date()
  1281. // ! function get_date_from_gmt()
  1282. // ! function iso8601_timezone_to_offset()
  1283. // ! function iso8601_to_datetime()
  1284. // ! popuplinks()
  1285. if ( !function_exists('sanitize_email') ) :
  1286. /**
  1287. * Strips out all characters that are not allowable in an email.
  1288. *
  1289. * @since 1.5.0
  1290. *
  1291. * @param string $email Email address to filter.
  1292. * @return string Filtered email address.
  1293. */
  1294. function sanitize_email( $email ) {
  1295. // Test for the minimum length the email can be
  1296. if ( strlen( $email ) < 3 ) {
  1297. return apply_filters( 'sanitize_email', '', $email, 'email_too_short' );
  1298. }
  1299. // Test for an @ character after the first position
  1300. if ( strpos( $email, '@', 1 ) === false ) {
  1301. return apply_filters( 'sanitize_email', '', $email, 'email_no_at' );
  1302. }
  1303. // Split out the local and domain parts
  1304. list( $local, $domain ) = explode( '@', $email, 2 );
  1305. // LOCAL PART
  1306. // Test for invalid characters
  1307. $local = preg_replace( '/[^a-zA-Z0-9!#$%&\'*+\/=?^_`{|}~\.-]/', '', $local );
  1308. if ( '' === $local ) {
  1309. return apply_filters( 'sanitize_email', '', $email, 'local_invalid_chars' );
  1310. }
  1311. // DOMAIN PART
  1312. // Test for sequences of periods
  1313. $domain = preg_replace( '/\.{2,}/', '', $domain );
  1314. if ( '' === $domain ) {
  1315. return apply_filters( 'sanitize_email', '', $email, 'domain_period_sequence' );
  1316. }
  1317. // Test for leading and trailing periods and whitespace
  1318. $domain = trim( $domain, " \t\n\r\0\x0B." );
  1319. if ( '' === $domain ) {
  1320. return apply_filters( 'sanitize_email', '', $email, 'domain_period_limits' );
  1321. }
  1322. // Split the domain into subs
  1323. $subs = explode( '.', $domain );
  1324. // Assume the domain will have at least two subs
  1325. if ( 2 > count( $subs ) ) {
  1326. return apply_filters( 'sanitize_email', '', $email, 'domain_no_periods' );
  1327. }
  1328. // Create an array that will contain valid subs
  1329. $new_subs = array();
  1330. // Loop through each sub
  1331. foreach ( $subs as $sub ) {
  1332. // Test for leading and trailing hyphens
  1333. $sub = trim( $sub, " \t\n\r\0\x0B-" );
  1334. // Test for invalid characters
  1335. $sub = preg_replace( '/^[^a-z0-9-]+$/i', '', $sub );
  1336. // If there's anything left, add it to the valid subs
  1337. if ( '' !== $sub ) {
  1338. $new_subs[] = $sub;
  1339. }
  1340. }
  1341. // If there aren't 2 or more valid subs
  1342. if ( 2 > count( $new_subs ) ) {
  1343. return apply_filters( 'sanitize_email', '', $email, 'domain_no_valid_subs' );
  1344. }
  1345. // Join valid subs into the new domain
  1346. $domain = join( '.', $new_subs );
  1347. // Put the email back together
  1348. $email = $local . '@' . $domain;
  1349. // Congratulations your email made it!
  1350. return apply_filters( 'sanitize_email', $email, $email, null );
  1351. }
  1352. endif;
  1353. // ! function human_time_diff()
  1354. // ! function wp_trim_excerpt()
  1355. // ! function wp_trim_excerpt()
  1356. if ( !function_exists( 'ent2ncr' ) ) : // Current at [WP9840]
  1357. /**
  1358. * Converts named entities into numbered entities.
  1359. *
  1360. * @since 1.5.1
  1361. *
  1362. * @param string $text The text within which entities will be converted.
  1363. * @return string Text with converted entities.
  1364. */
  1365. function ent2ncr($text) {
  1366. // Allow a plugin to short-circuit and override the mappings.
  1367. $filtered = apply_filters( 'pre_ent2ncr', null, $text );
  1368. if( null !== $filtered )
  1369. return $filtered;
  1370. $to_ncr = array(
  1371. '&quot;' => '&#34;',
  1372. '&amp;' => '&#38;',
  1373. '&frasl;' => '&#47;',
  1374. '&lt;' => '&#60;',
  1375. '&gt;' => '&#62;',
  1376. '|' => '&#124;',
  1377. '&nbsp;' => '&#160;',
  1378. '&iexcl;' => '&#161;',
  1379. '&cent;' => '&#162;',
  1380. '&pound;' => '&#163;',
  1381. '&curren;' => '&#164;',
  1382. '&yen;' => '&#165;',
  1383. '&brvbar;' => '&#166;',
  1384. '&brkbar;' => '&#166;',
  1385. '&sect;' => '&#167;',
  1386. '&uml;' => '&#168;',
  1387. '&die;' => '&#168;',
  1388. '&copy;' => '&#169;',
  1389. '&ordf;' => '&#170;',
  1390. '&laquo;' => '&#171;',
  1391. '&not;' => '&#172;',
  1392. '&shy;' => '&#173;',
  1393. '&reg;' => '&#174;',
  1394. '&macr;' => '&#175;',
  1395. '&hibar;' => '&#175;',
  1396. '&deg;' => '&#176;',
  1397. '&plusmn;' => '&#177;',
  1398. '&sup2;' => '&#178;',
  1399. '&sup3;' => '&#179;',
  1400. '&acute;' => '&#180;',
  1401. '&micro;' => '&#181;',
  1402. '&para;' => '&#182;',
  1403. '&middot;' => '&#183;',
  1404. '&cedil;' => '&#184;',
  1405. '&sup1;' => '&#185;',
  1406. '&ordm;' => '&#186;',
  1407. '&raquo;' => '&#187;',
  1408. '&frac14;' => '&#188;',
  1409. '&frac12;' => '&#189;',
  1410. '&frac34;' => '&#190;',
  1411. '&iquest;' => '&#191;',
  1412. '&Agrave;' => '&#192;',
  1413. '&Aacute;' => '&#193;',
  1414. '&Acirc;' => '&#194;',
  1415. '&Atilde;' => '&#195;',
  1416. '&Auml;' => '&#196;',
  1417. '&Aring;' => '&#197;',
  1418. '&AElig;' => '&#198;',
  1419. '&Ccedil;' => '&#199;',
  1420. '&Egrave;' => '&#200;',
  1421. '&Eacute;' => '&#201;',
  1422. '&Ecirc;' => '&#202;',
  1423. '&Euml;' => '&#203;',
  1424. '&Igrave;' => '&#204;',
  1425. '&Iacute;' => '&#205;',
  1426. '&Icirc;' => '&#206;',
  1427. '&Iuml;' => '&#207;',
  1428. '&ETH;' => '&#208;',
  1429. '&Ntilde;' => '&#209;',
  1430. '&Ograve;' => '&#210;',
  1431. '&Oacute;' => '&#211;',
  1432. '&Ocirc;' => '&#212;',
  1433. '&Otilde;' => '&#213;',
  1434. '&Ouml;' => '&#214;',
  1435. '&times;' => '&#215;',
  1436. '&Oslash;' => '&#216;',
  1437. '&Ugrave;' => '&#217;',
  1438. '&Uacute;' => '&#218;',
  1439. '&Ucirc;' => '&#219;',
  1440. '&Uuml;' => '&#220;',
  1441. '&Yacute;' => '&#221;',
  1442. '&THORN;' => '&#222;',
  1443. '&szlig;' => '&#223;',
  1444. '&agrave;' => '&#224;',
  1445. '&aacute;' => '&#225;',
  1446. '&acirc;' => '&#226;',
  1447. '&atilde;' => '&#227;',
  1448. '&auml;' => '&#228;',
  1449. '&aring;' => '&#229;',
  1450. '&aelig;' => '&#230;',
  1451. '&ccedil;' => '&#231;',
  1452. '&egrave;' => '&#232;',
  1453. '&eacute;' => '&#233;',
  1454. '&ecirc;' => '&#234;',
  1455. '&euml;' => '&#235;',
  1456. '&igrave;' => '&#236;',
  1457. '&iacute;' => '&#237;',
  1458. '&icirc;' => '&#238;',
  1459. '&iuml;' => '&#239;',
  1460. '&eth;' => '&#240;',
  1461. '&ntilde;' => '&#241;',
  1462. '&ograve;' => '&#242;',
  1463. '&oacute;' => '&#243;',
  1464. '&ocirc;' => '&#244;',
  1465. '&otilde;' => '&#245;',
  1466. '&ouml;' => '&#246;',
  1467. '&divide;' => '&#247;',
  1468. '&oslash;' => '&#248;',
  1469. '&ugrave;' => '&#249;',
  1470. '&uacute;' => '&#250;',
  1471. '&ucirc;' => '&#251;',
  1472. '&uuml;' => '&#252;',
  1473. '&yacute;' => '&#253;',
  1474. '&thorn;' => '&#254;',
  1475. '&yuml;' => '&#255;',
  1476. '&OElig;' => '&#338;',
  1477. '&oelig;' => '&#339;',
  1478. '&Scaron;' => '&#352;',
  1479. '&scaron;' => '&#353;',
  1480. '&Yuml;' => '&#376;',
  1481. '&fnof;' => '&#402;',
  1482. '&circ;' => '&#710;',
  1483. '&tilde;' => '&#732;',
  1484. '&Alpha;' => '&#913;',
  1485. '&Beta;' => '&#914;',
  1486. '&Gamma;' => '&#915;',
  1487. '&Delta;' => '&#916;',
  1488. '&Epsilon;' => '&#917;',
  1489. '&Zeta;' => '&#918;',
  1490. '&Eta;' => '&#919;',
  1491. '&Theta;' => '&#920;',
  1492. '&Iota;' => '&#921;',
  1493. '&Kappa;' => '&#922;',
  1494. '&Lambda;' => '&#923;',
  1495. '&Mu;' => '&#924;',
  1496. '&Nu;' => '&#925;',
  1497. '&Xi;' => '&#926;',
  1498. '&Omicron;' => '&#927;',
  1499. '&Pi;' => '&#928;',
  1500. '&Rho;' => '&#929;',
  1501. '&Sigma;' => '&#931;',
  1502. '&Tau;' => '&#932;',
  1503. '&Upsilon;' => '&#933;',
  1504. '&Phi;' => '&#934;',
  1505. '&Chi;' => '&#935;',
  1506. '&Psi;' => '&#936;',
  1507. '&Omega;' => '&#937;',
  1508. '&alpha;' => '&#945;',
  1509. '&beta;' => '&#946;',
  1510. '&gamma;' => '&#947;',
  1511. '&delta;' => '&#948;',
  1512. '&epsilon;' => '&#949;',
  1513. '&zeta;' => '&#950;',
  1514. '&eta;' => '&#951;',
  1515. '&theta;' => '&#952;',
  1516. '&iota;' => '&#953;',
  1517. '&kappa;' => '&#954;',
  1518. '&lambda;' => '&#955;',
  1519. '&mu;' => '&#956;',
  1520. '&nu;' => '&#957;',
  1521. '&xi;' => '&#958;',
  1522. '&omicron;' => '&#959;',
  1523. '&pi;' => '&#960;',
  1524. '&rho;' => '&#961;',
  1525. '&sigmaf;' => '&#962;',
  1526. '&sigma;' => '&#963;',
  1527. '&tau;' => '&#964;',
  1528. '&upsilon;' => '&#965;',
  1529. '&phi;' => '&#966;',
  1530. '&chi;' => '&#967;',
  1531. '&psi;' => '&#968;',
  1532. '&omega;' => '&#969;',
  1533. '&thetasym;' => '&#977;',
  1534. '&upsih;' => '&#978;',
  1535. '&piv;' => '&#982;',
  1536. '&ensp;' => '&#8194;',
  1537. '&emsp;' => '&#8195;',
  1538. '&thinsp;' => '&#8201;',
  1539. '&zwnj;' => '&#8204;',
  1540. '&zwj;' => '&#8205;',
  1541. '&lrm;' => '&#8206;',
  1542. '&rlm;' => '&#8207;',
  1543. '&ndash;' => '&#8211;',
  1544. '&mdash;' => '&#8212;',
  1545. '&lsquo;' => '&#8216;',
  1546. '&rsquo;' => '&#8217;',
  1547. '&sbquo;' => '&#8218;',
  1548. '&ldquo;' => '&#8220;',
  1549. '&rdquo;' => '&#8221;',
  1550. '&bdquo;' => '&#8222;',
  1551. '&dagger;' => '&#8224;',
  1552. '&Dagger;' => '&#8225;',
  1553. '&bull;' => '&#8226;',
  1554. '&hellip;' => '&#8230;',
  1555. '&permil;' => '&#8240;',
  1556. '&prime;' => '&#8242;',
  1557. '&Prime;' => '&#8243;',
  1558. '&lsaquo;' => '&#8249;',
  1559. '&rsaquo;' => '&#8250;',
  1560. '&oline;' => '&#8254;',
  1561. '&frasl;' => '&#8260;',
  1562. '&euro;' => '&#8364;',
  1563. '&image;' => '&#8465;',
  1564. '&weierp;' => '&#8472;',
  1565. '&real;' => '&#8476;',
  1566. '&trade;' => '&#8482;',
  1567. '&alefsym;' => '&#8501;',
  1568. '&crarr;' => '&#8629;',
  1569. '&lArr;' => '&#8656;',
  1570. '&uArr;' => '&#8657;',
  1571. '&rArr;' => '&#8658;',
  1572. '&dArr;' => '&#8659;',
  1573. '&hArr;' => '&#8660;',
  1574. '&forall;' => '&#8704;',
  1575. '&part;' => '&#8706;',
  1576. '&exist;' => '&#8707;',
  1577. '&empty;' => '&#8709;',
  1578. '&nabla;' => '&#8711;',
  1579. '&isin;' => '&#8712;',
  1580. '&notin;' => '&#8713;',
  1581. '&ni;' => '&#8715;',
  1582. '&prod;' => '&#8719;',
  1583. '&sum;' => '&#8721;',
  1584. '&minus;' => '&#8722;',
  1585. '&lowast;' => '&#8727;',
  1586. '&radic;' => '&#8730;',
  1587. '&prop;' => '&#8733;',
  1588. '&infin;' => '&#8734;',
  1589. '&ang;' => '&#8736;',
  1590. '&and;' => '&#8743;',
  1591. '&or;' => '&#8744;',
  1592. '&cap;' => '&#8745;',
  1593. '&cup;' => '&#8746;',
  1594. '&int;' => '&#8747;',
  1595. '&there4;' => '&#8756;',
  1596. '&sim;' => '&#8764;',
  1597. '&cong;' => '&#8773;',
  1598. '&asymp;' => '&#8776;',
  1599. '&ne;' => '&#8800;',
  1600. '&equiv;' => '&#8801;',
  1601. '&le;' => '&#8804;',
  1602. '&ge;' => '&#8805;',
  1603. '&sub;' => '&#8834;',
  1604. '&sup;' => '&#8835;',
  1605. '&nsub;' => '&#8836;',
  1606. '&sube;' => '&#8838;',
  1607. '&supe;' => '&#8839;',
  1608. '&oplus;' => '&#8853;',
  1609. '&otimes;' => '&#8855;',
  1610. '&perp;' => '&#8869;',
  1611. '&sdot;' => '&#8901;',
  1612. '&lceil;' => '&#8968;',
  1613. '&rceil;' => '&#8969;',
  1614. '&lfloor;' => '&#8970;',
  1615. '&rfloor;' => '&#8971;',
  1616. '&lang;' => '&#9001;',
  1617. '&rang;' => '&#9002;',
  1618. '&larr;' => '&#8592;',
  1619. '&uarr;' => '&#8593;',
  1620. '&rarr;' => '&#8594;',
  1621. '&darr;' => '&#8595;',
  1622. '&harr;' => '&#8596;',
  1623. '&loz;' => '&#9674;',
  1624. '&spades;' => '&#9824;',
  1625. '&clubs;' => '&#9827;',
  1626. '&hearts;' => '&#9829;',
  1627. '&diams;' => '&#9830;'
  1628. );
  1629. return str_replace( array_keys($to_ncr), array_values($to_ncr), $text );
  1630. }
  1631. endif;
  1632. // ! function wp_richedit_pre()
  1633. // ! function wp_htmledit_pre()
  1634. if ( !function_exists( '_deep_replace' ) ) :
  1635. /**
  1636. * Perform a deep string replace operation to ensure the values in $search are no longer present
  1637. *
  1638. * Repeats the replacement operation until it no longer replaces anything so as to remove "nested" values
  1639. * e.g. $subject = '%0%0%0DDD', $search ='%0D', $result ='' rather than the '%0%0DD' that
  1640. * str_replace would return
  1641. *
  1642. * @since 2.8.1
  1643. * @access private
  1644. *
  1645. * @param string|array $search
  1646. * @param string $subject
  1647. * @return string The processed string
  1648. */
  1649. function _deep_replace($search, $subject){
  1650. $found = true;
  1651. while($found) {
  1652. $found = false;
  1653. foreach( (array) $search as $val ) {
  1654. while(strpos($subject, $val) !== false) {
  1655. $found = true;
  1656. $subject = str_replace($val, '', $subject);
  1657. }
  1658. }
  1659. }
  1660. return $subject;
  1661. }
  1662. endif;
  1663. if ( !function_exists( 'esc_sql' ) ) :
  1664. /**
  1665. * Escapes data for use in a MySQL query
  1666. *
  1667. * This is just a handy shortcut for $bpdb->escape(), for completeness' sake
  1668. *
  1669. * @since 2.8.0
  1670. * @param string $sql Unescaped SQL data
  1671. * @return string The cleaned $sql
  1672. */
  1673. function esc_sql( $sql ) {
  1674. global $bpdb;
  1675. return $bpdb->escape( $sql );
  1676. }
  1677. endif;
  1678. // @todo: Deprecated function we should remove.
  1679. if ( !function_exists( 'clean_url' ) ) :
  1680. /**
  1681. * Checks and cleans a URL.
  1682. *
  1683. * A number of characters are removed from the URL. If the URL is for displaying
  1684. * (the default behaviour) ampersands are also replaced. The 'clean_url' filter
  1685. * is applied to the returned cleaned URL.
  1686. *
  1687. * @since 1.2.0
  1688. * @deprecated 3.0.0
  1689. * @deprecated Use esc_url()
  1690. * @see Alias for esc_url()
  1691. *
  1692. * @param string $url The URL to be cleaned.
  1693. * @param array $protocols Optional. An array of acceptable protocols.
  1694. * @param string $context Optional. How the URL will be used. Default is 'display'.
  1695. * @return string The cleaned $url after the 'clean_url' filter is applied.
  1696. */
  1697. function clean_url( $url, $protocols = null, $context = 'display' ) {
  1698. return esc_url( $url, $protocols, $context );
  1699. }
  1700. endif;
  1701. if ( !function_exists( 'esc_url' ) ) :
  1702. /**
  1703. * Checks and cleans a URL.
  1704. *
  1705. * A number of characters are removed from the URL. If the URL is for displaying
  1706. * (the default behaviour) ampersands are also replaced. The 'clean_url' filter
  1707. * is applied to the returned cleaned URL.
  1708. *
  1709. * @since 2.8.0
  1710. * @uses wp_kses_bad_protocol() To only permit protocols in the URL set
  1711. * via $protocols or the common ones set in the function.
  1712. *
  1713. * @param string $url The URL to be cleaned.
  1714. * @param array $protocols Optional. An array of acceptable protocols.
  1715. * Defaults to 'http', 'https', 'ftp', 'ftps', 'mailto', 'news', 'irc', 'gopher', 'nntp', 'feed', 'telnet', 'mms', 'rtsp', 'svn' if not set.
  1716. * @param string $_context Private. Use esc_url_raw() for database usage.
  1717. * @return string The cleaned $url after the 'clean_url' filter is applied.
  1718. */
  1719. function esc_url( $url, $protocols = null, $_context = 'display' ) {
  1720. $original_url = $url;
  1721. if ( '' == $url )
  1722. return $url;
  1723. $url = preg_replace('|[^a-z0-9-~+_.?#=!&;,/:%@$\|*\'()\\x80-\\xff]|i', '', $url);
  1724. $strip = array('%0d', '%0a', '%0D', '%0A');
  1725. $url = _deep_replace($strip, $url);
  1726. $url = str_replace(';//', '://', $url);
  1727. /* If the URL doesn't appear to contain a scheme, we
  1728. * presume it needs http:// appended (unless a relative
  1729. * link starting with /, # or ? or a php file).
  1730. */
  1731. if ( strpos($url, ':') === false && ! in_array( $url[0], array( '/', '#', '?' ) ) &&
  1732. ! preg_match('/^[a-z0-9-]+?\.php/i', $url) )
  1733. $url = 'http://' . $url;
  1734. // Replace ampersands and single quotes only when displaying.
  1735. if ( 'display' == $_context ) {
  1736. $url = wp_kses_normalize_entities( $url );
  1737. $url = str_replace( '&amp;', '&#038;', $url );
  1738. $url = str_replace( "'", '&#039;', $url );
  1739. }
  1740. // Todo: switch to wp_allowed_protocols() once it is merged to BackPress
  1741. if ( ! is_array( $protocols ) )
  1742. $protocols = array ('http', 'https', 'ftp', 'ftps', 'mailto', 'news', 'irc', 'gopher', 'nntp', 'feed', 'telnet', 'mms', 'rtsp', 'svn');
  1743. if ( wp_kses_bad_protocol( $url, $protocols ) != $url )
  1744. return '';
  1745. return apply_filters('clean_url', $url, $original_url, $_context);
  1746. }
  1747. endif;
  1748. if ( !function_exists( 'esc_url_raw' ) ) :
  1749. /**
  1750. * Performs esc_url() for database usage.
  1751. *
  1752. * @since 2.8.0
  1753. * @uses esc_url()
  1754. *
  1755. * @param string $url The URL to be cleaned.
  1756. * @param array $protocols An array of acceptable protocols.
  1757. * @return string The cleaned URL.
  1758. */
  1759. function esc_url_raw( $url, $protocols = null ) {
  1760. return esc_url( $url, $protocols, 'db' );
  1761. }
  1762. endif;
  1763. // ! function htmlentities2()
  1764. if ( !function_exists( 'esc_js' ) ) :
  1765. /**
  1766. * Escape single quotes, htmlspecialchar " < > &, and fix line endings.
  1767. *
  1768. * Escapes text strings for echoing in JS, both inline (for example in onclick="...")
  1769. * and inside <script> tag. Note that the strings have to be in single quotes.
  1770. * The filter 'js_escape' is also applied here.
  1771. *
  1772. * @since 2.8.0
  1773. *
  1774. * @param string $text The text to be escaped.
  1775. * @return string Escaped text.
  1776. */
  1777. function esc_js( $text ) {
  1778. $safe_text = wp_check_invalid_utf8( $text );
  1779. $safe_text = _wp_specialchars( $safe_text, ENT_COMPAT );
  1780. $safe_text = preg_replace( '/&#(x)?0*(?(1)27|39);?/i', "'", stripslashes( $safe_text ) );
  1781. $safe_text = str_replace( "\r", '', $safe_text );
  1782. $safe_text = str_replace( "\n", '\\n', addslashes( $safe_text ) );
  1783. return apply_filters( 'js_escape', $safe_text, $text );
  1784. }
  1785. endif;
  1786. // @todo: Deprecated function we should remove.
  1787. if ( !function_exists( 'js_escape' ) ) :
  1788. /**
  1789. * Escape single quotes, specialchar double quotes, and fix line endings.
  1790. *
  1791. * The filter 'js_escape' is also applied by esc_js()
  1792. *
  1793. * @since 2.0.4
  1794. *
  1795. * @deprecated 2.8.0
  1796. * @see esc_js()
  1797. *
  1798. * @param string $text The text to be escaped.
  1799. * @return string Escaped text.
  1800. */
  1801. function js_escape( $text ) {
  1802. return esc_js( $text );
  1803. }
  1804. endif;
  1805. if ( !function_exists( 'esc_html' ) ) :
  1806. /**
  1807. * Escaping for HTML blocks.
  1808. *
  1809. * @since 2.8.0
  1810. *
  1811. * @param string $text
  1812. * @return string
  1813. */
  1814. function esc_html( $text ) {
  1815. $safe_text = wp_check_invalid_utf8( $text );
  1816. $safe_text = _wp_specialchars( $safe_text, ENT_QUOTES );
  1817. return apply_filters( 'esc_html', $safe_text, $text );
  1818. return $text;
  1819. }
  1820. endif;
  1821. // @todo: Deprecated function we should remove.
  1822. if ( !function_exists( 'wp_specialchars' ) ) :
  1823. /**
  1824. * Escaping for HTML blocks
  1825. * @deprecated 2.8.0
  1826. * @see esc_html()
  1827. */
  1828. function wp_specialchars( $string, $quote_style = ENT_NOQUOTES, $charset = false, $double_encode = false ) {
  1829. if ( func_num_args() > 1 ) { // Maintain backwards compat for people passing additional args
  1830. $args = func_get_args();
  1831. return call_user_func_array( '_wp_specialchars', $args );
  1832. } else {
  1833. return esc_html( $string );
  1834. }
  1835. }
  1836. endif;
  1837. if ( !function_exists( 'esc_attr' ) ) :
  1838. /**
  1839. * Escaping for HTML attributes.
  1840. *
  1841. * @since 2.8.0
  1842. *
  1843. * @param string $text
  1844. * @return string
  1845. */
  1846. function esc_attr( $text ) {
  1847. $safe_text = wp_check_invalid_utf8( $text );
  1848. $safe_text = _wp_specialchars( $safe_text, ENT_QUOTES );
  1849. return apply_filters( 'attribute_escape', $safe_text, $text );
  1850. }
  1851. endif;
  1852. if ( !function_exists( 'esc_textarea' ) ) :
  1853. /**
  1854. * Escaping for textarea values.
  1855. *
  1856. * @since 3.1
  1857. *
  1858. * @param string $text
  1859. * @return string
  1860. */
  1861. function esc_textarea( $text ) {
  1862. $safe_text = htmlspecialchars( $text, ENT_QUOTES );
  1863. return apply_filters( 'esc_textarea', $safe_text, $text );
  1864. }
  1865. endif;
  1866. // @todo: Deprecated function we should remove.
  1867. if ( !function_exists( 'attribute_escape' ) ) :
  1868. /**
  1869. * Escaping for HTML attributes.
  1870. *
  1871. * @since 2.0.6
  1872. *
  1873. * @deprecated 2.8.0
  1874. * @see esc_attr()
  1875. *
  1876. * @param string $text
  1877. * @return string
  1878. */
  1879. function attribute_escape( $text ) {
  1880. return esc_attr( $text );
  1881. }
  1882. endif;
  1883. // ! function tag_escape()
  1884. if ( !function_exists('like_escape') ) :
  1885. /**
  1886. * Escapes text for SQL LIKE special characters % and _.
  1887. *
  1888. * @since 2.5.0
  1889. *
  1890. * @param string $text The text to be escaped.
  1891. * @return string text, safe for inclusion in LIKE query.
  1892. */
  1893. function like_escape($text) {
  1894. return str_replace(array("%", "_"), array("\\%", "\\_"), $text);
  1895. }
  1896. endif;
  1897. // ! function wp_make_link_relative()
  1898. // ! function sanitize_option()
  1899. if ( !function_exists('wp_parse_str') ) :
  1900. /**
  1901. * Parses a string into variables to be stored in an array.
  1902. *
  1903. * Uses {@link http://www.php.net/parse_str parse_str()} and stripslashes if
  1904. * {@link http://www.php.net/magic_quotes magic_quotes_gpc} is on.
  1905. *
  1906. * @since 2.2.1
  1907. * @uses apply_filters() for the 'wp_parse_str' filter.
  1908. *
  1909. * @param string $string The string to be parsed.
  1910. * @param array $array Variables will be stored in this array.
  1911. */
  1912. function wp_parse_str( $string, &$array ) {
  1913. parse_str( $string, $array );
  1914. if ( get_magic_quotes_gpc() )
  1915. $array = stripslashes_deep( $array );
  1916. $array = apply_filters( 'wp_parse_str', $array );
  1917. }
  1918. endif;
  1919. if ( !function_exists('wp_pre_kses_less_than') ) :
  1920. /**
  1921. * Convert lone less than signs.
  1922. *
  1923. * KSES already converts lone greater than signs.
  1924. *
  1925. * @uses wp_pre_kses_less_than_callback in the callback function.
  1926. * @since 2.3.0
  1927. *
  1928. * @param string $text Text to be converted.
  1929. * @return string Converted text.
  1930. */
  1931. function wp_pre_kses_less_than( $text ) {
  1932. return preg_replace_callback('%<[^>]*?((?=<)|>|$)%', 'wp_pre_kses_less_than_callback', $text);
  1933. }
  1934. endif;
  1935. if ( !function_exists('wp_pre_kses_less_than_callback') ) :
  1936. /**
  1937. * Callback function used by preg_replace.
  1938. *
  1939. * @uses esc_html to format the $matches text.
  1940. * @since 2.3.0
  1941. *
  1942. * @param array $matches Populated by matches to preg_replace.
  1943. * @return string The text returned after esc_html if needed.
  1944. */
  1945. function wp_pre_kses_less_than_callback( $matches ) {
  1946. if ( false === strpos($matches[0], '>') )
  1947. return esc_html($matches[0]);
  1948. return $matches[0];
  1949. }
  1950. endif;
  1951. // ! function wp_sprintf()
  1952. // ! function wp_sprintf_l()
  1953. if ( !function_exists('wp_html_excerpt') ) :
  1954. /**
  1955. * Safely extracts not more than the first $count characters from html string.
  1956. *
  1957. * UTF-8, tags and entities safe prefix extraction. Entities inside will *NOT*
  1958. * be counted as one character. For example &amp; will be counted as 4, &lt; as
  1959. * 3, etc.
  1960. *
  1961. * @since 2.5.0
  1962. *
  1963. * @param integer $str String to get the excerpt from.
  1964. * @param integer $count Maximum number of characters to take.
  1965. * @return string The excerpt.
  1966. */
  1967. function wp_html_excerpt( $str, $count ) {
  1968. $str = wp_strip_all_tags( $str, true );
  1969. $str = mb_substr( $str, 0, $count );
  1970. // remove part of an entity at the end
  1971. $str = preg_replace( '/&[^;\s]{0,6}$/', '', $str );
  1972. return $str;
  1973. }
  1974. endif;
  1975. // ! function links_add_base_url()
  1976. // ! function _links_add_base()
  1977. // ! function links_add_target()
  1978. // ! function _links_add_target()
  1979. // ! function normalize_whitespace()
  1980. if ( !function_exists('wp_strip_all_tags') ) :
  1981. /**
  1982. * Properly strip all HTML tags including script and style
  1983. *
  1984. * @since 2.9.0
  1985. *
  1986. * @param string $string String containing HTML tags
  1987. * @param bool $remove_breaks optional Whether to remove left over line breaks and white space chars
  1988. * @return string The processed string.
  1989. */
  1990. function wp_strip_all_tags($string, $remove_breaks = false) {
  1991. $string = preg_replace( '@<(script|style)[^>]*?>.*?</\\1>@si', '', $string );
  1992. $string = strip_tags($string);
  1993. if ( $remove_breaks )
  1994. $string = preg_replace('/[\r\n\t ]+/', ' ', $string);
  1995. return trim( $string );
  1996. }
  1997. endif;
  1998. if ( !function_exists('sanitize_text_field') ) :
  1999. /**
  2000. * Sanitize a string from user input or from the db
  2001. *
  2002. * check for invalid UTF-8,
  2003. * Convert single < characters to entity,
  2004. * strip all tags,
  2005. * remove line breaks, tabs and extra white space,
  2006. * strip octets.
  2007. *
  2008. * @since 2.9.0
  2009. *
  2010. * @param string $str
  2011. * @return string
  2012. */
  2013. function sanitize_text_field($str) {
  2014. $filtered = wp_check_invalid_utf8( $str );
  2015. if ( strpos($filtered, '<') !== false ) {
  2016. $filtered = wp_pre_kses_less_than( $filtered );
  2017. // This will strip extra whitespace for us.
  2018. $filtered = wp_strip_all_tags( $filtered, true );
  2019. } else {
  2020. $filtered = trim( preg_replace('/[\r\n\t ]+/', ' ', $filtered) );
  2021. }
  2022. $match = array();
  2023. $found = false;
  2024. while ( preg_match('/%[a-f0-9]{2}/i', $filtered, $match) ) {
  2025. $filtered = str_replace($match[0], '', $filtered);
  2026. $found = true;
  2027. }
  2028. if ( $found ) {
  2029. // Strip out the whitespace that may now exist after removing the octets.
  2030. $filtered = trim( preg_replace('/ +/', ' ', $filtered) );
  2031. }
  2032. return apply_filters('sanitize_text_field', $filtered, $str);
  2033. }
  2034. endif;
  2035. if ( !function_exists('wp_basename') ) :
  2036. /**
  2037. * i18n friendly version of basename()
  2038. *
  2039. * @since 3.1.0
  2040. *
  2041. * @param string $path A path.
  2042. * @param string $suffix If the filename ends in suffix this will also be cut off.
  2043. * @return string
  2044. */
  2045. function wp_basename( $path, $suffix = '' ) {
  2046. return urldecode( basename( str_replace( '%2F', '/', urlencode( $path ) ), $suffix ) );
  2047. }
  2048. endif;
  2049. // !function capital_P_dangit
  2050. if ( !function_exists('sanitize_mime_type') ) :
  2051. /**
  2052. * Sanitize a mime type
  2053. *
  2054. * @since 3.1.3
  2055. *
  2056. * @param string $mime_type Mime type
  2057. * @return string Sanitized mime type
  2058. */
  2059. function sanitize_mime_type( $mime_type ) {
  2060. $sani_mime_type = preg_replace( '/[^-+*.a-zA-Z0-9\/]/', '', $mime_type );
  2061. return apply_filters( 'sanitize_mime_type', $sani_mime_type, $mime_type );
  2062. }
  2063. endif;
  2064. // !function sanitize_trackback_urls