Skip to content

Commit 66777c1

Browse files
authored
Merge pull request #6039 from wp-cli/fix/6038-wp-config
2 parents ba1ccab + 030b66e commit 66777c1

File tree

2 files changed

+62
-17
lines changed

2 files changed

+62
-17
lines changed

features/config.feature

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -625,3 +625,56 @@ Feature: Have a config file
625625
"""
626626
Warning: UTF-8 byte-order mark (BOM) detected in wp-config.php file, stripping it for parsing.
627627
"""
628+
629+
Scenario: Strange wp-config.php file with missing wp-settings.php call
630+
Given a WP installation
631+
And a wp-config.php file:
632+
"""
633+
<?php
634+
define('DB_NAME', '{DB_NAME}');
635+
define('DB_USER', '{DB_USER}');
636+
define('DB_PASSWORD', '{DB_PASSWORD}');
637+
define('DB_HOST', '{DB_HOST}');
638+
define('DB_CHARSET', 'utf8');
639+
define('DB_COLLATE', '');
640+
$table_prefix = 'wp_';
641+
642+
/* That's all, stop editing! Happy publishing. */
643+
"""
644+
645+
When I try `wp core is-installed`
646+
Then STDERR should contain:
647+
"""
648+
Error: Strange wp-config.php file: wp-settings.php is not loaded directly.
649+
"""
650+
651+
Scenario: Strange wp-config.php file with multi-line wp-settings.php call
652+
Given a WP installation
653+
And a wp-config.php file:
654+
"""
655+
<?php
656+
if ( 1 === 1 ) {
657+
require_once ABSPATH . 'some-other-file.php';
658+
}
659+
660+
define('DB_NAME', '{DB_NAME}');
661+
define('DB_USER', '{DB_USER}');
662+
define('DB_PASSWORD', '{DB_PASSWORD}');
663+
define('DB_HOST', '{DB_HOST}');
664+
define('DB_CHARSET', 'utf8');
665+
define('DB_COLLATE', '');
666+
$table_prefix = 'wp_';
667+
668+
/* That's all, stop editing! Happy publishing. */
669+
670+
/** Sets up WordPress vars and included files. */
671+
require_once
672+
ABSPATH . 'wp-settings.php'
673+
;
674+
"""
675+
676+
When I try `wp core is-installed`
677+
Then STDERR should not contain:
678+
"""
679+
Error: Strange wp-config.php file: wp-settings.php is not loaded directly.
680+
"""

php/WP_CLI/Runner.php

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -707,7 +707,7 @@ public function get_wp_config_code( $wp_config_path = '' ) {
707707
$wp_config_path = Utils\locate_wp_config();
708708
}
709709

710-
$wp_config_code = explode( "\n", file_get_contents( $wp_config_path ) );
710+
$wp_config_code = file_get_contents( $wp_config_path );
711711

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

717717
$length = strlen( $bom_sequence );
718718

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

724-
$wp_config_code[0] = substr( $wp_config_code[0], $length );
724+
$wp_config_code = substr( $wp_config_code, $length );
725725
}
726726
}
727727

728-
$found_wp_settings = false;
728+
$matches = [];
729729

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

732-
foreach ( $wp_config_code as $line ) {
733-
if ( preg_match( '/^\s*require.+wp-settings\.php/', $line ) ) {
734-
$found_wp_settings = true;
735-
continue;
736-
}
737-
738-
$lines_to_run[] = $line;
739-
}
740-
741-
if ( ! $found_wp_settings ) {
732+
if ( empty( $matches[0] ) ) {
742733
WP_CLI::error( 'Strange wp-config.php file: wp-settings.php is not loaded directly.' );
743734
}
744735

745-
$source = implode( "\n", $lines_to_run );
746-
$source = Utils\replace_path_consts( $source, $wp_config_path );
736+
$wp_config_code = substr( $wp_config_code, 0, $matches[0][0][1] );
737+
738+
$source = Utils\replace_path_consts( $wp_config_code, $wp_config_path );
747739
return preg_replace( '|^\s*\<\?php\s*|', '', $source );
748740
}
749741

0 commit comments

Comments
 (0)