Ticket #27003: 27003.9.diff
File 27003.9.diff, 12.8 KB (added by , 11 years ago) |
---|
-
src/wp-includes/ms-load.php
134 134 } 135 135 136 136 /** 137 * Retrieve a network object by its domain and path. 138 * 139 * @since 3.9.0 140 * 141 * @param string $domain Domain to check. 142 * @param string $path Path to check. 143 * @return object|bool Network object if successful. False when no network is found. 144 */ 145 function get_network_by_path( $domain, $path ) { 146 global $wpdb; 147 148 $network_id = false; 149 150 $domains = array( $domain ); 151 $pieces = explode( '.', $domain ); 152 // It's possible one domain to search is 'com', but it might as well 153 // be 'localhost' or some other locally mapped domain. 154 while ( array_shift( $pieces ) ) { 155 if ( $pieces ) { 156 $domains[] = implode( '.', $pieces ); 157 } 158 } 159 160 if ( '/' !== $path ) { 161 $paths = array( '/', $path ); 162 } else { 163 $paths = array( '/' ); 164 } 165 166 $search_domains = "'" . implode( "', '", $wpdb->_escape( $domains ) ) . "'"; 167 $paths = "'" . implode( "', '", $wpdb->_escape( $paths ) ) . "'"; 168 169 $networks = $wpdb->get_results( "SELECT id, domain, path FROM $wpdb->site 170 WHERE domain IN ($search_domains) AND path IN ($paths) 171 ORDER BY CHAR_LENGTH(domain) DESC, CHAR_LENGTH(path) DESC" ); 172 173 /* 174 * Domains are sorted by length of domain, then by length of path. 175 * The domain must match for the path to be considered. Otherwise, 176 * a network with the path of / will suffice. 177 */ 178 $found = false; 179 foreach ( $domains as $domain ) { 180 foreach ( $networks as $network ) { 181 if ( $network->domain === $domain ) { 182 if ( $network->path === $path ) { 183 $found = true; 184 break 2; 185 } 186 if ( $network->path === '/' ) { 187 $found = true; 188 break 2; 189 } 190 } 191 } 192 } 193 194 if ( $found ) { 195 $network = wp_get_network( $network ); 196 197 return $network; 198 } 199 200 return false; 201 } 202 203 /** 204 * Retrieve an object containing information about the requested network. 205 * 206 * @since 3.9.0 207 * 208 * @param int $network_id The network's DB row or ID. 209 * @return mixed Object containing network information if found, false if not. 210 */ 211 function wp_get_network( $network ) { 212 global $wpdb; 213 214 if ( ! is_object( $network ) ) { 215 $network = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->site WHERE id = %d", $network ) ); 216 if ( ! $network ) { 217 return false; 218 } 219 } 220 221 return $network; 222 } 223 224 /** 137 225 * Sets current_site object. 138 226 * 139 227 * @access private … … 141 229 * @return object $current_site object 142 230 */ 143 231 function wpmu_current_site() { 144 global $wpdb, $current_site, $domain, $path , $sites, $cookie_domain;232 global $wpdb, $current_site, $domain, $path; 145 233 146 234 if ( empty( $current_site ) ) 147 235 $current_site = new stdClass; 148 236 237 // 1. If constants are defined, that's our network. 149 238 if ( defined( 'DOMAIN_CURRENT_SITE' ) && defined( 'PATH_CURRENT_SITE' ) ) { 150 239 $current_site->id = defined( 'SITE_ID_CURRENT_SITE' ) ? SITE_ID_CURRENT_SITE : 1; 151 240 $current_site->domain = DOMAIN_CURRENT_SITE; … … 154 243 $current_site->blog_id = BLOG_ID_CURRENT_SITE; 155 244 elseif ( defined( 'BLOGID_CURRENT_SITE' ) ) // deprecated. 156 245 $current_site->blog_id = BLOGID_CURRENT_SITE; 157 if ( DOMAIN_CURRENT_SITE == $domain )158 $current_site->cookie_domain = $cookie_domain;159 elseif ( substr( $current_site->domain, 0, 4 ) == 'www.' )160 $current_site->cookie_domain = substr( $current_site->domain, 4 );161 else162 $current_site->cookie_domain = $current_site->domain;163 246 164 wp_load_core_site_options( $current_site->id ); 247 // 2. Pull the network from cache, if possible. 248 } elseif ( ! $current_site = wp_cache_get( 'current_site', 'site-options' ) ) { 165 249 166 return $current_site;167 }250 // 3. See if they have only one network. 251 $networks = $wpdb->get_col( "SELECT id FROM $wpdb->site LIMIT 2" ); 168 252 169 $current_site = wp_cache_get( 'current_site', 'site-options' ); 170 if ( $current_site ) 171 return $current_site; 253 if ( count( $networks ) <= 1 ) { 254 $current_site = wp_get_network( $networks[0]->id ); 172 255 173 $sites = $wpdb->get_results( "SELECT * FROM $wpdb->site" ); // usually only one site 174 if ( 1 == count( $sites ) ) { 175 $current_site = $sites[0]; 176 wp_load_core_site_options( $current_site->id ); 177 $path = $current_site->path; 178 $current_site->blog_id = $wpdb->get_var( $wpdb->prepare( "SELECT blog_id FROM $wpdb->blogs WHERE domain = %s AND path = %s", $current_site->domain, $current_site->path ) ); 179 $current_site = get_current_site_name( $current_site ); 180 if ( substr( $current_site->domain, 0, 4 ) == 'www.' ) 181 $current_site->cookie_domain = substr( $current_site->domain, 4 ); 182 wp_cache_set( 'current_site', $current_site, 'site-options' ); 183 return $current_site; 184 } 185 $path = substr( $_SERVER[ 'REQUEST_URI' ], 0, 1 + strpos( $_SERVER[ 'REQUEST_URI' ], '/', 1 ) ); 256 $current_site->blog_id = $wpdb->get_var( $wpdb->prepare( "SELECT blog_id 257 FROM $wpdb->blogs WHERE domain = %s AND path = %s", 258 $current_site->domain, $current_site->path ) ); 186 259 187 if ( $domain == $cookie_domain ) 188 $current_site = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->site WHERE domain = %s AND path = %s", $domain, $path ) ); 189 else 190 $current_site = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->site WHERE domain IN ( %s, %s ) AND path = %s ORDER BY CHAR_LENGTH( domain ) DESC LIMIT 1", $domain, $cookie_domain, $path ) ); 260 wp_cache_set( 'current_site', 'site-options' ); 191 261 192 if ( ! $current_site ) { 193 if ( $domain == $cookie_domain ) 194 $current_site = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->site WHERE domain = %s AND path='/'", $domain ) ); 195 else 196 $current_site = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->site WHERE domain IN ( %s, %s ) AND path = '/' ORDER BY CHAR_LENGTH( domain ) DESC LIMIT 1", $domain, $cookie_domain ) ); 197 } 262 // 4. Multiple networks are in play. Determine which via domain and path. 263 } else { 264 // Find the first path segment. 265 $path = substr( $_SERVER['REQUEST_URI'], 0, 1 + strpos( $_SERVER['REQUEST_URI'], '/', 1 ) ); 266 $current_site = get_network_by_path( $domain, $path ); 198 267 199 if ( $current_site ) { 200 $path = $current_site->path; 201 $current_site->cookie_domain = $cookie_domain; 202 return $current_site; 203 } 204 205 if ( is_subdomain_install() ) { 206 $sitedomain = substr( $domain, 1 + strpos( $domain, '.' ) ); 207 $current_site = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->site WHERE domain = %s AND path = %s", $sitedomain, $path) ); 208 if ( $current_site ) { 209 $current_site->cookie_domain = $current_site->domain; 210 return $current_site; 268 // Option 1. We did not find anything. 269 if ( ! $current_site ) { 270 wp_load_translations_early(); 271 wp_die( __( 'No site defined on this host. If you are the owner of this site, please check <a href="http://codex.wordpress.org/Debugging_a_WordPress_Network">Debugging a WordPress Network</a> for help.' ) ); 272 } 211 273 } 212 213 $current_site = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->site WHERE domain = %s AND path='/'", $sitedomain) );214 274 } 215 275 216 if ( $current_site || defined( 'WP_INSTALLING' ) ) { 217 $path = '/'; 218 return $current_site; 219 } 220 221 // Still no dice. 222 wp_load_translations_early(); 223 224 if ( 1 == count( $sites ) ) 225 wp_die( sprintf( __( 'That site does not exist. Please try <a href="%s">%s</a>.' ), 'http://' . $sites[0]->domain . $sites[0]->path ) ); 226 else 227 wp_die( __( 'No site defined on this host. If you are the owner of this site, please check <a href="http://codex.wordpress.org/Debugging_a_WordPress_Network">Debugging a WordPress Network</a> for help.' ) ); 276 // Option 2. We found something. Load up site meta and return. 277 wp_load_core_site_options(); 278 $current_site = get_current_site_name( $current_site ); 279 return $current_site; 228 280 } 229 281 230 282 /** -
src/wp-includes/ms-settings.php
37 37 } 38 38 39 39 $domain = rtrim( $domain, '.' ); 40 $cookie_domain = $domain;41 if ( substr( $cookie_domain, 0, 4 ) == 'www.' )42 $cookie_domain = substr( $cookie_domain, 4 );43 40 44 41 $path = preg_replace( '|([a-z0-9-]+.php.*)|', '', $_SERVER['REQUEST_URI'] ); 45 42 $path = str_replace ( '/wp-admin/', '/', $path ); … … 46 43 $path = preg_replace( '|(/[a-z0-9-]+?/).*|', '$1', $path ); 47 44 48 45 $current_site = wpmu_current_site(); 46 $current_site->cookie_domain = $current_site->domain; 47 if ( 'www.' === substr( $current_site->cookie_domain, 0, 4 ) ) { 48 $current_site->cookie_domain = substr( $current_site->cookie_domain, 4 ); 49 } 50 49 51 if ( ! isset( $current_site->blog_id ) ) 50 52 $current_site->blog_id = $wpdb->get_var( $wpdb->prepare( "SELECT blog_id FROM $wpdb->blogs WHERE domain = %s AND path = %s", $current_site->domain, $current_site->path ) ); 51 53 -
tests/phpunit/includes/factory.php
42 42 */ 43 43 public $blog; 44 44 45 /** 46 * @var WP_UnitTest_Factory_For_Network 47 */ 48 public $network; 49 45 50 function __construct() { 46 51 $this->post = new WP_UnitTest_Factory_For_Post( $this ); 47 52 $this->attachment = new WP_UnitTest_Factory_For_Attachment( $this ); … … 50 55 $this->term = new WP_UnitTest_Factory_For_Term( $this ); 51 56 $this->category = new WP_UnitTest_Factory_For_Term( $this, 'category' ); 52 57 $this->tag = new WP_UnitTest_Factory_For_Term( $this, 'post_tag' ); 53 if ( is_multisite() ) 58 if ( is_multisite() ) { 54 59 $this->blog = new WP_UnitTest_Factory_For_Blog( $this ); 60 $this->network = new WP_UnitTest_Factory_For_Network( $this ); 61 } 55 62 } 56 63 } 57 64 … … 177 184 } 178 185 179 186 187 class WP_UnitTest_Factory_For_Network extends WP_UnitTest_Factory_For_Thing { 188 189 function __construct( $factory = null ) { 190 parent::__construct( $factory ); 191 $this->default_generation_definitions = array( 192 'domain' => WP_TESTS_DOMAIN, 193 'title' => new WP_UnitTest_Generator_Sequence( 'Network %s' ), 194 'path' => new WP_UnitTest_Generator_Sequence( '/testpath%s/' ), 195 'network_id' => new WP_UnitTest_Generator_Sequence( '%s', 2 ), 196 'subdomain_install' => false, 197 ); 198 } 199 200 function create_object( $args ) { 201 require_once ABSPATH . 'wp-admin/includes/upgrade.php'; 202 203 if ( ! isset( $args['user'] ) ) { 204 $email = WP_TESTS_EMAIL; 205 } else { 206 $email = get_userdata( $args['user'] )->user_email; 207 } 208 209 populate_network( $args['network_id'], $args['domain'], $email, $args['title'], $args['path'], $args['subdomain_install'] ); 210 return $args['network_id']; 211 } 212 213 function update_object( $network_id, $fields ) {} 214 215 function get_object_by_id( $network_id ) { 216 return wp_get_network( $network_id ); 217 } 218 } 219 180 220 class WP_UnitTest_Factory_For_Term extends WP_UnitTest_Factory_For_Thing { 181 221 182 222 private $taxonomy; -
tests/phpunit/tests/ms.php
1172 1172 // Expect 0 sites when using an offset larger than the number of sites 1173 1173 $this->assertCount( 0, wp_get_sites( array( 'offset' => 20 ) ) ); 1174 1174 } 1175 1176 /** 1177 * @ticket 27003 1178 */ 1179 function test_get_network_by_path() { 1180 global $wpdb; 1181 1182 $ids = array( 1183 'wordpress.org/' => array( 'domain' => 'wordpress.org', 'path' => '/' ), 1184 'wordpress.net/' => array( 'domain' => 'wordpress.net', 'path' => '/' ), 1185 'www.wordpress.net/' => array( 'domain' => 'www.wordpress.net', 'path' => '/' ), 1186 'wordpress.org/one/' => array( 'domain' => 'wordpress.org', 'path' => '/one/' ), 1187 'www.wordpress.org/two/' => array( 'domain' => 'www.wordpress.org', 'path' => '/two/' ), 1188 'www.wordpress.net/three/' => array( 'domain' => 'www.wordpress.net', 'path' => '/three/' ), 1189 'wordpress.net/four/' => array( 'domain' => 'wordpress.net', 'path' => '/four/' ), 1190 ); 1191 1192 foreach ( $ids as &$id ) { 1193 $id = $this->factory->network->create( $id ); 1194 } 1195 unset( $id ); 1196 1197 $this->assertEquals( $ids['www.wordpress.net/'], 1198 get_network_by_path( 'www.wordpress.net', '/notapath/' )->id ); 1199 1200 $this->assertEquals( $ids['www.wordpress.net/three/'], 1201 get_network_by_path( 'www.wordpress.net', '/three/' )->id ); 1202 1203 // This won't find /four/ because of the www. 1204 $this->assertEquals( $ids['www.wordpress.net/'], 1205 get_network_by_path( 'www.wordpress.net', '/four/' )->id ); 1206 1207 $this->assertEquals( $ids['wordpress.net/four/'], 1208 get_network_by_path( 'wordpress.net', '/four/' )->id ); 1209 1210 $this->assertEquals( $ids['wordpress.net/'], 1211 get_network_by_path( 'wordpress.net', '/notapath/' )->id ); 1212 1213 $this->assertEquals( $ids['wordpress.net/'], 1214 get_network_by_path( 'site1.wordpress.net', '/notapath/' )->id ); 1215 1216 $this->assertEquals( $ids['wordpress.net/four/'], 1217 get_network_by_path( 'site1.wordpress.net', '/four/' )->id ); 1218 } 1175 1219 } 1176 1220 1177 1221 endif;