-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclass.php
226 lines (205 loc) · 6.43 KB
/
class.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
<?php
namespace knerd;
/**
* WP_Config
* basic class to handle loading and maintaining default constants
* supports quick setup of where wordpress lives and where you want your content directory to be
* WP_DEBUG can also be turned on by using environment vars
* @version 1.0.0
* @author Knerd
* @see README.md
*/
class WP_Config{
private $CONSTANTS;
private $CUSTOM = [
'DIR',
'DOCROOT',
'SITE_SCHEME',
'WP_CONTENT',
'WP_DIR'
];
private $DEFAULTS;
private $DIR;
private $VENDOR_DIR;
/**
* __construct
* pass defaults to class to define default constants
* will autoload composer if found and define any constants not set in local config
*
* @param mixed $DEFAULTS
* @return void
*/
function __construct( $defaults, $autoload = true ) {
$this->fix_https();
$this->set_defaults( $defaults );
$this->set_vendor_dir( $this->get_vendor_dir() );
$this->set_constants( $this->get_constants() );
if( $autoload )
$this->autoload();
}
// HELP WORDPRESS DETECT WHEN IT’S LOADED OVER HTTPS!
private function fix_https(){
$httpForwarded = isset($_SERVER['HTTP_X_FORWARDED_PROTO']) ? $_SERVER['HTTP_X_FORWARDED_PROTO'] : false;
$_SERVER['HTTPS'] = ($httpForwarded === 'https') ? 'on' : '';
}
/**
* set_defaults
* @param mixed $defaults
* @return void
*/
public function set_defaults( $defaults ){
$this->DEFAULTS = $defaults;
}
public function set_vendor_dir( $vendor_dir ){
$this->VENDOR_DIR = $vendor_dir;
}
public function set_constants( $constants ){
$this->CONSTANTS = $constants;
}
/**
* get_vendor_dir
* reads vendor-dir setting if found in composer.json
* otherwise defaults to vendor
*
* @return string
*/
public function get_vendor_dir(){
$this->DIR = $this->DEFAULTS['DIR'];
if(!$this->DIR)
throw new Exception('DIR not specified', 1);
$json = "{$this->DIR}/composer.json";
$json = json_decode( file_get_contents( $json ) );
$vendor_dir = $json->config->{'vendor-dir'} ?? 'vendor';
return $vendor_dir;
}
/**
* get_constants
*
* @return void
*/
public function get_constants(){
$CONSTANTS = $this->get_defaults();
$CONSTANTS += $this->get_official_defaults( $this->DEFAULTS );
return $CONSTANTS;
}
/**
* get_defaults
*
* returns an array of defaults to be set
*
* @return array
*/
public function get_defaults(){
extract( $this->getenv_defaults() );
//? IN PRODUCTION ENV?
$isProduction = $this->is_production( $WP_DEBUG );
return [
'IS_PRODUCTION' => $isProduction,
'DISALLOW_FILE_EDIT' => $isProduction,
'WP_DEBUG' => !$isProduction,
'WP_DEBUG_LOG' => !$isProduction,
'WP_DEBUG_DISPLAY' => !$isProduction,
'WP_HOME' => "{$SITE_SCHEME}://{$SITE_HOST}",
'WP_SITEURL' => "{$SITE_SCHEME}://{$SITE_HOST}/{$WP_DIR}",
'WP_CONTENT_URL' => "{$SITE_SCHEME}://{$SITE_HOST}/{$WP_CONTENT}",
'WP_CONTENT_DIR' => "{$this->DIR}/{$DOCROOT}/{$WP_CONTENT}",
'ABSPATH' => "{$this->DIR}/{$DOCROOT}/{$WP_DIR}",
'DOCROOT' => "{$this->DIR}/{$DOCROOT}",
'VENDOR_PATH' => "{$this->DIR}/{$this->VENDOR_DIR}",
'PROJECT_ROOT' => "{$this->DIR}"
];
}
/**
* getenv_defaults
* these values can be overridden using $ENV vars
*
* @return array
*/
private function getenv_defaults(){
extract( $this->DEFAULTS );
$prefix = getenv('MYSQL_PREFIX') ?: $DB_PREFIX;
if($prefix)
$GLOBALS['table_prefix'] = $prefix;
return [
// ENV VARS OVERRIDE DEFAULTS
'DOCROOT' => getenv('DOCROOT') ?: $DOCROOT,
'SITE_HOST' => getenv('VIRTUAL_HOST') ?: $_SERVER['SERVER_NAME'],
'SITE_SCHEME' => getenv('SITE_SCHEME') ?: $SITE_SCHEME,
'WP_CONTENT' => getenv('WP_CONTENT') ?: $WP_CONTENT,
'WP_DEBUG' => getenv('WP_DEBUG') ?: $WP_DEBUG,
'WP_DIR' => getenv('WP_DIR') ?: $WP_DIR,
];
}
/**
* get_official_defaults
* removes custom defaults to make things official
*
* @param mixed $defaults
* @return array
*/
private function get_official_defaults( $defaults ){
foreach( $this->CUSTOM as $unofficial )
unset( $defaults[ $unofficial ] );
return $defaults;
}
/**
* autoload
* defines constants and setup psr-4 class auto-loading if using composer
*
* @return void
*/
public function autoload(){
$this->define_constants();
$this->require( "{$this->DIR}/{$this->VENDOR_DIR}/autoload.php" );
}
/**
* define_constants
* loads local configs before looping through defaults to set $CONSTANTS
*
* @return void
*/
private function define_constants(){
$this->load_local_config();
// LOOP $CONSTANTS AND DEFINE DEFAULT I
foreach ( $this->CONSTANTS as $CONST => $X )
if ( !defined( $CONST ) )
define( $CONST, $X );
}
/**
* load_local_config
* loads local configuration file if found, otherwise use salts provided
*
* @return void
*/
private function load_local_config(){
$local = $this->DIR . "/wp-config-local.php";
$secrets = $this->DIR . "/wp-secrets.php";
$this->require( $local, $secrets );
}
/**
* is_production
* debug is automatically turned on for devs using docksal.
* add your own logic here to determine how you define is_production
*
* @param mixed $debug
* @return bool
*/
private function is_production( $isDebug ){
$isDocksal = getenv('DOCKSAL_STACK');
return !$isDocksal && !$isDebug;
}
/**
* require method that checks file before requiring,
* pass 2nd argument to include a separate file should the 1st file not exists
*
* @param mixed $file
* @param mixed $else
* @return void
*/
private function require( $file, $else = false ){
if ( file_exists( $file ) )
require_once $file;
else if ( $else && file_exists( $else ) )
require_once $else;
}
}