82 } |
82 } |
83 return esc_url( $url ); |
83 return esc_url( $url ); |
84 } |
84 } |
85 |
85 |
86 /** |
86 /** |
87 * Given a blog's (subdomain or directory) name, retrieve it's id. |
87 * Given a blog's (subdomain or directory) slug, retrieve it's id. |
88 * |
88 * |
89 * @since MU |
89 * @since MU |
90 * |
90 * |
91 * @param string $name |
91 * @param string $slug |
92 * @return int A blog id |
92 * @return int A blog id |
93 */ |
93 */ |
94 function get_id_from_blogname( $name ) { |
94 function get_id_from_blogname( $slug ) { |
95 global $wpdb, $current_site; |
95 global $wpdb, $current_site; |
96 $blog_id = wp_cache_get( 'get_id_from_blogname_' . $name, 'blog-details' ); |
96 |
|
97 $slug = trim( $slug, '/' ); |
|
98 |
|
99 $blog_id = wp_cache_get( 'get_id_from_blogname_' . $slug, 'blog-details' ); |
97 if ( $blog_id ) |
100 if ( $blog_id ) |
98 return $blog_id; |
101 return $blog_id; |
99 |
102 |
100 if ( is_subdomain_install() ) { |
103 if ( is_subdomain_install() ) { |
101 $domain = $name . '.' . $current_site->domain; |
104 $domain = $slug . '.' . $current_site->domain; |
102 $path = $current_site->path; |
105 $path = $current_site->path; |
103 } else { |
106 } else { |
104 $domain = $current_site->domain; |
107 $domain = $current_site->domain; |
105 $path = $current_site->path . $name . '/'; |
108 $path = $current_site->path . $slug . '/'; |
106 } |
109 } |
|
110 |
107 $blog_id = $wpdb->get_var( $wpdb->prepare("SELECT blog_id FROM {$wpdb->blogs} WHERE domain = %s AND path = %s", $domain, $path) ); |
111 $blog_id = $wpdb->get_var( $wpdb->prepare("SELECT blog_id FROM {$wpdb->blogs} WHERE domain = %s AND path = %s", $domain, $path) ); |
108 wp_cache_set( 'get_id_from_blogname_' . $name, $blog_id, 'blog-details' ); |
112 wp_cache_set( 'get_id_from_blogname_' . $slug, $blog_id, 'blog-details' ); |
109 return $blog_id; |
113 return $blog_id; |
110 } |
114 } |
111 |
115 |
112 /** |
116 /** |
113 * Retrieve the details for a blog from the blogs table and blog options. |
117 * Retrieve the details for a blog from the blogs table and blog options. |
114 * |
118 * |
115 * @since MU |
119 * @since MU |
116 * |
120 * |
117 * @param int|string|array $fields A blog ID, a blog name, or an array of fields to query against. |
121 * @param int|string|array $fields A blog ID, a blog slug, or an array of fields to query against. Optional. If not specified the current blog ID is used. |
118 * @param bool $get_all Whether to retrieve all details or only the details in the blogs table. Default is true. |
122 * @param bool $get_all Whether to retrieve all details or only the details in the blogs table. Default is true. |
119 * @return object Blog details. |
123 * @return object Blog details. |
120 */ |
124 */ |
121 function get_blog_details( $fields, $get_all = true ) { |
125 function get_blog_details( $fields = null, $get_all = true ) { |
122 global $wpdb; |
126 global $wpdb; |
123 |
127 |
124 if ( is_array($fields ) ) { |
128 if ( is_array($fields ) ) { |
125 if ( isset($fields['blog_id']) ) { |
129 if ( isset($fields['blog_id']) ) { |
126 $blog_id = $fields['blog_id']; |
130 $blog_id = $fields['blog_id']; |
286 $update_details = array(); |
290 $update_details = array(); |
287 $fields = array( 'site_id', 'domain', 'path', 'registered', 'last_updated', 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id'); |
291 $fields = array( 'site_id', 'domain', 'path', 'registered', 'last_updated', 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id'); |
288 foreach ( array_intersect( array_keys( $details ), $fields ) as $field ) |
292 foreach ( array_intersect( array_keys( $details ), $fields ) as $field ) |
289 $update_details[$field] = $details[$field]; |
293 $update_details[$field] = $details[$field]; |
290 |
294 |
291 $wpdb->update( $wpdb->blogs, $update_details, array('blog_id' => $blog_id) ); |
295 $result = $wpdb->update( $wpdb->blogs, $update_details, array('blog_id' => $blog_id) ); |
|
296 |
|
297 if ( false === $result ) |
|
298 return false; |
292 |
299 |
293 // If spam status changed, issue actions. |
300 // If spam status changed, issue actions. |
294 if ( $details[ 'spam' ] != $current_details[ 'spam' ] ) { |
301 if ( $details[ 'spam' ] != $current_details[ 'spam' ] ) { |
295 if ( $details[ 'spam' ] == 1 ) |
302 if ( $details[ 'spam' ] == 1 ) |
296 do_action( "make_spam_blog", $blog_id ); |
303 do_action( 'make_spam_blog', $blog_id ); |
297 else |
304 else |
298 do_action( "make_ham_blog", $blog_id ); |
305 do_action( 'make_ham_blog', $blog_id ); |
299 } |
306 } |
300 |
307 |
301 if ( isset($details[ 'public' ]) ) |
308 // If mature status changed, issue actions. |
302 update_blog_option( $blog_id, 'blog_public', $details[ 'public' ] ); |
309 if ( $details[ 'mature' ] != $current_details[ 'mature' ] ) { |
|
310 if ( $details[ 'mature' ] == 1 ) |
|
311 do_action( 'mature_blog', $blog_id ); |
|
312 else |
|
313 do_action( 'unmature_blog', $blog_id ); |
|
314 } |
|
315 |
|
316 // If archived status changed, issue actions. |
|
317 if ( $details[ 'archived' ] != $current_details[ 'archived' ] ) { |
|
318 if ( $details[ 'archived' ] == 1 ) |
|
319 do_action( 'archive_blog', $blog_id ); |
|
320 else |
|
321 do_action( 'unarchive_blog', $blog_id ); |
|
322 } |
|
323 |
|
324 // If deleted status changed, issue actions. |
|
325 if ( $details[ 'deleted' ] != $current_details[ 'deleted' ] ) { |
|
326 if ( $details[ 'deleted' ] == 1 ) |
|
327 do_action( 'make_delete_blog', $blog_id ); |
|
328 else |
|
329 do_action( 'make_undelete_blog', $blog_id ); |
|
330 } |
|
331 |
|
332 if ( isset( $details[ 'public' ] ) ) { |
|
333 switch_to_blog( $blog_id ); |
|
334 update_option( 'blog_public', $details[ 'public' ] ); |
|
335 restore_current_blog(); |
|
336 } |
303 |
337 |
304 refresh_blog_details($blog_id); |
338 refresh_blog_details($blog_id); |
305 |
339 |
306 return true; |
340 return true; |
307 } |
341 } |
308 |
342 |
309 /** |
343 /** |
310 * Retrieve option value based on setting name and blog_id. |
344 * Clean the blog cache |
|
345 * |
|
346 * @since 3.5.0 |
|
347 * |
|
348 * @param stdClass $blog The blog details as returned from get_blog_details() |
|
349 */ |
|
350 function clean_blog_cache( $blog ) { |
|
351 $blog_id = $blog->blog_id; |
|
352 $domain_path_key = md5( $blog->domain . $blog->path ); |
|
353 |
|
354 wp_cache_delete( $blog_id , 'blog-details' ); |
|
355 wp_cache_delete( $blog_id . 'short' , 'blog-details' ); |
|
356 wp_cache_delete( $domain_path_key, 'blog-lookup' ); |
|
357 wp_cache_delete( 'current_blog_' . $blog->domain, 'site-options' ); |
|
358 wp_cache_delete( 'current_blog_' . $blog->domain . $blog->path, 'site-options' ); |
|
359 wp_cache_delete( 'get_id_from_blogname_' . trim( $blog->path, '/' ), 'blog-details' ); |
|
360 wp_cache_delete( $domain_path_key, 'blog-id-cache' ); |
|
361 } |
|
362 |
|
363 /** |
|
364 * Retrieve option value for a given blog id based on name of option. |
311 * |
365 * |
312 * If the option does not exist or does not have a value, then the return value |
366 * If the option does not exist or does not have a value, then the return value |
313 * will be false. This is useful to check whether you need to install an option |
367 * will be false. This is useful to check whether you need to install an option |
314 * and is commonly used during installation of plugin options and to test |
368 * and is commonly used during installation of plugin options and to test |
315 * whether upgrading is required. |
369 * whether upgrading is required. |
316 * |
370 * |
317 * There is a filter called 'blog_option_$option' with the $option being |
371 * If the option was serialized then it will be unserialized when it is returned. |
318 * replaced with the option name. The filter takes two parameters. $value and |
372 * |
319 * $blog_id. It returns $value. |
373 * @since MU |
320 * The 'option_$option' filter in get_option() is not called. |
374 * |
321 * |
375 * @param int $id A blog ID. Can be null to refer to the current blog. |
322 * @since MU |
376 * @param string $option Name of option to retrieve. Expected to not be SQL-escaped. |
323 * @uses apply_filters() Calls 'blog_option_$optionname' with the option name value. |
377 * @param mixed $default Optional. Default value to return if the option does not exist. |
324 * |
|
325 * @param int $blog_id Optional. Blog ID, can be null to refer to the current blog. |
|
326 * @param string $setting Name of option to retrieve. Should already be SQL-escaped. |
|
327 * @param string $default (optional) Default value returned if option not found. |
|
328 * @return mixed Value set for the option. |
378 * @return mixed Value set for the option. |
329 */ |
379 */ |
330 function get_blog_option( $blog_id, $setting, $default = false ) { |
380 function get_blog_option( $id, $option, $default = false ) { |
331 global $wpdb; |
381 $id = (int) $id; |
332 |
382 |
333 if ( null === $blog_id ) |
383 if ( empty( $id ) ) |
334 $blog_id = $wpdb->blogid; |
384 $id = get_current_blog_id(); |
335 |
385 |
336 $key = $blog_id . '-' . $setting . '-blog_option'; |
386 if ( get_current_blog_id() == $id ) |
337 $value = wp_cache_get( $key, 'site-options' ); |
387 return get_option( $option, $default ); |
338 if ( $value == null ) { |
388 |
339 if ( $blog_id == $wpdb->blogid ) { |
389 switch_to_blog( $id ); |
340 $value = get_option( $setting, $default ); |
390 $value = get_option( $option, $default ); |
341 $notoptions = wp_cache_get( 'notoptions', 'options' ); |
391 restore_current_blog(); |
342 if ( isset( $notoptions[$setting] ) ) { |
392 |
343 wp_cache_set( $key, 'noop', 'site-options' ); |
393 return apply_filters( 'blog_option_' . $option, $value, $id ); |
344 $value = $default; |
394 } |
345 } elseif ( $value == false ) { |
395 |
346 wp_cache_set( $key, 'falsevalue', 'site-options' ); |
396 /** |
347 } else { |
397 * Add a new option for a given blog id. |
348 wp_cache_set( $key, $value, 'site-options' ); |
398 * |
349 } |
399 * You do not need to serialize values. If the value needs to be serialized, then |
350 return apply_filters( 'blog_option_' . $setting, $value, $blog_id ); |
400 * it will be serialized before it is inserted into the database. Remember, |
351 } else { |
401 * resources can not be serialized or added as an option. |
352 $blog_prefix = $wpdb->get_blog_prefix( $blog_id ); |
402 * |
353 $row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$blog_prefix}options WHERE option_name = %s", $setting ) ); |
403 * You can create options without values and then update the values later. |
354 if ( is_object( $row ) ) { // Has to be get_row instead of get_var because of funkiness with 0, false, null values |
404 * Existing options will not be updated and checks are performed to ensure that you |
355 $value = $row->option_value; |
405 * aren't adding a protected WordPress option. Care should be taken to not name |
356 if ( $value == false ) |
406 * options the same as the ones which are protected. |
357 wp_cache_set( $key, 'falsevalue', 'site-options' ); |
407 * |
358 else |
408 * @since MU |
359 wp_cache_set( $key, $value, 'site-options' ); |
409 * |
360 } else { // option does not exist, so we must cache its non-existence |
410 * @param int $id A blog ID. Can be null to refer to the current blog. |
361 wp_cache_set( $key, 'noop', 'site-options' ); |
411 * @param string $option Name of option to add. Expected to not be SQL-escaped. |
362 $value = $default; |
412 * @param mixed $value Optional. Option value, can be anything. Expected to not be SQL-escaped. |
363 } |
413 * @return bool False if option was not added and true if option was added. |
364 } |
414 */ |
365 } elseif ( $value == 'noop' ) { |
415 function add_blog_option( $id, $option, $value ) { |
366 $value = $default; |
416 $id = (int) $id; |
367 } elseif ( $value == 'falsevalue' ) { |
417 |
368 $value = false; |
418 if ( empty( $id ) ) |
369 } |
419 $id = get_current_blog_id(); |
370 // If home is not set use siteurl. |
420 |
371 if ( 'home' == $setting && '' == $value ) |
421 if ( get_current_blog_id() == $id ) |
372 return get_blog_option( $blog_id, 'siteurl' ); |
422 return add_option( $option, $value ); |
373 |
423 |
374 if ( 'siteurl' == $setting || 'home' == $setting || 'category_base' == $setting ) |
424 switch_to_blog( $id ); |
375 $value = untrailingslashit( $value ); |
425 $return = add_option( $option, $value ); |
376 |
426 restore_current_blog(); |
377 return apply_filters( 'blog_option_' . $setting, maybe_unserialize( $value ), $blog_id ); |
427 |
378 } |
428 return $return; |
379 |
429 } |
380 /** |
430 |
381 * Add an option for a particular blog. |
431 /** |
|
432 * Removes option by name for a given blog id. Prevents removal of protected WordPress options. |
|
433 * |
|
434 * @since MU |
|
435 * |
|
436 * @param int $id A blog ID. Can be null to refer to the current blog. |
|
437 * @param string $option Name of option to remove. Expected to not be SQL-escaped. |
|
438 * @return bool True, if option is successfully deleted. False on failure. |
|
439 */ |
|
440 function delete_blog_option( $id, $option ) { |
|
441 $id = (int) $id; |
|
442 |
|
443 if ( empty( $id ) ) |
|
444 $id = get_current_blog_id(); |
|
445 |
|
446 if ( get_current_blog_id() == $id ) |
|
447 return delete_option( $option ); |
|
448 |
|
449 switch_to_blog( $id ); |
|
450 $return = delete_option( $option ); |
|
451 restore_current_blog(); |
|
452 |
|
453 return $return; |
|
454 } |
|
455 |
|
456 /** |
|
457 * Update an option for a particular blog. |
382 * |
458 * |
383 * @since MU |
459 * @since MU |
384 * |
460 * |
385 * @param int $id The blog id |
461 * @param int $id The blog id |
386 * @param string $key The option key |
462 * @param string $option The option key |
387 * @param mixed $value The option value |
|
388 * @return bool True on success, false on failure. |
|
389 */ |
|
390 function add_blog_option( $id, $key, $value ) { |
|
391 $id = (int) $id; |
|
392 |
|
393 switch_to_blog($id); |
|
394 $return = add_option( $key, $value ); |
|
395 restore_current_blog(); |
|
396 if ( $return ) |
|
397 wp_cache_set( $id . '-' . $key . '-blog_option', $value, 'site-options' ); |
|
398 return $return; |
|
399 } |
|
400 |
|
401 /** |
|
402 * Delete an option for a particular blog. |
|
403 * |
|
404 * @since MU |
|
405 * |
|
406 * @param int $id The blog id |
|
407 * @param string $key The option key |
|
408 * @return bool True on success, false on failure. |
|
409 */ |
|
410 function delete_blog_option( $id, $key ) { |
|
411 $id = (int) $id; |
|
412 |
|
413 switch_to_blog($id); |
|
414 $return = delete_option( $key ); |
|
415 restore_current_blog(); |
|
416 if ( $return ) |
|
417 wp_cache_set( $id . '-' . $key . '-blog_option', '', 'site-options' ); |
|
418 return $return; |
|
419 } |
|
420 |
|
421 /** |
|
422 * Update an option for a particular blog. |
|
423 * |
|
424 * @since MU |
|
425 * |
|
426 * @param int $id The blog id |
|
427 * @param string $key The option key |
|
428 * @param mixed $value The option value |
463 * @param mixed $value The option value |
429 * @return bool True on success, false on failrue. |
464 * @return bool True on success, false on failrue. |
430 */ |
465 */ |
431 function update_blog_option( $id, $key, $value, $deprecated = null ) { |
466 function update_blog_option( $id, $option, $value, $deprecated = null ) { |
432 $id = (int) $id; |
467 $id = (int) $id; |
433 |
468 |
434 if ( null !== $deprecated ) |
469 if ( null !== $deprecated ) |
435 _deprecated_argument( __FUNCTION__, '3.1' ); |
470 _deprecated_argument( __FUNCTION__, '3.1' ); |
436 |
471 |
437 switch_to_blog($id); |
472 if ( get_current_blog_id() == $id ) |
438 $return = update_option( $key, $value ); |
473 return update_option( $option, $value ); |
|
474 |
|
475 switch_to_blog( $id ); |
|
476 $return = update_option( $option, $value ); |
439 restore_current_blog(); |
477 restore_current_blog(); |
440 |
478 |
441 refresh_blog_details( $id ); |
479 refresh_blog_details( $id ); |
442 |
480 |
443 if ( $return ) |
|
444 wp_cache_set( $id . '-' . $key . '-blog_option', $value, 'site-options'); |
|
445 return $return; |
481 return $return; |
446 } |
482 } |
447 |
483 |
448 /** |
484 /** |
449 * Switch the current blog. |
485 * Switch the current blog. |
457 * |
493 * |
458 * @see restore_current_blog() |
494 * @see restore_current_blog() |
459 * @since MU |
495 * @since MU |
460 * |
496 * |
461 * @param int $new_blog The id of the blog you want to switch to. Default: current blog |
497 * @param int $new_blog The id of the blog you want to switch to. Default: current blog |
462 * @param bool $validate Whether to check if $new_blog exists before proceeding |
498 * @param bool $deprecated Deprecated argument |
463 * @return bool True on success, False if the validation failed |
499 * @return bool True on success, false if the validation failed |
464 */ |
500 */ |
465 function switch_to_blog( $new_blog, $validate = false ) { |
501 function switch_to_blog( $new_blog, $deprecated = null ) { |
466 global $wpdb, $table_prefix, $blog_id, $switched, $switched_stack, $wp_roles, $wp_object_cache; |
502 global $wpdb, $wp_roles; |
467 |
503 |
468 if ( empty($new_blog) ) |
504 if ( empty( $new_blog ) ) |
469 $new_blog = $blog_id; |
505 $new_blog = $GLOBALS['blog_id']; |
470 |
506 |
471 if ( $validate && ! get_blog_details( $new_blog ) ) |
507 $GLOBALS['_wp_switched_stack'][] = $GLOBALS['blog_id']; |
472 return false; |
|
473 |
|
474 if ( empty($switched_stack) ) |
|
475 $switched_stack = array(); |
|
476 |
|
477 $switched_stack[] = $blog_id; |
|
478 |
508 |
479 /* If we're switching to the same blog id that we're on, |
509 /* If we're switching to the same blog id that we're on, |
480 * set the right vars, do the associated actions, but skip |
510 * set the right vars, do the associated actions, but skip |
481 * the extra unnecessary work */ |
511 * the extra unnecessary work */ |
482 if ( $blog_id == $new_blog ) { |
512 if ( $new_blog == $GLOBALS['blog_id'] ) { |
483 do_action( 'switch_blog', $blog_id, $blog_id ); |
513 do_action( 'switch_blog', $new_blog, $new_blog ); |
484 $switched = true; |
514 $GLOBALS['switched'] = true; |
485 return true; |
515 return true; |
486 } |
516 } |
487 |
517 |
488 $wpdb->set_blog_id($new_blog); |
518 $wpdb->set_blog_id( $new_blog ); |
489 $table_prefix = $wpdb->prefix; |
519 $GLOBALS['table_prefix'] = $wpdb->prefix; |
490 $prev_blog_id = $blog_id; |
520 $prev_blog_id = $GLOBALS['blog_id']; |
491 $blog_id = $new_blog; |
521 $GLOBALS['blog_id'] = $new_blog; |
492 |
522 |
493 if ( is_object( $wp_roles ) ) { |
523 if ( function_exists( 'wp_cache_switch_to_blog' ) ) { |
494 $wpdb->suppress_errors(); |
524 wp_cache_switch_to_blog( $new_blog ); |
495 if ( method_exists( $wp_roles ,'_init' ) ) |
525 } else { |
496 $wp_roles->_init(); |
526 global $wp_object_cache; |
497 elseif ( method_exists( $wp_roles, '__construct' ) ) |
527 |
498 $wp_roles->__construct(); |
528 if ( is_object( $wp_object_cache ) && isset( $wp_object_cache->global_groups ) ) |
499 $wpdb->suppress_errors( false ); |
529 $global_groups = $wp_object_cache->global_groups; |
500 } |
530 else |
501 |
531 $global_groups = false; |
502 if ( did_action('init') ) { |
532 |
|
533 wp_cache_init(); |
|
534 |
|
535 if ( function_exists( 'wp_cache_add_global_groups' ) ) { |
|
536 if ( is_array( $global_groups ) ) |
|
537 wp_cache_add_global_groups( $global_groups ); |
|
538 else |
|
539 wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'site-transient', 'site-options', 'site-lookup', 'blog-lookup', 'blog-details', 'rss', 'global-posts', ' blog-id-cache' ) ); |
|
540 wp_cache_add_non_persistent_groups( array( 'comment', 'counts', 'plugins' ) ); |
|
541 } |
|
542 } |
|
543 |
|
544 if ( did_action( 'init' ) ) { |
|
545 $wp_roles->reinit(); |
503 $current_user = wp_get_current_user(); |
546 $current_user = wp_get_current_user(); |
504 if ( is_object( $current_user ) ) |
547 $current_user->for_blog( $new_blog ); |
505 $current_user->for_blog( $blog_id ); |
548 } |
506 } |
549 |
507 |
550 do_action( 'switch_blog', $new_blog, $prev_blog_id ); |
508 if ( is_object( $wp_object_cache ) && isset( $wp_object_cache->global_groups ) ) |
551 $GLOBALS['switched'] = true; |
509 $global_groups = $wp_object_cache->global_groups; |
552 |
510 else |
553 return true; |
511 $global_groups = false; |
554 } |
512 |
555 |
513 wp_cache_init(); |
556 /** |
514 if ( function_exists('wp_cache_add_global_groups') ) { |
557 * Restore the current blog, after calling switch_to_blog() |
515 if ( is_array( $global_groups ) ) |
558 * |
516 wp_cache_add_global_groups( $global_groups ); |
559 * @see switch_to_blog() |
|
560 * @since MU |
|
561 * |
|
562 * @return bool True on success, false if we're already on the current blog |
|
563 */ |
|
564 function restore_current_blog() { |
|
565 global $wpdb, $wp_roles; |
|
566 |
|
567 if ( empty( $GLOBALS['_wp_switched_stack'] ) ) |
|
568 return false; |
|
569 |
|
570 $blog = array_pop( $GLOBALS['_wp_switched_stack'] ); |
|
571 |
|
572 if ( $GLOBALS['blog_id'] == $blog ) { |
|
573 do_action( 'switch_blog', $blog, $blog ); |
|
574 // If we still have items in the switched stack, consider ourselves still 'switched' |
|
575 $GLOBALS['switched'] = ! empty( $GLOBALS['_wp_switched_stack'] ); |
|
576 return true; |
|
577 } |
|
578 |
|
579 $wpdb->set_blog_id( $blog ); |
|
580 $prev_blog_id = $GLOBALS['blog_id']; |
|
581 $GLOBALS['blog_id'] = $blog; |
|
582 $GLOBALS['table_prefix'] = $wpdb->prefix; |
|
583 |
|
584 if ( function_exists( 'wp_cache_switch_to_blog' ) ) { |
|
585 wp_cache_switch_to_blog( $blog ); |
|
586 } else { |
|
587 global $wp_object_cache; |
|
588 |
|
589 if ( is_object( $wp_object_cache ) && isset( $wp_object_cache->global_groups ) ) |
|
590 $global_groups = $wp_object_cache->global_groups; |
517 else |
591 else |
518 wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'site-transient', 'site-options', 'site-lookup', 'blog-lookup', 'blog-details', 'rss', 'global-posts' ) ); |
592 $global_groups = false; |
519 wp_cache_add_non_persistent_groups(array( 'comment', 'counts', 'plugins' )); |
593 |
520 } |
594 wp_cache_init(); |
521 |
595 |
522 do_action('switch_blog', $blog_id, $prev_blog_id); |
596 if ( function_exists( 'wp_cache_add_global_groups' ) ) { |
523 $switched = true; |
597 if ( is_array( $global_groups ) ) |
|
598 wp_cache_add_global_groups( $global_groups ); |
|
599 else |
|
600 wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'site-transient', 'site-options', 'site-lookup', 'blog-lookup', 'blog-details', 'rss', 'global-posts', ' blog-id-cache' ) ); |
|
601 wp_cache_add_non_persistent_groups( array( 'comment', 'counts', 'plugins' ) ); |
|
602 } |
|
603 } |
|
604 |
|
605 if ( did_action( 'init' ) ) { |
|
606 $wp_roles->reinit(); |
|
607 $current_user = wp_get_current_user(); |
|
608 $current_user->for_blog( $blog ); |
|
609 } |
|
610 |
|
611 do_action( 'switch_blog', $blog, $prev_blog_id ); |
|
612 |
|
613 // If we still have items in the switched stack, consider ourselves still 'switched' |
|
614 $GLOBALS['switched'] = ! empty( $GLOBALS['_wp_switched_stack'] ); |
|
615 |
524 return true; |
616 return true; |
525 } |
617 } |
526 |
618 |
527 /** |
619 /** |
528 * Restore the current blog, after calling switch_to_blog() |
620 * Determines if switch_to_blog() is in effect |
529 * |
621 * |
530 * @see switch_to_blog() |
622 * @since 3.5.0 |
531 * @since MU |
623 * |
532 * |
624 * @return bool True if switched, false otherwise. |
533 * @return bool True on success, False if we're already on the current blog |
625 */ |
534 */ |
626 function ms_is_switched() { |
535 function restore_current_blog() { |
627 return ! empty( $GLOBALS['_wp_switched_stack'] ); |
536 global $table_prefix, $wpdb, $blog_id, $switched, $switched_stack, $wp_roles, $wp_object_cache; |
|
537 |
|
538 if ( !$switched ) |
|
539 return false; |
|
540 |
|
541 if ( !is_array( $switched_stack ) ) |
|
542 return false; |
|
543 |
|
544 $blog = array_pop( $switched_stack ); |
|
545 if ( $blog_id == $blog ) { |
|
546 do_action( 'switch_blog', $blog, $blog ); |
|
547 /* If we still have items in the switched stack, consider ourselves still 'switched' */ |
|
548 $switched = ( is_array( $switched_stack ) && count( $switched_stack ) > 0 ); |
|
549 return true; |
|
550 } |
|
551 |
|
552 $wpdb->set_blog_id($blog); |
|
553 $prev_blog_id = $blog_id; |
|
554 $blog_id = $blog; |
|
555 $table_prefix = $wpdb->prefix; |
|
556 |
|
557 if ( is_object( $wp_roles ) ) { |
|
558 $wpdb->suppress_errors(); |
|
559 if ( method_exists( $wp_roles ,'_init' ) ) |
|
560 $wp_roles->_init(); |
|
561 elseif ( method_exists( $wp_roles, '__construct' ) ) |
|
562 $wp_roles->__construct(); |
|
563 $wpdb->suppress_errors( false ); |
|
564 } |
|
565 |
|
566 if ( did_action('init') ) { |
|
567 $current_user = wp_get_current_user(); |
|
568 if ( is_object( $current_user ) ) |
|
569 $current_user->for_blog( $blog_id ); |
|
570 } |
|
571 |
|
572 if ( is_object( $wp_object_cache ) && isset( $wp_object_cache->global_groups ) ) |
|
573 $global_groups = $wp_object_cache->global_groups; |
|
574 else |
|
575 $global_groups = false; |
|
576 |
|
577 wp_cache_init(); |
|
578 if ( function_exists('wp_cache_add_global_groups') ) { |
|
579 if ( is_array( $global_groups ) ) |
|
580 wp_cache_add_global_groups( $global_groups ); |
|
581 else |
|
582 wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'site-transient', 'site-options', 'site-lookup', 'blog-lookup', 'blog-details', 'rss', 'global-posts' ) ); |
|
583 wp_cache_add_non_persistent_groups(array( 'comment', 'counts', 'plugins' )); |
|
584 } |
|
585 |
|
586 do_action('switch_blog', $blog_id, $prev_blog_id); |
|
587 |
|
588 /* If we still have items in the switched stack, consider ourselves still 'switched' */ |
|
589 $switched = ( is_array( $switched_stack ) && count( $switched_stack ) > 0 ); |
|
590 return true; |
|
591 } |
628 } |
592 |
629 |
593 /** |
630 /** |
594 * Check if a particular blog is archived. |
631 * Check if a particular blog is archived. |
595 * |
632 * |
630 global $wpdb; |
667 global $wpdb; |
631 |
668 |
632 if ( null !== $deprecated ) |
669 if ( null !== $deprecated ) |
633 _deprecated_argument( __FUNCTION__, '3.1' ); |
670 _deprecated_argument( __FUNCTION__, '3.1' ); |
634 |
671 |
635 if ( !in_array( $pref, array( 'site_id', 'domain', 'path', 'registered', 'last_updated', 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id') ) ) |
672 if ( ! in_array( $pref, array( 'site_id', 'domain', 'path', 'registered', 'last_updated', 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id') ) ) |
636 return $value; |
673 return $value; |
637 |
674 |
638 $wpdb->update( $wpdb->blogs, array($pref => $value, 'last_updated' => current_time('mysql', true)), array('blog_id' => $blog_id) ); |
675 $result = $wpdb->update( $wpdb->blogs, array($pref => $value, 'last_updated' => current_time('mysql', true)), array('blog_id' => $blog_id) ); |
639 |
676 |
640 refresh_blog_details($blog_id); |
677 if ( false === $result ) |
|
678 return false; |
|
679 |
|
680 refresh_blog_details( $blog_id ); |
641 |
681 |
642 if ( 'spam' == $pref ) |
682 if ( 'spam' == $pref ) |
643 ( $value == 1 ) ? do_action( 'make_spam_blog', $blog_id ) : do_action( 'make_ham_blog', $blog_id ); |
683 ( $value == 1 ) ? do_action( 'make_spam_blog', $blog_id ) : do_action( 'make_ham_blog', $blog_id ); |
644 elseif ( 'mature' == $pref ) |
684 elseif ( 'mature' == $pref ) |
645 ( $value == 1 ) ? do_action( 'mature_blog', $blog_id ) : do_action( 'unmature_blog', $blog_id ); |
685 ( $value == 1 ) ? do_action( 'mature_blog', $blog_id ) : do_action( 'unmature_blog', $blog_id ); |
646 elseif ( 'archived' == $pref ) |
686 elseif ( 'archived' == $pref ) |
647 ( $value == 1 ) ? do_action( 'archive_blog', $blog_id ) : do_action( 'unarchive_blog', $blog_id ); |
687 ( $value == 1 ) ? do_action( 'archive_blog', $blog_id ) : do_action( 'unarchive_blog', $blog_id ); |
648 elseif ( 'archived' == $pref ) |
688 elseif ( 'deleted' == $pref ) |
649 ( $value == 1 ) ? do_action( 'archive_blog', $blog_id ) : do_action( 'unarchive_blog', $blog_id ); |
689 ( $value == 1 ) ? do_action( 'make_delete_blog', $blog_id ) : do_action( 'make_undelete_blog', $blog_id ); |
650 |
690 |
651 return $value; |
691 return $value; |
652 } |
692 } |
653 |
693 |
654 /** |
694 /** |