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