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