Skip to content
Merged
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
53 changes: 53 additions & 0 deletions features/config.feature
Original file line number Diff line number Diff line change
Expand Up @@ -625,3 +625,56 @@ Feature: Have a config file
"""
Warning: UTF-8 byte-order mark (BOM) detected in wp-config.php file, stripping it for parsing.
"""

Scenario: Strange wp-config.php file with missing wp-settings.php call
Given a WP installation
And a wp-config.php file:
"""
<?php
define('DB_NAME', '{DB_NAME}');
define('DB_USER', '{DB_USER}');
define('DB_PASSWORD', '{DB_PASSWORD}');
define('DB_HOST', '{DB_HOST}');
define('DB_CHARSET', 'utf8');
define('DB_COLLATE', '');
$table_prefix = 'wp_';

/* That's all, stop editing! Happy publishing. */
"""

When I try `wp core is-installed`
Then STDERR should contain:
"""
Error: Strange wp-config.php file: wp-settings.php is not loaded directly.
"""

Scenario: Strange wp-config.php file with multi-line wp-settings.php call
Given a WP installation
And a wp-config.php file:
"""
<?php
if ( 1 === 1 ) {
require_once ABSPATH . 'some-other-file.php';
}

define('DB_NAME', '{DB_NAME}');
define('DB_USER', '{DB_USER}');
define('DB_PASSWORD', '{DB_PASSWORD}');
define('DB_HOST', '{DB_HOST}');
define('DB_CHARSET', 'utf8');
define('DB_COLLATE', '');
$table_prefix = 'wp_';

/* That's all, stop editing! Happy publishing. */

/** Sets up WordPress vars and included files. */
require_once
ABSPATH . 'wp-settings.php'
;
"""

When I try `wp core is-installed`
Then STDERR should not contain:
"""
Error: Strange wp-config.php file: wp-settings.php is not loaded directly.
"""
26 changes: 9 additions & 17 deletions php/WP_CLI/Runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -707,7 +707,7 @@ public function get_wp_config_code( $wp_config_path = '' ) {
$wp_config_path = Utils\locate_wp_config();
}

$wp_config_code = explode( "\n", file_get_contents( $wp_config_path ) );
$wp_config_code = file_get_contents( $wp_config_path );

// Detect and strip byte-order marks (BOMs).
// This code assumes they can only be found on the first line.
Expand All @@ -716,34 +716,26 @@ public function get_wp_config_code( $wp_config_path = '' ) {

$length = strlen( $bom_sequence );

while ( substr( $wp_config_code[0], 0, $length ) === $bom_sequence ) {
while ( substr( $wp_config_code, 0, $length ) === $bom_sequence ) {
WP_CLI::warning(
"{$bom_name} byte-order mark (BOM) detected in wp-config.php file, stripping it for parsing."
);

$wp_config_code[0] = substr( $wp_config_code[0], $length );
$wp_config_code = substr( $wp_config_code, $length );
}
}

$found_wp_settings = false;
$matches = [];

$lines_to_run = [];
preg_match_all( '/\s*require(_once)?\s*.*wp-settings\.php/', $wp_config_code, $matches, PREG_OFFSET_CAPTURE );

foreach ( $wp_config_code as $line ) {
if ( preg_match( '/^\s*require.+wp-settings\.php/', $line ) ) {
$found_wp_settings = true;
continue;
}

$lines_to_run[] = $line;
}

if ( ! $found_wp_settings ) {
if ( empty( $matches[0] ) ) {
WP_CLI::error( 'Strange wp-config.php file: wp-settings.php is not loaded directly.' );
}

$source = implode( "\n", $lines_to_run );
$source = Utils\replace_path_consts( $source, $wp_config_path );
$wp_config_code = substr( $wp_config_code, 0, $matches[0][0][1] );

$source = Utils\replace_path_consts( $wp_config_code, $wp_config_path );
return preg_replace( '|^\s*\<\?php\s*|', '', $source );
}

Expand Down
Loading