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
55 changes: 55 additions & 0 deletions features/config.feature
Original file line number Diff line number Diff line change
Expand Up @@ -678,3 +678,58 @@ Feature: Have a config file
"""
Error: Strange wp-config.php file: wp-settings.php is not loaded directly.
"""

Scenario: Code after wp-settings.php call should be loaded
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'
;

require_once ABSPATH . 'includes-file.php';
"""
And a includes-file.php file:
"""
<?php
define( 'MY_CONSTANT', true );
"""
And a some-other-file.php file:
"""
<?php
define( 'MY_OTHER_CONSTANT', true );
"""

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.
"""

When I run `wp eval 'var_export( defined("MY_CONSTANT") );'`
Then STDOUT should be:
"""
true
"""

When I run `wp eval 'var_export( defined("MY_OTHER_CONSTANT") );'`
Then STDOUT should be:
"""
true
"""
8 changes: 3 additions & 5 deletions php/WP_CLI/Runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -725,16 +725,14 @@ public function get_wp_config_code( $wp_config_path = '' ) {
}
}

$matches = [];
$count = 0;

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

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

$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