PageRenderTime 57ms CodeModel.GetById 15ms RepoModel.GetById 0ms 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

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

  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 E…

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