-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathConfig.php
136 lines (109 loc) · 3.4 KB
/
Config.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
<?php
namespace NYCO;
/**
* Dependencies
*/
use Spyc;
use Illuminate;
use Exception;
/**
* The Config Class
*/
class Config {
const PROTECT = ['WP_ENV']; // List of protected env variables
/** The path to the configuration directory */
public $path = ABSPATH . 'wp-content/mu-plugins/config/';
private $config = 'config.yml';
private $env = 'env.php';
private $default = 'default.php';
/** Placeholder for environment variables */
private $envs = array();
/** List of allowable filters */
private $allowed_filters = [
'__return_false',
'__return_true',
'__return_empty_array',
'__return_zero',
'__return_null',
'__return_empty_string'
];
public function __construct($secret = false) {
$this->secret = $secret;
/**
* Auto load default environment
*/
if (file_exists($this->path . $this->default)) {
require_once $this->path . $this->default;
}
/**
* Auto load environment file if it exits
* Define WP_ENV in the root wp-config.php before the wp-settings.php include
*/
if (defined('WP_ENV') && file_exists($this->path . WP_ENV . '.php')) {
require_once $this->path . WP_ENV . '.php';
}
if (file_exists($this->path . $this->config)) {
$config = Spyc::YAMLLoad($this->path . $this->config);
// If there is a secret, then assume the file is encrypted
if ($secret) {
$secret = require_once(__DIR__ . '/' . $this->env);
$encrypter = new \Illuminate\Encryption\Encrypter($secret['key']);
}
/**
* Set configuration to environment variables
*/
// Extract env specific variables
if (defined('WP_ENV') && isset($config[WP_ENV])) {
$this->envs = $config[WP_ENV];
}
// Merge env variables over root variables
$this->set(array_merge($config, $this->envs));
} elseif (defined('WP_DEBUG') && WP_DEBUG) {
throw new Exception(
"The configuration file or secret could not be found at $this->path."
);
} else {
error_log(
"The configuration file or secret could not be found at $this->path."
);
}
/**
* A hook for the initiation of the plugin
*/
do_action('nyco_wp_config_loaded', $this);
}
/**
* Set configuration to environment variables
*
* @param Array $config An array containing environment variables to set.
* Variables containing arrays or objects are not set.
*/
private function set($config) {
foreach ($config as $key => $value) {
if (!is_array($value) && !is_object($value)) {
$name = strtoupper($key);
if (!in_array($name, Config::PROTECT)) {
$decrypted = ($this->secret) ? $encrypter->decrypt($value) : $value;
/**
* Define the constant
*/
define($name, $decrypted);
/**
* Update WordPress Admin option if it is an option
*/
if (substr($name, 0, 10) === 'WP_OPTION_') {
update_option(
strtolower(str_replace('WP_OPTION_', '', $name)), $decrypted
);
}
/**
* Create a filter if the option is a filter
*/
if (substr($name, 0, 10) === 'WP_FILTER_' && in_array($decrypted, $allowed_filters)) {
add_filter(strtolower(str_replace('WP_FILTER_', '', $name)), $decrypted);
}
}
}
}
}
}