PHP Cross Reference of WordPress Subversion HEAD

[ Index ]     [ Classes ]     [ Functions ]     [ Variables ]     [ Constants ]

title

Body

[close]

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

   1  <?php
   2  
   3  function mysql2date( $dateformatstring, $mysqlstring, $translate = true ) {
   4      global $wp_locale;
   5      $m = $mysqlstring;
   6      if ( empty( $m ) )
   7          return false;
   8      $i = mktime(
   9          (int) substr( $m, 11, 2 ), (int) substr( $m, 14, 2 ), (int) substr( $m, 17, 2 ),
  10          (int) substr( $m, 5, 2 ), (int) substr( $m, 8, 2 ), (int) substr( $m, 0, 4 )
  11      );
  12  
  13      if( 'U' == $dateformatstring )
  14          return $i;
  15  
  16      if ( -1 == $i || false == $i )
  17          $i = 0;
  18  
  19      if ( !empty( $wp_locale->month ) && !empty( $wp_locale->weekday ) && $translate ) {
  20          $datemonth = $wp_locale->get_month( date( 'm', $i ) );
  21          $datemonth_abbrev = $wp_locale->get_month_abbrev( $datemonth );
  22          $dateweekday = $wp_locale->get_weekday( date( 'w', $i ) );
  23          $dateweekday_abbrev = $wp_locale->get_weekday_abbrev( $dateweekday );
  24          $datemeridiem = $wp_locale->get_meridiem( date( 'a', $i ) );
  25          $datemeridiem_capital = $wp_locale->get_meridiem( date( 'A', $i ) );
  26          $dateformatstring = ' ' . $dateformatstring;
  27          $dateformatstring = preg_replace( "/([^\\\])D/", "\\1" . backslashit( $dateweekday_abbrev ), $dateformatstring );
  28          $dateformatstring = preg_replace( "/([^\\\])F/", "\\1" . backslashit( $datemonth ), $dateformatstring );
  29          $dateformatstring = preg_replace( "/([^\\\])l/", "\\1" . backslashit( $dateweekday ), $dateformatstring );
  30          $dateformatstring = preg_replace( "/([^\\\])M/", "\\1" . backslashit( $datemonth_abbrev ), $dateformatstring );
  31          $dateformatstring = preg_replace( "/([^\\\])a/", "\\1" . backslashit( $datemeridiem ), $dateformatstring );
  32          $dateformatstring = preg_replace( "/([^\\\])A/", "\\1" . backslashit( $datemeridiem_capital ), $dateformatstring );
  33  
  34          $dateformatstring = substr( $dateformatstring, 1, strlen( $dateformatstring ) -1 );
  35      }
  36      $j = @date( $dateformatstring, $i );
  37  
  38      /*
  39      if ( !$j ) // for debug purposes
  40          echo $i." ".$mysqlstring;
  41      */
  42  
  43      return $j;
  44  }
  45  
  46  
  47  function current_time( $type, $gmt = 0 ) {
  48      switch ( $type ) {
  49          case 'mysql':
  50              return ( $gmt ) ? gmdate( 'Y-m-d H:i:s' ) : gmdate( 'Y-m-d H:i:s', ( time() + ( get_option( 'gmt_offset' ) * 3600 ) ) );
  51              break;
  52          case 'timestamp':
  53              return ( $gmt ) ? time() : time() + ( get_option( 'gmt_offset' ) * 3600 );
  54              break;
  55      }
  56  }
  57  
  58  
  59  function date_i18n( $dateformatstring, $unixtimestamp ) {
  60      global $wp_locale;
  61      $i = $unixtimestamp;
  62      if ( ( !empty( $wp_locale->month ) ) && ( !empty( $wp_locale->weekday ) ) ) {
  63          $datemonth = $wp_locale->get_month( date( 'm', $i ) );
  64          $datemonth_abbrev = $wp_locale->get_month_abbrev( $datemonth );
  65          $dateweekday = $wp_locale->get_weekday( date( 'w', $i ) );
  66          $dateweekday_abbrev = $wp_locale->get_weekday_abbrev( $dateweekday );
  67          $datemeridiem = $wp_locale->get_meridiem( date( 'a', $i ) );
  68          $datemeridiem_capital = $wp_locale->get_meridiem( date( 'A', $i ) );
  69          $dateformatstring = ' '.$dateformatstring;
  70          $dateformatstring = preg_replace( "/([^\\\])D/", "\\1" . backslashit( $dateweekday_abbrev ), $dateformatstring );
  71          $dateformatstring = preg_replace( "/([^\\\])F/", "\\1" . backslashit( $datemonth ), $dateformatstring );
  72          $dateformatstring = preg_replace( "/([^\\\])l/", "\\1" . backslashit( $dateweekday ), $dateformatstring );
  73          $dateformatstring = preg_replace( "/([^\\\])M/", "\\1" . backslashit( $datemonth_abbrev ), $dateformatstring );
  74          $dateformatstring = preg_replace( "/([^\\\])a/", "\\1" . backslashit( $datemeridiem ), $dateformatstring );
  75          $dateformatstring = preg_replace( "/([^\\\])A/", "\\1" . backslashit( $datemeridiem_capital ), $dateformatstring );
  76  
  77          $dateformatstring = substr( $dateformatstring, 1, strlen( $dateformatstring ) -1 );
  78      }
  79      $j = @date( $dateformatstring, $i );
  80      return $j;
  81  }
  82  
  83  
  84  function number_format_i18n( $number, $decimals = null ) {
  85      global $wp_locale;
  86      // let the user override the precision only
  87      $decimals = ( is_null( $decimals ) ) ? $wp_locale->number_format['decimals'] : intval( $decimals );
  88  
  89      return number_format( $number, $decimals, $wp_locale->number_format['decimal_point'], $wp_locale->number_format['thousands_sep'] );
  90  }
  91  
  92  
  93  function size_format( $bytes, $decimals = null ) {
  94      // technically the correct unit names for powers of 1024 are KiB, MiB etc
  95      // see http://en.wikipedia.org/wiki/Byte
  96      $quant = array(
  97          // ========================= Origin ====
  98          'TB' => 1099511627776,  // pow( 1024, 4)
  99          'GB' => 1073741824,     // pow( 1024, 3)
 100          'MB' => 1048576,        // pow( 1024, 2)
 101          'kB' => 1024,           // pow( 1024, 1)
 102          'B ' => 1,              // pow( 1024, 0)
 103      );
 104  
 105      foreach ( $quant as $unit => $mag )
 106          if ( intval( $bytes ) >= $mag )
 107              return number_format_i18n( $bytes / $mag, $decimals ) . ' ' . $unit;
 108  }
 109  
 110  
 111  function get_weekstartend( $mysqlstring, $start_of_week ) {
 112      $my = substr( $mysqlstring, 0, 4 );
 113      $mm = substr( $mysqlstring, 8, 2 );
 114      $md = substr( $mysqlstring, 5, 2 );
 115      $day = mktime( 0, 0, 0, $md, $mm, $my );
 116      $weekday = date( 'w', $day );
 117      $i = 86400;
 118  
 119      if ( $weekday < get_option( 'start_of_week' ) )
 120          $weekday = 7 - ( get_option( 'start_of_week' ) - $weekday );
 121  
 122      while ( $weekday > get_option( 'start_of_week' ) ) {
 123          $weekday = date( 'w', $day );
 124          if ( $weekday < get_option( 'start_of_week' ) )
 125              $weekday = 7 - ( get_option( 'start_of_week' ) - $weekday );
 126  
 127          $day = $day - 86400;
 128          $i = 0;
 129      }
 130      $week['start'] = $day + 86400 - $i;
 131      $week['end'] = $week['start'] + 604799;
 132      return $week;
 133  }
 134  
 135  
 136  function maybe_unserialize( $original ) {
 137      if ( is_serialized( $original ) ) // don't attempt to unserialize data that wasn't serialized going in
 138          if ( false !== $gm = @unserialize( $original ) )
 139              return $gm;
 140      return $original;
 141  }
 142  
 143  
 144  function is_serialized( $data ) {
 145      // if it isn't a string, it isn't serialized
 146      if ( !is_string( $data ) )
 147          return false;
 148      $data = trim( $data );
 149      if ( 'N;' == $data )
 150          return true;
 151      if ( !preg_match( '/^([adObis]):/', $data, $badions ) )
 152          return false;
 153      switch ( $badions[1] ) {
 154          case 'a' :
 155          case 'O' :
 156          case 's' :
 157              if ( preg_match( "/^{$badions[1]}:[0-9]+:.*[;}]\$/s", $data ) )
 158                  return true;
 159              break;
 160          case 'b' :
 161          case 'i' :
 162          case 'd' :
 163              if ( preg_match( "/^{$badions[1]}:[0-9.E-]+;\$/", $data ) )
 164                  return true;
 165              break;
 166      }
 167      return false;
 168  }
 169  
 170  
 171  function is_serialized_string( $data ) {
 172      // if it isn't a string, it isn't a serialized string
 173      if ( !is_string( $data ) )
 174          return false;
 175      $data = trim( $data );
 176      if ( preg_match( '/^s:[0-9]+:.*;$/s', $data ) ) // this should fetch all serialized strings
 177          return true;
 178      return false;
 179  }
 180  
 181  
 182  /* Options functions */
 183  
 184  // expects $setting to already be SQL-escaped
 185  function get_option( $setting ) {
 186      global $wpdb;
 187  
 188      // Allow plugins to short-circuit options.
 189      $pre = apply_filters( 'pre_option_' . $setting, false );
 190      if ( false !== $pre )
 191          return $pre;
 192  
 193      // prevent non-existent options from triggering multiple queries
 194      $notoptions = wp_cache_get( 'notoptions', 'options' );
 195      if ( isset( $notoptions[$setting] ) )
 196          return false;
 197  
 198      $alloptions = wp_load_alloptions();
 199  
 200      if ( isset( $alloptions[$setting] ) ) {
 201          $value = $alloptions[$setting];
 202      } else {
 203          $value = wp_cache_get( $setting, 'options' );
 204  
 205          if ( false === $value ) {
 206              if ( defined( 'WP_INSTALLING' ) )
 207                  $wpdb->hide_errors();
 208              // expected_slashed ($setting)
 209              $row = $wpdb->get_row( "SELECT option_value FROM $wpdb->options WHERE option_name = '$setting' LIMIT 1" );
 210              if ( defined( 'WP_INSTALLING' ) )
 211                  $wpdb->show_errors();
 212  
 213              if ( is_object( $row) ) { // Has to be get_row instead of get_var because of funkiness with 0, false, null values
 214                  $value = $row->option_value;
 215                  wp_cache_add( $setting, $value, 'options' );
 216              } else { // option does not exist, so we must cache its non-existence
 217                  $notoptions[$setting] = true;
 218                  wp_cache_set( 'notoptions', $notoptions, 'options' );
 219                  return false;
 220              }
 221          }
 222      }
 223  
 224      // If home is not set use siteurl.
 225      if ( 'home' == $setting && '' == $value )
 226          return get_option( 'siteurl' );
 227  
 228      if ( in_array( $setting, array('siteurl', 'home', 'category_base', 'tag_base') ) )
 229          $value = untrailingslashit( $value );
 230  
 231      return apply_filters( 'option_' . $setting, maybe_unserialize( $value ) );
 232  }
 233  
 234  
 235  function wp_protect_special_option( $option ) {
 236      $protected = array( 'alloptions', 'notoptions' );
 237      if ( in_array( $option, $protected ) )
 238          die( sprintf( __( '%s is a protected WP option and may not be modified' ), wp_specialchars( $option ) ) );
 239  }
 240  
 241  function form_option( $option ) {
 242      echo attribute_escape (get_option( $option ) );
 243  }
 244  
 245  function get_alloptions() {
 246      global $wpdb, $wp_queries;
 247      $wpdb->hide_errors();
 248      if ( !$options = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options WHERE autoload = 'yes'" ) )
 249          $options = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options" );
 250      $wpdb->show_errors();
 251  
 252      foreach ( $options as $option ) {
 253          // "When trying to design a foolproof system,
 254          //  never underestimate the ingenuity of the fools :)" -- Dougal
 255          if ( in_array( $option->option_name, array( 'siteurl', 'home', 'category_base' ) ) )
 256              $option->option_value = untrailingslashit( $option->option_value );
 257          $value = maybe_unserialize( $option->option_value );
 258          $all_options->{$option->option_name} = apply_filters( 'pre_option_' . $option->option_name, $value );
 259      }
 260      return apply_filters( 'all_options', $all_options );
 261  }
 262  
 263  
 264  function wp_load_alloptions() {
 265      global $wpdb;
 266  
 267      $alloptions = wp_cache_get( 'alloptions', 'options' );
 268  
 269      if ( !$alloptions ) {
 270          $wpdb->hide_errors();
 271          if ( !$alloptions_db = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options WHERE autoload = 'yes'" ) )
 272              $alloptions_db = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options" );
 273          $wpdb->show_errors();
 274          $alloptions = array();
 275          foreach ( (array) $alloptions_db as $o )
 276              $alloptions[$o->option_name] = $o->option_value;
 277          wp_cache_add( 'alloptions', $alloptions, 'options' );
 278      }
 279      return $alloptions;
 280  }
 281  
 282  
 283  // expects $option_name to NOT be SQL-escaped
 284  function update_option( $option_name, $newvalue ) {
 285      global $wpdb;
 286  
 287      wp_protect_special_option( $option_name );
 288  
 289      $safe_option_name = $wpdb->escape( $option_name );
 290      $newvalue = sanitize_option( $option_name, $newvalue );
 291  
 292      // Likely legacy -- can we drop this?
 293      if ( is_string( $newvalue ) )
 294          $newvalue = trim( $newvalue );
 295  
 296      // If the new and old values are the same, no need to update.
 297      $oldvalue = get_option( $safe_option_name );
 298      if ( $newvalue === $oldvalue )
 299          return false;
 300  
 301      if ( false === $oldvalue ) {
 302          add_option( $option_name, $newvalue );
 303          return true;
 304      }
 305  
 306      $notoptions = wp_cache_get( 'notoptions', 'options' );
 307      if ( is_array( $notoptions ) && isset( $notoptions[$option_name] ) ) {
 308          unset( $notoptions[$option_name] );
 309          wp_cache_set( 'notoptions', $notoptions, 'options' );
 310      }
 311  
 312      $_newvalue = $newvalue;
 313      $newvalue = maybe_serialize( $newvalue );
 314  
 315      $alloptions = wp_load_alloptions();
 316      if ( isset( $alloptions[$option_name] ) ) {
 317          $alloptions[$option_name] = $newvalue;
 318          wp_cache_set( 'alloptions', $alloptions, 'options' );
 319      } else {
 320          wp_cache_set( $option_name, $newvalue, 'options' );
 321      }
 322  
 323      $wpdb->query( $wpdb->prepare( "UPDATE $wpdb->options SET option_value = %s WHERE option_name = %s", $newvalue, $option_name ) );
 324      if ( $wpdb->rows_affected == 1 ) {
 325          do_action( "update_option_{$option_name}", $oldvalue, $_newvalue );
 326          return true;
 327      }
 328      return false;
 329  }
 330  
 331  
 332  // thx Alex Stapleton, http://alex.vort-x.net/blog/
 333  // expects $name to NOT be SQL-escaped
 334  function add_option( $name, $value = '', $deprecated = '', $autoload = 'yes' ) {
 335      global $wpdb;
 336  
 337      wp_protect_special_option( $name );
 338      $safe_name = $wpdb->escape( $name );
 339  
 340      // Make sure the option doesn't already exist. We can check the 'notoptions' cache before we ask for a db query
 341      $notoptions = wp_cache_get( 'notoptions', 'options' );
 342      if ( !is_array( $notoptions ) || !isset( $notoptions[$name] ) )
 343          if ( false !== get_option( $safe_name ) )
 344              return;
 345  
 346      $value = maybe_serialize( $value );
 347      $autoload = ( 'no' === $autoload ) ? 'no' : 'yes';
 348  
 349      if ( 'yes' == $autoload ) {
 350          $alloptions = wp_load_alloptions();
 351          $alloptions[$name] = $value;
 352          wp_cache_set( 'alloptions', $alloptions, 'options' );
 353      } else {
 354          wp_cache_set( $name, $value, 'options' );
 355      }
 356  
 357      // This option exists now
 358      $notoptions = wp_cache_get( 'notoptions', 'options' ); // yes, again... we need it to be fresh
 359      if ( is_array( $notoptions ) && isset( $notoptions[$name] ) ) {
 360          unset( $notoptions[$name] );
 361          wp_cache_set( 'notoptions', $notoptions, 'options' );
 362      }
 363  
 364      $wpdb->query( $wpdb->prepare( "INSERT INTO $wpdb->options (option_name, option_value, autoload) VALUES (%s, %s, %s)", $name, $value, $autoload ) );
 365  
 366      return;
 367  }
 368  
 369  
 370  function delete_option( $name ) {
 371      global $wpdb;
 372  
 373      wp_protect_special_option( $name );
 374  
 375      // Get the ID, if no ID then return
 376      // expected_slashed ($name)
 377      $option = $wpdb->get_row( "SELECT option_id, autoload FROM $wpdb->options WHERE option_name = '$name'" );
 378      if ( !$option->option_id )
 379          return false;
 380      // expected_slashed ($name)
 381      $wpdb->query( "DELETE FROM $wpdb->options WHERE option_name = '$name'" );
 382      if ( 'yes' == $option->autoload ) {
 383          $alloptions = wp_load_alloptions();
 384          if ( isset( $alloptions[$name] ) ) {
 385              unset( $alloptions[$name] );
 386              wp_cache_set( 'alloptions', $alloptions, 'options' );
 387          }
 388      } else {
 389          wp_cache_delete( $name, 'options' );
 390      }
 391      return true;
 392  }
 393  
 394  
 395  function maybe_serialize( $data ) {
 396      if ( is_string( $data ) )
 397          $data = trim( $data );
 398      elseif ( is_array( $data ) || is_object( $data ) )
 399          return serialize