root/branches/1.5/wp-includes/functions.php

Revision 2616, 50.5 kB (checked in by matt, 4 years ago)

Bad cat ID cleanup

  • 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['wordpressuser_' . COOKIEHASH]);
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     if (!empty($month) && !empty($weekday) && $translate) {
26         $datemonth = $month[date('m', $i)];
27         $datemonth_abbrev = $month_abbrev[$datemonth];
28         $dateweekday = $weekday[date('w', $i)];
29         $dateweekday_abbrev = $weekday_abbrev[$dateweekday];         
30         $dateformatstring = ' '.$dateformatstring;
31         $dateformatstring = preg_replace("/([^\\\])D/", "\\1".backslashit($dateweekday_abbrev), $dateformatstring);
32         $dateformatstring = preg_replace("/([^\\\])F/", "\\1".backslashit($datemonth), $dateformatstring);
33         $dateformatstring = preg_replace("/([^\\\])l/", "\\1".backslashit($dateweekday), $dateformatstring);
34         $dateformatstring = preg_replace("/([^\\\])M/", "\\1".backslashit($datemonth_abbrev), $dateformatstring);
35     
36         $dateformatstring = substr($dateformatstring, 1, strlen($dateformatstring)-1);
37     }
38     $j = @date($dateformatstring, $i);
39     if (!$j) {
40     // for debug purposes
41     //    echo $i." ".$mysqlstring;
42     }
43     return $j;
44 }
45
46 function current_time($type, $gmt = 0) {
47     switch ($type) {
48         case 'mysql':
49             if ($gmt) $d = gmdate('Y-m-d H:i:s');
50             else $d = gmdate('Y-m-d H:i:s', (time() + (get_settings('gmt_offset') * 3600)));
51             return $d;
52             break;
53         case 'timestamp':
54             if ($gmt) $d = time();
55             else $d = time() + (get_settings('gmt_offset') * 3600);
56             return $d;
57             break;
58     }
59 }
60
61 function date_i18n($dateformatstring, $unixtimestamp) {
62     global $month, $weekday, $month_abbrev, $weekday_abbrev;
63     $i = $unixtimestamp;
64     if ((!empty($month)) && (!empty($weekday))) {
65         $datemonth = $month[date('m', $i)];
66         $datemonth_abbrev = $month_abbrev[$datemonth];
67         $dateweekday = $weekday[date('w', $i)];
68         $dateweekday_abbrev = $weekday_abbrev[$dateweekday];         
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 = substr($dateformatstring, 1, strlen($dateformatstring)-1);
75     }
76     $j = @date($dateformatstring, $i);
77     return $j;
78     }
79
80 function get_weekstartend($mysqlstring, $start_of_week) {
81     $my = substr($mysqlstring,0,4);
82     $mm = substr($mysqlstring,8,2);
83     $md = substr($mysqlstring,5,2);
84     $day = mktime(0,0,0, $md, $mm, $my);
85     $weekday = date('w',$day);
86     $i = 86400;
87
88     if ($weekday < get_settings('start_of_week'))
89         $weekday = 7 - (get_settings('start_of_week') - $weekday);
90
91     while ($weekday > get_settings('start_of_week')) {
92         $weekday = date('w',$day);
93         if ($weekday < get_settings('start_of_week'))
94             $weekday = 7 - (get_settings('start_of_week') - $weekday);
95
96         $day = $day - 86400;
97         $i = 0;
98     }
99     $week['start'] = $day + 86400 - $i;
100     //$week['end']   = $day - $i + 691199;
101     $week['end'] = $week['start'] + 604799;
102     return $week;
103 }
104
105 function get_lastpostdate($timezone = 'server') {
106     global $cache_lastpostdate, $pagenow, $wpdb;
107     $add_seconds_blog = get_settings('gmt_offset') * 3600;
108     $add_seconds_server = date('Z');
109     $now = current_time('mysql', 1);
110     if ( !isset($cache_lastpostdate[$timezone]) ) {
111         switch(strtolower($timezone)) {
112             case 'gmt':
113                 $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");
114                 break;
115             case 'blog':
116                 $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");
117                 break;
118             case 'server':
119                 $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");
120                 break;
121         }
122         $cache_lastpostdate[$timezone] = $lastpostdate;
123     } else {
124         $lastpostdate = $cache_lastpostdate[$timezone];
125     }
126     return $lastpostdate;
127 }
128
129 function get_lastpostmodified($timezone = 'server') {
130     global $cache_lastpostmodified, $pagenow, $wpdb;
131     $add_seconds_blog = get_settings('gmt_offset') * 3600;
132     $add_seconds_server = date('Z');
133     $now = current_time('mysql', 1);
134     if ( !isset($cache_lastpostmodified[$timezone]) ) {
135         switch(strtolower($timezone)) {
136             case 'gmt':
137                 $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");
138                 break;
139             case 'blog':
140                 $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");
141                 break;
142             case 'server':
143                 $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");
144                 break;
145         }
146         $lastpostdate = get_lastpostdate($timezone);
147         if ($lastpostdate > $lastpostmodified) {
148             $lastpostmodified = $lastpostdate;
149         }
150         $cache_lastpostmodified[$timezone] = $lastpostmodified;
151     } else {
152         $lastpostmodified = $cache_lastpostmodified[$timezone];
153     }
154     return $lastpostmodified;
155 }
156
157 function user_pass_ok($user_login,$user_pass) {
158     global $cache_userdata;
159     if ( empty($cache_userdata[$user_login]) ) {
160         $userdata = get_userdatabylogin($user_login);
161     } else {
162         $userdata = $cache_userdata[$user_login];
163     }
164     return (md5($user_pass) == $userdata->user_pass);
165 }
166
167
168 function get_usernumposts($userid) {
169     global $wpdb;
170     return $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_author = '$userid' AND post_status = 'publish'");
171 }
172
173 // examine a url (supposedly from this blog) and try to
174 // determine the post ID it represents.
175 function url_to_postid($url) {
176     global $wp_rewrite;
177
178     // First, check to see if there is a 'p=N' or 'page_id=N' to match against:
179     preg_match('#[?&](p|page_id)=(\d+)#', $url, $values);
180     $id = intval($values[2]);
181     if ($id) return $id;
182
183     // URI is probably a permalink.
184     $rewrite = $wp_rewrite->wp_rewrite_rules();
185
186     if ( empty($rewrite) )
187         return 0;
188
189     $req_uri = $url;
190
191     if ( false !== strpos($req_uri, get_settings('home')) ) {
192         $req_uri = str_replace(get_settings('home'), '', $req_uri);
193     } else {
194         $home_path = parse_url(get_settings('home'));
195         $home_path = $home_path['path'];
196         $req_uri = str_replace($home_path, '', $req_uri);
197     }
198
199     $req_uri = trim($req_uri, '/');
200     $request = $req_uri;
201     
202     // Look for matches.
203     $request_match = $request;
204     foreach ($rewrite as $match => $query) {
205         // If the requesting file is the anchor of the match, prepend it
206         // to the path info.
207         if ((! empty($req_uri)) && (strpos($match, $req_uri) === 0)) {
208             $request_match = $req_uri . '/' . $request;
209         }
210
211         if (preg_match("!^$match!", $request_match, $matches)) {
212             // Got a match.
213             // Trim the query of everything up to the '?'.
214             $query = preg_replace("!^.+\?!", '', $query);
215             
216             // Substitute the substring matches into the query.
217             eval("\$query = \"$query\";");
218             $query = new WP_Query($query);
219             if ( !empty($query->post) )
220                 return $query->post->ID;
221             else
222                 return 0;
223         }
224     }
225
226     return 0;
227 }
228
229
230 /* Options functions */
231
232 function get_settings($setting) {
233   global $wpdb, $cache_settings, $cache_nonexistantoptions;
234     if ( strstr($_SERVER['REQUEST_URI'], 'wp-admin/install.php') || defined('WP_INSTALLING') )
235         return false;
236
237     if ( empty($cache_settings) )
238         $cache_settings = get_alloptions();
239
240     if ( empty($cache_nonexistantoptions) )
241         $cache_nonexistantoptions = array();
242
243     if ('home' == $setting && '' == $cache_settings->home)
244         return apply_filters('option_' . $setting, $cache_settings->siteurl);
245
246     if ( isset($cache_settings->$setting) ) :
247         return apply_filters('option_' . $setting, $cache_settings->$setting);
248     else :
249         // for these cases when we're asking for an unknown option
250         if ( isset($cache_nonexistantoptions[$setting]) )
251             return false;
252
253         $option = $wpdb->get_var("SELECT option_value FROM $wpdb->options WHERE option_name = '$setting'");
254
255         if (!$option) :
256             $cache_nonexistantoptions[$setting] = true;
257             return false;
258         endif;
259
260         @ $kellogs = unserialize($option);
261         if ($kellogs !== FALSE)
262             return apply_filters('option_' . $setting, $kellogs);
263         else return apply_filters('option_' . $setting, $option);
264     endif;
265 }
266
267 function get_option($option) {
268     return get_settings($option);
269 }
270
271 function form_option($option) {
272     echo htmlspecialchars( get_option($option), ENT_QUOTES );
273 }
274
275 function get_alloptions() {
276     global $wpdb, $wp_queries;
277     $wpdb->hide_errors();
278     if (!$options = $wpdb->get_results("SELECT option_name, option_value FROM $wpdb->options WHERE autoload = 'yes'")) {
279         $options = $wpdb->get_results("SELECT option_name, option_value FROM $wpdb->options");
280     }
281     $wpdb->show_errors();
282
283     foreach ($options as $option) {
284         // "When trying to design a foolproof system,
285         //  never underestimate the ingenuity of the fools :)" -- Dougal
286         if ('siteurl' == $option->option_name) $option->option_value = preg_replace('|/+$|', '', $option->option_value);
287         if ('home' == $option->option_name) $option->option_value = preg_replace('|/+$|', '', $option->option_value);
288         if ('category_base' == $option->option_name) $option->option_value = preg_replace('|/+$|', '', $option->option_value);
289         @ $value = unserialize($option->option_value);
290         if ($value === FALSE)
291             $value = $option->option_value;
292         $all_options->{$option->option_name} = apply_filters('pre_option_' . $option->option_name, $value);
293     }
294     return apply_filters('all_options', $all_options);
295 }
296
297 function update_option($option_name, $newvalue) {
298     global $wpdb, $cache_settings;
299     if ( is_array($newvalue) || is_object($newvalue) )
300         $newvalue = serialize($newvalue);
301
302     $newvalue = trim($newvalue); // I can't think of any situation we wouldn't want to trim
303
304     // If the new and old values are the same, no need to update.
305     if ($newvalue == get_option($option_name)) {
306         return true;
307     }
308
309     // If it's not there add it
310     if ( !$wpdb->get_var("SELECT option_name FROM $wpdb->options WHERE option_name = '$option_name'") )
311         add_option($option_name);
312
313     $newvalue = $wpdb->escape($newvalue);
314     $wpdb->query("UPDATE $wpdb->options SET option_value = '$newvalue' WHERE option_name = '$option_name'");
315     $cache_settings = get_alloptions(); // Re cache settings
316     return true;
317 }
318
319
320 // thx Alex Stapleton, http://alex.vort-x.net/blog/
321 function add_option($name, $value = '', $description = '', $autoload = 'yes') {
322     global $wpdb;
323     $original = $value;
324     if ( is_array($value) || is_object($value) )
325         $value = serialize($value);
326
327     if( !$wpdb->get_var("SELECT option_name FROM $wpdb->options WHERE option_name = '$name'") ) {
328         $name = $wpdb->escape($name);
329         $value = $wpdb->escape($value);
330         $description = $wpdb->escape($description);
331         $wpdb->query("INSERT INTO $wpdb->options (option_name, option_value, option_description, autoload) VALUES ('$name', '$value', '$description', '$autoload')");
332
333         if($wpdb->insert_id) {
334             global $cache_settings;
335             $cache_settings->{$name} = $original;
336         }
337     }
338     return;
339 }
340
341 function delete_option($name) {
342     global $wpdb;
343     // Get the ID, if no ID then return
344     $option_id = $wpdb->get_var("SELECT option_id FROM $wpdb->options WHERE option_name = '$name'");
345     if (!$option_id) return false;
346     $wpdb->query("DELETE FROM $wpdb->options WHERE option_name = '$name'");
347     return true;
348 }
349
350 function add_post_meta($post_id, $key, $value, $unique = false) {
351     global $wpdb;
352     
353     if ($unique) {
354         if( $wpdb->get_var("SELECT meta_key FROM $wpdb->postmeta WHERE meta_key
355 = '$key' AND post_id = '$post_id'") ) {
356             return false;
357         }
358     }
359
360     $wpdb->query("INSERT INTO $wpdb->postmeta
361                                 (post_id,meta_key,meta_value)
362                                 VALUES ('$post_id','$key','$value')
363                         ");
364     
365     return true;
366 }
367
368 function delete_post_meta($post_id, $key, $value = '') {
369     global $wpdb;
370
371     if (empty($value)) {
372         $meta_id = $wpdb->get_var("SELECT meta_id FROM $wpdb->postmeta WHERE
373 post_id = '$post_id' AND meta_key = '$key'");
374     } else {
375         $meta_id = $wpdb->get_var("SELECT meta_id FROM $wpdb->postmeta WHERE
376 post_id = '$post_id' AND meta_key = '$key' AND meta_value = '$value'");
377     }
378
379     if (!$meta_id) return false;
380
381     if (empty($value)) {
382         $wpdb->query("DELETE FROM $wpdb->postmeta WHERE post_id = '$post_id'
383 AND meta_key = '$key'");
384     } else {
385         $wpdb->query("DELETE FROM $wpdb->postmeta WHERE post_id = '$post_id'
386 AND meta_key = '$key' AND meta_value = '$value'");
387     }
388         
389     return true;
390 }
391
392 function get_post_meta($post_id, $key, $single = false) {
393     global $wpdb, $post_meta_cache;
394
395     if (isset($post_meta_cache[$post_id][$key])) {
396         if ($single) {
397             return $post_meta_cache[$post_id][$key][0];
398         } else {
399             return $post_meta_cache[$post_id][$key];
400         }
401     }
402
403     $metalist = $wpdb->get_results("SELECT meta_value FROM $wpdb->postmeta WHERE post_id = '$post_id' AND meta_key = '$key'", ARRAY_N);
404
405     $values = array();
406     if ($metalist) {
407         foreach ($metalist as $metarow) {
408             $values[] = $metarow[0];
409         }
410     }
411
412     if ($single) {
413         if (count($values)) {
414             return $values[0];
415         } else {
416             return '';
417         }
418     } else {
419         return $values;
420     }
421 }
422
423 function update_post_meta($post_id, $key, $value, $prev_value = '') {
424     global $wpdb, $post_meta_cache;
425
426         if(! $wpdb->get_var("SELECT meta_key FROM $wpdb->postmeta WHERE meta_key
427 = '$key' AND post_id = '$post_id'") ) {
428             return false;
429         }
430
431     if (empty($prev_value)) {
432         $wpdb->query("UPDATE $wpdb->postmeta SET meta_value = '$value' WHERE
433 meta_key = '$key' AND post_id = '$post_id'");
434     } else {
435         $wpdb->query("UPDATE $wpdb->postmeta SET meta_value = '$value' WHERE
436 meta_key = '$key' AND post_id = '$post_id' AND meta_value = '$prev_value'");
437     }
438
439     return true;
440 }
441
442 // Deprecated.  Use get_post().
443 function get_postdata($postid) {
444     $post = &get_post($postid);
445     
446     $postdata = array (
447         'ID' => $post->ID,
448         'Author_ID' => $post->post_author,
449         'Date' => $post->post_date,
450         'Content' => $post->post_content,
451         'Excerpt' => $post->post_excerpt,
452         'Title' => $post->post_title,
453         'Category' => $post->post_category,
454         'post_status' => $post->post_status,
455         'comment_status' => $post->comment_status,
456         'ping_status' => $post->ping_status,
457         'post_password' => $post->post_password,
458         'to_ping' => $post->to_ping,
459         'pinged' => $post->pinged,
460         'post_name' => $post->post_name
461     );
462
463     return $postdata;
464 }
465
466 // Retrieves post data given a post ID or post object.
467 // Handles post caching.
468 function &get_post(&$post, $output = OBJECT) {
469