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

Revision 3495, 62.2 kB (checked in by ryan, 3 years ago)

Delete usermeta field if set to empty string. Props David House. fixes #2341

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 <?php
2
3 require_once(dirname(__FILE__).'/functions-compat.php');
4
5 if ( !function_exists('_') ) {
6     function _($string) {
7         return $string;
8     }
9 }
10
11 function get_profile($field, $user = false) {
12     global $wpdb;
13     if ( !$user )
14         $user = $wpdb->escape($_COOKIE[USER_COOKIE]);
15     return $wpdb->get_var("SELECT $field FROM $wpdb->users WHERE user_login = '$user'");
16 }
17
18 function mysql2date($dateformatstring, $mysqlstring, $translate = true) {
19     global $month, $weekday, $month_abbrev, $weekday_abbrev;
20     $m = $mysqlstring;
21     if ( empty($m) ) {
22         return false;
23     }
24     $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));
25     
26     if ( -1 == $i || false == $i )
27         $i = 0;
28
29     if ( !empty($month) && !empty($weekday) && $translate ) {
30         $datemonth = $month[date('m', $i)];
31         $datemonth_abbrev = $month_abbrev[$datemonth];
32         $dateweekday = $weekday[date('w', $i)];
33         $dateweekday_abbrev = $weekday_abbrev[$dateweekday];
34         $dateformatstring = ' '.$dateformatstring;
35         $dateformatstring = preg_replace("/([^\\\])D/", "\\1".backslashit($dateweekday_abbrev), $dateformatstring);
36         $dateformatstring = preg_replace("/([^\\\])F/", "\\1".backslashit($datemonth), $dateformatstring);
37         $dateformatstring = preg_replace("/([^\\\])l/", "\\1".backslashit($dateweekday), $dateformatstring);
38         $dateformatstring = preg_replace("/([^\\\])M/", "\\1".backslashit($datemonth_abbrev), $dateformatstring);
39
40         $dateformatstring = substr($dateformatstring, 1, strlen($dateformatstring)-1);
41     }
42     $j = @date($dateformatstring, $i);
43     if ( !$j ) {
44     // for debug purposes
45     //    echo $i." ".$mysqlstring;
46     }
47     return $j;
48 }
49
50 function current_time($type, $gmt = 0) {
51     switch ($type) {
52         case 'mysql':
53             if ( $gmt ) $d = gmdate('Y-m-d H:i:s');
54             else $d = gmdate('Y-m-d H:i:s', (time() + (get_settings('gmt_offset') * 3600)));
55             return $d;
56             break;
57         case 'timestamp':
58             if ( $gmt ) $d = time();
59             else $d = time() + (get_settings('gmt_offset') * 3600);
60             return $d;
61             break;
62     }
63 }
64
65 function date_i18n($dateformatstring, $unixtimestamp) {
66     global $month, $weekday, $month_abbrev, $weekday_abbrev;
67     $i = $unixtimestamp;
68     if ( (!empty($month)) && (!empty($weekday)) ) {
69         $datemonth = $month[date('m', $i)];
70         $datemonth_abbrev = $month_abbrev[$datemonth];
71         $dateweekday = $weekday[date('w', $i)];
72         $dateweekday_abbrev = $weekday_abbrev[$dateweekday];
73         $dateformatstring = ' '.$dateformatstring;
74         $dateformatstring = preg_replace("/([^\\\])D/", "\\1".backslashit($dateweekday_abbrev), $dateformatstring);
75         $dateformatstring = preg_replace("/([^\\\])F/", "\\1".backslashit($datemonth), $dateformatstring);
76         $dateformatstring = preg_replace("/([^\\\])l/", "\\1".backslashit($dateweekday), $dateformatstring);
77         $dateformatstring = preg_replace("/([^\\\])M/", "\\1".backslashit($datemonth_abbrev), $dateformatstring);
78         $dateformatstring = substr($dateformatstring, 1, strlen($dateformatstring)-1);
79     }
80     $j = @date($dateformatstring, $i);
81     return $j;
82     }
83
84 function get_weekstartend($mysqlstring, $start_of_week) {
85     $my = substr($mysqlstring,0,4);
86     $mm = substr($mysqlstring,8,2);
87     $md = substr($mysqlstring,5,2);
88     $day = mktime(0,0,0, $md, $mm, $my);
89     $weekday = date('w',$day);
90     $i = 86400;
91
92     if ( $weekday < get_settings('start_of_week') )
93         $weekday = 7 - (get_settings('start_of_week') - $weekday);
94
95     while ($weekday > get_settings('start_of_week')) {
96         $weekday = date('w',$day);
97         if ( $weekday < get_settings('start_of_week') )
98             $weekday = 7 - (get_settings('start_of_week') - $weekday);
99
100         $day = $day - 86400;
101         $i = 0;
102     }
103     $week['start'] = $day + 86400 - $i;
104     // $week['end'] = $day - $i + 691199;
105     $week['end'] = $week['start'] + 604799;
106     return $week;
107 }
108
109 function get_lastpostdate($timezone = 'server') {
110     global $cache_lastpostdate, $pagenow, $wpdb;
111     $add_seconds_blog = get_settings('gmt_offset') * 3600;
112     $add_seconds_server = date('Z');
113     $now = current_time('mysql', 1);
114     if ( !isset($cache_lastpostdate[$timezone]) ) {
115         switch(strtolower($timezone)) {
116             case 'gmt':
117                 $lastpostdate = $wpdb->get_var("SELECT post_date_gmt FROM $wpdb->posts WHERE post_date_gmt <= '$now' AND post_status = 'publish' ORDER BY post_date_gmt DESC LIMIT 1");
118                 break;
119             case 'blog':
120                 $lastpostdate = $wpdb->get_var("SELECT post_date FROM $wpdb->posts WHERE post_date_gmt <= '$now' AND post_status = 'publish' ORDER BY post_date_gmt DESC LIMIT 1");
121                 break;
122             case 'server':
123                 $lastpostdate = $wpdb->get_var("SELECT DATE_ADD(post_date_gmt, INTERVAL '$add_seconds_server' SECOND) FROM $wpdb->posts WHERE post_date_gmt <= '$now' AND post_status = 'publish' ORDER BY post_date_gmt DESC LIMIT 1");
124                 break;
125         }
126         $cache_lastpostdate[$timezone] = $lastpostdate;
127     } else {
128         $lastpostdate = $cache_lastpostdate[$timezone];
129     }
130     return $lastpostdate;
131 }
132
133 function get_lastpostmodified($timezone = 'server') {
134     global $cache_lastpostmodified, $pagenow, $wpdb;
135     $add_seconds_blog = get_settings('gmt_offset') * 3600;
136     $add_seconds_server = date('Z');
137     $now = current_time('mysql', 1);
138     if ( !isset($cache_lastpostmodified[$timezone]) ) {
139         switch(strtolower($timezone)) {
140             case 'gmt':
141                 $lastpostmodified = $wpdb->get_var("SELECT post_modified_gmt FROM $wpdb->posts WHERE post_modified_gmt <= '$now' AND post_status = 'publish' ORDER BY post_modified_gmt DESC LIMIT 1");
142                 break;
143             case 'blog':
144                 $lastpostmodified = $wpdb->get_var("SELECT post_modified FROM $wpdb->posts WHERE post_modified_gmt <= '$now' AND post_status = 'publish' ORDER BY post_modified_gmt DESC LIMIT 1");
145                 break;
146             case 'server':
147                 $lastpostmodified = $wpdb->get_var("SELECT DATE_ADD(post_modified_gmt, INTERVAL '$add_seconds_server' SECOND) FROM $wpdb->posts WHERE post_modified_gmt <= '$now' AND post_status = 'publish' ORDER BY post_modified_gmt DESC LIMIT 1");
148                 break;
149         }
150         $lastpostdate = get_lastpostdate($timezone);
151         if ( $lastpostdate > $lastpostmodified ) {
152             $lastpostmodified = $lastpostdate;
153         }
154         $cache_lastpostmodified[$timezone] = $lastpostmodified;
155     } else {
156         $lastpostmodified = $cache_lastpostmodified[$timezone];
157     }
158     return $lastpostmodified;
159 }
160
161 function user_pass_ok($user_login,$user_pass) {
162     global $cache_userdata;
163     if ( empty($cache_userdata[$user_login]) ) {
164         $userdata = get_userdatabylogin($user_login);
165     } else {
166         $userdata = $cache_userdata[$user_login];
167     }
168     return (md5($user_pass) == $userdata->user_pass);
169 }
170
171
172 function get_usernumposts($userid) {
173     global $wpdb;
174     return $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_author = '$userid' AND post_status = 'publish'");
175 }
176
177
178 // examine a url (supposedly from this blog) and try to
179 // determine the post ID it represents.
180 function url_to_postid($url) {
181     global $wp_rewrite;
182
183     // First, check to see if there is a 'p=N' or 'page_id=N' to match against
184     preg_match('#[?&](p|page_id)=(\d+)#', $url, $values);
185     $id = intval($values[2]);
186     if ( $id ) return $id;
187
188     // Check to see if we are using rewrite rules
189     $rewrite = $wp_rewrite->wp_rewrite_rules();
190
191     // Not using rewrite rules, and 'p=N' and 'page_id=N' methods failed, so we're out of options
192     if ( empty($rewrite) )
193         return 0;
194
195     // $url cleanup by Mark Jaquith
196     // This fixes things like #anchors, ?query=strings, missing 'www.',
197     // added 'www.', or added 'index.php/' that will mess up our WP_Query
198     // and return a false negative
199
200     // Get rid of the #anchor
201     $url_split = explode('#', $url);
202     $url = $url_split[0];
203
204     // Get rid of URI ?query=string
205     $url_split = explode('?', $url);
206     $url = $url_split[0];
207
208     // Add 'www.' if it is absent and should be there
209     if ( false !== strpos(get_settings('home'), '://www.') && false === strpos($url, '://www.') )
210         $url = str_replace('://', '://www.', $url);
211
212     // Strip 'www.' if it is present and shouldn't be
213     if ( false === strpos(get_settings('home'), '://www.') )
214         $url = str_replace('://www.', '://', $url);
215
216     // Strip 'index.php/' if we're not using path info permalinks
217     if ( false === strpos($rewrite, 'index.php/') )
218         $url = str_replace('index.php/', '', $url);
219
220     if ( false !== strpos($url, get_settings('home')) ) {
221         // Chop off http://domain.com
222         $url = str_replace(get_settings('home'), '', $url);
223     } else {
224         // Chop off /path/to/blog
225         $home_path = parse_url(get_settings('home'));
226         $home_path = $home_path['path'];
227         $url = str_replace($home_path, '', $url);
228     }
229
230     // Trim leading and lagging slashes
231     $url = trim($url, '/');
232
233     $request = $url;
234
235     // Done with cleanup
236
237     // Look for matches.
238     $request_match = $request;
239     foreach ($rewrite as $match => $query) {
240         // If the requesting file is the anchor of the match, prepend it
241         // to the path info.
242         if ( (! empty($url)) && (strpos($match, $url) === 0) ) {
243             $request_match = $url . '/' . $request;
244         }
245
246         if ( preg_match("!^$match!", $request_match, $matches) ) {
247             // Got a match.
248             // Trim the query of everything up to the '?'.
249             $query = preg_replace("!^.+\?!", '', $query);
250
251             // Substitute the substring matches into the query.
252             eval("\$query = \"$query\";");
253             $query = new WP_Query($query);
254             if ( $query->is_single || $query->is_page )
255                 return $query->post->ID;
256             else
257                 return 0;
258         }
259     }
260     return 0;
261 }
262
263
264 function maybe_unserialize($original) {
265     if ( false !== $gm = @ unserialize($original) )
266         return $gm;
267     else
268         return $original;
269 }
270
271 /* Options functions */
272
273 function get_settings($setting) {
274     global $wpdb;
275
276     $value = wp_cache_get($setting, 'options');
277
278     if ( false === $value ) {
279         if ( defined('WP_INSTALLING') )
280             $wpdb->hide_errors();
281         $row = $wpdb->get_row("SELECT option_value FROM $wpdb->options WHERE option_name = '$setting' LIMIT 1");
282         if ( defined('WP_INSTALLING') )
283             $wpdb->show_errors();
284
285         if( is_object( $row) ) { // Has to be get_row instead of get_var because of funkiness with 0, false, null values
286             $value = $row->option_value;
287             wp_cache_set($setting, $value, 'options');
288         } else {
289             return false;
290         }
291     }
292
293     // If home is not set use siteurl.
294     if ( 'home' == $setting && '' == $value )
295         return get_settings('siteurl');
296
297     if ( 'siteurl' == $setting || 'home' == $setting || 'category_base' == $setting )
298         $value = preg_replace('|/+$|', '', $value);
299
300     return apply_filters( 'option_' . $setting, maybe_unserialize($value) );
301 }
302
303 function get_option($option) {
304     return get_settings($option);
305 }
306
307 function get_user_option( $option, $user = 0 ) {
308     global $wpdb, $current_user;
309     
310     if ( empty($user) )
311         $user = $current_user;
312     else
313         $user = get_userdata($user);
314
315     if ( isset( $user->{$wpdb->prefix . $option} ) ) // Blog specific
316         return $user->{$wpdb->prefix . $option};
317     elseif ( isset( $user->{$option} ) ) // User specific and cross-blog
318         return $user->{$option};
319     else // Blog global
320         return get_option( $option );
321 }
322
323 function form_option($option) {
324     echo htmlspecialchars( get_option($option), ENT_QUOTES );
325 }
326
327 function get_alloptions() {
328     global $wpdb, $wp_queries;
329     $wpdb->hide_errors();
330     if ( !$options = $wpdb->get_results("SELECT option_name, option_value FROM $wpdb->options WHERE autoload = 'yes'") ) {
331         $options = $wpdb->get_results("SELECT option_name, option_value FROM $wpdb->options");
332     }
333     $wpdb->show_errors();
334
335     foreach ($options as $option) {
336         // "When trying to design a foolproof system,
337         //  never underestimate the ingenuity of the fools :)" -- Dougal
338         if ( 'siteurl' == $option->option_name )
339             $option->option_value = preg_replace('|/+$|', '', $option->option_value);
340         if ( 'home' == $option->option_name )
341             $option->option_value = preg_replace('|/+$|', '', $option->option_value);
342         if ( 'category_base' == $option->option_name )
343             $option->option_value = preg_replace('|/+$|', '', $option->option_value);
344         $value = maybe_unserialize($option->option_value);
345         $all_options->{$option->option_name} = apply_filters('pre_option_' . $option->option_name, $value);
346     }
347     return apply_filters('all_options', $all_options);
348 }
349
350 function update_option($option_name, $newvalue) {
351     global $wpdb;
352
353     if ( is_string($newvalue) )
354         $newvalue = trim($newvalue);
355
356     // If the new and old values are the same, no need to update.
357     $oldvalue = get_option($option_name);
358     if ( $newvalue == $oldvalue ) {
359         return false;
360     }
361
362     if ( false === $oldvalue ) {
363         add_option($option_name, $newvalue);
364         return true;
365     }
366
367     if ( is_array($newvalue) || is_object($newvalue) )
368         $newvalue = serialize($newvalue);
369
370     wp_cache_set($option_name, $newvalue, 'options');
371
372     $newvalue = $wpdb->escape($newvalue);
373     $option_name = $wpdb->escape($option_name);
374     $wpdb->query("UPDATE $wpdb->options SET option_value = '$newvalue' WHERE option_name = '$option_name'");
375     if ( $wpdb->rows_affected == 1 ) {
376         do_action("update_option_{$option_name}", $oldvalue, $newvalue);
377         return true;
378     }
379     return false;
380 }
381
382 function update_user_option( $user_id, $option_name, $newvalue, $global = false ) {
383     global $wpdb;
384     if ( !$global )
385         $option_name = $wpdb->prefix . $option_name;
386     return update_usermeta( $user_id, $option_name, $newvalue );
387 }
388
389 // thx Alex Stapleton, http://alex.vort-x.net/blog/
390 function add_option($name, $value = '', $description = '', $autoload = 'yes') {
391     global $wpdb;
392
393     // Make sure the option doesn't already exist
394     if ( false !== get_option($name) )
395         return;
396
397     if ( is_array($value) || is_object($value) )
398         $value = serialize($value);
399
400     wp_cache_set($name, $value, 'options');
401
402     $name = $wpdb->escape($name);
403     $value = $wpdb->escape($value);
404     $description = $wpdb->escape($description);
405     $wpdb->query("INSERT INTO $wpdb->options (option_name, option_value, option_description, autoload) VALUES ('$name', '$value', '$description', '$autoload')");
406
407     return;
408 }
409
410 function delete_option($name) {
411     global $wpdb;
412     // Get the ID, if no ID then return
413     $option_id = $wpdb->get_var("SELECT option_id FROM $wpdb->options WHERE option_name = '$name'");
414     if ( !$option_id ) return false;
415     $wpdb->query("DELETE FROM $wpdb->options WHERE option_name = '$name'");
416     wp_cache_delete($name, 'options');
417     return true;
418 }
419
420 function add_post_meta($post_id, $key, $value, $unique = false) {
421     global $wpdb, $post_meta_cache;
422
423     if ( $unique ) {
424         if ( $wpdb->get_var("SELECT meta_key FROM $wpdb->postmeta WHERE meta_key
425 = '$key' AND post_id = '$post_id'") ) {
426             return false;
427         }
428     }
429
430     $original = $value;
431     if ( is_array($value) || is_object($value) )
432         $value = $wpdb->escape(serialize($value));
433
434     $wpdb->query("INSERT INTO $wpdb->postmeta (post_id,meta_key,meta_value) VALUES ('$post_id','$key','$value')");
435
436     $post_meta_cache['$post_id'][$key][] = $original;
437
438     return true;
439 }
440
441 function delete_post_meta($post_id, $key, $value = '') {
442     global $wpdb, $post_meta_cache;
443
444     if ( empty($value) ) {
445         $meta_id = $wpdb->get_var("SELECT meta_id FROM $wpdb->postmeta WHERE
446 post_id = '$post_id' AND meta_key = '$key'");
447     } else {
448         $meta_id = $wpdb->get_var("SELECT meta_id FROM $wpdb->postmeta WHERE
449 post_id = '$post_id' AND meta_key = '$key' AND meta_value = '$value'");
450     }
451
452     if ( !$meta_id )
453         return false;
454
455     if ( empty($value) ) {
456         $wpdb->query("DELETE FROM $wpdb->postmeta WHERE post_id = '$post_id'
457 AND meta_key = '$key'");
458         unset($post_meta_cache['$post_id'][$key]);
459     } else {
460         $wpdb->query("DELETE FROM $wpdb->postmeta WHERE post_id = '$post_id'
461</