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