]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/ms-blogs.php
Wordpress 3.0.6
[autoinstalls/wordpress.git] / wp-includes / ms-blogs.php
1 <?php
2
3 /**
4  * Site/blog functions that work with the blogs table and related data.
5  *
6  * @package WordPress
7  * @subpackage Multisite
8  * @since 3.0.0
9  */
10
11 // @todo use update_blog_details
12 function wpmu_update_blogs_date() {
13         global $wpdb;
14
15         $wpdb->update( $wpdb->blogs, array('last_updated' => current_time('mysql', true)), array('blog_id' => $wpdb->blogid) );
16         refresh_blog_details( $wpdb->blogid );
17
18         do_action( 'wpmu_blog_updated', $wpdb->blogid );
19 }
20
21 function get_blogaddress_by_id( $blog_id ) {
22         $bloginfo = get_blog_details( (int) $blog_id, false ); // only get bare details!
23         return esc_url( 'http://' . $bloginfo->domain . $bloginfo->path );
24 }
25
26 function get_blogaddress_by_name( $blogname ) {
27         global $current_site;
28
29         if ( is_subdomain_install() ) {
30                 if ( $blogname == 'main' )
31                         $blogname = 'www';
32                 $url = rtrim( network_home_url(), '/' );
33                 if ( !empty( $blogname ) )
34                         $url = preg_replace( '|^([^\.]+://)|', '$1' . $blogname . '.', $url );
35         } else {
36                 $url = network_home_url( $blogname );
37         }
38         return esc_url( $url . '/' );
39 }
40
41 function get_blogaddress_by_domain( $domain, $path ){
42         if ( is_subdomain_install() ) {
43                 $url = "http://".$domain.$path;
44         } else {
45                 if ( $domain != $_SERVER['HTTP_HOST'] ) {
46                         $blogname = substr( $domain, 0, strpos( $domain, '.' ) );
47                         $url = 'http://' . substr( $domain, strpos( $domain, '.' ) + 1 ) . $path;
48                         // we're not installing the main blog
49                         if ( $blogname != 'www.' )
50                                 $url .= $blogname . '/';
51                 } else { // main blog
52                         $url = 'http://' . $domain . $path;
53                 }
54         }
55         return esc_url( $url );
56 }
57
58 function get_id_from_blogname( $name ) {
59         global $wpdb, $current_site;
60         $blog_id = wp_cache_get( "get_id_from_blogname_" . $name, 'blog-details' );
61         if ( $blog_id )
62                 return $blog_id;
63
64         if ( is_subdomain_install() ) {
65                 $domain = $name . '.' . $current_site->domain;
66                 $path = $current_site->path;
67         } else {
68                 $domain = $current_site->domain;
69                 $path = $current_site->path . $name . '/';
70         }
71         $blog_id = $wpdb->get_var( $wpdb->prepare("SELECT blog_id FROM {$wpdb->blogs} WHERE domain = %s AND path = %s", $domain, $path) );
72         wp_cache_set( 'get_id_from_blogname_' . $name, $blog_id, 'blog-details' );
73         return $blog_id;
74 }
75
76 /**
77  * Retrieve the details for a blog from the blogs table and blog options.
78  *
79  * @since 3.0.0
80  * @param int|string|array $fields A blog ID, a blog name, or an array of fields to query against.
81  * @param bool $get_all Whether to retrieve all details or only the details in the blogs table. Default is true.
82  * @return object Blog details.
83  */
84 function get_blog_details( $fields, $get_all = true ) {
85         global $wpdb;
86
87         if ( is_array($fields ) ) {
88                 if ( isset($fields['blog_id']) ) {
89                         $blog_id = $fields['blog_id'];
90                 } elseif ( isset($fields['domain']) && isset($fields['path']) ) {
91                         $key = md5( $fields['domain'] . $fields['path'] );
92                         $blog = wp_cache_get($key, 'blog-lookup');
93                         if ( false !== $blog )
94                                 return $blog;
95                         if ( substr( $fields['domain'], 0, 4 ) == 'www.' ) {
96                                 $nowww = substr( $fields['domain'], 4 );
97                                 $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain IN (%s,%s) AND path = %s ORDER BY CHAR_LENGTH(domain) DESC", $nowww, $fields['domain'], $fields['path'] ) );
98                         } else {
99                                 $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain = %s AND path = %s", $fields['domain'], $fields['path'] ) );
100                         }
101                         if ( $blog ) {
102                                 wp_cache_set($blog->blog_id . 'short', $blog, 'blog-details');
103                                 $blog_id = $blog->blog_id;
104                         } else {
105                                 return false;
106                         }
107                 } elseif ( isset($fields['domain']) && is_subdomain_install() ) {
108                         $key = md5( $fields['domain'] );
109                         $blog = wp_cache_get($key, 'blog-lookup');
110                         if ( false !== $blog )
111                                 return $blog;
112                         if ( substr( $fields['domain'], 0, 4 ) == 'www.' ) {
113                                 $nowww = substr( $fields['domain'], 4 );
114                                 $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain IN (%s,%s) ORDER BY CHAR_LENGTH(domain) DESC", $nowww, $fields['domain'] ) );
115                         } else {
116                                 $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain = %s", $fields['domain'] ) );
117                         }
118                         if ( $blog ) {
119                                 wp_cache_set($blog->blog_id . 'short', $blog, 'blog-details');
120                                 $blog_id = $blog->blog_id;
121                         } else {
122                                 return false;
123                         }
124                 } else {
125                         return false;
126                 }
127         } else {
128                 if ( !is_numeric( $fields ) )
129                         $blog_id = get_id_from_blogname( $fields );
130                 else
131                         $blog_id = $fields;
132         }
133
134         $blog_id = (int) $blog_id;
135
136         $all = $get_all == true ? '' : 'short';
137         $details = wp_cache_get( $blog_id . $all, 'blog-details' );
138
139         if ( $details ) {
140                 if ( ! is_object( $details ) ) {
141                         if ( $details == -1 ) {
142                                 return false;
143                         } else {
144                                 // Clear old pre-serialized objects. Cache clients do better with that.
145                                 wp_cache_delete( $blog_id . $all, 'blog-details' );
146                                 unset($details);
147                         }
148                 } else {
149                         return $details;
150                 }
151         }
152
153         // Try the other cache.
154         if ( $get_all ) {
155                 $details = wp_cache_get( $blog_id . 'short', 'blog-details' );
156         } else {
157                 $details = wp_cache_get( $blog_id, 'blog-details' );
158                 // If short was requested and full cache is set, we can return.
159                 if ( $details ) {
160                         if ( ! is_object( $details ) ) {
161                                 if ( $details == -1 ) {
162                                         return false;
163                                 } else {
164                                         // Clear old pre-serialized objects. Cache clients do better with that.
165                                         wp_cache_delete( $blog_id, 'blog-details' );
166                                         unset($details);
167                                 }
168                         } else {
169                                 return $details;
170                         }
171                 }
172         }
173
174         if ( empty($details) ) {
175                 $details = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE blog_id = %d /* get_blog_details */", $blog_id ) );
176                 if ( ! $details ) {
177                         // Set the full cache.
178                         wp_cache_set( $blog_id, -1, 'blog-details' );
179                         return false;
180                 }
181         }
182
183         if ( ! $get_all ) {
184                 wp_cache_set( $blog_id . $all, $details, 'blog-details' );
185                 return $details;
186         }
187
188         $details->blogname              = get_blog_option( $blog_id, 'blogname' );
189         $details->siteurl               = get_blog_option( $blog_id, 'siteurl' );
190         $details->post_count    = get_blog_option( $blog_id, 'post_count' );
191
192         $details = apply_filters( 'blog_details', $details );
193
194         wp_cache_set( $blog_id . $all, $details, 'blog-details' );
195
196         $key = md5( $details->domain . $details->path );
197         wp_cache_set( $key, $details, 'blog-lookup' );
198
199         return $details;
200 }
201
202 /**
203  * Clear the blog details cache.
204  *
205  * @since 3.0.0
206  *
207  * @param int $blog_id Blog ID
208  */
209 function refresh_blog_details( $blog_id ) {
210         $blog_id = (int) $blog_id;
211         $details = get_blog_details( $blog_id, false );
212
213         wp_cache_delete( $blog_id , 'blog-details' );
214         wp_cache_delete( $blog_id . 'short' , 'blog-details' );
215         wp_cache_delete( md5( $details->domain . $details->path )  , 'blog-lookup' );
216         wp_cache_delete( 'current_blog_' . $details->domain, 'site-options' );
217         wp_cache_delete( 'current_blog_' . $details->domain . $details->path, 'site-options' );
218 }
219
220 /**
221  * Update the details for a blog. Updates the blogs table for a given blog id.
222  *
223  * @since 3.0.0
224  *
225  * @param int $blog_id Blog ID
226  * @param array $details Array of details keyed by blogs table field names.
227  * @return bool True if update succeeds, false otherwise.
228  */
229 function update_blog_details( $blog_id, $details = array() ) {
230         global $wpdb;
231
232         if ( empty($details) )
233                 return false;
234
235         if ( is_object($details) )
236                 $details = get_object_vars($details);
237
238         $current_details = get_blog_details($blog_id, false);
239         if ( empty($current_details) )
240                 return false;
241
242         $current_details = get_object_vars($current_details);
243
244         $details = array_merge($current_details, $details);
245         $details['last_updated'] = current_time('mysql', true);
246
247         $update_details = array();
248         $fields = array( 'site_id', 'domain', 'path', 'registered', 'last_updated', 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id');
249         foreach ( array_intersect( array_keys( $details ), $fields ) as $field )
250                 $update_details[$field] = $details[$field];
251
252         $wpdb->update( $wpdb->blogs, $update_details, array('blog_id' => $blog_id) );
253
254         // If spam status changed, issue actions.
255         if ( $details[ 'spam' ] != $current_details[ 'spam' ] ) {
256                 if ( $details[ 'spam' ] == 1 )
257                         do_action( "make_spam_blog", $blog_id );
258                 else
259                         do_action( "make_ham_blog", $blog_id );
260         }
261
262         if ( isset($details[ 'public' ]) )
263                 update_blog_option( $blog_id, 'blog_public', $details[ 'public' ], false );
264
265         refresh_blog_details($blog_id);
266
267         return true;
268 }
269
270 /**
271  * Retrieve option value based on setting name and blog_id.
272  *
273  * If the option does not exist or does not have a value, then the return value
274  * will be false. This is useful to check whether you need to install an option
275  * and is commonly used during installation of plugin options and to test
276  * whether upgrading is required.
277  *
278  * There is a filter called 'blog_option_$option' with the $option being
279  * replaced with the option name. The filter takes two parameters. $value and
280  * $blog_id. It returns $value.
281  * The 'option_$option' filter in get_option() is not called.
282  *
283  * @since NA
284  * @package WordPress MU
285  * @subpackage Option
286  * @uses apply_filters() Calls 'blog_option_$optionname' with the option name value.
287  *
288  * @param int $blog_id is the id of the blog.
289  * @param string $setting Name of option to retrieve. Should already be SQL-escaped
290  * @param string $default (optional) Default value returned if option not found.
291  * @return mixed Value set for the option.
292  */
293 function get_blog_option( $blog_id, $setting, $default = false ) {
294         global $wpdb;
295
296         $key = $blog_id."-".$setting."-blog_option";
297         $value = wp_cache_get( $key, "site-options" );
298         if ( $value == null ) {
299                 if ( $blog_id == $wpdb->blogid ) {
300                         $value = get_option( $setting, $default );
301                         $notoptions = wp_cache_get( 'notoptions', 'options' );
302                         if ( isset( $notoptions[$setting] ) ) {
303                                 wp_cache_set( $key, 'noop', 'site-options' );
304                                 $value = $default;
305                         } elseif ( $value == false ) {
306                                 wp_cache_set( $key, 'falsevalue', 'site-options' );
307                         } else {
308                                 wp_cache_set( $key, $value, 'site-options' );
309                         }
310                         return apply_filters( 'blog_option_' . $setting, $value, $blog_id );
311                 } else {
312                         $blog_prefix = $wpdb->get_blog_prefix( $blog_id );
313                         $row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$blog_prefix}options WHERE option_name = %s", $setting ) );
314                         if ( is_object( $row ) ) { // Has to be get_row instead of get_var because of funkiness with 0, false, null values
315                                 $value = $row->option_value;
316                                 if ( $value == false )
317                                         wp_cache_set( $key, 'falsevalue', 'site-options' );
318                                 else
319                                         wp_cache_set( $key, $value, 'site-options' );
320                         } else { // option does not exist, so we must cache its non-existence
321                                 wp_cache_set( $key, 'noop', 'site-options' );
322                                 $value = $default;
323                         }
324                 }
325         } elseif ( $value == 'noop' ) {
326                 $value = $default;
327         } elseif ( $value == 'falsevalue' ) {
328                 $value = false;
329         }
330         // If home is not set use siteurl.
331         if ( 'home' == $setting && '' == $value )
332                 return get_blog_option( $blog_id, 'siteurl' );
333
334         if ( 'siteurl' == $setting || 'home' == $setting || 'category_base' == $setting )
335                 $value = untrailingslashit( $value );
336
337         if (! @unserialize( $value ) )
338                 $value = stripslashes( $value );
339
340         return apply_filters( 'blog_option_' . $setting, maybe_unserialize( $value ), $blog_id );
341 }
342
343 function add_blog_option( $id, $key, $value ) {
344         $id = (int) $id;
345
346         switch_to_blog($id);
347         add_option( $key, $value );
348         restore_current_blog();
349         wp_cache_set( $id."-".$key."-blog_option", $value, 'site-options' );
350 }
351
352 function delete_blog_option( $id, $key ) {
353         $id = (int) $id;
354
355         switch_to_blog($id);
356         delete_option( $key );
357         restore_current_blog();
358         wp_cache_set( $id."-".$key."-blog_option", '', 'site-options' );
359 }
360
361 function update_blog_option( $id, $key, $value, $refresh = true ) {
362         $id = (int) $id;
363
364         switch_to_blog($id);
365         update_option( $key, $value );
366         restore_current_blog();
367
368         if ( $refresh == true )
369                 refresh_blog_details( $id );
370         wp_cache_set( $id."-".$key."-blog_option", $value, 'site-options');
371 }
372
373 function switch_to_blog( $new_blog, $validate = false ) {
374         global $wpdb, $table_prefix, $blog_id, $switched, $switched_stack, $wp_roles, $wp_object_cache;
375
376         if ( empty($new_blog) )
377                 $new_blog = $blog_id;
378
379         if ( $validate && ! get_blog_details( $new_blog ) )
380                 return false;
381
382         if ( empty($switched_stack) )
383                 $switched_stack = array();
384
385         $switched_stack[] = $blog_id;
386
387         /* If we're switching to the same blog id that we're on,
388         * set the right vars, do the associated actions, but skip
389         * the extra unnecessary work */
390         if ( $blog_id == $new_blog ) {
391                 do_action( 'switch_blog', $blog_id, $blog_id );
392                 $switched = true;
393                 return true;
394         }
395
396         $wpdb->set_blog_id($new_blog);
397         $table_prefix = $wpdb->prefix;
398         $prev_blog_id = $blog_id;
399         $blog_id = $new_blog;
400
401         if ( is_object( $wp_roles ) ) {
402                 $wpdb->suppress_errors();
403                 if ( method_exists( $wp_roles ,'_init' ) )
404                         $wp_roles->_init();
405                 elseif ( method_exists( $wp_roles, '__construct' ) )
406                         $wp_roles->__construct();
407                 $wpdb->suppress_errors( false );
408         }
409  
410         if ( did_action('init') ) {
411                 $current_user = wp_get_current_user();  
412                 if ( is_object( $current_user ) )
413                         $current_user->for_blog( $blog_id );
414         }
415
416         if ( is_object( $wp_object_cache ) && isset( $wp_object_cache->global_groups ) )
417                 $global_groups = $wp_object_cache->global_groups;
418         else
419                 $global_groups = false;
420
421         wp_cache_init();
422         if ( function_exists('wp_cache_add_global_groups') ) {
423                 if ( is_array( $global_groups ) )
424                         wp_cache_add_global_groups( $global_groups );
425                 else
426                         wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'site-options', 'site-lookup', 'blog-lookup', 'blog-details', 'rss', 'site-transient', 'global-posts' ) );
427                 wp_cache_add_non_persistent_groups(array( 'comment', 'counts', 'plugins' ));
428         }
429
430         do_action('switch_blog', $blog_id, $prev_blog_id);
431         $switched = true;
432         return true;
433 }
434
435 function restore_current_blog() {
436         global $table_prefix, $wpdb, $blog_id, $switched, $switched_stack, $wp_roles, $wp_object_cache;
437
438         if ( !$switched )
439                 return false;
440
441         if ( !is_array( $switched_stack ) )
442                 return false;
443
444         $blog = array_pop( $switched_stack );
445         if ( $blog_id == $blog ) {
446                 do_action( 'switch_blog', $blog, $blog );
447                 /* If we still have items in the switched stack, consider ourselves still 'switched' */
448                 $switched = ( is_array( $switched_stack ) && count( $switched_stack ) > 0 );
449                 return true;
450         }
451
452         $wpdb->set_blog_id($blog);
453         $prev_blog_id = $blog_id;
454         $blog_id = $blog;
455         $table_prefix = $wpdb->prefix;
456
457         if ( is_object( $wp_roles ) ) {
458                 $wpdb->suppress_errors();
459                 if ( method_exists( $wp_roles ,'_init' ) )
460                         $wp_roles->_init();
461                 elseif ( method_exists( $wp_roles, '__construct' ) )
462                         $wp_roles->__construct();
463                 $wpdb->suppress_errors( false );
464         }
465
466         if ( did_action('init') ) {
467                 $current_user = wp_get_current_user();
468                 if ( is_object( $current_user ) )
469                         $current_user->for_blog( $blog_id );
470         }
471
472         if ( is_object( $wp_object_cache ) && isset( $wp_object_cache->global_groups ) )
473                 $global_groups = $wp_object_cache->global_groups;
474         else
475                 $global_groups = false;
476
477         wp_cache_init();
478         if ( function_exists('wp_cache_add_global_groups') ) {
479                 if ( is_array( $global_groups ) )
480                         wp_cache_add_global_groups( $global_groups );
481                 else
482                         wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'site-options', 'site-lookup', 'blog-lookup', 'blog-details', 'rss', 'site-transient' ) );
483                 wp_cache_add_non_persistent_groups(array( 'comment', 'counts', 'plugins' ));
484         }
485
486         do_action('switch_blog', $blog_id, $prev_blog_id);
487
488         /* If we still have items in the switched stack, consider ourselves still 'switched' */
489         $switched = ( is_array( $switched_stack ) && count( $switched_stack ) > 0 );
490         return true;
491 }
492
493 function is_archived( $id ) {
494         return get_blog_status($id, 'archived');
495 }
496
497 function update_archived( $id, $archived ) {
498         update_blog_status($id, 'archived', $archived);
499         return $archived;
500 }
501
502 /**
503  * Update a blog details field.
504  *
505  * @since 3.0.0
506  *
507  * @param int $blog_id BLog ID
508  * @param string $pref A field name
509  * @param string $value Value for $pref
510  * @param bool $refresh Whether to refresh the blog details cache. Default is true.
511  */
512 function update_blog_status( $blog_id, $pref, $value, $refresh = true ) {
513         global $wpdb;
514
515         if ( !in_array( $pref, array( 'site_id', 'domain', 'path', 'registered', 'last_updated', 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id') ) )
516                 return $value;
517
518         $wpdb->update( $wpdb->blogs, array($pref => $value, 'last_updated' => current_time('mysql', true)), array('blog_id' => $blog_id) );
519
520         if ( $refresh )
521                 refresh_blog_details($blog_id);
522
523         if ( $pref == 'spam' ) {
524                 if ( $value == 1 )
525                         do_action( "make_spam_blog", $blog_id );
526                 else
527                         do_action( "make_ham_blog", $blog_id );
528         }
529
530         return $value;
531 }
532
533 function get_blog_status( $id, $pref ) {
534         global $wpdb;
535
536         $details = get_blog_details( $id, false );
537         if ( $details )
538                 return $details->$pref;
539
540         return $wpdb->get_var( $wpdb->prepare("SELECT %s FROM {$wpdb->blogs} WHERE blog_id = %d", $pref, $id) );
541 }
542
543 function get_last_updated( $deprecated = '', $start = 0, $quantity = 40 ) {
544         global $wpdb;
545         return $wpdb->get_results( $wpdb->prepare("SELECT blog_id, domain, path FROM $wpdb->blogs WHERE site_id = %d AND public = '1' AND archived = '0' AND mature = '0' AND spam = '0' AND deleted = '0' AND last_updated != '0000-00-00 00:00:00' ORDER BY last_updated DESC limit %d, %d", $wpdb->siteid, $start, $quantity ) , ARRAY_A );
546 }
547
548 ?>