author | ymh <ymh.work@gmail.com> |
Thu, 29 Sep 2022 08:06:27 +0200 | |
changeset 20 | 7b1b88e27a20 |
parent 18 | be944660c56a |
child 21 | 48c4eec2b7e6 |
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 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
15 |
* Update 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 |
/** |
|
16 | 34 |
* Get a full blog URL, given a blog 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 |
* |
16 | 38 |
* @param int $blog_id Blog ID. |
5 | 39 |
* @return string Full URL of the blog 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 |
/** |
|
55 |
* Get a full blog URL, given a blog name. |
|
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 |
* |
59 |
* @param string $blogname The (subdomain or directory) name |
|
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 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
78 |
* Retrieves a sites 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 |
/** |
|
116 |
* Retrieve the details for a blog from the blogs table and blog options. |
|
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. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
123 |
* If not specified the current blog ID is used. |
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 |
} |
16 | 140 |
if ( 'www.' === substr( $fields['domain'], 0, 4 ) ) { |
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 |
} |
16 | 158 |
if ( 'www.' === substr( $fields['domain'], 0, 4 ) ) { |
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 ) ) { |
|
9 | 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 ) ) { |
|
9 | 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 |
/** |
|
276 |
* Clear the blog details cache. |
|
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 |
/** |
|
16 | 292 |
* Update the details for a blog. Updates 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 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
296 |
* @global wpdb $wpdb WordPress database abstraction object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
297 |
* |
16 | 298 |
* @param int $blog_id Blog ID. |
0 | 299 |
* @param array $details Array of details keyed by blogs table field names. |
300 |
* @return bool True if update succeeds, false otherwise. |
|
301 |
*/ |
|
302 |
function update_blog_details( $blog_id, $details = array() ) { |
|
303 |
global $wpdb; |
|
304 |
||
9 | 305 |
if ( empty( $details ) ) { |
0 | 306 |
return false; |
5 | 307 |
} |
0 | 308 |
|
9 | 309 |
if ( is_object( $details ) ) { |
310 |
$details = get_object_vars( $details ); |
|
0 | 311 |
} |
312 |
||
9 | 313 |
$site = wp_update_site( $blog_id, $details ); |
0 | 314 |
|
9 | 315 |
if ( is_wp_error( $site ) ) { |
316 |
return false; |
|
7
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 |
|
9 | 319 |
return true; |
7
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 |
|
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 |
* Cleans the site details cache for a site. |
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 |
* @since 4.7.4 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
326 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
327 |
* @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
|
328 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
329 |
function clean_site_details_cache( $site_id = 0 ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
330 |
$site_id = (int) $site_id; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
331 |
if ( ! $site_id ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
332 |
$site_id = get_current_blog_id(); |
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 |
wp_cache_delete( $site_id, 'site-details' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
336 |
wp_cache_delete( $site_id, 'blog-details' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
337 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
338 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
339 |
/** |
0 | 340 |
* Retrieve option value for a given blog id based on name of option. |
341 |
* |
|
342 |
* If the option does not exist or does not have a value, then the return value |
|
343 |
* will be false. This is useful to check whether you need to install an option |
|
344 |
* and is commonly used during installation of plugin options and to test |
|
345 |
* whether upgrading is required. |
|
346 |
* |
|
347 |
* If the option was serialized then it will be unserialized when it is returned. |
|
348 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
349 |
* @since MU (3.0.0) |
0 | 350 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
351 |
* @param int $id A blog ID. Can be null to refer to the current blog. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
352 |
* @param string $option Name of option to retrieve. Expected to not be SQL-escaped. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
353 |
* @param mixed $default Optional. Default value to return if the option does not exist. |
0 | 354 |
* @return mixed Value set for the option. |
355 |
*/ |
|
356 |
function get_blog_option( $id, $option, $default = false ) { |
|
357 |
$id = (int) $id; |
|
358 |
||
9 | 359 |
if ( empty( $id ) ) { |
0 | 360 |
$id = get_current_blog_id(); |
9 | 361 |
} |
0 | 362 |
|
9 | 363 |
if ( get_current_blog_id() == $id ) { |
0 | 364 |
return get_option( $option, $default ); |
9 | 365 |
} |
0 | 366 |
|
367 |
switch_to_blog( $id ); |
|
368 |
$value = get_option( $option, $default ); |
|
369 |
restore_current_blog(); |
|
370 |
||
5 | 371 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
372 |
* Filters a blog option value. |
5 | 373 |
* |
374 |
* The dynamic portion of the hook name, `$option`, refers to the blog option name. |
|
375 |
* |
|
376 |
* @since 3.5.0 |
|
377 |
* |
|
378 |
* @param string $value The option value. |
|
379 |
* @param int $id Blog ID. |
|
380 |
*/ |
|
381 |
return apply_filters( "blog_option_{$option}", $value, $id ); |
|
0 | 382 |
} |
383 |
||
384 |
/** |
|
16 | 385 |
* Add a new option for a given blog ID. |
0 | 386 |
* |
387 |
* You do not need to serialize values. If the value needs to be serialized, then |
|
388 |
* it will be serialized before it is inserted into the database. Remember, |
|
389 |
* resources can not be serialized or added as an option. |
|
390 |
* |
|
391 |
* You can create options without values and then update the values later. |
|
392 |
* Existing options will not be updated and checks are performed to ensure that you |
|
393 |
* aren't adding a protected WordPress option. Care should be taken to not name |
|
394 |
* options the same as the ones which are protected. |
|
395 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
396 |
* @since MU (3.0.0) |
0 | 397 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
398 |
* @param int $id A blog ID. Can be null to refer to the current blog. |
0 | 399 |
* @param string $option Name of option to add. Expected to not be SQL-escaped. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
400 |
* @param mixed $value Optional. Option value, can be anything. Expected to not be SQL-escaped. |
16 | 401 |
* @return bool True if the option was added, false otherwise. |
0 | 402 |
*/ |
403 |
function add_blog_option( $id, $option, $value ) { |
|
404 |
$id = (int) $id; |
|
405 |
||
9 | 406 |
if ( empty( $id ) ) { |
0 | 407 |
$id = get_current_blog_id(); |
9 | 408 |
} |
0 | 409 |
|
9 | 410 |
if ( get_current_blog_id() == $id ) { |
0 | 411 |
return add_option( $option, $value ); |
9 | 412 |
} |
0 | 413 |
|
414 |
switch_to_blog( $id ); |
|
415 |
$return = add_option( $option, $value ); |
|
416 |
restore_current_blog(); |
|
417 |
||
418 |
return $return; |
|
419 |
} |
|
420 |
||
421 |
/** |
|
16 | 422 |
* Removes option by name for a given blog ID. Prevents removal of protected WordPress options. |
0 | 423 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
424 |
* @since MU (3.0.0) |
0 | 425 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
426 |
* @param int $id A blog ID. Can be null to refer to the current blog. |
0 | 427 |
* @param string $option Name of option to remove. Expected to not be SQL-escaped. |
16 | 428 |
* @return bool True if the option was deleted, false otherwise. |
0 | 429 |
*/ |
430 |
function delete_blog_option( $id, $option ) { |
|
431 |
$id = (int) $id; |
|
432 |
||
9 | 433 |
if ( empty( $id ) ) { |
0 | 434 |
$id = get_current_blog_id(); |
9 | 435 |
} |
0 | 436 |
|
9 | 437 |
if ( get_current_blog_id() == $id ) { |
0 | 438 |
return delete_option( $option ); |
9 | 439 |
} |
0 | 440 |
|
441 |
switch_to_blog( $id ); |
|
442 |
$return = delete_option( $option ); |
|
443 |
restore_current_blog(); |
|
444 |
||
445 |
return $return; |
|
446 |
} |
|
447 |
||
448 |
/** |
|
449 |
* Update an option for a particular blog. |
|
450 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
451 |
* @since MU (3.0.0) |
0 | 452 |
* |
16 | 453 |
* @param int $id The blog ID. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
454 |
* @param string $option The option key. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
455 |
* @param mixed $value The option value. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
456 |
* @param mixed $deprecated Not used. |
16 | 457 |
* @return bool True if the value was updated, false otherwise. |
0 | 458 |
*/ |
459 |
function update_blog_option( $id, $option, $value, $deprecated = null ) { |
|
460 |
$id = (int) $id; |
|
461 |
||
9 | 462 |
if ( null !== $deprecated ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
463 |
_deprecated_argument( __FUNCTION__, '3.1.0' ); |
9 | 464 |
} |
0 | 465 |
|
9 | 466 |
if ( get_current_blog_id() == $id ) { |
0 | 467 |
return update_option( $option, $value ); |
9 | 468 |
} |
0 | 469 |
|
470 |
switch_to_blog( $id ); |
|
471 |
$return = update_option( $option, $value ); |
|
472 |
restore_current_blog(); |
|
473 |
||
474 |
return $return; |
|
475 |
} |
|
476 |
||
477 |
/** |
|
478 |
* Switch the current blog. |
|
479 |
* |
|
480 |
* This function is useful if you need to pull posts, or other information, |
|
481 |
* from other blogs. You can switch back afterwards using restore_current_blog(). |
|
482 |
* |
|
483 |
* Things that aren't switched: |
|
484 |
* - plugins. See #14941 |
|
485 |
* |
|
486 |
* @see restore_current_blog() |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
487 |
* @since MU (3.0.0) |
0 | 488 |
* |
16 | 489 |
* @global wpdb $wpdb WordPress database abstraction object. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
490 |
* @global int $blog_id |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
491 |
* @global array $_wp_switched_stack |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
492 |
* @global bool $switched |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
493 |
* @global string $table_prefix |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
494 |
* @global WP_Object_Cache $wp_object_cache |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
495 |
* |
16 | 496 |
* @param int $new_blog_id The ID of the blog to switch to. Default: current blog. |
497 |
* @param bool $deprecated Not used. |
|
498 |
* @return true Always returns true. |
|
0 | 499 |
*/ |
16 | 500 |
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
|
501 |
global $wpdb; |
0 | 502 |
|
16 | 503 |
$prev_blog_id = get_current_blog_id(); |
504 |
if ( empty( $new_blog_id ) ) { |
|
505 |
$new_blog_id = $prev_blog_id; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
506 |
} |
0 | 507 |
|
16 | 508 |
$GLOBALS['_wp_switched_stack'][] = $prev_blog_id; |
0 | 509 |
|
5 | 510 |
/* |
511 |
* If we're switching to the same blog id that we're on, |
|
512 |
* set the right vars, do the associated actions, but skip |
|
513 |
* the extra unnecessary work |
|
514 |
*/ |
|
16 | 515 |
if ( $new_blog_id == $prev_blog_id ) { |
5 | 516 |
/** |
517 |
* Fires when the blog is switched. |
|
518 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
519 |
* @since MU (3.0.0) |
16 | 520 |
* @since 5.4.0 The `$context` parameter was added. |
5 | 521 |
* |
16 | 522 |
* @param int $new_blog_id New blog ID. |
523 |
* @param int $prev_blog_id Previous blog ID. |
|
524 |
* @param string $context Additional context. Accepts 'switch' when called from switch_to_blog() |
|
525 |
* or 'restore' when called from restore_current_blog(). |
|
5 | 526 |
*/ |
16 | 527 |
do_action( 'switch_blog', $new_blog_id, $prev_blog_id, 'switch' ); |
528 |
||
0 | 529 |
$GLOBALS['switched'] = true; |
16 | 530 |
|
0 | 531 |
return true; |
532 |
} |
|
533 |
||
16 | 534 |
$wpdb->set_blog_id( $new_blog_id ); |
0 | 535 |
$GLOBALS['table_prefix'] = $wpdb->get_blog_prefix(); |
16 | 536 |
$GLOBALS['blog_id'] = $new_blog_id; |
0 | 537 |
|
538 |
if ( function_exists( 'wp_cache_switch_to_blog' ) ) { |
|
16 | 539 |
wp_cache_switch_to_blog( $new_blog_id ); |
0 | 540 |
} else { |
541 |
global $wp_object_cache; |
|
542 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
543 |
if ( is_object( $wp_object_cache ) && isset( $wp_object_cache->global_groups ) ) { |
0 | 544 |
$global_groups = $wp_object_cache->global_groups; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
545 |
} else { |
0 | 546 |
$global_groups = false; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
547 |
} |
0 | 548 |
|
549 |
wp_cache_init(); |
|
550 |
||
551 |
if ( function_exists( 'wp_cache_add_global_groups' ) ) { |
|
5 | 552 |
if ( is_array( $global_groups ) ) { |
0 | 553 |
wp_cache_add_global_groups( $global_groups ); |
5 | 554 |
} else { |
9 | 555 |
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' ) ); |
5 | 556 |
} |
16 | 557 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
558 |
wp_cache_add_non_persistent_groups( array( 'counts', 'plugins' ) ); |
0 | 559 |
} |
560 |
} |
|
561 |
||
5 | 562 |
/** This filter is documented in wp-includes/ms-blogs.php */ |
16 | 563 |
do_action( 'switch_blog', $new_blog_id, $prev_blog_id, 'switch' ); |
564 |
||
565 |
$GLOBALS['switched'] = true; |
|
566 |
||
567 |
return true; |
|
568 |
} |
|
569 |
||
570 |
/** |
|
571 |
* Restore the current blog, after calling switch_to_blog(). |
|
572 |
* |
|
573 |
* @see switch_to_blog() |
|
574 |
* @since MU (3.0.0) |
|
575 |
* |
|
576 |
* @global wpdb $wpdb WordPress database abstraction object. |
|
577 |
* @global array $_wp_switched_stack |
|
578 |
* @global int $blog_id |
|
579 |
* @global bool $switched |
|
580 |
* @global string $table_prefix |
|
581 |
* @global WP_Object_Cache $wp_object_cache |
|
582 |
* |
|
583 |
* @return bool True on success, false if we're already on the current blog. |
|
584 |
*/ |
|
585 |
function restore_current_blog() { |
|
586 |
global $wpdb; |
|
587 |
||
588 |
if ( empty( $GLOBALS['_wp_switched_stack'] ) ) { |
|
589 |
return false; |
|
590 |
} |
|
591 |
||
592 |
$new_blog_id = array_pop( $GLOBALS['_wp_switched_stack'] ); |
|
593 |
$prev_blog_id = get_current_blog_id(); |
|
594 |
||
595 |
if ( $new_blog_id == $prev_blog_id ) { |
|
596 |
/** This filter is documented in wp-includes/ms-blogs.php */ |
|
597 |
do_action( 'switch_blog', $new_blog_id, $prev_blog_id, 'restore' ); |
|
0 | 598 |
|
16 | 599 |
// If we still have items in the switched stack, consider ourselves still 'switched'. |
600 |
$GLOBALS['switched'] = ! empty( $GLOBALS['_wp_switched_stack'] ); |
|
601 |
||
602 |
return true; |
|
603 |
} |
|
604 |
||
605 |
$wpdb->set_blog_id( $new_blog_id ); |
|
606 |
$GLOBALS['blog_id'] = $new_blog_id; |
|
607 |
$GLOBALS['table_prefix'] = $wpdb->get_blog_prefix(); |
|
608 |
||
609 |
if ( function_exists( 'wp_cache_switch_to_blog' ) ) { |
|
610 |
wp_cache_switch_to_blog( $new_blog_id ); |
|
611 |
} else { |
|
612 |
global $wp_object_cache; |
|
613 |
||
614 |
if ( is_object( $wp_object_cache ) && isset( $wp_object_cache->global_groups ) ) { |
|
615 |
$global_groups = $wp_object_cache->global_groups; |
|
616 |
} else { |
|
617 |
$global_groups = false; |
|
618 |
} |
|
619 |
||
620 |
wp_cache_init(); |
|
621 |
||
622 |
if ( function_exists( 'wp_cache_add_global_groups' ) ) { |
|
623 |
if ( is_array( $global_groups ) ) { |
|
624 |
wp_cache_add_global_groups( $global_groups ); |
|
625 |
} else { |
|
626 |
wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'useremail', 'userslugs', 'site-transient', 'site-options', 'blog-lookup', 'blog-details', 'rss', 'global-posts', 'blog-id-cache', 'networks', 'sites', 'site-details', 'blog_meta' ) ); |
|
627 |
} |
|
628 |
||
629 |
wp_cache_add_non_persistent_groups( array( 'counts', 'plugins' ) ); |
|
630 |
} |
|
631 |
} |
|
632 |
||
633 |
/** This filter is documented in wp-includes/ms-blogs.php */ |
|
634 |
do_action( 'switch_blog', $new_blog_id, $prev_blog_id, 'restore' ); |
|
635 |
||
636 |
// If we still have items in the switched stack, consider ourselves still 'switched'. |
|
0 | 637 |
$GLOBALS['switched'] = ! empty( $GLOBALS['_wp_switched_stack'] ); |
638 |
||
639 |
return true; |
|
640 |
} |
|
641 |
||
642 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
643 |
* 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
|
644 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
645 |
* @since 4.9.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
646 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
647 |
* @param int $new_site_id New site ID. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
648 |
* @param int $old_site_id Old site ID. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
649 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
650 |
function wp_switch_roles_and_user( $new_site_id, $old_site_id ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
651 |
if ( $new_site_id == $old_site_id ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
652 |
return; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
653 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
654 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
655 |
if ( ! did_action( 'init' ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
656 |
return; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
657 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
658 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
659 |
wp_roles()->for_site( $new_site_id ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
660 |
wp_get_current_user()->for_site( $new_site_id ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
661 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
662 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
663 |
/** |
0 | 664 |
* Determines if switch_to_blog() is in effect |
665 |
* |
|
666 |
* @since 3.5.0 |
|
667 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
668 |
* @global array $_wp_switched_stack |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
669 |
* |
0 | 670 |
* @return bool True if switched, false otherwise. |
671 |
*/ |
|
672 |
function ms_is_switched() { |
|
673 |
return ! empty( $GLOBALS['_wp_switched_stack'] ); |
|
674 |
} |
|
675 |
||
676 |
/** |
|
677 |
* Check if a particular blog is archived. |
|
678 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
679 |
* @since MU (3.0.0) |
0 | 680 |
* |
16 | 681 |
* @param int $id Blog ID. |
682 |
* @return string Whether the blog is archived or not. |
|
0 | 683 |
*/ |
684 |
function is_archived( $id ) { |
|
9 | 685 |
return get_blog_status( $id, 'archived' ); |
0 | 686 |
} |
687 |
||
688 |
/** |
|
689 |
* Update the 'archived' status of a particular blog. |
|
690 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
691 |
* @since MU (3.0.0) |
0 | 692 |
* |
16 | 693 |
* @param int $id Blog ID. |
694 |
* @param string $archived The new status. |
|
0 | 695 |
* @return string $archived |
696 |
*/ |
|
697 |
function update_archived( $id, $archived ) { |
|
9 | 698 |
update_blog_status( $id, 'archived', $archived ); |
0 | 699 |
return $archived; |
700 |
} |
|
701 |
||
702 |
/** |
|
703 |
* Update a blog details field. |
|
704 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
705 |
* @since MU (3.0.0) |
9 | 706 |
* @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
|
707 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
708 |
* @global wpdb $wpdb WordPress database abstraction object. |
0 | 709 |
* |
16 | 710 |
* @param int $blog_id Blog ID. |
711 |
* @param string $pref Field name. |
|
712 |
* @param string $value Field value. |
|
713 |
* @param null $deprecated Not used. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
714 |
* @return string|false $value |
0 | 715 |
*/ |
716 |
function update_blog_status( $blog_id, $pref, $value, $deprecated = null ) { |
|
717 |
global $wpdb; |
|
718 |
||
9 | 719 |
if ( null !== $deprecated ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
720 |
_deprecated_argument( __FUNCTION__, '3.1.0' ); |
9 | 721 |
} |
0 | 722 |
|
16 | 723 |
$allowed_field_names = array( 'site_id', 'domain', 'path', 'registered', 'last_updated', 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id' ); |
724 |
||
725 |
if ( ! in_array( $pref, $allowed_field_names, true ) ) { |
|
9 | 726 |
return $value; |
727 |
} |
|
0 | 728 |
|
9 | 729 |
$result = wp_update_site( |
730 |
$blog_id, |
|
731 |
array( |
|
732 |
$pref => $value, |
|
733 |
) |
|
734 |
); |
|
735 |
||
736 |
if ( is_wp_error( $result ) ) { |
|
737 |
return false; |
|
5 | 738 |
} |
0 | 739 |
|
740 |
return $value; |
|
741 |
} |
|
742 |
||
743 |
/** |
|
744 |
* Get a blog details field. |
|
745 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
746 |
* @since MU (3.0.0) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
747 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
748 |
* @global wpdb $wpdb WordPress database abstraction object. |
0 | 749 |
* |
16 | 750 |
* @param int $id Blog ID. |
751 |
* @param string $pref Field name. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
752 |
* @return bool|string|null $value |
0 | 753 |
*/ |
754 |
function get_blog_status( $id, $pref ) { |
|
755 |
global $wpdb; |
|
756 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
757 |
$details = get_site( $id ); |
9 | 758 |
if ( $details ) { |
0 | 759 |
return $details->$pref; |
9 | 760 |
} |
0 | 761 |
|
9 | 762 |
return $wpdb->get_var( $wpdb->prepare( "SELECT %s FROM {$wpdb->blogs} WHERE blog_id = %d", $pref, $id ) ); |
0 | 763 |
} |
764 |
||
765 |
/** |
|
766 |
* Get a list of most recently updated blogs. |
|
767 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
768 |
* @since MU (3.0.0) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
769 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
770 |
* @global wpdb $wpdb WordPress database abstraction object. |
0 | 771 |
* |
16 | 772 |
* @param mixed $deprecated Not used. |
773 |
* @param int $start Optional. Number of blogs to offset the query. Used to build LIMIT clause. |
|
774 |
* Can be used for pagination. Default 0. |
|
775 |
* @param int $quantity Optional. The maximum number of blogs to retrieve. Default 40. |
|
776 |
* @return array The list of blogs. |
|
0 | 777 |
*/ |
778 |
function get_last_updated( $deprecated = '', $start = 0, $quantity = 40 ) { |
|
779 |
global $wpdb; |
|
780 |
||
9 | 781 |
if ( ! empty( $deprecated ) ) { |
16 | 782 |
_deprecated_argument( __FUNCTION__, 'MU' ); // Never used. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
783 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
784 |
|
9 | 785 |
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
|
786 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
787 |
|
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 |
* 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
|
790 |
* an already published post is changed. |
0 | 791 |
* |
792 |
* @since 3.3.0 |
|
793 |
* |
|
16 | 794 |
* @param string $new_status The new post status. |
795 |
* @param string $old_status The old post status. |
|
796 |
* @param WP_Post $post Post object. |
|
0 | 797 |
*/ |
798 |
function _update_blog_date_on_post_publish( $new_status, $old_status, $post ) { |
|
799 |
$post_type_obj = get_post_type_object( $post->post_type ); |
|
5 | 800 |
if ( ! $post_type_obj || ! $post_type_obj->public ) { |
0 | 801 |
return; |
5 | 802 |
} |
0 | 803 |
|
16 | 804 |
if ( 'publish' !== $new_status && 'publish' !== $old_status ) { |
0 | 805 |
return; |
5 | 806 |
} |
0 | 807 |
|
808 |
// Post was freshly published, published post was saved, or published post was unpublished. |
|
809 |
||
810 |
wpmu_update_blogs_date(); |
|
811 |
} |
|
812 |
||
813 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
814 |
* 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
|
815 |
* post is deleted. |
0 | 816 |
* |
817 |
* @since 3.4.0 |
|
818 |
* |
|
819 |
* @param int $post_id Post ID |
|
820 |
*/ |
|
821 |
function _update_blog_date_on_post_delete( $post_id ) { |
|
822 |
$post = get_post( $post_id ); |
|
823 |
||
824 |
$post_type_obj = get_post_type_object( $post->post_type ); |
|
5 | 825 |
if ( ! $post_type_obj || ! $post_type_obj->public ) { |
0 | 826 |
return; |
5 | 827 |
} |
0 | 828 |
|
16 | 829 |
if ( 'publish' !== $post->post_status ) { |
0 | 830 |
return; |
5 | 831 |
} |
0 | 832 |
|
833 |
wpmu_update_blogs_date(); |
|
834 |
} |
|
835 |
||
5 | 836 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
837 |
* Handler for updating the current site's posts count when a post is deleted. |
5 | 838 |
* |
839 |
* @since 4.0.0 |
|
840 |
* |
|
841 |
* @param int $post_id Post ID. |
|
842 |
*/ |
|
843 |
function _update_posts_count_on_delete( $post_id ) { |
|
844 |
$post = get_post( $post_id ); |
|
845 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
846 |
if ( ! $post || 'publish' !== $post->post_status || 'post' !== $post->post_type ) { |
5 | 847 |
return; |
848 |
} |
|
849 |
||
850 |
update_posts_count(); |
|
851 |
} |
|
852 |
||
853 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
854 |
* Handler for updating the current site's posts count when a post status changes. |
5 | 855 |
* |
856 |
* @since 4.0.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
857 |
* @since 4.9.0 Added the `$post` parameter. |
5 | 858 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
859 |
* @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
|
860 |
* @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
|
861 |
* @param WP_Post $post Post object |
5 | 862 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
863 |
function _update_posts_count_on_transition_post_status( $new_status, $old_status, $post = null ) { |
5 | 864 |
if ( $new_status === $old_status ) { |
865 |
return; |
|
866 |
} |
|
867 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
868 |
if ( 'post' !== get_post_type( $post ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
869 |
return; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
870 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
871 |
|
5 | 872 |
if ( 'publish' !== $new_status && 'publish' !== $old_status ) { |
873 |
return; |
|
874 |
} |
|
875 |
||
876 |
update_posts_count(); |
|
877 |
} |
|
16 | 878 |
|
879 |
/** |
|
880 |
* Count number of sites grouped by site status. |
|
881 |
* |
|
882 |
* @since 5.3.0 |
|
883 |
* |
|
884 |
* @param int $network_id Optional. The network to get counts for. Default is the current network ID. |
|
885 |
* @return int[] { |
|
886 |
* Numbers of sites grouped by site status. |
|
887 |
* |
|
888 |
* @type int $all The total number of sites. |
|
889 |
* @type int $public The number of public sites. |
|
890 |
* @type int $archived The number of archived sites. |
|
891 |
* @type int $mature The number of mature sites. |
|
892 |
* @type int $spam The number of spam sites. |
|
893 |
* @type int $deleted The number of deleted sites. |
|
894 |
* } |
|
895 |
*/ |
|
896 |
function wp_count_sites( $network_id = null ) { |
|
897 |
if ( empty( $network_id ) ) { |
|
898 |
$network_id = get_current_network_id(); |
|
899 |
} |
|
900 |
||
901 |
$counts = array(); |
|
902 |
$args = array( |
|
903 |
'network_id' => $network_id, |
|
904 |
'number' => 1, |
|
905 |
'fields' => 'ids', |
|
906 |
'no_found_rows' => false, |
|
907 |
); |
|
908 |
||
909 |
$q = new WP_Site_Query( $args ); |
|
910 |
$counts['all'] = $q->found_sites; |
|
911 |
||
912 |
$_args = $args; |
|
913 |
$statuses = array( 'public', 'archived', 'mature', 'spam', 'deleted' ); |
|
914 |
||
915 |
foreach ( $statuses as $status ) { |
|
916 |
$_args = $args; |
|
917 |
$_args[ $status ] = 1; |
|
918 |
||
919 |
$q = new WP_Site_Query( $_args ); |
|
920 |
$counts[ $status ] = $q->found_sites; |
|
921 |
} |
|
922 |
||
923 |
return $counts; |
|
924 |
} |