Skip to content

Commit

Permalink
Merge pull request #25 from studio24/hotfix/fix-wp-cli-issue-21
Browse files Browse the repository at this point in the history
Amended the  file to ensure that it works correctly with the WP CLI (…
  • Loading branch information
simonrjones committed May 31, 2018
2 parents c334883 + f7d2e71 commit 322ec39
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
30 changes: 27 additions & 3 deletions wp-config.load.php
Expand Up @@ -13,8 +13,11 @@ function s24_load_environment_config() {
* Setup environment
*/

// We need to set $argv as global to be able to access it
global $argv;

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

Expand All @@ -30,15 +33,18 @@ function s24_load_environment_config() {
if (!defined('WP_ENV')) {
if (file_exists(__DIR__ . '/.env')) {
$environment = trim(file_get_contents(__DIR__ . '/.env'));
define('WP_ENV', preg_replace('/[^a-z]/', '', $environment));
$value = preg_replace('/[^a-z]/', '', $environment);
if (!empty($value)) {
define('WP_ENV', $value);
}
}
}
}

// Define site host
if (isset($_SERVER['HTTP_X_FORWARDED_HOST']) && !empty($_SERVER['HTTP_X_FORWARDED_HOST'])) {
$hostname = strtolower(filter_var($_SERVER['HTTP_X_FORWARDED_HOST'], FILTER_SANITIZE_STRING));
} else {
} elseif (isset($_SERVER['HTTP_HOST'])) {
$hostname = strtolower(filter_var($_SERVER['HTTP_HOST'], FILTER_SANITIZE_STRING));
}

Expand All @@ -49,6 +55,24 @@ function s24_load_environment_config() {
// Load environments
require __DIR__ . '/wp-config.env.php';

/*
* If the hostname isn't already defined (if we are interacting with WordPress
* via the CLI for example) then get the Hostname using the WP_ENV environment
* variable
*/
if (empty($hostname) && isset($env[WP_ENV])) {
if (is_array($env[WP_ENV]['domain'])) {
// Take first defined domain if config has an array of domains
$hostname = $env[WP_ENV]['domain'][0];
} else {
$hostname = $env[WP_ENV]['domain'];
}
}

if (empty($hostname)) {
throw new Exception("Cannot determine current WordPress domain");
}

foreach ($env as $environment => $env_vars) {
if (!isset($env_vars['domain'])) {
throw new Exception('You must set the domain value in your environment array, see wp-config.env.php');
Expand Down
4 changes: 4 additions & 0 deletions wp-config.local.php
Expand Up @@ -2,6 +2,10 @@
/**
* Local settings, you should keep these outside of version control
*
* Please note this file is loaded first, then the environment config is loaded.
* Don't duplicate any WP config settings in this file and the environment file
* since WordPress uses constants and you'll raise a PHP notice error.
*
* Enter any WordPress config settings that are specific to the local environment
* in this file.
*
Expand Down

0 comments on commit 322ec39

Please sign in to comment.