Skip to content

Commit 60262b5

Browse files
Copilotswissspidy
andauthored
Restore wpdb fallback in run_query() and fix WP 4.9 compatibility
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
1 parent 7e177ee commit 60262b5

2 files changed

Lines changed: 38 additions & 65 deletions

File tree

‎features/db.feature‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ Feature: Perform database operations
405405
"""
406406
And STDERR should contain:
407407
"""
408-
Query via mysqli:
408+
Query via wpdb:
409409
"""
410410
411411
@require-mysql-or-mariadb
@@ -430,7 +430,7 @@ Feature: Perform database operations
430430
"""
431431
And STDERR should contain:
432432
"""
433-
Query via mysqli:
433+
Query via wpdb:
434434
"""
435435
436436
# Skipped on Windows due to persistent file locking issues when run via Behat.

‎src/DB_Command.php‎

Lines changed: 36 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1959,55 +1959,6 @@ private static function get_create_query() {
19591959
return $create_query;
19601960
}
19611961

1962-
/**
1963-
* Run a single query via PHP's mysqli extension.
1964-
*
1965-
* Used as a fallback when the MySQL/MariaDB binary is not available.
1966-
* Creates a fresh connection per call so that DDL operations like
1967-
* DROP DATABASE followed by CREATE DATABASE work correctly.
1968-
*
1969-
* @param string $query SQL query to execute.
1970-
*/
1971-
protected function run_query_via_mysqli( $query ) {
1972-
$db_host = DB_HOST;
1973-
$port = null;
1974-
$socket = null;
1975-
1976-
if ( ':' === substr( $db_host, 0, 1 ) ) {
1977-
$socket = substr( $db_host, 1 );
1978-
$db_host = 'localhost';
1979-
} else {
1980-
$parts = explode( ':', $db_host, 2 );
1981-
$db_host = $parts[0];
1982-
if ( isset( $parts[1] ) ) {
1983-
$port_or_socket = trim( $parts[1] );
1984-
if ( '' !== $port_or_socket && '/' === $port_or_socket[0] ) {
1985-
$socket = $port_or_socket;
1986-
} elseif ( '' !== $port_or_socket ) {
1987-
$port = (int) $port_or_socket;
1988-
}
1989-
}
1990-
}
1991-
1992-
// When using a socket, pass 0 for port. When a port is explicitly set, use it; otherwise use the default.
1993-
$port_val = null !== $port ? $port : 0;
1994-
$socket_val = null !== $socket ? $socket : '';
1995-
1996-
// phpcs:ignore WordPress.DB.RestrictedClasses.mysql__mysqli -- direct mysqli required as wpdb fallback when mysql binary is unavailable.
1997-
$conn = new mysqli( $db_host, DB_USER, DB_PASSWORD, '', $port_val, $socket_val );
1998-
1999-
if ( $conn->connect_errno ) {
2000-
WP_CLI::error( sprintf( 'Failed to connect to the database: %s', $conn->connect_error ) );
2001-
}
2002-
2003-
if ( ! $conn->query( $query ) ) {
2004-
$error = $conn->error;
2005-
$conn->close();
2006-
WP_CLI::error( sprintf( 'Query failed: %s', $error ) );
2007-
}
2008-
$conn->close();
2009-
}
2010-
20111962
/**
20121963
* Run a single query via the 'mysql' binary.
20131964
*
@@ -2019,8 +1970,20 @@ protected function run_query_via_mysqli( $query ) {
20191970
*/
20201971
protected function run_query( $query, $assoc_args = [] ) {
20211972
if ( ! $this->is_mysql_binary_available() ) {
2022-
WP_CLI::debug( "Query via mysqli: {$query}", 'db' );
2023-
$this->run_query_via_mysqli( $query );
1973+
WP_CLI::debug( "Query via wpdb: {$query}", 'db' );
1974+
$this->maybe_load_wpdb();
1975+
global $wpdb;
1976+
1977+
if ( ! isset( $wpdb ) ) {
1978+
WP_CLI::error( 'WordPress database (wpdb) could not be initialized. Please ensure WordPress core files are properly installed.' );
1979+
}
1980+
1981+
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
1982+
$result = $wpdb->query( $query );
1983+
if ( false === $result ) {
1984+
// phpcs:ignore WordPress.WP.AlternativeFunctions.strip_tags_strip_tags
1985+
WP_CLI::error( 'Query failed: ' . strip_tags( $wpdb->last_error ) );
1986+
}
20241987
return;
20251988
}
20261989

@@ -2527,22 +2490,32 @@ protected function maybe_load_wpdb() {
25272490
define( 'WP_CONTENT_DIR', ABSPATH . 'wp-content' );
25282491
}
25292492

2530-
// Load required WordPress files if not already loaded.
2493+
// Load prerequisite files if not already loaded.
25312494
if ( ! function_exists( 'add_action' ) ) {
2532-
$required_files = [
2533-
ABSPATH . WPINC . '/load.php',
2534-
ABSPATH . WPINC . '/compat.php',
2535-
ABSPATH . WPINC . '/plugin.php',
2536-
// Defines `wp_debug_backtrace_summary()` as used by wpdb.
2537-
ABSPATH . WPINC . '/functions.php',
2538-
ABSPATH . WPINC . '/class-wpdb.php',
2539-
];
2495+
foreach ( [ 'load.php', 'compat.php', 'plugin.php' ] as $file ) {
2496+
$path = ABSPATH . WPINC . '/' . $file;
2497+
if ( file_exists( $path ) ) {
2498+
require_once $path;
2499+
}
2500+
}
2501+
}
25402502

2541-
foreach ( $required_files as $required_file ) {
2542-
if ( file_exists( $required_file ) ) {
2543-
require_once $required_file;
2503+
// Load class-wpdb.php if the wpdb class is not yet available.
2504+
// This is checked independently of add_action, since WP-CLI's config loading
2505+
// may have already loaded plugin.php (defining add_action) without loading wpdb.
2506+
if ( ! class_exists( 'wpdb' ) ) {
2507+
// Defines `wp_debug_backtrace_summary()` as used by wpdb.
2508+
if ( ! function_exists( 'wp_debug_backtrace_summary' ) ) {
2509+
$functions_file = ABSPATH . WPINC . '/functions.php';
2510+
if ( file_exists( $functions_file ) ) {
2511+
require_once $functions_file;
25442512
}
25452513
}
2514+
2515+
$wpdb_file = ABSPATH . WPINC . '/class-wpdb.php';
2516+
if ( file_exists( $wpdb_file ) ) {
2517+
require_once $wpdb_file;
2518+
}
25462519
}
25472520

25482521
// Load db.php drop-in if it exists (e.g., HyperDB or other custom drivers).

0 commit comments

Comments
 (0)