From acad9eba0c05357224b1f21e8c6e51a32189d9a5 Mon Sep 17 00:00:00 2001 From: Chris Reynolds Date: Mon, 15 May 2023 15:26:04 -0600 Subject: [PATCH] use WP_CLI::add_hook so we don't need to restore_current_blog --- php/commands/launchcheck.php | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/php/commands/launchcheck.php b/php/commands/launchcheck.php index 6c11ac0..e311e00 100644 --- a/php/commands/launchcheck.php +++ b/php/commands/launchcheck.php @@ -30,17 +30,7 @@ public function all($args, $assoc_args) { // wp-config is going to be loaded again, and we need to avoid notices @WP_CLI::get_runner()->load_wordpress(); - - // Check for multisite. If we're on multisite, switch to the main site. - if ( is_multisite() ) { - if ( defined( 'BLOG_ID_CURRENT_SITE' ) ) { - switch_to_blog( BLOG_ID_CURRENT_SITE ); - } else { - switch_to_blog( 1 ); - } - \WP_CLI::log( sprintf( esc_html__( 'Multisite detected. Running checks on %s site.' ), get_bloginfo( 'name' ) ) ); - } - + WP_CLI::add_hook( 'before_run_command', [ $this, 'maybe_switch_to_blog' ] ); // WordPress is now loaded, so other checks can run $searcher = new \Pantheon\Filesearcher( WP_CONTENT_DIR ); @@ -58,6 +48,24 @@ public function all($args, $assoc_args) { \Pantheon\Messenger::emit($format); } + /** + * Switch to BLOG_ID_CURRENT_SITE if we're on a multisite. + * + * This forces the launchcheck command to use the main site's info for all + * the checks. + */ + public function maybe_switch_to_blog() { + // Check for multisite. If we're on multisite, switch to the main site. + if ( is_multisite() ) { + if ( defined( 'BLOG_ID_CURRENT_SITE' ) ) { + switch_to_blog( BLOG_ID_CURRENT_SITE ); + } else { + switch_to_blog( 1 ); + } + \WP_CLI::log( sprintf( esc_html__( 'Multisite detected. Running checks on %s site.' ), get_bloginfo( 'name' ) ) ); + } + } + /** * Checks for a properly-configured wp-config *