Skip to content

Commit f5834bc

Browse files
committed
Fix to apply correct domain in WP CLI
1 parent b3362b9 commit f5834bc

File tree

1 file changed

+58
-59
lines changed

1 file changed

+58
-59
lines changed

wp-config.load.php

+58-59
Original file line numberDiff line numberDiff line change
@@ -17,103 +17,102 @@ function s24_load_environment_config() {
1717
global $argv;
1818

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

2424
// Set env via --env=<environment> argument if running via WP-CLI
2525
if (!defined('WP_ENV') && PHP_SAPI == "cli" && defined('WP_CLI_ROOT')) {
26-
foreach ($argv as $arg) {
27-
if (preg_match('/--env=(.+)/', $arg, $m)) {
28-
define('WP_ENV', $m[1]);
29-
break;
26+
27+
if (isset($argv)) {
28+
foreach ($argv as $arg) {
29+
if (preg_match('/--env=(.+)/', $arg, $m)) {
30+
define('WP_ENV', $m[1]);
31+
break;
32+
}
3033
}
3134
}
35+
3236
// Also support via .env file in config directory
3337
if (!defined('WP_ENV')) {
3438
if (file_exists(__DIR__ . '/.env')) {
3539
$environment = trim(file_get_contents(__DIR__ . '/.env'));
36-
$value = preg_replace('/[^a-z]/', '', $environment);
37-
if (!empty($value)) {
38-
define('WP_ENV', $value);
39-
}
40+
define('WP_ENV', preg_replace('/[^a-z]/', '', $environment));
4041
}
4142
}
4243
}
4344

44-
// Define site host
45-
if (isset($_SERVER['HTTP_X_FORWARDED_HOST']) && !empty($_SERVER['HTTP_X_FORWARDED_HOST'])) {
46-
$hostname = strtolower(filter_var($_SERVER['HTTP_X_FORWARDED_HOST'], FILTER_SANITIZE_STRING));
47-
} elseif (isset($_SERVER['HTTP_HOST'])) {
48-
$hostname = strtolower(filter_var($_SERVER['HTTP_HOST'], FILTER_SANITIZE_STRING));
49-
}
50-
51-
if (!defined('WP_ENV') && empty($hostname)) {
52-
throw new Exception("Cannot determine current environment via WP_ENV or hostname");
45+
// Define ENV from hostname
46+
if (!defined('WP_ENV')) {
47+
if (isset($_SERVER['HTTP_X_FORWARDED_HOST']) && !empty($_SERVER['HTTP_X_FORWARDED_HOST'])) {
48+
$hostname = strtolower(filter_var($_SERVER['HTTP_X_FORWARDED_HOST'], FILTER_SANITIZE_STRING));
49+
} else {
50+
$hostname = strtolower(filter_var($_SERVER['HTTP_HOST'], FILTER_SANITIZE_STRING));
51+
}
5352
}
5453

5554
// Load environments
5655
require __DIR__ . '/wp-config.env.php';
5756

58-
/*
59-
* If the hostname isn't already defined (if we are interacting with WordPress
60-
* via the CLI for example) then get the Hostname using the WP_ENV environment
61-
* variable
62-
*/
63-
if (empty($hostname) && isset($env[WP_ENV])) {
64-
if (is_array($env[WP_ENV]['domain'])) {
65-
// Take first defined domain if config has an array of domains
66-
$hostname = $env[WP_ENV]['domain'][0];
67-
} else {
68-
$hostname = $env[WP_ENV]['domain'];
57+
// Set environment constants
58+
if (defined('WP_ENV')) {
59+
if (isset($env[WP_ENV])) {
60+
define('WP_ENV_DOMAIN', $env[WP_ENV]['domain']);
61+
define('WP_ENV_PATH', trim($env[WP_ENV]['path'], '/'));
62+
define('WP_ENV_SSL', (bool) $env[WP_ENV]['ssl']);
6963
}
70-
}
7164

72-
if (empty($hostname)) {
73-
throw new Exception("Cannot determine current WordPress domain");
74-
}
65+
} else {
7566

76-
foreach ($env as $environment => $env_vars) {
77-
if (!isset($env_vars['domain'])) {
78-
throw new Exception('You must set the domain value in your environment array, see wp-config.env.php');
79-
}
80-
$domain = $env_vars['domain'];
81-
if (!is_array($domain)) {
82-
$domain = [$domain];
83-
}
84-
foreach ($domain as $domain_name) {
85-
$wildcard = (strpos($domain_name, '*') !== false) ? true : false;
67+
// Detect environment from hostname
68+
foreach ($env as $environment => $env_vars) {
69+
if (!isset($env_vars['domain'])) {
70+
throw new Exception('You must set the domain value in your environment array, see wp-config.env.php');
71+
}
72+
$domain = $env_vars['domain'];
73+
74+
$wildcard = (strpos($domain, '*') !== false) ? true : false;
8675
if ($wildcard) {
87-
$match = '/' . str_replace('*', '([^.]+)', preg_quote($domain_name, '/')) . '/';
76+
$match = '/' . str_replace('*', '([^.]+)', preg_quote($domain, '/')) . '/';
8877
if (preg_match($match, $hostname, $m)) {
8978
if (!defined('WP_ENV')) {
9079
define('WP_ENV', $environment);
9180
}
92-
define('WP_ENV_DOMAIN', str_replace('*', $m[1], $domain_name));
81+
define('WP_ENV_DOMAIN', str_replace('*', $m[1], $domain));
9382
if (isset($env_vars['ssl'])) {
94-
define('WP_ENV_SSL', (bool) $env_vars['ssl']);
83+
define('WP_ENV_SSL', (bool)$env_vars['ssl']);
9584
}
9685
if (isset($env_vars['path'])) {
9786
define('WP_ENV_PATH', trim($env_vars['path'], '/'));
9887
}
9988
break;
10089
}
101-
} elseif ($hostname === $domain_name) {
102-
if (!defined('WP_ENV')) {
103-
define('WP_ENV', $environment);
104-
}
105-
define('WP_ENV_DOMAIN', $domain_name);
106-
if (isset($env_vars['ssl'])) {
107-
define('WP_ENV_SSL', (bool) $env_vars['ssl']);
108-
}
109-
if (isset($env_vars['path'])) {
110-
define('WP_ENV_PATH', trim($env_vars['path'], '/'));
90+
}
91+
if (!is_array($domain)) {
92+
$domain = [$domain];
93+
}
94+
foreach ($domain as $domain_name) {
95+
if ($hostname === $domain_name) {
96+
if (!defined('WP_ENV')) {
97+
define('WP_ENV', $environment);
98+
}
99+
define('WP_ENV_DOMAIN', $domain_name);
100+
if (isset($env_vars['ssl'])) {
101+
define('WP_ENV_SSL', (bool)$env_vars['ssl']);
102+
}
103+
if (isset($env_vars['path'])) {
104+
define('WP_ENV_PATH', trim($env_vars['path'], '/'));
105+
}
106+
break;
111107
}
112-
break;
113108
}
114109
}
115110
}
116111

112+
if (!defined('WP_ENV')) {
113+
throw new Exception("Cannot determine current environment");
114+
}
115+
117116
/**
118117
* Define WordPress Site URLs
119118
*/
@@ -131,10 +130,10 @@ function s24_load_environment_config() {
131130
$path = (defined('WP_ENV_PATH')) ? '/' . trim(WP_ENV_PATH, '/') : '';
132131

133132
if (!defined('WP_SITEURL')) {
134-
define('WP_SITEURL', $protocol . trim($hostname, '/') . $path);
133+
define('WP_SITEURL', $protocol . trim(WP_ENV_DOMAIN, '/') . $path);
135134
}
136135
if (!defined('WP_HOME')) {
137-
define('WP_HOME', $protocol . trim($hostname, '/') . $path);
136+
define('WP_HOME', $protocol . trim(WP_ENV_DOMAIN, '/') . $path);
138137
}
139138

140139
// Define W3 Total Cache hostname

0 commit comments

Comments
 (0)