]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/ms-blogs.php
Wordpress 3.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 MU
9  */
10
11 /**
12  * Update the last_updated field for the current blog.
13  *
14  * @since MU
15  */
16 function wpmu_update_blogs_date() {
17         global $wpdb;
18
19         update_blog_details( $wpdb->blogid, array('last_updated' => current_time('mysql', true)) );
20
21         do_action( 'wpmu_blog_updated', $wpdb->blogid );
22 }
23
24 /**
25  * Get a full blog URL, given a blog id.
26  *
27  * @since MU
28  *
29  * @param int $blog_id Blog ID
30  * @return string
31  */
32 function get_blogaddress_by_id( $blog_id ) {
33         $bloginfo = get_blog_details( (int) $blog_id, false ); // only get bare details!
34         return esc_url( 'http://' . $bloginfo->domain . $bloginfo->path );
35 }
36
37 /**
38  * Get a full blog URL, given a blog name.
39  *
40  * @since MU
41  *
42  * @param string $blogname The (subdomain or directory) name
43  * @return string
44  */
45 function get_blogaddress_by_name( $blogname ) {
46         if ( is_subdomain_install() ) {
47                 if ( $blogname == 'main' )
48                         $blogname = 'www';
49                 $url = rtrim( network_home_url(), '/' );
50                 if ( !empty( $blogname ) )
51                         $url = preg_replace( '|^([^\.]+://)|', "\${1}" . $blogname . '.', $url );
52         } else {
53                 $url = network_home_url( $blogname );
54         }
55         return esc_url( $url . '/' );
56 }
57
58 /**
59  * Get a full blog URL, given a domain and a path.
60  *
61  * @since MU
62  *
63  * @param string $domain
64  * @param string $path
65  * @return string
66  */
67 function get_blogaddress_by_domain( $domain, $path ) {
68         if ( is_subdomain_install() ) {
69                 $url = "http://" . $domain.$path;
70         } else {
71                 if ( $domain != $_SERVER['HTTP_HOST'] ) {
72                         $blogname = substr( $domain, 0, strpos( $domain, '.' ) );
73                         $url = 'http://' . substr( $domain, strpos( $domain, '.' ) + 1 ) . $path;
74                         // we're not installing the main blog
75                         if ( $blogname != 'www.' )
76                                 $url .= $blogname . '/';
77                 } else { // main blog
78                         $url = 'http://' . $domain . $path;
79                 }
80         }
81         return esc_url( $url );
82 }
83
84 /**
85  * Given a blog's (subdomain or directory) slug, retrieve its id.
86  *
87  * @since MU
88  *
89  * @param string $slug
90  * @return int A blog id
91  */
92 function get_id_from_blogname( $slug ) {
93         global $wpdb, $current_site;
94
95         $slug = trim( $slug, '/' );
96
97         $blog_id = wp_cache_get( 'get_id_from_blogname_' . $slug, 'blog-details' );
98         if ( $blog_id )
99                 return $blog_id;
100
101         if ( is_subdomain_install() ) {
102                 $domain = $slug . '.' . $current_site->domain;
103                 $path = $current_site->path;
104         } else {
105                 $domain = $current_site->domain;
106                 $path = $current_site->path . $slug . '/';
107         }
108
109         $blog_id = $wpdb->get_var( $wpdb->prepare("SELECT blog_id FROM {$wpdb->blogs} WHERE domain = %s AND path = %s", $domain, $path) );
110         wp_cache_set( 'get_id_from_blogname_' . $slug, $blog_id, 'blog-details' );
111         return $blog_id;
112 }
113
114 /**
115  * Retrieve the details for a blog from the blogs table and blog options.
116  *
117  * @since MU
118  *
119  * @param int|string|array $fields A blog ID, a blog slug, or an array of fields to query against. Optional. If not specified the current blog ID is used.
120  * @param bool $get_all Whether to retrieve all details or only the details in the blogs table. Default is true.
121  * @return object Blog details.
122  */
123 function get_blog_details( $fields = null, $get_all = true ) {
124         global $wpdb;
125
126         if ( is_array($fields ) ) {
127                 if ( isset($fields['blog_id']) ) {
128                         $blog_id = $fields['blog_id'];
129                 } elseif ( isset($fields['domain']) && isset($fields['path']) ) {
130                         $key = md5( $fields['domain'] . $fields['path'] );
131                         $blog = wp_cache_get($key, 'blog-lookup');
132                         if ( false !== $blog )
133                                 return $blog;
134                         if ( substr( $fields['domain'], 0, 4 ) == 'www.' ) {
135                                 $nowww = substr( $fields['domain'], 4 );
136                                 $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'] ) );
137                         } else {
138                                 $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain = %s AND path = %s", $fields['domain'], $fields['path'] ) );
139                         }
140                         if ( $blog ) {
141                                 wp_cache_set($blog->blog_id . 'short', $blog, 'blog-details');
142                                 $blog_id = $blog->blog_id;
143                         } else {
144                                 return false;
145                         }
146                 } elseif ( isset($fields['domain']) && is_subdomain_install() ) {
147                         $key = md5( $fields['domain'] );
148                         $blog = wp_cache_get($key, 'blog-lookup');
149                         if ( false !== $blog )
150                                 return $blog;
151                         if ( substr( $fields['domain'], 0, 4 ) == 'www.' ) {
152                                 $nowww = substr( $fields['domain'], 4 );
153                                 $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain IN (%s,%s) ORDER BY CHAR_LENGTH(domain) DESC", $nowww, $fields['domain'] ) );
154                         } else {
155                                 $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain = %s", $fields['domain'] ) );
156                         }
157                         if ( $blog ) {
158                                 wp_cache_set($blog->blog_id . 'short', $blog, 'blog-details');
159                                 $blog_id = $blog->blog_id;
160                         } else {
161                                 return false;
162                         }
163                 } else {
164                         return false;
165                 }
166         } else {
167                 if ( ! $fields )
168                         $blog_id = get_current_blog_id();
169                 elseif ( ! is_numeric( $fields ) )
170                         $blog_id = get_id_from_blogname( $fields );
171                 else
172                         $blog_id = $fields;
173         }
174
175         $blog_id = (int) $blog_id;
176
177         $all = $get_all == true ? '' : 'short';
178         $details = wp_cache_get( $blog_id . $all, 'blog-details' );
179
180         if ( $details ) {
181                 if ( ! is_object( $details ) ) {
182                         if ( $details == -1 ) {
183                                 return false;
184                         } else {
185                                 // Clear old pre-serialized objects. Cache clients do better with that.
186                                 wp_cache_delete( $blog_id . $all, 'blog-details' );
187                                 unset($details);
188                         }
189                 } else {
190                         return $details;
191                 }
192         }
193
194         // Try the other cache.
195         if ( $get_all ) {
196                 $details = wp_cache_get( $blog_id . 'short', 'blog-details' );
197         } else {
198                 $details = wp_cache_get( $blog_id, 'blog-details' );
199                 // If short was requested and full cache is set, we can return.
200                 if ( $details ) {
201                         if ( ! is_object( $details ) ) {
202                                 if ( $details == -1 ) {
203                                         return false;
204                                 } else {
205                                         // Clear old pre-serialized objects. Cache clients do better with that.
206                                         wp_cache_delete( $blog_id, 'blog-details' );
207                                         unset($details);
208                                 }
209                         } else {
210                                 return $details;
211                         }
212                 }
213         }
214
215         if ( empty($details) ) {
216                 $details = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE blog_id = %d /* get_blog_details */", $blog_id ) );
217                 if ( ! $details ) {
218                         // Set the full cache.
219                         wp_cache_set( $blog_id, -1, 'blog-details' );
220                         return false;
221                 }
222         }
223
224         if ( ! $get_all ) {
225                 wp_cache_set( $blog_id . $all, $details, 'blog-details' );
226                 return $details;
227         }
228
229         switch_to_blog( $blog_id );
230         $details->blogname              = get_option( 'blogname' );
231         $details->siteurl               = get_option( 'siteurl' );
232         $details->post_count    = get_option( 'post_count' );
233         restore_current_blog();
234
235         $details = apply_filters( 'blog_details', $details );
236
237         wp_cache_set( $blog_id . $all, $details, 'blog-details' );
238
239         $key = md5( $details->domain . $details->path );
240         wp_cache_set( $key, $details, 'blog-lookup' );
241
242         return $details;
243 }
244
245 /**
246  * Clear the blog details cache.
247  *
248  * @since MU
249  *
250  * @param int $blog_id Blog ID
251  */
252 function refresh_blog_details( $blog_id ) {
253         $blog_id = (int) $blog_id;
254         $details = get_blog_details( $blog_id, false );
255         if ( ! $details ) {
256                 // Make sure clean_blog_cache() gets the blog ID
257                 // when the blog has been previously cached as
258                 // non-existent.
259                 $details = (object) array(
260                         'blog_id' => $blog_id,
261                         'domain' => null,
262                         'path' => null
263                 );
264         }
265
266         clean_blog_cache( $details );
267
268         do_action( 'refresh_blog_details', $blog_id );
269 }
270
271 /**
272  * Update the details for a blog. Updates the blogs table for a given blog id.
273  *
274  * @since MU
275  *
276  * @param int $blog_id Blog ID
277  * @param array $details Array of details keyed by blogs table field names.
278  * @return bool True if update succeeds, false otherwise.
279  */
280 function update_blog_details( $blog_id, $details = array() ) {
281         global $wpdb;
282
283         if ( empty($details) )
284                 return false;
285
286         if ( is_object($details) )
287                 $details = get_object_vars($details);
288
289         $current_details = get_blog_details($blog_id, false);
290         if ( empty($current_details) )
291                 return false;
292
293         $current_details = get_object_vars($current_details);
294
295         $details = array_merge($current_details, $details);
296         $details['last_updated'] = current_time('mysql', true);
297
298         $update_details = array();
299         $fields = array( 'site_id', 'domain', 'path', 'registered', 'last_updated', 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id');
300         foreach ( array_intersect( array_keys( $details ), $fields ) as $field )
301                 $update_details[$field] = $details[$field];
302
303         $result = $wpdb->update( $wpdb->blogs, $update_details, array('blog_id' => $blog_id) );
304
305         if ( false === $result )
306                 return false;
307
308         // If spam status changed, issue actions.
309         if ( $details[ 'spam' ] != $current_details[ 'spam' ] ) {
310                 if ( $details[ 'spam' ] == 1 )
311                         do_action( 'make_spam_blog', $blog_id );
312                 else
313                         do_action( 'make_ham_blog', $blog_id );
314         }
315
316         // If mature status changed, issue actions.
317         if ( $details[ 'mature' ] != $current_details[ 'mature' ] ) {
318                 if ( $details[ 'mature' ] == 1 )
319                         do_action( 'mature_blog', $blog_id );
320                 else
321                         do_action( 'unmature_blog', $blog_id );
322         }
323
324         // If archived status changed, issue actions.
325         if ( $details[ 'archived' ] != $current_details[ 'archived' ] ) {
326                 if ( $details[ 'archived' ] == 1 )
327                         do_action( 'archive_blog', $blog_id );
328                 else
329                         do_action( 'unarchive_blog', $blog_id );
330         }
331
332         // If deleted status changed, issue actions.
333         if ( $details[ 'deleted' ] != $current_details[ 'deleted' ] ) {
334                 if ( $details[ 'deleted' ] == 1 )
335                         do_action( 'make_delete_blog', $blog_id );
336                 else
337                         do_action( 'make_undelete_blog', $blog_id );
338         }
339
340         if ( isset( $details[ 'public' ] ) ) {
341                 switch_to_blog( $blog_id );
342                 update_option( 'blog_public', $details[ 'public' ] );
343                 restore_current_blog();
344         }
345
346         refresh_blog_details($blog_id);
347
348         return true;
349 }
350
351 /**
352  * Clean the blog cache
353  *
354  * @since 3.5.0
355  *
356  * @param stdClass $blog The blog details as returned from get_blog_details()
357  */
358 function clean_blog_cache( $blog ) {
359         $blog_id = $blog->blog_id;
360         $domain_path_key = md5( $blog->domain . $blog->path );
361
362         wp_cache_delete( $blog_id , 'blog-details' );
363         wp_cache_delete( $blog_id . 'short' , 'blog-details' );
364         wp_cache_delete(  $domain_path_key, 'blog-lookup' );
365         wp_cache_delete( 'current_blog_' . $blog->domain, 'site-options' );
366         wp_cache_delete( 'current_blog_' . $blog->domain . $blog->path, 'site-options' );
367         wp_cache_delete( 'get_id_from_blogname_' . trim( $blog->path, '/' ), 'blog-details' );
368         wp_cache_delete( $domain_path_key, 'blog-id-cache' );
369 }
370
371 /**
372  * Retrieve option value for a given blog id based on name of option.
373  *
374  * If the option does not exist or does not have a value, then the return value
375  * will be false. This is useful to check whether you need to install an option
376  * and is commonly used during installation of plugin options and to test
377  * whether upgrading is required.
378  *
379  * If the option was serialized then it will be unserialized when it is returned.
380  *
381  * @since MU
382  *
383  * @param int $id A blog ID. Can be null to refer to the current blog.
384  * @param string $option Name of option to retrieve. Expected to not be SQL-escaped.
385  * @param mixed $default Optional. Default value to return if the option does not exist.
386  * @return mixed Value set for the option.
387  */
388 function get_blog_option( $id, $option, $default = false ) {
389         $id = (int) $id;
390
391         if ( empty( $id ) )
392                 $id = get_current_blog_id();
393
394         if ( get_current_blog_id() == $id )
395                 return get_option( $option, $default );
396
397         switch_to_blog( $id );
398         $value = get_option( $option, $default );
399         restore_current_blog();
400
401         return apply_filters( 'blog_option_' . $option, $value, $id );
402 }
403
404 /**
405  * Add a new option for a given blog id.
406  *
407  * You do not need to serialize values. If the value needs to be serialized, then
408  * it will be serialized before it is inserted into the database. Remember,
409  * resources can not be serialized or added as an option.
410  *
411  * You can create options without values and then update the values later.
412  * Existing options will not be updated and checks are performed to ensure that you
413  * aren't adding a protected WordPress option. Care should be taken to not name
414  * options the same as the ones which are protected.
415  *
416  * @since MU
417  *
418  * @param int $id A blog ID. Can be null to refer to the current blog.
419  * @param string $option Name of option to add. Expected to not be SQL-escaped.
420  * @param mixed $value Optional. Option value, can be anything. Expected to not be SQL-escaped.
421  * @return bool False if option was not added and true if option was added.
422  */
423 function add_blog_option( $id, $option, $value ) {
424         $id = (int) $id;
425
426         if ( empty( $id ) )
427                 $id = get_current_blog_id();
428
429         if ( get_current_blog_id() == $id )
430                 return add_option( $option, $value );
431
432         switch_to_blog( $id );
433         $return = add_option( $option, $value );
434         restore_current_blog();
435
436         return $return;
437 }
438
439 /**
440  * Removes option by name for a given blog id. Prevents removal of protected WordPress options.
441  *
442  * @since MU
443  *
444  * @param int $id A blog ID. Can be null to refer to the current blog.
445  * @param string $option Name of option to remove. Expected to not be SQL-escaped.
446  * @return bool True, if option is successfully deleted. False on failure.
447  */
448 function delete_blog_option( $id, $option ) {
449         $id = (int) $id;
450
451         if ( empty( $id ) )
452                 $id = get_current_blog_id();
453
454         if ( get_current_blog_id() == $id )
455                 return delete_option( $option );
456
457         switch_to_blog( $id );
458         $return = delete_option( $option );
459         restore_current_blog();
460
461         return $return;
462 }
463
464 /**
465  * Update an option for a particular blog.
466  *
467  * @since MU
468  *
469  * @param int $id The blog id
470  * @param string $option The option key
471  * @param mixed $value The option value
472  * @return bool True on success, false on failure.
473  */
474 function update_blog_option( $id, $option, $value, $deprecated = null ) {
475         $id = (int) $id;
476
477         if ( null !== $deprecated  )
478                 _deprecated_argument( __FUNCTION__, '3.1' );
479
480         if ( get_current_blog_id() == $id )
481                 return update_option( $option, $value );
482
483         switch_to_blog( $id );
484         $return = update_option( $option, $value );
485         restore_current_blog();
486
487         refresh_blog_details( $id );
488
489         return $return;
490 }
491
492 /**
493  * Switch the current blog.
494  *
495  * This function is useful if you need to pull posts, or other information,
496  * from other blogs. You can switch back afterwards using restore_current_blog().
497  *
498  * Things that aren't switched:
499  *  - autoloaded options. See #14992
500  *  - plugins. See #14941
501  *
502  * @see restore_current_blog()
503  * @since MU
504  *
505  * @param int $new_blog The id of the blog you want to switch to. Default: current blog
506  * @param bool $deprecated Deprecated argument
507  * @return bool True on success, false if the validation failed
508  */
509 function switch_to_blog( $new_blog, $deprecated = null ) {
510         global $wpdb, $wp_roles;
511
512         if ( empty( $new_blog ) )
513                 $new_blog = $GLOBALS['blog_id'];
514
515         $GLOBALS['_wp_switched_stack'][] = $GLOBALS['blog_id'];
516
517         /* If we're switching to the same blog id that we're on,
518         * set the right vars, do the associated actions, but skip
519         * the extra unnecessary work */
520         if ( $new_blog == $GLOBALS['blog_id'] ) {
521                 do_action( 'switch_blog', $new_blog, $new_blog );
522                 $GLOBALS['switched'] = true;
523                 return true;
524         }
525
526         $wpdb->set_blog_id( $new_blog );
527         $GLOBALS['table_prefix'] = $wpdb->prefix;
528         $prev_blog_id = $GLOBALS['blog_id'];
529         $GLOBALS['blog_id'] = $new_blog;
530
531         if ( function_exists( 'wp_cache_switch_to_blog' ) ) {
532                 wp_cache_switch_to_blog( $new_blog );
533         } else {
534                 global $wp_object_cache;
535
536                 if ( is_object( $wp_object_cache ) && isset( $wp_object_cache->global_groups ) )
537                         $global_groups = $wp_object_cache->global_groups;
538                 else
539                         $global_groups = false;
540
541                 wp_cache_init();
542
543                 if ( function_exists( 'wp_cache_add_global_groups' ) ) {
544                         if ( is_array( $global_groups ) )
545                                 wp_cache_add_global_groups( $global_groups );
546                         else
547                                 wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'site-transient', 'site-options', 'site-lookup', 'blog-lookup', 'blog-details', 'rss', 'global-posts', ' blog-id-cache' ) );
548                         wp_cache_add_non_persistent_groups( array( 'comment', 'counts', 'plugins' ) );
549                 }
550         }
551
552         if ( did_action( 'init' ) ) {
553                 $wp_roles->reinit();
554                 $current_user = wp_get_current_user();
555                 $current_user->for_blog( $new_blog );
556         }
557
558         do_action( 'switch_blog', $new_blog, $prev_blog_id );
559         $GLOBALS['switched'] = true;
560
561         return true;
562 }
563
564 /**
565  * Restore the current blog, after calling switch_to_blog()
566  *
567  * @see switch_to_blog()
568  * @since MU
569  *
570  * @return bool True on success, false if we're already on the current blog
571  */
572 function restore_current_blog() {
573         global $wpdb, $wp_roles;
574
575         if ( empty( $GLOBALS['_wp_switched_stack'] ) )
576                 return false;
577
578         $blog = array_pop( $GLOBALS['_wp_switched_stack'] );
579
580         if ( $GLOBALS['blog_id'] == $blog ) {
581                 do_action( 'switch_blog', $blog, $blog );
582                 // If we still have items in the switched stack, consider ourselves still 'switched'
583                 $GLOBALS['switched'] = ! empty( $GLOBALS['_wp_switched_stack'] );
584                 return true;
585         }
586
587         $wpdb->set_blog_id( $blog );
588         $prev_blog_id = $GLOBALS['blog_id'];
589         $GLOBALS['blog_id'] = $blog;
590         $GLOBALS['table_prefix'] = $wpdb->prefix;
591
592         if ( function_exists( 'wp_cache_switch_to_blog' ) ) {
593                 wp_cache_switch_to_blog( $blog );
594         } else {
595                 global $wp_object_cache;
596
597                 if ( is_object( $wp_object_cache ) && isset( $wp_object_cache->global_groups ) )
598                         $global_groups = $wp_object_cache->global_groups;
599                 else
600                         $global_groups = false;
601
602                 wp_cache_init();
603
604                 if ( function_exists( 'wp_cache_add_global_groups' ) ) {
605                         if ( is_array( $global_groups ) )
606                                 wp_cache_add_global_groups( $global_groups );
607                         else
608                                 wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'site-transient', 'site-options', 'site-lookup', 'blog-lookup', 'blog-details', 'rss', 'global-posts', ' blog-id-cache' ) );
609                         wp_cache_add_non_persistent_groups( array( 'comment', 'counts', 'plugins' ) );
610                 }
611         }
612
613         if ( did_action( 'init' ) ) {
614                 $wp_roles->reinit();
615                 $current_user = wp_get_current_user();
616                 $current_user->for_blog( $blog );
617         }
618
619         do_action( 'switch_blog', $blog, $prev_blog_id );
620
621         // If we still have items in the switched stack, consider ourselves still 'switched'
622         $GLOBALS['switched'] = ! empty( $GLOBALS['_wp_switched_stack'] );
623
624         return true;
625 }
626
627 /**
628  * Determines if switch_to_blog() is in effect
629  *
630  * @since 3.5.0
631  *
632  * @return bool True if switched, false otherwise.
633  */
634 function ms_is_switched() {
635         return ! empty( $GLOBALS['_wp_switched_stack'] );
636 }
637
638 /**
639  * Check if a particular blog is archived.
640  *
641  * @since MU
642  *
643  * @param int $id The blog id
644  * @return string Whether the blog is archived or not
645  */
646 function is_archived( $id ) {
647         return get_blog_status($id, 'archived');
648 }
649
650 /**
651  * Update the 'archived' status of a particular blog.
652  *
653  * @since MU
654  *
655  * @param int $id The blog id
656  * @param string $archived The new status
657  * @return string $archived
658  */
659 function update_archived( $id, $archived ) {
660         update_blog_status($id, 'archived', $archived);
661         return $archived;
662 }
663
664 /**
665  * Update a blog details field.
666  *
667  * @since MU
668  *
669  * @param int $blog_id BLog ID
670  * @param string $pref A field name
671  * @param string $value Value for $pref
672  * @return string $value
673  */
674 function update_blog_status( $blog_id, $pref, $value, $deprecated = null ) {
675         global $wpdb;
676
677         if ( null !== $deprecated  )
678                 _deprecated_argument( __FUNCTION__, '3.1' );
679
680         if ( ! in_array( $pref, array( 'site_id', 'domain', 'path', 'registered', 'last_updated', 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id') ) )
681                 return $value;
682
683         $result = $wpdb->update( $wpdb->blogs, array($pref => $value, 'last_updated' => current_time('mysql', true)), array('blog_id' => $blog_id) );
684
685         if ( false === $result )
686                 return false;
687
688         refresh_blog_details( $blog_id );
689
690         if ( 'spam' == $pref )
691                 ( $value == 1 ) ? do_action( 'make_spam_blog', $blog_id ) :     do_action( 'make_ham_blog', $blog_id );
692         elseif ( 'mature' == $pref )
693                 ( $value == 1 ) ? do_action( 'mature_blog', $blog_id ) : do_action( 'unmature_blog', $blog_id );
694         elseif ( 'archived' == $pref )
695                 ( $value == 1 ) ? do_action( 'archive_blog', $blog_id ) : do_action( 'unarchive_blog', $blog_id );
696         elseif ( 'deleted' == $pref )
697                 ( $value == 1 ) ? do_action( 'make_delete_blog', $blog_id ) : do_action( 'make_undelete_blog', $blog_id );
698         elseif ( 'public' == $pref )
699                 do_action( 'update_blog_public', $blog_id, $value ); // Moved here from update_blog_public().
700
701         return $value;
702 }
703
704 /**
705  * Get a blog details field.
706  *
707  * @since MU
708  *
709  * @param int $id The blog id
710  * @param string $pref A field name
711  * @return bool $value
712  */
713 function get_blog_status( $id, $pref ) {
714         global $wpdb;
715
716         $details = get_blog_details( $id, false );
717         if ( $details )
718                 return $details->$pref;
719
720         return $wpdb->get_var( $wpdb->prepare("SELECT %s FROM {$wpdb->blogs} WHERE blog_id = %d", $pref, $id) );
721 }
722
723 /**
724  * Get a list of most recently updated blogs.
725  *
726  * @since MU
727  *
728  * @param mixed $deprecated Not used
729  * @param int $start The offset
730  * @param int $quantity The maximum number of blogs to retrieve. Default is 40.
731  * @return array The list of blogs
732  */
733 function get_last_updated( $deprecated = '', $start = 0, $quantity = 40 ) {
734         global $wpdb;
735
736         if ( ! empty( $deprecated ) )
737                 _deprecated_argument( __FUNCTION__, 'MU' ); // never used
738
739         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 );
740 }
741
742 /**
743  * Handler for updating the blog date when a post is published or an already published post is changed.
744  *
745  * @since 3.3.0
746  *
747  * @param string $new_status The new post status
748  * @param string $old_status The old post status
749  * @param object $post Post object
750  */
751 function _update_blog_date_on_post_publish( $new_status, $old_status, $post ) {
752         $post_type_obj = get_post_type_object( $post->post_type );
753         if ( ! $post_type_obj->public )
754                 return;
755
756         if ( 'publish' != $new_status && 'publish' != $old_status )
757                 return;
758
759         // Post was freshly published, published post was saved, or published post was unpublished.
760
761         wpmu_update_blogs_date();
762 }
763
764 /**
765  * Handler for updating the blog date when a published post is deleted.
766  *
767  * @since 3.4.0
768  *
769  * @param int $post_id Post ID
770  */
771 function _update_blog_date_on_post_delete( $post_id ) {
772         $post = get_post( $post_id );
773
774         $post_type_obj = get_post_type_object( $post->post_type );
775         if ( ! $post_type_obj->public )
776                 return;
777
778         if ( 'publish' != $post->post_status )
779                 return;
780
781         wpmu_update_blogs_date();
782 }
783