root/tags/1.3/wp-includes/functions.php

Revision 1139, 43.8 kB (checked in by donncha, 1 year ago)

Merge with WordPress? 2.3.1

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