Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions features/config-create.feature
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,23 @@ Feature: Create a wp-config file
Then the return code should be 0
And the subdir/wp-config.php file should exist

@require-sqlite
Scenario: Configure without --dbname and --dbuser when SQLite integration is active
Given an empty directory
And WP files
And a wp-content/db.php file:
"""
<?php
define( 'SQLITE_DB_DROPIN_VERSION', '1.0.0' );
"""

When I run `wp config create --skip-salts --skip-check`
Then the return code should be 0
And STDOUT should contain:
"""
Generated 'wp-config.php' file.
"""

@require-mysql @require-mysql-5.7
Scenario: Configure with required SSL connection
Given an empty directory
Expand Down
37 changes: 34 additions & 3 deletions src/Config_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,10 @@ private static function get_initial_locale() {
*
* ## OPTIONS
*
* --dbname=<dbname>
* [--dbname=<dbname>]
* : Set the database name.
*
* --dbuser=<dbuser>
* [--dbuser=<dbuser>]
* : Set the database user.
*
* [--dbpass=<dbpass>]
Expand Down Expand Up @@ -219,6 +219,18 @@ public function create( $_, $assoc_args ) {
'ssl' => false,
];
$assoc_args = array_merge( $defaults, $assoc_args );

$is_sqlite = self::is_sqlite_integration_active();

if ( ! $is_sqlite ) {
if ( empty( $assoc_args['dbname'] ) ) {
WP_CLI::error( 'Parameter errors:' . PHP_EOL . 'missing --dbname parameter (Set the database name.)' );
}
if ( empty( $assoc_args['dbuser'] ) ) {
WP_CLI::error( 'Parameter errors:' . PHP_EOL . 'missing --dbuser parameter (Set the database user.)' );
}
}

if ( empty( $assoc_args['dbprefix'] ) ) {
WP_CLI::error( '--dbprefix cannot be empty' );
}
Expand All @@ -228,7 +240,7 @@ public function create( $_, $assoc_args ) {

// Check DB connection. To make command more portable, we are not using MySQL CLI and using
// mysqli directly instead, as $wpdb is not accessible in this context.
if ( ! Utils\get_flag_value( $assoc_args, 'skip-check' ) ) {
if ( ! $is_sqlite && ! Utils\get_flag_value( $assoc_args, 'skip-check' ) ) {
// phpcs:disable WordPress.DB.RestrictedFunctions
$mysql = mysqli_init();

Expand Down Expand Up @@ -1480,6 +1492,25 @@ private function print_dotenv( array $value ) {
WP_CLI::line( "{$name}={$variable_value}" );
}

/**
* Check if the SQLite integration drop-in is active.
*
* @return bool True if SQLite integration is detected, false otherwise.
*/
private static function is_sqlite_integration_active() {
$wp_content_dir = defined( 'WP_CONTENT_DIR' ) ? WP_CONTENT_DIR : ABSPATH . 'wp-content';
$db_dropin_path = $wp_content_dir . '/db.php';

if ( file_exists( $db_dropin_path ) ) {
$db_dropin_contents = file_get_contents( $db_dropin_path, false, null, 0, 8192 );
if ( false !== $db_dropin_contents && false !== strpos( $db_dropin_contents, 'SQLITE_DB_DROPIN_VERSION' ) ) {
return true;
}
}

return false;
}

/**
* Escape a config value so it can be safely used within single quotes.
*
Expand Down
Loading