[ PHPXref.com ] [ Generated: Sun Sep 13 08:17:58 2009 ] [ WordPress 2.8.4 ]
[ Index ]     [ Variables ]     [ Functions ]     [ Classes ]     [ Constants ]     [ Statistics ]

title

Body

[close]

/wp-includes/ -> formatting.php (source)

   1  <?php
   2  /**
   3   * Main Wordpress Formatting API.
   4   *
   5   * Handles many functions for formatting output.
   6   *
   7   * @package WordPress
   8   **/
   9  
  10  /**
  11   * Replaces common plain text characters into formatted entities
  12   *
  13   * As an example,
  14   * <code>
  15   * 'cause today's effort makes it worth tomorrow's "holiday"...
  16   * </code>
  17   * Becomes:
  18   * <code>
  19   * &#8217;cause today&#8217;s effort makes it worth tomorrow&#8217;s &#8220;holiday&#8221;&#8230;
  20   * </code>
  21   * Code within certain html blocks are skipped.
  22   *
  23   * @since 0.71
  24   * @uses $wp_cockneyreplace Array of formatted entities for certain common phrases
  25   *
  26   * @param string $text The text to be formatted
  27   * @return string The string replaced with html entities
  28   */
  29  function wptexturize($text) {
  30      global $wp_cockneyreplace;
  31      $output = '';
  32      $curl = '';
  33      $textarr = preg_split('/(<.*>|\[.*\])/Us', $text, -1, PREG_SPLIT_DELIM_CAPTURE);
  34      $stop = count($textarr);
  35      
  36      /* translators: opening curly quote */
  37      $opening_quote = _x('&#8220;', 'opening curly quote');
  38      /* translators: closing curly quote */
  39      $closing_quote = _x('&#8221;', 'closing curly quote');
  40      
  41      $no_texturize_tags = apply_filters('no_texturize_tags', array('pre', 'code', 'kbd', 'style', 'script', 'tt'));
  42      $no_texturize_shortcodes = apply_filters('no_texturize_shortcodes', array('code'));
  43      $no_texturize_tags_stack = array();
  44      $no_texturize_shortcodes_stack = array();
  45  
  46      // if a plugin has provided an autocorrect array, use it
  47      if ( isset($wp_cockneyreplace) ) {
  48          $cockney = array_keys($wp_cockneyreplace);
  49          $cockneyreplace = array_values($wp_cockneyreplace);
  50      } else {
  51          $cockney = array("'tain't","'twere","'twas","'tis","'twill","'til","'bout","'nuff","'round","'cause");
  52          $cockneyreplace = array("&#8217;tain&#8217;t","&#8217;twere","&#8217;twas","&#8217;tis","&#8217;twill","&#8217;til","&#8217;bout","&#8217;nuff","&#8217;round","&#8217;cause");
  53      }
  54  
  55      $static_characters = array_merge(array('---', ' -- ', '--', ' - ', 'xn&#8211;', '...', '``', '\'s', '\'\'', ' (tm)'), $cockney);
  56      $static_replacements = array_merge(array('&#8212;', ' &#8212; ', '&#8211;', ' &#8211; ', 'xn--', '&#8230;', $opening_quote, '&#8217;s', $closing_quote, ' &#8482;'), $cockneyreplace);
  57  
  58      $dynamic_characters = array('/\'(\d\d(?:&#8217;|\')?s)/', '/(\s|\A|")\'/', '/(\d+)"/', '/(\d+)\'/', '/(\S)\'([^\'\s])/', '/(\s|\A)"(?!\s)/', '/"(\s|\S|\Z)/', '/\'([\s.]|\Z)/', '/(\d+)x(\d+)/');
  59      $dynamic_replacements = array('&#8217;$1','$1&#8216;', '$1&#8243;', '$1&#8242;', '$1&#8217;$2', '$1' . $opening_quote . '$2', $closing_quote . '$1', '&#8217;$1', '$1&#215;$2');
  60  
  61      for ( $i = 0; $i < $stop; $i++ ) {
  62          $curl = $textarr[$i];
  63  
  64          if ( !empty($curl) && '<' != $curl{0} && '[' != $curl{0}
  65                  && empty($no_texturize_shortcodes_stack) && empty($no_texturize_tags_stack)) { // If it's not a tag
  66              // static strings
  67              $curl = str_replace($static_characters, $static_replacements, $curl);
  68              // regular expressions
  69              $curl = preg_replace($dynamic_characters, $dynamic_replacements, $curl);
  70          } else {
  71              wptexturize_pushpop_element($curl, $no_texturize_tags_stack, $no_texturize_tags, '<', '>');
  72              wptexturize_pushpop_element($curl, $no_texturize_shortcodes_stack, $no_texturize_shortcodes, '[', ']');
  73          }
  74  
  75          $curl = preg_replace('/&([^#])(?![a-zA-Z1-4]{1,8};)/', '&#038;$1', $curl);
  76          $output .= $curl;
  77      }
  78  
  79      return $output;
  80  }
  81  
  82  function wptexturize_pushpop_element($text, &$stack, $disabled_elements, $opening = '<', $closing = '>') {
  83      $o = preg_quote($opening, '/');
  84      $c = preg_quote($closing, '/');
  85      foreach($disabled_elements as $element) {
  86          if (preg_match('/^'.$o.$element.'\b/', $text)) array_push($stack, $element);
  87          if (preg_match('/^'.$o.'\/'.$element.$c.'/', $text)) {
  88              $last = array_pop($stack);
  89              // disable texturize until we find a closing tag of our type (e.g. <pre>)
  90              // even if there was invalid nesting before that
  91              // Example: in the case <pre>sadsadasd</code>"baba"</pre> "baba" won't be texturized
  92              if ($last != $element) array_push($stack, $last);
  93          }
  94      }
  95  }
  96  
  97  /**
  98   * Accepts matches array from preg_replace_callback in wpautop() or a string.
  99   *
 100   * Ensures that the contents of a <<pre>>...<</pre>> HTML block are not
 101   * converted into paragraphs or line-breaks.
 102   *
 103   * @since 1.2.0
 104   *
 105   * @param array|string $matches The array or string
 106   * @return string The pre block without paragraph/line-break conversion.
 107   */
 108  function clean_pre($matches) {
 109      if ( is_array($matches) )
 110          $text = $matches[1] . $matches[2] . "</pre>";
 111      else
 112          $text = $matches;
 113  
 114      $text = str_replace('<br />', '', $text);
 115      $text = str_replace('<p>', "\n", $text);
 116      $text = str_replace('</p>', '', $text);
 117  
 118      return $text;
 119  }
 120  
 121  /**
 122   * Replaces double line-breaks with paragraph elements.
 123   *
 124   * A group of regex replaces used to identify text formatted with newlines and
 125   * replace double line-breaks with HTML paragraph tags. The remaining
 126   * line-breaks after conversion become <<br />> tags, unless $br is set to '0'
 127   * or 'false'.
 128   *
 129   * @since 0.71
 130   *
 131   * @param string $pee The text which has to be formatted.
 132   * @param int|bool $br Optional. If set, this will convert all remaining line-breaks after paragraphing. Default true.
 133   * @return string Text which has been converted into correct paragraph tags.
 134   */
 135  function wpautop($pee, $br = 1) {
 136      if ( trim($pee) === '' )
 137          return '';
 138      $pee = $pee . "\n"; // just to make things a little easier, pad the end
 139      $pee = preg_replace('|<br />\s*<br />|', "\n\n", $pee);
 140      // Space things out a little
 141      $allblocks = '(?:table|thead|tfoot|caption|col|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|select|form|map|area|blockquote|address|math|style|input|p|h[1-6]|hr)';
 142      $pee = preg_replace('!(<' . $allblocks . '[^>]*>)!', "\n$1", $pee);
 143      $pee = preg_replace('!(</' . $allblocks . '>)!', "$1\n\n", $pee);
 144      $pee = str_replace(array("\r\n", "\r"), "\n", $pee); // cross-platform newlines
 145      if ( strpos($pee, '<object') !== false ) {
 146          $pee = preg_replace('|\s*<param([^>]*)>\s*|', "<param$1>", $pee); // no pee inside object/embed
 147          $pee = preg_replace('|\s*</embed>\s*|', '</embed>', $pee);
 148      }
 149      $pee = preg_replace("/\n\n+/", "\n\n", $pee); // take care of duplicates
 150      // make paragraphs, including one at the end
 151      $pees = preg_split('/\n\s*\n/', $pee, -1, PREG_SPLIT_NO_EMPTY);
 152      $pee = '';
 153      foreach ( $pees as $tinkle )
 154          $pee .= '<p>' . trim($tinkle, "\n") . "</p>\n";
 155      $pee = preg_replace('|<p>\s*</p>|', '', $pee); // under certain strange conditions it could create a P of entirely whitespace
 156      $pee = preg_replace('!<p>([^<]+)</(div|address|form)>!', "<p>$1</p></$2>", $pee);
 157      $pee = preg_replace('!<p>\s*(</?' . $allblocks . '[^>]*>)\s*</p>!', "$1", $pee); // don't pee all over a tag
 158      $pee = preg_replace("|<p>(<li.+?)</p>|", "$1", $pee); // problem with nested lists
 159      $pee = preg_replace('|<p><blockquote([^>]*)>|i', "<blockquote$1><p>", $pee);
 160      $pee = str_replace('</blockquote></p>', '</p></blockquote>', $pee);
 161      $pee = preg_replace('!<p>\s*(</?' . $allblocks . '[^>]*>)!', "$1", $pee);
 162      $pee = preg_replace('!(</?' . $allblocks . '[^>]*>)\s*</p>!', "$1", $pee);
 163      if ($br) {
 164          $pee = preg_replace_callback('/<(script|style).*?<\/\\1>/s', create_function('$matches', 'return str_replace("\n", "<WPPreserveNewline />", $matches[0]);'), $pee);
 165          $pee = preg_replace('|(?<!<br />)\s*\n|', "<br />\n", $pee); // optionally make line breaks
 166          $pee = str_replace('<WPPreserveNewline />', "\n", $pee);
 167      }
 168      $pee = preg_replace('!(</?' . $allblocks . '[^>]*>)\s*<br />!', "$1", $pee);
 169      $pee = preg_replace('!<br />(\s*</?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)[^>]*>)!', '$1', $pee);
 170      if (strpos($pee, '<pre') !== false)
 171          $pee = preg_replace_callback('!(<pre[^>]*>)(.*?)</pre>!is', 'clean_pre', $pee );
 172      $pee = preg_replace( "|\n</p>$|", '</p>', $pee );
 173      $pee = preg_replace('/<p>\s*?(' . get_shortcode_regex() . ')\s*<\/p>/s', '$1', $pee); // don't auto-p wrap shortcodes that stand alone
 174  
 175      return $pee;
 176  }
 177  
 178  /**
 179   * Checks to see if a string is utf8 encoded.
 180   *
 181   * NOTE: This function checks for 5-Byte sequences, UTF8
 182   *       has Bytes Sequences with a maximum length of 4.
 183   *
 184   * @author bmorel at ssi dot fr (modified)
 185   * @since 1.2.1
 186   *
 187   * @param string $str The string to be checked
 188   * @return bool True if $str fits a UTF-8 model, false otherwise.
 189   */
 190  function seems_utf8($str) {
 191      $length = strlen($str);
 192      for ($i=0; $i < $length; $i++) {
 193          $c = ord($str[$i]);
 194          if ($c < 0x80) $n = 0; # 0bbbbbbb
 195          elseif (($c & 0xE0) == 0xC0) $n=1; # 110bbbbb
 196          elseif (($c & 0xF0) == 0xE0) $n=2; # 1110bbbb
 197          elseif (($c & 0xF8) == 0xF0) $n=3; # 11110bbb
 198          elseif (($c & 0xFC) == 0xF8) $n=4; # 111110bb
 199          elseif (($c & 0xFE) == 0xFC) $n=5; # 1111110b
 200          else return false; # Does not match any model
 201          for ($j=0; $j<$n; $j++) { # n bytes matching 10bbbbbb follow ?
 202              if ((++$i == $length) || ((ord($str[$i]) & 0xC0) != 0x80))
 203                  return false;
 204          }
 205      }
 206      return true;
 207  }
 208  
 209  /**
 210   * Converts a number of special characters into their HTML entities.
 211   *
 212   * Specifically deals with: &, <, >, ", and '.
 213   *
 214   * $quote_style can be set to ENT_COMPAT to encode " to
 215   * &quot;, or ENT_QUOTES to do both. Default is ENT_NOQUOTES where no quotes are encoded.
 216   *
 217   * @since 1.2.2
 218   *
 219   * @param string $string The text which is to be encoded.
 220   * @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.
 221   * @param string $charset Optional. The character encoding of the string. Default is false.
 222   * @param boolean $double_encode Optional. Whether or not to encode existing html entities. Default is false.
 223   * @return string The encoded text with HTML entities.
 224   */
 225  function _wp_specialchars( $string, $quote_style = ENT_NOQUOTES, $charset = false, $double_encode = false ) {
 226      $string = (string) $string;
 227  
 228      if ( 0 === strlen( $string ) ) {
 229          return '';
 230      }
 231  
 232      // Don't bother if there are no specialchars - saves some processing
 233      if ( !preg_match( '/[&<>"\']/', $string ) ) {
 234          return $string;
 235      }
 236  
 237      // Account for the previous behaviour of the function when the $quote_style is not an accepted value
 238      if ( empty( $quote_style ) ) {
 239          $quote_style = ENT_NOQUOTES;
 240      } elseif ( !in_array( $quote_style, array( 0, 2, 3, 'single', 'double' ), true ) ) {
 241          $quote_style = ENT_QUOTES;
 242      }
 243  
 244      // Store the site charset as a static to avoid multiple calls to wp_load_alloptions()
 245      if ( !$charset ) {
 246          static $_charset;
 247          if ( !isset( $_charset ) ) {
 248              $alloptions = wp_load_alloptions();
 249              $_charset = isset( $alloptions['blog_charset'] ) ? $alloptions['blog_charset'] : '';
 250          }
 251          $charset = $_charset;
 252      }
 253      if ( in_array( $charset, array( 'utf8', 'utf-8', 'UTF8' ) ) ) {
 254          $charset = 'UTF-8';
 255      }
 256  
 257      $_quote_style = $quote_style;
 258  
 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  
 266      // Handle double encoding ourselves
 267      if ( !$double_encode ) {
 268          $string = wp_specialchars_decode( $string, $_quote_style );
 269          $string = preg_replace( '/&(#?x?[0-9a-z]+);/i', '|wp_entity|$1|/wp_entity|', $string );
 270      }
 271  
 272      $string = @htmlspecialchars( $string, $quote_style, $charset );
 273  
 274      // Handle double encoding ourselves
 275      if ( !$double_encode ) {
 276          $string = str_replace( array( '|wp_entity|', '|/wp_entity|' ), array( '&', ';' ), $string );
 277      }
 278  
 279      // Backwards compatibility
 280      if ( 'single' === $_quote_style ) {
 281          $string = str_replace( "'", '&#039;', $string );
 282      }
 283  
 284      return $string;
 285  }
 286  
 287  /**
 288   * Converts a number of HTML entities into their special characters.
 289   *
 290   * Specifically deals with: &, <, >, ", and '.
 291   *
 292   * $quote_style can be set to ENT_COMPAT to decode " entities,
 293   * or ENT_QUOTES to do both " and '. Default is ENT_NOQUOTES where no quotes are decoded.
 294   *
 295   * @since 2.8
 296   *
 297   * @param string $string The text which is to be decoded.
 298   * @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.
 299   * @return string The decoded text without HTML entities.
 300   */
 301  function wp_specialchars_decode( $string, $quote_style = ENT_NOQUOTES ) {
 302      $string = (string) $string;
 303  
 304      if ( 0 === strlen( $string ) ) {
 305          return '';
 306      }
 307  
 308      // Don't bother if there are no entities - saves a lot of processing
 309      if ( strpos( $string, '&' ) === false ) {
 310          return $string;
 311      }
 312  
 313      // Match the previous behaviour of _wp_specialchars() when the $quote_style is not an accepted value
 314      if ( empty( $quote_style ) ) {
 315          $quote_style = ENT_NOQUOTES;
 316      } elseif ( !in_array( $quote_style, array( 0, 2, 3, 'single', 'double' ), true ) ) {
 317          $quote_style = ENT_QUOTES;
 318      }
 319  
 320      // More complete than get_html_translation_table( HTML_SPECIALCHARS )
 321      $single = array( '&#039;'  => '\'', '&#x27;' => '\'' );
 322      $single_preg = array( '/&#0*39;/'  => '&#039;', '/&#x0*27;/i' => '&#x27;' );
 323      $double = array( '&quot;' => '"', '&#034;'  => '"', '&#x22;' => '"' );
 324      $double_preg = array( '/&#0*34;/'  => '&#034;', '/&#x0*22;/i' => '&#x22;' );
 325      $others = array( '&lt;'   => '<', '&#060;'  => '<', '&gt;'   => '>', '&#062;'  => '>', '&amp;'  => '&', '&#038;'  => '&', '&#x26;' => '&' );
 326      $others_preg = array( '/&#0*60;/'  => '&#060;', '/&#0*62;/'  => '&#062;', '/&#0*38;/'  => '&#038;', '/&#x0*26;/i' => '&#x26;' );
 327  
 328      if ( $quote_style === ENT_QUOTES ) {
 329          $translation = array_merge( $single, $double, $others );
 330          $translation_preg = array_merge( $single_preg, $double_preg, $others_preg );
 331      } elseif ( $quote_style === ENT_COMPAT || $quote_style === 'double' ) {
 332          $translation = array_merge( $double, $others );
 333          $translation_preg = array_merge( $double_preg, $others_preg );
 334      } elseif ( $quote_style === 'single' ) {
 335          $translation = array_merge( $single, $others );
 336          $translation_preg = array_merge( $single_preg, $others_preg );
 337      } elseif ( $quote_style === ENT_NOQUOTES ) {
 338          $translation = $others;
 339          $translation_preg = $others_preg;
 340      }
 341  
 342      // Remove zero padding on numeric entities
 343      $string = preg_replace( array_keys( $translation_preg ), array_values( $translation_preg ), $string );
 344  
 345      // Replace characters according to translation table
 346      return strtr( $string, $translation );
 347  }
 348  
 349  /**
 350   * Checks for invalid UTF8 in a string.
 351   *
 352   * @since 2.8
 353   *
 354   * @param string $string The text which is to be checked.
 355   * @param boolean $strip Optional. Whether to attempt to strip out invalid UTF8. Default is false.
 356   * @return string The checked text.
 357   */
 358  function wp_check_invalid_utf8( $string, $strip = false ) {
 359      $string = (string) $string;
 360  
 361      if ( 0 === strlen( $string ) ) {
 362          return '';
 363      }
 364  
 365      // Store the site charset as a static to avoid multiple calls to get_option()
 366      static $is_utf8;
 367      if ( !isset( $is_utf8 ) ) {
 368          $is_utf8 = in_array( get_option( 'blog_charset' ), array( 'utf8', 'utf-8', 'UTF8', 'UTF-8' ) );
 369      }
 370      if ( !$is_utf8 ) {
 371          return $string;
 372      }
 373  
 374      // Check for support for utf8 in the installed PCRE library once and store the result in a static
 375      static $utf8_pcre;
 376      if ( !isset( $utf8_pcre ) ) {
 377          $utf8_pcre = @preg_match( '/^./u', 'a' );
 378      }
 379      // We can't demand utf8 in the PCRE installation, so just return the string in those cases
 380      if ( !$utf8_pcre ) {
 381          return $string;
 382      }
 383  
 384      // preg_match fails when it encounters invalid UTF8 in $string
 385      if ( 1 === @preg_match( '/^./us', $string ) ) {
 386          return $string;
 387      }
 388  
 389      // Attempt to strip the bad chars if requested (not recommended)
 390      if ( $strip && function_exists( 'iconv' ) ) {
 391          return iconv( 'utf-8', 'utf-8', $string );
 392      }
 393  
 394      return '';
 395  }
 396  
 397  /**
 398   * Encode the Unicode values to be used in the URI.
 399   *
 400   * @since 1.5.0
 401   *
 402   * @param string $utf8_string
 403   * @param int $length Max length of the string
 404   * @return string String with Unicode encoded for URI.
 405   */
 406  function utf8_uri_encode( $utf8_string, $length = 0 ) {
 407      $unicode = '';
 408      $values = array();
 409      $num_octets = 1;
 410      $unicode_length = 0;
 411  
 412      $string_length = strlen( $utf8_string );
 413      for ($i = 0; $i < $string_length; $i++ ) {
 414  
 415          $value = ord( $utf8_string[ $i ] );
 416  
 417          if ( $value < 128 ) {
 418              if ( $length && ( $unicode_length >= $length ) )
 419                  break;
 420              $unicode .= chr($value);
 421              $unicode_length++;
 422          } else {
 423              if ( count( $values ) == 0 ) $num_octets = ( $value < 224 ) ? 2 : 3;
 424  
 425              $values[] = $value;
 426  
 427              if ( $length && ( $unicode_length + ($num_octets * 3) ) > $length )
 428                  break;
 429              if ( count( $values ) == $num_octets ) {
 430                  if ($num_octets == 3) {
 431                      $unicode .= '%' . dechex($values[0]) . '%' . dechex($values[1]) . '%' . dechex($values[2]);
 432                      $unicode_length += 9;
 433                  } else {
 434                      $unicode .= '%' . dechex($values[0]) . '%' . dechex($values[1]);
 435                      $unicode_length += 6;
 436                  }
 437  
 438                  $values = array();
 439                  $num_octets = 1;
 440              }
 441          }
 442      }
 443  
 444      return $unicode;
 445  }
 446  
 447  /**
 448   * Converts all accent characters to ASCII characters.
 449   *
 450   * If there are no accent characters, then the string given is just returned.
 451   *
 452   * @since 1.2.1
 453   *
 454   * @param string $string Text that might have accent characters
 455   * @return string Filtered string with replaced "nice" characters.
 456   */
 457  function remove_accents($string) {
 458      if ( !preg_match('/[\x80-\xff]/', $string) )
 459          return $string;
 460  
 461      if (seems_utf8($string)) {
 462          $chars = array(
 463          // Decompositions for Latin-1 Supplement
 464          chr(195).chr(128) => 'A', chr(195).chr(129) => 'A',
 465          chr(195).chr(130) => 'A', chr(195).chr(131) => 'A',
 466          chr(195).chr(132) => 'A', chr(195).chr(133) => 'A',
 467          chr(195).chr(135) => 'C', chr(195).chr(136) => 'E',
 468          chr(195).chr(137) => 'E', chr(195).chr(138) => 'E',
 469          chr(195).chr(139) => 'E', chr(195).chr(140) => 'I',
 470          chr(195).chr(141) => 'I', chr(195).chr(142) => 'I',
 471          chr(195).chr(143) => 'I', chr(195).chr(145) => 'N',
 472          chr(195).chr(146) => 'O', chr(195).chr(147) => 'O',
 473          chr(195).chr(148) => 'O', chr(195).chr(149) => 'O',
 474          chr(195).chr(150) => 'O', chr(195).chr(153) => 'U',
 475          chr(195).chr(154) => 'U', chr(195).chr(155) => 'U',
 476          chr(195).chr(156) => 'U', chr(195).chr(157) => 'Y',
 477          chr(195).chr(159) => 's', chr(195).chr(160) => 'a',
 478          chr(195).chr(161) => 'a', chr(195).chr(162) => 'a',
 479          chr(195).chr(163) => 'a', chr(195).chr(164) => 'a',
 480          chr(195).chr(165) => 'a', chr(195).chr(167) => 'c',
 481          chr(195).chr(168) => 'e', chr(195).chr(169) => 'e',
 482          chr(195).chr(170) => 'e', chr(195).chr(171) => 'e',
 483          chr(195).chr(172) => 'i', chr(195).chr(173) => 'i',
 484          chr(195).chr(174) => 'i', chr(195).chr(175) => 'i',
 485          chr(195).chr(177) => 'n', chr(195).chr(178) => 'o',
 486          chr(195).chr(179) => 'o', chr(195).chr(180) => 'o',
 487          chr(195).chr(181) => 'o', chr(195).chr(182) => 'o',
 488          chr(195).chr(182) => 'o', chr(195).chr(185) => 'u',
 489          chr(195).chr(186) => 'u', chr(195).chr(187) => 'u',
 490          chr(195).chr(188) => 'u', chr(195).chr(189) => 'y',
 491          chr(195).chr(191) => 'y',
 492          // Decompositions for Latin Extended-A
 493          chr(196).chr(128) => 'A', chr(196).chr(129) => 'a',
 494          chr(196).chr(130) => 'A', chr(196).chr(131) => 'a',
 495          chr(196).chr(132) => 'A', chr(196).chr(133) => 'a',
 496          chr(196).chr(134) => 'C', chr(196).chr(135) => 'c',
 497          chr(196).chr(136) => 'C', chr(196).chr(137) => 'c',
 498          chr(196).chr(138) => 'C', chr(196).chr(139) => 'c',
 499          chr(196).chr(140) => 'C', chr(196).chr(141) => 'c',
 500          chr(196).chr(142) => 'D', chr(196).chr(143) => 'd',
 501          chr(196).chr(144) => 'D', chr(196).chr(145) => 'd',
 502          chr(196).chr(146) => 'E', chr(196).chr(147) => 'e',
 503          chr(196).chr(148) => 'E', chr(196).chr(149) => 'e',
 504          chr(196).chr(150) => 'E', chr(196).chr(151) => 'e',
 505          chr(196).chr(152) => 'E', chr(196).chr(153) => 'e',
 506          chr(196).chr(154) => 'E', chr(196).chr(155) => 'e',
 507          chr(196).chr(156) => 'G', chr(196).chr(157) => 'g',
 508          chr(196).chr(158) => 'G', chr(196).chr(159) => 'g',
 509          chr(196).chr(160) => 'G', chr(196).chr(161) => 'g',
 510          chr(196).chr(162) => 'G', chr(196).chr(163) => 'g',
 511          chr(196).chr(164) => 'H', chr(196).chr(165) => 'h',
 512          chr(196).chr(166) => 'H', chr(196).chr(167) => 'h',
 513          chr(196).chr(168) => 'I', chr(196).chr(169) => 'i',
 514          chr(196).chr(170) => 'I', chr(196).chr(171) => 'i',
 515          chr(196).chr(172) => 'I', chr(196).chr(173) => 'i',
 516          chr(196).chr(174) => 'I', chr(196).chr(175) => 'i',
 517          chr(196).chr(176) => 'I', chr(196).chr(177) => 'i',
 518          chr(196).chr(178) => 'IJ',chr(196).chr(179) => 'ij',
 519          chr(196).chr(180) => 'J', chr(196).chr(181) => 'j',
 520          chr(196).chr(182) => 'K', chr(196).chr(183) => 'k',
 521          chr(196).chr(184) => 'k', chr(196).chr(185) => 'L',
 522          chr(196).chr(186) => 'l', chr(196).chr(187) => 'L',
 523          chr(196).chr(188) => 'l', chr(196).chr(189) => 'L',
 524          chr(196).chr(190) => 'l', chr(196).chr(191) => 'L',
 525          chr(197).chr(128) => 'l', chr(197).chr(129) => 'L',
 526          chr(197).chr(130) => 'l', chr(197).chr(131) => 'N',
 527          chr(197).chr(132) => 'n', chr(197).chr(133) => 'N',
 528          chr(197).chr(134) => 'n', chr(197).chr(135) => 'N',
 529          chr(197).chr(136) => 'n', chr(197).chr(137) => 'N',
 530          chr(197).chr(138) => 'n', chr(197).chr(139) => 'N',
 531          chr(197).chr(140) => 'O', chr(197).chr(141) => 'o',
 532          chr(197).chr(142) => 'O', chr(197).chr(143) => 'o',
 533          chr(197).chr(144) => 'O', chr(197).chr(145) => 'o',
 534          chr(197).chr(146) => 'OE',chr(197).chr(147) => 'oe',
 535          chr(197).chr(148) => 'R',chr(197).chr(149) => 'r',
 536          chr(197).chr(150) => 'R',chr(197).chr(151) => 'r',
 537          chr(197).chr(152) => 'R',chr(197).chr(153) => 'r',
 538          chr(197).chr(154) => 'S',chr(197).chr(155) => 's',
 539          chr(197).chr(156) => 'S',chr(197).chr(157) => 's',
 540          chr(197).chr(158) => 'S',chr(197).chr(159) => 's',
 541          chr(197).chr(160) => 'S', chr(197).chr(161) => 's',
 542          chr(197).chr(162) => 'T', chr(197).chr(163) => 't',
 543          chr(197).chr(164) => 'T', chr(197).chr(165) => 't',
 544          chr(197).chr(166) => 'T', chr(197).chr(167) => 't',
 545          chr(197).chr(168) => 'U', chr(197).chr(169) => 'u',
 546          chr(197).chr(170) => 'U', chr(197).chr(171) => 'u',
 547          chr(197).chr(172) => 'U', chr(197).chr(173) => 'u',
 548          chr(197).chr(174) => 'U', chr(197).chr(175) => 'u',
 549          chr(197).chr(176) => 'U', chr(197).chr(177) => 'u',
 550          chr(197).chr(178) => 'U', chr(197).chr(179) => 'u',
 551          chr(197).chr(180) => 'W', chr(197).chr(181) => 'w',
 552          chr(197).chr(182) => 'Y', chr(197).chr(183) => 'y',
 553          chr(197).chr(184) => 'Y', chr(197).chr(185) => 'Z',
 554          chr(197).chr(186) => 'z', chr(197).chr(187) => 'Z',
 555          chr(197).chr(188) => 'z', chr(197).chr(189) => 'Z',
 556          chr(197).chr(190) => 'z', chr(197).chr(191) => 's',
 557          // Euro Sign
 558          chr(226).chr(130).chr(172) => 'E',
 559          // GBP (Pound) Sign
 560          chr(194).chr(163) => '');
 561  
 562          $string = strtr($string, $chars);
 563      } else {
 564          // Assume ISO-8859-1 if not UTF-8
 565          $chars['in'] = chr(128).chr(131).chr(138).chr(142).chr(154).chr(158)
 566              .chr(159).chr(162).chr(165).chr(181).chr(192).chr(193).chr(194)
 567              .chr(195).chr(196).chr(197).chr(199).chr(200).chr(201).chr(202)
 568              .chr(203).chr(204).chr(205).chr(206).chr(207).chr(209).chr(210)
 569              .chr(211).chr(212).chr(213).chr(214).chr(216).chr(217).chr(218)
 570              .chr(219).chr(220).chr(221).chr(224).chr(225).chr(226).chr(227)
 571              .chr(228).chr(229).chr(231).chr(232).chr(233).chr(234).chr(235)
 572              .chr(236).chr(237).chr(238).chr(239).chr(241).chr(242).chr(243)
 573              .chr(244).chr(245).chr(246).chr(248).chr(249).chr(250).chr(251)
 574              .chr(252).chr(253).chr(255);
 575  
 576          $chars['out'] = "EfSZszYcYuAAAAAACEEEEIIIINOOOOOOUUUUYaaaaaaceeeeiiiinoooooouuuuyy";
 577  
 578          $string = strtr($string, $chars['in'], $chars['out']);
 579          $double_chars['in'] = array(chr(140), chr(156), chr(198), chr(208), chr(222), chr(223), chr(230), chr(240), chr(254));
 580          $double_chars['out'] = array('OE', 'oe', 'AE', 'DH', 'TH', 'ss', 'ae', 'dh', 'th');
 581          $string = str_replace($double_chars['in'], $double_chars['out'], $string);
 582      }
 583  
 584      return $string;
 585  }
 586  
 587  /**
 588   * Sanitizes a filename replacing whitespace with dashes
 589   *
 590   * Removes special characters that are illegal in filenames on certain
 591   * operating systems and special characters requiring special escaping
 592   * to manipulate at the command line. Replaces spaces and consecutive
 593   * dashes with a single dash. Trim period, dash and underscore from beginning
 594   * and end of filename.
 595   *
 596   * @since 2.1.0
 597   *
 598   * @param string $filename The filename to be sanitized
 599   * @return string The sanitized filename
 600   */
 601  function sanitize_file_name( $filename ) {
 602      $filename_raw = $filename;
 603      $special_chars = array("?", "[", "]", "/", "\\", "=", "<", ">", ":", ";", ",", "'", "\"", "&", "$", "#", "*", "(", ")", "|", "~", "`", "!", "{", "}");
 604      $special_chars = apply_filters('sanitize_file_name_chars', $special_chars, $filename_raw);
 605      $filename = str_replace($special_chars, '', $filename);
 606      $filename = preg_replace('/[\s-]+/', '-', $filename);
 607      $filename = trim($filename, '.-_');
 608      return apply_filters('sanitize_file_name', $filename, $filename_raw);
 609  }
 610  
 611  /**
 612   * Sanitize username stripping out unsafe characters.
 613   *
 614   * If $strict is true, only alphanumeric characters (as well as _, space, ., -,
 615   * @) are returned.
 616   * Removes tags, octets, entities, and if strict is enabled, will remove all
 617   * non-ASCII characters. After sanitizing, it passes the username, raw username
 618   * (the username in the parameter), and the strict parameter as parameters for
 619   * the filter.
 620   *
 621   * @since 2.0.0
 622   * @uses apply_filters() Calls 'sanitize_user' hook on username, raw username,
 623   *        and $strict parameter.
 624   *
 625   * @param string $username The username to be sanitized.
 626   * @param bool $strict If set limits $username to specific characters. Default false.
 627   * @return string The sanitized username, after passing through filters.
 628   */
 629  function sanitize_user( $username, $strict = false ) {
 630      $raw_username = $username;
 631      $username = strip_tags($username);
 632      // Kill octets
 633      $username = preg_replace('|%([a-fA-F0-9][a-fA-F0-9])|', '', $username);
 634      $username = preg_replace('/&.+?;/', '', $username); // Kill entities
 635  
 636      // If strict, reduce to ASCII for max portability.
 637      if ( $strict )
 638          $username = preg_replace('|[^a-z0-9 _.\-@]|i', '', $username);
 639  
 640      // Consolidate contiguous whitespace
 641      $username = preg_replace('|\s+|', ' ', $username);
 642  
 643      return apply_filters('sanitize_user', $username, $raw_username, $strict);
 644  }
 645  
 646  /**
 647   * Sanitizes title or use fallback title.
 648   *
 649   * Specifically, HTML and PHP tags are stripped. Further actions can be added
 650   * via the plugin API. If $title is empty and $fallback_title is set, the latter
 651   * will be used.
 652   *
 653   * @since 1.0.0
 654   *
 655   * @param string $title The string to be sanitized.
 656   * @param string $fallback_title Optional. A title to use if $title is empty.
 657   * @return string The sanitized string.
 658   */
 659  function sanitize_title($title, $fallback_title = '') {
 660      $raw_title = $title;
 661      $title = strip_tags($title);
 662      $title = apply_filters('sanitize_title', $title, $raw_title);
 663  
 664      if ( '' === $title || false === $title )
 665          $title = $fallback_title;
 666  
 667      return $title;
 668  }
 669  
 670  /**
 671   * Sanitizes title, replacing whitespace with dashes.
 672   *
 673   * Limits the output to alphanumeric characters, underscore (_) and dash (-).
 674   * Whitespace becomes a dash.
 675   *
 676   * @since 1.2.0
 677   *
 678   * @param string $title The title to be sanitized.
 679   * @return string The sanitized title.
 680   */
 681  function sanitize_title_with_dashes($title) {
 682      $title = strip_tags($title);
 683      // Preserve escaped octets.
 684      $title = preg_replace('|%([a-fA-F0-9][a-fA-F0-9])|', '---$1---', $title);
 685      // Remove percent signs that are not part of an octet.
 686      $title = str_replace('%', '', $title);
 687      // Restore octets.
 688      $title = preg_replace('|---([a-fA-F0-9][a-fA-F0-9])---|', '%$1', $title);
 689  
 690      $title = remove_accents($title);
 691      if (seems_utf8($title)) {
 692          if (function_exists('mb_strtolower')) {
 693              $title = mb_strtolower($title, 'UTF-8');
 694          }
 695          $title = utf8_uri_encode($title, 200);
 696      }
 697  
 698      $title = strtolower($title);
 699      $title = preg_replace('/&.+?;/', '', $title); // kill entities
 700      $title = str_replace('.', '-', $title);
 701      $title = preg_replace('/[^%a-z0-9 _-]/', '', $title);
 702      $title = preg_replace('/\s+/', '-', $title);
 703      $title = preg_replace('|-+|', '-', $title);
 704      $title = trim($title, '-');
 705  
 706      return $title;
 707  }
 708  
 709  /**
 710   * Ensures a string is a valid SQL order by clause.
 711   *
 712   * Accepts one or more columns, with or without ASC/DESC, and also accepts
 713   * RAND().
 714   *
 715   * @since 2.5.1
 716   *
 717   * @param string $orderby Order by string to be checked.
 718   * @return string|false Returns the order by clause if it is a match, false otherwise.
 719   */
 720  function sanitize_sql_orderby( $orderby ){
 721      preg_match('/^\s*([a-z0-9_]+(\s+(ASC|DESC))?(\s*,\s*|\s*$))+|^\s*RAND\(\s*\)\s*$/i', $orderby, $obmatches);
 722      if ( !$obmatches )
 723          return false;
 724      return $orderby;
 725  }
 726  
 727  /**
 728   * Santizes a html classname to ensure it only contains valid characters
 729   *
 730   * Strips the string down to A-Z,a-z,0-9,'-' if this results in an empty
 731   * string then it will return the alternative value supplied.
 732   *
 733   * @todo Expand to support the full range of CDATA that a class attribute can contain.
 734   *
 735   * @since 2.8.0
 736   *
 737   * @param string $class The classname to be sanitized
 738   * @param string $fallback The value to return if the sanitization end's up as an empty string.
 739   * @return string The sanitized value
 740   */
 741  function sanitize_html_class($class, $fallback){
 742      //Strip out any % encoded octets
 743      $sanitized = preg_replace('|%[a-fA-F0-9][a-fA-F0-9]|', '', $class);
 744  
 745      //Limit to A-Z,a-z,0-9,'-'
 746      $sanitized = preg_replace('/[^A-Za-z0-9-]/', '', $sanitized);
 747  
 748      if ('' == $sanitized)
 749          $sanitized = $fallback;
 750  
 751      return apply_filters('sanitize_html_class',$sanitized, $class, $fallback);
 752  }
 753  
 754  /**
 755   * Converts a number of characters from a string.
 756   *
 757   * Metadata tags <<title>> and <<category>> are removed, <<br>> and <<hr>> are
 758   * converted into correct XHTML and Unicode characters are converted to the
 759   * valid range.
 760   *
 761   * @since 0.71
 762   *
 763   * @param string $content String of characters to be converted.
 764   * @param string $deprecated Not used.
 765   * @return string Converted string.
 766   */
 767  function convert_chars($content, $deprecated = '') {
 768      // Translation of invalid Unicode references range to valid range
 769      $wp_htmltranswinuni = array(
 770      '&#128;' => '&#8364;', // the Euro sign
 771      '&#129;' => '',
 772      '&#130;' => '&#8218;', // these are Windows CP1252 specific characters
 773      '&#131;' => '&#402;',  // they would look weird on non-Windows browsers
 774      '&#132;' => '&#8222;',
 775      '&#133;' => '&#8230;',
 776      '&#134;' => '&#8224;',
 777      '&#135;' => '&#8225;',
 778      '&#136;' => '&#710;',
 779      '&#137;' => '&#8240;',
 780      '&#138;' => '&#352;',
 781      '&#139;' => '&#8249;',
 782      '&#140;' => '&#338;',
 783      '&#141;' => '',
 784      '&#142;' => '&#382;',
 785      '&#143;' => '',
 786      '&#144;' => '',
 787      '&#145;' => '&#8216;',
 788      '&#146;' => '&#8217;',
 789      '&#147;' => '&#8220;',
 790      '&#148;' => '&#8221;',
 791      '&#149;' => '&#8226;',
 792      '&#150;' => '&#8211;',
 793      '&#151;' => '&#8212;',
 794      '&#152;' => '&#732;',
 795      '&#153;' => '&#8482;',
 796      '&#154;' => '&#353;',
 797      '&#155;' => '&#8250;',
 798      '&#156;' => '&#339;',
 799      '&#157;' => '',
 800      '&#158;' => '',
 801      '&#159;' => '&#376;'
 802      );
 803  
 804      // Remove metadata tags
 805      $content = preg_replace('/<title>(.+?)<\/title>/','',$content);
 806      $content = preg_replace('/<category>(.+?)<\/category>/','',$content);
 807  
 808      // Converts lone & characters into &#38; (a.k.a. &amp;)
 809      $content = preg_replace('/&([^#])(?![a-z1-4]{1,8};)/i', '&#038;$1', $content);
 810  
 811      // Fix Word pasting
 812      $content = strtr($content, $wp_htmltranswinuni);
 813  
 814      // Just a little XHTML help
 815      $content = str_replace('<br>', '<br />', $content);
 816      $content = str_replace('<hr>', '<hr />', $content);
 817  
 818      return $content;
 819  }
 820  
 821  /**
 822   * Callback used to change %uXXXX to &#YYY; syntax
 823   *
 824   * @since 2.8?
 825   *
 826   * @param array $matches Single Match
 827   * @return string An HTML entity
 828   */
 829  function funky_javascript_callback($matches) {
 830      return "&#".base_convert($matches[1],16,10).";";
 831  }
 832  
 833  /**
 834   * Fixes javascript bugs in browsers.
 835   *
 836   * Converts unicode characters to HTML numbered entities.
 837   *
 838   * @since 1.5.0
 839   * @uses $is_macIE
 840   * @uses $is_winIE
 841   *
 842   * @param string $text Text to be made safe.
 843   * @return string Fixed text.
 844   */
 845  function funky_javascript_fix($text) {
 846      // Fixes for browsers' javascript bugs
 847      global $is_macIE, $is_winIE;
 848  
 849      if ( $is_winIE || $is_macIE )
 850          $text =  preg_replace_callback("/\%u([0-9A-F]{4,4})/",
 851                             "funky_javascript_callback",
 852                             $text);
 853  
 854      return $text;
 855  }
 856  
 857  /**
 858   * Will only balance the tags if forced to and the option is set to balance tags.
 859   *
 860   * The option 'use_balanceTags' is used for whether the tags will be balanced.
 861   * Both the $force parameter and 'use_balanceTags' option will have to be true
 862   * before the tags will be balanced.
 863   *
 864   * @since 0.71
 865   *
 866   * @param string $text Text to be balanced
 867   * @param bool $force Forces balancing, ignoring the value of the option. Default false.
 868   * @return string Balanced text
 869   */
 870  function balanceTags( $text, $force = false ) {
 871      if ( !$force && get_option('use_balanceTags') == 0 )
 872          return $text;
 873      return force_balance_tags( $text );
 874  }
 875  
 876  /**
 877   * Balances tags of string using a modified stack.
 878   *
 879   * @since 2.0.4
 880   *
 881   * @author Leonard Lin <leonard@acm.org>
 882   * @license GPL v2.0
 883   * @copyright November 4, 2001
 884   * @version 1.1
 885   * @todo Make better - change loop condition to $text in 1.2
 886   * @internal Modified by Scott Reilly (coffee2code) 02 Aug 2004
 887   *        1.1  Fixed handling of append/stack pop order of end text
 888   *             Added Cleaning Hooks
 889   *        1.0  First Version
 890   *
 891   * @param string $text Text to be balanced.
 892   * @return string Balanced text.
 893   */
 894  function force_balance_tags( $text ) {
 895      $tagstack = array(); $stacksize = 0; $tagqueue = ''; $newtext = '';
 896      $single_tags = array('br', 'hr', 'img', 'input'); //Known single-entity/self-closing tags
 897      $nestable_tags = array('blockquote', 'div', 'span'); //Tags that can be immediately nested within themselves
 898  
 899      # WP bug fix for comments - in case you REALLY meant to type '< !--'
 900      $text = str_replace('< !--', '<    !--', $text);
 901      # WP bug fix for LOVE <3 (and other situations with '<' before a number)
 902      $text = preg_replace('#<([0-9]{1})#', '&lt;$1', $text);
 903  
 904      while (preg_match("/<(\/?\w*)\s*([^>]*)>/",$text,$regex)) {
 905          $newtext .= $tagqueue;
 906  
 907          $i = strpos($text,$regex[0]);
 908          $l = strlen($regex[0]);
 909  
 910          // clear the shifter
 911          $tagqueue = '';
 912          // Pop or Push
 913          if ( isset($regex[1][0]) && '/' == $regex[1][0] ) { // End Tag
 914              $tag = strtolower(substr($regex[1],1));
 915              // if too many closing tags
 916              if($stacksize <= 0) {
 917                  $tag = '';
 918                  //or close to be safe $tag = '/' . $tag;
 919              }
 920              // if stacktop value = tag close value then pop
 921              else if ($tagstack[$stacksize - 1] == $tag) { // found closing tag
 922                  $tag = '</' . $tag . '>'; // Close Tag
 923                  // Pop
 924                  array_pop ($tagstack);
 925                  $stacksize--;
 926              } else { // closing tag not at top, search for it
 927                  for ($j=$stacksize-1;$j>=0;$j--) {
 928                      if ($tagstack[$j] == $tag) {
 929                      // add tag to tagqueue
 930                          for ($k=$stacksize-1;$k>=$j;$k--){
 931                              $tagqueue .= '</' . array_pop ($tagstack) . '>';
 932                              $stacksize--;
 933                          }
 934                          break;
 935                      }
 936                  }
 937                  $tag = '';
 938              }
 939          } else { // Begin Tag
 940              $tag = strtolower($regex[1]);
 941  
 942              // Tag Cleaning
 943  
 944              // If self-closing or '', don't do anything.
 945              if((substr($regex[2],-1) == '/') || ($tag == '')) {
 946              }
 947              // ElseIf it's a known single-entity tag but it doesn't close itself, do so
 948              elseif ( in_array($tag, $single_tags) ) {
 949                  $regex[2] .= '/';
 950              } else {    // Push the tag onto the stack
 951                  // If the top of the stack is the same as the tag we want to push, close previous tag
 952                  if (($stacksize > 0) && !in_array($tag, $nestable_tags) && ($tagstack[$stacksize - 1] == $tag)) {
 953                      $tagqueue = '</' . array_pop ($tagstack) . '>';
 954                      $stacksize--;
 955                  }
 956                  $stacksize = array_push ($tagstack, $tag);
 957              }
 958  
 959              // Attributes
 960              $attributes = $regex[2];
 961              if($attributes) {
 962                  $attributes = ' '.$attributes;
 963              }
 964              $tag = '<'.$tag.$attributes.'>';
 965              //If already queuing a close tag, then put this tag on, too
 966              if ($tagqueue) {
 967                  $tagqueue .= $tag;
 968                  $tag = '';
 969              }
 970          }
 971          $newtext .= substr($text,0,$i) . $tag;
 972          $text = substr($text,$i+$l);
 973      }
 974  
 975      // Clear Tag Queue
 976      $newtext .= $tagqueue;
 977  
 978      // Add Remaining text
 979      $newtext .= $text;
 980  
 981      // Empty Stack
 982      while($x = array_pop($tagstack)) {
 983          $newtext .= '</' . $x . '>'; // Add remaining tags to close
 984      }
 985  
 986      // WP fix for the bug with HTML comments
 987      $newtext = str_replace("< !--","<!--",$newtext);
 988      $newtext = str_replace("<    !--","< !--",$newtext);
 989  
 990      return $newtext;
 991  }
 992  
 993  /**
 994   * Acts on text which is about to be edited.
 995   *
 996   * Unless $richedit is set, it is simply a holder for the 'format_to_edit'
 997   * filter. If $richedit is set true htmlspecialchars() will be run on the
 998   * content, converting special characters to HTMl entities.
 999   *
1000   * @since 0.71
1001   *
1002   * @param string $content The text about to be edited.
1003   * @param bool $richedit Whether or not the $content should pass through htmlspecialchars(). Default false.
1004   * @return string The text after the filter (and possibly htmlspecialchars()) has been run.
1005   */
1006  function format_to_edit($content, $richedit = false) {
1007      $content = apply_filters('format_to_edit', $content);
1008      if (! $richedit )
1009          $content = htmlspecialchars($content);
1010      return $content;
1011  }
1012  
1013  /**
1014   * Holder for the 'format_to_post' filter.
1015   *
1016   * @since 0.71
1017   *
1018   * @param string $content The text to pass through the filter.
1019   * @return string Text returned from the 'format_to_post' filter.
1020   */
1021  function format_to_post($content) {
1022      $content = apply_filters('format_to_post', $content);
1023      return $content;
1024  }
1025  
1026  /**
1027   * Add leading zeros when necessary.
1028   *
1029   * If you set the threshold to '4' and the number is '10', then you will get
1030   * back '0010'. If you set the number to '4' and the number is '5000', then you
1031   * will get back '5000'.
1032   *
1033   * Uses sprintf to append the amount of zeros based on the $threshold parameter
1034   * and the size of the number. If the number is large enough, then no zeros will
1035   * be appended.
1036   *
1037   * @since 0.71
1038   *
1039   * @param mixed $number Number to append zeros to if not greater than threshold.
1040   * @param int $threshold Digit places number needs to be to not have zeros added.
1041   * @return string Adds leading zeros to number if needed.
1042   */
1043  function zeroise($number, $threshold) {
1044      return sprintf('%0'.$threshold.'s', $number);
1045  }
1046  
1047  /**
1048   * Adds backslashes before letters and before a number at the start of a string.
1049   *
1050   * @since 0.71
1051   *
1052   * @param string $string Value to which backslashes will be added.
1053   * @return string String with backslashes inserted.
1054   */
1055  function backslashit($string) {
1056      $string = preg_replace('/^([0-9])/', '\\\\\\\\\1', $string);
1057      $string = preg_replace('/([a-z])/i', '\\\\\1', $string);
1058      return $string;
1059  }
1060  
1061  /**
1062   * Appends a trailing slash.
1063   *
1064   * Will remove trailing slash if it exists already before adding a trailing
1065   * slash. This prevents double slashing a string or path.
1066   *
1067   * The primary use of this is for paths and thus should be used for paths. It is
1068   * not restricted to paths and offers no specific path support.
1069   *
1070   * @since 1.2.0
1071   * @uses untrailingslashit() Unslashes string if it was slashed already.
1072   *
1073   * @param string $string What to add the trailing slash to.
1074   * @return string String with trailing slash added.
1075   */
1076  function trailingslashit($string) {
1077      return untrailingslashit($string) . '/';
1078  }
1079  
1080  /**
1081   * Removes trailing slash if it exists.
1082   *
1083   * The primary use of this is for paths and thus should be used for paths. It is
1084   * not restricted to paths and offers no specific path support.
1085   *
1086   * @since 2.2.0
1087   *
1088   * @param string $string What to remove the trailing slash from.
1089   * @return string String without the trailing slash.
1090   */
1091  function untrailingslashit($string) {
1092      return rtrim($string, '/');
1093  }
1094  
1095  /**
1096   * Adds slashes to escape strings.
1097   *
1098   * Slashes will first be removed if magic_quotes_gpc is set, see {@link
1099   * http://www.php.net/magic_quotes} for more details.
1100   *
1101   * @since 0.71
1102   *
1103   * @param string $gpc The string returned from HTTP request data.
1104   * @return string Returns a string escaped with slashes.
1105   */
1106  function addslashes_gpc($gpc) {
1107      global $wpdb;
1108  
1109      if (get_magic_quotes_gpc()) {
1110          $gpc = stripslashes($gpc);
1111      }
1112  
1113      return $wpdb->escape($gpc);
1114  }
1115  
1116  /**
1117   * Navigates through an array and removes slashes from the values.
1118   *
1119   * If an array is passed, the array_map() function causes a callback to pass the
1120   * value back to the function. The slashes from this value will removed.
1121   *
1122   * @since 2.0.0
1123   *
1124   * @param array|string $value The array or string to be striped.
1125   * @return array|string Stripped array (or string in the callback).
1126   */
1127  function stripslashes_deep($value) {
1128      $value = is_array($value) ? array_map('stripslashes_deep', $value) : stripslashes($value);
1129      return $value;
1130  }
1131  
1132  /**
1133   * Navigates through an array and encodes the values to be used in a URL.
1134   *
1135   * Uses a callback to pass the value of the array back to the function as a
1136   * string.
1137   *
1138   * @since 2.2.0
1139   *
1140   * @param array|string $value The array or string to be encoded.
1141   * @return array|string $value The encoded array (or string from the callback).
1142   */
1143  function urlencode_deep($value) {
1144      $value = is_array($value) ? array_map('urlencode_deep', $value) : urlencode($value);
1145      return $value;
1146  }
1147  
1148  /**
1149   * Converts email addresses characters to HTML entities to block spam bots.
1150   *
1151   * @since 0.71
1152   *
1153   * @param string $emailaddy Email address.
1154   * @param int $mailto Optional. Range from 0 to 1. Used for encoding.
1155   * @return string Converted email address.
1156   */
1157  function antispambot($emailaddy, $mailto=0) {
1158      $emailNOSPAMaddy = '';
1159      srand ((float) microtime() * 1000000);
1160      for ($i = 0; $i < strlen($emailaddy); $i = $i + 1) {
1161          $j = floor(rand(0, 1+$mailto));
1162          if ($j==0) {
1163              $emailNOSPAMaddy .= '&#'.ord(substr($emailaddy,$i,1)).';';
1164          } elseif ($j==1) {
1165              $emailNOSPAMaddy .= substr($emailaddy,$i,1);
1166          } elseif ($j==2) {
1167              $emailNOSPAMaddy .= '%'.zeroise(dechex(ord(substr($emailaddy, $i, 1))), 2);
1168          }
1169      }
1170      $emailNOSPAMaddy = str_replace('@','&#64;',$emailNOSPAMaddy);
1171      return $emailNOSPAMaddy;
1172  }
1173  
1174  /**
1175   * Callback to convert URI match to HTML A element.
1176   *
1177   * This function was backported from 2.5.0 to 2.3.2. Regex callback for {@link
1178   * make_clickable()}.
1179   *
1180   * @since 2.3.2
1181   * @access private
1182   *
1183   * @param array $matches Single Regex Match.
1184   * @return string HTML A element with URI address.
1185   */
1186  function _make_url_clickable_cb($matches) {
1187      $url = $matches[2];
1188      $url = esc_url($url);
1189      if ( empty($url) )
1190          return $matches[0];
1191      return $matches[1] . "<a href=\"$url\" rel=\"nofollow\">$url</a>";
1192  }
1193  
1194  /**
1195   * Callback to convert URL match to HTML A element.
1196   *
1197   * This function was backported from 2.5.0 to 2.3.2. Regex callback for {@link
1198   * make_clickable()}.
1199   *
1200   * @since 2.3.2
1201   * @access private
1202   *
1203   * @param array $matches Single Regex Match.
1204   * @return string HTML A element with URL address.
1205   */
1206  function _make_web_ftp_clickable_cb($matches) {
1207      $ret = '';
1208      $dest = $matches[2];
1209      $dest = 'http://' . $dest;
1210      $dest = esc_url($dest);
1211      if ( empty($dest) )
1212          return $matches[0];
1213      // removed trailing [,;:] from URL
1214      if ( in_array(substr($dest, -1), array('.', ',', ';', ':')) === true ) {
1215          $ret = substr($dest, -1);
1216          $dest = substr($dest, 0, strlen($dest)-1);
1217      }
1218      return $matches[1] . "<a href=\"$dest\" rel=\"nofollow\">$dest</a>" . $ret;
1219  }
1220  
1221  /**
1222   * Callback to convert email address match to HTML A element.
1223   *
1224   * This function was backported from 2.5.0 to 2.3.2. Regex callback for {@link
1225   * make_clickable()}.
1226   *
1227   * @since 2.3.2
1228   * @access private
1229   *
1230   * @param array $matches Single Regex Match.
1231   * @return string HTML A element with email address.
1232   */
1233  function _make_email_clickable_cb($matches) {
1234      $email = $matches[2] . '@' . $matches[3];
1235      return $matches[1] . "<a href=\"mailto:$email\">$email</a>";
1236  }
1237  
1238  /**
1239   * Convert plaintext URI to HTML links.
1240   *
1241   * Converts URI, www and ftp, and email addresses. Finishes by fixing links
1242   * within links.
1243   *
1244   * @since 0.71
1245   *
1246   * @param string $ret Content to convert URIs.
1247   * @return string Content with converted URIs.
1248   */
1249  function make_clickable($ret) {
1250      $ret = ' ' . $ret;
1251      // in testing, using arrays here was found to be faster
1252      $ret = preg_replace_callback('#(?<=[\s>])(\()?([\w]+?://(?:[\w\\x80-\\xff\#$%&~/\-=?@\[\](+]|[.,;:](?![\s<])|(?(1)\)(?![\s<])|\)))+)#is', '_make_url_clickable_cb', $ret);
1253      $ret = preg_replace_callback('#([\s>])((www|ftp)\.[\w\\x80-\\xff\#$%&~/.\-;:=,?@\[\]+]+)#is', '_make_web_ftp_clickable_cb', $ret);
1254      $ret = preg_replace_callback('#([\s>])([.0-9a-z_+-]+)@(([0-9a-z-]+\.)+[0-9a-z]{2,})#i', '_make_email_clickable_cb', $ret);
1255      // this one is not in an array because we need it to run last, for cleanup of accidental links within links
1256      $ret = preg_replace("#(<a( [^>]+?>|>))<a [^>]+?>([^>]+?)</a></a>#i", "$1$3</a>", $ret);
1257      $ret = trim($ret);
1258      return $ret;
1259  }
1260  
1261  /**
1262   * Adds rel nofollow string to all HTML A elements in content.
1263   *
1264   * @since 1.5.0
1265   *
1266   * @param string $text Content that may contain HTML A elements.
1267   * @return string Converted content.
1268   */
1269  function wp_rel_nofollow( $text ) {
1270      global $wpdb;
1271      // This is a pre save filter, so text is already escaped.
1272      $text = stripslashes($text);
1273      $text = preg_replace_callback('|<a (.+?)>|i', 'wp_rel_nofollow_callback', $text);
1274      $text = $wpdb->escape($text);
1275      return $text;
1276  }
1277  
1278  /**
1279   * Callback to used to add rel=nofollow string to HTML A element.
1280   *
1281   * Will remove already existing rel="nofollow" and rel='nofollow' from the
1282   * string to prevent from invalidating (X)HTML.
1283   *
1284   * @since 2.3.0
1285   *
1286   * @param array $matches Single Match
1287   * @return string HTML A Element with rel nofollow.
1288   */
1289  function wp_rel_nofollow_callback( $matches ) {
1290      $text = $matches[1];
1291      $text = str_replace(array(' rel="nofollow"', " rel='nofollow'"), '', $text);
1292      return "<a $text rel=\"nofollow\">";
1293  }
1294  
1295  
1296  /**
1297   * Convert one smiley code to the icon graphic file equivalent.
1298   *
1299   * Looks up one smiley code in the $wpsmiliestrans global array and returns an
1300   * <img> string for that smiley.
1301   *
1302   * @global array $wpsmiliestrans
1303   * @since 2.8.0
1304   *
1305   * @param string $smiley Smiley code to convert to image.
1306   * @return string Image string for smiley.
1307   */
1308  function translate_smiley($smiley) {
1309      global $wpsmiliestrans;
1310  
1311      if (count($smiley) == 0) {
1312          return '';
1313      }
1314  
1315      $siteurl = get_option( 'siteurl' );
1316  
1317      $smiley = trim(reset($smiley));
1318      $img = $wpsmiliestrans[$smiley];
1319      $smiley_masked = esc_attr($smiley);
1320  
1321      return " <img src='$siteurl/wp-includes/images/smilies/$img' alt='$smiley_masked' class='wp-smiley' /> ";
1322  }
1323  
1324  
1325  /**
1326   * Convert text equivalent of smilies to images.
1327   *
1328   * Will only convert smilies if the option 'use_smilies' is true and the global
1329   * used in the function isn't empty.
1330   *
1331   * @since 0.71
1332   * @uses $wp_smiliessearch
1333   *
1334   * @param string $text Content to convert smilies from text.
1335   * @return string Converted content with text smilies replaced with images.
1336   */
1337  function convert_smilies($text) {
1338      global $wp_smiliessearch;
1339      $output = '';
1340      if ( get_option('use_smilies') && !empty($wp_smiliessearch) ) {
1341          // HTML loop taken from texturize function, could possible be consolidated
1342          $textarr = preg_split("/(<.*>)/U", $text, -1, PREG_SPLIT_DELIM_CAPTURE); // capture the tags as well as in between
1343          $stop = count($textarr);// loop stuff
1344          for ($i = 0; $i < $stop; $i++) {
1345              $content = $textarr[$i];
1346              if ((strlen($content) > 0) && ('<' != $content{0})) { // If it's not a tag
1347                  $content = preg_replace_callback($wp_smiliessearch, 'translate_smiley', $content);
1348              }
1349              $output .= $content;
1350          }
1351      } else {
1352          // return default text.
1353          $output = $text;
1354      }
1355      return $output;
1356  }
1357  
1358  /**
1359   * Verifies that an email is valid.
1360   *
1361   * Does not grok i18n domains. Not RFC compliant.
1362   *
1363   * @since 0.71
1364   *
1365   * @param string $email Email address to verify.
1366   * @param boolean $check_dns Whether to check the DNS for the domain using checkdnsrr().
1367   * @return string|bool Either false or the valid email address.
1368   */
1369  function is_email( $email, $check_dns = false ) {
1370      // Test for the minimum length the email can be
1371      if ( strlen( $email ) < 3 ) {
1372          return apply_filters( 'is_email', false, $email, 'email_too_short' );
1373      }
1374  
1375      // Test for an @ character after the first position
1376      if ( strpos( $email, '@', 1 ) === false ) {
1377          return apply_filters( 'is_email', false, $email, 'email_no_at' );
1378      }
1379  
1380      // Split out the local and domain parts
1381      list( $local, $domain ) = explode( '@', $email, 2 );
1382  
1383      // LOCAL PART
1384      // Test for invalid characters
1385      if ( !preg_match( '/^[a-zA-Z0-9!#$%&\'*+\/=?^_`{|}~\.-]+$/', $local ) ) {
1386          return apply_filters( 'is_email', false, $email, 'local_invalid_chars' );
1387      }
1388  
1389      // DOMAIN PART
1390      // Test for sequences of periods
1391      if ( preg_match( '/\.{2,}/', $domain ) ) {
1392          return apply_filters( 'is_email', false, $email, 'domain_period_sequence' );
1393      }
1394  
1395      // Test for leading and trailing periods and whitespace
1396      if ( trim( $domain, " \t\n\r\0\x0B." ) !== $domain ) {
1397          return apply_filters( 'is_email', false, $email, 'domain_period_limits' );
1398      }
1399  
1400      // Split the domain into subs
1401      $subs = explode( '.', $domain );
1402  
1403      // Assume the domain will have at least two subs
1404      if ( 2 > count( $subs ) ) {
1405          return apply_filters( 'is_email', false, $email, 'domain_no_periods' );
1406      }
1407  
1408      // Loop through each sub
1409      foreach ( $subs as $sub ) {
1410          // Test for leading and trailing hyphens and whitespace
1411          if ( trim( $sub, " \t\n\r\0\x0B-" ) !== $sub ) {
1412              return apply_filters( 'is_email', false, $email, 'sub_hyphen_limits' );
1413          }
1414  
1415          // Test for invalid characters
1416          if ( !preg_match('/^[a-z0-9-]+$/i', $sub ) ) {
1417              return apply_filters( 'is_email', false, $email, 'sub_invalid_chars' );
1418          }
1419      }
1420  
1421      // DNS
1422      // Check the domain has a valid MX and A resource record
1423      if ( $check_dns && function_exists( 'checkdnsrr' ) && !( checkdnsrr( $domain . '.', 'MX' ) || checkdnsrr( $domain . '.', 'A' ) ) ) {
1424          return apply_filters( 'is_email', false, $email, 'dns_no_rr' );
1425      }
1426  
1427      // Congratulations your email made it!
1428      return apply_filters( 'is_email', $email, $email, null );
1429  }
1430  
1431  /**
1432   * Convert to ASCII from email subjects.
1433   *
1434   * @since 1.2.0
1435   * @usedby wp_mail() handles charsets in email subjects
1436   *
1437   * @param string $string Subject line
1438   * @return string Converted string to ASCII
1439   */
1440  function wp_iso_descrambler($string) {
1441      /* this may only work with iso-8859-1, I'm afraid */
1442      if (!preg_match('#\=\?(.+)\?Q\?(.+)\?\=#i', $string, $matches)) {
1443          return $string;
1444      } else {
1445          $subject = str_replace('_', ' ', $matches[2]);
1446          $subject = preg_replace_callback('#\=([0-9a-f]{2})#i', create_function('$match', 'return chr(hexdec(strtolower($match[1])));'), $subject);
1447          return $subject;
1448      }
1449  }
1450  
1451  /**
1452   * Returns a date in the GMT equivalent.
1453   *
1454   * Requires and returns a date in the Y-m-d H:i:s format. Simply subtracts the
1455   * value of the 'gmt_offset' option.
1456   *
1457   * @since 1.2.0
1458   *
1459   * @uses get_option() to retrieve the the value of 'gmt_offset'.
1460   * @param string $string The date to be converted.
1461   * @return string GMT version of the date provided.
1462   */
1463  function get_gmt_from_date($string) {
1464      preg_match('#([0-9]{1,4})-([0-9]{1,2})-([0-9]{1,2}) ([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})#', $string, $matches);
1465      $string_time = gmmktime($matches[4], $matches[5], $matches[6], $matches[2], $matches[3], $matches[1]);
1466      $string_gmt = gmdate('Y-m-d H:i:s', $string_time - get_option('gmt_offset') * 3600);
1467      return $string_gmt;
1468  }
1469  
1470  /**
1471   * Converts a GMT date into the correct format for the blog.
1472   *
1473   * Requires and returns in the Y-m-d H:i:s format. Simply adds the value of
1474   * gmt_offset.
1475   *
1476   * @since 1.2.0
1477   *
1478   * @param string $string The date to be converted.
1479   * @return string Formatted date relative to the GMT offset.
1480   */
1481  function get_date_from_gmt($string) {
1482      preg_match('#([0-9]{1,4})-([0-9]{1,2})-([0-9]{1,2}) ([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})#', $string, $matches);
1483      $string_time = gmmktime($matches[4], $matches[5], $matches[6], $matches[2], $matches[3], $matches[1]);
1484      $string_localtime = gmdate('Y-m-d H:i:s', $string_time + get_option('gmt_offset')*3600);
1485      return $string_localtime;
1486  }
1487  
1488  /**
1489   * Computes an offset in seconds from an iso8601 timezone.
1490   *
1491   * @since 1.5.0
1492   *
1493   * @param string $timezone Either 'Z' for 0 offset or '±hhmm'.
1494   * @return int|float The offset in seconds.
1495   */
1496  function iso8601_timezone_to_offset($timezone) {
1497      // $timezone is either 'Z' or '[+|-]hhmm'
1498      if ($timezone == 'Z') {
1499          $offset = 0;
1500      } else {
1501          $sign    = (substr($timezone, 0, 1) == '+') ? 1 : -1;
1502          $hours   = intval(substr($timezone, 1, 2));
1503          $minutes = intval(substr($timezone, 3, 4)) / 60;
1504          $offset  = $sign * 3600 * ($hours + $minutes);
1505      }
1506      return $offset;
1507  }
1508  
1509  /**
1510   * Converts an iso8601 date to MySQL DateTime format used by post_date[_gmt].
1511   *
1512   * @since 1.5.0
1513   *
1514   * @param string $date_string Date and time in ISO 8601 format {@link http://en.wikipedia.org/wiki/ISO_8601}.
1515   * @param string $timezone Optional. If set to GMT returns the time minus gmt_offset. Default is 'user'.
1516   * @return string The date and time in MySQL DateTime format - Y-m-d H:i:s.
1517   */
1518  function iso8601_to_datetime($date_string, $timezone = 'user') {
1519      $timezone = strtolower($timezone);
1520  
1521      if ($timezone == 'gmt') {
1522  
1523          preg_match('#([0-9]{4})([0-9]{2})([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})(Z|[\+|\-][0-9]{2,4}){0,1}#', $date_string, $date_bits);
1524  
1525          if (!empty($date_bits[7])) { // we have a timezone, so let's compute an offset
1526              $offset = iso8601_timezone_to_offset($date_bits[7]);
1527          } else { // we don't have a timezone, so we assume user local timezone (not server's!)
1528              $offset = 3600 * get_option('gmt_offset');
1529          }
1530  
1531          $timestamp = gmmktime($date_bits[4], $date_bits[5], $date_bits[6], $date_bits[2], $date_bits[3], $date_bits[1]);
1532          $timestamp -= $offset;
1533  
1534          return gmdate('Y-m-d H:i:s', $timestamp);
1535  
1536      } else if ($timezone == 'user') {
1537          return preg_replace('#([0-9]{4})([0-9]{2})([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})(Z|[\+|\-][0-9]{2,4}){0,1}#', '$1-$2-$3 $4:$5:$6', $date_string);
1538      }
1539  }
1540  
1541  /**
1542   * Adds a element attributes to open links in new windows.
1543   *
1544   * Comment text in popup windows should be filtered through this. Right now it's
1545   * a moderately dumb function, ideally it would detect whether a target or rel
1546   * attribute was already there and adjust its actions accordingly.
1547   *
1548   * @since 0.71
1549   *
1550   * @param string $text Content to replace links to open in a new window.
1551   * @return string Content that has filtered links.
1552   */
1553  function popuplinks($text) {
1554      $text = preg_replace('/<a (.+?)>/i', "<a $1 target='_blank' rel='external'>", $text);
1555      return $text;
1556  }
1557  
1558  /**
1559   * Strips out all characters that are not allowable in an email.
1560   *
1561   * @since 1.5.0
1562   *
1563   * @param string $email Email address to filter.
1564   * @return string Filtered email address.
1565   */
1566  function sanitize_email( $email ) {
1567      // Test for the minimum length the email can be
1568      if ( strlen( $email ) < 3 ) {
1569          return apply_filters( 'sanitize_email', '', $email, 'email_too_short' );
1570      }
1571  
1572      // Test for an @ character after the first position
1573      if ( strpos( $email, '@', 1 ) === false ) {
1574          return apply_filters( 'sanitize_email', '', $email, 'email_no_at' );
1575      }
1576  
1577      // Split out the local and domain parts
1578      list( $local, $domain ) = explode( '@', $email, 2 );
1579  
1580      // LOCAL PART
1581      // Test for invalid characters
1582      $local = preg_replace( '/[^a-zA-Z0-9!#$%&\'*+\/=?^_`{|}~\.-]/', '', $local );
1583      if ( '' === $local ) {
1584          return apply_filters( 'sanitize_email', '', $email, 'local_invalid_chars' );
1585      }
1586  
1587      // DOMAIN PART
1588      // Test for sequences of periods
1589      $domain = preg_replace( '/\.{2,}/', '', $domain );
1590      if ( '' === $domain ) {
1591          return apply_filters( 'sanitize_email', '', $email, 'domain_period_sequence' );
1592      }
1593  
1594      // Test for leading and trailing periods and whitespace
1595      $domain = trim( $domain, " \t\n\r\0\x0B." );
1596      if ( '' === $domain ) {
1597          return apply_filters( 'sanitize_email', '', $email, 'domain_period_limits' );
1598      }
1599  
1600      // Split the domain into subs
1601      $subs = explode( '.', $domain );
1602  
1603      // Assume the domain will have at least two subs
1604      if ( 2 > count( $subs ) ) {
1605          return apply_filters( 'sanitize_email', '', $email, 'domain_no_periods' );
1606      }
1607  
1608      // Create an array that will contain valid subs
1609      $new_subs = array();
1610  
1611      // Loop through each sub
1612      foreach ( $subs as $sub ) {
1613          // Test for leading and trailing hyphens
1614          $sub = trim( $sub, " \t\n\r\0\x0B-" );
1615  
1616          // Test for invalid characters
1617          $sub = preg_replace( '/^[^a-z0-9-]+$/i', '', $sub );
1618  
1619          // If there's anything left, add it to the valid subs
1620          if ( '' !== $sub ) {
1621              $new_subs[] = $sub;
1622          }
1623      }
1624  
1625      // If there aren't 2 or more valid subs
1626      if ( 2 > count( $new_subs ) ) {
1627          return apply_filters( 'sanitize_email', '', $email, 'domain_no_valid_subs' );
1628      }
1629  
1630      // Join valid subs into the new domain
1631      $domain = join( '.', $new_subs );
1632  
1633      // Put the email back together
1634      $email = $local . '@' . $domain;
1635  
1636      // Congratulations your email made it!
1637      return apply_filters( 'sanitize_email', $email, $email, null );
1638  }
1639  
1640  /**
1641   * Determines the difference between two timestamps.
1642   *
1643   * The difference is returned in a human readable format such as "1 hour",
1644   * "5 mins", "2 days".
1645   *
1646   * @since 1.5.0
1647   *
1648   * @param int $from Unix timestamp from which the difference begins.
1649   * @param int $to Optional. Unix timestamp to end the time difference. Default becomes time() if not set.
1650   * @return string Human readable time difference.
1651   */
1652  function human_time_diff( $from, $to = '' ) {
1653      if ( empty($to) )
1654          $to = time();
1655      $diff = (int) abs($to - $from);
1656      if ($diff <= 3600) {
1657          $mins = round($diff / 60);
1658          if ($mins <= 1) {
1659              $mins = 1;
1660          }
1661          $since = sprintf(_n('%s min', '%s mins', $mins), $mins);
1662      } else if (($diff <= 86400) && ($diff > 3600)) {
1663          $hours = round($diff / 3600);
1664          if ($hours <= 1) {
1665              $hours = 1;
1666          }
1667          $since = sprintf(_n('%s hour', '%s hours', $hours), $hours);
1668      } elseif ($diff >= 86400) {
1669          $days = round($diff / 86400);
1670          if ($days <= 1) {
1671              $days = 1;
1672          }
1673          $since = sprintf(_n('%s day', '%s days', $days), $days);
1674      }
1675      return $since;
1676  }
1677  
1678  /**
1679   * Generates an excerpt from the content, if needed.
1680   *
1681   * The excerpt word amount will be 55 words and if the amount is greater than
1682   * that, then the string '[...]' will be appended to the excerpt. If the string
1683   * is less than 55 words, then the content will be returned as is.
1684   *
1685   * @since 1.5.0
1686   *
1687   * @param string $text The exerpt. If set to empty an excerpt is generated.
1688   * @return string The excerpt.
1689   */
1690  function wp_trim_excerpt($text) {
1691      $raw_excerpt = $text;
1692      if ( '' == $text ) {
1693          $text = get_the_content('');
1694  
1695          $text = strip_shortcodes( $text );
1696  
1697          $text = apply_filters('the_content', $text);
1698          $text = str_replace(']]>', ']]&gt;', $text);
1699          $text = strip_tags($text);
1700          $excerpt_length = apply_filters('excerpt_length', 55);
1701          $words = explode(' ', $text, $excerpt_length + 1);
1702          if (count($words) > $excerpt_length) {
1703              array_pop($words);
1704              array_push($words, '[...]');
1705              $text = implode(' ', $words);
1706          }
1707      }
1708      return apply_filters('wp_trim_excerpt', $text, $raw_excerpt);
1709  }
1710  
1711  /**
1712   * Converts named entities into numbered entities.
1713   *
1714   * @since 1.5.1
1715   *
1716   * @param string $text The text within which entities will be converted.
1717   * @return string Text with converted entities.
1718   */
1719  function ent2ncr($text) {
1720      $to_ncr = array(
1721          '&quot;' => '&#34;',
1722          '&amp;' => '&#38;',
1723          '&frasl;' => '&#47;',
1724          '&lt;' => '&#60;',
1725          '&gt;' => '&#62;',
1726          '|' => '&#124;',
1727          '&nbsp;' => '&#160;',
1728          '&iexcl;' => '&#161;',
1729          '&cent;' => '&#162;',
1730          '&pound;' => '&#163;',
1731          '&curren;' => '&#164;',
1732          '&yen;' => '&#165;',
1733          '&brvbar;' => '&#166;',
1734          '&brkbar;' => '&#166;',
1735          '&sect;' => '&#167;',
1736          '&uml;' => '&#168;',
1737          '&die;' => '&#168;',
1738          '&copy;' => '&#169;',
1739          '&ordf;' => '&#170;',
1740          '&laquo;' => '&#171;',
1741          '&not;' => '&#172;',
1742          '&shy;' => '&#173;',
1743          '&reg;' => '&#174;',
1744          '&macr;' => '&#175;',
1745          '&hibar;' => '&#175;',
1746          '&deg;' => '&#176;',
1747          '&plusmn;' => '&#177;',
1748          '&sup2;' => '&#178;',
1749          '&sup3;' => '&#179;',
1750          '&acute;' => '&#180;',
1751          '&micro;' => '&#181;',
1752          '&para;' => '&#182;',
1753          '&middot;' => '&#183;',
1754          '&cedil;' => '&#184;',
1755          '&sup1;' => '&#185;',
1756          '&ordm;' => '&#186;',
1757          '&raquo;' => '&#187;',
1758          '&frac14;' => '&#188;',
1759          '&frac12;' => '&#189;',
1760          '&frac34;' => '&#190;',
1761          '&iquest;' => '&#191;',
1762          '&Agrave;' => '&#192;',
1763          '&Aacute;' => '&#193;',
1764          '&Acirc;' => '&#194;',
1765          '&Atilde;' => '&#195;',
1766          '&Auml;' => '&#196;',
1767          '&Aring;' => '&#197;',
1768          '&AElig;' => '&#198;',
1769          '&Ccedil;' => '&#199;',
1770          '&Egrave;' => '&#200;',
1771          '&Eacute;' => '&#201;',
1772          '&Ecirc;' => '&#202;',
1773          '&Euml;' => '&#203;',
1774          '&Igrave;' => '&#204;',
1775          '&Iacute;' => '&#205;',
1776          '&Icirc;' => '&#206;',
1777          '&Iuml;' => '&#207;',
1778          '&ETH;' => '&#208;',
1779          '&Ntilde;' => '&#209;',
1780          '&Ograve;' => '&#210;',
1781          '&Oacute;' => '&#211;',
1782          '&Ocirc;' => '&#212;',
1783          '&Otilde;' => '&#213;',
1784          '&Ouml;' => '&#214;',
1785          '&times;' => '&#215;',
1786          '&Oslash;' => '&#216;',
1787          '&Ugrave;' => '&#217;',
1788          '&Uacute;' => '&#218;',
1789          '&Ucirc;' => '&#219;',
1790          '&Uuml;' => '&#220;',
1791          '&Yacute;' => '&#221;',
1792          '&THORN;' => '&#222;',
1793          '&szlig;' => '&#223;',
1794          '&agrave;' => '&#224;',
1795          '&aacute;' => '&#225;',
1796          '&acirc;' => '&#226;',
1797          '&atilde;' => '&#227;',
1798          '&auml;' => '&#228;',
1799          '&aring;' => '&#229;',
1800          '&aelig;' => '&#230;',
1801          '&ccedil;' => '&#231;',
1802          '&egrave;' => '&#232;',
1803          '&eacute;' => '&#233;',
1804          '&ecirc;' => '&#234;',
1805          '&euml;' => '&#235;',
1806          '&igrave;' => '&#236;',
1807          '&iacute;' => '&#237;',
1808          '&icirc;' => '&#238;',
1809          '&iuml;' => '&#239;',
1810          '&eth;' => '&#240;',
1811          '&ntilde;' => '&#241;',
1812          '&ograve;' => '&#242;',
1813          '&oacute;' => '&#243;',
1814          '&ocirc;' => '&#244;',
1815          '&otilde;' => '&#245;',
1816          '&ouml;' => '&#246;',
1817          '&divide;' => '&#247;',
1818          '&oslash;' => '&#248;',
1819          '&ugrave;' => '&#249;',
1820          '&uacute;' => '&#250;',
1821          '&ucirc;' => '&#251;',
1822          '&uuml;' => '&#252;',
1823          '&yacute;' => '&#253;',
1824          '&thorn;' => '&#254;',
1825          '&yuml;' => '&#255;',
1826          '&OElig;' => '&#338;',
1827          '&oelig;' => '&#339;',
1828          '&Scaron;' => '&#352;',
1829          '&scaron;' => '&#353;',
1830          '&Yuml;' => '&#376;',
1831          '&fnof;' => '&#402;',
1832          '&circ;' => '&#710;',
1833          '&tilde;' => '&#732;',
1834          '&Alpha;' => '&#913;',
1835          '&Beta;' => '&#914;',
1836          '&Gamma;' => '&#915;',
1837          '&Delta;' => '&#916;',
1838          '&Epsilon;' => '&#917;',
1839          '&Zeta;' => '&#918;',
1840          '&Eta;' => '&#919;',
1841          '&Theta;' => '&#920;',
1842          '&Iota;' => '&#921;',
1843          '&Kappa;' => '&#922;',
1844          '&Lambda;' => '&#923;',
1845          '&Mu;' => '&#924;',
1846          '&Nu;' => '&#925;',
1847          '&Xi;' => '&#926;',
1848          '&Omicron;' => '&#927;',
1849          '&Pi;' => '&#928;',
1850          '&Rho;' => '&#929;',
1851          '&Sigma;' => '&#931;',
1852          '&Tau;' => '&#932;',
1853          '&Upsilon;' => '&#933;',
1854          '&Phi;' => '&#934;',
1855          '&Chi;' => '&#935;',
1856          '&Psi;' => '&#936;',
1857          '&Omega;' => '&#937;',
1858          '&alpha;' => '&#945;',
1859          '&beta;' => '&#946;',
1860          '&gamma;' => '&#947;',
1861          '&delta;' => '&#948;',
1862          '&epsilon;' => '&#949;',
1863          '&zeta;' => '&#950;',
1864          '&eta;' => '&#951;',
1865          '&theta;' => '&#952;',
1866          '&iota;' => '&#953;',
1867          '&kappa;' => '&#954;',
1868          '&lambda;' => '&#955;',
1869          '&mu;' => '&#956;',
1870          '&nu;' => '&#957;',
1871          '&xi;' => '&#958;',
1872          '&omicron;' => '&#959;',
1873          '&pi;' => '&#960;',
1874          '&rho;' => '&#961;',
1875          '&sigmaf;' => '&#962;',
1876          '&sigma;' => '&#963;',
1877          '&tau;' => '&#964;',
1878          '&upsilon;' => '&#965;',
1879          '&phi;' => '&#966;',
1880          '&chi;' => '&#967;',
1881          '&psi;' => '&#968;',
1882          '&omega;' => '&#969;',
1883          '&thetasym;' => '&#977;',
1884          '&upsih;' => '&#978;',
1885          '&piv;' => '&#982;',
1886          '&ensp;' => '&#8194;',
1887          '&emsp;' => '&#8195;',
1888          '&thinsp;' => '&#8201;',
1889          '&zwnj;' => '&#8204;',
1890          '&zwj;' => '&#8205;',
1891          '&lrm;' => '&#8206;',
1892          '&rlm;' => '&#8207;',
1893          '&ndash;' => '&#8211;',
1894          '&mdash;' => '&#8212;',
1895          '&lsquo;' => '&#8216;',
1896          '&rsquo;' => '&#8217;',
1897          '&sbquo;' => '&#8218;',
1898          '&ldquo;' => '&#8220;',
1899          '&rdquo;' => '&#8221;',
1900          '&bdquo;' => '&#8222;',
1901          '&dagger;' => '&#8224;',
1902          '&Dagger;' => '&#8225;',
1903          '&bull;' => '&#8226;',
1904          '&hellip;' => '&#8230;',
1905          '&permil;' => '&#8240;',
1906          '&prime;' => '&#8242;',
1907          '&Prime;' => '&#8243;',
1908          '&lsaquo;' => '&#8249;',
1909          '&rsaquo;' => '&#8250;',
1910          '&oline;' => '&#8254;',
1911          '&frasl;' => '&#8260;',
1912          '&euro;' => '&#8364;',
1913          '&image;' => '&#8465;',
1914          '&weierp;' => '&#8472;',
1915          '&real;' => '&#8476;',
1916          '&trade;' => '&#8482;',
1917          '&alefsym;' => '&#8501;',
1918          '&crarr;' => '&#8629;',
1919          '&lArr;' => '&#8656;',
1920          '&uArr;' => '&#8657;',
1921          '&rArr;' => '&#8658;',
1922          '&dArr;' => '&#8659;',
1923          '&hArr;' => '&#8660;',
1924          '&forall;' => '&#8704;',
1925          '&part;' => '&#8706;',
1926          '&exist;' => '&#8707;',
1927          '&empty;' => '&#8709;',
1928          '&nabla;' => '&#8711;',
1929          '&isin;' => '&#8712;',
1930          '&notin;' => '&#8713;',
1931          '&ni;' => '&#8715;',
1932          '&prod;' => '&#8719;',
1933          '&sum;' => '&#8721;',
1934          '&minus;' => '&#8722;',
1935          '&lowast;' => '&#8727;',
1936          '&radic;' => '&#8730;',
1937          '&prop;' => '&#8733;',
1938          '&infin;' => '&#8734;',
1939          '&ang;' => '&#8736;',
1940          '&and;' => '&#8743;',
1941          '&or;' => '&#8744;',
1942          '&cap;' => '&#8745;',
1943          '&cup;' => '&#8746;',
1944          '&int;' => '&#8747;',
1945          '&there4;' => '&#8756;',
1946          '&sim;' => '&#8764;',
1947          '&cong;' => '&#8773;',
1948          '&asymp;' => '&#8776;',
1949          '&ne;' => '&#8800;',
1950          '&equiv;' => '&#8801;',
1951          '&le;' => '&#8804;',
1952          '&ge;' => '&#8805;',
1953          '&sub;' => '&#8834;',
1954          '&sup;' => '&#8835;',
1955          '&nsub;' => '&#8836;',
1956          '&sube;' => '&#8838;',
1957          '&supe;' => '&#8839;',
1958          '&oplus;' => '&#8853;',
1959          '&otimes;' => '&#8855;',
1960          '&perp;' => '&#8869;',
1961          '&sdot;' => '&#8901;',
1962          '&lceil;' => '&#8968;',
1963          '&rceil;' => '&#8969;',
1964          '&lfloor;' => '&#8970;',
1965          '&rfloor;' => '&#8971;',
1966          '&lang;' => '&#9001;',
1967          '&rang;' => '&#9002;',
1968          '&larr;' => '&#8592;',
1969          '&uarr;' => '&#8593;',
1970          '&rarr;' => '&#8594;',
1971          '&darr;' => '&#8595;',
1972          '&harr;' => '&#8596;',
1973          '&loz;' => '&#9674;',
1974          '&spades;' => '&#9824;',
1975          '&clubs;' => '&#9827;',
1976          '&hearts;' => '&#9829;',
1977          '&diams;' => '&#9830;'
1978      );
1979  
1980      return str_replace( array_keys($to_ncr), array_values($to_ncr), $text );
1981  }
1982  
1983  /**
1984   * Formats text for the rich text editor.
1985   *
1986   * The filter 'richedit_pre' is applied here. If $text is empty the filter will
1987   * be applied to an empty string.
1988   *
1989   * @since 2.0.0
1990   *
1991   * @param string $text The text to be formatted.
1992   * @return string The formatted text after filter is applied.
1993   */
1994  function wp_richedit_pre($text) {
1995      // Filtering a blank results in an annoying <br />\n
1996      if ( empty($text) ) return apply_filters('richedit_pre', '');
1997  
1998      $output = convert_chars($text);
1999      $output = wpautop($output);
2000      $output = htmlspecialchars($output, ENT_NOQUOTES);
2001  
2002      return apply_filters('richedit_pre', $output);
2003  }
2004  
2005  /**
2006   * Formats text for the HTML editor.
2007   *
2008   * Unless $output is empty it will pass through htmlspecialchars before the
2009   * 'htmledit_pre' filter is applied.
2010   *
2011   * @since 2.5.0
2012   *
2013   * @param string $output The text to be formatted.
2014   * @return string Formatted text after filter applied.
2015   */
2016  function wp_htmledit_pre($output) {
2017      if ( !empty($output) )
2018          $output = htmlspecialchars($output, ENT_NOQUOTES); // convert only < > &
2019  
2020      return apply_filters('htmledit_pre', $output);
2021  }
2022  
2023  /**
2024   * Checks and cleans a URL.
2025   *
2026   * A number of characters are removed from the URL. If the URL is for displaying
2027   * (the default behaviour) amperstands are also replaced. The 'esc_url' filter
2028   * is applied to the returned cleaned URL.
2029   *
2030   * @since 1.2.0
2031   * @uses wp_kses_bad_protocol() To only permit protocols in the URL set
2032   *        via $protocols or the common ones set in the function.
2033   *
2034   * @param string $url The URL to be cleaned.
2035   * @param array $protocols Optional. An array of acceptable protocols.
2036   *        Defaults to 'http', 'https', 'ftp', 'ftps', 'mailto', 'news', 'irc', 'gopher', 'nntp', 'feed', 'telnet' if not set.
2037   * @param string $context Optional. How the URL will be used. Default is 'display'.
2038   * @return string The cleaned $url after the 'cleaned_url' filter is applied.
2039   */
2040  function clean_url( $url, $protocols = null, $context = 'display' ) {
2041      $original_url = $url;
2042  
2043      if ('' == $url) return $url;
2044      $url = preg_replace('|[^a-z0-9-~+_.?#=!&;,/:%@$\|*\'()\\x80-\\xff]|i', '', $url);
2045      $strip = array('%0d', '%0a', '%0D', '%0A');
2046      $url = _deep_replace($strip, $url);
2047      $url = str_replace(';//', '://', $url);
2048      /* If the URL doesn't appear to contain a scheme, we
2049       * presume it needs http:// appended (unless a relative
2050       * link starting with / or a php file).
2051       */
2052      if ( strpos($url, ':') === false &&
2053          substr( $url, 0, 1 ) != '/' && substr( $url, 0, 1 ) != '#' && !preg_match('/^[a-z0-9-]+?\.php/i', $url) )
2054          $url = 'http://' . $url;
2055  
2056      // Replace ampersands and single quotes only when displaying.
2057      if ( 'display' == $context ) {
2058          $url = preg_replace('/&([^#])(?![a-z]{2,8};)/', '&#038;$1', $url);
2059          $url = str_replace( "'", '&#039;', $url );
2060      }
2061  
2062      if ( !is_array($protocols) )
2063          $protocols = array('http', 'https', 'ftp', 'ftps', 'mailto', 'news', 'irc', 'gopher', 'nntp', 'feed', 'telnet');
2064      if ( wp_kses_bad_protocol( $url, $protocols ) != $url )
2065          return '';
2066  
2067      return apply_filters('clean_url', $url, $original_url, $context);
2068  }
2069  
2070  /**
2071   * Perform a deep string replace operation to ensure the values in $search are no longer present
2072   * 
2073   * Repeats the replacement operation until it no longer replaces anything so as to remove "nested" values
2074   * e.g. $subject = '%0%0%0DDD', $search ='%0D', $result ='' rather than the '%0%0DD' that
2075   * str_replace would return
2076   * 
2077   * @since 2.8.1
2078   * @access private
2079   * 
2080   * @param string|array $search
2081   * @param string $subject
2082   * @return string The processed string
2083   */
2084  function _deep_replace($search, $subject){
2085      $found = true;
2086      while($found) {
2087          $found = false;
2088          foreach( (array) $search as $val ) {
2089              while(strpos($subject, $val) !== false) {
2090                  $found = true;
2091                  $subject = str_replace($val, '', $subject);
2092              }
2093          }
2094      }
2095      
2096      return $subject;
2097  }
2098  
2099  /**
2100   * Escapes data for use in a MySQL query
2101   *
2102   * This is just a handy shortcut for $wpdb->escape(), for completeness' sake
2103   *
2104   * @since 2.8.0
2105   * @param string $sql Unescaped SQL data
2106   * @return string The cleaned $sql
2107   */
2108  function esc_sql( $sql ) {
2109      global $wpdb;
2110      return $wpdb->escape( $sql );
2111  }
2112  
2113  
2114  /**
2115   * Checks and cleans a URL.
2116   *
2117   * A number of characters are removed from the URL. If the URL is for displaying
2118   * (the default behaviour) amperstands are also replaced. The 'esc_url' filter
2119   * is applied to the returned cleaned URL.
2120   *
2121   * @since 2.8.0
2122   * @uses esc_url()
2123   * @uses wp_kses_bad_protocol() To only permit protocols in the URL set
2124   *        via $protocols or the common ones set in the function.
2125   *
2126   * @param string $url The URL to be cleaned.
2127   * @param array $protocols Optional. An array of acceptable protocols.
2128   *        Defaults to 'http', 'https', 'ftp', 'ftps', 'mailto', 'news', 'irc', 'gopher', 'nntp', 'feed', 'telnet' if not set.
2129   * @return string The cleaned $url after the 'cleaned_url' filter is applied.
2130   */
2131  function esc_url( $url, $protocols = null ) {
2132      return clean_url( $url, $protocols, 'display' );
2133  }
2134  
2135  /**
2136   * Performs esc_url() for database usage.
2137   *
2138   * @see esc_url()
2139   * @see esc_url()
2140   *
2141   * @since 2.8.0
2142   *
2143   * @param string $url The URL to be cleaned.
2144   * @param array $protocols An array of acceptable protocols.
2145   * @return string The cleaned URL.
2146   */
2147  function esc_url_raw( $url, $protocols = null ) {
2148      return clean_url( $url, $protocols, 'db' );
2149  }
2150  
2151  /**
2152   * Performs esc_url() for database or redirect usage.
2153   *
2154   * @see esc_url()
2155   * @deprecated 2.8.0
2156   *
2157   * @since 2.3.1
2158   *
2159   * @param string $url The URL to be cleaned.
2160   * @param array $protocols An array of acceptable protocols.
2161   * @return string The cleaned URL.
2162   */
2163  function sanitize_url( $url, $protocols = null ) {
2164      return clean_url( $url, $protocols, 'db' );
2165  }
2166  
2167  /**
2168   * Convert entities, while preserving already-encoded entities.
2169   *
2170   * @link http://www.php.net/htmlentities Borrowed from the PHP Manual user notes.
2171   *
2172   * @since 1.2.2
2173   *
2174   * @param string $myHTML The text to be converted.
2175   * @return string Converted text.
2176   */
2177  function htmlentities2($myHTML) {
2178      $translation_table = get_html_translation_table( HTML_ENTITIES, ENT_QUOTES );
2179      $translation_table[chr(38)] = '&';
2180      return preg_replace( "/&(?![A-Za-z]{0,4}\w{2,3};|#[0-9]{2,3};)/", "&amp;", strtr($myHTML, $translation_table) );
2181  }
2182  
2183  /**
2184   * Escape single quotes, specialchar double quotes, and fix line endings.
2185   *
2186   * The filter 'js_escape' is also applied here.
2187   *
2188   * @since 2.8.0
2189   *
2190   * @param string $text The text to be escaped.
2191   * @return string Escaped text.
2192   */
2193  function esc_js( $text ) {
2194      $safe_text = wp_check_invalid_utf8( $text );
2195      $safe_text = _wp_specialchars( $safe_text, ENT_COMPAT );
2196      $safe_text = preg_replace( '/&#(x)?0*(?(1)27|39);?/i', "'", stripslashes( $safe_text ) );
2197      $safe_text = preg_replace( "/\r?\n/", "\\n", addslashes( $safe_text ) );
2198      return apply_filters( 'js_escape', $safe_text, $text );
2199  }
2200  
2201  /**
2202   * Escape single quotes, specialchar double quotes, and fix line endings.
2203   *
2204   * The filter 'js_escape' is also applied by esc_js()
2205   *
2206   * @since 2.0.4
2207   *
2208   * @deprecated 2.8.0
2209   * @see esc_js()
2210   *
2211   * @param string $text The text to be escaped.
2212   * @return string Escaped text.
2213   */
2214  function js_escape( $text ) {
2215      return esc_js( $text );
2216  }
2217  
2218  /**
2219   * Escaping for HTML blocks.
2220   *
2221   * @since 2.8.0
2222   *
2223   * @param string $text
2224   * @return string
2225   */
2226  function esc_html( $text ) {
2227      $safe_text = wp_check_invalid_utf8( $text );
2228      $safe_text = _wp_specialchars( $safe_text, ENT_QUOTES );
2229      return apply_filters( 'esc_html', $safe_text, $text );
2230      return $text;
2231  }
2232  
2233  /**
2234   * Escaping for HTML blocks
2235   * @deprecated 2.8.0
2236   * @see esc_html()
2237   */
2238  function wp_specialchars( $string, $quote_style = ENT_NOQUOTES, $charset = false, $double_encode = false ) {
2239      if ( func_num_args() > 1 ) { // Maintain backwards compat for people passing additional args
2240          $args = func_get_args();
2241          return call_user_func_array( '_wp_specialchars', $args );
2242      } else {
2243          return esc_html( $string );
2244      }
2245  }
2246  
2247  /**
2248   * Escaping for HTML attributes.
2249   *
2250   * @since 2.8.0
2251   *
2252   * @param string $text
2253   * @return string
2254   */
2255  function esc_attr( $text ) {
2256      $safe_text = wp_check_invalid_utf8( $text );
2257      $safe_text = _wp_specialchars( $safe_text, ENT_QUOTES );
2258      return apply_filters( 'attribute_escape', $safe_text, $text );
2259  }
2260  
2261  /**
2262   * Escaping for HTML attributes.
2263   *
2264   * @since 2.0.6
2265   *
2266   * @deprecated 2.8.0
2267   * @see esc_attr()
2268   *
2269   * @param string $text
2270   * @return string
2271   */
2272  function attribute_escape( $text ) {
2273      return esc_attr( $text );
2274  }
2275  
2276  /**
2277   * Escape a HTML tag name.
2278   *
2279   * @since 2.5.0
2280   *
2281   * @param string $tag_name
2282   * @return string
2283   */
2284  function tag_escape($tag_name) {
2285      $safe_tag = strtolower( preg_replace('/[^a-zA-Z_:]/', '', $tag_name) );
2286      return apply_filters('tag_escape', $safe_tag, $tag_name);
2287  }
2288  
2289  /**
2290   * Escapes text for SQL LIKE special characters % and _.
2291   *
2292   * @since 2.5.0
2293   *
2294   * @param string $text The text to be escaped.
2295   * @return string text, safe for inclusion in LIKE query.
2296   */
2297  function like_escape($text) {
2298      return str_replace(array("%", "_"), array("\\%", "\\_"), $text);
2299  }
2300  
2301  /**
2302   * Convert full URL paths to absolute paths.
2303   *
2304   * Removes the http or https protocols and the domain. Keeps the path '/' at the
2305   * beginning, so it isn't a true relative link, but from the web root base.
2306   *
2307   * @since 2.1.0
2308   *
2309   * @param string $link Full URL path.
2310   * @return string Absolute path.
2311   */
2312  function wp_make_link_relative( $link ) {
2313      return preg_replace( '|https?://[^/]+(/.*)|i', '$1', $link );
2314  }
2315  
2316  /**
2317   * Sanitises various option values based on the nature of the option.
2318   *
2319   * This is basically a switch statement which will pass $value through a number
2320   * of functions depending on the $option.
2321   *
2322   * @since 2.0.5
2323   *
2324   * @param string $option The name of the option.
2325   * @param string $value The unsanitised value.
2326   * @return string Sanitized value.
2327   */
2328  function sanitize_option($option, $value) {
2329  
2330      switch ($option) {
2331          case 'admin_email':
2332              $value = sanitize_email($value);
2333              break;
2334  
2335          case 'thumbnail_size_w':
2336          case 'thumbnail_size_h':
2337          case 'medium_size_w':
2338          case 'medium_size_h':
2339          case 'large_size_w':
2340          case 'large_size_h':
2341          case 'default_post_edit_rows':
2342          case 'mailserver_port':
2343          case 'comment_max_links':
2344          case 'page_on_front':
2345          case 'rss_excerpt_length':
2346          case 'default_category':
2347          case 'default_email_category':
2348          case 'default_link_category':
2349          case 'close_comments_days_old':
2350          case 'comments_per_page':
2351          case 'thread_comments_depth':
2352              $value = abs((int) $value);
2353              break;
2354  
2355          case 'posts_per_page':
2356          case 'posts_per_rss':
2357              $value = (int) $value;
2358              if ( empty($value) ) $value = 1;
2359              if ( $value < -1 ) $value = abs($value);
2360              break;
2361  
2362          case 'default_ping_status':
2363          case 'default_comment_status':
2364              // Options that if not there have 0 value but need to be something like "closed"
2365              if ( $value == '0' || $value == '')
2366                  $value = 'closed';
2367              break;
2368  
2369          case 'blogdescription':
2370          case 'blogname':
2371              $value = addslashes($value);
2372              $value = wp_filter_post_kses( $value ); // calls stripslashes then addslashes
2373              $value = stripslashes($value);
2374              $value = esc_html( $value );
2375              break;
2376  
2377          case 'blog_charset':
2378              $value = preg_replace('/[^a-zA-Z0-9_-]/', '', $value); // strips slashes
2379              break;
2380  
2381          case 'date_format':
2382          case 'time_format':
2383          case 'mailserver_url':
2384          case 'mailserver_login':
2385          case 'mailserver_pass':
2386          case 'ping_sites':
2387          case 'upload_path':
2388              $value = strip_tags($value);
2389              $value = addslashes($value);
2390              $value = wp_filter_kses($value); // calls stripslashes then addslashes
2391              $value = stripslashes($value);
2392              break;
2393  
2394          case 'gmt_offset':
2395              $value = preg_replace('/[^0-9:.-]/', '', $value); // strips slashes
2396              break;
2397  
2398          case 'siteurl':
2399          case 'home':
2400              $value = stripslashes($value);
2401              $value = esc_url($value);
2402              break;
2403          default :
2404              $value = apply_filters("sanitize_option_{$option}", $value, $option);
2405              break;
2406      }
2407  
2408      return $value;
2409  }
2410  
2411  /**
2412   * Parses a string into variables to be stored in an array.
2413   *
2414   * Uses {@link http://www.php.net/parse_str parse_str()} and stripslashes if
2415   * {@link http://www.php.net/magic_quotes magic_quotes_gpc} is on.
2416   *
2417   * @since 2.2.1
2418   * @uses apply_filters() for the 'wp_parse_str' filter.
2419   *
2420   * @param string $string The string to be parsed.
2421   * @param array $array Variables will be stored in this array.
2422   */
2423  function wp_parse_str( $string, &$array ) {
2424      parse_str( $string, $array );
2425      if ( get_magic_quotes_gpc() )
2426          $array = stripslashes_deep( $array );
2427      $array = apply_filters( 'wp_parse_str', $array );
2428  }
2429  
2430  /**
2431   * Convert lone less than signs.
2432   *
2433   * KSES already converts lone greater than signs.
2434   *
2435   * @uses wp_pre_kses_less_than_callback in the callback function.
2436   * @since 2.3.0
2437   *
2438   * @param string $text Text to be converted.
2439   * @return string Converted text.
2440   */
2441  function wp_pre_kses_less_than( $text ) {
2442      return preg_replace_callback('%<[^>]*?((?=<)|>|$)%', 'wp_pre_kses_less_than_callback', $text);
2443  }
2444  
2445  /**
2446   * Callback function used by preg_replace.
2447   *
2448   * @uses esc_html to format the $matches text.
2449   * @since 2.3.0
2450   *
2451   * @param array $matches Populated by matches to preg_replace.
2452   * @return string The text returned after esc_html if needed.
2453   */
2454  function wp_pre_kses_less_than_callback( $matches ) {
2455      if ( false === strpos($matches[0], '>') )
2456          return esc_html($matches[0]);
2457      return $matches[0];
2458  }
2459  
2460  /**
2461   * WordPress implementation of PHP sprintf() with filters.
2462   *
2463   * @since 2.5.0
2464   * @link http://www.php.net/sprintf
2465   *
2466   * @param string $pattern The string which formatted args are inserted.
2467   * @param mixed $args,... Arguments to be formatted into the $pattern string.
2468   * @return string The formatted string.
2469   */
2470  function wp_sprintf( $pattern ) {
2471      $args = func_get_args( );
2472      $len = strlen($pattern);
2473      $start = 0;
2474      $result = '';
2475      $arg_index = 0;
2476      while ( $len > $start ) {
2477          // Last character: append and break
2478          if ( strlen($pattern) - 1 == $start ) {
2479              $result .= substr($pattern, -1);
2480              break;
2481          }
2482  
2483          // Literal %: append and continue
2484          if ( substr($pattern, $start, 2) == '%%' ) {
2485              $start += 2;
2486              $result .= '%';
2487              continue;
2488          }
2489  
2490          // Get fragment before next %
2491          $end = strpos($pattern, '%', $start + 1);
2492          if ( false === $end )
2493              $end = $len;
2494          $fragment = substr($pattern, $start, $end - $start);
2495  
2496          // Fragment has a specifier
2497          if ( $pattern{$start} == '%' ) {
2498              // Find numbered arguments or take the next one in order
2499              if ( preg_match('/^%(\d+)\$/', $fragment, $matches) ) {
2500                  $arg = isset($args[$matches[1]]) ? $args[$matches[1]] : '';
2501                  $fragment = str_replace("%{$matches[1]}$", '%', $fragment);
2502              } else {
2503                  ++$arg_index;
2504                  $arg = isset($args[$arg_index]) ? $args[$arg_index] : '';
2505              }
2506  
2507              // Apply filters OR sprintf
2508              $_fragment = apply_filters( 'wp_sprintf', $fragment, $arg );
2509              if ( $_fragment != $fragment )
2510                  $fragment = $_fragment;
2511              else
2512                  $fragment = sprintf($fragment, strval($arg) );
2513          }
2514  
2515          // Append to result and move to next fragment
2516          $result .= $fragment;
2517          $start = $end;
2518      }
2519      return $result;
2520  }
2521  
2522  /**
2523   * Localize list items before the rest of the content.
2524   *
2525   * The '%l' must be at the first characters can then contain the rest of the
2526   * content. The list items will have ', ', ', and', and ' and ' added depending
2527   * on the amount of list items in the $args parameter.
2528   *
2529   * @since 2.5.0
2530   *
2531   * @param string $pattern Content containing '%l' at the beginning.
2532   * @param array $args List items to prepend to the content and replace '%l'.
2533   * @return string Localized list items and rest of the content.
2534   */
2535  function wp_sprintf_l($pattern, $args) {
2536      // Not a match
2537      if ( substr($pattern, 0, 2) != '%l' )
2538          return $pattern;
2539  
2540      // Nothing to work with
2541      if ( empty($args) )
2542          return '';
2543  
2544      // Translate and filter the delimiter set (avoid ampersands and entities here)
2545      $l = apply_filters('wp_sprintf_l', array(
2546          /* translators: used between list items, there is a space after the coma */
2547          'between'          => __(', '),
2548          /* translators: used between list items, there is a space after the and */
2549          'between_last_two' => __(', and '),
2550          /* translators: used between only two list items, there is a space after the and */
2551          'between_only_two' => __(' and '),
2552          ));
2553  
2554      $args = (array) $args;
2555      $result = array_shift($args);
2556      if ( count($args) == 1 )
2557          $result .= $l['between_only_two'] . array_shift($args);
2558      // Loop when more than two args
2559      $i = count($args);
2560      while ( $i ) {
2561          $arg = array_shift($args);
2562          $i--;
2563          if ( 0 == $i )
2564              $result .= $l['between_last_two'] . $arg;
2565          else
2566              $result .= $l['between'] . $arg;
2567      }
2568      return $result . substr($pattern, 2);
2569  }
2570  
2571  /**
2572   * Safely extracts not more than the first $count characters from html string.
2573   *
2574   * UTF-8, tags and entities safe prefix extraction. Entities inside will *NOT*
2575   * be counted as one character. For example &amp; will be counted as 4, &lt; as
2576   * 3, etc.
2577   *
2578   * @since 2.5.0
2579   *
2580   * @param integer $str String to get the excerpt from.
2581   * @param integer $count Maximum number of characters to take.
2582   * @return string The excerpt.
2583   */
2584  function wp_html_excerpt( $str, $count ) {
2585      $str = strip_tags( $str );
2586      $str = mb_substr( $str, 0, $count );
2587      // remove part of an entity at the end
2588      $str = preg_replace( '/&[^;\s]{0,6}$/', '', $str );
2589      return $str;
2590  }
2591  
2592  /**
2593   * Add a Base url to relative links in passed content.
2594   *
2595   * By default it supports the 'src' and 'href' attributes. However this can be
2596   * changed via the 3rd param.
2597   *
2598   * @since 2.7.0
2599   *
2600   * @param string $content String to search for links in.
2601   * @param string $base The base URL to prefix to links.
2602   * @param array $attrs The attributes which should be processed.
2603   * @return string The processed content.
2604   */
2605  function links_add_base_url( $content, $base, $attrs = array('src', 'href') ) {
2606      $attrs = implode('|', (array)$attrs);
2607      return preg_replace_callback("!($attrs)=(['\"])(.+?)\\2!i",
2608              create_function('$m', 'return _links_add_base($m, "' . $base . '");'),
2609              $content);
2610  }
2611  
2612  /**
2613   * Callback to add a base url to relative links in passed content.
2614   *
2615   * @since 2.7.0
2616   * @access private
2617   *
2618   * @param string $m The matched link.
2619   * @param string $base The base URL to prefix to links.
2620   * @return string The processed link.
2621   */
2622  function _links_add_base($m, $base) {
2623      //1 = attribute name  2 = quotation mark  3 = URL
2624      return $m[1] . '=' . $m[2] .
2625          (strpos($m[3], 'http://') === false ?
2626              path_join($base, $m[3]) :
2627              $m[3])
2628          . $m[2];
2629  }
2630  
2631  /**
2632   * Adds a Target attribute to all links in passed content.
2633   *
2634   * This function by default only applies to <a> tags, however this can be
2635   * modified by the 3rd param.
2636   *
2637   * <b>NOTE:</b> Any current target attributed will be striped and replaced.
2638   *
2639   * @since 2.7.0
2640   *
2641   * @param string $content String to search for links in.
2642   * @param string $target The Target to add to the links.
2643   * @param array $tags An array of tags to apply to.
2644   * @return string The processed content.
2645   */
2646  function links_add_target( $content, $target = '_blank', $tags = array('a') ) {
2647      $tags = implode('|', (array)$tags);
2648      return preg_replace_callback("!<($tags)(.+?)>!i",
2649              create_function('$m', 'return _links_add_target($m, "' . $target . '");'),
2650              $content);
2651  }
2652  /**
2653   * Callback to add a target attribute to all links in passed content.
2654   *
2655   * @since 2.7.0
2656   * @access private
2657   *
2658   * @param string $m The matched link.
2659   * @param string $target The Target to add to the links.
2660   * @return string The processed link.
2661   */
2662  function _links_add_target( $m, $target ) {
2663      $tag = $m[1];
2664      $link = preg_replace('|(target=[\'"](.*?)[\'"])|i', '', $m[2]);
2665      return '<' . $tag . $link . ' target="' . $target . '">';
2666  }
2667  
2668  // normalize EOL characters and strip duplicate whitespace
2669  function normalize_whitespace( $str ) {
2670      $str  = trim($str);
2671      $str  = str_replace("\r", "\n", $str);
2672      $str  = preg_replace( array( '/\n+/', '/[ \t]+/' ), array( "\n", ' ' ), $str );
2673      return $str;
2674  }
2675  
2676  ?>


[ Powered by PHPXref - Served by Debian GNU/Linux ]