Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Amended the file to ensure that it works correctly with the WP CLI (… #25

Merged
merged 4 commits into from
May 31, 2018
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
30 changes: 27 additions & 3 deletions wp-config.load.php
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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