Skip to content

Commit bda46ea

Browse files
authored
Don't require WordPress to be fully loaded in WP CLI (#205)
2 parents e7e411d + da1ffa7 commit bda46ea

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

wp-includes/sqlite-ast/class-wp-sqlite-information-schema-reconstructor.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,25 @@ private function get_information_schema_table_names(): array {
168168
* @return array<string, WP_Parser_Node> The WordPress CREATE TABLE statements.
169169
*/
170170
private function get_wp_create_table_statements(): array {
171+
// Bail out when not in a WordPress environment.
171172
if ( ! defined( 'ABSPATH' ) ) {
172173
return array();
173174
}
175+
176+
/*
177+
* In WP CLI, $wpdb may not be set. In that case, we can't load the schema.
178+
* We need to bail out and use the standard non-WordPress-specific behavior.
179+
*/
180+
global $wpdb;
181+
if ( ! isset( $wpdb ) ) {
182+
// Outside of WP CLI, let's trigger a warning.
183+
if ( ! defined( 'WP_CLI' ) || ! WP_CLI ) {
184+
trigger_error( 'The $wpdb global is not initialized.', E_USER_WARNING );
185+
}
186+
return array();
187+
}
188+
189+
// Ensure the "wp_get_db_schema()" function is defined.
174190
if ( file_exists( ABSPATH . 'wp-admin/includes/schema.php' ) ) {
175191
require_once ABSPATH . 'wp-admin/includes/schema.php';
176192
}
@@ -183,7 +199,7 @@ private function get_wp_create_table_statements(): array {
183199
* the database connection. Let's only populate the table names using
184200
* the "$table_prefix" global so we can get correct table names.
185201
*/
186-
global $wpdb, $table_prefix;
202+
global $table_prefix;
187203
$wpdb->set_prefix( $table_prefix );
188204

189205
// Get schema for global tables.

0 commit comments

Comments
 (0)