-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbootstrap.php
More file actions
319 lines (284 loc) · 10.7 KB
/
Copy pathbootstrap.php
File metadata and controls
319 lines (284 loc) · 10.7 KB
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
<?php
/*
* This file is part of the {APP-NAME}.
*
* (c) {APP-AUTHOR}
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Class : Bootstrap
*
* @author {APP-AUTHOR}
* @package {APP-NAME}
* @since {APP-VERSION}
*/
class __{APP-CLASS}_Bootstrap
{
// Application default settings
protected $settings = array(
'name' => 'Project', // Application name
'slug' => 'project', // Application slug
'mode' => 'theme', // Application mode
'rpv' => '8.1', // Required PHP version
'rwv' => '6.5', // Required WordPress version
'dev' => false,
'class' => 'Project\Application',
'class-alias' => 'zc',
'file-load-path' => false,
'session' => false,
// Debugger
'debug' => true,
'email' => null,
'log' => true,
'log-directory' => false,
'log-severity' => 0,
'log-disable-e-deprecated' => false,
'log-max-files' => 10,
'strict-mode' => false,
'show-bar' => true,
'max-depth' => 4,
'max-length' => 150,
'editor' => false,
// Callbacks
'checker-cb' => false,
);
protected $tasks = array();
protected $message = '';
public $app;
/**
* Constructor
*
* @param array $config Bootstrap config
* @throws ZimbruCodeBootstrapException
* @since 1.0.0
*/
public function __construct($settings = array())
{
if (empty($this->settings) || !is_array($this->settings)) {
throw new ZimbruCodeBootstrapException('Bootstrap : Default configs is not defined.');
}
$this->settings = wp_parse_args($settings, $this->settings);
$this->condition();
add_action('after_switch_theme', array($this, '__action_after_switch_theme'));
if (isset($this->settings['dev']) && $this->settings['dev'] === true) {
try
{
$this->prepConditions();
} catch (ZimbruCodeBootstrapException $e) {
if (!isset($_GET['activated'])) {
wp_die(sprintf('<h2>%s</h2><p>%s</p>', esc_html__('Error', 'zc'), $e->getMessage()));
}
}
} else {
if (!isset($_GET['activated'])) {
$this->prepConditions();
}
}
$this->setup();
}
/**
* Prepare conditions
*
* @throws ZimbruCodeBootstrapException
* @return void
* @since 1.0.0
*/
protected function prepConditions()
{
foreach ($this->tasks as $task) {
if ($task['condition']) {
throw new ZimbruCodeBootstrapException($task['message']);
}
}
}
/**
* Loading conditions
*
* @return void
* @since 1.0.0
*/
protected function condition()
{
// Check PHP Version
$this->check(
!is_php_version_compatible($this->settings['rpv']),
sprintf(
esc_html__('Requires at least PHP version "%s" or greater. You are running version "%s". Ask your host to update to a newer PHP version for FREE. For more information you can read on %s and %s', 'zc'),
$this->settings['rpv'],
PHP_VERSION,
'<a href="https://php.net/supported-versions.php" target="_blank" rel="nofollow noopener noreferrer">'. esc_html__('PHP : Supported Versions', 'zc') .'</a>',
'<a href="https://wordpress.org/about/requirements" target="_blank" rel="nofollow noopener noreferrer">'. esc_html__('WordPress : Requirements', 'zc') .'</a>'
)
);
// Check WordPress Version
$this->check(
!is_wp_version_compatible($this->settings['rwv']),
sprintf(
esc_html__('Requires at least WordPress version "%s". You are running version "%s". Please upgrade and try again.', 'zc'),
$this->settings['rwv'],
$GLOBALS['wp_version']
)
);
// Additional checkers
if (!empty($this->settings['checker-cb']) && is_callable($this->settings['checker-cb'])) {
call_user_func($this->settings['checker-cb'], $this);
}
}
/**
* Setup application
*
* @return void
* @since 1.0.0
*/
protected function setup()
{
// Include auto loader
$composer = require get_template_directory() . '/vendor/autoload.php';
// Debug
if ($this->settings['debug'] === true) {
$debugger = array(
'strictMode' => $this->settings['strict-mode'],
'showBar' => $this->settings['show-bar'],
'logDirectory' => $this->settings['log-directory'],
'logSeverity' => $this->settings['log-severity'],
'logDisableEDeprecated' => $this->settings['log-disable-e-deprecated'],
'email' => $this->settings['email'],
'maxDepth' => $this->settings['max-depth'],
'maxLength' => $this->settings['max-length'],
'dev' => $this->settings['dev'],
'editor' => $this->settings['editor'],
);
// Log
if ($this->settings['log']) {
if (defined('ZC_LOG_DIR_WP_UPLOAD_STATUS') && ZC_LOG_DIR_WP_UPLOAD_STATUS === true) {
$uploadDir = wp_get_upload_dir();
if (isset($uploadDir['error']) && $uploadDir['error'] === false && $uploadDir['basedir']) {
$debugger['logDirectory'] = wp_normalize_path("{$uploadDir['basedir']}/{$this->settings['slug']}/logs");
} else {
$debugger['logDirectory'] = wp_normalize_path(__DIR__ . '/app/Resources/var/logs');
}
} else if (!$this->settings['log-directory']) {
$debugger['logDirectory'] = wp_normalize_path(__DIR__ . '/app/Resources/var/logs');
}
if (wp_mkdir_p($debugger['logDirectory'])) {
if (!file_exists("{$debugger['logDirectory']}/.htaccess")) {
@file_put_contents("{$debugger['logDirectory']}/.htaccess", "Deny From All\n<FilesMatch \"\.(?:html)$\">\n\tAllow From All\n</FilesMatch>");
}
// Check max number of log files
$this->checkLogFilesMaxNumber($debugger['logDirectory']);
}
}
call_user_func('ZimbruCode\Component\Debug\DebugController::runTracy', $debugger);
}
// Check file load path
if (empty($this->settings['file-load-path'])) {
$dbt = debug_backtrace();
$rootPath = isset($dbt[1]['file']) ? $dbt[1]['file'] : false;
}
// Build application
$app = $this->settings['class'];
$this->app = new $app($this->settings['slug'], $this->settings['mode'], $this->settings['dev'], $rootPath, $this->settings['session'], $composer);
class_alias($app, $this->settings['class-alias']);
}
/**
* Add condition
*
* @param mixed $condition Condition : true/false
* @param mixed $message Message of exception
* @return void
* @since 1.0.0
*/
public function check($condition = true, $message = false)
{
if ($condition && $message) {
$this->tasks[] = array(
'condition' => $condition,
'message' => $message,
);
}
}
/**
* Delete all log files if greater than "N"
*
* @param mixed $dir Log directory path
* @return void
* @since 1.0.0
*/
protected function checkLogFilesMaxNumber($dir)
{
if ($dir && is_string($dir)) {
$i = 0;
$max = (!empty($this->settings['log-max-files']) && is_int($this->settings['log-max-files'])) ? $this->settings['log-max-files'] : 10;
if ($handle = @opendir($dir)) {
while (($file = @readdir($handle)) !== false){
if (!in_array($file, array('.', '..')) && !is_dir("{$dir}/{$file}")) {
$info = new SplFileInfo("{$dir}/{$file}");
if ($info->getExtension() == 'html') {
$i++;
}
}
}
}
if ($i > $max) {
if ($handle = @opendir($dir)) {
while (($file = @readdir($handle)) !== false){
if (!in_array($file, array('.', '..')) && !is_dir("{$dir}/{$file}")) {
$info = new SplFileInfo("{$dir}/{$file}");
if ($info->getExtension() == 'html') {
wp_delete_file("{$dir}/{$file}");
}
}
}
}
}
}
}
/**
* Action : Processes after switch theme
*
* @return void
* @since 1.0.0
*/
public function __action_after_switch_theme()
{
try
{
$this->prepConditions();
} catch (ZimbruCodeBootstrapException $e) {
$this->message = $e->getMessage();
if (defined('WP_DEFAULT_THEME')) {
switch_theme(WP_DEFAULT_THEME, WP_DEFAULT_THEME);
}
if (isset($_GET['activated'])) {
unset($_GET['activated']);
}
// Add message about failed theme switch
add_action('admin_notices', array($this, '__action_admin_notices'));
}
}
/**
* Action : Add message about failed theme switch
*
* @return void
* @since 1.0.0
*/
public function __action_admin_notices()
{
printf('<div class="error"><p>%s</p></div>', $this->settings['name'] . ' ' . esc_html__('Error', 'zc') . ' : ' . $this->message);
}
}
/**
* Class : Bootstrap exception
*
* @author Junjulini
* @package ZimbruCode
* @since ZimbruCode 1.0.0
*/
if (!class_exists('ZimbruCodeBootstrapException')) {
class ZimbruCodeBootstrapException extends Exception
{
// Exception
}
}