| 137 | * Retrieve a network object by its domain and path. |
| 138 | * |
| 139 | * @since 3.9.0 |
| 140 | * |
| 141 | * @param string $domain |
| 142 | * @param string $path |
| 143 | * |
| 144 | * @return mixed |
| 145 | */ |
| 146 | function get_network_by_path( $domain, $path ) { |
| 147 | global $wpdb; |
| 148 | |
| 149 | $network_id = false; |
| 150 | |
| 151 | while ( 1 <= substr_count( $domain, '.' ) ) { |
| 152 | // Check for a match on the full domain and first path |
| 153 | $network_id = $wpdb->get_var( $wpdb->prepare( "SELECT id FROM $wpdb->site WHERE domain = %s AND path = %s", $domain, $path ) ); |
| 154 | |
| 155 | if ( $network_id ) { |
| 156 | break; |
| 157 | } |
| 158 | |
| 159 | // Check for a match on the full domain and / path |
| 160 | if ( $path !== '/' ) { |
| 161 | $network_id = $wpdb->get_var( $wpdb->prepare( "SELECT id FROM $wpdb->site WHERE domain = %s AND path = '/'", $domain ) ); |
| 162 | } |
| 163 | |
| 164 | if ( $network_id ) { |
| 165 | break; |
| 166 | } |
| 167 | |
| 168 | $domain = substr( $domain, 1 + strpos( $domain, '.' ) ); |
| 169 | |
| 170 | // Check for a match on the root domain and first path |
| 171 | $network_id = $wpdb->get_var( $wpdb->prepare( "SELECT id FROM $wpdb->site WHERE domain = %s AND path = %s", $domain, $path ) ); |
| 172 | |
| 173 | if ( $network_id ) { |
| 174 | break; |
| 175 | } |
| 176 | |
| 177 | if ( $path !== '/' ) { |
| 178 | $network_id = $wpdb->get_var( $wpdb->prepare( "SELECT id FROM $wpdb->site WHERE domain = %s AND path = '/'", $domain ) ); |
| 179 | } |
| 180 | |
| 181 | if ( $network_id ) { |
| 182 | break; |
| 183 | } |
| 184 | |
| 185 | } |
| 186 | |
| 187 | if ( $network_id ) { |
| 188 | $network = wp_get_network( $network_id ); |
| 189 | |
| 190 | return $network; |
| 191 | } |
| 192 | |
| 193 | return false; |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Retrieve an object containing information about the requested network. |
| 198 | * |
| 199 | * @since 3.9.0 |
| 200 | * |
| 201 | * @param int $network_id The network's ID |
| 202 | * |
| 203 | * @return mixed Object containing network information if found, false if not. |
| 204 | */ |
| 205 | function wp_get_network( $network_id ) { |
| 206 | global $wpdb; |
| 207 | |
| 208 | $network = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->site WHERE id = %d", $network_id ) ); |
| 209 | |
| 210 | if ( $network ) { |
| 211 | wp_load_core_site_options( $network->id ); |
| 212 | $network = get_current_site_name( $network ); |
| 213 | return $network; |
| 214 | } |
| 215 | |
| 216 | return false; |
| 217 | } |
| 218 | |
| 219 | /** |
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 | | } |
| 256 | // Find the first complete path after the domain. |
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 ) ); |
191 | | |
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 | | } |
198 | | |
| 259 | $current_site = get_network_by_path( $domain, $path ); |
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; |
211 | | } |
212 | | |
213 | | $current_site = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->site WHERE domain = %s AND path='/'", $sitedomain) ); |
214 | | } |
215 | | |
216 | | if ( $current_site || defined( 'WP_INSTALLING' ) ) { |
217 | | $path = '/'; |