56 * @param string $blogname The (subdomain or directory) name |
59 * @param string $blogname The (subdomain or directory) name |
57 * @return string |
60 * @return string |
58 */ |
61 */ |
59 function get_blogaddress_by_name( $blogname ) { |
62 function get_blogaddress_by_name( $blogname ) { |
60 if ( is_subdomain_install() ) { |
63 if ( is_subdomain_install() ) { |
61 if ( $blogname == 'main' ) |
64 if ( 'main' === $blogname ) { |
62 $blogname = 'www'; |
65 $blogname = 'www'; |
|
66 } |
63 $url = rtrim( network_home_url(), '/' ); |
67 $url = rtrim( network_home_url(), '/' ); |
64 if ( !empty( $blogname ) ) |
68 if ( ! empty( $blogname ) ) { |
65 $url = preg_replace( '|^([^\.]+://)|', "\${1}" . $blogname . '.', $url ); |
69 $url = preg_replace( '|^([^\.]+://)|', '${1}' . $blogname . '.', $url ); |
|
70 } |
66 } else { |
71 } else { |
67 $url = network_home_url( $blogname ); |
72 $url = network_home_url( $blogname ); |
68 } |
73 } |
69 return esc_url( $url . '/' ); |
74 return esc_url( $url . '/' ); |
70 } |
75 } |
71 |
76 |
72 /** |
77 /** |
73 * Retrieves a sites ID given its (subdomain or directory) slug. |
78 * Retrieves a sites ID given its (subdomain or directory) slug. |
74 * |
79 * |
75 * @since MU (3.0.0) |
80 * @since MU (3.0.0) |
76 * @since 4.7.0 Converted to use get_sites(). |
81 * @since 4.7.0 Converted to use `get_sites()`. |
77 * |
82 * |
78 * @param string $slug A site's slug. |
83 * @param string $slug A site's slug. |
79 * @return int|null The site ID, or null if no site is found for the given slug. |
84 * @return int|null The site ID, or null if no site is found for the given slug. |
80 */ |
85 */ |
81 function get_id_from_blogname( $slug ) { |
86 function get_id_from_blogname( $slug ) { |
82 $current_network = get_network(); |
87 $current_network = get_network(); |
83 $slug = trim( $slug, '/' ); |
88 $slug = trim( $slug, '/' ); |
84 |
89 |
85 if ( is_subdomain_install() ) { |
90 if ( is_subdomain_install() ) { |
86 $domain = $slug . '.' . preg_replace( '|^www\.|', '', $current_network->domain ); |
91 $domain = $slug . '.' . preg_replace( '|^www\.|', '', $current_network->domain ); |
87 $path = $current_network->path; |
92 $path = $current_network->path; |
88 } else { |
93 } else { |
89 $domain = $current_network->domain; |
94 $domain = $current_network->domain; |
90 $path = $current_network->path . $slug . '/'; |
95 $path = $current_network->path . $slug . '/'; |
91 } |
96 } |
92 |
97 |
93 $site_ids = get_sites( array( |
98 $site_ids = get_sites( |
94 'number' => 1, |
99 array( |
95 'fields' => 'ids', |
100 'number' => 1, |
96 'domain' => $domain, |
101 'fields' => 'ids', |
97 'path' => $path, |
102 'domain' => $domain, |
98 ) ); |
103 'path' => $path, |
|
104 'update_site_meta_cache' => false, |
|
105 ) |
|
106 ); |
99 |
107 |
100 if ( empty( $site_ids ) ) { |
108 if ( empty( $site_ids ) ) { |
101 return null; |
109 return null; |
102 } |
110 } |
103 |
111 |
118 * @return WP_Site|false Blog details on success. False on failure. |
126 * @return WP_Site|false Blog details on success. False on failure. |
119 */ |
127 */ |
120 function get_blog_details( $fields = null, $get_all = true ) { |
128 function get_blog_details( $fields = null, $get_all = true ) { |
121 global $wpdb; |
129 global $wpdb; |
122 |
130 |
123 if ( is_array($fields ) ) { |
131 if ( is_array( $fields ) ) { |
124 if ( isset($fields['blog_id']) ) { |
132 if ( isset( $fields['blog_id'] ) ) { |
125 $blog_id = $fields['blog_id']; |
133 $blog_id = $fields['blog_id']; |
126 } elseif ( isset($fields['domain']) && isset($fields['path']) ) { |
134 } elseif ( isset( $fields['domain'] ) && isset( $fields['path'] ) ) { |
127 $key = md5( $fields['domain'] . $fields['path'] ); |
135 $key = md5( $fields['domain'] . $fields['path'] ); |
128 $blog = wp_cache_get($key, 'blog-lookup'); |
136 $blog = wp_cache_get( $key, 'blog-lookup' ); |
129 if ( false !== $blog ) |
137 if ( false !== $blog ) { |
130 return $blog; |
138 return $blog; |
|
139 } |
131 if ( substr( $fields['domain'], 0, 4 ) == 'www.' ) { |
140 if ( substr( $fields['domain'], 0, 4 ) == 'www.' ) { |
132 $nowww = substr( $fields['domain'], 4 ); |
141 $nowww = substr( $fields['domain'], 4 ); |
133 $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'] ) ); |
142 $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'] ) ); |
134 } else { |
143 } else { |
135 $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain = %s AND path = %s", $fields['domain'], $fields['path'] ) ); |
144 $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain = %s AND path = %s", $fields['domain'], $fields['path'] ) ); |
136 } |
145 } |
137 if ( $blog ) { |
146 if ( $blog ) { |
138 wp_cache_set($blog->blog_id . 'short', $blog, 'blog-details'); |
147 wp_cache_set( $blog->blog_id . 'short', $blog, 'blog-details' ); |
139 $blog_id = $blog->blog_id; |
148 $blog_id = $blog->blog_id; |
140 } else { |
149 } else { |
141 return false; |
150 return false; |
142 } |
151 } |
143 } elseif ( isset($fields['domain']) && is_subdomain_install() ) { |
152 } elseif ( isset( $fields['domain'] ) && is_subdomain_install() ) { |
144 $key = md5( $fields['domain'] ); |
153 $key = md5( $fields['domain'] ); |
145 $blog = wp_cache_get($key, 'blog-lookup'); |
154 $blog = wp_cache_get( $key, 'blog-lookup' ); |
146 if ( false !== $blog ) |
155 if ( false !== $blog ) { |
147 return $blog; |
156 return $blog; |
|
157 } |
148 if ( substr( $fields['domain'], 0, 4 ) == 'www.' ) { |
158 if ( substr( $fields['domain'], 0, 4 ) == 'www.' ) { |
149 $nowww = substr( $fields['domain'], 4 ); |
159 $nowww = substr( $fields['domain'], 4 ); |
150 $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain IN (%s,%s) ORDER BY CHAR_LENGTH(domain) DESC", $nowww, $fields['domain'] ) ); |
160 $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain IN (%s,%s) ORDER BY CHAR_LENGTH(domain) DESC", $nowww, $fields['domain'] ) ); |
151 } else { |
161 } else { |
152 $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain = %s", $fields['domain'] ) ); |
162 $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain = %s", $fields['domain'] ) ); |
153 } |
163 } |
154 if ( $blog ) { |
164 if ( $blog ) { |
155 wp_cache_set($blog->blog_id . 'short', $blog, 'blog-details'); |
165 wp_cache_set( $blog->blog_id . 'short', $blog, 'blog-details' ); |
156 $blog_id = $blog->blog_id; |
166 $blog_id = $blog->blog_id; |
157 } else { |
167 } else { |
158 return false; |
168 return false; |
159 } |
169 } |
160 } else { |
170 } else { |
161 return false; |
171 return false; |
162 } |
172 } |
163 } else { |
173 } else { |
164 if ( ! $fields ) |
174 if ( ! $fields ) { |
165 $blog_id = get_current_blog_id(); |
175 $blog_id = get_current_blog_id(); |
166 elseif ( ! is_numeric( $fields ) ) |
176 } elseif ( ! is_numeric( $fields ) ) { |
167 $blog_id = get_id_from_blogname( $fields ); |
177 $blog_id = get_id_from_blogname( $fields ); |
168 else |
178 } else { |
169 $blog_id = $fields; |
179 $blog_id = $fields; |
|
180 } |
170 } |
181 } |
171 |
182 |
172 $blog_id = (int) $blog_id; |
183 $blog_id = (int) $blog_id; |
173 |
184 |
174 $all = $get_all == true ? '' : 'short'; |
185 $all = $get_all ? '' : 'short'; |
175 $details = wp_cache_get( $blog_id . $all, 'blog-details' ); |
186 $details = wp_cache_get( $blog_id . $all, 'blog-details' ); |
176 |
187 |
177 if ( $details ) { |
188 if ( $details ) { |
178 if ( ! is_object( $details ) ) { |
189 if ( ! is_object( $details ) ) { |
179 if ( $details == -1 ) { |
190 if ( -1 == $details ) { |
180 return false; |
191 return false; |
181 } else { |
192 } else { |
182 // Clear old pre-serialized objects. Cache clients do better with that. |
193 // Clear old pre-serialized objects. Cache clients do better with that. |
183 wp_cache_delete( $blog_id . $all, 'blog-details' ); |
194 wp_cache_delete( $blog_id . $all, 'blog-details' ); |
184 unset($details); |
195 unset( $details ); |
185 } |
196 } |
186 } else { |
197 } else { |
187 return $details; |
198 return $details; |
188 } |
199 } |
189 } |
200 } |
280 * @return bool True if update succeeds, false otherwise. |
291 * @return bool True if update succeeds, false otherwise. |
281 */ |
292 */ |
282 function update_blog_details( $blog_id, $details = array() ) { |
293 function update_blog_details( $blog_id, $details = array() ) { |
283 global $wpdb; |
294 global $wpdb; |
284 |
295 |
285 if ( empty($details) ) |
296 if ( empty( $details ) ) { |
286 return false; |
297 return false; |
287 |
298 } |
288 if ( is_object($details) ) |
299 |
289 $details = get_object_vars($details); |
300 if ( is_object( $details ) ) { |
290 |
301 $details = get_object_vars( $details ); |
291 $current_details = get_site( $blog_id ); |
302 } |
292 if ( empty($current_details) ) |
303 |
|
304 $site = wp_update_site( $blog_id, $details ); |
|
305 |
|
306 if ( is_wp_error( $site ) ) { |
293 return false; |
307 return false; |
294 |
308 } |
295 $current_details = get_object_vars($current_details); |
|
296 |
|
297 $details = array_merge($current_details, $details); |
|
298 $details['last_updated'] = current_time('mysql', true); |
|
299 |
|
300 $update_details = array(); |
|
301 $fields = array( 'site_id', 'domain', 'path', 'registered', 'last_updated', 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id'); |
|
302 foreach ( array_intersect( array_keys( $details ), $fields ) as $field ) { |
|
303 if ( 'path' === $field ) { |
|
304 $details[ $field ] = trailingslashit( '/' . trim( $details[ $field ], '/' ) ); |
|
305 } |
|
306 |
|
307 $update_details[ $field ] = $details[ $field ]; |
|
308 } |
|
309 |
|
310 $result = $wpdb->update( $wpdb->blogs, $update_details, array('blog_id' => $blog_id) ); |
|
311 |
|
312 if ( false === $result ) |
|
313 return false; |
|
314 |
|
315 // If spam status changed, issue actions. |
|
316 if ( $details['spam'] != $current_details['spam'] ) { |
|
317 if ( $details['spam'] == 1 ) { |
|
318 /** |
|
319 * Fires when the 'spam' status is added to a blog. |
|
320 * |
|
321 * @since MU (3.0.0) |
|
322 * |
|
323 * @param int $blog_id Blog ID. |
|
324 */ |
|
325 do_action( 'make_spam_blog', $blog_id ); |
|
326 } else { |
|
327 /** |
|
328 * Fires when the 'spam' status is removed from a blog. |
|
329 * |
|
330 * @since MU (3.0.0) |
|
331 * |
|
332 * @param int $blog_id Blog ID. |
|
333 */ |
|
334 do_action( 'make_ham_blog', $blog_id ); |
|
335 } |
|
336 } |
|
337 |
|
338 // If mature status changed, issue actions. |
|
339 if ( $details['mature'] != $current_details['mature'] ) { |
|
340 if ( $details['mature'] == 1 ) { |
|
341 /** |
|
342 * Fires when the 'mature' status is added to a blog. |
|
343 * |
|
344 * @since 3.1.0 |
|
345 * |
|
346 * @param int $blog_id Blog ID. |
|
347 */ |
|
348 do_action( 'mature_blog', $blog_id ); |
|
349 } else { |
|
350 /** |
|
351 * Fires when the 'mature' status is removed from a blog. |
|
352 * |
|
353 * @since 3.1.0 |
|
354 * |
|
355 * @param int $blog_id Blog ID. |
|
356 */ |
|
357 do_action( 'unmature_blog', $blog_id ); |
|
358 } |
|
359 } |
|
360 |
|
361 // If archived status changed, issue actions. |
|
362 if ( $details['archived'] != $current_details['archived'] ) { |
|
363 if ( $details['archived'] == 1 ) { |
|
364 /** |
|
365 * Fires when the 'archived' status is added to a blog. |
|
366 * |
|
367 * @since MU (3.0.0) |
|
368 * |
|
369 * @param int $blog_id Blog ID. |
|
370 */ |
|
371 do_action( 'archive_blog', $blog_id ); |
|
372 } else { |
|
373 /** |
|
374 * Fires when the 'archived' status is removed from a blog. |
|
375 * |
|
376 * @since MU (3.0.0) |
|
377 * |
|
378 * @param int $blog_id Blog ID. |
|
379 */ |
|
380 do_action( 'unarchive_blog', $blog_id ); |
|
381 } |
|
382 } |
|
383 |
|
384 // If deleted status changed, issue actions. |
|
385 if ( $details['deleted'] != $current_details['deleted'] ) { |
|
386 if ( $details['deleted'] == 1 ) { |
|
387 /** |
|
388 * Fires when the 'deleted' status is added to a blog. |
|
389 * |
|
390 * @since 3.5.0 |
|
391 * |
|
392 * @param int $blog_id Blog ID. |
|
393 */ |
|
394 do_action( 'make_delete_blog', $blog_id ); |
|
395 } else { |
|
396 /** |
|
397 * Fires when the 'deleted' status is removed from a blog. |
|
398 * |
|
399 * @since 3.5.0 |
|
400 * |
|
401 * @param int $blog_id Blog ID. |
|
402 */ |
|
403 do_action( 'make_undelete_blog', $blog_id ); |
|
404 } |
|
405 } |
|
406 |
|
407 if ( isset( $details['public'] ) ) { |
|
408 switch_to_blog( $blog_id ); |
|
409 update_option( 'blog_public', $details['public'] ); |
|
410 restore_current_blog(); |
|
411 } |
|
412 |
|
413 clean_blog_cache( $blog_id ); |
|
414 |
309 |
415 return true; |
310 return true; |
416 } |
|
417 |
|
418 /** |
|
419 * Clean the blog cache |
|
420 * |
|
421 * @since 3.5.0 |
|
422 * |
|
423 * @global bool $_wp_suspend_cache_invalidation |
|
424 * |
|
425 * @param WP_Site|int $blog The site object or ID to be cleared from cache. |
|
426 */ |
|
427 function clean_blog_cache( $blog ) { |
|
428 global $_wp_suspend_cache_invalidation; |
|
429 |
|
430 if ( ! empty( $_wp_suspend_cache_invalidation ) ) { |
|
431 return; |
|
432 } |
|
433 |
|
434 if ( empty( $blog ) ) { |
|
435 return; |
|
436 } |
|
437 |
|
438 $blog_id = $blog; |
|
439 $blog = get_site( $blog_id ); |
|
440 if ( ! $blog ) { |
|
441 if ( ! is_numeric( $blog_id ) ) { |
|
442 return; |
|
443 } |
|
444 |
|
445 // Make sure a WP_Site object exists even when the site has been deleted. |
|
446 $blog = new WP_Site( (object) array( |
|
447 'blog_id' => $blog_id, |
|
448 'domain' => null, |
|
449 'path' => null, |
|
450 ) ); |
|
451 } |
|
452 |
|
453 $blog_id = $blog->blog_id; |
|
454 $domain_path_key = md5( $blog->domain . $blog->path ); |
|
455 |
|
456 wp_cache_delete( $blog_id, 'sites' ); |
|
457 wp_cache_delete( $blog_id, 'site-details' ); |
|
458 wp_cache_delete( $blog_id, 'blog-details' ); |
|
459 wp_cache_delete( $blog_id . 'short' , 'blog-details' ); |
|
460 wp_cache_delete( $domain_path_key, 'blog-lookup' ); |
|
461 wp_cache_delete( $domain_path_key, 'blog-id-cache' ); |
|
462 wp_cache_delete( 'current_blog_' . $blog->domain, 'site-options' ); |
|
463 wp_cache_delete( 'current_blog_' . $blog->domain . $blog->path, 'site-options' ); |
|
464 |
|
465 /** |
|
466 * Fires immediately after a site has been removed from the object cache. |
|
467 * |
|
468 * @since 4.6.0 |
|
469 * |
|
470 * @param int $id Blog ID. |
|
471 * @param WP_Site $blog Site object. |
|
472 * @param string $domain_path_key md5 hash of domain and path. |
|
473 */ |
|
474 do_action( 'clean_site_cache', $blog_id, $blog, $domain_path_key ); |
|
475 |
|
476 wp_cache_set( 'last_changed', microtime(), 'sites' ); |
|
477 |
|
478 /** |
|
479 * Fires after the blog details cache is cleared. |
|
480 * |
|
481 * @since 3.4.0 |
|
482 * @deprecated 4.9.0 Use clean_site_cache |
|
483 * |
|
484 * @param int $blog_id Blog ID. |
|
485 */ |
|
486 do_action_deprecated( 'refresh_blog_details', array( $blog_id ), '4.9.0', 'clean_site_cache' ); |
|
487 } |
311 } |
488 |
312 |
489 /** |
313 /** |
490 * Cleans the site details cache for a site. |
314 * Cleans the site details cache for a site. |
491 * |
315 * |
502 wp_cache_delete( $site_id, 'site-details' ); |
326 wp_cache_delete( $site_id, 'site-details' ); |
503 wp_cache_delete( $site_id, 'blog-details' ); |
327 wp_cache_delete( $site_id, 'blog-details' ); |
504 } |
328 } |
505 |
329 |
506 /** |
330 /** |
507 * Retrieves site data given a site ID or site object. |
|
508 * |
|
509 * Site data will be cached and returned after being passed through a filter. |
|
510 * If the provided site is empty, the current site global will be used. |
|
511 * |
|
512 * @since 4.6.0 |
|
513 * |
|
514 * @param WP_Site|int|null $site Optional. Site to retrieve. Default is the current site. |
|
515 * @return WP_Site|null The site object or null if not found. |
|
516 */ |
|
517 function get_site( $site = null ) { |
|
518 if ( empty( $site ) ) { |
|
519 $site = get_current_blog_id(); |
|
520 } |
|
521 |
|
522 if ( $site instanceof WP_Site ) { |
|
523 $_site = $site; |
|
524 } elseif ( is_object( $site ) ) { |
|
525 $_site = new WP_Site( $site ); |
|
526 } else { |
|
527 $_site = WP_Site::get_instance( $site ); |
|
528 } |
|
529 |
|
530 if ( ! $_site ) { |
|
531 return null; |
|
532 } |
|
533 |
|
534 /** |
|
535 * Fires after a site is retrieved. |
|
536 * |
|
537 * @since 4.6.0 |
|
538 * |
|
539 * @param WP_Site $_site Site data. |
|
540 */ |
|
541 $_site = apply_filters( 'get_site', $_site ); |
|
542 |
|
543 return $_site; |
|
544 } |
|
545 |
|
546 /** |
|
547 * Adds any sites from the given ids to the cache that do not already exist in cache. |
|
548 * |
|
549 * @since 4.6.0 |
|
550 * @access private |
|
551 * |
|
552 * @see update_site_cache() |
|
553 * @global wpdb $wpdb WordPress database abstraction object. |
|
554 * |
|
555 * @param array $ids ID list. |
|
556 */ |
|
557 function _prime_site_caches( $ids ) { |
|
558 global $wpdb; |
|
559 |
|
560 $non_cached_ids = _get_non_cached_ids( $ids, 'sites' ); |
|
561 if ( ! empty( $non_cached_ids ) ) { |
|
562 $fresh_sites = $wpdb->get_results( sprintf( "SELECT * FROM $wpdb->blogs WHERE blog_id IN (%s)", join( ",", array_map( 'intval', $non_cached_ids ) ) ) ); |
|
563 |
|
564 update_site_cache( $fresh_sites ); |
|
565 } |
|
566 } |
|
567 |
|
568 /** |
|
569 * Updates sites in cache. |
|
570 * |
|
571 * @since 4.6.0 |
|
572 * |
|
573 * @param array $sites Array of site objects. |
|
574 */ |
|
575 function update_site_cache( $sites ) { |
|
576 if ( ! $sites ) { |
|
577 return; |
|
578 } |
|
579 |
|
580 foreach ( $sites as $site ) { |
|
581 wp_cache_add( $site->blog_id, $site, 'sites' ); |
|
582 wp_cache_add( $site->blog_id . 'short', $site, 'blog-details' ); |
|
583 } |
|
584 } |
|
585 |
|
586 /** |
|
587 * Retrieves a list of sites matching requested arguments. |
|
588 * |
|
589 * @since 4.6.0 |
|
590 * @since 4.8.0 Introduced the 'lang_id', 'lang__in', and 'lang__not_in' parameters. |
|
591 * |
|
592 * @see WP_Site_Query::parse_query() |
|
593 * |
|
594 * @param string|array $args { |
|
595 * Optional. Array or query string of site query parameters. Default empty. |
|
596 * |
|
597 * @type array $site__in Array of site IDs to include. Default empty. |
|
598 * @type array $site__not_in Array of site IDs to exclude. Default empty. |
|
599 * @type bool $count Whether to return a site count (true) or array of site objects. |
|
600 * Default false. |
|
601 * @type array $date_query Date query clauses to limit sites by. See WP_Date_Query. |
|
602 * Default null. |
|
603 * @type string $fields Site fields to return. Accepts 'ids' (returns an array of site IDs) |
|
604 * or empty (returns an array of complete site objects). Default empty. |
|
605 * @type int $ID A site ID to only return that site. Default empty. |
|
606 * @type int $number Maximum number of sites to retrieve. Default 100. |
|
607 * @type int $offset Number of sites to offset the query. Used to build LIMIT clause. |
|
608 * Default 0. |
|
609 * @type bool $no_found_rows Whether to disable the `SQL_CALC_FOUND_ROWS` query. Default true. |
|
610 * @type string|array $orderby Site status or array of statuses. Accepts 'id', 'domain', 'path', |
|
611 * 'network_id', 'last_updated', 'registered', 'domain_length', |
|
612 * 'path_length', 'site__in' and 'network__in'. Also accepts false, |
|
613 * an empty array, or 'none' to disable `ORDER BY` clause. |
|
614 * Default 'id'. |
|
615 * @type string $order How to order retrieved sites. Accepts 'ASC', 'DESC'. Default 'ASC'. |
|
616 * @type int $network_id Limit results to those affiliated with a given network ID. If 0, |
|
617 * include all networks. Default 0. |
|
618 * @type array $network__in Array of network IDs to include affiliated sites for. Default empty. |
|
619 * @type array $network__not_in Array of network IDs to exclude affiliated sites for. Default empty. |
|
620 * @type string $domain Limit results to those affiliated with a given domain. Default empty. |
|
621 * @type array $domain__in Array of domains to include affiliated sites for. Default empty. |
|
622 * @type array $domain__not_in Array of domains to exclude affiliated sites for. Default empty. |
|
623 * @type string $path Limit results to those affiliated with a given path. Default empty. |
|
624 * @type array $path__in Array of paths to include affiliated sites for. Default empty. |
|
625 * @type array $path__not_in Array of paths to exclude affiliated sites for. Default empty. |
|
626 * @type int $public Limit results to public sites. Accepts '1' or '0'. Default empty. |
|
627 * @type int $archived Limit results to archived sites. Accepts '1' or '0'. Default empty. |
|
628 * @type int $mature Limit results to mature sites. Accepts '1' or '0'. Default empty. |
|
629 * @type int $spam Limit results to spam sites. Accepts '1' or '0'. Default empty. |
|
630 * @type int $deleted Limit results to deleted sites. Accepts '1' or '0'. Default empty. |
|
631 * @type int $lang_id Limit results to a language ID. Default empty. |
|
632 * @type array $lang__in Array of language IDs to include affiliated sites for. Default empty. |
|
633 * @type array $lang__not_in Array of language IDs to exclude affiliated sites for. Default empty. |
|
634 * @type string $search Search term(s) to retrieve matching sites for. Default empty. |
|
635 * @type array $search_columns Array of column names to be searched. Accepts 'domain' and 'path'. |
|
636 * Default empty array. |
|
637 * @type bool $update_site_cache Whether to prime the cache for found sites. Default true. |
|
638 * } |
|
639 * @return array|int List of WP_Site objects, a list of site ids when 'fields' is set to 'ids', |
|
640 * or the number of sites when 'count' is passed as a query var. |
|
641 */ |
|
642 function get_sites( $args = array() ) { |
|
643 $query = new WP_Site_Query(); |
|
644 |
|
645 return $query->query( $args ); |
|
646 } |
|
647 |
|
648 /** |
|
649 * Retrieve option value for a given blog id based on name of option. |
331 * Retrieve option value for a given blog id based on name of option. |
650 * |
332 * |
651 * If the option does not exist or does not have a value, then the return value |
333 * If the option does not exist or does not have a value, then the return value |
652 * will be false. This is useful to check whether you need to install an option |
334 * will be false. This is useful to check whether you need to install an option |
653 * and is commonly used during installation of plugin options and to test |
335 * and is commonly used during installation of plugin options and to test |
846 |
536 |
847 if ( function_exists( 'wp_cache_add_global_groups' ) ) { |
537 if ( function_exists( 'wp_cache_add_global_groups' ) ) { |
848 if ( is_array( $global_groups ) ) { |
538 if ( is_array( $global_groups ) ) { |
849 wp_cache_add_global_groups( $global_groups ); |
539 wp_cache_add_global_groups( $global_groups ); |
850 } else { |
540 } else { |
851 wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'useremail', 'userslugs', 'site-transient', 'site-options', 'blog-lookup', 'blog-details', 'rss', 'global-posts', 'blog-id-cache', 'networks', 'sites', 'site-details' ) ); |
541 wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'useremail', 'userslugs', 'site-transient', 'site-options', 'blog-lookup', 'blog-details', 'rss', 'global-posts', 'blog-id-cache', 'networks', 'sites', 'site-details', 'blog_meta' ) ); |
852 } |
542 } |
853 wp_cache_add_non_persistent_groups( array( 'counts', 'plugins' ) ); |
543 wp_cache_add_non_persistent_groups( array( 'counts', 'plugins' ) ); |
854 } |
544 } |
855 } |
545 } |
856 |
546 |
914 |
604 |
915 if ( function_exists( 'wp_cache_add_global_groups' ) ) { |
605 if ( function_exists( 'wp_cache_add_global_groups' ) ) { |
916 if ( is_array( $global_groups ) ) { |
606 if ( is_array( $global_groups ) ) { |
917 wp_cache_add_global_groups( $global_groups ); |
607 wp_cache_add_global_groups( $global_groups ); |
918 } else { |
608 } else { |
919 wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'useremail', 'userslugs', 'site-transient', 'site-options', 'blog-lookup', 'blog-details', 'rss', 'global-posts', 'blog-id-cache', 'networks', 'sites', 'site-details' ) ); |
609 wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'useremail', 'userslugs', 'site-transient', 'site-options', 'blog-lookup', 'blog-details', 'rss', 'global-posts', 'blog-id-cache', 'networks', 'sites', 'site-details', 'blog_meta' ) ); |
920 } |
610 } |
921 wp_cache_add_non_persistent_groups( array( 'counts', 'plugins' ) ); |
611 wp_cache_add_non_persistent_groups( array( 'counts', 'plugins' ) ); |
922 } |
612 } |
923 } |
613 } |
924 |
614 |
1005 * @return string|false $value |
696 * @return string|false $value |
1006 */ |
697 */ |
1007 function update_blog_status( $blog_id, $pref, $value, $deprecated = null ) { |
698 function update_blog_status( $blog_id, $pref, $value, $deprecated = null ) { |
1008 global $wpdb; |
699 global $wpdb; |
1009 |
700 |
1010 if ( null !== $deprecated ) |
701 if ( null !== $deprecated ) { |
1011 _deprecated_argument( __FUNCTION__, '3.1.0' ); |
702 _deprecated_argument( __FUNCTION__, '3.1.0' ); |
1012 |
703 } |
1013 if ( ! in_array( $pref, array( 'site_id', 'domain', 'path', 'registered', 'last_updated', 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id') ) ) |
704 |
|
705 if ( ! in_array( $pref, array( 'site_id', 'domain', 'path', 'registered', 'last_updated', 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id' ) ) ) { |
1014 return $value; |
706 return $value; |
1015 |
707 } |
1016 $result = $wpdb->update( $wpdb->blogs, array($pref => $value, 'last_updated' => current_time('mysql', true)), array('blog_id' => $blog_id) ); |
708 |
1017 |
709 $result = wp_update_site( |
1018 if ( false === $result ) |
710 $blog_id, |
|
711 array( |
|
712 $pref => $value, |
|
713 ) |
|
714 ); |
|
715 |
|
716 if ( is_wp_error( $result ) ) { |
1019 return false; |
717 return false; |
1020 |
|
1021 clean_blog_cache( $blog_id ); |
|
1022 |
|
1023 if ( 'spam' == $pref ) { |
|
1024 if ( $value == 1 ) { |
|
1025 /** This filter is documented in wp-includes/ms-blogs.php */ |
|
1026 do_action( 'make_spam_blog', $blog_id ); |
|
1027 } else { |
|
1028 /** This filter is documented in wp-includes/ms-blogs.php */ |
|
1029 do_action( 'make_ham_blog', $blog_id ); |
|
1030 } |
|
1031 } elseif ( 'mature' == $pref ) { |
|
1032 if ( $value == 1 ) { |
|
1033 /** This filter is documented in wp-includes/ms-blogs.php */ |
|
1034 do_action( 'mature_blog', $blog_id ); |
|
1035 } else { |
|
1036 /** This filter is documented in wp-includes/ms-blogs.php */ |
|
1037 do_action( 'unmature_blog', $blog_id ); |
|
1038 } |
|
1039 } elseif ( 'archived' == $pref ) { |
|
1040 if ( $value == 1 ) { |
|
1041 /** This filter is documented in wp-includes/ms-blogs.php */ |
|
1042 do_action( 'archive_blog', $blog_id ); |
|
1043 } else { |
|
1044 /** This filter is documented in wp-includes/ms-blogs.php */ |
|
1045 do_action( 'unarchive_blog', $blog_id ); |
|
1046 } |
|
1047 } elseif ( 'deleted' == $pref ) { |
|
1048 if ( $value == 1 ) { |
|
1049 /** This filter is documented in wp-includes/ms-blogs.php */ |
|
1050 do_action( 'make_delete_blog', $blog_id ); |
|
1051 } else { |
|
1052 /** This filter is documented in wp-includes/ms-blogs.php */ |
|
1053 do_action( 'make_undelete_blog', $blog_id ); |
|
1054 } |
|
1055 } elseif ( 'public' == $pref ) { |
|
1056 /** |
|
1057 * Fires after the current blog's 'public' setting is updated. |
|
1058 * |
|
1059 * @since MU (3.0.0) |
|
1060 * |
|
1061 * @param int $blog_id Blog ID. |
|
1062 * @param string $value The value of blog status. |
|
1063 */ |
|
1064 do_action( 'update_blog_public', $blog_id, $value ); // Moved here from update_blog_public(). |
|
1065 } |
718 } |
1066 |
719 |
1067 return $value; |
720 return $value; |
1068 } |
721 } |
1069 |
722 |
1101 * @return array The list of blogs |
755 * @return array The list of blogs |
1102 */ |
756 */ |
1103 function get_last_updated( $deprecated = '', $start = 0, $quantity = 40 ) { |
757 function get_last_updated( $deprecated = '', $start = 0, $quantity = 40 ) { |
1104 global $wpdb; |
758 global $wpdb; |
1105 |
759 |
1106 if ( ! empty( $deprecated ) ) |
760 if ( ! empty( $deprecated ) ) { |
1107 _deprecated_argument( __FUNCTION__, 'MU' ); // never used |
761 _deprecated_argument( __FUNCTION__, 'MU' ); // never used |
|
762 } |
1108 |
763 |
1109 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 ); |
764 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 ); |
1110 } |
|
1111 |
|
1112 /** |
|
1113 * Retrieves a list of networks. |
|
1114 * |
|
1115 * @since 4.6.0 |
|
1116 * |
|
1117 * @param string|array $args Optional. Array or string of arguments. See WP_Network_Query::parse_query() |
|
1118 * for information on accepted arguments. Default empty array. |
|
1119 * @return array|int List of WP_Network objects, a list of network ids when 'fields' is set to 'ids', |
|
1120 * or the number of networks when 'count' is passed as a query var. |
|
1121 */ |
|
1122 function get_networks( $args = array() ) { |
|
1123 $query = new WP_Network_Query(); |
|
1124 |
|
1125 return $query->query( $args ); |
|
1126 } |
|
1127 |
|
1128 /** |
|
1129 * Retrieves network data given a network ID or network object. |
|
1130 * |
|
1131 * Network data will be cached and returned after being passed through a filter. |
|
1132 * If the provided network is empty, the current network global will be used. |
|
1133 * |
|
1134 * @since 4.6.0 |
|
1135 * |
|
1136 * @global WP_Network $current_site |
|
1137 * |
|
1138 * @param WP_Network|int|null $network Optional. Network to retrieve. Default is the current network. |
|
1139 * @return WP_Network|null The network object or null if not found. |
|
1140 */ |
|
1141 function get_network( $network = null ) { |
|
1142 global $current_site; |
|
1143 if ( empty( $network ) && isset( $current_site ) ) { |
|
1144 $network = $current_site; |
|
1145 } |
|
1146 |
|
1147 if ( $network instanceof WP_Network ) { |
|
1148 $_network = $network; |
|
1149 } elseif ( is_object( $network ) ) { |
|
1150 $_network = new WP_Network( $network ); |
|
1151 } else { |
|
1152 $_network = WP_Network::get_instance( $network ); |
|
1153 } |
|
1154 |
|
1155 if ( ! $_network ) { |
|
1156 return null; |
|
1157 } |
|
1158 |
|
1159 /** |
|
1160 * Fires after a network is retrieved. |
|
1161 * |
|
1162 * @since 4.6.0 |
|
1163 * |
|
1164 * @param WP_Network $_network Network data. |
|
1165 */ |
|
1166 $_network = apply_filters( 'get_network', $_network ); |
|
1167 |
|
1168 return $_network; |
|
1169 } |
|
1170 |
|
1171 /** |
|
1172 * Removes a network from the object cache. |
|
1173 * |
|
1174 * @since 4.6.0 |
|
1175 * |
|
1176 * @global bool $_wp_suspend_cache_invalidation |
|
1177 * |
|
1178 * @param int|array $ids Network ID or an array of network IDs to remove from cache. |
|
1179 */ |
|
1180 function clean_network_cache( $ids ) { |
|
1181 global $_wp_suspend_cache_invalidation; |
|
1182 |
|
1183 if ( ! empty( $_wp_suspend_cache_invalidation ) ) { |
|
1184 return; |
|
1185 } |
|
1186 |
|
1187 foreach ( (array) $ids as $id ) { |
|
1188 wp_cache_delete( $id, 'networks' ); |
|
1189 |
|
1190 /** |
|
1191 * Fires immediately after a network has been removed from the object cache. |
|
1192 * |
|
1193 * @since 4.6.0 |
|
1194 * |
|
1195 * @param int $id Network ID. |
|
1196 */ |
|
1197 do_action( 'clean_network_cache', $id ); |
|
1198 } |
|
1199 |
|
1200 wp_cache_set( 'last_changed', microtime(), 'networks' ); |
|
1201 } |
|
1202 |
|
1203 /** |
|
1204 * Updates the network cache of given networks. |
|
1205 * |
|
1206 * Will add the networks in $networks to the cache. If network ID already exists |
|
1207 * in the network cache then it will not be updated. The network is added to the |
|
1208 * cache using the network group with the key using the ID of the networks. |
|
1209 * |
|
1210 * @since 4.6.0 |
|
1211 * |
|
1212 * @param array $networks Array of network row objects. |
|
1213 */ |
|
1214 function update_network_cache( $networks ) { |
|
1215 foreach ( (array) $networks as $network ) { |
|
1216 wp_cache_add( $network->id, $network, 'networks' ); |
|
1217 } |
|
1218 } |
|
1219 |
|
1220 /** |
|
1221 * Adds any networks from the given IDs to the cache that do not already exist in cache. |
|
1222 * |
|
1223 * @since 4.6.0 |
|
1224 * @access private |
|
1225 * |
|
1226 * @see update_network_cache() |
|
1227 * @global wpdb $wpdb WordPress database abstraction object. |
|
1228 * |
|
1229 * @param array $network_ids Array of network IDs. |
|
1230 */ |
|
1231 function _prime_network_caches( $network_ids ) { |
|
1232 global $wpdb; |
|
1233 |
|
1234 $non_cached_ids = _get_non_cached_ids( $network_ids, 'networks' ); |
|
1235 if ( !empty( $non_cached_ids ) ) { |
|
1236 $fresh_networks = $wpdb->get_results( sprintf( "SELECT $wpdb->site.* FROM $wpdb->site WHERE id IN (%s)", join( ",", array_map( 'intval', $non_cached_ids ) ) ) ); |
|
1237 |
|
1238 update_network_cache( $fresh_networks ); |
|
1239 } |
|
1240 } |
765 } |
1241 |
766 |
1242 /** |
767 /** |
1243 * Handler for updating the site's last updated date when a post is published or |
768 * Handler for updating the site's last updated date when a post is published or |
1244 * an already published post is changed. |
769 * an already published post is changed. |