Make WordPress Core

Changeset 48044


Ignore:
Timestamp:
06/15/2020 12:29:13 PM (4 years ago)
Author:
SergeyBiryukov
Message:

Networks and Sites: Don't unnecessarily switch to the current blog in get_blog_details().

Props arpitgshah, djennez, acsnaterse, joostdevalk, SergeyBiryukov.
Fixes #50391.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/ms-blogs.php

    r47808 r48044  
    239239    }
    240240
    241     switch_to_blog( $blog_id );
     241    $switched_blog = false;
     242
     243    if ( get_current_blog_id() !== $blog_id ) {
     244        switch_to_blog( $blog_id );
     245        $switched_blog = true;
     246    }
     247
    242248    $details->blogname   = get_option( 'blogname' );
    243249    $details->siteurl    = get_option( 'siteurl' );
    244250    $details->post_count = get_option( 'post_count' );
    245251    $details->home       = get_option( 'home' );
    246     restore_current_blog();
     252
     253    if ( $switched_blog ) {
     254        restore_current_blog();
     255    }
    247256
    248257    /**
     
    517526         */
    518527        do_action( 'switch_blog', $new_blog_id, $prev_blog_id, 'switch' );
     528
    519529        $GLOBALS['switched'] = true;
     530
    520531        return true;
    521532    }
     
    535546            $global_groups = false;
    536547        }
     548
    537549        wp_cache_init();
    538550
     
    543555                wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'useremail', 'userslugs', 'site-transient', 'site-options', 'blog-lookup', 'blog-details', 'rss', 'global-posts', 'blog-id-cache', 'networks', 'sites', 'site-details', 'blog_meta' ) );
    544556            }
     557
    545558            wp_cache_add_non_persistent_groups( array( 'counts', 'plugins' ) );
    546559        }
     
    549562    /** This filter is documented in wp-includes/ms-blogs.php */
    550563    do_action( 'switch_blog', $new_blog_id, $prev_blog_id, 'switch' );
     564
    551565    $GLOBALS['switched'] = true;
    552566
     
    582596        /** This filter is documented in wp-includes/ms-blogs.php */
    583597        do_action( 'switch_blog', $new_blog_id, $prev_blog_id, 'restore' );
     598
    584599        // If we still have items in the switched stack, consider ourselves still 'switched'.
    585600        $GLOBALS['switched'] = ! empty( $GLOBALS['_wp_switched_stack'] );
     601
    586602        return true;
    587603    }
     
    610626                wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'useremail', 'userslugs', 'site-transient', 'site-options', 'blog-lookup', 'blog-details', 'rss', 'global-posts', 'blog-id-cache', 'networks', 'sites', 'site-details', 'blog_meta' ) );
    611627            }
     628
    612629            wp_cache_add_non_persistent_groups( array( 'counts', 'plugins' ) );
    613630        }
  • trunk/tests/phpunit/tests/multisite/getBlogDetails.php

    r47012 r48044  
    141141            $site = get_blog_details( array( 'path' => '/foo/' ) );
    142142            $this->assertFalse( $site );
     143        }
     144
     145        /**
     146         * @ticket 50391
     147         */
     148        public function test_get_blog_details_does_not_switch_to_current_blog() {
     149            $count = did_action( 'switch_blog' );
     150
     151            get_blog_details();
     152            $this->assertSame( $count, did_action( 'switch_blog' ) );
    143153        }
    144154
Note: See TracChangeset for help on using the changeset viewer.