Ticket #25293: 25293.2.diff
File 25293.2.diff, 11.9 KB (added by , 8 years ago) |
---|
-
src/wp-includes/class-wp-state.php
1 <?php 2 /** 3 * State API: WP_State class 4 * 5 * @package WordPress 6 * @subpackage Multisite 7 * @since 4.7.0 8 */ 9 10 /** 11 * Core class used for handling switched state of the current site and network. 12 * 13 * @since 4.7.0 14 */ 15 class WP_State { 16 /** 17 * Stack of the switched sites. 18 * 19 * @since 4.7.0 20 * @access private 21 * @var array 22 */ 23 private $switched_stack = array(); 24 25 /** 26 * Switches the current site. 27 * 28 * This function is useful if you need to pull posts, or other information, 29 * from other sites. You can switch back afterwards using WP_State::restore_current_site(). 30 * 31 * If the target site is part of a different network, the network is switched as well. 32 * 33 * Things that aren't switched: 34 * - autoloaded options. See #14992 35 * - plugins. See #14941 36 * 37 * @see WP_State::restore_current_site() 38 * 39 * @since 4.7.0 40 * @access public 41 * 42 * @global WP_Site $current_blog 43 * 44 * @param int $new_site The id of the site you want to switch to. Default: current site 45 * @return true Always returns true. 46 */ 47 public function switch_to_site( $new_site ) { 48 $old_site = $GLOBALS['current_blog']; 49 $new_site = get_site( $new_site ); 50 51 $this->switched_stack[] = $old_site; 52 53 return $this->perform_switch( $new_site, $old_site ); 54 } 55 56 /** 57 * Restores the current site, after calling WP_State::switch_to_site(). 58 * 59 * If the current site is part of a different network, the network is restored as well. 60 * 61 * @see WP_State::switch_to_site() 62 * 63 * @since 4.7.0 64 * @access public 65 * 66 * @global WP_Site $current_blog 67 * 68 * @return bool True on success, false if we're already on the current site. 69 */ 70 public function restore_current_site() { 71 if ( empty( $this->switched_stack ) ) { 72 return false; 73 } 74 75 $old_site = $GLOBALS['current_blog']; 76 $new_site = array_pop( $this->switched_stack ); 77 78 return $this->perform_switch( $new_site, $old_site ); 79 } 80 81 /** 82 * Determines if WP_State::switch_to_site() is in effect 83 * 84 * @since 4.7.0 85 * @access public 86 * 87 * @return bool True if switched, false otherwise. 88 */ 89 public function is_switched() { 90 return ! empty( $this->switched_stack ); 91 } 92 93 /** 94 * Switched the current state from one site to another. 95 * 96 * @since 4.7.0 97 * @access private 98 * 99 * @global WP_Site $current_blog 100 * @global WP_Network $current_site 101 * @global bool $switched 102 * 103 * @param WP_Site $new_site The site to switch to. 104 * @param WP_Site $old_site The site to switch from. 105 * @return true Always returns true. 106 */ 107 private function perform_switch( $new_site, $old_site ) { 108 /* 109 * If we're switching to the same site that we're on, 110 * set the right vars, do the associated actions, but skip 111 * the extra unnecessary work. 112 */ 113 if ( $new_site->id === $old_site->id ) { 114 /** 115 * Fires when the site is switched. 116 * 117 * @since MU 118 * 119 * @param int $new_site_id New site ID. 120 * @param int $old_site_id Old site ID. 121 */ 122 do_action( 'switch_blog', $new_site->id, $new_site->id ); 123 $GLOBALS['switched'] = ! empty( $this->switched_stack ); 124 125 return true; 126 } 127 128 $GLOBALS['current_blog'] = $new_site; 129 130 if ( $new_site->network_id !== $old_site->network_id ) { 131 $GLOBALS['current_site'] = get_network( $new_site->network_id ); 132 } 133 134 $this->initialize_after_switch(); 135 136 /* This filter is documented in wp-includes/class-wp-state.php */ 137 do_action( 'switch_blog', $new_site->id, $old_site->id ); 138 $GLOBALS['switched'] = ! empty( $this->switched_stack ); 139 140 return true; 141 } 142 143 /** 144 * Reinitializes the necessary parts of WordPress after the current site has been switched. 145 * 146 * @since 4.7.0 147 * @access private 148 * 149 * @global wpdb $wpdb 150 * @global WP_Site $current_blog 151 * @global WP_Network $current_site 152 * @global int $blog_id 153 * @global string $table_prefix 154 * @global WP_Object_Cache $wp_object_cache 155 */ 156 private function initialize_after_switch() { 157 global $wpdb, $current_blog, $current_site, $blog_id, $table_prefix; 158 159 $wpdb->set_blog_id( $current_blog->id, $current_site->id ); 160 $table_prefix = $wpdb->get_blog_prefix(); 161 $blog_id = $current_blog->id; 162 163 if ( function_exists( 'wp_cache_switch_to_blog' ) ) { 164 wp_cache_switch_to_blog( $blog_id ); 165 } else { 166 global $wp_object_cache; 167 168 if ( is_object( $wp_object_cache ) && isset( $wp_object_cache->global_groups ) ) { 169 $global_groups = $wp_object_cache->global_groups; 170 } else { 171 $global_groups = false; 172 } 173 174 wp_cache_init(); 175 176 if ( function_exists( 'wp_cache_add_global_groups' ) ) { 177 if ( is_array( $global_groups ) ) { 178 wp_cache_add_global_groups( $global_groups ); 179 } else { 180 wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'useremail', 'userslugs', 'site-transient', 'site-options', 'site-lookup', 'blog-lookup', 'blog-details', 'rss', 'global-posts', 'blog-id-cache', 'networks', 'sites', 'site-details' ) ); 181 } 182 wp_cache_add_non_persistent_groups( array( 'counts', 'plugins' ) ); 183 } 184 } 185 186 if ( did_action( 'init' ) ) { 187 wp_roles()->reinit(); 188 $current_user = wp_get_current_user(); 189 $current_user->for_blog( $blog_id ); 190 } 191 } 192 } -
src/wp-includes/ms-blogs.php
752 752 * @see restore_current_blog() 753 753 * @since MU 754 754 * 755 * @global wpdb $wpdb 756 * @global int $blog_id 757 * @global array $_wp_switched_stack 758 * @global bool $switched 759 * @global string $table_prefix 760 * @global WP_Object_Cache $wp_object_cache 755 * @global WP_State $wp_state 761 756 * 762 * @param int $new_blog The idof the blog you want to switch to. Default: current blog757 * @param int|WP_Site $new_blog The id or object of the blog you want to switch to. Default: current blog 763 758 * @param bool $deprecated Deprecated argument 764 759 * @return true Always returns True. 765 760 */ 766 761 function switch_to_blog( $new_blog, $deprecated = null ) { 767 global $wpdb; 768 769 $blog_id = get_current_blog_id(); 770 if ( empty( $new_blog ) ) { 771 $new_blog = $blog_id; 772 } 773 774 $GLOBALS['_wp_switched_stack'][] = $blog_id; 775 776 /* 777 * If we're switching to the same blog id that we're on, 778 * set the right vars, do the associated actions, but skip 779 * the extra unnecessary work 780 */ 781 if ( $new_blog == $blog_id ) { 782 /** 783 * Fires when the blog is switched. 784 * 785 * @since MU 786 * 787 * @param int $new_blog New blog ID. 788 * @param int $new_blog Blog ID. 789 */ 790 do_action( 'switch_blog', $new_blog, $new_blog ); 791 $GLOBALS['switched'] = true; 792 return true; 793 } 794 795 $wpdb->set_blog_id( $new_blog ); 796 $GLOBALS['table_prefix'] = $wpdb->get_blog_prefix(); 797 $prev_blog_id = $blog_id; 798 $GLOBALS['blog_id'] = $new_blog; 799 800 if ( function_exists( 'wp_cache_switch_to_blog' ) ) { 801 wp_cache_switch_to_blog( $new_blog ); 802 } else { 803 global $wp_object_cache; 804 805 if ( is_object( $wp_object_cache ) && isset( $wp_object_cache->global_groups ) ) { 806 $global_groups = $wp_object_cache->global_groups; 807 } else { 808 $global_groups = false; 809 } 810 wp_cache_init(); 811 812 if ( function_exists( 'wp_cache_add_global_groups' ) ) { 813 if ( is_array( $global_groups ) ) { 814 wp_cache_add_global_groups( $global_groups ); 815 } else { 816 wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'useremail', 'userslugs', 'site-transient', 'site-options', 'site-lookup', 'blog-lookup', 'blog-details', 'rss', 'global-posts', 'blog-id-cache', 'networks', 'sites', 'site-details' ) ); 817 } 818 wp_cache_add_non_persistent_groups( array( 'counts', 'plugins' ) ); 819 } 820 } 821 822 if ( did_action( 'init' ) ) { 823 wp_roles()->reinit(); 824 $current_user = wp_get_current_user(); 825 $current_user->for_blog( $new_blog ); 826 } 827 828 /** This filter is documented in wp-includes/ms-blogs.php */ 829 do_action( 'switch_blog', $new_blog, $prev_blog_id ); 830 $GLOBALS['switched'] = true; 831 832 return true; 762 return $GLOBALS['wp_state']->switch_to_site( $new_blog ); 833 763 } 834 764 835 765 /** … … 838 768 * @see switch_to_blog() 839 769 * @since MU 840 770 * 841 * @global wpdb $wpdb 842 * @global array $_wp_switched_stack 843 * @global int $blog_id 844 * @global bool $switched 845 * @global string $table_prefix 846 * @global WP_Object_Cache $wp_object_cache 771 * @global WP_State $wp_state 847 772 * 848 773 * @return bool True on success, false if we're already on the current blog 849 774 */ 850 775 function restore_current_blog() { 851 global $wpdb; 852 853 if ( empty( $GLOBALS['_wp_switched_stack'] ) ) { 854 return false; 855 } 856 857 $blog = array_pop( $GLOBALS['_wp_switched_stack'] ); 858 $blog_id = get_current_blog_id(); 859 860 if ( $blog_id == $blog ) { 861 /** This filter is documented in wp-includes/ms-blogs.php */ 862 do_action( 'switch_blog', $blog, $blog ); 863 // If we still have items in the switched stack, consider ourselves still 'switched' 864 $GLOBALS['switched'] = ! empty( $GLOBALS['_wp_switched_stack'] ); 865 return true; 866 } 867 868 $wpdb->set_blog_id( $blog ); 869 $prev_blog_id = $blog_id; 870 $GLOBALS['blog_id'] = $blog; 871 $GLOBALS['table_prefix'] = $wpdb->get_blog_prefix(); 872 873 if ( function_exists( 'wp_cache_switch_to_blog' ) ) { 874 wp_cache_switch_to_blog( $blog ); 875 } else { 876 global $wp_object_cache; 877 878 if ( is_object( $wp_object_cache ) && isset( $wp_object_cache->global_groups ) ) { 879 $global_groups = $wp_object_cache->global_groups; 880 } else { 881 $global_groups = false; 882 } 883 884 wp_cache_init(); 885 886 if ( function_exists( 'wp_cache_add_global_groups' ) ) { 887 if ( is_array( $global_groups ) ) { 888 wp_cache_add_global_groups( $global_groups ); 889 } else { 890 wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'useremail', 'userslugs', 'site-transient', 'site-options', 'site-lookup', 'blog-lookup', 'blog-details', 'rss', 'global-posts', 'blog-id-cache', 'networks', 'sites', 'site-details' ) ); 891 } 892 wp_cache_add_non_persistent_groups( array( 'counts', 'plugins' ) ); 893 } 894 } 895 896 if ( did_action( 'init' ) ) { 897 wp_roles()->reinit(); 898 $current_user = wp_get_current_user(); 899 $current_user->for_blog( $blog ); 900 } 901 902 /** This filter is documented in wp-includes/ms-blogs.php */ 903 do_action( 'switch_blog', $blog, $prev_blog_id ); 904 905 // If we still have items in the switched stack, consider ourselves still 'switched' 906 $GLOBALS['switched'] = ! empty( $GLOBALS['_wp_switched_stack'] ); 907 908 return true; 776 return $GLOBALS['wp_state']->restore_current_site(); 909 777 } 910 778 911 779 /** … … 913 781 * 914 782 * @since 3.5.0 915 783 * 916 * @global array $_wp_switched_stack784 * @global WP_State $wp_state 917 785 * 918 786 * @return bool True if switched, false otherwise. 919 787 */ 920 788 function ms_is_switched() { 921 return ! empty( $GLOBALS['_wp_switched_stack']);789 return $GLOBALS['wp_state']->is_switched(); 922 790 } 923 791 924 792 /** -
src/wp-includes/ms-settings.php
28 28 /** WP_Site class */ 29 29 require_once( ABSPATH . WPINC . '/class-wp-site.php' ); 30 30 31 /* WP_State class */ 32 require_once( ABSPATH . WPINC . '/class-wp-state.php' ); 33 31 34 /** Multisite loader */ 32 35 require_once( ABSPATH . WPINC . '/ms-load.php' ); 33 36 … … 88 91 $wpdb->set_prefix( $table_prefix, false ); // $table_prefix can be set in sunrise.php 89 92 $wpdb->set_blog_id( $current_blog->blog_id, $current_blog->site_id ); 90 93 $table_prefix = $wpdb->get_blog_prefix(); 91 $_wp_switched_stack = array();92 94 $switched = false; 95 $wp_state = new WP_State(); 93 96 94 97 // need to init cache again after blog_id is set 95 98 wp_start_object_cache();