author | ymh <ymh.work@gmail.com> |
Mon, 14 Oct 2019 18:28:13 +0200 | |
changeset 9 | 177826044cd9 |
parent 7 | cf61fcea0001 |
child 16 | a86126ab1dd4 |
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 |
||
9 | 11 |
require_once( ABSPATH . WPINC . '/ms-site.php' ); |
12 |
require_once( ABSPATH . WPINC . '/ms-network.php' ); |
|
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 |
/** |
|
34 |
* Get a full blog URL, given a blog id. |
|
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 |
* |
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 |
} |
0 | 140 |
if ( substr( $fields['domain'], 0, 4 ) == 'www.' ) { |
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 |
} |
0 | 158 |
if ( substr( $fields['domain'], 0, 4 ) == 'www.' ) { |
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 |
||
241 |
switch_to_blog( $blog_id ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
242 |
$details->blogname = get_option( 'blogname' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
243 |
$details->siteurl = get_option( 'siteurl' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
244 |
$details->post_count = get_option( 'post_count' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
245 |
$details->home = get_option( 'home' ); |
0 | 246 |
restore_current_blog(); |
247 |
||
5 | 248 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
249 |
* Filters a blog's details. |
5 | 250 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
251 |
* @since MU (3.0.0) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
252 |
* @deprecated 4.7.0 Use site_details |
5 | 253 |
* |
254 |
* @param object $details The blog details. |
|
255 |
*/ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
256 |
$details = apply_filters_deprecated( 'blog_details', array( $details ), '4.7.0', 'site_details' ); |
0 | 257 |
|
258 |
wp_cache_set( $blog_id . $all, $details, 'blog-details' ); |
|
259 |
||
260 |
$key = md5( $details->domain . $details->path ); |
|
261 |
wp_cache_set( $key, $details, 'blog-lookup' ); |
|
262 |
||
263 |
return $details; |
|
264 |
} |
|
265 |
||
266 |
/** |
|
267 |
* Clear the blog details cache. |
|
268 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
269 |
* @since MU (3.0.0) |
0 | 270 |
* |
5 | 271 |
* @param int $blog_id Optional. Blog ID. Defaults to current blog. |
0 | 272 |
*/ |
5 | 273 |
function refresh_blog_details( $blog_id = 0 ) { |
0 | 274 |
$blog_id = (int) $blog_id; |
5 | 275 |
if ( ! $blog_id ) { |
276 |
$blog_id = get_current_blog_id(); |
|
277 |
} |
|
278 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
279 |
clean_blog_cache( $blog_id ); |
0 | 280 |
} |
281 |
||
282 |
/** |
|
283 |
* Update the details for a blog. Updates the blogs table for a given blog id. |
|
284 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
285 |
* @since MU (3.0.0) |
0 | 286 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
287 |
* @global wpdb $wpdb WordPress database abstraction object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
288 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
289 |
* @param int $blog_id Blog ID |
0 | 290 |
* @param array $details Array of details keyed by blogs table field names. |
291 |
* @return bool True if update succeeds, false otherwise. |
|
292 |
*/ |
|
293 |
function update_blog_details( $blog_id, $details = array() ) { |
|
294 |
global $wpdb; |
|
295 |
||
9 | 296 |
if ( empty( $details ) ) { |
0 | 297 |
return false; |
5 | 298 |
} |
0 | 299 |
|
9 | 300 |
if ( is_object( $details ) ) { |
301 |
$details = get_object_vars( $details ); |
|
0 | 302 |
} |
303 |
||
9 | 304 |
$site = wp_update_site( $blog_id, $details ); |
0 | 305 |
|
9 | 306 |
if ( is_wp_error( $site ) ) { |
307 |
return false; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
308 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
309 |
|
9 | 310 |
return true; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
311 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
312 |
|
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 |
* Cleans the site details cache for a site. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
315 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
316 |
* @since 4.7.4 |
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 |
* @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
|
319 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
320 |
function clean_site_details_cache( $site_id = 0 ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
321 |
$site_id = (int) $site_id; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
322 |
if ( ! $site_id ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
323 |
$site_id = get_current_blog_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 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
326 |
wp_cache_delete( $site_id, 'site-details' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
327 |
wp_cache_delete( $site_id, 'blog-details' ); |
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 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
330 |
/** |
0 | 331 |
* Retrieve option value for a given blog id based on name of option. |
332 |
* |
|
333 |
* If the option does not exist or does not have a value, then the return value |
|
334 |
* will be false. This is useful to check whether you need to install an option |
|
335 |
* and is commonly used during installation of plugin options and to test |
|
336 |
* whether upgrading is required. |
|
337 |
* |
|
338 |
* If the option was serialized then it will be unserialized when it is returned. |
|
339 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
340 |
* @since MU (3.0.0) |
0 | 341 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
342 |
* @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
|
343 |
* @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
|
344 |
* @param mixed $default Optional. Default value to return if the option does not exist. |
0 | 345 |
* @return mixed Value set for the option. |
346 |
*/ |
|
347 |
function get_blog_option( $id, $option, $default = false ) { |
|
348 |
$id = (int) $id; |
|
349 |
||
9 | 350 |
if ( empty( $id ) ) { |
0 | 351 |
$id = get_current_blog_id(); |
9 | 352 |
} |
0 | 353 |
|
9 | 354 |
if ( get_current_blog_id() == $id ) { |
0 | 355 |
return get_option( $option, $default ); |
9 | 356 |
} |
0 | 357 |
|
358 |
switch_to_blog( $id ); |
|
359 |
$value = get_option( $option, $default ); |
|
360 |
restore_current_blog(); |
|
361 |
||
5 | 362 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
363 |
* Filters a blog option value. |
5 | 364 |
* |
365 |
* The dynamic portion of the hook name, `$option`, refers to the blog option name. |
|
366 |
* |
|
367 |
* @since 3.5.0 |
|
368 |
* |
|
369 |
* @param string $value The option value. |
|
370 |
* @param int $id Blog ID. |
|
371 |
*/ |
|
372 |
return apply_filters( "blog_option_{$option}", $value, $id ); |
|
0 | 373 |
} |
374 |
||
375 |
/** |
|
376 |
* Add a new option for a given blog id. |
|
377 |
* |
|
378 |
* You do not need to serialize values. If the value needs to be serialized, then |
|
379 |
* it will be serialized before it is inserted into the database. Remember, |
|
380 |
* resources can not be serialized or added as an option. |
|
381 |
* |
|
382 |
* You can create options without values and then update the values later. |
|
383 |
* Existing options will not be updated and checks are performed to ensure that you |
|
384 |
* aren't adding a protected WordPress option. Care should be taken to not name |
|
385 |
* options the same as the ones which are protected. |
|
386 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
387 |
* @since MU (3.0.0) |
0 | 388 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
389 |
* @param int $id A blog ID. Can be null to refer to the current blog. |
0 | 390 |
* @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
|
391 |
* @param mixed $value Optional. Option value, can be anything. Expected to not be SQL-escaped. |
0 | 392 |
* @return bool False if option was not added and true if option was added. |
393 |
*/ |
|
394 |
function add_blog_option( $id, $option, $value ) { |
|
395 |
$id = (int) $id; |
|
396 |
||
9 | 397 |
if ( empty( $id ) ) { |
0 | 398 |
$id = get_current_blog_id(); |
9 | 399 |
} |
0 | 400 |
|
9 | 401 |
if ( get_current_blog_id() == $id ) { |
0 | 402 |
return add_option( $option, $value ); |
9 | 403 |
} |
0 | 404 |
|
405 |
switch_to_blog( $id ); |
|
406 |
$return = add_option( $option, $value ); |
|
407 |
restore_current_blog(); |
|
408 |
||
409 |
return $return; |
|
410 |
} |
|
411 |
||
412 |
/** |
|
413 |
* Removes option by name for a given blog id. Prevents removal of protected WordPress options. |
|
414 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
415 |
* @since MU (3.0.0) |
0 | 416 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
417 |
* @param int $id A blog ID. Can be null to refer to the current blog. |
0 | 418 |
* @param string $option Name of option to remove. Expected to not be SQL-escaped. |
419 |
* @return bool True, if option is successfully deleted. False on failure. |
|
420 |
*/ |
|
421 |
function delete_blog_option( $id, $option ) { |
|
422 |
$id = (int) $id; |
|
423 |
||
9 | 424 |
if ( empty( $id ) ) { |
0 | 425 |
$id = get_current_blog_id(); |
9 | 426 |
} |
0 | 427 |
|
9 | 428 |
if ( get_current_blog_id() == $id ) { |
0 | 429 |
return delete_option( $option ); |
9 | 430 |
} |
0 | 431 |
|
432 |
switch_to_blog( $id ); |
|
433 |
$return = delete_option( $option ); |
|
434 |
restore_current_blog(); |
|
435 |
||
436 |
return $return; |
|
437 |
} |
|
438 |
||
439 |
/** |
|
440 |
* Update an option for a particular blog. |
|
441 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
442 |
* @since MU (3.0.0) |
0 | 443 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
444 |
* @param int $id The blog id. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
445 |
* @param string $option The option key. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
446 |
* @param mixed $value The option value. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
447 |
* @param mixed $deprecated Not used. |
0 | 448 |
* @return bool True on success, false on failure. |
449 |
*/ |
|
450 |
function update_blog_option( $id, $option, $value, $deprecated = null ) { |
|
451 |
$id = (int) $id; |
|
452 |
||
9 | 453 |
if ( null !== $deprecated ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
454 |
_deprecated_argument( __FUNCTION__, '3.1.0' ); |
9 | 455 |
} |
0 | 456 |
|
9 | 457 |
if ( get_current_blog_id() == $id ) { |
0 | 458 |
return update_option( $option, $value ); |
9 | 459 |
} |
0 | 460 |
|
461 |
switch_to_blog( $id ); |
|
462 |
$return = update_option( $option, $value ); |
|
463 |
restore_current_blog(); |
|
464 |
||
465 |
return $return; |
|
466 |
} |
|
467 |
||
468 |
/** |
|
469 |
* Switch the current blog. |
|
470 |
* |
|
471 |
* This function is useful if you need to pull posts, or other information, |
|
472 |
* from other blogs. You can switch back afterwards using restore_current_blog(). |
|
473 |
* |
|
474 |
* Things that aren't switched: |
|
475 |
* - plugins. See #14941 |
|
476 |
* |
|
477 |
* @see restore_current_blog() |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
478 |
* @since MU (3.0.0) |
0 | 479 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
480 |
* @global wpdb $wpdb |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
481 |
* @global int $blog_id |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
482 |
* @global array $_wp_switched_stack |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
483 |
* @global bool $switched |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
484 |
* @global string $table_prefix |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
485 |
* @global WP_Object_Cache $wp_object_cache |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
486 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
487 |
* @param int $new_blog The id of the blog you want to switch to. Default: current blog |
0 | 488 |
* @param bool $deprecated Deprecated argument |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
489 |
* @return true Always returns True. |
0 | 490 |
*/ |
491 |
function switch_to_blog( $new_blog, $deprecated = null ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
492 |
global $wpdb; |
0 | 493 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
494 |
$blog_id = get_current_blog_id(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
495 |
if ( empty( $new_blog ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
496 |
$new_blog = $blog_id; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
497 |
} |
0 | 498 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
499 |
$GLOBALS['_wp_switched_stack'][] = $blog_id; |
0 | 500 |
|
5 | 501 |
/* |
502 |
* If we're switching to the same blog id that we're on, |
|
503 |
* set the right vars, do the associated actions, but skip |
|
504 |
* the extra unnecessary work |
|
505 |
*/ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
506 |
if ( $new_blog == $blog_id ) { |
5 | 507 |
/** |
508 |
* Fires when the blog is switched. |
|
509 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
510 |
* @since MU (3.0.0) |
5 | 511 |
* |
512 |
* @param int $new_blog New blog ID. |
|
513 |
* @param int $new_blog Blog ID. |
|
514 |
*/ |
|
0 | 515 |
do_action( 'switch_blog', $new_blog, $new_blog ); |
516 |
$GLOBALS['switched'] = true; |
|
517 |
return true; |
|
518 |
} |
|
519 |
||
520 |
$wpdb->set_blog_id( $new_blog ); |
|
521 |
$GLOBALS['table_prefix'] = $wpdb->get_blog_prefix(); |
|
9 | 522 |
$prev_blog_id = $blog_id; |
523 |
$GLOBALS['blog_id'] = $new_blog; |
|
0 | 524 |
|
525 |
if ( function_exists( 'wp_cache_switch_to_blog' ) ) { |
|
526 |
wp_cache_switch_to_blog( $new_blog ); |
|
527 |
} else { |
|
528 |
global $wp_object_cache; |
|
529 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
530 |
if ( is_object( $wp_object_cache ) && isset( $wp_object_cache->global_groups ) ) { |
0 | 531 |
$global_groups = $wp_object_cache->global_groups; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
532 |
} else { |
0 | 533 |
$global_groups = false; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
534 |
} |
0 | 535 |
wp_cache_init(); |
536 |
||
537 |
if ( function_exists( 'wp_cache_add_global_groups' ) ) { |
|
5 | 538 |
if ( is_array( $global_groups ) ) { |
0 | 539 |
wp_cache_add_global_groups( $global_groups ); |
5 | 540 |
} else { |
9 | 541 |
wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'useremail', 'userslugs', 'site-transient', 'site-options', 'blog-lookup', 'blog-details', 'rss', 'global-posts', 'blog-id-cache', 'networks', 'sites', 'site-details', 'blog_meta' ) ); |
5 | 542 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
543 |
wp_cache_add_non_persistent_groups( array( 'counts', 'plugins' ) ); |
0 | 544 |
} |
545 |
} |
|
546 |
||
5 | 547 |
/** This filter is documented in wp-includes/ms-blogs.php */ |
0 | 548 |
do_action( 'switch_blog', $new_blog, $prev_blog_id ); |
549 |
$GLOBALS['switched'] = true; |
|
550 |
||
551 |
return true; |
|
552 |
} |
|
553 |
||
554 |
/** |
|
555 |
* Restore the current blog, after calling switch_to_blog() |
|
556 |
* |
|
557 |
* @see switch_to_blog() |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
558 |
* @since MU (3.0.0) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
559 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
560 |
* @global wpdb $wpdb |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
561 |
* @global array $_wp_switched_stack |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
562 |
* @global int $blog_id |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
563 |
* @global bool $switched |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
564 |
* @global string $table_prefix |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
565 |
* @global WP_Object_Cache $wp_object_cache |
0 | 566 |
* |
567 |
* @return bool True on success, false if we're already on the current blog |
|
568 |
*/ |
|
569 |
function restore_current_blog() { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
570 |
global $wpdb; |
0 | 571 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
572 |
if ( empty( $GLOBALS['_wp_switched_stack'] ) ) { |
0 | 573 |
return false; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
574 |
} |
0 | 575 |
|
9 | 576 |
$blog = array_pop( $GLOBALS['_wp_switched_stack'] ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
577 |
$blog_id = get_current_blog_id(); |
0 | 578 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
579 |
if ( $blog_id == $blog ) { |
5 | 580 |
/** This filter is documented in wp-includes/ms-blogs.php */ |
0 | 581 |
do_action( 'switch_blog', $blog, $blog ); |
582 |
// If we still have items in the switched stack, consider ourselves still 'switched' |
|
583 |
$GLOBALS['switched'] = ! empty( $GLOBALS['_wp_switched_stack'] ); |
|
584 |
return true; |
|
585 |
} |
|
586 |
||
587 |
$wpdb->set_blog_id( $blog ); |
|
9 | 588 |
$prev_blog_id = $blog_id; |
589 |
$GLOBALS['blog_id'] = $blog; |
|
0 | 590 |
$GLOBALS['table_prefix'] = $wpdb->get_blog_prefix(); |
591 |
||
592 |
if ( function_exists( 'wp_cache_switch_to_blog' ) ) { |
|
593 |
wp_cache_switch_to_blog( $blog ); |
|
594 |
} else { |
|
595 |
global $wp_object_cache; |
|
596 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
597 |
if ( is_object( $wp_object_cache ) && isset( $wp_object_cache->global_groups ) ) { |
0 | 598 |
$global_groups = $wp_object_cache->global_groups; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
599 |
} else { |
0 | 600 |
$global_groups = false; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
601 |
} |
0 | 602 |
|
603 |
wp_cache_init(); |
|
604 |
||
605 |
if ( function_exists( 'wp_cache_add_global_groups' ) ) { |
|
5 | 606 |
if ( is_array( $global_groups ) ) { |
0 | 607 |
wp_cache_add_global_groups( $global_groups ); |
5 | 608 |
} else { |
9 | 609 |
wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'useremail', 'userslugs', 'site-transient', 'site-options', 'blog-lookup', 'blog-details', 'rss', 'global-posts', 'blog-id-cache', 'networks', 'sites', 'site-details', 'blog_meta' ) ); |
5 | 610 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
611 |
wp_cache_add_non_persistent_groups( array( 'counts', 'plugins' ) ); |
0 | 612 |
} |
613 |
} |
|
614 |
||
5 | 615 |
/** This filter is documented in wp-includes/ms-blogs.php */ |
0 | 616 |
do_action( 'switch_blog', $blog, $prev_blog_id ); |
617 |
||
618 |
// If we still have items in the switched stack, consider ourselves still 'switched' |
|
619 |
$GLOBALS['switched'] = ! empty( $GLOBALS['_wp_switched_stack'] ); |
|
620 |
||
621 |
return true; |
|
622 |
} |
|
623 |
||
624 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
625 |
* 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
|
626 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
627 |
* @since 4.9.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
628 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
629 |
* @param int $new_site_id New site ID. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
630 |
* @param int $old_site_id Old site ID. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
631 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
632 |
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
|
633 |
if ( $new_site_id == $old_site_id ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
634 |
return; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
635 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
636 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
637 |
if ( ! did_action( 'init' ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
638 |
return; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
639 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
640 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
641 |
wp_roles()->for_site( $new_site_id ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
642 |
wp_get_current_user()->for_site( $new_site_id ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
643 |
} |
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 |
/** |
0 | 646 |
* Determines if switch_to_blog() is in effect |
647 |
* |
|
648 |
* @since 3.5.0 |
|
649 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
650 |
* @global array $_wp_switched_stack |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
651 |
* |
0 | 652 |
* @return bool True if switched, false otherwise. |
653 |
*/ |
|
654 |
function ms_is_switched() { |
|
655 |
return ! empty( $GLOBALS['_wp_switched_stack'] ); |
|
656 |
} |
|
657 |
||
658 |
/** |
|
659 |
* Check if a particular blog is archived. |
|
660 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
661 |
* @since MU (3.0.0) |
0 | 662 |
* |
663 |
* @param int $id The blog id |
|
664 |
* @return string Whether the blog is archived or not |
|
665 |
*/ |
|
666 |
function is_archived( $id ) { |
|
9 | 667 |
return get_blog_status( $id, 'archived' ); |
0 | 668 |
} |
669 |
||
670 |
/** |
|
671 |
* Update the 'archived' status of a particular blog. |
|
672 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
673 |
* @since MU (3.0.0) |
0 | 674 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
675 |
* @param int $id The blog id |
0 | 676 |
* @param string $archived The new status |
677 |
* @return string $archived |
|
678 |
*/ |
|
679 |
function update_archived( $id, $archived ) { |
|
9 | 680 |
update_blog_status( $id, 'archived', $archived ); |
0 | 681 |
return $archived; |
682 |
} |
|
683 |
||
684 |
/** |
|
685 |
* Update a blog details field. |
|
686 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
687 |
* @since MU (3.0.0) |
9 | 688 |
* @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
|
689 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
690 |
* @global wpdb $wpdb WordPress database abstraction object. |
0 | 691 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
692 |
* @param int $blog_id BLog ID |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
693 |
* @param string $pref A field name |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
694 |
* @param string $value Value for $pref |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
695 |
* @param null $deprecated |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
696 |
* @return string|false $value |
0 | 697 |
*/ |
698 |
function update_blog_status( $blog_id, $pref, $value, $deprecated = null ) { |
|
699 |
global $wpdb; |
|
700 |
||
9 | 701 |
if ( null !== $deprecated ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
702 |
_deprecated_argument( __FUNCTION__, '3.1.0' ); |
9 | 703 |
} |
0 | 704 |
|
9 | 705 |
if ( ! in_array( $pref, array( 'site_id', 'domain', 'path', 'registered', 'last_updated', 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id' ) ) ) { |
706 |
return $value; |
|
707 |
} |
|
0 | 708 |
|
9 | 709 |
$result = wp_update_site( |
710 |
$blog_id, |
|
711 |
array( |
|
712 |
$pref => $value, |
|
713 |
) |
|
714 |
); |
|
715 |
||
716 |
if ( is_wp_error( $result ) ) { |
|
717 |
return false; |
|
5 | 718 |
} |
0 | 719 |
|
720 |
return $value; |
|
721 |
} |
|
722 |
||
723 |
/** |
|
724 |
* Get a blog details field. |
|
725 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
726 |
* @since MU (3.0.0) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
727 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
728 |
* @global wpdb $wpdb WordPress database abstraction object. |
0 | 729 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
730 |
* @param int $id The blog id |
0 | 731 |
* @param string $pref A field name |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
732 |
* @return bool|string|null $value |
0 | 733 |
*/ |
734 |
function get_blog_status( $id, $pref ) { |
|
735 |
global $wpdb; |
|
736 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
737 |
$details = get_site( $id ); |
9 | 738 |
if ( $details ) { |
0 | 739 |
return $details->$pref; |
9 | 740 |
} |
0 | 741 |
|
9 | 742 |
return $wpdb->get_var( $wpdb->prepare( "SELECT %s FROM {$wpdb->blogs} WHERE blog_id = %d", $pref, $id ) ); |
0 | 743 |
} |
744 |
||
745 |
/** |
|
746 |
* Get a list of most recently updated blogs. |
|
747 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
748 |
* @since MU (3.0.0) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
749 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
750 |
* @global wpdb $wpdb WordPress database abstraction object. |
0 | 751 |
* |
752 |
* @param mixed $deprecated Not used |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
753 |
* @param int $start The offset |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
754 |
* @param int $quantity The maximum number of blogs to retrieve. Default is 40. |
0 | 755 |
* @return array The list of blogs |
756 |
*/ |
|
757 |
function get_last_updated( $deprecated = '', $start = 0, $quantity = 40 ) { |
|
758 |
global $wpdb; |
|
759 |
||
9 | 760 |
if ( ! empty( $deprecated ) ) { |
0 | 761 |
_deprecated_argument( __FUNCTION__, 'MU' ); // never used |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
762 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
763 |
|
9 | 764 |
return $wpdb->get_results( $wpdb->prepare( "SELECT blog_id, domain, path FROM $wpdb->blogs WHERE site_id = %d AND public = '1' AND archived = '0' AND mature = '0' AND spam = '0' AND deleted = '0' AND last_updated != '0000-00-00 00:00:00' ORDER BY last_updated DESC limit %d, %d", get_current_network_id(), $start, $quantity ), ARRAY_A ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
765 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
766 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
767 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
768 |
* 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
|
769 |
* an already published post is changed. |
0 | 770 |
* |
771 |
* @since 3.3.0 |
|
772 |
* |
|
773 |
* @param string $new_status The new post status |
|
774 |
* @param string $old_status The old post status |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
775 |
* @param object $post Post object |
0 | 776 |
*/ |
777 |
function _update_blog_date_on_post_publish( $new_status, $old_status, $post ) { |
|
778 |
$post_type_obj = get_post_type_object( $post->post_type ); |
|
5 | 779 |
if ( ! $post_type_obj || ! $post_type_obj->public ) { |
0 | 780 |
return; |
5 | 781 |
} |
0 | 782 |
|
5 | 783 |
if ( 'publish' != $new_status && 'publish' != $old_status ) { |
0 | 784 |
return; |
5 | 785 |
} |
0 | 786 |
|
787 |
// Post was freshly published, published post was saved, or published post was unpublished. |
|
788 |
||
789 |
wpmu_update_blogs_date(); |
|
790 |
} |
|
791 |
||
792 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
793 |
* 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
|
794 |
* post is deleted. |
0 | 795 |
* |
796 |
* @since 3.4.0 |
|
797 |
* |
|
798 |
* @param int $post_id Post ID |
|
799 |
*/ |
|
800 |
function _update_blog_date_on_post_delete( $post_id ) { |
|
801 |
$post = get_post( $post_id ); |
|
802 |
||
803 |
$post_type_obj = get_post_type_object( $post->post_type ); |
|
5 | 804 |
if ( ! $post_type_obj || ! $post_type_obj->public ) { |
0 | 805 |
return; |
5 | 806 |
} |
0 | 807 |
|
5 | 808 |
if ( 'publish' != $post->post_status ) { |
0 | 809 |
return; |
5 | 810 |
} |
0 | 811 |
|
812 |
wpmu_update_blogs_date(); |
|
813 |
} |
|
814 |
||
5 | 815 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
816 |
* Handler for updating the current site's posts count when a post is deleted. |
5 | 817 |
* |
818 |
* @since 4.0.0 |
|
819 |
* |
|
820 |
* @param int $post_id Post ID. |
|
821 |
*/ |
|
822 |
function _update_posts_count_on_delete( $post_id ) { |
|
823 |
$post = get_post( $post_id ); |
|
824 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
825 |
if ( ! $post || 'publish' !== $post->post_status || 'post' !== $post->post_type ) { |
5 | 826 |
return; |
827 |
} |
|
828 |
||
829 |
update_posts_count(); |
|
830 |
} |
|
831 |
||
832 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
833 |
* Handler for updating the current site's posts count when a post status changes. |
5 | 834 |
* |
835 |
* @since 4.0.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
836 |
* @since 4.9.0 Added the `$post` parameter. |
5 | 837 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
838 |
* @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
|
839 |
* @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
|
840 |
* @param WP_Post $post Post object |
5 | 841 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
842 |
function _update_posts_count_on_transition_post_status( $new_status, $old_status, $post = null ) { |
5 | 843 |
if ( $new_status === $old_status ) { |
844 |
return; |
|
845 |
} |
|
846 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
847 |
if ( 'post' !== get_post_type( $post ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
848 |
return; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
849 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
850 |
|
5 | 851 |
if ( 'publish' !== $new_status && 'publish' !== $old_status ) { |
852 |
return; |
|
853 |
} |
|
854 |
||
855 |
update_posts_count(); |
|
856 |
} |