Skip to content
Merged
59 changes: 59 additions & 0 deletions features/framework.feature
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,65 @@ Feature: Load WP-CLI
Error: Site 'example.io' not found. Verify `--url=<url>` matches an existing site.
"""

# `wp db query` does not yet work on SQLite,
# See https://github.com/wp-cli/db-command/issues/234
@require-mysql
Scenario: Show detailed error when multisite network is not found
Given a WP multisite installation
And a force-network-not-found.php file:
"""
<?php
// Force ms_not_installed() to be triggered by returning empty network
WP_CLI::add_wp_hook(
'the_networks',
static function() {
return [];
}
);
"""
And I run `wp config delete DOMAIN_CURRENT_SITE --type=constant`

# This should trigger ms_not_installed() which now shows detailed error
# because we set current_screen to make is_admin() return true
When I try `wp --require=force-network-not-found.php option get home`
Then STDERR should contain:
"""
Error establishing a database connection.
"""
And STDERR should contain:
"""
If your site does not display, please contact the owner of this network.
"""

# `wp db query` does not yet work on SQLite,
# See https://github.com/wp-cli/db-command/issues/234
@require-mysql
Scenario: Show detailed error when hitting ms_network_not_found
Given a WP multisite installation
And a force-network-not-found.php file:
"""
<?php
// Force ms_not_installed() to be triggered by returning empty network
WP_CLI::add_wp_hook(
'the_networks',
static function() {
return [
new \WP_Network( new stdClass() ),
new \WP_Network( new stdClass() ),
];
}
);
"""
And I run `wp config delete DOMAIN_CURRENT_SITE --type=constant`

# This should trigger ms_not_installed() which now shows detailed error
# because we set current_screen to make is_admin() return true
When I try `wp --require=force-network-not-found.php option get home`
Then STDERR should contain:
"""
Error: Network not found. Verify the network exists in the database or run `wp core multisite-install`.
"""

Scenario: Don't show 'sitecategories' table unless global terms are enabled
Given a WP multisite installation

Expand Down
33 changes: 32 additions & 1 deletion php/WP_CLI/Runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -1539,16 +1539,34 @@ public function load_wordpress() {
define( 'WP_DEBUG_DISPLAY', true );
}
}

// For multisite, set a pseudo WP_Screen to make is_admin() return true.
// This ensures ms_not_installed() shows detailed error messages instead of
// the generic "Error establishing a database connection" message.
if ( $this->is_multisite() ) {
// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- Intentional temporary override for error messaging.
$GLOBALS['current_screen'] = new class() {
public function in_admin() {
return true;
}
};
}

require ABSPATH . 'wp-settings.php';

// Clean up the pseudo screen object after WordPress has loaded
if ( isset( $GLOBALS['current_screen'] ) && ! ( $GLOBALS['current_screen'] instanceof \WP_Screen ) ) {
unset( $GLOBALS['current_screen'] );
}

// Fix memory limit. See https://core.trac.wordpress.org/ticket/14889
// phpcs:ignore WordPress.PHP.IniSet.memory_limit_Disallowed -- This is perfectly fine for CLI usage.
ini_set( 'memory_limit', -1 );

// Load all the admin APIs, for convenience
require ABSPATH . 'wp-admin/includes/admin.php';

add_filter(
WP_CLI::add_wp_hook(
'filesystem_method',
static function () {
return 'direct';
Expand Down Expand Up @@ -1803,6 +1821,19 @@ static function ( $current_site, $domain, $path ) {
10,
3
);

// Handle ms_network_not_found to provide better error messages
WP_CLI::add_wp_hook(
'ms_network_not_found',
static function ( $domain, $path ) {
$url = $domain . $path;
$message = $url ? "Network '{$url}' not found." : 'Network not found.';
$message .= ' Verify the network exists in the database or run `wp core multisite-install`.';
WP_CLI::error( $message );
},
10,
2
);
}

// The APC cache is not available on the command-line, so bail, to prevent cache poisoning
Expand Down