Skip to content

Commit

Permalink
use WP_CLI::add_hook so we don't need to restore_current_blog
Browse files Browse the repository at this point in the history
  • Loading branch information
jazzsequence committed May 15, 2023
1 parent 57f440a commit acad9eb
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions php/commands/launchcheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand All @@ -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
*
Expand Down

0 comments on commit acad9eb

Please sign in to comment.