Skip to:
Content

BuddyPress.org

Changeset 8107


Ignore:
Timestamp:
03/10/2014 08:47:41 PM (12 years ago)
Author:
boonebgorges
Message:

Update BP_UnitTestCase::go_to() for WP 3.9 trunk

WordPress 3.9 will introduce an overhauled method for determining the current
network and site when running multisite. The current changeset introduces
conditional logic that works properly with the new multisite routing methods.

A couple of specific tests will need updates after this commit.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/includes/testcase.php

    r8106 r8107  
    5858    function go_to( $url ) {
    5959        global $wpdb;
     60        global $current_site, $current_blog, $blog_id, $switched, $_wp_switched_stack, $public, $table_prefix, $current_user, $wp_roles;
    6061
    6162        // note: the WP and WP_Query classes like to silently fetch parameters
     
    9899        // REQUEST_URI; mostly copied from /wp-includes/ms-settings.php
    99100        if ( is_multisite() ) {
    100             $GLOBALS['current_blog'] = $GLOBALS['current_site'] = $GLOBALS['blog_id'] = null;
    101 
    102             $domain = addslashes( $_SERVER['HTTP_HOST'] );
    103             if ( false !== strpos( $domain, ':' ) ) {
     101            $current_blog = $current_site = $blog_id = null;
     102
     103            if ( version_compare( $GLOBALS['wp_version'], '3.8.2', '>' ) ) {
     104
     105                $domain = strtolower( stripslashes( $_SERVER['HTTP_HOST'] ) );
    104106                if ( substr( $domain, -3 ) == ':80' ) {
    105107                    $domain = substr( $domain, 0, -3 );
     
    109111                    $_SERVER['HTTP_HOST'] = substr( $_SERVER['HTTP_HOST'], 0, -4 );
    110112                }
     113
     114                $path = stripslashes( $_SERVER['REQUEST_URI'] );
     115                if ( is_admin() ) {
     116                    $path = preg_replace( '#(.*)/wp-admin/.*#', '$1/', $path );
     117                }
     118                list( $path ) = explode( '?', $path );
     119
     120                // Are there even two networks installed?
     121                $one_network = $wpdb->get_row( "SELECT * FROM $wpdb->site LIMIT 2" ); // [sic]
     122                if ( 1 === $wpdb->num_rows ) {
     123                    $current_site = wp_get_network( $one_network );
     124                } elseif ( 0 === $wpdb->num_rows ) {
     125                    ms_not_installed();
     126                }
     127
     128                if ( empty( $current_site ) ) {
     129                    $current_site = get_network_by_path( $domain, $path, 1 );
     130                }
     131
     132                if ( empty( $current_site ) ) {
     133                    ms_not_installed();
     134                } elseif ( $path === $current_site->path ) {
     135                    $current_blog = get_site_by_path( $domain, $path );
     136                } else {
     137                    // Search the network path + one more path segment (on top of the network path).
     138                    $current_blog = get_site_by_path( $domain, $path, substr_count( $current_site->path, '/' ) );
     139                }
     140
     141                // The network declared by the site trumps any constants.
     142                if ( $current_blog && $current_blog->site_id != $current_site->id ) {
     143                    $current_site = wp_get_network( $current_blog->site_id );
     144                }
     145
     146                // If we don't have a network by now, we have a problem.
     147                if ( empty( $current_site ) ) {
     148                    ms_not_installed();
     149                }
     150
     151                // @todo What if the domain of the network doesn't match the current site?
     152                $current_site->cookie_domain = $current_site->domain;
     153                if ( 'www.' === substr( $current_site->cookie_domain, 0, 4 ) ) {
     154                    $current_site->cookie_domain = substr( $current_site->cookie_domain, 4 );
     155                }
     156
     157                // Figure out the current network's main site.
     158                if ( ! isset( $current_site->blog_id ) ) {
     159                    if ( $current_blog && $current_blog->domain === $current_site->domain && $current_blog->path === $current_site->path ) {
     160                        $current_site->blog_id = $current_blog->blog_id;
     161                    } else {
     162                        // @todo we should be able to cache the blog ID of a network's main site easily.
     163                        $current_site->blog_id = $wpdb->get_var( $wpdb->prepare( "SELECT blog_id FROM $wpdb->blogs WHERE domain = %s AND path = %s",
     164                            $current_site->domain, $current_site->path ) );
     165                    }
     166                }
     167
     168                $blog_id = $current_blog->blog_id;
     169                $public  = $current_blog->public;
     170
     171                if ( empty( $current_blog->site_id ) ) {
     172                    // This dates to [MU134] and shouldn't be relevant anymore,
     173                    // but it could be possible for arguments passed to insert_blog() etc.
     174                    $current_blog->site_id = 1;
     175                }
     176
     177                $site_id = $current_blog->site_id;
     178                wp_load_core_site_options( $site_id );
     179
     180                $table_prefix = $wpdb->get_blog_prefix( $current_blog->blog_id );
     181                $wpdb->set_blog_id( $current_blog->blog_id, $current_blog->site_id );
     182                $_wp_switched_stack = array();
     183                $switched = false;
     184
     185                if ( ! isset( $current_site->site_name ) ) {
     186                    $current_site->site_name = get_site_option( 'site_name' );
     187                    if ( ! $current_site->site_name ) {
     188                        $current_site->site_name = ucfirst( $current_site->domain );
     189                    }
     190                }
     191
     192            // Pre WP 3.9
     193            } else {
     194
     195                $domain = addslashes( $_SERVER['HTTP_HOST'] );
     196                if ( false !== strpos( $domain, ':' ) ) {
     197                    if ( substr( $domain, -3 ) == ':80' ) {
     198                        $domain = substr( $domain, 0, -3 );
     199                        $_SERVER['HTTP_HOST'] = substr( $_SERVER['HTTP_HOST'], 0, -3 );
     200                    } elseif ( substr( $domain, -4 ) == ':443' ) {
     201                        $domain = substr( $domain, 0, -4 );
     202                        $_SERVER['HTTP_HOST'] = substr( $_SERVER['HTTP_HOST'], 0, -4 );
     203                    }
     204                }
     205
     206                $domain = rtrim( $domain, '.' );
     207                $cookie_domain = $domain;
     208                if ( substr( $cookie_domain, 0, 4 ) == 'www.' )
     209                    $cookie_domain = substr( $cookie_domain, 4 );
     210
     211                $path = preg_replace( '|([a-z0-9-]+.php.*)|', '', $GLOBALS['_SERVER']['REQUEST_URI'] );
     212                $path = str_replace ( '/wp-admin/', '/', $path );
     213                $path = preg_replace( '|(/[a-z0-9-]+?/).*|', '$1', $path );
     214
     215                $GLOBALS['current_site'] = wpmu_current_site();
     216                if ( ! isset( $GLOBALS['current_site']->blog_id ) && ! empty( $GLOBALS['current_site'] ) )
     217                    $GLOBALS['current_site']->blog_id = $wpdb->get_var( $wpdb->prepare( "SELECT blog_id FROM $wpdb->blogs WHERE domain = %s AND path = %s", $GLOBALS['current_site']->domain, $GLOBALS['current_site']->path ) );
     218
     219                // unit tests only support subdirectory install at the moment
     220                // removed object cache references
     221                if ( ! is_subdomain_install() ) {
     222                    $blogname = htmlspecialchars( substr( $GLOBALS['_SERVER']['REQUEST_URI'], strlen( $path ) ) );
     223                    if ( false !== strpos( $blogname, '/' ) )
     224                        $blogname = substr( $blogname, 0, strpos( $blogname, '/' ) );
     225                    if ( false !== strpos( $blogname, '?' ) )
     226                        $blogname = substr( $blogname, 0, strpos( $blogname, '?' ) );
     227                    $reserved_blognames = array( 'page', 'comments', 'blog', 'wp-admin', 'wp-includes', 'wp-content', 'files', 'feed' );
     228                    if ( $blogname != '' && ! in_array( $blogname, $reserved_blognames ) && ! is_file( $blogname ) )
     229                        $path .= $blogname . '/';
     230
     231                    $GLOBALS['current_blog'] = get_blog_details( array( 'domain' => $domain, 'path' => $path ), false );
     232
     233                    unset($reserved_blognames);
     234                }
     235
     236                if ( $GLOBALS['current_site'] && ! $GLOBALS['current_blog'] ) {
     237                    $GLOBALS['current_blog'] = get_blog_details( array( 'domain' => $GLOBALS['current_site']->domain, 'path' => $GLOBALS['current_site']->path ), false );
     238                }
     239
     240                if ( ! empty( $GLOBALS['current_blog'] ) )
     241                    $GLOBALS['blog_id'] = $GLOBALS['current_blog']->blog_id;
    111242            }
    112 
    113             $domain = rtrim( $domain, '.' );
    114             $cookie_domain = $domain;
    115             if ( substr( $cookie_domain, 0, 4 ) == 'www.' )
    116                 $cookie_domain = substr( $cookie_domain, 4 );
    117 
    118             $path = preg_replace( '|([a-z0-9-]+.php.*)|', '', $GLOBALS['_SERVER']['REQUEST_URI'] );
    119             $path = str_replace ( '/wp-admin/', '/', $path );
    120             $path = preg_replace( '|(/[a-z0-9-]+?/).*|', '$1', $path );
    121 
    122             $GLOBALS['current_site'] = wpmu_current_site();
    123             if ( ! isset( $GLOBALS['current_site']->blog_id ) )
    124                 $GLOBALS['current_site']->blog_id = $wpdb->get_var( $wpdb->prepare( "SELECT blog_id FROM $wpdb->blogs WHERE domain = %s AND path = %s", $GLOBALS['current_site']->domain, $GLOBALS['current_site']->path ) );
    125 
    126             // unit tests only support subdirectory install at the moment
    127             // removed object cache references
    128             if ( ! is_subdomain_install() ) {
    129                 $blogname = htmlspecialchars( substr( $GLOBALS['_SERVER']['REQUEST_URI'], strlen( $path ) ) );
    130                 if ( false !== strpos( $blogname, '/' ) )
    131                     $blogname = substr( $blogname, 0, strpos( $blogname, '/' ) );
    132                 if ( false !== strpos( $blogname, '?' ) )
    133                     $blogname = substr( $blogname, 0, strpos( $blogname, '?' ) );
    134                 $reserved_blognames = array( 'page', 'comments', 'blog', 'wp-admin', 'wp-includes', 'wp-content', 'files', 'feed' );
    135                 if ( $blogname != '' && ! in_array( $blogname, $reserved_blognames ) && ! is_file( $blogname ) )
    136                     $path .= $blogname . '/';
    137 
    138                 $GLOBALS['current_blog'] = get_blog_details( array( 'domain' => $domain, 'path' => $path ), false );
    139 
    140                 unset($reserved_blognames);
    141             }
    142 
    143             if ( $GLOBALS['current_site'] && ! $GLOBALS['current_blog'] ) {
    144                 $GLOBALS['current_blog'] = get_blog_details( array( 'domain' => $GLOBALS['current_site']->domain, 'path' => $GLOBALS['current_site']->path ), false );
    145             }
    146 
    147             $GLOBALS['blog_id'] = $GLOBALS['current_blog']->blog_id;
    148         }
    149 
     243        }
     244
     245        $this->flush_cache();
    150246        unset($GLOBALS['wp_query'], $GLOBALS['wp_the_query']);
    151247        $GLOBALS['wp_the_query'] = new WP_Query();
     
    162258
    163259        $GLOBALS['wp']->main($parts['query']);
     260
     261        $wp_roles->reinit();
     262        $current_user = wp_get_current_user();
     263        $current_user->for_blog( $blog_id );
    164264
    165265        // For BuddyPress, James.
Note: See TracChangeset for help on using the changeset viewer.