-
Notifications
You must be signed in to change notification settings - Fork 97
/
Copy pathwp-config.load.php
180 lines (155 loc) · 6.34 KB
/
wp-config.load.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
/**
* WordPress Multi-Environment Config - Load config settings
*
* @package Studio 24 WordPress Multi-Environment Config
* @version 2.0.1 (x-release-please-version)
* @author Studio 24 <[email protected]>
*/
function s24_load_environment_config() {
/**
* Setup environment
*/
// Set env if set via environment variable
if (getenv('WP_ENV') !== false) {
define('WP_ENV', preg_replace('/[^a-z]/', '', getenv('WP_ENV')));
}
// Set env via --env=<environment> argument if running via WP-CLI
if (!defined('WP_ENV') && PHP_SAPI == "cli" && defined('WP_CLI_ROOT')) {
// We need to set $argv as global to be able to access it
global $argv;
if (isset($argv)) {
foreach ($argv as $arg) {
if (preg_match('/--env=(.+)/', $arg, $m)) {
define('WP_ENV', preg_replace('/[^a-z]/', '', $m[1]));
break;
}
}
}
// Also support via .env file in config directory
if (!defined('WP_ENV')) {
if (file_exists(__DIR__ . '/.env')) {
$environment = trim(file_get_contents(__DIR__ . '/.env'));
define('WP_ENV', preg_replace('/[^a-z]/', '', $environment));
}
}
}
// Define ENV from hostname
if (!defined('WP_ENV')) {
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 {
$hostname = strtolower(filter_var($_SERVER['HTTP_HOST'], FILTER_SANITIZE_STRING));
}
}
// Load environments
require __DIR__ . '/wp-config.env.php';
if (!isset($env) || !is_array($env)) {
throw new Exception('$env array not detected, you must set this in wp-config.env.php');
}
// Set environment constants
if (defined('WP_ENV')) {
if (isset($env[WP_ENV])) {
define('WP_ENV_DOMAIN', $env[WP_ENV]['domain']);
define('WP_ENV_PATH', trim($env[WP_ENV]['path'], '/'));
define('WP_ENV_SSL', (bool) $env[WP_ENV]['ssl']);
}
} else {
// Detect environment from hostname
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');
}
$domain = $env_vars['domain'];
$wildcard = (is_string($domain) && strpos($domain, '*') !== false) ? true : false;
if ($wildcard) {
$match = '/' . str_replace('\*', '([^.]+)', preg_quote($domain, '/')) . '/';
if (preg_match($match, $hostname, $m)) {
if (!defined('WP_ENV')) {
define('WP_ENV', preg_replace('/[^a-z]/', '', $environment));
}
define('WP_ENV_DOMAIN', str_replace('*', $m[1], $domain));
if (isset($env_vars['ssl'])) {
define('WP_ENV_SSL', (bool)$env_vars['ssl']);
} else {
define('WP_ENV_SSL', false);
}
if (isset($env_vars['path'])) {
define('WP_ENV_PATH', trim($env_vars['path'], '/'));
}
/**
* Define WordPress Site URLs
*/
$protocol = (WP_ENV_SSL) ? 'https://' : 'http://';
$path = (defined('WP_ENV_PATH')) ? '/' . trim(WP_ENV_PATH, '/') : '';
if (!defined('WP_SITEURL')) {
define('WP_SITEURL', $protocol . trim(WP_ENV_DOMAIN, '/') . $path);
}
if (!defined('WP_HOME')) {
define('WP_HOME', $protocol . trim(WP_ENV_DOMAIN, '/') . $path);
}
break;
}
}
if (!is_array($domain)) {
$domain = [$domain];
}
foreach ($domain as $domain_name) {
if ($hostname === $domain_name) {
if (!defined('WP_ENV')) {
define('WP_ENV', preg_replace('/[^a-z]/', '', $environment));
}
define('WP_ENV_DOMAIN', $domain_name);
if (isset($env_vars['ssl'])) {
define('WP_ENV_SSL', (bool)$env_vars['ssl']);
} else {
define('WP_ENV_SSL', false);
}
if (isset($env_vars['path'])) {
define('WP_ENV_PATH', trim($env_vars['path'], '/'));
}
/**
* Define WordPress Site URLs
*/
$protocol = (WP_ENV_SSL) ? 'https://' : 'http://';
$path = (defined('WP_ENV_PATH')) ? '/' . trim(WP_ENV_PATH, '/') : '';
if (!defined('WP_SITEURL')) {
define('WP_SITEURL', $protocol . trim(WP_ENV_DOMAIN, '/') . $path);
}
if (!defined('WP_HOME')) {
define('WP_HOME', $protocol . trim(WP_ENV_DOMAIN, '/') . $path);
}
break;
}
}
}
}
if (!defined('WP_ENV')) {
throw new Exception("Cannot determine current environment");
}
if (!defined('WP_ENV_DOMAIN')) {
throw new Exception("Cannot determine current environment domain, make sure this is set in wp-config.env.php");
}
if (!defined('WP_ENV_SSL')) {
define('WP_ENV_SSL', false);
}
if (WP_ENV_SSL && (!defined('FORCE_SSL_ADMIN'))) {
define('FORCE_SSL_ADMIN', true);
}
// Define W3 Total Cache hostname
if (defined('WP_CACHE')) {
define('COOKIE_DOMAIN', $hostname);
}
}
s24_load_environment_config();
/**
* Load config
*/
// 1st - Load default config
require __DIR__ . '/wp-config.default.php';
// 2nd - Load config file for current environment
require __DIR__ . '/wp-config.' . WP_ENV . '.php';
// 3rd - Load local config file with any sensitive settings
if (file_exists( __DIR__ . '/wp-config.local.php')) {
require __DIR__ . '/wp-config.local.php';
}