1
1
<?php
2
2
/**
3
3
* Class Cherry Core
4
- * Version: 1.4.3.1
4
+ * Version: 1.5.0
5
5
*
6
6
* @package Cherry_Framework
7
7
* @subpackage Class
@@ -59,25 +59,29 @@ class Cherry_Core {
59
59
* Constructor.
60
60
*
61
61
* @since 1.0.0
62
- * @since 1.1.1 Using dirname( __FILE__ ) instead of __DIR__.
63
62
*/
64
63
public function __construct ( $ settings = array () ) {
65
- $ base_dir = trailingslashit ( dirname ( __FILE__ ) );
66
- $ base_url = trailingslashit ( $ this ->base_url ( '' , __FILE__ ) );
64
+ global $ chery_core_version ;
67
65
68
66
$ defaults = array (
69
67
'framework_path ' => 'cherry-framework ' ,
70
68
'modules ' => array (),
71
- 'base_dir ' => $ base_dir ,
72
- 'base_url ' => $ base_url ,
73
- 'extra_base_dir ' => '' ,
69
+ 'base_dir ' => '' ,
70
+ 'base_url ' => '' ,
74
71
);
75
72
76
73
$ this ->settings = array_merge ( $ defaults , $ settings );
77
74
78
- $ this ->settings ['extra_base_dir ' ] = trailingslashit ( $ this ->settings ['base_dir ' ] );
79
- $ this ->settings ['base_dir ' ] = $ base_dir ;
80
- $ this ->settings ['base_url ' ] = $ base_url ;
75
+ if ( isset ( $ chery_core_version ) && 0 < sizeof ( $ chery_core_version ) ) {
76
+ $ core_paths = array_values ( $ chery_core_version );
77
+ $ path_parts = pathinfo ( $ core_paths [0 ] );
78
+ $ this ->settings ['base_dir ' ] = trailingslashit ( $ path_parts ['dirname ' ] );
79
+ } else {
80
+ // This condition and the using of the function dirname is due to core backwards compatibility with old framework versions
81
+ $ this ->settings ['base_dir ' ] = trailingslashit ( dirname ( __FILE__ ) );
82
+ }
83
+
84
+ $ this ->settings ['base_url ' ] = trailingslashit ( $ this ->base_url ( '' , $ this ->settings ['base_dir ' ] ) );
81
85
82
86
$ this ->run_collector ();
83
87
@@ -118,30 +122,10 @@ private function run_collector() {
118
122
}
119
123
120
124
foreach ( $ this ->settings ['modules ' ] as $ module => $ settings ) {
121
- $ priority = $ this ->get_module_priority ( $ module );
122
- $ path = $ this ->get_module_path ( $ module );
125
+ $ file_path = $ this ->get_module_file ( $ module );
123
126
124
127
if ( ! array_key_exists ( $ module , self ::$ all_modules ) ) {
125
- self ::$ all_modules [ $ module ] = array (
126
- $ priority => $ path ,
127
- );
128
-
129
- } else {
130
- $ old_priority = array_keys ( self ::$ all_modules [ $ module ] );
131
-
132
- if ( ! is_array ( $ old_priority ) || ! isset ( $ old_priority [0 ] ) ) {
133
- continue ;
134
- }
135
-
136
- $ compare = version_compare ( $ old_priority [0 ], $ priority , '< ' );
137
-
138
- if ( $ compare ) {
139
- continue ;
140
- }
141
-
142
- self ::$ all_modules [ $ module ] = array (
143
- $ priority => $ path ,
144
- );
128
+ self ::$ all_modules [ $ module ] = $ file_path ;
145
129
}
146
130
}
147
131
@@ -160,10 +144,8 @@ private function run_collector() {
160
144
* @since 1.1.0
161
145
*/
162
146
public static function load_all_modules () {
147
+ foreach ( self ::$ all_modules as $ module => $ path ) {
163
148
164
- foreach ( self ::$ all_modules as $ module => $ data ) {
165
-
166
- $ path = current ( $ data );
167
149
$ loaded = self ::load_module ( $ module , $ path );
168
150
169
151
if ( ! $ loaded ) {
@@ -178,7 +160,7 @@ public static function load_all_modules() {
178
160
* @since 1.4.0
179
161
*/
180
162
public function load_textdomain () {
181
- $ mo_file_path = dirname ( __FILE__ ) . '/ languages/ ' . get_locale () . '.mo ' ;
163
+ $ mo_file_path = $ this -> settings [ ' base_dir ' ] . 'languages/ ' . get_locale () . '.mo ' ;
182
164
183
165
load_textdomain ( 'cherry-framework ' , $ mo_file_path );
184
166
}
@@ -213,7 +195,6 @@ public function init_required_modules() {
213
195
* @since 1.1.0
214
196
*/
215
197
public function init_autoload_modules () {
216
-
217
198
if ( empty ( $ this ->modules ) ) {
218
199
return ;
219
200
}
@@ -229,6 +210,7 @@ public function init_autoload_modules() {
229
210
}
230
211
231
212
$ args = ! empty ( $ settings ['args ' ] ) ? $ settings ['args ' ] : array ();
213
+
232
214
$ this ->init_module ( $ module , $ args );
233
215
}
234
216
}
@@ -242,6 +224,12 @@ public function init_autoload_modules() {
242
224
* @return mixed
243
225
*/
244
226
public function init_module ( $ module , $ args = array () ) {
227
+
228
+ if ( empty ( $ args [ 'module_path ' ] ) ) {
229
+ $ get_module_path = $ this ->get_module_path ( $ module );
230
+ $ args ['module_path ' ] = ( $ get_module_path ) ? $ get_module_path : '' ;
231
+ }
232
+
245
233
$ this ->modules [ $ module ] = $ this ->get_module_instance ( $ module , $ args );
246
234
247
235
/**
@@ -279,14 +267,14 @@ public function is_module_autoload( $module ) {
279
267
public static function load_module ( $ module , $ path ) {
280
268
$ class_name = self ::get_class_name ( $ module );
281
269
282
- if ( class_exists ( $ class_name ) ) {
283
- return true ;
284
- }
285
-
286
270
if ( ! $ path ) {
287
271
return false ;
288
272
}
289
273
274
+ if ( class_exists ( $ class_name ) ) {
275
+ return true ;
276
+ }
277
+
290
278
require_once ( $ path );
291
279
292
280
return true ;
@@ -336,72 +324,31 @@ public static function get_class_name( $slug = '' ) {
336
324
*/
337
325
public function get_module_path ( $ module ) {
338
326
$ abs_path = false ;
339
- $ rel_path = 'modules/ ' . $ module . '/ ' . $ module . ' .php ' ;
327
+ $ rel_path = 'modules/ ' . $ module . '/ ' ;
340
328
341
- if ( file_exists ( $ this ->settings ['extra_base_dir ' ] . $ rel_path ) ) {
342
- $ abs_path = $ this ->settings ['extra_base_dir ' ] . $ rel_path ;
343
- } else if ( file_exists ( $ this ->settings ['base_dir ' ] . $ rel_path ) ) {
329
+ if ( file_exists ( $ this ->settings ['base_dir ' ] . $ rel_path ) ) {
344
330
$ abs_path = $ this ->settings ['base_dir ' ] . $ rel_path ;
345
331
}
346
332
347
333
return $ abs_path ;
348
334
}
349
335
350
336
/**
351
- * Get module priority from it's version.
352
- * Version information should be provided as a value stored in the header notation.
337
+ * Get path to main file for passed module.
353
338
*
354
- * @link https://developer.wordpress.org/reference/functions/get_file_data/
355
- * @since 1.0.0
356
- * @param string $module Module slug or path.
357
- * @param bool $is_path Set this as true, if `$module` contains a path.
358
- * @return int
339
+ * @since 1.0.1
340
+ * @param string $module Module slug.
341
+ * @return string
359
342
*/
360
- public function get_module_priority ( $ module , $ is_path = false ) {
361
-
362
- // Default phpDoc headers.
363
- $ default_headers = array (
364
- 'version ' => 'Version ' ,
365
- );
366
-
367
- // Maximum version number (major, minor, patch).
368
- $ max_version = array (
369
- 99 ,
370
- 99 ,
371
- 999 ,
372
- );
373
-
374
- // If `$module` is a slug, get module path.
375
- if ( ! $ is_path ) {
376
- $ module = $ this ->get_module_path ( $ module );
377
- }
378
-
379
- $ version = '1.0.0 ' ;
380
-
381
- /* @TODO: Add smart check */
382
- if ( ! $ module ) {
383
- return $ version ;
384
- }
385
-
386
- $ data = get_file_data ( $ module , $ default_headers );
387
-
388
- // Check if version string has a valid value.
389
- if ( isset ( $ data ['version ' ] ) && false !== strpos ( $ data ['version ' ], '. ' ) ) {
390
-
391
- // Clean the version string.
392
- preg_match ( '/[\d\.]+/ ' , $ data ['version ' ], $ version );
393
- $ version = $ version [0 ];
394
- }
395
-
396
- // Convert version into integer.
397
- $ parts = explode ( '. ' , $ version );
343
+ public function get_module_file ( $ module ) {
344
+ $ abs_path = false ;
345
+ $ rel_path = 'modules/ ' . $ module . '/ ' . $ module . '.php ' ;
398
346
399
- // Calculate priority.
400
- foreach ( $ parts as $ index => $ part ) {
401
- $ parts [ $ index ] = $ max_version [ $ index ] - (int ) $ part ;
347
+ if ( file_exists ( $ this ->settings ['base_dir ' ] . $ rel_path ) ) {
348
+ $ abs_path = $ this ->settings ['base_dir ' ] . $ rel_path ;
402
349
}
403
350
404
- return ( int ) join ( '' , $ parts ) ;
351
+ return $ abs_path ;
405
352
}
406
353
407
354
/**
@@ -416,7 +363,14 @@ public function get_module_priority( $module, $is_path = false ) {
416
363
*/
417
364
public static function base_url ( $ file_path = '' , $ module_path ) {
418
365
$ module_path = wp_normalize_path ( $ module_path );
419
- $ module_dir = dirname ( $ module_path );
366
+ preg_match ( '/\..*$/ ' , $ module_path , $ ext );
367
+
368
+ if ( empty ( $ ext ) ) {
369
+ $ module_dir = $ module_path ;
370
+ } else {
371
+ // This condition and the using of the function dirname is due to core backwards compatibility with old framework versions
372
+ $ module_dir = dirname ( $ module_path );
373
+ }
420
374
421
375
$ plugin_dir = wp_normalize_path ( WP_PLUGIN_DIR );
422
376
$ stylesheet = get_stylesheet ();
@@ -452,7 +406,7 @@ public static function base_url( $file_path = '', $module_path ) {
452
406
*/
453
407
public function pass_core_to_widgets ( $ core , $ path ) {
454
408
$ path = str_replace ( '\\' , '/ ' , $ path );
455
- $ current_core = str_replace ( '\\' , '/ ' , $ this ->settings ['extra_base_dir ' ] );
409
+ $ current_core = str_replace ( '\\' , '/ ' , $ this ->settings ['base_dir ' ] );
456
410
457
411
if ( false !== strpos ( $ path , $ current_core ) ) {
458
412
return self ::get_instance ();
@@ -461,11 +415,22 @@ public function pass_core_to_widgets( $core, $path ) {
461
415
return $ core ;
462
416
}
463
417
418
+ /**
419
+ * Get core version.
420
+ *
421
+ * @since 1.5.0
422
+ * @return string
423
+ */
424
+ public function get_core_version () {
425
+ global $ chery_core_version ;
426
+
427
+ return key ( $ chery_core_version );
428
+ }
429
+
464
430
/**
465
431
* Get path to the core directory.
466
432
*
467
433
* @since 1.0.0
468
- * @deprecated 1.1.0 Use constant `dirname( __FILE__ )`
469
434
* @return string
470
435
*/
471
436
public function get_core_dir () {
0 commit comments