1
|
|
|
<?php if ( ! defined('EVENT_ESPRESSO_VERSION')) { exit('No direct script access allowed'); } |
2
|
|
|
/** |
3
|
|
|
* EE_Registry Class |
4
|
|
|
* |
5
|
|
|
* Centralized Application Data Storage and Management |
6
|
|
|
* |
7
|
|
|
* @package Event Espresso |
8
|
|
|
* @subpackage core |
9
|
|
|
* @author Brent Christensen |
10
|
|
|
*/ |
11
|
|
|
class EE_Registry { |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* EE_Registry Object |
15
|
|
|
* |
16
|
|
|
* @var EE_Registry $_instance |
17
|
|
|
* @access private |
18
|
|
|
*/ |
19
|
|
|
private static $_instance = null; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var EE_Dependency_Map $_dependency_map |
23
|
|
|
* @access protected |
24
|
|
|
*/ |
25
|
|
|
protected $_dependency_map = null; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var array $_class_abbreviations |
29
|
|
|
* @access protected |
30
|
|
|
*/ |
31
|
|
|
protected $_class_abbreviations = array(); |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* EE_Cart Object |
35
|
|
|
* @access public |
36
|
|
|
* @var EE_Cart $CART |
37
|
|
|
*/ |
38
|
|
|
public $CART = null; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* EE_Config Object |
42
|
|
|
* @access public |
43
|
|
|
* @var EE_Config $CFG |
44
|
|
|
*/ |
45
|
|
|
public $CFG = null; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* EE_Network_Config Object |
49
|
|
|
* @access public |
50
|
|
|
* @var EE_Network_Config $NET_CFG |
51
|
|
|
*/ |
52
|
|
|
public $NET_CFG = null; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* StdClass object for storing library classes in |
56
|
|
|
* |
57
|
|
|
* @public LIB |
58
|
|
|
* @var StdClass $LIB |
59
|
|
|
*/ |
60
|
|
|
public $LIB = null; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* EE_Request_Handler Object |
64
|
|
|
* @access public |
65
|
|
|
* @var EE_Request_Handler $REQ |
66
|
|
|
*/ |
67
|
|
|
public $REQ = null; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* EE_Session Object |
71
|
|
|
* @access public |
72
|
|
|
* @var EE_Session $SSN |
73
|
|
|
*/ |
74
|
|
|
public $SSN = null; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* holds the ee capabilities object. |
78
|
|
|
* |
79
|
|
|
* @since 4.5.0 |
80
|
|
|
* |
81
|
|
|
* @var EE_Capabilities |
82
|
|
|
*/ |
83
|
|
|
public $CAP = null; |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* holds the EE_Message_Resource_Manager object. |
87
|
|
|
* |
88
|
|
|
* @since 4.9.0 |
89
|
|
|
* |
90
|
|
|
* @var EE_Message_Resource_Manager |
91
|
|
|
*/ |
92
|
|
|
public $MRM = null; |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* $addons - StdClass object for holding addons which have registered themselves to work with EE core |
96
|
|
|
* @access public |
97
|
|
|
* @var EE_Addon[] |
98
|
|
|
*/ |
99
|
|
|
public $addons = null; |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* $models |
103
|
|
|
* @access public |
104
|
|
|
* @var EEM_Base[] $models keys are 'short names' (eg Event), values are class names (eg 'EEM_Event') |
105
|
|
|
*/ |
106
|
|
|
public $models = array(); |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* $modules |
110
|
|
|
* @access public |
111
|
|
|
* @var EED_Module[] $modules |
112
|
|
|
*/ |
113
|
|
|
public $modules = null; |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* $shortcodes |
117
|
|
|
* @access public |
118
|
|
|
* @var EES_Shortcode[] $shortcodes |
119
|
|
|
*/ |
120
|
|
|
public $shortcodes = null; |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* $widgets |
124
|
|
|
* @access public |
125
|
|
|
* @var WP_Widget[] $widgets |
126
|
|
|
*/ |
127
|
|
|
public $widgets = null; |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* $non_abstract_db_models |
131
|
|
|
* @access public |
132
|
|
|
* @var array this is an array of all implemented model names (i.e. not the parent abstract models, or models |
133
|
|
|
* which don't actually fetch items from the DB in the normal way (ie, are not children of EEM_Base)). |
134
|
|
|
* Keys are model "short names" (eg "Event") as used in model relations, and values are |
135
|
|
|
* classnames (eg "EEM_Event") |
136
|
|
|
*/ |
137
|
|
|
public $non_abstract_db_models = array(); |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* $i18n_js_strings - internationalization for JS strings |
141
|
|
|
* usage: EE_Registry::i18n_js_strings['string_key'] = __( 'string to translate.', 'event_espresso' ); |
142
|
|
|
* in js file: var translatedString = eei18n.string_key; |
143
|
|
|
* |
144
|
|
|
* @access public |
145
|
|
|
* @var array |
146
|
|
|
*/ |
147
|
|
|
public static $i18n_js_strings = array(); |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* $main_file - path to espresso.php |
151
|
|
|
* |
152
|
|
|
* @access public |
153
|
|
|
* @var array |
154
|
|
|
*/ |
155
|
|
|
public $main_file; |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* array of ReflectionClass objects where the key is the class name |
159
|
|
|
* |
160
|
|
|
* @access public |
161
|
|
|
* @var ReflectionClass[] |
162
|
|
|
*/ |
163
|
|
|
public $_reflectors; |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* boolean flag to indicate whether or not to load/save dependencies from/to the cache |
167
|
|
|
* |
168
|
|
|
* @access protected |
169
|
|
|
* @var boolean $_cache_on |
170
|
|
|
*/ |
171
|
|
|
protected $_cache_on = true; |
172
|
|
|
|
173
|
|
|
|
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @singleton method used to instantiate class object |
177
|
|
|
* @access public |
178
|
|
|
* @param \EE_Dependency_Map $dependency_map |
179
|
|
|
* @return \EE_Registry instance |
180
|
|
|
*/ |
181
|
|
|
public static function instance( \EE_Dependency_Map $dependency_map = null ) { |
182
|
|
|
// check if class object is instantiated |
183
|
|
|
if ( ! self::$_instance instanceof EE_Registry ) { |
184
|
|
|
self::$_instance = new EE_Registry( $dependency_map ); |
|
|
|
|
185
|
|
|
} |
186
|
|
|
return self::$_instance; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
|
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
*protected constructor to prevent direct creation |
193
|
|
|
* |
194
|
|
|
* @Constructor |
195
|
|
|
* @access protected |
196
|
|
|
* @param \EE_Dependency_Map $dependency_map |
197
|
|
|
* @return \EE_Registry |
|
|
|
|
198
|
|
|
*/ |
199
|
|
|
protected function __construct( \EE_Dependency_Map $dependency_map ) { |
200
|
|
|
$this->_dependency_map = $dependency_map; |
201
|
|
|
add_action( 'EE_Load_Espresso_Core__handle_request__initialize_core_loading', array( $this, 'initialize' ) ); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
|
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* initialize |
208
|
|
|
*/ |
209
|
|
|
public function initialize() { |
210
|
|
|
$this->_class_abbreviations = apply_filters( |
211
|
|
|
'FHEE__EE_Registry____construct___class_abbreviations', |
212
|
|
|
array( |
213
|
|
|
'EE_Config' => 'CFG', |
214
|
|
|
'EE_Session' => 'SSN', |
215
|
|
|
'EE_Capabilities' => 'CAP', |
216
|
|
|
'EE_Cart' => 'CART', |
217
|
|
|
'EE_Network_Config' => 'NET_CFG', |
218
|
|
|
'EE_Request_Handler' => 'REQ', |
219
|
|
|
'EE_Message_Resource_Manager' => 'MRM', |
220
|
|
|
) |
221
|
|
|
); |
222
|
|
|
// class library |
223
|
|
|
$this->LIB = new StdClass(); |
224
|
|
|
$this->addons = new StdClass(); |
|
|
|
|
225
|
|
|
$this->modules = new StdClass(); |
|
|
|
|
226
|
|
|
$this->shortcodes = new StdClass(); |
|
|
|
|
227
|
|
|
$this->widgets = new StdClass(); |
|
|
|
|
228
|
|
|
$this->load_core( 'Base', array(), true ); |
229
|
|
|
// add our request and response objects to the cache |
230
|
|
|
$request_loader = $this->_dependency_map->class_loader( 'EE_Request' ); |
231
|
|
|
$this->_set_cached_class( |
232
|
|
|
$request_loader(), |
233
|
|
|
'EE_Request' |
234
|
|
|
); |
235
|
|
|
$response_loader = $this->_dependency_map->class_loader( 'EE_Response' ); |
236
|
|
|
$this->_set_cached_class( |
237
|
|
|
$response_loader(), |
238
|
|
|
'EE_Response' |
239
|
|
|
); |
240
|
|
|
add_action( 'AHEE__EE_System__set_hooks_for_core', array( $this, 'init' ) ); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
|
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* init |
247
|
|
|
* |
248
|
|
|
* @access public |
249
|
|
|
* @return void |
250
|
|
|
*/ |
251
|
|
|
public function init() { |
252
|
|
|
// Get current page protocol |
253
|
|
|
$protocol = isset( $_SERVER[ 'HTTPS' ] ) ? 'https://' : 'http://'; |
254
|
|
|
// Output admin-ajax.php URL with same protocol as current page |
255
|
|
|
self::$i18n_js_strings[ 'ajax_url' ] = admin_url( 'admin-ajax.php', $protocol ); |
256
|
|
|
self::$i18n_js_strings[ 'wp_debug' ] = defined( 'WP_DEBUG' ) ? WP_DEBUG : false; |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
|
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* localize_i18n_js_strings |
263
|
|
|
* |
264
|
|
|
* @return string |
265
|
|
|
*/ |
266
|
|
|
public static function localize_i18n_js_strings() { |
267
|
|
|
$i18n_js_strings = (array)EE_Registry::$i18n_js_strings; |
268
|
|
|
foreach ( $i18n_js_strings as $key => $value ) { |
269
|
|
|
if ( is_scalar( $value ) ) { |
270
|
|
|
$i18n_js_strings[ $key ] = html_entity_decode( (string)$value, ENT_QUOTES, 'UTF-8' ); |
271
|
|
|
} |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
return "/* <![CDATA[ */ var eei18n = " . wp_json_encode( $i18n_js_strings ) . '; /* ]]> */'; |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
|
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* @param mixed string | EED_Module $module |
281
|
|
|
*/ |
282
|
|
|
public function add_module( $module ) { |
283
|
|
|
if ( $module instanceof EED_Module ) { |
284
|
|
|
$module_class = get_class( $module ); |
285
|
|
|
$this->modules->{$module_class} = $module; |
286
|
|
|
} else { |
287
|
|
|
if ( ! class_exists( 'EE_Module_Request_Router' ) ) { |
288
|
|
|
$this->load_core( 'Module_Request_Router' ); |
289
|
|
|
} |
290
|
|
|
$this->modules->{$module} = EE_Module_Request_Router::module_factory( $module ); |
291
|
|
|
} |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
|
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* @param string $module_name |
298
|
|
|
* @return mixed EED_Module | NULL |
299
|
|
|
*/ |
300
|
|
|
public function get_module( $module_name = '' ) { |
301
|
|
|
return isset( $this->modules->{$module_name} ) ? $this->modules->{$module_name} : null; |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
|
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* loads core classes - must be singletons |
308
|
|
|
* |
309
|
|
|
* @access public |
310
|
|
|
* @param string $class_name - simple class name ie: session |
311
|
|
|
* @param mixed $arguments |
312
|
|
|
* @param bool $load_only |
313
|
|
|
* @return mixed |
314
|
|
|
*/ |
315
|
|
|
public function load_core( $class_name, $arguments = array(), $load_only = false ) { |
316
|
|
|
$core_paths = apply_filters( |
317
|
|
|
'FHEE__EE_Registry__load_core__core_paths', |
318
|
|
|
array( |
319
|
|
|
EE_CORE, |
320
|
|
|
EE_ADMIN, |
321
|
|
|
EE_CPTS, |
322
|
|
|
EE_CORE . 'data_migration_scripts' . DS, |
323
|
|
|
EE_CORE . 'request_stack' . DS, |
324
|
|
|
EE_CORE . 'middleware' . DS, |
325
|
|
|
) |
326
|
|
|
); |
327
|
|
|
// retrieve instantiated class |
328
|
|
|
return $this->_load( $core_paths, 'EE_', $class_name, 'core', $arguments, false, true, $load_only ); |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
|
332
|
|
|
|
333
|
|
|
/** |
334
|
|
|
* loads service classes |
335
|
|
|
* |
336
|
|
|
* @access public |
337
|
|
|
* @param string $class_name - simple class name ie: session |
338
|
|
|
* @param mixed $arguments |
339
|
|
|
* @param bool $load_only |
340
|
|
|
* @return mixed |
341
|
|
|
*/ |
342
|
|
|
public function load_service( $class_name, $arguments = array(), $load_only = false ) { |
343
|
|
|
$service_paths = apply_filters( |
344
|
|
|
'FHEE__EE_Registry__load_service__service_paths', |
345
|
|
|
array( |
346
|
|
|
EE_CORE . 'services' . DS, |
347
|
|
|
) |
348
|
|
|
); |
349
|
|
|
// retrieve instantiated class |
350
|
|
|
return $this->_load( $service_paths, 'EE_', $class_name, 'class', $arguments, false, true, $load_only ); |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
|
354
|
|
|
|
355
|
|
|
/** |
356
|
|
|
* loads data_migration_scripts |
357
|
|
|
* |
358
|
|
|
* @access public |
359
|
|
|
* @param string $class_name - class name for the DMS ie: EE_DMS_Core_4_2_0 |
360
|
|
|
* @param mixed $arguments |
361
|
|
|
* @return EE_Data_Migration_Script_Base |
362
|
|
|
*/ |
363
|
|
|
public function load_dms( $class_name, $arguments = array() ) { |
364
|
|
|
// retrieve instantiated class |
365
|
|
|
return $this->_load( EE_Data_Migration_Manager::instance()->get_data_migration_script_folders(), 'EE_DMS_', $class_name, 'dms', $arguments, false, false, false ); |
|
|
|
|
366
|
|
|
} |
367
|
|
|
|
368
|
|
|
|
369
|
|
|
|
370
|
|
|
/** |
371
|
|
|
* loads object creating classes - must be singletons |
372
|
|
|
* |
373
|
|
|
* @param string $class_name - simple class name ie: attendee |
374
|
|
|
* @param mixed $arguments - an array of arguments to pass to the class |
375
|
|
|
* @param bool $from_db - some classes are instantiated from the db and thus call a different method to instantiate |
376
|
|
|
* @param bool $cache if you don't want the class to be stored in the internal cache (non-persistent) then set this to FALSE (ie. when instantiating model objects from client in a loop) |
377
|
|
|
* @param bool $load_only whether or not to just load the file and NOT instantiate, or load AND instantiate (default) |
378
|
|
|
* @return EE_Base_Class | bool |
379
|
|
|
*/ |
380
|
|
|
public function load_class( $class_name, $arguments = array(), $from_db = false, $cache = true, $load_only = false ) { |
381
|
|
|
$paths = apply_filters( 'FHEE__EE_Registry__load_class__paths', array( |
382
|
|
|
EE_CORE, |
383
|
|
|
EE_CLASSES, |
384
|
|
|
EE_BUSINESS |
385
|
|
|
) ); |
386
|
|
|
// retrieve instantiated class |
387
|
|
|
return $this->_load( $paths, 'EE_', $class_name, 'class', $arguments, $from_db, $cache, $load_only ); |
|
|
|
|
388
|
|
|
} |
389
|
|
|
|
390
|
|
|
|
391
|
|
|
|
392
|
|
|
/** |
393
|
|
|
* loads helper classes - must be singletons |
394
|
|
|
* |
395
|
|
|
* @param string $class_name - simple class name ie: price |
396
|
|
|
* @param mixed $arguments |
397
|
|
|
* @param bool $load_only |
398
|
|
|
* @return EEH_Base | bool |
399
|
|
|
*/ |
400
|
|
|
public function load_helper( $class_name, $arguments = array(), $load_only = true ) { |
401
|
|
|
// todo: add doing_it_wrong() in a few versions after all addons have had calls to this method removed |
402
|
|
|
$helper_paths = apply_filters( 'FHEE__EE_Registry__load_helper__helper_paths', array( EE_HELPERS ) ); |
403
|
|
|
// retrieve instantiated class |
404
|
|
|
return $this->_load( $helper_paths, 'EEH_', $class_name, 'helper', $arguments, false, true, $load_only ); |
|
|
|
|
405
|
|
|
} |
406
|
|
|
|
407
|
|
|
|
408
|
|
|
|
409
|
|
|
/** |
410
|
|
|
* loads core classes - must be singletons |
411
|
|
|
* |
412
|
|
|
* @access public |
413
|
|
|
* @param string $class_name - simple class name ie: session |
414
|
|
|
* @param mixed $arguments |
415
|
|
|
* @param bool $load_only |
416
|
|
|
* @param bool $cache whether to cache the object or not. |
417
|
|
|
* @return mixed |
418
|
|
|
*/ |
419
|
|
|
public function load_lib( $class_name, $arguments = array(), $load_only = false, $cache = true ) { |
420
|
|
|
$paths = array( |
421
|
|
|
EE_LIBRARIES, |
422
|
|
|
EE_LIBRARIES . 'messages' . DS, |
423
|
|
|
EE_LIBRARIES . 'shortcodes' . DS, |
424
|
|
|
EE_LIBRARIES . 'qtips' . DS, |
425
|
|
|
EE_LIBRARIES . 'payment_methods' . DS, |
426
|
|
|
); |
427
|
|
|
// retrieve instantiated class |
428
|
|
|
return $this->_load( $paths, 'EE_', $class_name, 'lib', $arguments, false, $cache, $load_only ); |
429
|
|
|
} |
430
|
|
|
|
431
|
|
|
|
432
|
|
|
|
433
|
|
|
/** |
434
|
|
|
* loads model classes - must be singletons |
435
|
|
|
* |
436
|
|
|
* @param string $class_name - simple class name ie: price |
437
|
|
|
* @param mixed $arguments |
438
|
|
|
* @param bool $load_only |
439
|
|
|
* @return EEM_Base | bool |
440
|
|
|
*/ |
441
|
|
|
public function load_model( $class_name, $arguments = array(), $load_only = false ) { |
442
|
|
|
$paths = apply_filters( 'FHEE__EE_Registry__load_model__paths', array( |
443
|
|
|
EE_MODELS, |
444
|
|
|
EE_CORE |
445
|
|
|
) ); |
446
|
|
|
// retrieve instantiated class |
447
|
|
|
return $this->_load( $paths, 'EEM_', $class_name, 'model', $arguments, false, true, $load_only ); |
|
|
|
|
448
|
|
|
} |
449
|
|
|
|
450
|
|
|
|
451
|
|
|
|
452
|
|
|
/** |
453
|
|
|
* loads model classes - must be singletons |
454
|
|
|
* |
455
|
|
|
* @param string $class_name - simple class name ie: price |
456
|
|
|
* @param mixed $arguments |
457
|
|
|
* @param bool $load_only |
458
|
|
|
* @return mixed | bool |
459
|
|
|
*/ |
460
|
|
|
public function load_model_class( $class_name, $arguments = array(), $load_only = true ) { |
461
|
|
|
$paths = array( |
462
|
|
|
EE_MODELS . 'fields' . DS, |
463
|
|
|
EE_MODELS . 'helpers' . DS, |
464
|
|
|
EE_MODELS . 'relations' . DS, |
465
|
|
|
EE_MODELS . 'strategies' . DS |
466
|
|
|
); |
467
|
|
|
// retrieve instantiated class |
468
|
|
|
return $this->_load( $paths, 'EE_', $class_name, '', $arguments, false, true, $load_only ); |
469
|
|
|
} |
470
|
|
|
|
471
|
|
|
|
472
|
|
|
|
473
|
|
|
/** |
474
|
|
|
* Determines if $model_name is the name of an actual EE model. |
475
|
|
|
* @param string $model_name like Event, Attendee, Question_Group_Question, etc. |
476
|
|
|
* @return boolean |
477
|
|
|
*/ |
478
|
|
|
public function is_model_name( $model_name ) { |
479
|
|
|
return isset( $this->models[ $model_name ] ) ? true : false; |
480
|
|
|
} |
481
|
|
|
|
482
|
|
|
|
483
|
|
|
|
484
|
|
|
/** |
485
|
|
|
* generic class loader |
486
|
|
|
* |
487
|
|
|
* @param string $path_to_file - directory path to file location, not including filename |
488
|
|
|
* @param string $file_name - file name ie: my_file.php, including extension |
489
|
|
|
* @param string $type - file type - core? class? helper? model? |
490
|
|
|
* @param mixed $arguments |
491
|
|
|
* @param bool $load_only |
492
|
|
|
* @return mixed |
493
|
|
|
*/ |
494
|
|
|
public function load_file( $path_to_file, $file_name, $type = '', $arguments = array(), $load_only = true ) { |
495
|
|
|
// retrieve instantiated class |
496
|
|
|
return $this->_load( $path_to_file, '', $file_name, $type, $arguments, false, true, $load_only ); |
|
|
|
|
497
|
|
|
} |
498
|
|
|
|
499
|
|
|
|
500
|
|
|
|
501
|
|
|
/** |
502
|
|
|
* load_addon |
503
|
|
|
* |
504
|
|
|
* @param string $path_to_file - directory path to file location, not including filename |
505
|
|
|
* @param string $class_name - full class name ie: My_Class |
506
|
|
|
* @param string $type - file type - core? class? helper? model? |
507
|
|
|
* @param mixed $arguments |
508
|
|
|
* @param bool $load_only |
509
|
|
|
* @return EE_Addon |
510
|
|
|
*/ |
511
|
|
|
public function load_addon( $path_to_file, $class_name, $type = 'class', $arguments = array(), $load_only = false ) { |
512
|
|
|
// retrieve instantiated class |
513
|
|
|
return $this->_load( $path_to_file, 'addon', $class_name, $type, $arguments, false, true, $load_only ); |
|
|
|
|
514
|
|
|
} |
515
|
|
|
|
516
|
|
|
|
517
|
|
|
|
518
|
|
|
/** |
519
|
|
|
* loads and tracks classes |
520
|
|
|
* |
521
|
|
|
* @param array $file_paths |
522
|
|
|
* @param string $class_prefix - EE or EEM or... ??? |
523
|
|
|
* @param bool|string $class_name - $class name |
524
|
|
|
* @param string $type - file type - core? class? helper? model? |
525
|
|
|
* @param mixed $arguments - an argument or array of arguments to pass to the class upon instantiation |
526
|
|
|
* @param bool $from_db - some classes are instantiated from the db and thus call a different method to instantiate |
527
|
|
|
* @param bool $cache |
528
|
|
|
* @param bool $load_only |
529
|
|
|
* @return null|object|bool null = failure to load or instantiate class object. |
530
|
|
|
* object = class loaded and instantiated successfully. |
531
|
|
|
* bool = fail or success when $load_only is true |
532
|
|
|
*/ |
533
|
|
|
protected function _load( |
534
|
|
|
$file_paths = array(), |
535
|
|
|
$class_prefix = 'EE_', |
536
|
|
|
$class_name = false, |
537
|
|
|
$type = 'class', |
538
|
|
|
$arguments = array(), |
539
|
|
|
$from_db = false, |
540
|
|
|
$cache = true, |
541
|
|
|
$load_only = false |
542
|
|
|
) { |
543
|
|
|
// strip php file extension |
544
|
|
|
$class_name = str_replace( '.php', '', trim( $class_name ) ); |
545
|
|
|
// does the class have a prefix ? |
546
|
|
|
if ( ! empty( $class_prefix ) && $class_prefix != 'addon' ) { |
547
|
|
|
// make sure $class_prefix is uppercase |
548
|
|
|
$class_prefix = strtoupper( trim( $class_prefix ) ); |
549
|
|
|
// add class prefix ONCE!!! |
550
|
|
|
$class_name = $class_prefix . str_replace( $class_prefix, '', $class_name ); |
551
|
|
|
} |
552
|
|
|
$class_exists = class_exists( $class_name ); |
553
|
|
|
// if we're only loading the class and it already exists, then let's just return true immediately |
554
|
|
|
if ( $load_only && $class_exists ) { |
555
|
|
|
return true; |
556
|
|
|
} |
557
|
|
|
// $this->_cache_on is toggled during the recursive loading that can occur with dependency injection |
558
|
|
|
// $cache is controlled by individual calls to separate Registry loader methods like load_class() |
559
|
|
|
// $load_only is also controlled by individual calls to separate Registry loader methods like load_file() |
560
|
|
|
if ( $this->_cache_on && $cache && ! $load_only ) { |
561
|
|
|
// return object if it's already cached |
562
|
|
|
$cached_class = $this->_get_cached_class( $class_name, $class_prefix ); |
563
|
|
|
if ( $cached_class !== null ) { |
564
|
|
|
return $cached_class; |
565
|
|
|
} |
566
|
|
|
} |
567
|
|
|
// if the class doesn't already exist.. then we need to try and find the file and load it |
568
|
|
|
if ( ! $class_exists ) { |
569
|
|
|
// get full path to file |
570
|
|
|
$path = $this->_resolve_path( $class_name, $type, $file_paths ); |
571
|
|
|
// load the file |
572
|
|
|
$loaded = $this->_require_file( $path, $class_name, $type, $file_paths ); |
|
|
|
|
573
|
|
|
// if loading failed, or we are only loading a file but NOT instantiating an object |
574
|
|
|
if ( ! $loaded || $load_only ) { |
575
|
|
|
// return boolean if only loading, or null if an object was expected |
576
|
|
|
return $load_only ? $loaded : null; |
577
|
|
|
} |
578
|
|
|
} |
579
|
|
|
// instantiate the requested object |
580
|
|
|
$class_obj = $this->_create_object( $class_name, $arguments, $type, $from_db ); |
|
|
|
|
581
|
|
|
if ( $this->_cache_on && $cache ) { |
582
|
|
|
// save it for later... kinda like gum { : $ |
583
|
|
|
$this->_set_cached_class( $class_obj, $class_name, $class_prefix, $from_db ); |
|
|
|
|
584
|
|
|
} |
585
|
|
|
$this->_cache_on = true; |
586
|
|
|
return $class_obj; |
587
|
|
|
} |
588
|
|
|
|
589
|
|
|
|
590
|
|
|
|
591
|
|
|
/** |
592
|
|
|
* _get_cached_class |
593
|
|
|
* |
594
|
|
|
* attempts to find a cached version of the requested class |
595
|
|
|
* by looking in the following places: |
596
|
|
|
* $this->{$class_abbreviation} ie: $this->CART |
597
|
|
|
* $this->{$class_name} ie: $this->Some_Class |
598
|
|
|
* $this->LIB->{$class_name} ie: $this->LIB->Some_Class |
599
|
|
|
* $this->addon->{$class_name} ie: $this->addon->Some_Addon_Class |
600
|
|
|
* |
601
|
|
|
* @access protected |
602
|
|
|
* @param string $class_name |
603
|
|
|
* @param string $class_prefix |
604
|
|
|
* @return null|object |
605
|
|
|
*/ |
606
|
|
|
protected function _get_cached_class( $class_name, $class_prefix = '' ) { |
607
|
|
|
if ( isset( $this->_class_abbreviations[ $class_name ] ) ) { |
608
|
|
|
$class_abbreviation = $this->_class_abbreviations[ $class_name ]; |
609
|
|
|
} else { |
610
|
|
|
// have to specify something, but not anything that will conflict |
611
|
|
|
$class_abbreviation = 'FANCY_BATMAN_PANTS'; |
612
|
|
|
} |
613
|
|
|
// check if class has already been loaded, and return it if it has been |
614
|
|
|
if ( isset( $this->{$class_abbreviation} ) && ! is_null( $this->{$class_abbreviation} ) ) { |
615
|
|
|
return $this->{$class_abbreviation}; |
616
|
|
|
} else if ( isset ( $this->{$class_name} ) ) { |
617
|
|
|
return $this->{$class_name}; |
618
|
|
|
} else if ( isset ( $this->LIB->{$class_name} ) ) { |
619
|
|
|
return $this->LIB->{$class_name}; |
620
|
|
|
} else if ( $class_prefix == 'addon' && isset ( $this->addons->{$class_name} ) ) { |
621
|
|
|
return $this->addons->{$class_name}; |
622
|
|
|
} |
623
|
|
|
return null; |
624
|
|
|
} |
625
|
|
|
|
626
|
|
|
|
627
|
|
|
|
628
|
|
|
/** |
629
|
|
|
* _resolve_path |
630
|
|
|
* |
631
|
|
|
* attempts to find a full valid filepath for the requested class. |
632
|
|
|
* loops thru each of the base paths in the $file_paths array and appends : "{classname} . {file type} . php" |
633
|
|
|
* then returns that path if the target file has been found and is readable |
634
|
|
|
* |
635
|
|
|
* @access protected |
636
|
|
|
* @param string $class_name |
637
|
|
|
* @param string $type |
638
|
|
|
* @param array $file_paths |
639
|
|
|
* @return string | bool |
640
|
|
|
*/ |
641
|
|
|
protected function _resolve_path( $class_name, $type = '', $file_paths = array() ) { |
642
|
|
|
// make sure $file_paths is an array |
643
|
|
|
$file_paths = is_array( $file_paths ) ? $file_paths : array( $file_paths ); |
644
|
|
|
// cycle thru paths |
645
|
|
|
foreach ( $file_paths as $key => $file_path ) { |
646
|
|
|
// convert all separators to proper DS, if no filepath, then use EE_CLASSES |
647
|
|
|
$file_path = $file_path ? str_replace( array( '/', '\\' ), DS, $file_path ) : EE_CLASSES; |
648
|
|
|
// prep file type |
649
|
|
|
$type = ! empty( $type ) ? trim( $type, '.' ) . '.' : ''; |
650
|
|
|
// build full file path |
651
|
|
|
$file_paths[ $key ] = rtrim( $file_path, DS ) . DS . $class_name . '.' . $type . 'php'; |
652
|
|
|
//does the file exist and can be read ? |
653
|
|
|
if ( is_readable( $file_paths[ $key ] ) ) { |
654
|
|
|
return $file_paths[ $key ]; |
655
|
|
|
} |
656
|
|
|
} |
657
|
|
|
return false; |
658
|
|
|
} |
659
|
|
|
|
660
|
|
|
|
661
|
|
|
|
662
|
|
|
/** |
663
|
|
|
* _require_file |
664
|
|
|
* |
665
|
|
|
* basically just performs a require_once() |
666
|
|
|
* but with some error handling |
667
|
|
|
* |
668
|
|
|
* @access protected |
669
|
|
|
* @param string $path |
670
|
|
|
* @param string $class_name |
671
|
|
|
* @param string $type |
672
|
|
|
* @param array $file_paths |
673
|
|
|
* @return boolean |
674
|
|
|
* @throws \EE_Error |
675
|
|
|
*/ |
676
|
|
|
protected function _require_file( $path, $class_name, $type = '', $file_paths = array() ) { |
677
|
|
|
// don't give up! you gotta... |
678
|
|
|
try { |
679
|
|
|
//does the file exist and can it be read ? |
680
|
|
View Code Duplication |
if ( ! $path ) { |
681
|
|
|
// so sorry, can't find the file |
682
|
|
|
throw new EE_Error ( |
683
|
|
|
sprintf( |
684
|
|
|
__( 'The %1$s file %2$s could not be located or is not readable due to file permissions. Please ensure that the following filepath(s) are correct: %3$s', 'event_espresso' ), |
685
|
|
|
trim( $type, '.' ), |
686
|
|
|
$class_name, |
687
|
|
|
'<br />' . implode( ',<br />', $file_paths ) |
688
|
|
|
) |
689
|
|
|
); |
690
|
|
|
} |
691
|
|
|
// get the file |
692
|
|
|
require_once( $path ); |
693
|
|
|
// if the class isn't already declared somewhere |
694
|
|
View Code Duplication |
if ( class_exists( $class_name, false ) === false ) { |
695
|
|
|
// so sorry, not a class |
696
|
|
|
throw new EE_Error( |
697
|
|
|
sprintf( |
698
|
|
|
__( 'The %s file %s does not appear to contain the %s Class.', 'event_espresso' ), |
699
|
|
|
$type, |
700
|
|
|
$path, |
701
|
|
|
$class_name |
702
|
|
|
) |
703
|
|
|
); |
704
|
|
|
} |
705
|
|
|
|
706
|
|
|
} catch ( EE_Error $e ) { |
707
|
|
|
$e->get_error(); |
708
|
|
|
return false; |
709
|
|
|
} |
710
|
|
|
return true; |
711
|
|
|
} |
712
|
|
|
|
713
|
|
|
|
714
|
|
|
|
715
|
|
|
/** |
716
|
|
|
* _create_object |
717
|
|
|
* Attempts to instantiate the requested class via any of the |
718
|
|
|
* commonly used instantiation methods employed throughout EE. |
719
|
|
|
* The priority for instantiation is as follows: |
720
|
|
|
* - abstract classes or any class flagged as "load only" (no instantiation occurs) |
721
|
|
|
* - model objects via their 'new_instance_from_db' method |
722
|
|
|
* - model objects via their 'new_instance' method |
723
|
|
|
* - "singleton" classes" via their 'instance' method |
724
|
|
|
* - standard instantiable classes via their __constructor |
725
|
|
|
* Prior to instantiation, if the classname exists in the dependency_map, |
726
|
|
|
* then the constructor for the requested class will be examined to determine |
727
|
|
|
* if any dependencies exist, and if they can be injected. |
728
|
|
|
* If so, then those classes will be added to the array of arguments passed to the constructor |
729
|
|
|
* |
730
|
|
|
* @access protected |
731
|
|
|
* @param string $class_name |
732
|
|
|
* @param array $arguments |
733
|
|
|
* @param string $type |
734
|
|
|
* @param bool $from_db |
735
|
|
|
* @return null | object |
736
|
|
|
* @throws \EE_Error |
737
|
|
|
*/ |
738
|
|
|
protected function _create_object( $class_name, $arguments = array(), $type = '', $from_db = false ) { |
739
|
|
|
$class_obj = null; |
740
|
|
|
// don't give up! you gotta... |
741
|
|
|
try { |
742
|
|
|
// create reflection |
743
|
|
|
$reflector = $this->get_ReflectionClass( $class_name ); |
744
|
|
|
// make sure arguments are an array |
745
|
|
|
$arguments = is_array( $arguments ) ? $arguments : array( $arguments ); |
746
|
|
|
// and if arguments array is numerically and sequentially indexed, then we want it to remain as is, |
747
|
|
|
// else wrap it in an additional array so that it doesn't get split into multiple parameters |
748
|
|
|
$arguments = $this->_array_is_numerically_and_sequentially_indexed( $arguments ) |
749
|
|
|
? $arguments |
750
|
|
|
: array( $arguments ); |
751
|
|
|
// attempt to inject dependencies ? |
752
|
|
|
if ( $this->_dependency_map->has( $class_name ) ) { |
753
|
|
|
$arguments = $this->_resolve_dependencies( $reflector, $class_name, $arguments ); |
754
|
|
|
} |
755
|
|
|
// instantiate the class and add to the LIB array for tracking |
756
|
|
|
// EE_Base_Classes are instantiated via new_instance by default (models call them via new_instance_from_db) |
757
|
|
|
if ( $reflector->getConstructor() === null || $reflector->isAbstract() ) { |
758
|
|
|
// no constructor = static methods only... nothing to instantiate, loading file was enough |
759
|
|
|
//$instantiation_mode = "no constructor"; |
760
|
|
|
$class_obj = true; |
761
|
|
|
} else if ( $from_db && method_exists( $class_name, 'new_instance_from_db' ) ) { |
762
|
|
|
//$instantiation_mode = "new_instance_from_db"; |
763
|
|
|
$class_obj = call_user_func_array( array( $class_name, 'new_instance_from_db' ), $arguments ); |
764
|
|
|
} else if ( method_exists( $class_name, 'new_instance' ) ) { |
765
|
|
|
//$instantiation_mode = "new_instance"; |
766
|
|
|
$class_obj = call_user_func_array( array( $class_name, 'new_instance' ), $arguments ); |
767
|
|
|
} else if ( method_exists( $class_name, 'instance' ) ) { |
768
|
|
|
//$instantiation_mode = "instance"; |
769
|
|
|
$class_obj = call_user_func_array( array( $class_name, 'instance' ), $arguments ); |
770
|
|
|
} else if ( $reflector->isInstantiable() ) { |
771
|
|
|
//$instantiation_mode = "isInstantiable"; |
772
|
|
|
$class_obj = $reflector->newInstanceArgs( $arguments ); |
773
|
|
|
} else { |
774
|
|
|
// heh ? something's not right ! |
775
|
|
|
//$instantiation_mode = 'none'; |
776
|
|
|
throw new EE_Error( |
777
|
|
|
sprintf( |
778
|
|
|
__( 'The %s file %s could not be instantiated.', 'event_espresso' ), |
779
|
|
|
$type, |
780
|
|
|
$class_name |
781
|
|
|
) |
782
|
|
|
); |
783
|
|
|
} |
784
|
|
|
} catch ( Exception $e ) { |
785
|
|
|
if ( ! $e instanceof EE_Error ) { |
786
|
|
|
$e = new EE_Error( $e->getMessage() ); |
787
|
|
|
} |
788
|
|
|
$e->get_error(); |
789
|
|
|
} |
790
|
|
|
return $class_obj; |
791
|
|
|
} |
792
|
|
|
|
793
|
|
|
|
794
|
|
|
|
795
|
|
|
/** |
796
|
|
|
* @see http://stackoverflow.com/questions/173400/how-to-check-if-php-array-is-associative-or-sequential |
797
|
|
|
* @param array $array |
798
|
|
|
* @return bool |
799
|
|
|
*/ |
800
|
|
|
protected function _array_is_numerically_and_sequentially_indexed( array $array ) { |
801
|
|
|
return ! empty( $array ) ? array_keys( $array ) === range( 0, count( $array ) - 1 ) : true; |
802
|
|
|
} |
803
|
|
|
|
804
|
|
|
|
805
|
|
|
|
806
|
|
|
/** |
807
|
|
|
* getReflectionClass |
808
|
|
|
* |
809
|
|
|
* checks if a ReflectionClass object has already been generated for a class |
810
|
|
|
* and returns that instead of creating a new one |
811
|
|
|
* |
812
|
|
|
* @access public |
813
|
|
|
* @param string $class_name |
814
|
|
|
* @return ReflectionClass |
815
|
|
|
*/ |
816
|
|
|
public function get_ReflectionClass( $class_name ) { |
817
|
|
|
if ( |
818
|
|
|
! isset( $this->_reflectors[ $class_name ] ) |
819
|
|
|
|| ! $this->_reflectors[ $class_name ] instanceof ReflectionClass |
820
|
|
|
) { |
821
|
|
|
$this->_reflectors[ $class_name ] = new ReflectionClass( $class_name ); |
822
|
|
|
} |
823
|
|
|
return $this->_reflectors[ $class_name ]; |
824
|
|
|
} |
825
|
|
|
|
826
|
|
|
|
827
|
|
|
|
828
|
|
|
/** |
829
|
|
|
* _resolve_dependencies |
830
|
|
|
* |
831
|
|
|
* examines the constructor for the requested class to determine |
832
|
|
|
* if any dependencies exist, and if they can be injected. |
833
|
|
|
* If so, then those classes will be added to the array of arguments passed to the constructor |
834
|
|
|
* PLZ NOTE: this is achieved by type hinting the constructor params |
835
|
|
|
* For example: |
836
|
|
|
* if attempting to load a class "Foo" with the following constructor: |
837
|
|
|
* __construct( Bar $bar_class, Fighter $grohl_class ) |
838
|
|
|
* then $bar_class and $grohl_class will be added to the $arguments array, |
839
|
|
|
* but only IF they are NOT already present in the incoming arguments array, |
840
|
|
|
* and the correct classes can be loaded |
841
|
|
|
* |
842
|
|
|
* @access protected |
843
|
|
|
* @param ReflectionClass $reflector |
844
|
|
|
* @param string $class_name |
845
|
|
|
* @param array $arguments |
846
|
|
|
* @return array |
847
|
|
|
*/ |
848
|
|
|
protected function _resolve_dependencies( ReflectionClass $reflector, $class_name, $arguments = array() ) { |
849
|
|
|
// let's examine the constructor |
850
|
|
|
$constructor = $reflector->getConstructor(); |
851
|
|
|
// whu? huh? nothing? |
852
|
|
|
if ( ! $constructor ) { |
853
|
|
|
return $arguments; |
854
|
|
|
} |
855
|
|
|
// get constructor parameters |
856
|
|
|
$params = $constructor->getParameters(); |
857
|
|
|
// and the keys for the incoming arguments array so that we can compare existing arguments with what is expected |
858
|
|
|
$argument_keys = array_keys( $arguments ); |
859
|
|
|
// now loop thru all of the constructors expected parameters |
860
|
|
|
foreach ( $params as $index => $param ) { |
861
|
|
|
// is this a dependency for a specific class ? |
862
|
|
|
$param_class = $param->getClass() ? $param->getClass()->name : null; |
863
|
|
|
if ( |
864
|
|
|
// param is not even a class |
865
|
|
|
empty( $param_class ) |
866
|
|
|
// and something already exists in the incoming arguments for this param |
867
|
|
|
&& isset( $argument_keys[ $index ], $arguments[ $argument_keys[ $index ] ] ) |
868
|
|
|
) { |
869
|
|
|
// so let's skip this argument and move on to the next |
870
|
|
|
continue; |
871
|
|
|
} else if ( |
872
|
|
|
// parameter is type hinted as a class, exists as an incoming argument, AND it's the correct class |
873
|
|
|
! empty( $param_class ) |
874
|
|
|
&& isset( $argument_keys[ $index ], $arguments[ $argument_keys[ $index ] ] ) |
875
|
|
|
&& $arguments[ $argument_keys[ $index ] ] instanceof $param_class |
876
|
|
|
) { |
877
|
|
|
// skip this argument and move on to the next |
878
|
|
|
continue; |
879
|
|
|
} else if ( |
880
|
|
|
// parameter is type hinted as a class, and should be injected |
881
|
|
|
! empty( $param_class ) |
882
|
|
|
&& $this->_dependency_map->has_dependency_for_class( $class_name, $param_class ) |
883
|
|
|
) { |
884
|
|
|
$arguments = $this->_resolve_dependency( $class_name, $param_class, $arguments, $index ); |
885
|
|
|
} else { |
886
|
|
|
$arguments[ $index ] = $param->getDefaultValue(); |
887
|
|
|
} |
888
|
|
|
|
889
|
|
|
} |
890
|
|
|
return $arguments; |
891
|
|
|
} |
892
|
|
|
|
893
|
|
|
|
894
|
|
|
|
895
|
|
|
/** |
896
|
|
|
* @access protected |
897
|
|
|
* @param string $class_name |
898
|
|
|
* @param string $param_class |
899
|
|
|
* @param array $arguments |
900
|
|
|
* @param mixed $index |
901
|
|
|
* @return array |
902
|
|
|
*/ |
903
|
|
|
protected function _resolve_dependency( $class_name, $param_class , $arguments, $index ) { |
904
|
|
|
$dependency = null; |
905
|
|
|
// should dependency be loaded from cache ? |
906
|
|
|
$cache_on = $this->_dependency_map->loading_strategy_for_class_dependency( |
907
|
|
|
$class_name, $param_class |
908
|
|
|
) !== EE_Dependency_Map::load_new_object |
909
|
|
|
? true |
910
|
|
|
: false; |
911
|
|
|
// we might have a dependency... |
912
|
|
|
// let's MAYBE try and find it in our cache if that's what's been requested |
913
|
|
|
$cached_class = $cache_on ? $this->_get_cached_class( $param_class ) : null; |
914
|
|
|
// and grab it if it exists |
915
|
|
|
if ( $cached_class instanceof $param_class ) { |
916
|
|
|
$dependency = $cached_class; |
917
|
|
|
} else if ( $param_class != $class_name ) { |
918
|
|
|
// obtain the loader method from the dependency map |
919
|
|
|
$loader = $this->_dependency_map->class_loader( $param_class ); |
920
|
|
|
// is loader a custom closure ? |
921
|
|
|
if ( $loader instanceof Closure ) { |
922
|
|
|
$dependency = $loader(); |
923
|
|
|
} else { |
924
|
|
|
// set the cache on property for the recursive loading call |
925
|
|
|
$this->_cache_on = $cache_on; |
926
|
|
|
// if not, then let's try and load it via the registry |
927
|
|
|
$dependency = $this->{$loader}( $param_class ); |
928
|
|
|
} |
929
|
|
|
} |
930
|
|
|
// did we successfully find the correct dependency ? |
931
|
|
|
if ( $dependency instanceof $param_class ) { |
932
|
|
|
// then let's inject it into the incoming array of arguments at the correct location |
933
|
|
|
if ( isset( $argument_keys[ $index ] ) ) { |
|
|
|
|
934
|
|
|
$arguments[ $argument_keys[ $index ] ] = $dependency; |
935
|
|
|
} else { |
936
|
|
|
$arguments[ $index ] = $dependency; |
937
|
|
|
} |
938
|
|
|
} |
939
|
|
|
return $arguments; |
940
|
|
|
} |
941
|
|
|
|
942
|
|
|
|
943
|
|
|
|
944
|
|
|
/** |
945
|
|
|
* _set_cached_class |
946
|
|
|
* |
947
|
|
|
* attempts to cache the instantiated class locally |
948
|
|
|
* in one of the following places, in the following order: |
949
|
|
|
* $this->{class_abbreviation} ie: $this->CART |
950
|
|
|
* $this->{$class_name} ie: $this->Some_Class |
951
|
|
|
* $this->addon->{$$class_name} ie: $this->addon->Some_Addon_Class |
952
|
|
|
* $this->LIB->{$class_name} ie: $this->LIB->Some_Class |
953
|
|
|
* |
954
|
|
|
* @access protected |
955
|
|
|
* @param object $class_obj |
956
|
|
|
* @param string $class_name |
957
|
|
|
* @param string $class_prefix |
958
|
|
|
* @param bool $from_db |
959
|
|
|
* @return void |
960
|
|
|
*/ |
961
|
|
|
protected function _set_cached_class( $class_obj, $class_name, $class_prefix = '', $from_db = false ) { |
962
|
|
|
// return newly instantiated class |
963
|
|
|
if ( isset( $this->_class_abbreviations[ $class_name ] ) ) { |
964
|
|
|
$class_abbreviation = $this->_class_abbreviations[ $class_name ]; |
965
|
|
|
$this->{$class_abbreviation} = $class_obj; |
966
|
|
|
} else if ( property_exists( $this, $class_name ) ) { |
967
|
|
|
$this->{$class_name} = $class_obj; |
968
|
|
|
} else if ( $class_prefix == 'addon' ) { |
969
|
|
|
$this->addons->{$class_name} = $class_obj; |
970
|
|
|
} else if ( ! $from_db ) { |
971
|
|
|
$this->LIB->{$class_name} = $class_obj; |
972
|
|
|
} |
973
|
|
|
} |
974
|
|
|
|
975
|
|
|
|
976
|
|
|
|
977
|
|
|
/** |
978
|
|
|
* call any loader that's been registered in the EE_Dependency_Map::$_class_loaders array |
979
|
|
|
* |
980
|
|
|
* |
981
|
|
|
* @param string $classname PLEASE NOTE: the class name needs to match what's registered |
982
|
|
|
* in the EE_Dependency_Map::$_class_loaders array, |
983
|
|
|
* including the class prefix, ie: "EE_", "EEM_", "EEH_", etc |
984
|
|
|
* @param array $arguments |
985
|
|
|
* @return object |
986
|
|
|
*/ |
987
|
|
|
public static function factory( $classname, $arguments = array() ) { |
988
|
|
|
$loader = self::instance()->_dependency_map->class_loader( $classname ); |
989
|
|
|
if ( $loader instanceof Closure ) { |
990
|
|
|
return $loader( $arguments ); |
991
|
|
|
} else if ( method_exists( EE_Registry::instance(), $loader ) ) { |
992
|
|
|
return EE_Registry::instance()->{$loader}( $classname, $arguments ); |
993
|
|
|
} |
994
|
|
|
return null; |
995
|
|
|
} |
996
|
|
|
|
997
|
|
|
|
998
|
|
|
|
999
|
|
|
/** |
1000
|
|
|
* Gets the addon by its name/slug (not classname. For that, just |
1001
|
|
|
* use the classname as the property name on EE_Config::instance()->addons) |
1002
|
|
|
* @param string $name |
1003
|
|
|
* @return EE_Addon |
1004
|
|
|
*/ |
1005
|
|
|
public function get_addon_by_name( $name ) { |
1006
|
|
|
foreach ( $this->addons as $addon ) { |
1007
|
|
|
if ( $addon->name() == $name ) { |
1008
|
|
|
return $addon; |
1009
|
|
|
} |
1010
|
|
|
} |
1011
|
|
|
return null; |
1012
|
|
|
} |
1013
|
|
|
|
1014
|
|
|
|
1015
|
|
|
|
1016
|
|
|
/** |
1017
|
|
|
* Gets an array of all the registered addons, where the keys are their names. (ie, what each returns for their name() function) They're already available on EE_Config::instance()->addons as properties, where each property's name is the addon's classname. So if you just want to get the addon by classname, use EE_Config::instance()->addons->{classname} |
1018
|
|
|
* |
1019
|
|
|
* @return EE_Addon[] where the KEYS are the addon's name() |
1020
|
|
|
*/ |
1021
|
|
|
public function get_addons_by_name() { |
1022
|
|
|
$addons = array(); |
1023
|
|
|
foreach ( $this->addons as $addon ) { |
1024
|
|
|
$addons[ $addon->name() ] = $addon; |
1025
|
|
|
} |
1026
|
|
|
return $addons; |
1027
|
|
|
} |
1028
|
|
|
|
1029
|
|
|
|
1030
|
|
|
|
1031
|
|
|
/** |
1032
|
|
|
* Resets the specified model's instance AND makes sure EE_Registry doesn't keep |
1033
|
|
|
* a stale copy of it around |
1034
|
|
|
* |
1035
|
|
|
* @param string $model_name |
1036
|
|
|
* @return \EEM_Base |
1037
|
|
|
* @throws \EE_Error |
1038
|
|
|
*/ |
1039
|
|
|
public function reset_model( $model_name ) { |
1040
|
|
|
$model = $this->load_model( $model_name ); |
1041
|
|
|
$model_class_name = get_class( $model ); |
1042
|
|
|
//get that model reset it and make sure we nuke the old reference to it |
1043
|
|
|
if ( $model instanceof $model_class_name && is_callable( array( $model_class_name, 'reset' ))) { |
1044
|
|
|
$this->LIB->{$model_class_name} = $model::reset(); |
1045
|
|
|
} else { |
1046
|
|
|
throw new EE_Error( sprintf( __( 'Model %s does not have a method "reset"', 'event_espresso' ), $model_name ) ); |
1047
|
|
|
} |
1048
|
|
|
return $this->LIB->{$model_class_name}; |
1049
|
|
|
} |
1050
|
|
|
|
1051
|
|
|
|
1052
|
|
|
|
1053
|
|
|
/** |
1054
|
|
|
* Resets the registry. |
1055
|
|
|
* |
1056
|
|
|
* The criteria for what gets reset is based on what can be shared between sites on the same request when switch_to_blog |
1057
|
|
|
* is used in a multisite install. Here is a list of things that are NOT reset. |
1058
|
|
|
* |
1059
|
|
|
* - $_dependency_map |
1060
|
|
|
* - $_class_abbreviations |
1061
|
|
|
* - $NET_CFG (EE_Network_Config): The config is shared network wide so no need to reset. |
1062
|
|
|
* - $REQ: Still on the same request so no need to change. |
1063
|
|
|
* - $CAP: There is no site specific state in the EE_Capability class. |
1064
|
|
|
* - $SSN: Although ideally, the session should not be shared between site switches, we can't reset it because only one Session |
1065
|
|
|
* can be active in a single request. Resetting could resolve in "headers already sent" errors. |
1066
|
|
|
* - $addons: In multisite, the state of the addons is something controlled via hooks etc in a normal request. So |
1067
|
|
|
* for now, we won't reset the addons because it could break calls to an add-ons class/methods in the |
1068
|
|
|
* switch or on the restore. |
1069
|
|
|
* - $modules |
1070
|
|
|
* - $shortcodes |
1071
|
|
|
* - $widgets |
1072
|
|
|
* - $LIB: Only specific classes get unset from $LIB (current EE_Data_Migration_Manager) that persist state. |
1073
|
|
|
* |
1074
|
|
|
* @param boolean $hard whether to reset data in the database too, or just refresh |
1075
|
|
|
* the Registry to its state at the beginning of the request |
1076
|
|
|
* @param boolean $reinstantiate whether to create new instances of EE_Registry's singletons too, |
1077
|
|
|
* or just reset without re-instantiating (handy to set to FALSE if you're not sure if you CAN |
1078
|
|
|
* currently reinstantiate the singletons at the moment) |
1079
|
|
|
* @param bool $reset_models Defaults to true. When false, then the models are not reset. This is so client |
1080
|
|
|
* code instead can just change the model context to a different blog id if necessary |
1081
|
|
|
* |
1082
|
|
|
* @return EE_Registry |
1083
|
|
|
*/ |
1084
|
|
|
public static function reset( $hard = false, $reinstantiate = true, $reset_models = true ) { |
1085
|
|
|
$instance = self::instance(); |
1086
|
|
|
EEH_Activation::reset(); |
1087
|
|
|
|
1088
|
|
|
//properties that get reset |
1089
|
|
|
$instance->_cache_on = true; |
1090
|
|
|
$instance->CFG = EE_Config::reset( $hard, $reinstantiate ); |
1091
|
|
|
$instance->CART = null; |
1092
|
|
|
$instance->MRM = null; |
1093
|
|
|
|
1094
|
|
|
//handle of objects cached on LIB |
1095
|
|
|
foreach ( $instance->_classes_to_unset_from_LIB_on_reset() as $class_name ) { |
1096
|
|
|
if ( isset( $instance->LIB->$class_name ) ) { |
1097
|
|
|
unset( $instance->LIB->$class_name ); |
1098
|
|
|
} |
1099
|
|
|
} |
1100
|
|
|
|
1101
|
|
|
if ( $reset_models ) { |
1102
|
|
|
foreach ( array_keys( $instance->non_abstract_db_models ) as $model_name ) { |
1103
|
|
|
$instance->reset_model( $model_name ); |
1104
|
|
|
} |
1105
|
|
|
} |
1106
|
|
|
|
1107
|
|
|
return $instance; |
1108
|
|
|
} |
1109
|
|
|
|
1110
|
|
|
|
1111
|
|
|
|
1112
|
|
|
|
1113
|
|
|
/** |
1114
|
|
|
* Returns a filtered array of classes to unset from the $LIB property when EE_Registry::reset is called. |
1115
|
|
|
* @return array |
1116
|
|
|
*/ |
1117
|
|
|
protected function _classes_to_unset_from_LIB_on_reset() { |
1118
|
|
|
return apply_filters( 'EE_Registry___classes_to_unset_from_LIB_on_reset', array( |
1119
|
|
|
'EE_Data_Migration_Manager', |
1120
|
|
|
'EE_Messages_Processor', |
1121
|
|
|
'EE_Messages_Queue', |
1122
|
|
|
)); |
1123
|
|
|
} |
1124
|
|
|
|
1125
|
|
|
|
1126
|
|
|
|
1127
|
|
|
/** |
1128
|
|
|
* @override magic methods |
1129
|
|
|
* @return void |
1130
|
|
|
*/ |
1131
|
|
|
final function __destruct() { |
1132
|
|
|
} |
1133
|
|
|
|
1134
|
|
|
|
1135
|
|
|
|
1136
|
|
|
/** |
1137
|
|
|
* @param $a |
1138
|
|
|
* @param $b |
1139
|
|
|
*/ |
1140
|
|
|
final function __call( $a, $b ) { |
1141
|
|
|
} |
1142
|
|
|
|
1143
|
|
|
|
1144
|
|
|
|
1145
|
|
|
/** |
1146
|
|
|
* @param $a |
1147
|
|
|
*/ |
1148
|
|
|
final function __get( $a ) { |
|
|
|
|
1149
|
|
|
} |
1150
|
|
|
|
1151
|
|
|
|
1152
|
|
|
|
1153
|
|
|
/** |
1154
|
|
|
* @param $a |
1155
|
|
|
* @param $b |
1156
|
|
|
*/ |
1157
|
|
|
final function __set( $a, $b ) { |
|
|
|
|
1158
|
|
|
} |
1159
|
|
|
|
1160
|
|
|
|
1161
|
|
|
|
1162
|
|
|
/** |
1163
|
|
|
* @param $a |
1164
|
|
|
*/ |
1165
|
|
|
final function __isset( $a ) { |
|
|
|
|
1166
|
|
|
} |
1167
|
|
|
|
1168
|
|
|
|
1169
|
|
|
|
1170
|
|
|
/** |
1171
|
|
|
* @param $a |
1172
|
|
|
*/ |
1173
|
|
|
final function __unset( $a ) { |
|
|
|
|
1174
|
|
|
} |
1175
|
|
|
|
1176
|
|
|
|
1177
|
|
|
|
1178
|
|
|
/** |
1179
|
|
|
* @return array |
1180
|
|
|
*/ |
1181
|
|
|
final function __sleep() { |
1182
|
|
|
return array(); |
1183
|
|
|
} |
1184
|
|
|
|
1185
|
|
|
|
1186
|
|
|
|
1187
|
|
|
final function __wakeup() { |
1188
|
|
|
} |
1189
|
|
|
|
1190
|
|
|
|
1191
|
|
|
|
1192
|
|
|
/** |
1193
|
|
|
* @return string |
1194
|
|
|
*/ |
1195
|
|
|
final function __toString() { |
1196
|
|
|
return ''; |
1197
|
|
|
} |
1198
|
|
|
|
1199
|
|
|
|
1200
|
|
|
|
1201
|
|
|
final function __invoke() { |
1202
|
|
|
} |
1203
|
|
|
|
1204
|
|
|
|
1205
|
|
|
|
1206
|
|
|
final function __set_state() { |
1207
|
|
|
} |
1208
|
|
|
|
1209
|
|
|
|
1210
|
|
|
|
1211
|
|
|
final function __clone() { |
1212
|
|
|
} |
1213
|
|
|
|
1214
|
|
|
|
1215
|
|
|
|
1216
|
|
|
/** |
1217
|
|
|
* @param $a |
1218
|
|
|
* @param $b |
1219
|
|
|
*/ |
1220
|
|
|
final static function __callStatic( $a, $b ) { |
|
|
|
|
1221
|
|
|
} |
1222
|
|
|
|
1223
|
|
|
/** |
1224
|
|
|
* Gets all the custom post type models defined |
1225
|
|
|
* @return array keys are model "short names" (Eg "Event") and keys are classnames (eg "EEM_Event") |
1226
|
|
|
*/ |
1227
|
|
|
public function cpt_models() { |
1228
|
|
|
$cpt_models = array(); |
1229
|
|
|
foreach( $this->non_abstract_db_models as $short_name => $classname ) { |
1230
|
|
|
if( is_subclass_of( $classname, 'EEM_CPT_Base' ) ) { |
1231
|
|
|
$cpt_models[ $short_name ] = $classname; |
1232
|
|
|
} |
1233
|
|
|
} |
1234
|
|
|
return $cpt_models; |
1235
|
|
|
} |
1236
|
|
|
|
1237
|
|
|
|
1238
|
|
|
|
1239
|
|
|
public static function CFG() { |
1240
|
|
|
return self::instance()->CFG; |
1241
|
|
|
} |
1242
|
|
|
|
1243
|
|
|
|
1244
|
|
|
} |
1245
|
|
|
// End of file EE_Registry.core.php |
1246
|
|
|
// Location: ./core/EE_Registry.core.php |
1247
|
|
|
|
It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.
We recommend to add an additional type check (or disallow null for the parameter):