Skip to:
Content

BuddyPress.org

Changeset 11254


Ignore:
Timestamp:
11/28/2016 01:03:55 PM (10 years ago)
Author:
djpaul
Message:

Tests: rely on WP Core's version of go_to().

Our version of go_to() is very messy, and since we copied it a few years ago, it has been refactored in WordPress.
We can remove the duplication which makes for a cleaner method, and rely on the upstream version.

This also helps avoid false positives in tools that check for use of deprecated WordPress functions.
There was a conditional block in the old go_to() that was for compatibility with WordPress <3.9.

Fixes #7326

Props netweb, DJPaul

File:
1 edited

Legend:

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

    r11230 r11254  
    141141
    142142        function go_to( $url ) {
    143                 global $wpdb, $current_site, $current_blog, $blog_id, $switched, $_wp_switched_stack, $public, $table_prefix, $current_user, $wp_roles;
    144 
    145                 // note: the WP and WP_Query classes like to silently fetch parameters
    146                 // from all over the place (globals, GET, etc), which makes it tricky
    147                 // to run them more than once without very carefully clearing everything
    148                 $_GET = $_POST = array();
    149                 foreach (array('query_string', 'id', 'postdata', 'authordata', 'day', 'currentmonth', 'page', 'pages', 'multipage', 'more', 'numpages', 'pagenow') as $v) {
    150                         if ( isset( $GLOBALS[$v] ) ) unset( $GLOBALS[$v] );
    151                 }
    152                 $parts = parse_url($url);
    153                 if (isset($parts['scheme'])) {
    154                         // set the HTTP_HOST
    155                         $GLOBALS['_SERVER']['HTTP_HOST'] = $parts['host'];
    156 
    157                         $req = $parts['path'];
    158                         if (isset($parts['query'])) {
    159                                 $req .= '?' . $parts['query'];
    160                                 // parse the url query vars into $_GET
    161                                 parse_str($parts['query'], $_GET);
    162                         }
    163                 } else {
    164                         $req = $url;
    165                 }
    166                 if ( ! isset( $parts['query'] ) ) {
    167                         $parts['query'] = '';
    168                 }
    169 
    170                 // Scheme
    171                 if ( 0 === strpos( $req, '/wp-admin' ) && force_ssl_admin() ) {
    172                         $_SERVER['HTTPS'] = 'on';
    173                 } else {
    174                         unset( $_SERVER['HTTPS'] );
    175                 }
    176 
    177                 // Set this for bp_core_set_uri_globals()
    178                 $GLOBALS['_SERVER']['REQUEST_URI'] = $req;
    179                 unset($_SERVER['PATH_INFO']);
    180 
    181                 // setup $current_site and $current_blog globals for multisite based on
    182                 // REQUEST_URI; mostly copied from /wp-includes/ms-settings.php
    183                 if ( is_multisite() ) {
    184                         $current_blog = $current_site = $blog_id = null;
    185 
    186                         $domain = addslashes( $_SERVER['HTTP_HOST'] );
    187                         if ( false !== strpos( $domain, ':' ) ) {
    188                                 if ( substr( $domain, -3 ) == ':80' ) {
    189                                         $domain = substr( $domain, 0, -3 );
    190                                         $_SERVER['HTTP_HOST'] = substr( $_SERVER['HTTP_HOST'], 0, -3 );
    191                                 } elseif ( substr( $domain, -4 ) == ':443' ) {
    192                                         $domain = substr( $domain, 0, -4 );
    193                                         $_SERVER['HTTP_HOST'] = substr( $_SERVER['HTTP_HOST'], 0, -4 );
    194                                 }
    195                         }
    196                         $path = stripslashes( $_SERVER['REQUEST_URI'] );
    197 
    198                         // Get a cleaned-up version of the wp_version string
    199                         // (strip -src, -alpha, etc which may trip up version_compare())
    200                         $wp_version = (float) $GLOBALS['wp_version'];
    201                         if ( version_compare( $wp_version, '4.4', '>=' ) ) {
    202                                 if ( ! $current_site = wp_cache_get( 'current_network', 'site-options' ) ) {
    203                                         // Are there even two networks installed?
    204                                         $one_network = $wpdb->get_row( "SELECT * FROM $wpdb->site LIMIT 2" ); // [sic]
    205                                         if ( 1 === $wpdb->num_rows ) {
    206                                                 $current_site = new WP_Network( $one_network );
    207                                                 wp_cache_add( 'current_network', $current_site, 'site-options' );
    208                                         } elseif ( 0 === $wpdb->num_rows ) {
    209                                                 ms_not_installed( $domain, $path );
    210                                         }
    211                                 }
    212                                 if ( empty( $current_site ) ) {
    213                                         $current_site = WP_Network::get_by_path( $domain, $path, 1 );
    214                                 }
    215 
    216                                 // The network declared by the site trumps any constants.
    217                                 if ( $current_blog && $current_blog->site_id != $current_site->id ) {
    218                                         $current_site = WP_Network::get_instance( $current_blog->site_id );
    219                                 }
    220 
    221                                 if ( empty( $current_site ) ) {
    222                                         do_action( 'ms_network_not_found', $domain, $path );
    223 
    224                                         ms_not_installed( $domain, $path );
    225                                 } elseif ( $path === $current_site->path ) {
    226                                         $current_blog = get_site_by_path( $domain, $path );
    227                                 } else {
    228                                         // Search the network path + one more path segment (on top of the network path).
    229                                         $current_blog = get_site_by_path( $domain, $path, substr_count( $current_site->path, '/' ) );
    230                                 }
    231 
    232                                 // Figure out the current network's main site.
    233                                 if ( empty( $current_site->blog_id ) ) {
    234                                         if ( $current_blog->domain === $current_site->domain && $current_blog->path === $current_site->path ) {
    235                                                 $current_site->blog_id = $current_blog->blog_id;
    236                                         } elseif ( ! $current_site->blog_id = wp_cache_get( 'network:' . $current_site->id . ':main_site', 'site-options' ) ) {
    237                                                 $current_site->blog_id = $wpdb->get_var( $wpdb->prepare( "SELECT blog_id FROM $wpdb->blogs WHERE domain = %s AND path = %s",
    238                                                         $current_site->domain, $current_site->path ) );
    239                                                 wp_cache_add( 'network:' . $current_site->id . ':main_site', $current_site->blog_id, 'site-options' );
    240                                         }
    241                                 }
    242 
    243                                 $blog_id = $current_blog->blog_id;
    244                                 $public  = $current_blog->public;
    245 
    246                                 if ( empty( $current_blog->site_id ) ) {
    247                                         // This dates to [MU134] and shouldn't be relevant anymore,
    248                                         // but it could be possible for arguments passed to insert_blog() etc.
    249                                         $current_blog->site_id = 1;
    250                                 }
    251 
    252                                 $site_id = $current_blog->site_id;
    253                                 wp_load_core_site_options( $site_id );
    254 
    255                         } elseif ( version_compare( $wp_version, '3.9', '>=' ) ) {
    256 
    257                                 if ( is_admin() ) {
    258                                         $path = preg_replace( '#(.*)/wp-admin/.*#', '$1/', $path );
    259                                 }
    260                                 list( $path ) = explode( '?', $path );
    261 
    262                                 // Are there even two networks installed?
    263                                 $one_network = $wpdb->get_row( "SELECT * FROM $wpdb->site LIMIT 2" ); // [sic]
    264                                 if ( 1 === $wpdb->num_rows ) {
    265                                         $current_site = wp_get_network( $one_network );
    266                                 } elseif ( 0 === $wpdb->num_rows ) {
    267                                         ms_not_installed();
    268                                 }
    269 
    270                                 if ( empty( $current_site ) ) {
    271                                         $current_site = get_network_by_path( $domain, $path, 1 );
    272                                 }
    273 
    274                                 if ( empty( $current_site ) ) {
    275                                         ms_not_installed();
    276                                 } elseif ( $path === $current_site->path ) {
    277                                         $current_blog = get_site_by_path( $domain, $path );
    278                                 } else {
    279                                         // Search the network path + one more path segment (on top of the network path).
    280                                         $current_blog = get_site_by_path( $domain, $path, substr_count( $current_site->path, '/' ) );
    281                                 }
    282 
    283                                 // The network declared by the site trumps any constants.
    284                                 if ( $current_blog && $current_blog->site_id != $current_site->id ) {
    285                                         $current_site = wp_get_network( $current_blog->site_id );
    286                                 }
    287 
    288                                 // If we don't have a network by now, we have a problem.
    289                                 if ( empty( $current_site ) ) {
    290                                         ms_not_installed();
    291                                 }
    292 
    293                                 // @todo What if the domain of the network doesn't match the current site?
    294                                 $current_site->cookie_domain = $current_site->domain;
    295                                 if ( 'www.' === substr( $current_site->cookie_domain, 0, 4 ) ) {
    296                                         $current_site->cookie_domain = substr( $current_site->cookie_domain, 4 );
    297                                 }
    298 
    299                                 // Figure out the current network's main site.
    300                                 if ( ! isset( $current_site->blog_id ) ) {
    301                                         if ( $current_blog && $current_blog->domain === $current_site->domain && $current_blog->path === $current_site->path ) {
    302                                                 $current_site->blog_id = $current_blog->blog_id;
    303                                         } else {
    304                                                 // @todo we should be able to cache the blog ID of a network's main site easily.
    305                                                 $current_site->blog_id = $wpdb->get_var( $wpdb->prepare( "SELECT blog_id FROM $wpdb->blogs WHERE domain = %s AND path = %s",
    306                                                         $current_site->domain, $current_site->path ) );
    307                                         }
    308                                 }
    309 
    310                                 $blog_id = $current_blog->blog_id;
    311                                 $public  = $current_blog->public;
    312 
    313                                 if ( empty( $current_blog->site_id ) ) {
    314                                         // This dates to [MU134] and shouldn't be relevant anymore,
    315                                         // but it could be possible for arguments passed to insert_blog() etc.
    316                                         $current_blog->site_id = 1;
    317                                 }
    318 
    319                                 $site_id = $current_blog->site_id;
    320                                 wp_load_core_site_options( $site_id );
    321 
    322 
    323                         // Pre WP 3.9
    324                         } else {
    325 
    326                                 $domain = rtrim( $domain, '.' );
    327                                 $cookie_domain = $domain;
    328                                 if ( substr( $cookie_domain, 0, 4 ) == 'www.' )
    329                                         $cookie_domain = substr( $cookie_domain, 4 );
    330 
    331                                 $path = preg_replace( '|([a-z0-9-]+.php.*)|', '', $GLOBALS['_SERVER']['REQUEST_URI'] );
    332                                 $path = str_replace ( '/wp-admin/', '/', $path );
    333                                 $path = preg_replace( '|(/[a-z0-9-]+?/).*|', '$1', $path );
    334 
    335                                 $GLOBALS['current_site'] = wpmu_current_site();
    336                                 if ( ! isset( $GLOBALS['current_site']->blog_id ) && ! empty( $GLOBALS['current_site'] ) )
    337                                         $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 ) );
    338 
    339                                 $blogname = htmlspecialchars( substr( $GLOBALS['_SERVER']['REQUEST_URI'], strlen( $path ) ) );
    340                                 if ( false !== strpos( $blogname, '/' ) )
    341                                         $blogname = substr( $blogname, 0, strpos( $blogname, '/' ) );
    342                                 if ( false !== strpos( $blogname, '?' ) )
    343                                         $blogname = substr( $blogname, 0, strpos( $blogname, '?' ) );
    344                                 $reserved_blognames = array( 'page', 'comments', 'blog', 'wp-admin', 'wp-includes', 'wp-content', 'files', 'feed' );
    345                                 if ( $blogname != '' && ! in_array( $blogname, $reserved_blognames ) && ! is_file( $blogname ) )
    346                                         $path .= $blogname . '/';
    347 
    348                                 $GLOBALS['current_blog'] = get_blog_details( array( 'domain' => $domain, 'path' => $path ), false );
    349 
    350                                 unset($reserved_blognames);
    351 
    352                                 if ( $GLOBALS['current_site'] && ! $GLOBALS['current_blog'] ) {
    353                                         $GLOBALS['current_blog'] = get_blog_details( array( 'domain' => $GLOBALS['current_site']->domain, 'path' => $GLOBALS['current_site']->path ), false );
    354                                 }
    355 
    356                                 $GLOBALS['blog_id'] = $GLOBALS['current_blog']->blog_id;
    357                         }
    358 
    359                         // Emulate a switch_to_blog()
    360                         $table_prefix = $wpdb->get_blog_prefix( $current_blog->blog_id );
    361                         $wpdb->set_blog_id( $current_blog->blog_id, $current_blog->site_id );
    362                         $_wp_switched_stack = array();
    363                         $switched = false;
    364 
    365                         if ( ! isset( $current_site->site_name ) ) {
    366                                 $current_site->site_name = get_site_option( 'site_name' );
    367                                 if ( ! $current_site->site_name ) {
    368                                         $current_site->site_name = ucfirst( $current_site->domain );
    369                                 }
    370                         }
    371                 }
    372 
    373                 $this->flush_cache();
    374                 unset($GLOBALS['wp_query'], $GLOBALS['wp_the_query']);
    375                 $GLOBALS['wp_the_query'] = new WP_Query();
    376                 $GLOBALS['wp_query'] = $GLOBALS['wp_the_query'];
    377                 $GLOBALS['wp'] = new WP();
    378 
    379                 // clean out globals to stop them polluting wp and wp_query
    380                 foreach ($GLOBALS['wp']->public_query_vars as $v) {
    381                         unset($GLOBALS[$v]);
    382                 }
    383                 foreach ($GLOBALS['wp']->private_query_vars as $v) {
    384                         unset($GLOBALS[$v]);
    385                 }
    386 
    387                 $GLOBALS['wp']->main($parts['query']);
    388 
    389                 $wp_roles = new WP_Roles();
    390                 $current_user = wp_get_current_user();
    391                 $current_user->for_blog( $blog_id );
    392 
    393                 // For BuddyPress, James.
    394                 $this->clean_up_global_scope();
    395143                $GLOBALS['bp']->loggedin_user = NULL;
    396144                $GLOBALS['bp']->pages = bp_core_get_directory_pages();
     145
     146                parent::go_to( $url );
     147
    397148                do_action( 'bp_init' );
    398149        }
Note: See TracChangeset for help on using the changeset viewer.