forked from newclarity/ExoWP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexowp.php
More file actions
535 lines (471 loc) · 14.6 KB
/
exowp.php
File metadata and controls
535 lines (471 loc) · 14.6 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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
<?php
define( 'EXO_VERSION', '0.1.12' );
/**
* All Exo implementations should load exo-core.php first.
*/
require( __DIR__ . '/core/exo-core.php');
/**
* Class Exo
*/
class Exo extends Exo_Library_Base {
const SHORT_PREFIX = 'exo_';
/**x
* @var bool Flag variable to track if the 'wp_loaded' hook has fired yet or not.
*/
private static $_is_wp_loaded = false;
/**x
* @var bool Flag variable to track if the 'exo_init' hook has fired yet or not.
*/
private static $_is_exo_init = false;
/**
* @var string Directory for the theme dir for this site. If it has a parent theme, it returns the child theme's dir.
*/
private static $_theme_dir;
/**
* @var string URL for the theme dir for this site. If it has a parent theme, it returns the child theme's dir.
*/
private static $_theme_uri;
/**
* @var string Target environment, must be one of 'dev', 'test', 'stage' or 'live.'
* Defaults to 'live' because that's the safest default.
*/
private static $_runmode = 'live';
/**
* @var string
*/
private static $_included_template;
/**
* @var bool
*/
private static $_bootstrap_loaded = false;
/**
* @var Exo_View_Base
*/
private static $_queried_view;
/**
* Return the files required to be require()d by Exo itself.
*
* Exo looks for a uppercase method of this name to get a list of files.
*
* @return array
*/
function REQUIRED_FILES() {
return array(
'core/class-autoloader.php',
'base/class-mixin-base.php',
'base/class-model-base.php',
'base/class-collection-base.php',
'base/class-view-base.php',
'base/class-post-base.php',
);
}
/**
* Return the local paths that autoloadable files can be found for Exo itself.
*
* Exo looks for a uppercase method of this name to get a list of paths.
*
* @return array
*/
function AUTOLOAD_PATHS() {
return array(
'base',
'helpers',
'models/post-types',
'models/taxonomies',
'collections',
'views',
);
}
/**
* Boostrap Exo
*/
static function on_load() {
if ( defined( 'EXO_RUNMODE' ) ) {
/*
* This is a fallback so it can be set when using require( 'wp-load.php' );
*/
self::set_runmode( EXO_RUNMODE );
}
/*
* Grabs the theme directory for convenience (and so the name isn't confusing!)
*/
self::$_theme_dir = get_stylesheet_directory();
/**
* Ensure we are using the right scheme for the incoming URL (http vs. https)
*/
self::$_theme_uri = _Exo_Helpers::maybe_adjust_http_scheme( get_stylesheet_directory_uri() );
/*
* Records the fact that the 'wp_loaded' hook as indeed been reached.
*/
add_action( 'wp_loaded', array( __CLASS__, '_wp_loaded_0' ), 0 );
/*
* Call as late as possible so that no other hooks modify after since I goal is to just capture the value.
*/
add_action( 'template_include', array( __CLASS__, '_template_include_9999999' ), 9999999 );
/**
* Initializes and 'fixes up' all things registered in plugins and themes.
*/
self::add_static_action( 'after_setup_theme', 11 );
}
/**
* Initialize the Main and Implementation classes.
*
* To be called after all the other code is called.
*/
static function _after_setup_theme_11() {
if ( ! Exo::is_dev_mode() ) {
/**
* @todo Test to see if theme is same as when $bootstrap_php generated otherwise regenerate.
*/
if ( is_file( $bootstrap_php = Exo::bootstrap_filepath() ) ) {
require( $bootstrap_php );
}
if ( ! self::$_bootstrap_loaded ) {
_Exo_Helpers::trigger_warning( __( 'The Exo bootstrap file not found in theme; REGENERATING. RELOAD PAGE.', 'exo' ) );
} else {
/**
* @var Exo_Implementation $implementation
*/
foreach( self::_get_implementations() as $implementation ) {
/*
* Scan $implementation->_required_files and load the file for each associated class.
*/
$implementation->_load_required_files();
}
Exo::_add_hooks();
}
}
if ( ! self::$_bootstrap_loaded ) {
/*
* Convert class_names in self::$_implementations to actual Exo_Implementations.
*/
Exo_Main_Base::_record_implementations();
/**
* Sort component type by load precendence
*/
self::_sort_implementations();
/**
* @var Exo_Implementation $implementation
*/
foreach( self::_get_implementations() as $implementation ) {
/*
* Scan $implementation->_required_files and load the file for each associated class.
*/
$implementation->_load_required_files();
/*
* Scan the autoload directories and for each file record an associated class.
*/
$implementation->_record_autoload_class_filepaths();
/**
* Collect up registered helpers for this implementation
*/
$implementation->_register_helpers( $implementation->apply_instance_filters( 'exo_register_helpers', array() ) );
/**
* Generate list of $this->_helper_callables[$method_name] from shorter list of this->_helpers[$class_name].
*/
$implementation->_fixup_registered_helpers();
/*
* Autoload all class files found in the autoload directories.
*/
$implementation->autoload_all();
}
Exo::_record_hooks();
Exo::_add_hooks();
Exo::_record_post_types();
Exo::_fixup_post_types();
Exo::_record_mixins();
Exo::_fixup_mixins();
Exo::_record_collection_model_classes();
Exo::_generate_bootstrap_file();
}
foreach( self::_get_implementations() as $implementation ) {
/*
* Allow something else to do something at this point.
*/
$implementation->do_instance_action( 'exo_implementation_init' );
}
self::$_is_exo_init = true;
}
/**
* @return string
*/
static function bootstrap_filepath() {
return Exo::theme_dir( 'exo-bootstrap.php' );
}
/**
* @param array $bootstrap_data
* @return bool
*/
static function _load_bootstrap_data( $bootstrap_data ) {
$loaded = true;
/**
* @var Exo_Implementation $implementation
*/
$bootstrap_data = unserialize( $bootstrap_data );
if ( $loaded && $loaded = ( isset( $bootstrap_data->implementations ) && is_array( $bootstrap_data->implementations ) ) ) {
Exo_Main_Base::_set_implementations( $bootstrap_data->implementations );
}
if ( $loaded && $loaded = isset( $bootstrap_data->registered_hooks ) ) {
_Exo_Hook_Helpers::_get_hooks( $bootstrap_data->registered_hooks);
}
foreach( self::_get_implementations() as $implementation ) {
if ( $loaded && $loaded = isset( $bootstrap_data->implementations[$implementation->main_class] ) ) {
$loaded = $implementation->_set_bootstrap_data( $bootstrap_data->implementations[$implementation->main_class] );
}
}
if ( $loaded && $loaded = isset( $bootstrap_data->mixin_callables ) ) {
Exo_Instance_Base::_set_callable_templates( $bootstrap_data->mixin_callables );
}
if ( $loaded && $loaded = isset( $bootstrap_data->exo_post_types ) ) {
_Exo_Post_Helpers::_set_exo_post_types( $bootstrap_data->exo_post_types );
}
return self::$_bootstrap_loaded = $loaded;
}
/**
* @return object|void
*/
static function _generate_bootstrap_file() {
$bootstrap_data = (object)array(
'registered_hooks' => _Exo_Hook_Helpers::_get_hooks(),
'mixin_callables' => Exo_Instance_Base::_get_callable_templates(),
'exo_post_types' => _Exo_Post_Helpers::_get_exo_post_types(),
'implementations' => array(),
);
/**
* @var Exo_Implementation $implementation
*/
foreach( self::_get_implementations() as $implementation ) {
$bootstrap_data->implementations[$implementation->main_class] = $implementation->_get_bootstrap_data();
}
$bootstrap_data = serialize( $bootstrap_data );
$bootstrap_php = <<<PHP
<?php
if ( ! defined( 'WP_CONTENT_DIR' ) ) {
header( 'HTTP/1.0 404 Not Found' );
echo '404 File Not Found.';
exit;
}
\$_exo_bootstrap_data =<<<BOOTSTRAP_DATA
{$bootstrap_data}
BOOTSTRAP_DATA;
Exo::_load_bootstrap_data( trim( \$_exo_bootstrap_data ) );
unset( \$_exo_bootstrap_data );
PHP;
if ( $bootstrap_php != Exo::get_file_contents( $filepath = Exo::bootstrap_filepath() ) ) {
Exo::put_file_contents( $filepath, $bootstrap_php );
}
}
/**
*
*/
static function is_exo_init() {
return self::$_is_exo_init;
}
/**
* Walks the list of $classes from get_declared_classes() and first 'exo_scan_class' hook.
*
* @param callable $callback
* @note All classes must be loaded to call this.
*/
static function walk_declared_classes( $callback ) {
foreach( get_declared_classes() as $class_name ) {
call_user_func( $callback, $class_name );
}
}
/**
*
*/
static function _wp_loaded_0() {
self::$_is_wp_loaded = true;
}
/**
*
*/
static function is_wp_loaded() {
return self::$_is_wp_loaded;
}
/**
* Returns the directory for the theme dir for this site. If it has a parent theme, it returns the child theme's dir.
*
* @note Does not contain a trailing slash if no $path is passed.
*
* @param bool|string $path
*
* @return string
*/
static function theme_dir( $path = false ) {
return $path ? self::$_theme_dir . '/' . ltrim( $path, '/' ) : self::$_theme_dir;
}
/**
* Returns the URI/URL for the theme dir for this site. If it has a parent theme, it returns the child theme's dir.
*
* @note Does not contain a trailing slash if no $path is passed.
*
* @param bool|string $path
*
* @return string
*/
static function theme_uri( $path = false ) {
return $path ? self::$_theme_uri . '/' . ltrim( $path, '/' ) : self::$_theme_uri;
}
/**
* Returns the URI/URL for the theme dir for this site. If it has a parent theme, it returns the child theme's dir.
*
* @note Does not contain a trailing slash if no $path is passed.
*
* @param bool|string $path
*
* @return string
*/
static function theme_url( $path = false ) {
return self::theme_uri( $path );
}
/**
* Echos the URI/URL for the theme dir for this site. If it has a parent theme, it returns the child theme's dir.
*
* @note Does not contain a trailing slash if no $path is passed.
*
* @param bool|string $path
*/
static function the_theme_uri( $path = false ) {
echo self::theme_uri( $path );
}
/**
* Echos the URI/URL for the theme dir for this site. If it has a parent theme, it returns the child theme's dir.
*
* @note Does not contain a trailing slash if no $path is passed.
*
* @param bool|string $path
*/
static function the_theme_url( $path = false ) {
echo self::theme_uri( $path );
}
/**
* Returns true if a Development Deployment.
*
* @return string
*/
static function is_dev_mode() {
return 'dev' == self::$_runmode;
}
/**
* Returns true if a Testing Deployment.
*
* @return string
*/
static function is_test_mode() {
return 'test' == self::$_runmode;
}
/**
* Returns true if a Staging Deployment.
*
* @return string
*/
static function is_stage_mode() {
return 'stage' == self::$_runmode;
}
/**
* Returns true if a Live Deployment, i.e. Production.
*
* @return string
*/
static function is_live_mode() {
return 'live' == self::$_runmode;
}
/**
* Returns the Run Mode, one of: 'dev', 'test', 'stage' or 'live.'
*
* @return string
*/
static function runmode() {
return self::$_runmode;
}
/**
* @param $runmode
*
* @throws Exception
*/
static function set_runmode( $runmode ) {
if ( ! WP_DEBUG ) {
self::$_runmode = strtolower( $runmode );
} else {
switch ( $runmode ) {
case 'dev':
case 'test':
case 'stage':
case 'live':
self::$_runmode = strtolower( $runmode );
break;
default:
$message = __( 'ERROR: Neither Exo nor any of it\'s helper classes have the method %s().', 'exo' );
Exo::trigger_warning( $message, $method_name );
break;
}
}
}
/**
* Capture filepath of the theme template file that was loaded by WordPress' template-loader.php into a static var.
*
* @return bool
*/
static function _template_include_9999999() {
self::$_included_template = func_get_arg( 0 );
if ( ! defined( 'EXO_DISABLE_TEMPLATE_GLOBALS' ) || ! EXO_DISABLE_TEMPLATE_GLOBALS ) {
extract( $GLOBALS );
}
if ( $view = self::get_queried_view() ) {
require( self::$_included_template );
}
return self::implementation()->dir( 'templates/empty.php' );
}
/**
* @return Exo_Post_Base
*/
static function get_queried_view() {
if ( ! isset( self::$_queried_view ) ) {
global $wp_the_query;
$queried_object = get_queried_object();
if ( $wp_the_query->is_home ) {
if ( get_option( 'page_on_front' ) ) {
$view = Exo::get_post_view( $wp_the_query->post );
} else {
$view = Exo::get_post_collection_view( $wp_the_query->posts );
}
} elseif ( $wp_the_query->is_category || $wp_the_query->is_tag || $wp_the_query->is_tax ) {
$view = Exo::get_taxonomy_term_view( $queried_object );
} elseif ( $wp_the_query->is_post_type_archive ) {
$view = Exo::get_post_collection_view( $wp_the_query->posts );
} elseif ( $wp_the_query->is_posts_page ) {
$view = Exo::get_page_view( $queried_object );
} elseif ( $wp_the_query->is_singular && ! is_null( $wp_the_query->post ) ) {
$view = Exo::get_post_view( $queried_object );
} elseif ( $wp_the_query->is_author ) {
$view = Exo::get_author_view( $queried_object );
} else {
$view = false;
}
self::$_queried_view = apply_filters( 'exo_queried_view', $view );
}
return self::$_queried_view;
}
/**
* Returns filepath of the theme template file that was loaded by WordPress' template-loader.php
*
* @return string
*/
static function included_template() {
return self::$_included_template;
}
}
/**
* Initialize Exo using the 'plugins_loaded' priority 5, or load directly assumed require()d in function.php file.
*/
if ( defined( 'EXO_LOAD_IN_THEME' ) && EXO_LOAD_IN_THEME ) {
if ( defined( 'EXO_THEME_LOAD_CALLABLE' ) ) {
call_user_func( EXO_THEME_LOAD_CALLABLE );
} else {
Exo::on_load();
}
} else {
add_action( 'plugins_loaded', array( 'Exo', 'on_load' ), 5 );
}