[ Index ]

WordPress Source Cross Reference

title

Body

[close]

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

   1  <?php
   2  
   3  require_once(dirname(__FILE__).'/compat.php');
   4  
   5  function mysql2date($dateformatstring, $mysqlstring, $translate = true) {
   6      global $wp_locale;
   7      $m = $mysqlstring;
   8      if ( empty($m) ) {
   9          return false;
  10      }
  11      $i = mktime(substr($m,11,2),substr($m,14,2),substr($m,17,2),substr($m,5,2),substr($m,8,2),substr($m,0,4));
  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      if ( !$j ) {
  38      // for debug purposes
  39      //    echo $i." ".$mysqlstring;
  40      }
  41      return $j;
  42  }
  43  
  44  function current_time($type, $gmt = 0) {
  45      switch ($type) {
  46          case 'mysql':
  47              if ( $gmt ) $d = gmdate('Y-m-d H:i:s');
  48              else $d = gmdate('Y-m-d H:i:s', (time() + (get_settings('gmt_offset') * 3600)));
  49              return $d;
  50              break;
  51          case 'timestamp':
  52              if ( $gmt ) $d = time();
  53              else $d = time() + (get_settings('gmt_offset') * 3600);
  54              return $d;
  55              break;
  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  function get_weekstartend($mysqlstring, $start_of_week) {
  84      $my = substr($mysqlstring,0,4);
  85      $mm = substr($mysqlstring,8,2);
  86      $md = substr($mysqlstring,5,2);
  87      $day = mktime(0,0,0, $md, $mm, $my);
  88      $weekday = date('w',$day);
  89      $i = 86400;
  90  
  91      if ( $weekday < get_settings('start_of_week') )
  92          $weekday = 7 - (get_settings('start_of_week') - $weekday);
  93  
  94      while ($weekday > get_settings('start_of_week')) {
  95          $weekday = date('w',$day);
  96          if ( $weekday < get_settings('start_of_week') )
  97              $weekday = 7 - (get_settings('start_of_week') - $weekday);
  98  
  99          $day = $day - 86400;
 100          $i = 0;
 101      }
 102      $week['start'] = $day + 86400 - $i;
 103      // $week['end'] = $day - $i + 691199;
 104      $week['end'] = $week['start'] + 604799;
 105      return $week;
 106  }
 107  
 108  function get_lastpostdate($timezone = 'server') {
 109      global $cache_lastpostdate, $pagenow, $wpdb;
 110      $add_seconds_blog = get_settings('gmt_offset') * 3600;
 111      $add_seconds_server = date('Z');
 112      if ( !isset($cache_lastpostdate[$timezone]) ) {
 113          switch(strtolower($timezone)) {
 114              case 'gmt':
 115                  $lastpostdate = $wpdb->get_var("SELECT post_date_gmt FROM $wpdb->posts WHERE post_status = 'publish' ORDER BY post_date_gmt DESC LIMIT 1");
 116                  break;
 117              case 'blog':
 118                  $lastpostdate = $wpdb->get_var("SELECT post_date FROM $wpdb->posts WHERE post_status = 'publish' ORDER BY post_date_gmt DESC LIMIT 1");
 119                  break;
 120              case 'server':
 121                  $lastpostdate = $wpdb->get_var("SELECT DATE_ADD(post_date_gmt, INTERVAL '$add_seconds_server' SECOND) FROM $wpdb->posts WHERE post_status = 'publish' ORDER BY post_date_gmt DESC LIMIT 1");
 122                  break;
 123          }
 124          $cache_lastpostdate[$timezone] = $lastpostdate;
 125      } else {
 126          $lastpostdate = $cache_lastpostdate[$timezone];
 127      }
 128      return $lastpostdate;
 129  }
 130  
 131  function get_lastpostmodified($timezone = 'server') {
 132      global $cache_lastpostmodified, $pagenow, $wpdb;
 133      $add_seconds_blog = get_settings('gmt_offset') * 3600;
 134      $add_seconds_server = date('Z');
 135      if ( !isset($cache_lastpostmodified[$timezone]) ) {
 136          switch(strtolower($timezone)) {
 137              case 'gmt':
 138                  $lastpostmodified = $wpdb->get_var("SELECT post_modified_gmt FROM $wpdb->posts WHERE post_status = 'publish' ORDER BY post_modified_gmt DESC LIMIT 1");
 139                  break;
 140              case 'blog':
 141                  $lastpostmodified = $wpdb->get_var("SELECT post_modified FROM $wpdb->posts WHERE post_status = 'publish' ORDER BY post_modified_gmt DESC LIMIT 1");
 142                  break;
 143              case 'server':
 144                  $lastpostmodified = $wpdb->get_var("SELECT DATE_ADD(post_modified_gmt, INTERVAL '$add_seconds_server' SECOND) FROM $wpdb->posts WHERE post_status = 'publish' ORDER BY post_modified_gmt DESC LIMIT 1");
 145                  break;
 146          }
 147          $lastpostdate = get_lastpostdate($timezone);
 148          if ( $lastpostdate > $lastpostmodified ) {
 149              $lastpostmodified = $lastpostdate;
 150          }
 151          $cache_lastpostmodified[$timezone] = $lastpostmodified;
 152      } else {
 153          $lastpostmodified = $cache_lastpostmodified[$timezone];
 154      }
 155      return $lastpostmodified;
 156  }
 157  
 158  function maybe_unserialize($original) {
 159      if ( false !== $gm = @ unserialize($original) )
 160          return $gm;
 161      else
 162          return $original;
 163  }
 164  
 165  /* Options functions */
 166  
 167  function get_settings($setting) {
 168      global $wpdb;
 169  
 170      $value = wp_cache_get($setting, 'options');
 171  
 172      if ( false === $value ) {
 173          if ( defined('WP_INSTALLING') )
 174              $wpdb->hide_errors();
 175          $row = $wpdb->get_row("SELECT option_value FROM $wpdb->options WHERE option_name = '$setting' LIMIT 1");
 176          if ( defined('WP_INSTALLING') )
 177              $wpdb->show_errors();
 178  
 179          if( is_object( $row) ) { // Has to be get_row instead of get_var because of funkiness with 0, false, null values
 180              $value = $row->option_value;
 181              wp_cache_set($setting, $value, 'options');
 182          } else {
 183              return false;
 184          }
 185      }
 186  
 187      // If home is not set use siteurl.
 188      if ( 'home' == $setting && '' == $value )
 189          return get_settings('siteurl');
 190  
 191      if ( 'siteurl' == $setting || 'home' == $setting || 'category_base' == $setting )
 192          $value = preg_replace('|/+$|', '', $value);
 193  
 194      return apply_filters( 'option_' . $setting, maybe_unserialize($value) );
 195  }
 196  
 197  function get_option($option) {
 198      return get_settings($option);
 199  }
 200  
 201  function form_option($option) {
 202      echo htmlspecialchars( get_option($option), ENT_QUOTES );
 203  }
 204  
 205  function get_alloptions() {
 206      global $wpdb, $wp_queries;
 207      $wpdb->hide_errors();
 208      if ( !$options = $wpdb->get_results("SELECT option_name, option_value FROM $wpdb->options WHERE autoload = 'yes'") ) {
 209          $options = $wpdb->get_results("SELECT option_name, option_value FROM $wpdb->options");
 210      }
 211      $wpdb->show_errors();
 212  
 213      foreach ($options as $option) {
 214          // "When trying to design a foolproof system,
 215          //  never underestimate the ingenuity of the fools :)" -- Dougal
 216          if ( 'siteurl' == $option->option_name )
 217              $option->option_value = preg_replace('|/+$|', '', $option->option_value);
 218          if ( 'home' == $option->option_name )
 219              $option->option_value = preg_replace('|/+$|', '', $option->option_value);
 220          if ( 'category_base' == $option->option_name )
 221              $option->option_value = preg_replace('|/+$|', '', $option->option_value);
 222          $value = maybe_unserialize($option->option_value);
 223          $all_options->{$option->option_name} = apply_filters('pre_option_' . $option->option_name, $value);
 224      }
 225      return apply_filters('all_options', $all_options);
 226  }
 227  
 228  function update_option($option_name, $newvalue) {
 229      global $wpdb;
 230  
 231      if ( is_string($newvalue) )
 232          $newvalue = trim($newvalue);
 233  
 234      // If the new and old values are the same, no need to update.
 235      $oldvalue = get_option($option_name);
 236      if ( $newvalue == $oldvalue ) {
 237          return false;
 238      }
 239  
 240      if ( false === $oldvalue ) {
 241          add_option($option_name, $newvalue);
 242          return true;
 243      }
 244  
 245      $_newvalue = $newvalue;
 246      if ( is_array($newvalue) || is_object($newvalue) )
 247          $newvalue = serialize($newvalue);
 248  
 249      wp_cache_set($option_name, $newvalue, 'options');
 250  
 251      $newvalue = $wpdb->escape($newvalue);
 252      $option_name = $wpdb->escape($option_name);
 253      $wpdb->query("UPDATE $wpdb->options SET option_value = '$newvalue' WHERE option_name = '$option_name'");
 254      if ( $wpdb->rows_affected == 1 ) {
 255          do_action("update_option_{$option_name}", array('old'=>$oldvalue, 'new'=>$_newvalue));
 256          return true;
 257      }
 258      return false;
 259  }
 260  
 261  // thx Alex Stapleton, http://alex.vort-x.net/blog/
 262  function add_option($name, $value = '', $description = '', $autoload = 'yes') {
 263      global $wpdb;
 264  
 265      // Make sure the option doesn't already exist
 266      if ( false !== get_option($name) )
 267          return;
 268  
 269      if ( is_array($value) || is_object($value) )
 270          $value = serialize($value);
 271  
 272      wp_cache_set($name, $value, 'options');
 273  
 274      $name = $wpdb->escape($name);
 275      $value = $wpdb->escape($value);
 276      $description = $wpdb->escape($description);
 277      $wpdb->query("INSERT INTO $wpdb->options (option_name, option_value, option_description, autoload) VALUES ('$name', '$value', '$description', '$autoload')");
 278  
 279      return;
 280  }
 281  
 282  function delete_option($name) {
 283      global $wpdb;
 284      // Get the ID, if no ID then return
 285      $option_id = $wpdb->get_var("SELECT option_id FROM $wpdb->options WHERE option_name = '$name'");
 286      if ( !$option_id ) return false;
 287      $wpdb->query("DELETE FROM $wpdb->options WHERE option_name = '$name'");
 288      wp_cache_delete($name, 'options');
 289      return true;
 290  }
 291  
 292  function gzip_compression() {
 293      if ( !get_settings('gzipcompression') ) return false;
 294  
 295      if ( extension_loaded('zlib') ) {
 296          ob_start('ob_gzhandler');
 297      }
 298  }
 299  
 300  
 301  // functions to count the page generation time (from phpBB2)
 302  // ( or just any time between timer_start() and timer_stop() )
 303  
 304  function timer_stop($display = 0, $precision = 3) { //if called like timer_stop(1), will echo $timetotal
 305      global $timestart, $timeend;
 306      $mtime = microtime();
 307      $mtime = explode(' ',$mtime);
 308      $mtime = $mtime[1] + $mtime[0];
 309      $timeend = $mtime;
 310      $timetotal = $timeend-$timestart;
 311      if ( $display )
 312          echo number_format($timetotal,$precision);
 313      return $timetotal;
 314  }
 315  
 316  function make_url_footnote($content) {
 317      preg_match_all('/<a(.+?)href=\"(.+?)\"(.*?)>(.+?)<\/a>/', $content, $matches);
 318      $j = 0;
 319      for ($i=0; $i<count($matches[0]); $i++) {
 320          $links_summary = (!$j) ? "\n" : $links_summary;
 321          $j++;
 322          $link_match = $matches[0][$i];
 323          $link_number = '['.($i+1).']';
 324          $link_url = $matches[2][$i];
 325          $link_text = $matches[4][$i];
 326          $content = str_replace($link_match, $link_text.' '.$link_number, $content);
 327          $link_url = ((strtolower(substr($link_url,0,7)) != 'http://') && (strtolower(substr($link_url,0,8)) != 'https://')) ? get_settings('home') . $link_url : $link_url;
 328          $links_summary .= "\n".$link_number.' '.$link_url;
 329      }
 330      $content = strip_tags($content);
 331      $content .= $links_summary;
 332      return $content;
 333  }
 334  
 335  
 336  function xmlrpc_getposttitle($content) {
 337      global $post_default_title;
 338      if ( preg_match('/<title>(.+?)<\/title>/is', $content, $matchtitle) ) {
 339          $post_title = $matchtitle[0];
 340          $post_title = preg_replace('/<title>/si', '', $post_title);
 341          $post_title = preg_replace('/<\/title>/si', '', $post_title);
 342      } else {
 343          $post_title = $post_default_title;
 344      }
 345      return $post_title;
 346  }
 347  
 348  function xmlrpc_getpostcategory($content) {
 349      global $post_default_category;
 350      if ( preg_match('/<category>(.+?)<\/category>/is', $content, $matchcat) ) {
 351          $post_category = trim($matchcat[1], ',');