Skip to content

Commit 8dee816

Browse files
authored
Merge pull request #6042 from wp-cli/fix/6039-followup
2 parents 66777c1 + 35c4d23 commit 8dee816

File tree

2 files changed

+58
-5
lines changed

2 files changed

+58
-5
lines changed

features/config.feature

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -678,3 +678,58 @@ Feature: Have a config file
678678
"""
679679
Error: Strange wp-config.php file: wp-settings.php is not loaded directly.
680680
"""
681+
682+
Scenario: Code after wp-settings.php call should be loaded
683+
Given a WP installation
684+
And a wp-config.php file:
685+
"""
686+
<?php
687+
if ( 1 === 1 ) {
688+
require_once ABSPATH . 'some-other-file.php';
689+
}
690+
691+
define('DB_NAME', '{DB_NAME}');
692+
define('DB_USER', '{DB_USER}');
693+
define('DB_PASSWORD', '{DB_PASSWORD}');
694+
define('DB_HOST', '{DB_HOST}');
695+
define('DB_CHARSET', 'utf8');
696+
define('DB_COLLATE', '');
697+
$table_prefix = 'wp_';
698+
699+
/* That's all, stop editing! Happy publishing. */
700+
701+
/** Sets up WordPress vars and included files. */
702+
require_once
703+
ABSPATH . 'wp-settings.php'
704+
;
705+
706+
require_once ABSPATH . 'includes-file.php';
707+
"""
708+
And a includes-file.php file:
709+
"""
710+
<?php
711+
define( 'MY_CONSTANT', true );
712+
"""
713+
And a some-other-file.php file:
714+
"""
715+
<?php
716+
define( 'MY_OTHER_CONSTANT', true );
717+
"""
718+
719+
When I try `wp core is-installed`
720+
Then STDERR should not contain:
721+
"""
722+
Error: Strange wp-config.php file: wp-settings.php is not loaded directly.
723+
"""
724+
725+
When I run `wp eval 'var_export( defined("MY_CONSTANT") );'`
726+
Then STDOUT should be:
727+
"""
728+
true
729+
"""
730+
731+
When I run `wp eval 'var_export( defined("MY_OTHER_CONSTANT") );'`
732+
Then STDOUT should be:
733+
"""
734+
true
735+
"""

php/WP_CLI/Runner.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -725,16 +725,14 @@ public function get_wp_config_code( $wp_config_path = '' ) {
725725
}
726726
}
727727

728-
$matches = [];
728+
$count = 0;
729729

730-
preg_match_all( '/\s*require(_once)?\s*.*wp-settings\.php/', $wp_config_code, $matches, PREG_OFFSET_CAPTURE );
730+
$wp_config_code = preg_replace( '/\s*require(?:_once)?\s*.*wp-settings\.php.*\s*;/', '', $wp_config_code, -1, $count );
731731

732-
if ( empty( $matches[0] ) ) {
732+
if ( 0 === $count ) {
733733
WP_CLI::error( 'Strange wp-config.php file: wp-settings.php is not loaded directly.' );
734734
}
735735

736-
$wp_config_code = substr( $wp_config_code, 0, $matches[0][0][1] );
737-
738736
$source = Utils\replace_path_consts( $wp_config_code, $wp_config_path );
739737
return preg_replace( '|^\s*\<\?php\s*|', '', $source );
740738
}

0 commit comments

Comments
 (0)