Skip to content

Commit b327fff

Browse files
committed
Add further checks to make code more robust
1 parent c4884a7 commit b327fff

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

wp-config.load.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ function s24_load_environment_config() {
1717
global $argv;
1818

1919
// Set env if set via environment variable
20-
if (getenv('WP_ENV') !== false) {
20+
if (getenv('WP_ENV') !== false && !empty(getenv('WP_ENV'))) {
2121
define('WP_ENV', preg_replace('/[^a-z]/', '', getenv('WP_ENV')));
2222
}
2323

@@ -33,15 +33,18 @@ function s24_load_environment_config() {
3333
if (!defined('WP_ENV')) {
3434
if (file_exists(__DIR__ . '/.env')) {
3535
$environment = trim(file_get_contents(__DIR__ . '/.env'));
36-
define('WP_ENV', preg_replace('/[^a-z]/', '', $environment));
36+
$value = preg_replace('/[^a-z]/', '', $environment);
37+
if (!empty($value)) {
38+
define('WP_ENV', preg_replace('/[^a-z]/', '', $environment));
39+
}
3740
}
3841
}
3942
}
4043

4144
// Define site host
4245
if (isset($_SERVER['HTTP_X_FORWARDED_HOST']) && !empty($_SERVER['HTTP_X_FORWARDED_HOST'])) {
4346
$hostname = strtolower(filter_var($_SERVER['HTTP_X_FORWARDED_HOST'], FILTER_SANITIZE_STRING));
44-
} else {
47+
} elseif (isset($_SERVER['HTTP_HOST'])) {
4548
$hostname = strtolower(filter_var($_SERVER['HTTP_HOST'], FILTER_SANITIZE_STRING));
4649
}
4750

@@ -57,10 +60,14 @@ function s24_load_environment_config() {
5760
* via the CLI for example) then get the Hostname using the WP_ENV environment
5861
* variable
5962
*/
60-
if(empty($hostname)) {
63+
if (empty($hostname)) {
6164
$hostname = $env[WP_ENV]['domain'];
6265
}
6366

67+
if (empty($hostname)) {
68+
throw new Exception("Cannot determine current WordPress domain");
69+
}
70+
6471
foreach ($env as $environment => $env_vars) {
6572
if (!isset($env_vars['domain'])) {
6673
throw new Exception('You must set the domain value in your environment array, see wp-config.env.php');

wp-config.local.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
/**
33
* Local settings, you should keep these outside of version control
44
*
5+
* Please note this file is loaded first, then the environment config is loaded.
6+
* Don't duplicate any WP config settings in this file and the environment file
7+
* since WordPress uses constants and you'll raise a PHP notice error.
8+
*
59
* Enter any WordPress config settings that are specific to the local environment
610
* in this file.
711
*

0 commit comments

Comments
 (0)