forked from studio24/wordpress-multi-env-config
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwp-config.load.php
162 lines (141 loc) · 5.37 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
<?php
/**
* WordPress Multi-Environment Config - Load config settings
*
* @package Studio 24 WordPress Multi-Environment Config
* @version 2.0.0
* @author Studio 24 <hello@studio24.net>
*/
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 && !empty(getenv('WP_ENV'))) {
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')) {
foreach ($argv as $arg) {
if (preg_match('/--env=(.+)/', $arg, $m)) {
define('WP_ENV', $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'));
$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));
} elseif (isset($_SERVER['HTTP_HOST'])) {
$hostname = strtolower(filter_var($_SERVER['HTTP_HOST'], FILTER_SANITIZE_STRING));
}
if (!defined('WP_ENV') && empty($hostname)) {
throw new Exception("Cannot determine current environment via WP_ENV or hostname");
}
// 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');
}
$domain = $env_vars['domain'];
if (!is_array($domain)) {
$domain = [$domain];
}
foreach ($domain as $domain_name) {
$wildcard = (strpos($domain_name, '*') !== false) ? true : false;
if ($wildcard) {
$match = '/' . str_replace('*', '([^.]+)', preg_quote($domain_name, '/')) . '/';
if (preg_match($match, $hostname, $m)) {
if (!defined('WP_ENV')) {
define('WP_ENV', $environment);
}
define('WP_ENV_DOMAIN', str_replace('*', $m[1], $domain_name));
if (isset($env_vars['ssl'])) {
define('WP_ENV_SSL', (bool) $env_vars['ssl']);
}
if (isset($env_vars['path'])) {
define('WP_ENV_PATH', trim($env_vars['path'], '/'));
}
break;
}
} elseif ($hostname === $domain_name) {
if (!defined('WP_ENV')) {
define('WP_ENV', $environment);
}
define('WP_ENV_DOMAIN', $domain_name);
if (isset($env_vars['ssl'])) {
define('WP_ENV_SSL', (bool) $env_vars['ssl']);
}
if (isset($env_vars['path'])) {
define('WP_ENV_PATH', trim($env_vars['path'], '/'));
}
break;
}
}
}
/**
* Define WordPress Site URLs
*/
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);
}
$protocol = (WP_ENV_SSL) ? 'https://' : 'http://';
$path = (defined('WP_ENV_PATH')) ? '/' . trim(WP_ENV_PATH, '/') : '';
if (!defined('WP_SITEURL')) {
define('WP_SITEURL', $protocol . trim($hostname, '/') . $path);
}
if (!defined('WP_HOME')) {
define('WP_HOME', $protocol . trim($hostname, '/') . $path);
}
// 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';
}