@@ -9,19 +9,19 @@ |
||
9 | 9 | interface LoaderDecoratorInterface |
10 | 10 | { |
11 | 11 | |
12 | - /** |
|
13 | - * @param string $fqcn |
|
14 | - * @param array $arguments |
|
15 | - * @return mixed |
|
16 | - */ |
|
17 | - public function load($fqcn, $arguments = array()); |
|
12 | + /** |
|
13 | + * @param string $fqcn |
|
14 | + * @param array $arguments |
|
15 | + * @return mixed |
|
16 | + */ |
|
17 | + public function load($fqcn, $arguments = array()); |
|
18 | 18 | |
19 | 19 | |
20 | 20 | |
21 | - /** |
|
22 | - * calls reset() on loader if method exists |
|
23 | - */ |
|
24 | - public function reset(); |
|
21 | + /** |
|
22 | + * calls reset() on loader if method exists |
|
23 | + */ |
|
24 | + public function reset(); |
|
25 | 25 | |
26 | 26 | } |
27 | 27 | // End of file LoaderInterface.php |
@@ -24,59 +24,59 @@ |
||
24 | 24 | class CoreLoader implements LoaderDecoratorInterface |
25 | 25 | { |
26 | 26 | |
27 | - /** |
|
28 | - * @var EE_Registry|CoffeeShop $generator |
|
29 | - */ |
|
30 | - private $generator; |
|
31 | - |
|
32 | - |
|
33 | - |
|
34 | - /** |
|
35 | - * CoreLoader constructor. |
|
36 | - * |
|
37 | - * @param EE_Registry|CoffeeShop $generator |
|
38 | - * @throws InvalidArgumentException |
|
39 | - */ |
|
40 | - public function __construct($generator) |
|
41 | - { |
|
42 | - if(!($generator instanceof EE_Registry || $generator instanceof CoffeeShop)) { |
|
43 | - throw new InvalidArgumentException( |
|
44 | - esc_html__( |
|
45 | - 'The CoreLoader class must receive an instance of EE_Registry or the CoffeeShop DI container.', |
|
46 | - 'event_espresso' |
|
47 | - ) |
|
48 | - ); |
|
49 | - } |
|
50 | - $this->generator = $generator; |
|
51 | - } |
|
52 | - |
|
53 | - |
|
54 | - |
|
55 | - /** |
|
56 | - * @param string $fqcn |
|
57 | - * @param array $arguments |
|
58 | - * @return mixed |
|
59 | - * @throws EE_Error |
|
60 | - * @throws ServiceNotFoundException |
|
61 | - */ |
|
62 | - public function load($fqcn, $arguments = array()) |
|
63 | - { |
|
64 | - return $this->generator instanceof EE_Registry |
|
65 | - ? $this->generator->create($fqcn, $arguments) |
|
66 | - : $this->generator->brew($fqcn, $arguments); |
|
67 | - } |
|
68 | - |
|
69 | - |
|
70 | - |
|
71 | - /** |
|
72 | - * calls reset() on generator if method exists |
|
73 | - */ |
|
74 | - public function reset() |
|
75 | - { |
|
76 | - if (method_exists($this->generator, 'reset')) { |
|
77 | - $this->generator->reset(); |
|
78 | - } |
|
79 | - } |
|
27 | + /** |
|
28 | + * @var EE_Registry|CoffeeShop $generator |
|
29 | + */ |
|
30 | + private $generator; |
|
31 | + |
|
32 | + |
|
33 | + |
|
34 | + /** |
|
35 | + * CoreLoader constructor. |
|
36 | + * |
|
37 | + * @param EE_Registry|CoffeeShop $generator |
|
38 | + * @throws InvalidArgumentException |
|
39 | + */ |
|
40 | + public function __construct($generator) |
|
41 | + { |
|
42 | + if(!($generator instanceof EE_Registry || $generator instanceof CoffeeShop)) { |
|
43 | + throw new InvalidArgumentException( |
|
44 | + esc_html__( |
|
45 | + 'The CoreLoader class must receive an instance of EE_Registry or the CoffeeShop DI container.', |
|
46 | + 'event_espresso' |
|
47 | + ) |
|
48 | + ); |
|
49 | + } |
|
50 | + $this->generator = $generator; |
|
51 | + } |
|
52 | + |
|
53 | + |
|
54 | + |
|
55 | + /** |
|
56 | + * @param string $fqcn |
|
57 | + * @param array $arguments |
|
58 | + * @return mixed |
|
59 | + * @throws EE_Error |
|
60 | + * @throws ServiceNotFoundException |
|
61 | + */ |
|
62 | + public function load($fqcn, $arguments = array()) |
|
63 | + { |
|
64 | + return $this->generator instanceof EE_Registry |
|
65 | + ? $this->generator->create($fqcn, $arguments) |
|
66 | + : $this->generator->brew($fqcn, $arguments); |
|
67 | + } |
|
68 | + |
|
69 | + |
|
70 | + |
|
71 | + /** |
|
72 | + * calls reset() on generator if method exists |
|
73 | + */ |
|
74 | + public function reset() |
|
75 | + { |
|
76 | + if (method_exists($this->generator, 'reset')) { |
|
77 | + $this->generator->reset(); |
|
78 | + } |
|
79 | + } |
|
80 | 80 | |
81 | 81 | } |
82 | 82 | // End of file CoreLoader.php |
@@ -272,6 +272,7 @@ |
||
272 | 272 | |
273 | 273 | /** |
274 | 274 | * @param mixed string | EED_Module $module |
275 | + * @param string $module |
|
275 | 276 | * @throws EE_Error |
276 | 277 | * @throws ReflectionException |
277 | 278 | */ |
@@ -22,1491 +22,1491 @@ |
||
22 | 22 | class EE_Registry implements ResettableInterface |
23 | 23 | { |
24 | 24 | |
25 | - /** |
|
26 | - * @var EE_Registry $_instance |
|
27 | - */ |
|
28 | - private static $_instance; |
|
29 | - |
|
30 | - /** |
|
31 | - * @var EE_Dependency_Map $_dependency_map |
|
32 | - */ |
|
33 | - protected $_dependency_map; |
|
34 | - |
|
35 | - /** |
|
36 | - * @var array $_class_abbreviations |
|
37 | - */ |
|
38 | - protected $_class_abbreviations = array(); |
|
39 | - |
|
40 | - /** |
|
41 | - * @var CommandBusInterface $BUS |
|
42 | - */ |
|
43 | - public $BUS; |
|
44 | - |
|
45 | - /** |
|
46 | - * @var EE_Cart $CART |
|
47 | - */ |
|
48 | - public $CART; |
|
49 | - |
|
50 | - /** |
|
51 | - * @var EE_Config $CFG |
|
52 | - */ |
|
53 | - public $CFG; |
|
54 | - |
|
55 | - /** |
|
56 | - * @var EE_Network_Config $NET_CFG |
|
57 | - */ |
|
58 | - public $NET_CFG; |
|
59 | - |
|
60 | - /** |
|
61 | - * StdClass object for storing library classes in |
|
62 | - * |
|
63 | - * @var StdClass $LIB |
|
64 | - */ |
|
65 | - public $LIB; |
|
66 | - |
|
67 | - /** |
|
68 | - * @var EE_Request_Handler $REQ |
|
69 | - */ |
|
70 | - public $REQ; |
|
71 | - |
|
72 | - /** |
|
73 | - * @var EE_Session $SSN |
|
74 | - */ |
|
75 | - public $SSN; |
|
76 | - |
|
77 | - /** |
|
78 | - * @since 4.5.0 |
|
79 | - * @var EE_Capabilities $CAP |
|
80 | - */ |
|
81 | - public $CAP; |
|
82 | - |
|
83 | - /** |
|
84 | - * @since 4.9.0 |
|
85 | - * @var EE_Message_Resource_Manager $MRM |
|
86 | - */ |
|
87 | - public $MRM; |
|
88 | - |
|
89 | - |
|
90 | - /** |
|
91 | - * @var Registry $AssetsRegistry |
|
92 | - */ |
|
93 | - public $AssetsRegistry; |
|
94 | - |
|
95 | - /** |
|
96 | - * StdClass object for holding addons which have registered themselves to work with EE core |
|
97 | - * |
|
98 | - * @var EE_Addon[] $addons |
|
99 | - */ |
|
100 | - public $addons; |
|
101 | - |
|
102 | - /** |
|
103 | - * keys are 'short names' (eg Event), values are class names (eg 'EEM_Event') |
|
104 | - * |
|
105 | - * @var EEM_Base[] $models |
|
106 | - */ |
|
107 | - public $models = array(); |
|
108 | - |
|
109 | - /** |
|
110 | - * @var EED_Module[] $modules |
|
111 | - */ |
|
112 | - public $modules; |
|
113 | - |
|
114 | - /** |
|
115 | - * @var EES_Shortcode[] $shortcodes |
|
116 | - */ |
|
117 | - public $shortcodes; |
|
118 | - |
|
119 | - /** |
|
120 | - * @var WP_Widget[] $widgets |
|
121 | - */ |
|
122 | - public $widgets; |
|
123 | - |
|
124 | - /** |
|
125 | - * this is an array of all implemented model names (i.e. not the parent abstract models, or models |
|
126 | - * which don't actually fetch items from the DB in the normal way (ie, are not children of EEM_Base)). |
|
127 | - * Keys are model "short names" (eg "Event") as used in model relations, and values are |
|
128 | - * classnames (eg "EEM_Event") |
|
129 | - * |
|
130 | - * @var array $non_abstract_db_models |
|
131 | - */ |
|
132 | - public $non_abstract_db_models = array(); |
|
133 | - |
|
134 | - |
|
135 | - /** |
|
136 | - * internationalization for JS strings |
|
137 | - * usage: EE_Registry::i18n_js_strings['string_key'] = esc_html__( 'string to translate.', 'event_espresso' ); |
|
138 | - * in js file: var translatedString = eei18n.string_key; |
|
139 | - * |
|
140 | - * @var array $i18n_js_strings |
|
141 | - */ |
|
142 | - public static $i18n_js_strings = array(); |
|
143 | - |
|
144 | - |
|
145 | - /** |
|
146 | - * $main_file - path to espresso.php |
|
147 | - * |
|
148 | - * @var array $main_file |
|
149 | - */ |
|
150 | - public $main_file; |
|
151 | - |
|
152 | - /** |
|
153 | - * array of ReflectionClass objects where the key is the class name |
|
154 | - * |
|
155 | - * @var ReflectionClass[] $_reflectors |
|
156 | - */ |
|
157 | - public $_reflectors; |
|
158 | - |
|
159 | - /** |
|
160 | - * boolean flag to indicate whether or not to load/save dependencies from/to the cache |
|
161 | - * |
|
162 | - * @var boolean $_cache_on |
|
163 | - */ |
|
164 | - protected $_cache_on = true; |
|
165 | - |
|
166 | - |
|
167 | - |
|
168 | - /** |
|
169 | - * @singleton method used to instantiate class object |
|
170 | - * @param EE_Dependency_Map $dependency_map |
|
171 | - * @return EE_Registry instance |
|
172 | - * @throws InvalidArgumentException |
|
173 | - * @throws InvalidInterfaceException |
|
174 | - * @throws InvalidDataTypeException |
|
175 | - */ |
|
176 | - public static function instance(EE_Dependency_Map $dependency_map = null) |
|
177 | - { |
|
178 | - // check if class object is instantiated |
|
179 | - if (! self::$_instance instanceof EE_Registry) { |
|
180 | - self::$_instance = new self($dependency_map); |
|
181 | - } |
|
182 | - return self::$_instance; |
|
183 | - } |
|
184 | - |
|
185 | - |
|
186 | - |
|
187 | - /** |
|
188 | - * protected constructor to prevent direct creation |
|
189 | - * |
|
190 | - * @Constructor |
|
191 | - * @param EE_Dependency_Map $dependency_map |
|
192 | - * @throws InvalidDataTypeException |
|
193 | - * @throws InvalidInterfaceException |
|
194 | - * @throws InvalidArgumentException |
|
195 | - */ |
|
196 | - protected function __construct(EE_Dependency_Map $dependency_map) |
|
197 | - { |
|
198 | - $this->_dependency_map = $dependency_map; |
|
199 | - $this->LIB = new stdClass(); |
|
200 | - $this->addons = new stdClass(); |
|
201 | - $this->modules = new stdClass(); |
|
202 | - $this->shortcodes = new stdClass(); |
|
203 | - $this->widgets = new stdClass(); |
|
204 | - add_action('EE_Load_Espresso_Core__handle_request__initialize_core_loading', array($this, 'initialize')); |
|
205 | - } |
|
206 | - |
|
207 | - |
|
208 | - |
|
209 | - /** |
|
210 | - * initialize |
|
211 | - * |
|
212 | - * @throws EE_Error |
|
213 | - * @throws ReflectionException |
|
214 | - */ |
|
215 | - public function initialize() |
|
216 | - { |
|
217 | - $this->_class_abbreviations = apply_filters( |
|
218 | - 'FHEE__EE_Registry____construct___class_abbreviations', |
|
219 | - array( |
|
220 | - 'EE_Config' => 'CFG', |
|
221 | - 'EE_Session' => 'SSN', |
|
222 | - 'EE_Capabilities' => 'CAP', |
|
223 | - 'EE_Cart' => 'CART', |
|
224 | - 'EE_Network_Config' => 'NET_CFG', |
|
225 | - 'EE_Request_Handler' => 'REQ', |
|
226 | - 'EE_Message_Resource_Manager' => 'MRM', |
|
227 | - 'EventEspresso\core\services\commands\CommandBus' => 'BUS', |
|
228 | - 'EventEspresso\core\services\assets\Registry' => 'AssetsRegistry', |
|
229 | - ) |
|
230 | - ); |
|
231 | - $this->load_core('Base', array(), true); |
|
232 | - // add our request and response objects to the cache |
|
233 | - $request_loader = $this->_dependency_map->class_loader('EE_Request'); |
|
234 | - $this->_set_cached_class( |
|
235 | - $request_loader(), |
|
236 | - 'EE_Request' |
|
237 | - ); |
|
238 | - $response_loader = $this->_dependency_map->class_loader('EE_Response'); |
|
239 | - $this->_set_cached_class( |
|
240 | - $response_loader(), |
|
241 | - 'EE_Response' |
|
242 | - ); |
|
243 | - add_action('AHEE__EE_System__set_hooks_for_core', array($this, 'init')); |
|
244 | - } |
|
245 | - |
|
246 | - |
|
247 | - |
|
248 | - /** |
|
249 | - * @return void |
|
250 | - */ |
|
251 | - public function init() |
|
252 | - { |
|
253 | - // Get current page protocol |
|
254 | - $protocol = isset($_SERVER['HTTPS']) ? 'https://' : 'http://'; |
|
255 | - // Output admin-ajax.php URL with same protocol as current page |
|
256 | - self::$i18n_js_strings['ajax_url'] = admin_url('admin-ajax.php', $protocol); |
|
257 | - self::$i18n_js_strings['wp_debug'] = defined('WP_DEBUG') ? WP_DEBUG : false; |
|
258 | - } |
|
259 | - |
|
260 | - |
|
261 | - |
|
262 | - /** |
|
263 | - * localize_i18n_js_strings |
|
264 | - * |
|
265 | - * @return string |
|
266 | - */ |
|
267 | - public static function localize_i18n_js_strings() |
|
268 | - { |
|
269 | - $i18n_js_strings = (array)self::$i18n_js_strings; |
|
270 | - foreach ($i18n_js_strings as $key => $value) { |
|
271 | - if (is_scalar($value)) { |
|
272 | - $i18n_js_strings[$key] = html_entity_decode((string)$value, ENT_QUOTES, 'UTF-8'); |
|
273 | - } |
|
274 | - } |
|
275 | - return '/* <![CDATA[ */ var eei18n = ' . wp_json_encode($i18n_js_strings) . '; /* ]]> */'; |
|
276 | - } |
|
277 | - |
|
278 | - |
|
279 | - |
|
280 | - /** |
|
281 | - * @param mixed string | EED_Module $module |
|
282 | - * @throws EE_Error |
|
283 | - * @throws ReflectionException |
|
284 | - */ |
|
285 | - public function add_module($module) |
|
286 | - { |
|
287 | - if ($module instanceof EED_Module) { |
|
288 | - $module_class = get_class($module); |
|
289 | - $this->modules->{$module_class} = $module; |
|
290 | - } else { |
|
291 | - if (! class_exists('EE_Module_Request_Router')) { |
|
292 | - $this->load_core('Module_Request_Router'); |
|
293 | - } |
|
294 | - $this->modules->{$module} = EE_Module_Request_Router::module_factory($module); |
|
295 | - } |
|
296 | - } |
|
297 | - |
|
298 | - |
|
299 | - |
|
300 | - /** |
|
301 | - * @param string $module_name |
|
302 | - * @return mixed EED_Module | NULL |
|
303 | - */ |
|
304 | - public function get_module($module_name = '') |
|
305 | - { |
|
306 | - return isset($this->modules->{$module_name}) |
|
307 | - ? $this->modules->{$module_name} |
|
308 | - : null; |
|
309 | - } |
|
310 | - |
|
311 | - |
|
312 | - |
|
313 | - /** |
|
314 | - * loads core classes - must be singletons |
|
315 | - * |
|
316 | - * @param string $class_name - simple class name ie: session |
|
317 | - * @param mixed $arguments |
|
318 | - * @param bool $load_only |
|
319 | - * @return mixed |
|
320 | - * @throws EE_Error |
|
321 | - * @throws ReflectionException |
|
322 | - */ |
|
323 | - public function load_core($class_name, $arguments = array(), $load_only = false) |
|
324 | - { |
|
325 | - $core_paths = apply_filters( |
|
326 | - 'FHEE__EE_Registry__load_core__core_paths', |
|
327 | - array( |
|
328 | - EE_CORE, |
|
329 | - EE_ADMIN, |
|
330 | - EE_CPTS, |
|
331 | - EE_CORE . 'data_migration_scripts' . DS, |
|
332 | - EE_CORE . 'request_stack' . DS, |
|
333 | - EE_CORE . 'middleware' . DS, |
|
334 | - ) |
|
335 | - ); |
|
336 | - // retrieve instantiated class |
|
337 | - return $this->_load( |
|
338 | - $core_paths, |
|
339 | - 'EE_', |
|
340 | - $class_name, |
|
341 | - 'core', |
|
342 | - $arguments, |
|
343 | - false, |
|
344 | - true, |
|
345 | - $load_only |
|
346 | - ); |
|
347 | - } |
|
348 | - |
|
349 | - |
|
350 | - |
|
351 | - /** |
|
352 | - * loads service classes |
|
353 | - * |
|
354 | - * @param string $class_name - simple class name ie: session |
|
355 | - * @param mixed $arguments |
|
356 | - * @param bool $load_only |
|
357 | - * @return mixed |
|
358 | - * @throws EE_Error |
|
359 | - * @throws ReflectionException |
|
360 | - */ |
|
361 | - public function load_service($class_name, $arguments = array(), $load_only = false) |
|
362 | - { |
|
363 | - $service_paths = apply_filters( |
|
364 | - 'FHEE__EE_Registry__load_service__service_paths', |
|
365 | - array( |
|
366 | - EE_CORE . 'services' . DS, |
|
367 | - ) |
|
368 | - ); |
|
369 | - // retrieve instantiated class |
|
370 | - return $this->_load( |
|
371 | - $service_paths, |
|
372 | - 'EE_', |
|
373 | - $class_name, |
|
374 | - 'class', |
|
375 | - $arguments, |
|
376 | - false, |
|
377 | - true, |
|
378 | - $load_only |
|
379 | - ); |
|
380 | - } |
|
381 | - |
|
382 | - |
|
383 | - |
|
384 | - /** |
|
385 | - * loads data_migration_scripts |
|
386 | - * |
|
387 | - * @param string $class_name - class name for the DMS ie: EE_DMS_Core_4_2_0 |
|
388 | - * @param mixed $arguments |
|
389 | - * @return EE_Data_Migration_Script_Base|mixed |
|
390 | - * @throws EE_Error |
|
391 | - * @throws ReflectionException |
|
392 | - */ |
|
393 | - public function load_dms($class_name, $arguments = array()) |
|
394 | - { |
|
395 | - // retrieve instantiated class |
|
396 | - return $this->_load( |
|
397 | - EE_Data_Migration_Manager::instance()->get_data_migration_script_folders(), |
|
398 | - 'EE_DMS_', |
|
399 | - $class_name, |
|
400 | - 'dms', |
|
401 | - $arguments, |
|
402 | - false, |
|
403 | - false |
|
404 | - ); |
|
405 | - } |
|
406 | - |
|
407 | - |
|
408 | - |
|
409 | - /** |
|
410 | - * loads object creating classes - must be singletons |
|
411 | - * |
|
412 | - * @param string $class_name - simple class name ie: attendee |
|
413 | - * @param mixed $arguments - an array of arguments to pass to the class |
|
414 | - * @param bool $from_db - some classes are instantiated from the db and thus call a different method to |
|
415 | - * instantiate |
|
416 | - * @param bool $cache if you don't want the class to be stored in the internal cache (non-persistent) then |
|
417 | - * set this to FALSE (ie. when instantiating model objects from client in a loop) |
|
418 | - * @param bool $load_only whether or not to just load the file and NOT instantiate, or load AND instantiate |
|
419 | - * (default) |
|
420 | - * @return EE_Base_Class | bool |
|
421 | - * @throws EE_Error |
|
422 | - * @throws ReflectionException |
|
423 | - */ |
|
424 | - public function load_class($class_name, $arguments = array(), $from_db = false, $cache = true, $load_only = false) |
|
425 | - { |
|
426 | - $paths = apply_filters( |
|
427 | - 'FHEE__EE_Registry__load_class__paths', array( |
|
428 | - EE_CORE, |
|
429 | - EE_CLASSES, |
|
430 | - EE_BUSINESS, |
|
431 | - ) |
|
432 | - ); |
|
433 | - // retrieve instantiated class |
|
434 | - return $this->_load( |
|
435 | - $paths, |
|
436 | - 'EE_', |
|
437 | - $class_name, |
|
438 | - 'class', |
|
439 | - $arguments, |
|
440 | - $from_db, |
|
441 | - $cache, |
|
442 | - $load_only |
|
443 | - ); |
|
444 | - } |
|
445 | - |
|
446 | - |
|
447 | - |
|
448 | - /** |
|
449 | - * loads helper classes - must be singletons |
|
450 | - * |
|
451 | - * @param string $class_name - simple class name ie: price |
|
452 | - * @param mixed $arguments |
|
453 | - * @param bool $load_only |
|
454 | - * @return EEH_Base | bool |
|
455 | - * @throws EE_Error |
|
456 | - * @throws ReflectionException |
|
457 | - */ |
|
458 | - public function load_helper($class_name, $arguments = array(), $load_only = true) |
|
459 | - { |
|
460 | - // todo: add doing_it_wrong() in a few versions after all addons have had calls to this method removed |
|
461 | - $helper_paths = apply_filters('FHEE__EE_Registry__load_helper__helper_paths', array(EE_HELPERS)); |
|
462 | - // retrieve instantiated class |
|
463 | - return $this->_load( |
|
464 | - $helper_paths, |
|
465 | - 'EEH_', |
|
466 | - $class_name, |
|
467 | - 'helper', |
|
468 | - $arguments, |
|
469 | - false, |
|
470 | - true, |
|
471 | - $load_only |
|
472 | - ); |
|
473 | - } |
|
474 | - |
|
475 | - |
|
476 | - |
|
477 | - /** |
|
478 | - * loads core classes - must be singletons |
|
479 | - * |
|
480 | - * @param string $class_name - simple class name ie: session |
|
481 | - * @param mixed $arguments |
|
482 | - * @param bool $load_only |
|
483 | - * @param bool $cache whether to cache the object or not. |
|
484 | - * @return mixed |
|
485 | - * @throws EE_Error |
|
486 | - * @throws ReflectionException |
|
487 | - */ |
|
488 | - public function load_lib($class_name, $arguments = array(), $load_only = false, $cache = true) |
|
489 | - { |
|
490 | - $paths = array( |
|
491 | - EE_LIBRARIES, |
|
492 | - EE_LIBRARIES . 'messages' . DS, |
|
493 | - EE_LIBRARIES . 'shortcodes' . DS, |
|
494 | - EE_LIBRARIES . 'qtips' . DS, |
|
495 | - EE_LIBRARIES . 'payment_methods' . DS, |
|
496 | - ); |
|
497 | - // retrieve instantiated class |
|
498 | - return $this->_load( |
|
499 | - $paths, |
|
500 | - 'EE_', |
|
501 | - $class_name, |
|
502 | - 'lib', |
|
503 | - $arguments, |
|
504 | - false, |
|
505 | - $cache, |
|
506 | - $load_only |
|
507 | - ); |
|
508 | - } |
|
509 | - |
|
510 | - |
|
511 | - |
|
512 | - /** |
|
513 | - * loads model classes - must be singletons |
|
514 | - * |
|
515 | - * @param string $class_name - simple class name ie: price |
|
516 | - * @param mixed $arguments |
|
517 | - * @param bool $load_only |
|
518 | - * @return EEM_Base | bool |
|
519 | - * @throws EE_Error |
|
520 | - * @throws ReflectionException |
|
521 | - */ |
|
522 | - public function load_model($class_name, $arguments = array(), $load_only = false) |
|
523 | - { |
|
524 | - $paths = apply_filters( |
|
525 | - 'FHEE__EE_Registry__load_model__paths', array( |
|
526 | - EE_MODELS, |
|
527 | - EE_CORE, |
|
528 | - ) |
|
529 | - ); |
|
530 | - // retrieve instantiated class |
|
531 | - return $this->_load( |
|
532 | - $paths, |
|
533 | - 'EEM_', |
|
534 | - $class_name, |
|
535 | - 'model', |
|
536 | - $arguments, |
|
537 | - false, |
|
538 | - true, |
|
539 | - $load_only |
|
540 | - ); |
|
541 | - } |
|
542 | - |
|
543 | - |
|
544 | - |
|
545 | - /** |
|
546 | - * loads model classes - must be singletons |
|
547 | - * |
|
548 | - * @param string $class_name - simple class name ie: price |
|
549 | - * @param mixed $arguments |
|
550 | - * @param bool $load_only |
|
551 | - * @return mixed | bool |
|
552 | - * @throws EE_Error |
|
553 | - * @throws ReflectionException |
|
554 | - */ |
|
555 | - public function load_model_class($class_name, $arguments = array(), $load_only = true) |
|
556 | - { |
|
557 | - $paths = array( |
|
558 | - EE_MODELS . 'fields' . DS, |
|
559 | - EE_MODELS . 'helpers' . DS, |
|
560 | - EE_MODELS . 'relations' . DS, |
|
561 | - EE_MODELS . 'strategies' . DS, |
|
562 | - ); |
|
563 | - // retrieve instantiated class |
|
564 | - return $this->_load( |
|
565 | - $paths, |
|
566 | - 'EE_', |
|
567 | - $class_name, |
|
568 | - '', |
|
569 | - $arguments, |
|
570 | - false, |
|
571 | - true, |
|
572 | - $load_only |
|
573 | - ); |
|
574 | - } |
|
575 | - |
|
576 | - |
|
577 | - |
|
578 | - /** |
|
579 | - * Determines if $model_name is the name of an actual EE model. |
|
580 | - * |
|
581 | - * @param string $model_name like Event, Attendee, Question_Group_Question, etc. |
|
582 | - * @return boolean |
|
583 | - */ |
|
584 | - public function is_model_name($model_name) |
|
585 | - { |
|
586 | - return isset($this->models[$model_name]); |
|
587 | - } |
|
588 | - |
|
589 | - |
|
590 | - |
|
591 | - /** |
|
592 | - * generic class loader |
|
593 | - * |
|
594 | - * @param string $path_to_file - directory path to file location, not including filename |
|
595 | - * @param string $file_name - file name ie: my_file.php, including extension |
|
596 | - * @param string $type - file type - core? class? helper? model? |
|
597 | - * @param mixed $arguments |
|
598 | - * @param bool $load_only |
|
599 | - * @return mixed |
|
600 | - * @throws EE_Error |
|
601 | - * @throws ReflectionException |
|
602 | - */ |
|
603 | - public function load_file($path_to_file, $file_name, $type = '', $arguments = array(), $load_only = true) |
|
604 | - { |
|
605 | - // retrieve instantiated class |
|
606 | - return $this->_load( |
|
607 | - $path_to_file, |
|
608 | - '', |
|
609 | - $file_name, |
|
610 | - $type, |
|
611 | - $arguments, |
|
612 | - false, |
|
613 | - true, |
|
614 | - $load_only |
|
615 | - ); |
|
616 | - } |
|
617 | - |
|
618 | - |
|
619 | - |
|
620 | - /** |
|
621 | - * @param string $path_to_file - directory path to file location, not including filename |
|
622 | - * @param string $class_name - full class name ie: My_Class |
|
623 | - * @param string $type - file type - core? class? helper? model? |
|
624 | - * @param mixed $arguments |
|
625 | - * @param bool $load_only |
|
626 | - * @return bool|EE_Addon|object |
|
627 | - * @throws EE_Error |
|
628 | - * @throws ReflectionException |
|
629 | - */ |
|
630 | - public function load_addon($path_to_file, $class_name, $type = 'class', $arguments = array(), $load_only = false) |
|
631 | - { |
|
632 | - // retrieve instantiated class |
|
633 | - return $this->_load( |
|
634 | - $path_to_file, |
|
635 | - 'addon', |
|
636 | - $class_name, |
|
637 | - $type, |
|
638 | - $arguments, |
|
639 | - false, |
|
640 | - true, |
|
641 | - $load_only |
|
642 | - ); |
|
643 | - } |
|
644 | - |
|
645 | - |
|
646 | - |
|
647 | - /** |
|
648 | - * instantiates, caches, and automatically resolves dependencies |
|
649 | - * for classes that use a Fully Qualified Class Name. |
|
650 | - * if the class is not capable of being loaded using PSR-4 autoloading, |
|
651 | - * then you need to use one of the existing load_*() methods |
|
652 | - * which can resolve the classname and filepath from the passed arguments |
|
653 | - * |
|
654 | - * @param bool|string $class_name Fully Qualified Class Name |
|
655 | - * @param array $arguments an argument, or array of arguments to pass to the class upon instantiation |
|
656 | - * @param bool $cache whether to cache the instantiated object for reuse |
|
657 | - * @param bool $from_db some classes are instantiated from the db |
|
658 | - * and thus call a different method to instantiate |
|
659 | - * @param bool $load_only if true, will only load the file, but will NOT instantiate an object |
|
660 | - * @param bool|string $addon if true, will cache the object in the EE_Registry->$addons array |
|
661 | - * @return bool|null|mixed null = failure to load or instantiate class object. |
|
662 | - * object = class loaded and instantiated successfully. |
|
663 | - * bool = fail or success when $load_only is true |
|
664 | - * @throws EE_Error |
|
665 | - * @throws ReflectionException |
|
666 | - */ |
|
667 | - public function create( |
|
668 | - $class_name = false, |
|
669 | - $arguments = array(), |
|
670 | - $cache = false, |
|
671 | - $from_db = false, |
|
672 | - $load_only = false, |
|
673 | - $addon = false |
|
674 | - ) { |
|
675 | - $class_name = ltrim($class_name, '\\'); |
|
676 | - $class_name = $this->_dependency_map->get_alias($class_name); |
|
677 | - if (! class_exists($class_name)) { |
|
678 | - // maybe the class is registered with a preceding \ |
|
679 | - $class_name = strpos($class_name, '\\') !== 0 |
|
680 | - ? '\\' . $class_name |
|
681 | - : $class_name; |
|
682 | - // still doesn't exist ? |
|
683 | - if (! class_exists($class_name)) { |
|
684 | - return null; |
|
685 | - } |
|
686 | - } |
|
687 | - // if we're only loading the class and it already exists, then let's just return true immediately |
|
688 | - if ($load_only) { |
|
689 | - return true; |
|
690 | - } |
|
691 | - $addon = $addon |
|
692 | - ? 'addon' |
|
693 | - : ''; |
|
694 | - // $this->_cache_on is toggled during the recursive loading that can occur with dependency injection |
|
695 | - // $cache is controlled by individual calls to separate Registry loader methods like load_class() |
|
696 | - // $load_only is also controlled by individual calls to separate Registry loader methods like load_file() |
|
697 | - if ($this->_cache_on && $cache && ! $load_only) { |
|
698 | - // return object if it's already cached |
|
699 | - $cached_class = $this->_get_cached_class($class_name, $addon); |
|
700 | - if ($cached_class !== null) { |
|
701 | - return $cached_class; |
|
702 | - } |
|
703 | - } |
|
704 | - // obtain the loader method from the dependency map |
|
705 | - $loader = $this->_dependency_map->class_loader($class_name); |
|
706 | - // instantiate the requested object |
|
707 | - if ($loader instanceof Closure) { |
|
708 | - $class_obj = $loader($arguments); |
|
709 | - } else if ($loader && method_exists($this, $loader)) { |
|
710 | - $class_obj = $this->{$loader}($class_name, $arguments); |
|
711 | - } else { |
|
712 | - $class_obj = $this->_create_object($class_name, $arguments, $addon, $from_db); |
|
713 | - } |
|
714 | - if (($this->_cache_on && $cache) || $this->get_class_abbreviation($class_name, '')) { |
|
715 | - // save it for later... kinda like gum { : $ |
|
716 | - $this->_set_cached_class($class_obj, $class_name, $addon, $from_db); |
|
717 | - } |
|
718 | - $this->_cache_on = true; |
|
719 | - return $class_obj; |
|
720 | - } |
|
721 | - |
|
722 | - |
|
723 | - |
|
724 | - /** |
|
725 | - * instantiates, caches, and injects dependencies for classes |
|
726 | - * |
|
727 | - * @param array $file_paths an array of paths to folders to look in |
|
728 | - * @param string $class_prefix EE or EEM or... ??? |
|
729 | - * @param bool|string $class_name $class name |
|
730 | - * @param string $type file type - core? class? helper? model? |
|
731 | - * @param mixed $arguments an argument or array of arguments to pass to the class upon instantiation |
|
732 | - * @param bool $from_db some classes are instantiated from the db |
|
733 | - * and thus call a different method to instantiate |
|
734 | - * @param bool $cache whether to cache the instantiated object for reuse |
|
735 | - * @param bool $load_only if true, will only load the file, but will NOT instantiate an object |
|
736 | - * @return bool|null|object null = failure to load or instantiate class object. |
|
737 | - * object = class loaded and instantiated successfully. |
|
738 | - * bool = fail or success when $load_only is true |
|
739 | - * @throws EE_Error |
|
740 | - * @throws ReflectionException |
|
741 | - */ |
|
742 | - protected function _load( |
|
743 | - $file_paths = array(), |
|
744 | - $class_prefix = 'EE_', |
|
745 | - $class_name = false, |
|
746 | - $type = 'class', |
|
747 | - $arguments = array(), |
|
748 | - $from_db = false, |
|
749 | - $cache = true, |
|
750 | - $load_only = false |
|
751 | - ) { |
|
752 | - $class_name = ltrim($class_name, '\\'); |
|
753 | - // strip php file extension |
|
754 | - $class_name = str_replace('.php', '', trim($class_name)); |
|
755 | - // does the class have a prefix ? |
|
756 | - if (! empty($class_prefix) && $class_prefix !== 'addon') { |
|
757 | - // make sure $class_prefix is uppercase |
|
758 | - $class_prefix = strtoupper(trim($class_prefix)); |
|
759 | - // add class prefix ONCE!!! |
|
760 | - $class_name = $class_prefix . str_replace($class_prefix, '', $class_name); |
|
761 | - } |
|
762 | - $class_name = $this->_dependency_map->get_alias($class_name); |
|
763 | - $class_exists = class_exists($class_name); |
|
764 | - // if we're only loading the class and it already exists, then let's just return true immediately |
|
765 | - if ($load_only && $class_exists) { |
|
766 | - return true; |
|
767 | - } |
|
768 | - // $this->_cache_on is toggled during the recursive loading that can occur with dependency injection |
|
769 | - // $cache is controlled by individual calls to separate Registry loader methods like load_class() |
|
770 | - // $load_only is also controlled by individual calls to separate Registry loader methods like load_file() |
|
771 | - if ($this->_cache_on && $cache && ! $load_only) { |
|
772 | - // return object if it's already cached |
|
773 | - $cached_class = $this->_get_cached_class($class_name, $class_prefix); |
|
774 | - if ($cached_class !== null) { |
|
775 | - return $cached_class; |
|
776 | - } |
|
777 | - } |
|
778 | - // if the class doesn't already exist.. then we need to try and find the file and load it |
|
779 | - if (! $class_exists) { |
|
780 | - // get full path to file |
|
781 | - $path = $this->_resolve_path($class_name, $type, $file_paths); |
|
782 | - // load the file |
|
783 | - $loaded = $this->_require_file($path, $class_name, $type, $file_paths); |
|
784 | - // if loading failed, or we are only loading a file but NOT instantiating an object |
|
785 | - if (! $loaded || $load_only) { |
|
786 | - // return boolean if only loading, or null if an object was expected |
|
787 | - return $load_only |
|
788 | - ? $loaded |
|
789 | - : null; |
|
790 | - } |
|
791 | - } |
|
792 | - // instantiate the requested object |
|
793 | - $class_obj = $this->_create_object($class_name, $arguments, $type, $from_db); |
|
794 | - if ($this->_cache_on && $cache) { |
|
795 | - // save it for later... kinda like gum { : $ |
|
796 | - $this->_set_cached_class($class_obj, $class_name, $class_prefix, $from_db); |
|
797 | - } |
|
798 | - $this->_cache_on = true; |
|
799 | - return $class_obj; |
|
800 | - } |
|
801 | - |
|
802 | - |
|
803 | - |
|
804 | - /** |
|
805 | - * @param string $class_name |
|
806 | - * @param string $default have to specify something, but not anything that will conflict |
|
807 | - * @return mixed|string |
|
808 | - */ |
|
809 | - protected function get_class_abbreviation($class_name, $default = 'FANCY_BATMAN_PANTS') |
|
810 | - { |
|
811 | - return isset($this->_class_abbreviations[$class_name]) |
|
812 | - ? $this->_class_abbreviations[$class_name] |
|
813 | - : $default; |
|
814 | - } |
|
815 | - |
|
816 | - /** |
|
817 | - * attempts to find a cached version of the requested class |
|
818 | - * by looking in the following places: |
|
819 | - * $this->{$class_abbreviation} ie: $this->CART |
|
820 | - * $this->{$class_name} ie: $this->Some_Class |
|
821 | - * $this->LIB->{$class_name} ie: $this->LIB->Some_Class |
|
822 | - * $this->addon->{$class_name} ie: $this->addon->Some_Addon_Class |
|
823 | - * |
|
824 | - * @param string $class_name |
|
825 | - * @param string $class_prefix |
|
826 | - * @return mixed |
|
827 | - */ |
|
828 | - protected function _get_cached_class($class_name, $class_prefix = '') |
|
829 | - { |
|
830 | - if ($class_name === 'EE_Registry') { |
|
831 | - return $this; |
|
832 | - } |
|
833 | - $class_abbreviation = $this->get_class_abbreviation($class_name); |
|
834 | - $class_name = str_replace('\\', '_', $class_name); |
|
835 | - // check if class has already been loaded, and return it if it has been |
|
836 | - if (isset($this->{$class_abbreviation})) { |
|
837 | - return $this->{$class_abbreviation}; |
|
838 | - } |
|
839 | - if (isset ($this->{$class_name})) { |
|
840 | - return $this->{$class_name}; |
|
841 | - } |
|
842 | - if (isset ($this->LIB->{$class_name})) { |
|
843 | - return $this->LIB->{$class_name}; |
|
844 | - } |
|
845 | - if ($class_prefix === 'addon' && isset ($this->addons->{$class_name})) { |
|
846 | - return $this->addons->{$class_name}; |
|
847 | - } |
|
848 | - return null; |
|
849 | - } |
|
850 | - |
|
851 | - |
|
852 | - |
|
853 | - /** |
|
854 | - * removes a cached version of the requested class |
|
855 | - * |
|
856 | - * @param string $class_name |
|
857 | - * @param boolean $addon |
|
858 | - * @return boolean |
|
859 | - */ |
|
860 | - public function clear_cached_class($class_name, $addon = false) |
|
861 | - { |
|
862 | - $class_abbreviation = $this->get_class_abbreviation($class_name); |
|
863 | - $class_name = str_replace('\\', '_', $class_name); |
|
864 | - // check if class has already been loaded, and return it if it has been |
|
865 | - if (isset($this->{$class_abbreviation})) { |
|
866 | - $this->{$class_abbreviation} = null; |
|
867 | - return true; |
|
868 | - } |
|
869 | - if (isset($this->{$class_name})) { |
|
870 | - $this->{$class_name} = null; |
|
871 | - return true; |
|
872 | - } |
|
873 | - if (isset($this->LIB->{$class_name})) { |
|
874 | - unset($this->LIB->{$class_name}); |
|
875 | - return true; |
|
876 | - } |
|
877 | - if ($addon && isset($this->addons->{$class_name})) { |
|
878 | - unset($this->addons->{$class_name}); |
|
879 | - return true; |
|
880 | - } |
|
881 | - return false; |
|
882 | - } |
|
883 | - |
|
884 | - |
|
885 | - |
|
886 | - /** |
|
887 | - * attempts to find a full valid filepath for the requested class. |
|
888 | - * loops thru each of the base paths in the $file_paths array and appends : "{classname} . {file type} . php" |
|
889 | - * then returns that path if the target file has been found and is readable |
|
890 | - * |
|
891 | - * @param string $class_name |
|
892 | - * @param string $type |
|
893 | - * @param array $file_paths |
|
894 | - * @return string | bool |
|
895 | - */ |
|
896 | - protected function _resolve_path($class_name, $type = '', $file_paths = array()) |
|
897 | - { |
|
898 | - // make sure $file_paths is an array |
|
899 | - $file_paths = is_array($file_paths) |
|
900 | - ? $file_paths |
|
901 | - : array($file_paths); |
|
902 | - // cycle thru paths |
|
903 | - foreach ($file_paths as $key => $file_path) { |
|
904 | - // convert all separators to proper DS, if no filepath, then use EE_CLASSES |
|
905 | - $file_path = $file_path |
|
906 | - ? str_replace(array('/', '\\'), DS, $file_path) |
|
907 | - : EE_CLASSES; |
|
908 | - // prep file type |
|
909 | - $type = ! empty($type) |
|
910 | - ? trim($type, '.') . '.' |
|
911 | - : ''; |
|
912 | - // build full file path |
|
913 | - $file_paths[$key] = rtrim($file_path, DS) . DS . $class_name . '.' . $type . 'php'; |
|
914 | - //does the file exist and can be read ? |
|
915 | - if (is_readable($file_paths[$key])) { |
|
916 | - return $file_paths[$key]; |
|
917 | - } |
|
918 | - } |
|
919 | - return false; |
|
920 | - } |
|
921 | - |
|
922 | - |
|
923 | - |
|
924 | - /** |
|
925 | - * basically just performs a require_once() |
|
926 | - * but with some error handling |
|
927 | - * |
|
928 | - * @param string $path |
|
929 | - * @param string $class_name |
|
930 | - * @param string $type |
|
931 | - * @param array $file_paths |
|
932 | - * @return bool |
|
933 | - * @throws EE_Error |
|
934 | - * @throws ReflectionException |
|
935 | - */ |
|
936 | - protected function _require_file($path, $class_name, $type = '', $file_paths = array()) |
|
937 | - { |
|
938 | - // don't give up! you gotta... |
|
939 | - try { |
|
940 | - //does the file exist and can it be read ? |
|
941 | - if (! $path) { |
|
942 | - // so sorry, can't find the file |
|
943 | - throw new EE_Error ( |
|
944 | - sprintf( |
|
945 | - esc_html__( |
|
946 | - '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', |
|
947 | - 'event_espresso' |
|
948 | - ), |
|
949 | - trim($type, '.'), |
|
950 | - $class_name, |
|
951 | - '<br />' . implode(',<br />', $file_paths) |
|
952 | - ) |
|
953 | - ); |
|
954 | - } |
|
955 | - // get the file |
|
956 | - require_once($path); |
|
957 | - // if the class isn't already declared somewhere |
|
958 | - if (class_exists($class_name, false) === false) { |
|
959 | - // so sorry, not a class |
|
960 | - throw new EE_Error( |
|
961 | - sprintf( |
|
962 | - esc_html__('The %s file %s does not appear to contain the %s Class.', 'event_espresso'), |
|
963 | - $type, |
|
964 | - $path, |
|
965 | - $class_name |
|
966 | - ) |
|
967 | - ); |
|
968 | - } |
|
969 | - } catch (EE_Error $e) { |
|
970 | - $e->get_error(); |
|
971 | - return false; |
|
972 | - } |
|
973 | - return true; |
|
974 | - } |
|
975 | - |
|
976 | - |
|
977 | - |
|
978 | - /** |
|
979 | - * _create_object |
|
980 | - * Attempts to instantiate the requested class via any of the |
|
981 | - * commonly used instantiation methods employed throughout EE. |
|
982 | - * The priority for instantiation is as follows: |
|
983 | - * - abstract classes or any class flagged as "load only" (no instantiation occurs) |
|
984 | - * - model objects via their 'new_instance_from_db' method |
|
985 | - * - model objects via their 'new_instance' method |
|
986 | - * - "singleton" classes" via their 'instance' method |
|
987 | - * - standard instantiable classes via their __constructor |
|
988 | - * Prior to instantiation, if the classname exists in the dependency_map, |
|
989 | - * then the constructor for the requested class will be examined to determine |
|
990 | - * if any dependencies exist, and if they can be injected. |
|
991 | - * If so, then those classes will be added to the array of arguments passed to the constructor |
|
992 | - * |
|
993 | - * @param string $class_name |
|
994 | - * @param array $arguments |
|
995 | - * @param string $type |
|
996 | - * @param bool $from_db |
|
997 | - * @return null|object |
|
998 | - * @throws EE_Error |
|
999 | - * @throws ReflectionException |
|
1000 | - */ |
|
1001 | - protected function _create_object($class_name, $arguments = array(), $type = '', $from_db = false) |
|
1002 | - { |
|
1003 | - $class_obj = null; |
|
1004 | - $instantiation_mode = '0) none'; |
|
1005 | - // don't give up! you gotta... |
|
1006 | - try { |
|
1007 | - // create reflection |
|
1008 | - $reflector = $this->get_ReflectionClass($class_name); |
|
1009 | - // make sure arguments are an array |
|
1010 | - $arguments = is_array($arguments) |
|
1011 | - ? $arguments |
|
1012 | - : array($arguments); |
|
1013 | - // and if arguments array is numerically and sequentially indexed, then we want it to remain as is, |
|
1014 | - // else wrap it in an additional array so that it doesn't get split into multiple parameters |
|
1015 | - $arguments = $this->_array_is_numerically_and_sequentially_indexed($arguments) |
|
1016 | - ? $arguments |
|
1017 | - : array($arguments); |
|
1018 | - // attempt to inject dependencies ? |
|
1019 | - if ($this->_dependency_map->has($class_name)) { |
|
1020 | - $arguments = $this->_resolve_dependencies($reflector, $class_name, $arguments); |
|
1021 | - } |
|
1022 | - // instantiate the class if possible |
|
1023 | - if ($reflector->isAbstract()) { |
|
1024 | - // nothing to instantiate, loading file was enough |
|
1025 | - // does not throw an exception so $instantiation_mode is unused |
|
1026 | - // $instantiation_mode = "1) no constructor abstract class"; |
|
1027 | - $class_obj = true; |
|
1028 | - } else if (empty($arguments) && $reflector->getConstructor() === null && $reflector->isInstantiable()) { |
|
1029 | - // no constructor = static methods only... nothing to instantiate, loading file was enough |
|
1030 | - $instantiation_mode = '2) no constructor but instantiable'; |
|
1031 | - $class_obj = $reflector->newInstance(); |
|
1032 | - } else if ($from_db && method_exists($class_name, 'new_instance_from_db')) { |
|
1033 | - $instantiation_mode = '3) new_instance_from_db()'; |
|
1034 | - $class_obj = call_user_func_array(array($class_name, 'new_instance_from_db'), $arguments); |
|
1035 | - } else if (method_exists($class_name, 'new_instance')) { |
|
1036 | - $instantiation_mode = '4) new_instance()'; |
|
1037 | - $class_obj = call_user_func_array(array($class_name, 'new_instance'), $arguments); |
|
1038 | - } else if (method_exists($class_name, 'instance')) { |
|
1039 | - $instantiation_mode = '5) instance()'; |
|
1040 | - $class_obj = call_user_func_array(array($class_name, 'instance'), $arguments); |
|
1041 | - } else if ($reflector->isInstantiable()) { |
|
1042 | - $instantiation_mode = '6) constructor'; |
|
1043 | - $class_obj = $reflector->newInstanceArgs($arguments); |
|
1044 | - } else { |
|
1045 | - // heh ? something's not right ! |
|
1046 | - throw new EE_Error( |
|
1047 | - sprintf( |
|
1048 | - esc_html__('The %s file %s could not be instantiated.', 'event_espresso'), |
|
1049 | - $type, |
|
1050 | - $class_name |
|
1051 | - ) |
|
1052 | - ); |
|
1053 | - } |
|
1054 | - } catch (Exception $e) { |
|
1055 | - if (! $e instanceof EE_Error) { |
|
1056 | - $e = new EE_Error( |
|
1057 | - sprintf( |
|
1058 | - esc_html__( |
|
1059 | - 'The following error occurred while attempting to instantiate "%1$s": %2$s %3$s %2$s instantiation mode : %4$s', |
|
1060 | - 'event_espresso' |
|
1061 | - ), |
|
1062 | - $class_name, |
|
1063 | - '<br />', |
|
1064 | - $e->getMessage(), |
|
1065 | - $instantiation_mode |
|
1066 | - ) |
|
1067 | - ); |
|
1068 | - } |
|
1069 | - $e->get_error(); |
|
1070 | - } |
|
1071 | - return $class_obj; |
|
1072 | - } |
|
1073 | - |
|
1074 | - |
|
1075 | - |
|
1076 | - /** |
|
1077 | - * @see http://stackoverflow.com/questions/173400/how-to-check-if-php-array-is-associative-or-sequential |
|
1078 | - * @param array $array |
|
1079 | - * @return bool |
|
1080 | - */ |
|
1081 | - protected function _array_is_numerically_and_sequentially_indexed(array $array) |
|
1082 | - { |
|
1083 | - return ! empty($array) |
|
1084 | - ? array_keys($array) === range(0, count($array) - 1) |
|
1085 | - : true; |
|
1086 | - } |
|
1087 | - |
|
1088 | - |
|
1089 | - |
|
1090 | - /** |
|
1091 | - * getReflectionClass |
|
1092 | - * checks if a ReflectionClass object has already been generated for a class |
|
1093 | - * and returns that instead of creating a new one |
|
1094 | - * |
|
1095 | - * @param string $class_name |
|
1096 | - * @return ReflectionClass |
|
1097 | - * @throws ReflectionException |
|
1098 | - */ |
|
1099 | - public function get_ReflectionClass($class_name) |
|
1100 | - { |
|
1101 | - if ( |
|
1102 | - ! isset($this->_reflectors[$class_name]) |
|
1103 | - || ! $this->_reflectors[$class_name] instanceof ReflectionClass |
|
1104 | - ) { |
|
1105 | - $this->_reflectors[$class_name] = new ReflectionClass($class_name); |
|
1106 | - } |
|
1107 | - return $this->_reflectors[$class_name]; |
|
1108 | - } |
|
1109 | - |
|
1110 | - |
|
1111 | - |
|
1112 | - /** |
|
1113 | - * _resolve_dependencies |
|
1114 | - * examines the constructor for the requested class to determine |
|
1115 | - * if any dependencies exist, and if they can be injected. |
|
1116 | - * If so, then those classes will be added to the array of arguments passed to the constructor |
|
1117 | - * PLZ NOTE: this is achieved by type hinting the constructor params |
|
1118 | - * For example: |
|
1119 | - * if attempting to load a class "Foo" with the following constructor: |
|
1120 | - * __construct( Bar $bar_class, Fighter $grohl_class ) |
|
1121 | - * then $bar_class and $grohl_class will be added to the $arguments array, |
|
1122 | - * but only IF they are NOT already present in the incoming arguments array, |
|
1123 | - * and the correct classes can be loaded |
|
1124 | - * |
|
1125 | - * @param ReflectionClass $reflector |
|
1126 | - * @param string $class_name |
|
1127 | - * @param array $arguments |
|
1128 | - * @return array |
|
1129 | - * @throws EE_Error |
|
1130 | - * @throws ReflectionException |
|
1131 | - */ |
|
1132 | - protected function _resolve_dependencies(ReflectionClass $reflector, $class_name, $arguments = array()) |
|
1133 | - { |
|
1134 | - // let's examine the constructor |
|
1135 | - $constructor = $reflector->getConstructor(); |
|
1136 | - // whu? huh? nothing? |
|
1137 | - if (! $constructor) { |
|
1138 | - return $arguments; |
|
1139 | - } |
|
1140 | - // get constructor parameters |
|
1141 | - $params = $constructor->getParameters(); |
|
1142 | - // and the keys for the incoming arguments array so that we can compare existing arguments with what is expected |
|
1143 | - $argument_keys = array_keys($arguments); |
|
1144 | - // now loop thru all of the constructors expected parameters |
|
1145 | - foreach ($params as $index => $param) { |
|
1146 | - // is this a dependency for a specific class ? |
|
1147 | - $param_class = $param->getClass() |
|
1148 | - ? $param->getClass()->name |
|
1149 | - : null; |
|
1150 | - // BUT WAIT !!! This class may be an alias for something else (or getting replaced at runtime) |
|
1151 | - $param_class = $this->_dependency_map->has_alias($param_class, $class_name) |
|
1152 | - ? $this->_dependency_map->get_alias($param_class, $class_name) |
|
1153 | - : $param_class; |
|
1154 | - if ( |
|
1155 | - // param is not even a class |
|
1156 | - $param_class === null |
|
1157 | - // and something already exists in the incoming arguments for this param |
|
1158 | - && isset($argument_keys[$index], $arguments[$argument_keys[$index]]) |
|
1159 | - ) { |
|
1160 | - // so let's skip this argument and move on to the next |
|
1161 | - continue; |
|
1162 | - } |
|
1163 | - if ( |
|
1164 | - // parameter is type hinted as a class, exists as an incoming argument, AND it's the correct class |
|
1165 | - $param_class !== null |
|
1166 | - && isset($argument_keys[$index], $arguments[$argument_keys[$index]]) |
|
1167 | - && $arguments[$argument_keys[$index]] instanceof $param_class |
|
1168 | - ) { |
|
1169 | - // skip this argument and move on to the next |
|
1170 | - continue; |
|
1171 | - } |
|
1172 | - if ( |
|
1173 | - // parameter is type hinted as a class, and should be injected |
|
1174 | - $param_class !== null |
|
1175 | - && $this->_dependency_map->has_dependency_for_class($class_name, $param_class) |
|
1176 | - ) { |
|
1177 | - $arguments = $this->_resolve_dependency( |
|
1178 | - $class_name, |
|
1179 | - $param_class, |
|
1180 | - $arguments, |
|
1181 | - $index, |
|
1182 | - $argument_keys |
|
1183 | - ); |
|
1184 | - } else { |
|
1185 | - try { |
|
1186 | - $arguments[$index] = $param->isDefaultValueAvailable() |
|
1187 | - ? $param->getDefaultValue() |
|
1188 | - : null; |
|
1189 | - } catch (ReflectionException $e) { |
|
1190 | - throw new ReflectionException( |
|
1191 | - sprintf( |
|
1192 | - esc_html__('%1$s for parameter "$%2$s"', 'event_espresso'), |
|
1193 | - $e->getMessage(), |
|
1194 | - $param->getName() |
|
1195 | - ) |
|
1196 | - ); |
|
1197 | - } |
|
1198 | - } |
|
1199 | - } |
|
1200 | - return $arguments; |
|
1201 | - } |
|
1202 | - |
|
1203 | - |
|
1204 | - |
|
1205 | - /** |
|
1206 | - * @param string $class_name |
|
1207 | - * @param string $param_class |
|
1208 | - * @param array $arguments |
|
1209 | - * @param mixed $index |
|
1210 | - * @param array $argument_keys |
|
1211 | - * @return array |
|
1212 | - * @throws EE_Error |
|
1213 | - * @throws ReflectionException |
|
1214 | - * @throws InvalidArgumentException |
|
1215 | - * @throws InvalidInterfaceException |
|
1216 | - * @throws InvalidDataTypeException |
|
1217 | - */ |
|
1218 | - protected function _resolve_dependency($class_name, $param_class, $arguments, $index, array $argument_keys) |
|
1219 | - { |
|
1220 | - $dependency = null; |
|
1221 | - // should dependency be loaded from cache ? |
|
1222 | - $cache_on = $this->_dependency_map->loading_strategy_for_class_dependency( |
|
1223 | - $class_name, |
|
1224 | - $param_class |
|
1225 | - ); |
|
1226 | - $cache_on = $cache_on !== EE_Dependency_Map::load_new_object; |
|
1227 | - // we might have a dependency... |
|
1228 | - // let's MAYBE try and find it in our cache if that's what's been requested |
|
1229 | - $cached_class = $cache_on |
|
1230 | - ? $this->_get_cached_class($param_class) |
|
1231 | - : null; |
|
1232 | - // and grab it if it exists |
|
1233 | - if ($cached_class instanceof $param_class) { |
|
1234 | - $dependency = $cached_class; |
|
1235 | - } else if ($param_class !== $class_name) { |
|
1236 | - // obtain the loader method from the dependency map |
|
1237 | - $loader = $this->_dependency_map->class_loader($param_class); |
|
1238 | - // is loader a custom closure ? |
|
1239 | - if ($loader instanceof Closure) { |
|
1240 | - $dependency = $loader($arguments); |
|
1241 | - } else { |
|
1242 | - // set the cache on property for the recursive loading call |
|
1243 | - $this->_cache_on = $cache_on; |
|
1244 | - // if not, then let's try and load it via the registry |
|
1245 | - if ($loader && method_exists($this, $loader)) { |
|
1246 | - $dependency = $this->{$loader}($param_class); |
|
1247 | - } else { |
|
1248 | - $dependency = LoaderFactory::getLoader()->load( |
|
1249 | - $param_class, |
|
1250 | - array(), |
|
1251 | - $cache_on |
|
1252 | - ); |
|
1253 | - } |
|
1254 | - } |
|
1255 | - } |
|
1256 | - // did we successfully find the correct dependency ? |
|
1257 | - if ($dependency instanceof $param_class) { |
|
1258 | - // then let's inject it into the incoming array of arguments at the correct location |
|
1259 | - $arguments[$index] = $dependency; |
|
1260 | - } |
|
1261 | - return $arguments; |
|
1262 | - } |
|
1263 | - |
|
1264 | - |
|
1265 | - |
|
1266 | - /** |
|
1267 | - * _set_cached_class |
|
1268 | - * attempts to cache the instantiated class locally |
|
1269 | - * in one of the following places, in the following order: |
|
1270 | - * $this->{class_abbreviation} ie: $this->CART |
|
1271 | - * $this->{$class_name} ie: $this->Some_Class |
|
1272 | - * $this->addon->{$$class_name} ie: $this->addon->Some_Addon_Class |
|
1273 | - * $this->LIB->{$class_name} ie: $this->LIB->Some_Class |
|
1274 | - * |
|
1275 | - * @param object $class_obj |
|
1276 | - * @param string $class_name |
|
1277 | - * @param string $class_prefix |
|
1278 | - * @param bool $from_db |
|
1279 | - * @return void |
|
1280 | - */ |
|
1281 | - protected function _set_cached_class($class_obj, $class_name, $class_prefix = '', $from_db = false) |
|
1282 | - { |
|
1283 | - if ($class_name === 'EE_Registry' || empty($class_obj)) { |
|
1284 | - return; |
|
1285 | - } |
|
1286 | - // return newly instantiated class |
|
1287 | - $class_abbreviation = $this->get_class_abbreviation($class_name, ''); |
|
1288 | - if ($class_abbreviation) { |
|
1289 | - $this->{$class_abbreviation} = $class_obj; |
|
1290 | - return; |
|
1291 | - } |
|
1292 | - $class_name = str_replace('\\', '_', $class_name); |
|
1293 | - if (property_exists($this, $class_name)) { |
|
1294 | - $this->{$class_name} = $class_obj; |
|
1295 | - return; |
|
1296 | - } |
|
1297 | - if ($class_prefix === 'addon') { |
|
1298 | - $this->addons->{$class_name} = $class_obj; |
|
1299 | - return; |
|
1300 | - } |
|
1301 | - if (! $from_db) { |
|
1302 | - $this->LIB->{$class_name} = $class_obj; |
|
1303 | - } |
|
1304 | - } |
|
1305 | - |
|
1306 | - |
|
1307 | - |
|
1308 | - /** |
|
1309 | - * call any loader that's been registered in the EE_Dependency_Map::$_class_loaders array |
|
1310 | - * |
|
1311 | - * @param string $classname PLEASE NOTE: the class name needs to match what's registered |
|
1312 | - * in the EE_Dependency_Map::$_class_loaders array, |
|
1313 | - * including the class prefix, ie: "EE_", "EEM_", "EEH_", etc |
|
1314 | - * @param array $arguments |
|
1315 | - * @return object |
|
1316 | - */ |
|
1317 | - public static function factory($classname, $arguments = array()) |
|
1318 | - { |
|
1319 | - $loader = self::instance()->_dependency_map->class_loader($classname); |
|
1320 | - if ($loader instanceof Closure) { |
|
1321 | - return $loader($arguments); |
|
1322 | - } |
|
1323 | - if (method_exists(self::instance(), $loader)) { |
|
1324 | - return self::instance()->{$loader}($classname, $arguments); |
|
1325 | - } |
|
1326 | - return null; |
|
1327 | - } |
|
1328 | - |
|
1329 | - |
|
1330 | - |
|
1331 | - /** |
|
1332 | - * Gets the addon by its name/slug (not classname. For that, just |
|
1333 | - * use the classname as the property name on EE_Config::instance()->addons) |
|
1334 | - * |
|
1335 | - * @param string $name |
|
1336 | - * @return EE_Addon |
|
1337 | - */ |
|
1338 | - public function get_addon_by_name($name) |
|
1339 | - { |
|
1340 | - foreach ($this->addons as $addon) { |
|
1341 | - if ($addon->name() === $name) { |
|
1342 | - return $addon; |
|
1343 | - } |
|
1344 | - } |
|
1345 | - return null; |
|
1346 | - } |
|
1347 | - |
|
1348 | - |
|
1349 | - |
|
1350 | - /** |
|
1351 | - * Gets an array of all the registered addons, where the keys are their names. (ie, what each returns for their |
|
1352 | - * name() function) They're already available on EE_Config::instance()->addons as properties, where each property's |
|
1353 | - * name is the addon's classname. So if you just want to get the addon by classname, use |
|
1354 | - * EE_Config::instance()->addons->{classname} |
|
1355 | - * |
|
1356 | - * @return EE_Addon[] where the KEYS are the addon's name() |
|
1357 | - */ |
|
1358 | - public function get_addons_by_name() |
|
1359 | - { |
|
1360 | - $addons = array(); |
|
1361 | - foreach ($this->addons as $addon) { |
|
1362 | - $addons[$addon->name()] = $addon; |
|
1363 | - } |
|
1364 | - return $addons; |
|
1365 | - } |
|
1366 | - |
|
1367 | - |
|
1368 | - |
|
1369 | - /** |
|
1370 | - * Resets the specified model's instance AND makes sure EE_Registry doesn't keep |
|
1371 | - * a stale copy of it around |
|
1372 | - * |
|
1373 | - * @param string $model_name |
|
1374 | - * @return \EEM_Base |
|
1375 | - * @throws \EE_Error |
|
1376 | - */ |
|
1377 | - public function reset_model($model_name) |
|
1378 | - { |
|
1379 | - $model_class_name = strpos($model_name, 'EEM_') !== 0 |
|
1380 | - ? "EEM_{$model_name}" |
|
1381 | - : $model_name; |
|
1382 | - if (! isset($this->LIB->{$model_class_name}) || ! $this->LIB->{$model_class_name} instanceof EEM_Base) { |
|
1383 | - return null; |
|
1384 | - } |
|
1385 | - //get that model reset it and make sure we nuke the old reference to it |
|
1386 | - if ($this->LIB->{$model_class_name} instanceof $model_class_name |
|
1387 | - && is_callable( |
|
1388 | - array($model_class_name, 'reset') |
|
1389 | - )) { |
|
1390 | - $this->LIB->{$model_class_name} = $this->LIB->{$model_class_name}->reset(); |
|
1391 | - } else { |
|
1392 | - throw new EE_Error(sprintf(esc_html__('Model %s does not have a method "reset"', 'event_espresso'), $model_name)); |
|
1393 | - } |
|
1394 | - return $this->LIB->{$model_class_name}; |
|
1395 | - } |
|
1396 | - |
|
1397 | - |
|
1398 | - |
|
1399 | - /** |
|
1400 | - * Resets the registry. |
|
1401 | - * The criteria for what gets reset is based on what can be shared between sites on the same request when |
|
1402 | - * switch_to_blog is used in a multisite install. Here is a list of things that are NOT reset. |
|
1403 | - * - $_dependency_map |
|
1404 | - * - $_class_abbreviations |
|
1405 | - * - $NET_CFG (EE_Network_Config): The config is shared network wide so no need to reset. |
|
1406 | - * - $REQ: Still on the same request so no need to change. |
|
1407 | - * - $CAP: There is no site specific state in the EE_Capability class. |
|
1408 | - * - $SSN: Although ideally, the session should not be shared between site switches, we can't reset it because only |
|
1409 | - * one Session can be active in a single request. Resetting could resolve in "headers already sent" errors. |
|
1410 | - * - $addons: In multisite, the state of the addons is something controlled via hooks etc in a normal request. So |
|
1411 | - * for now, we won't reset the addons because it could break calls to an add-ons class/methods in the |
|
1412 | - * switch or on the restore. |
|
1413 | - * - $modules |
|
1414 | - * - $shortcodes |
|
1415 | - * - $widgets |
|
1416 | - * |
|
1417 | - * @param boolean $hard [deprecated] |
|
1418 | - * @param boolean $reinstantiate whether to create new instances of EE_Registry's singletons too, |
|
1419 | - * or just reset without re-instantiating (handy to set to FALSE if you're not |
|
1420 | - * sure if you CAN currently reinstantiate the singletons at the moment) |
|
1421 | - * @param bool $reset_models Defaults to true. When false, then the models are not reset. This is so |
|
1422 | - * client |
|
1423 | - * code instead can just change the model context to a different blog id if |
|
1424 | - * necessary |
|
1425 | - * @return EE_Registry |
|
1426 | - * @throws EE_Error |
|
1427 | - * @throws ReflectionException |
|
1428 | - */ |
|
1429 | - public static function reset($hard = false, $reinstantiate = true, $reset_models = true) |
|
1430 | - { |
|
1431 | - $instance = self::instance(); |
|
1432 | - $instance->_cache_on = true; |
|
1433 | - // reset some "special" classes |
|
1434 | - EEH_Activation::reset(); |
|
1435 | - $hard = apply_filters( 'FHEE__EE_Registry__reset__hard', $hard); |
|
1436 | - $instance->CFG = EE_Config::reset($hard, $reinstantiate); |
|
1437 | - $instance->CART = null; |
|
1438 | - $instance->MRM = null; |
|
1439 | - $instance->AssetsRegistry = $instance->create('EventEspresso\core\services\assets\Registry'); |
|
1440 | - //messages reset |
|
1441 | - EED_Messages::reset(); |
|
1442 | - //handle of objects cached on LIB |
|
1443 | - foreach (array('LIB', 'modules', 'shortcodes') as $cache) { |
|
1444 | - foreach ($instance->{$cache} as $class_name => $class) { |
|
1445 | - if (self::_reset_and_unset_object($class, $reset_models)) { |
|
1446 | - unset($instance->{$cache}->{$class_name}); |
|
1447 | - } |
|
1448 | - } |
|
1449 | - } |
|
1450 | - return $instance; |
|
1451 | - } |
|
1452 | - |
|
1453 | - |
|
1454 | - |
|
1455 | - /** |
|
1456 | - * if passed object implements ResettableInterface, then call it's reset() method |
|
1457 | - * if passed object implements InterminableInterface, then return false, |
|
1458 | - * to indicate that it should NOT be cleared from the Registry cache |
|
1459 | - * |
|
1460 | - * @param $object |
|
1461 | - * @param bool $reset_models |
|
1462 | - * @return bool returns true if cached object should be unset |
|
1463 | - */ |
|
1464 | - private static function _reset_and_unset_object($object, $reset_models) |
|
1465 | - { |
|
1466 | - if ($object instanceof ResettableInterface) { |
|
1467 | - if ($object instanceof EEM_Base) { |
|
1468 | - if ($reset_models) { |
|
1469 | - $object->reset(); |
|
1470 | - return true; |
|
1471 | - } |
|
1472 | - return false; |
|
1473 | - } |
|
1474 | - $object->reset(); |
|
1475 | - return true; |
|
1476 | - } |
|
1477 | - if (! $object instanceof InterminableInterface) { |
|
1478 | - return true; |
|
1479 | - } |
|
1480 | - return false; |
|
1481 | - } |
|
1482 | - |
|
1483 | - |
|
1484 | - |
|
1485 | - /** |
|
1486 | - * Gets all the custom post type models defined |
|
1487 | - * |
|
1488 | - * @return array keys are model "short names" (Eg "Event") and keys are classnames (eg "EEM_Event") |
|
1489 | - */ |
|
1490 | - public function cpt_models() |
|
1491 | - { |
|
1492 | - $cpt_models = array(); |
|
1493 | - foreach ($this->non_abstract_db_models as $short_name => $classname) { |
|
1494 | - if (is_subclass_of($classname, 'EEM_CPT_Base')) { |
|
1495 | - $cpt_models[$short_name] = $classname; |
|
1496 | - } |
|
1497 | - } |
|
1498 | - return $cpt_models; |
|
1499 | - } |
|
1500 | - |
|
1501 | - |
|
1502 | - |
|
1503 | - /** |
|
1504 | - * @return \EE_Config |
|
1505 | - */ |
|
1506 | - public static function CFG() |
|
1507 | - { |
|
1508 | - return self::instance()->CFG; |
|
1509 | - } |
|
25 | + /** |
|
26 | + * @var EE_Registry $_instance |
|
27 | + */ |
|
28 | + private static $_instance; |
|
29 | + |
|
30 | + /** |
|
31 | + * @var EE_Dependency_Map $_dependency_map |
|
32 | + */ |
|
33 | + protected $_dependency_map; |
|
34 | + |
|
35 | + /** |
|
36 | + * @var array $_class_abbreviations |
|
37 | + */ |
|
38 | + protected $_class_abbreviations = array(); |
|
39 | + |
|
40 | + /** |
|
41 | + * @var CommandBusInterface $BUS |
|
42 | + */ |
|
43 | + public $BUS; |
|
44 | + |
|
45 | + /** |
|
46 | + * @var EE_Cart $CART |
|
47 | + */ |
|
48 | + public $CART; |
|
49 | + |
|
50 | + /** |
|
51 | + * @var EE_Config $CFG |
|
52 | + */ |
|
53 | + public $CFG; |
|
54 | + |
|
55 | + /** |
|
56 | + * @var EE_Network_Config $NET_CFG |
|
57 | + */ |
|
58 | + public $NET_CFG; |
|
59 | + |
|
60 | + /** |
|
61 | + * StdClass object for storing library classes in |
|
62 | + * |
|
63 | + * @var StdClass $LIB |
|
64 | + */ |
|
65 | + public $LIB; |
|
66 | + |
|
67 | + /** |
|
68 | + * @var EE_Request_Handler $REQ |
|
69 | + */ |
|
70 | + public $REQ; |
|
71 | + |
|
72 | + /** |
|
73 | + * @var EE_Session $SSN |
|
74 | + */ |
|
75 | + public $SSN; |
|
76 | + |
|
77 | + /** |
|
78 | + * @since 4.5.0 |
|
79 | + * @var EE_Capabilities $CAP |
|
80 | + */ |
|
81 | + public $CAP; |
|
82 | + |
|
83 | + /** |
|
84 | + * @since 4.9.0 |
|
85 | + * @var EE_Message_Resource_Manager $MRM |
|
86 | + */ |
|
87 | + public $MRM; |
|
88 | + |
|
89 | + |
|
90 | + /** |
|
91 | + * @var Registry $AssetsRegistry |
|
92 | + */ |
|
93 | + public $AssetsRegistry; |
|
94 | + |
|
95 | + /** |
|
96 | + * StdClass object for holding addons which have registered themselves to work with EE core |
|
97 | + * |
|
98 | + * @var EE_Addon[] $addons |
|
99 | + */ |
|
100 | + public $addons; |
|
101 | + |
|
102 | + /** |
|
103 | + * keys are 'short names' (eg Event), values are class names (eg 'EEM_Event') |
|
104 | + * |
|
105 | + * @var EEM_Base[] $models |
|
106 | + */ |
|
107 | + public $models = array(); |
|
108 | + |
|
109 | + /** |
|
110 | + * @var EED_Module[] $modules |
|
111 | + */ |
|
112 | + public $modules; |
|
113 | + |
|
114 | + /** |
|
115 | + * @var EES_Shortcode[] $shortcodes |
|
116 | + */ |
|
117 | + public $shortcodes; |
|
118 | + |
|
119 | + /** |
|
120 | + * @var WP_Widget[] $widgets |
|
121 | + */ |
|
122 | + public $widgets; |
|
123 | + |
|
124 | + /** |
|
125 | + * this is an array of all implemented model names (i.e. not the parent abstract models, or models |
|
126 | + * which don't actually fetch items from the DB in the normal way (ie, are not children of EEM_Base)). |
|
127 | + * Keys are model "short names" (eg "Event") as used in model relations, and values are |
|
128 | + * classnames (eg "EEM_Event") |
|
129 | + * |
|
130 | + * @var array $non_abstract_db_models |
|
131 | + */ |
|
132 | + public $non_abstract_db_models = array(); |
|
133 | + |
|
134 | + |
|
135 | + /** |
|
136 | + * internationalization for JS strings |
|
137 | + * usage: EE_Registry::i18n_js_strings['string_key'] = esc_html__( 'string to translate.', 'event_espresso' ); |
|
138 | + * in js file: var translatedString = eei18n.string_key; |
|
139 | + * |
|
140 | + * @var array $i18n_js_strings |
|
141 | + */ |
|
142 | + public static $i18n_js_strings = array(); |
|
143 | + |
|
144 | + |
|
145 | + /** |
|
146 | + * $main_file - path to espresso.php |
|
147 | + * |
|
148 | + * @var array $main_file |
|
149 | + */ |
|
150 | + public $main_file; |
|
151 | + |
|
152 | + /** |
|
153 | + * array of ReflectionClass objects where the key is the class name |
|
154 | + * |
|
155 | + * @var ReflectionClass[] $_reflectors |
|
156 | + */ |
|
157 | + public $_reflectors; |
|
158 | + |
|
159 | + /** |
|
160 | + * boolean flag to indicate whether or not to load/save dependencies from/to the cache |
|
161 | + * |
|
162 | + * @var boolean $_cache_on |
|
163 | + */ |
|
164 | + protected $_cache_on = true; |
|
165 | + |
|
166 | + |
|
167 | + |
|
168 | + /** |
|
169 | + * @singleton method used to instantiate class object |
|
170 | + * @param EE_Dependency_Map $dependency_map |
|
171 | + * @return EE_Registry instance |
|
172 | + * @throws InvalidArgumentException |
|
173 | + * @throws InvalidInterfaceException |
|
174 | + * @throws InvalidDataTypeException |
|
175 | + */ |
|
176 | + public static function instance(EE_Dependency_Map $dependency_map = null) |
|
177 | + { |
|
178 | + // check if class object is instantiated |
|
179 | + if (! self::$_instance instanceof EE_Registry) { |
|
180 | + self::$_instance = new self($dependency_map); |
|
181 | + } |
|
182 | + return self::$_instance; |
|
183 | + } |
|
184 | + |
|
185 | + |
|
186 | + |
|
187 | + /** |
|
188 | + * protected constructor to prevent direct creation |
|
189 | + * |
|
190 | + * @Constructor |
|
191 | + * @param EE_Dependency_Map $dependency_map |
|
192 | + * @throws InvalidDataTypeException |
|
193 | + * @throws InvalidInterfaceException |
|
194 | + * @throws InvalidArgumentException |
|
195 | + */ |
|
196 | + protected function __construct(EE_Dependency_Map $dependency_map) |
|
197 | + { |
|
198 | + $this->_dependency_map = $dependency_map; |
|
199 | + $this->LIB = new stdClass(); |
|
200 | + $this->addons = new stdClass(); |
|
201 | + $this->modules = new stdClass(); |
|
202 | + $this->shortcodes = new stdClass(); |
|
203 | + $this->widgets = new stdClass(); |
|
204 | + add_action('EE_Load_Espresso_Core__handle_request__initialize_core_loading', array($this, 'initialize')); |
|
205 | + } |
|
206 | + |
|
207 | + |
|
208 | + |
|
209 | + /** |
|
210 | + * initialize |
|
211 | + * |
|
212 | + * @throws EE_Error |
|
213 | + * @throws ReflectionException |
|
214 | + */ |
|
215 | + public function initialize() |
|
216 | + { |
|
217 | + $this->_class_abbreviations = apply_filters( |
|
218 | + 'FHEE__EE_Registry____construct___class_abbreviations', |
|
219 | + array( |
|
220 | + 'EE_Config' => 'CFG', |
|
221 | + 'EE_Session' => 'SSN', |
|
222 | + 'EE_Capabilities' => 'CAP', |
|
223 | + 'EE_Cart' => 'CART', |
|
224 | + 'EE_Network_Config' => 'NET_CFG', |
|
225 | + 'EE_Request_Handler' => 'REQ', |
|
226 | + 'EE_Message_Resource_Manager' => 'MRM', |
|
227 | + 'EventEspresso\core\services\commands\CommandBus' => 'BUS', |
|
228 | + 'EventEspresso\core\services\assets\Registry' => 'AssetsRegistry', |
|
229 | + ) |
|
230 | + ); |
|
231 | + $this->load_core('Base', array(), true); |
|
232 | + // add our request and response objects to the cache |
|
233 | + $request_loader = $this->_dependency_map->class_loader('EE_Request'); |
|
234 | + $this->_set_cached_class( |
|
235 | + $request_loader(), |
|
236 | + 'EE_Request' |
|
237 | + ); |
|
238 | + $response_loader = $this->_dependency_map->class_loader('EE_Response'); |
|
239 | + $this->_set_cached_class( |
|
240 | + $response_loader(), |
|
241 | + 'EE_Response' |
|
242 | + ); |
|
243 | + add_action('AHEE__EE_System__set_hooks_for_core', array($this, 'init')); |
|
244 | + } |
|
245 | + |
|
246 | + |
|
247 | + |
|
248 | + /** |
|
249 | + * @return void |
|
250 | + */ |
|
251 | + public function init() |
|
252 | + { |
|
253 | + // Get current page protocol |
|
254 | + $protocol = isset($_SERVER['HTTPS']) ? 'https://' : 'http://'; |
|
255 | + // Output admin-ajax.php URL with same protocol as current page |
|
256 | + self::$i18n_js_strings['ajax_url'] = admin_url('admin-ajax.php', $protocol); |
|
257 | + self::$i18n_js_strings['wp_debug'] = defined('WP_DEBUG') ? WP_DEBUG : false; |
|
258 | + } |
|
259 | + |
|
260 | + |
|
261 | + |
|
262 | + /** |
|
263 | + * localize_i18n_js_strings |
|
264 | + * |
|
265 | + * @return string |
|
266 | + */ |
|
267 | + public static function localize_i18n_js_strings() |
|
268 | + { |
|
269 | + $i18n_js_strings = (array)self::$i18n_js_strings; |
|
270 | + foreach ($i18n_js_strings as $key => $value) { |
|
271 | + if (is_scalar($value)) { |
|
272 | + $i18n_js_strings[$key] = html_entity_decode((string)$value, ENT_QUOTES, 'UTF-8'); |
|
273 | + } |
|
274 | + } |
|
275 | + return '/* <![CDATA[ */ var eei18n = ' . wp_json_encode($i18n_js_strings) . '; /* ]]> */'; |
|
276 | + } |
|
277 | + |
|
278 | + |
|
279 | + |
|
280 | + /** |
|
281 | + * @param mixed string | EED_Module $module |
|
282 | + * @throws EE_Error |
|
283 | + * @throws ReflectionException |
|
284 | + */ |
|
285 | + public function add_module($module) |
|
286 | + { |
|
287 | + if ($module instanceof EED_Module) { |
|
288 | + $module_class = get_class($module); |
|
289 | + $this->modules->{$module_class} = $module; |
|
290 | + } else { |
|
291 | + if (! class_exists('EE_Module_Request_Router')) { |
|
292 | + $this->load_core('Module_Request_Router'); |
|
293 | + } |
|
294 | + $this->modules->{$module} = EE_Module_Request_Router::module_factory($module); |
|
295 | + } |
|
296 | + } |
|
297 | + |
|
298 | + |
|
299 | + |
|
300 | + /** |
|
301 | + * @param string $module_name |
|
302 | + * @return mixed EED_Module | NULL |
|
303 | + */ |
|
304 | + public function get_module($module_name = '') |
|
305 | + { |
|
306 | + return isset($this->modules->{$module_name}) |
|
307 | + ? $this->modules->{$module_name} |
|
308 | + : null; |
|
309 | + } |
|
310 | + |
|
311 | + |
|
312 | + |
|
313 | + /** |
|
314 | + * loads core classes - must be singletons |
|
315 | + * |
|
316 | + * @param string $class_name - simple class name ie: session |
|
317 | + * @param mixed $arguments |
|
318 | + * @param bool $load_only |
|
319 | + * @return mixed |
|
320 | + * @throws EE_Error |
|
321 | + * @throws ReflectionException |
|
322 | + */ |
|
323 | + public function load_core($class_name, $arguments = array(), $load_only = false) |
|
324 | + { |
|
325 | + $core_paths = apply_filters( |
|
326 | + 'FHEE__EE_Registry__load_core__core_paths', |
|
327 | + array( |
|
328 | + EE_CORE, |
|
329 | + EE_ADMIN, |
|
330 | + EE_CPTS, |
|
331 | + EE_CORE . 'data_migration_scripts' . DS, |
|
332 | + EE_CORE . 'request_stack' . DS, |
|
333 | + EE_CORE . 'middleware' . DS, |
|
334 | + ) |
|
335 | + ); |
|
336 | + // retrieve instantiated class |
|
337 | + return $this->_load( |
|
338 | + $core_paths, |
|
339 | + 'EE_', |
|
340 | + $class_name, |
|
341 | + 'core', |
|
342 | + $arguments, |
|
343 | + false, |
|
344 | + true, |
|
345 | + $load_only |
|
346 | + ); |
|
347 | + } |
|
348 | + |
|
349 | + |
|
350 | + |
|
351 | + /** |
|
352 | + * loads service classes |
|
353 | + * |
|
354 | + * @param string $class_name - simple class name ie: session |
|
355 | + * @param mixed $arguments |
|
356 | + * @param bool $load_only |
|
357 | + * @return mixed |
|
358 | + * @throws EE_Error |
|
359 | + * @throws ReflectionException |
|
360 | + */ |
|
361 | + public function load_service($class_name, $arguments = array(), $load_only = false) |
|
362 | + { |
|
363 | + $service_paths = apply_filters( |
|
364 | + 'FHEE__EE_Registry__load_service__service_paths', |
|
365 | + array( |
|
366 | + EE_CORE . 'services' . DS, |
|
367 | + ) |
|
368 | + ); |
|
369 | + // retrieve instantiated class |
|
370 | + return $this->_load( |
|
371 | + $service_paths, |
|
372 | + 'EE_', |
|
373 | + $class_name, |
|
374 | + 'class', |
|
375 | + $arguments, |
|
376 | + false, |
|
377 | + true, |
|
378 | + $load_only |
|
379 | + ); |
|
380 | + } |
|
381 | + |
|
382 | + |
|
383 | + |
|
384 | + /** |
|
385 | + * loads data_migration_scripts |
|
386 | + * |
|
387 | + * @param string $class_name - class name for the DMS ie: EE_DMS_Core_4_2_0 |
|
388 | + * @param mixed $arguments |
|
389 | + * @return EE_Data_Migration_Script_Base|mixed |
|
390 | + * @throws EE_Error |
|
391 | + * @throws ReflectionException |
|
392 | + */ |
|
393 | + public function load_dms($class_name, $arguments = array()) |
|
394 | + { |
|
395 | + // retrieve instantiated class |
|
396 | + return $this->_load( |
|
397 | + EE_Data_Migration_Manager::instance()->get_data_migration_script_folders(), |
|
398 | + 'EE_DMS_', |
|
399 | + $class_name, |
|
400 | + 'dms', |
|
401 | + $arguments, |
|
402 | + false, |
|
403 | + false |
|
404 | + ); |
|
405 | + } |
|
406 | + |
|
407 | + |
|
408 | + |
|
409 | + /** |
|
410 | + * loads object creating classes - must be singletons |
|
411 | + * |
|
412 | + * @param string $class_name - simple class name ie: attendee |
|
413 | + * @param mixed $arguments - an array of arguments to pass to the class |
|
414 | + * @param bool $from_db - some classes are instantiated from the db and thus call a different method to |
|
415 | + * instantiate |
|
416 | + * @param bool $cache if you don't want the class to be stored in the internal cache (non-persistent) then |
|
417 | + * set this to FALSE (ie. when instantiating model objects from client in a loop) |
|
418 | + * @param bool $load_only whether or not to just load the file and NOT instantiate, or load AND instantiate |
|
419 | + * (default) |
|
420 | + * @return EE_Base_Class | bool |
|
421 | + * @throws EE_Error |
|
422 | + * @throws ReflectionException |
|
423 | + */ |
|
424 | + public function load_class($class_name, $arguments = array(), $from_db = false, $cache = true, $load_only = false) |
|
425 | + { |
|
426 | + $paths = apply_filters( |
|
427 | + 'FHEE__EE_Registry__load_class__paths', array( |
|
428 | + EE_CORE, |
|
429 | + EE_CLASSES, |
|
430 | + EE_BUSINESS, |
|
431 | + ) |
|
432 | + ); |
|
433 | + // retrieve instantiated class |
|
434 | + return $this->_load( |
|
435 | + $paths, |
|
436 | + 'EE_', |
|
437 | + $class_name, |
|
438 | + 'class', |
|
439 | + $arguments, |
|
440 | + $from_db, |
|
441 | + $cache, |
|
442 | + $load_only |
|
443 | + ); |
|
444 | + } |
|
445 | + |
|
446 | + |
|
447 | + |
|
448 | + /** |
|
449 | + * loads helper classes - must be singletons |
|
450 | + * |
|
451 | + * @param string $class_name - simple class name ie: price |
|
452 | + * @param mixed $arguments |
|
453 | + * @param bool $load_only |
|
454 | + * @return EEH_Base | bool |
|
455 | + * @throws EE_Error |
|
456 | + * @throws ReflectionException |
|
457 | + */ |
|
458 | + public function load_helper($class_name, $arguments = array(), $load_only = true) |
|
459 | + { |
|
460 | + // todo: add doing_it_wrong() in a few versions after all addons have had calls to this method removed |
|
461 | + $helper_paths = apply_filters('FHEE__EE_Registry__load_helper__helper_paths', array(EE_HELPERS)); |
|
462 | + // retrieve instantiated class |
|
463 | + return $this->_load( |
|
464 | + $helper_paths, |
|
465 | + 'EEH_', |
|
466 | + $class_name, |
|
467 | + 'helper', |
|
468 | + $arguments, |
|
469 | + false, |
|
470 | + true, |
|
471 | + $load_only |
|
472 | + ); |
|
473 | + } |
|
474 | + |
|
475 | + |
|
476 | + |
|
477 | + /** |
|
478 | + * loads core classes - must be singletons |
|
479 | + * |
|
480 | + * @param string $class_name - simple class name ie: session |
|
481 | + * @param mixed $arguments |
|
482 | + * @param bool $load_only |
|
483 | + * @param bool $cache whether to cache the object or not. |
|
484 | + * @return mixed |
|
485 | + * @throws EE_Error |
|
486 | + * @throws ReflectionException |
|
487 | + */ |
|
488 | + public function load_lib($class_name, $arguments = array(), $load_only = false, $cache = true) |
|
489 | + { |
|
490 | + $paths = array( |
|
491 | + EE_LIBRARIES, |
|
492 | + EE_LIBRARIES . 'messages' . DS, |
|
493 | + EE_LIBRARIES . 'shortcodes' . DS, |
|
494 | + EE_LIBRARIES . 'qtips' . DS, |
|
495 | + EE_LIBRARIES . 'payment_methods' . DS, |
|
496 | + ); |
|
497 | + // retrieve instantiated class |
|
498 | + return $this->_load( |
|
499 | + $paths, |
|
500 | + 'EE_', |
|
501 | + $class_name, |
|
502 | + 'lib', |
|
503 | + $arguments, |
|
504 | + false, |
|
505 | + $cache, |
|
506 | + $load_only |
|
507 | + ); |
|
508 | + } |
|
509 | + |
|
510 | + |
|
511 | + |
|
512 | + /** |
|
513 | + * loads model classes - must be singletons |
|
514 | + * |
|
515 | + * @param string $class_name - simple class name ie: price |
|
516 | + * @param mixed $arguments |
|
517 | + * @param bool $load_only |
|
518 | + * @return EEM_Base | bool |
|
519 | + * @throws EE_Error |
|
520 | + * @throws ReflectionException |
|
521 | + */ |
|
522 | + public function load_model($class_name, $arguments = array(), $load_only = false) |
|
523 | + { |
|
524 | + $paths = apply_filters( |
|
525 | + 'FHEE__EE_Registry__load_model__paths', array( |
|
526 | + EE_MODELS, |
|
527 | + EE_CORE, |
|
528 | + ) |
|
529 | + ); |
|
530 | + // retrieve instantiated class |
|
531 | + return $this->_load( |
|
532 | + $paths, |
|
533 | + 'EEM_', |
|
534 | + $class_name, |
|
535 | + 'model', |
|
536 | + $arguments, |
|
537 | + false, |
|
538 | + true, |
|
539 | + $load_only |
|
540 | + ); |
|
541 | + } |
|
542 | + |
|
543 | + |
|
544 | + |
|
545 | + /** |
|
546 | + * loads model classes - must be singletons |
|
547 | + * |
|
548 | + * @param string $class_name - simple class name ie: price |
|
549 | + * @param mixed $arguments |
|
550 | + * @param bool $load_only |
|
551 | + * @return mixed | bool |
|
552 | + * @throws EE_Error |
|
553 | + * @throws ReflectionException |
|
554 | + */ |
|
555 | + public function load_model_class($class_name, $arguments = array(), $load_only = true) |
|
556 | + { |
|
557 | + $paths = array( |
|
558 | + EE_MODELS . 'fields' . DS, |
|
559 | + EE_MODELS . 'helpers' . DS, |
|
560 | + EE_MODELS . 'relations' . DS, |
|
561 | + EE_MODELS . 'strategies' . DS, |
|
562 | + ); |
|
563 | + // retrieve instantiated class |
|
564 | + return $this->_load( |
|
565 | + $paths, |
|
566 | + 'EE_', |
|
567 | + $class_name, |
|
568 | + '', |
|
569 | + $arguments, |
|
570 | + false, |
|
571 | + true, |
|
572 | + $load_only |
|
573 | + ); |
|
574 | + } |
|
575 | + |
|
576 | + |
|
577 | + |
|
578 | + /** |
|
579 | + * Determines if $model_name is the name of an actual EE model. |
|
580 | + * |
|
581 | + * @param string $model_name like Event, Attendee, Question_Group_Question, etc. |
|
582 | + * @return boolean |
|
583 | + */ |
|
584 | + public function is_model_name($model_name) |
|
585 | + { |
|
586 | + return isset($this->models[$model_name]); |
|
587 | + } |
|
588 | + |
|
589 | + |
|
590 | + |
|
591 | + /** |
|
592 | + * generic class loader |
|
593 | + * |
|
594 | + * @param string $path_to_file - directory path to file location, not including filename |
|
595 | + * @param string $file_name - file name ie: my_file.php, including extension |
|
596 | + * @param string $type - file type - core? class? helper? model? |
|
597 | + * @param mixed $arguments |
|
598 | + * @param bool $load_only |
|
599 | + * @return mixed |
|
600 | + * @throws EE_Error |
|
601 | + * @throws ReflectionException |
|
602 | + */ |
|
603 | + public function load_file($path_to_file, $file_name, $type = '', $arguments = array(), $load_only = true) |
|
604 | + { |
|
605 | + // retrieve instantiated class |
|
606 | + return $this->_load( |
|
607 | + $path_to_file, |
|
608 | + '', |
|
609 | + $file_name, |
|
610 | + $type, |
|
611 | + $arguments, |
|
612 | + false, |
|
613 | + true, |
|
614 | + $load_only |
|
615 | + ); |
|
616 | + } |
|
617 | + |
|
618 | + |
|
619 | + |
|
620 | + /** |
|
621 | + * @param string $path_to_file - directory path to file location, not including filename |
|
622 | + * @param string $class_name - full class name ie: My_Class |
|
623 | + * @param string $type - file type - core? class? helper? model? |
|
624 | + * @param mixed $arguments |
|
625 | + * @param bool $load_only |
|
626 | + * @return bool|EE_Addon|object |
|
627 | + * @throws EE_Error |
|
628 | + * @throws ReflectionException |
|
629 | + */ |
|
630 | + public function load_addon($path_to_file, $class_name, $type = 'class', $arguments = array(), $load_only = false) |
|
631 | + { |
|
632 | + // retrieve instantiated class |
|
633 | + return $this->_load( |
|
634 | + $path_to_file, |
|
635 | + 'addon', |
|
636 | + $class_name, |
|
637 | + $type, |
|
638 | + $arguments, |
|
639 | + false, |
|
640 | + true, |
|
641 | + $load_only |
|
642 | + ); |
|
643 | + } |
|
644 | + |
|
645 | + |
|
646 | + |
|
647 | + /** |
|
648 | + * instantiates, caches, and automatically resolves dependencies |
|
649 | + * for classes that use a Fully Qualified Class Name. |
|
650 | + * if the class is not capable of being loaded using PSR-4 autoloading, |
|
651 | + * then you need to use one of the existing load_*() methods |
|
652 | + * which can resolve the classname and filepath from the passed arguments |
|
653 | + * |
|
654 | + * @param bool|string $class_name Fully Qualified Class Name |
|
655 | + * @param array $arguments an argument, or array of arguments to pass to the class upon instantiation |
|
656 | + * @param bool $cache whether to cache the instantiated object for reuse |
|
657 | + * @param bool $from_db some classes are instantiated from the db |
|
658 | + * and thus call a different method to instantiate |
|
659 | + * @param bool $load_only if true, will only load the file, but will NOT instantiate an object |
|
660 | + * @param bool|string $addon if true, will cache the object in the EE_Registry->$addons array |
|
661 | + * @return bool|null|mixed null = failure to load or instantiate class object. |
|
662 | + * object = class loaded and instantiated successfully. |
|
663 | + * bool = fail or success when $load_only is true |
|
664 | + * @throws EE_Error |
|
665 | + * @throws ReflectionException |
|
666 | + */ |
|
667 | + public function create( |
|
668 | + $class_name = false, |
|
669 | + $arguments = array(), |
|
670 | + $cache = false, |
|
671 | + $from_db = false, |
|
672 | + $load_only = false, |
|
673 | + $addon = false |
|
674 | + ) { |
|
675 | + $class_name = ltrim($class_name, '\\'); |
|
676 | + $class_name = $this->_dependency_map->get_alias($class_name); |
|
677 | + if (! class_exists($class_name)) { |
|
678 | + // maybe the class is registered with a preceding \ |
|
679 | + $class_name = strpos($class_name, '\\') !== 0 |
|
680 | + ? '\\' . $class_name |
|
681 | + : $class_name; |
|
682 | + // still doesn't exist ? |
|
683 | + if (! class_exists($class_name)) { |
|
684 | + return null; |
|
685 | + } |
|
686 | + } |
|
687 | + // if we're only loading the class and it already exists, then let's just return true immediately |
|
688 | + if ($load_only) { |
|
689 | + return true; |
|
690 | + } |
|
691 | + $addon = $addon |
|
692 | + ? 'addon' |
|
693 | + : ''; |
|
694 | + // $this->_cache_on is toggled during the recursive loading that can occur with dependency injection |
|
695 | + // $cache is controlled by individual calls to separate Registry loader methods like load_class() |
|
696 | + // $load_only is also controlled by individual calls to separate Registry loader methods like load_file() |
|
697 | + if ($this->_cache_on && $cache && ! $load_only) { |
|
698 | + // return object if it's already cached |
|
699 | + $cached_class = $this->_get_cached_class($class_name, $addon); |
|
700 | + if ($cached_class !== null) { |
|
701 | + return $cached_class; |
|
702 | + } |
|
703 | + } |
|
704 | + // obtain the loader method from the dependency map |
|
705 | + $loader = $this->_dependency_map->class_loader($class_name); |
|
706 | + // instantiate the requested object |
|
707 | + if ($loader instanceof Closure) { |
|
708 | + $class_obj = $loader($arguments); |
|
709 | + } else if ($loader && method_exists($this, $loader)) { |
|
710 | + $class_obj = $this->{$loader}($class_name, $arguments); |
|
711 | + } else { |
|
712 | + $class_obj = $this->_create_object($class_name, $arguments, $addon, $from_db); |
|
713 | + } |
|
714 | + if (($this->_cache_on && $cache) || $this->get_class_abbreviation($class_name, '')) { |
|
715 | + // save it for later... kinda like gum { : $ |
|
716 | + $this->_set_cached_class($class_obj, $class_name, $addon, $from_db); |
|
717 | + } |
|
718 | + $this->_cache_on = true; |
|
719 | + return $class_obj; |
|
720 | + } |
|
721 | + |
|
722 | + |
|
723 | + |
|
724 | + /** |
|
725 | + * instantiates, caches, and injects dependencies for classes |
|
726 | + * |
|
727 | + * @param array $file_paths an array of paths to folders to look in |
|
728 | + * @param string $class_prefix EE or EEM or... ??? |
|
729 | + * @param bool|string $class_name $class name |
|
730 | + * @param string $type file type - core? class? helper? model? |
|
731 | + * @param mixed $arguments an argument or array of arguments to pass to the class upon instantiation |
|
732 | + * @param bool $from_db some classes are instantiated from the db |
|
733 | + * and thus call a different method to instantiate |
|
734 | + * @param bool $cache whether to cache the instantiated object for reuse |
|
735 | + * @param bool $load_only if true, will only load the file, but will NOT instantiate an object |
|
736 | + * @return bool|null|object null = failure to load or instantiate class object. |
|
737 | + * object = class loaded and instantiated successfully. |
|
738 | + * bool = fail or success when $load_only is true |
|
739 | + * @throws EE_Error |
|
740 | + * @throws ReflectionException |
|
741 | + */ |
|
742 | + protected function _load( |
|
743 | + $file_paths = array(), |
|
744 | + $class_prefix = 'EE_', |
|
745 | + $class_name = false, |
|
746 | + $type = 'class', |
|
747 | + $arguments = array(), |
|
748 | + $from_db = false, |
|
749 | + $cache = true, |
|
750 | + $load_only = false |
|
751 | + ) { |
|
752 | + $class_name = ltrim($class_name, '\\'); |
|
753 | + // strip php file extension |
|
754 | + $class_name = str_replace('.php', '', trim($class_name)); |
|
755 | + // does the class have a prefix ? |
|
756 | + if (! empty($class_prefix) && $class_prefix !== 'addon') { |
|
757 | + // make sure $class_prefix is uppercase |
|
758 | + $class_prefix = strtoupper(trim($class_prefix)); |
|
759 | + // add class prefix ONCE!!! |
|
760 | + $class_name = $class_prefix . str_replace($class_prefix, '', $class_name); |
|
761 | + } |
|
762 | + $class_name = $this->_dependency_map->get_alias($class_name); |
|
763 | + $class_exists = class_exists($class_name); |
|
764 | + // if we're only loading the class and it already exists, then let's just return true immediately |
|
765 | + if ($load_only && $class_exists) { |
|
766 | + return true; |
|
767 | + } |
|
768 | + // $this->_cache_on is toggled during the recursive loading that can occur with dependency injection |
|
769 | + // $cache is controlled by individual calls to separate Registry loader methods like load_class() |
|
770 | + // $load_only is also controlled by individual calls to separate Registry loader methods like load_file() |
|
771 | + if ($this->_cache_on && $cache && ! $load_only) { |
|
772 | + // return object if it's already cached |
|
773 | + $cached_class = $this->_get_cached_class($class_name, $class_prefix); |
|
774 | + if ($cached_class !== null) { |
|
775 | + return $cached_class; |
|
776 | + } |
|
777 | + } |
|
778 | + // if the class doesn't already exist.. then we need to try and find the file and load it |
|
779 | + if (! $class_exists) { |
|
780 | + // get full path to file |
|
781 | + $path = $this->_resolve_path($class_name, $type, $file_paths); |
|
782 | + // load the file |
|
783 | + $loaded = $this->_require_file($path, $class_name, $type, $file_paths); |
|
784 | + // if loading failed, or we are only loading a file but NOT instantiating an object |
|
785 | + if (! $loaded || $load_only) { |
|
786 | + // return boolean if only loading, or null if an object was expected |
|
787 | + return $load_only |
|
788 | + ? $loaded |
|
789 | + : null; |
|
790 | + } |
|
791 | + } |
|
792 | + // instantiate the requested object |
|
793 | + $class_obj = $this->_create_object($class_name, $arguments, $type, $from_db); |
|
794 | + if ($this->_cache_on && $cache) { |
|
795 | + // save it for later... kinda like gum { : $ |
|
796 | + $this->_set_cached_class($class_obj, $class_name, $class_prefix, $from_db); |
|
797 | + } |
|
798 | + $this->_cache_on = true; |
|
799 | + return $class_obj; |
|
800 | + } |
|
801 | + |
|
802 | + |
|
803 | + |
|
804 | + /** |
|
805 | + * @param string $class_name |
|
806 | + * @param string $default have to specify something, but not anything that will conflict |
|
807 | + * @return mixed|string |
|
808 | + */ |
|
809 | + protected function get_class_abbreviation($class_name, $default = 'FANCY_BATMAN_PANTS') |
|
810 | + { |
|
811 | + return isset($this->_class_abbreviations[$class_name]) |
|
812 | + ? $this->_class_abbreviations[$class_name] |
|
813 | + : $default; |
|
814 | + } |
|
815 | + |
|
816 | + /** |
|
817 | + * attempts to find a cached version of the requested class |
|
818 | + * by looking in the following places: |
|
819 | + * $this->{$class_abbreviation} ie: $this->CART |
|
820 | + * $this->{$class_name} ie: $this->Some_Class |
|
821 | + * $this->LIB->{$class_name} ie: $this->LIB->Some_Class |
|
822 | + * $this->addon->{$class_name} ie: $this->addon->Some_Addon_Class |
|
823 | + * |
|
824 | + * @param string $class_name |
|
825 | + * @param string $class_prefix |
|
826 | + * @return mixed |
|
827 | + */ |
|
828 | + protected function _get_cached_class($class_name, $class_prefix = '') |
|
829 | + { |
|
830 | + if ($class_name === 'EE_Registry') { |
|
831 | + return $this; |
|
832 | + } |
|
833 | + $class_abbreviation = $this->get_class_abbreviation($class_name); |
|
834 | + $class_name = str_replace('\\', '_', $class_name); |
|
835 | + // check if class has already been loaded, and return it if it has been |
|
836 | + if (isset($this->{$class_abbreviation})) { |
|
837 | + return $this->{$class_abbreviation}; |
|
838 | + } |
|
839 | + if (isset ($this->{$class_name})) { |
|
840 | + return $this->{$class_name}; |
|
841 | + } |
|
842 | + if (isset ($this->LIB->{$class_name})) { |
|
843 | + return $this->LIB->{$class_name}; |
|
844 | + } |
|
845 | + if ($class_prefix === 'addon' && isset ($this->addons->{$class_name})) { |
|
846 | + return $this->addons->{$class_name}; |
|
847 | + } |
|
848 | + return null; |
|
849 | + } |
|
850 | + |
|
851 | + |
|
852 | + |
|
853 | + /** |
|
854 | + * removes a cached version of the requested class |
|
855 | + * |
|
856 | + * @param string $class_name |
|
857 | + * @param boolean $addon |
|
858 | + * @return boolean |
|
859 | + */ |
|
860 | + public function clear_cached_class($class_name, $addon = false) |
|
861 | + { |
|
862 | + $class_abbreviation = $this->get_class_abbreviation($class_name); |
|
863 | + $class_name = str_replace('\\', '_', $class_name); |
|
864 | + // check if class has already been loaded, and return it if it has been |
|
865 | + if (isset($this->{$class_abbreviation})) { |
|
866 | + $this->{$class_abbreviation} = null; |
|
867 | + return true; |
|
868 | + } |
|
869 | + if (isset($this->{$class_name})) { |
|
870 | + $this->{$class_name} = null; |
|
871 | + return true; |
|
872 | + } |
|
873 | + if (isset($this->LIB->{$class_name})) { |
|
874 | + unset($this->LIB->{$class_name}); |
|
875 | + return true; |
|
876 | + } |
|
877 | + if ($addon && isset($this->addons->{$class_name})) { |
|
878 | + unset($this->addons->{$class_name}); |
|
879 | + return true; |
|
880 | + } |
|
881 | + return false; |
|
882 | + } |
|
883 | + |
|
884 | + |
|
885 | + |
|
886 | + /** |
|
887 | + * attempts to find a full valid filepath for the requested class. |
|
888 | + * loops thru each of the base paths in the $file_paths array and appends : "{classname} . {file type} . php" |
|
889 | + * then returns that path if the target file has been found and is readable |
|
890 | + * |
|
891 | + * @param string $class_name |
|
892 | + * @param string $type |
|
893 | + * @param array $file_paths |
|
894 | + * @return string | bool |
|
895 | + */ |
|
896 | + protected function _resolve_path($class_name, $type = '', $file_paths = array()) |
|
897 | + { |
|
898 | + // make sure $file_paths is an array |
|
899 | + $file_paths = is_array($file_paths) |
|
900 | + ? $file_paths |
|
901 | + : array($file_paths); |
|
902 | + // cycle thru paths |
|
903 | + foreach ($file_paths as $key => $file_path) { |
|
904 | + // convert all separators to proper DS, if no filepath, then use EE_CLASSES |
|
905 | + $file_path = $file_path |
|
906 | + ? str_replace(array('/', '\\'), DS, $file_path) |
|
907 | + : EE_CLASSES; |
|
908 | + // prep file type |
|
909 | + $type = ! empty($type) |
|
910 | + ? trim($type, '.') . '.' |
|
911 | + : ''; |
|
912 | + // build full file path |
|
913 | + $file_paths[$key] = rtrim($file_path, DS) . DS . $class_name . '.' . $type . 'php'; |
|
914 | + //does the file exist and can be read ? |
|
915 | + if (is_readable($file_paths[$key])) { |
|
916 | + return $file_paths[$key]; |
|
917 | + } |
|
918 | + } |
|
919 | + return false; |
|
920 | + } |
|
921 | + |
|
922 | + |
|
923 | + |
|
924 | + /** |
|
925 | + * basically just performs a require_once() |
|
926 | + * but with some error handling |
|
927 | + * |
|
928 | + * @param string $path |
|
929 | + * @param string $class_name |
|
930 | + * @param string $type |
|
931 | + * @param array $file_paths |
|
932 | + * @return bool |
|
933 | + * @throws EE_Error |
|
934 | + * @throws ReflectionException |
|
935 | + */ |
|
936 | + protected function _require_file($path, $class_name, $type = '', $file_paths = array()) |
|
937 | + { |
|
938 | + // don't give up! you gotta... |
|
939 | + try { |
|
940 | + //does the file exist and can it be read ? |
|
941 | + if (! $path) { |
|
942 | + // so sorry, can't find the file |
|
943 | + throw new EE_Error ( |
|
944 | + sprintf( |
|
945 | + esc_html__( |
|
946 | + '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', |
|
947 | + 'event_espresso' |
|
948 | + ), |
|
949 | + trim($type, '.'), |
|
950 | + $class_name, |
|
951 | + '<br />' . implode(',<br />', $file_paths) |
|
952 | + ) |
|
953 | + ); |
|
954 | + } |
|
955 | + // get the file |
|
956 | + require_once($path); |
|
957 | + // if the class isn't already declared somewhere |
|
958 | + if (class_exists($class_name, false) === false) { |
|
959 | + // so sorry, not a class |
|
960 | + throw new EE_Error( |
|
961 | + sprintf( |
|
962 | + esc_html__('The %s file %s does not appear to contain the %s Class.', 'event_espresso'), |
|
963 | + $type, |
|
964 | + $path, |
|
965 | + $class_name |
|
966 | + ) |
|
967 | + ); |
|
968 | + } |
|
969 | + } catch (EE_Error $e) { |
|
970 | + $e->get_error(); |
|
971 | + return false; |
|
972 | + } |
|
973 | + return true; |
|
974 | + } |
|
975 | + |
|
976 | + |
|
977 | + |
|
978 | + /** |
|
979 | + * _create_object |
|
980 | + * Attempts to instantiate the requested class via any of the |
|
981 | + * commonly used instantiation methods employed throughout EE. |
|
982 | + * The priority for instantiation is as follows: |
|
983 | + * - abstract classes or any class flagged as "load only" (no instantiation occurs) |
|
984 | + * - model objects via their 'new_instance_from_db' method |
|
985 | + * - model objects via their 'new_instance' method |
|
986 | + * - "singleton" classes" via their 'instance' method |
|
987 | + * - standard instantiable classes via their __constructor |
|
988 | + * Prior to instantiation, if the classname exists in the dependency_map, |
|
989 | + * then the constructor for the requested class will be examined to determine |
|
990 | + * if any dependencies exist, and if they can be injected. |
|
991 | + * If so, then those classes will be added to the array of arguments passed to the constructor |
|
992 | + * |
|
993 | + * @param string $class_name |
|
994 | + * @param array $arguments |
|
995 | + * @param string $type |
|
996 | + * @param bool $from_db |
|
997 | + * @return null|object |
|
998 | + * @throws EE_Error |
|
999 | + * @throws ReflectionException |
|
1000 | + */ |
|
1001 | + protected function _create_object($class_name, $arguments = array(), $type = '', $from_db = false) |
|
1002 | + { |
|
1003 | + $class_obj = null; |
|
1004 | + $instantiation_mode = '0) none'; |
|
1005 | + // don't give up! you gotta... |
|
1006 | + try { |
|
1007 | + // create reflection |
|
1008 | + $reflector = $this->get_ReflectionClass($class_name); |
|
1009 | + // make sure arguments are an array |
|
1010 | + $arguments = is_array($arguments) |
|
1011 | + ? $arguments |
|
1012 | + : array($arguments); |
|
1013 | + // and if arguments array is numerically and sequentially indexed, then we want it to remain as is, |
|
1014 | + // else wrap it in an additional array so that it doesn't get split into multiple parameters |
|
1015 | + $arguments = $this->_array_is_numerically_and_sequentially_indexed($arguments) |
|
1016 | + ? $arguments |
|
1017 | + : array($arguments); |
|
1018 | + // attempt to inject dependencies ? |
|
1019 | + if ($this->_dependency_map->has($class_name)) { |
|
1020 | + $arguments = $this->_resolve_dependencies($reflector, $class_name, $arguments); |
|
1021 | + } |
|
1022 | + // instantiate the class if possible |
|
1023 | + if ($reflector->isAbstract()) { |
|
1024 | + // nothing to instantiate, loading file was enough |
|
1025 | + // does not throw an exception so $instantiation_mode is unused |
|
1026 | + // $instantiation_mode = "1) no constructor abstract class"; |
|
1027 | + $class_obj = true; |
|
1028 | + } else if (empty($arguments) && $reflector->getConstructor() === null && $reflector->isInstantiable()) { |
|
1029 | + // no constructor = static methods only... nothing to instantiate, loading file was enough |
|
1030 | + $instantiation_mode = '2) no constructor but instantiable'; |
|
1031 | + $class_obj = $reflector->newInstance(); |
|
1032 | + } else if ($from_db && method_exists($class_name, 'new_instance_from_db')) { |
|
1033 | + $instantiation_mode = '3) new_instance_from_db()'; |
|
1034 | + $class_obj = call_user_func_array(array($class_name, 'new_instance_from_db'), $arguments); |
|
1035 | + } else if (method_exists($class_name, 'new_instance')) { |
|
1036 | + $instantiation_mode = '4) new_instance()'; |
|
1037 | + $class_obj = call_user_func_array(array($class_name, 'new_instance'), $arguments); |
|
1038 | + } else if (method_exists($class_name, 'instance')) { |
|
1039 | + $instantiation_mode = '5) instance()'; |
|
1040 | + $class_obj = call_user_func_array(array($class_name, 'instance'), $arguments); |
|
1041 | + } else if ($reflector->isInstantiable()) { |
|
1042 | + $instantiation_mode = '6) constructor'; |
|
1043 | + $class_obj = $reflector->newInstanceArgs($arguments); |
|
1044 | + } else { |
|
1045 | + // heh ? something's not right ! |
|
1046 | + throw new EE_Error( |
|
1047 | + sprintf( |
|
1048 | + esc_html__('The %s file %s could not be instantiated.', 'event_espresso'), |
|
1049 | + $type, |
|
1050 | + $class_name |
|
1051 | + ) |
|
1052 | + ); |
|
1053 | + } |
|
1054 | + } catch (Exception $e) { |
|
1055 | + if (! $e instanceof EE_Error) { |
|
1056 | + $e = new EE_Error( |
|
1057 | + sprintf( |
|
1058 | + esc_html__( |
|
1059 | + 'The following error occurred while attempting to instantiate "%1$s": %2$s %3$s %2$s instantiation mode : %4$s', |
|
1060 | + 'event_espresso' |
|
1061 | + ), |
|
1062 | + $class_name, |
|
1063 | + '<br />', |
|
1064 | + $e->getMessage(), |
|
1065 | + $instantiation_mode |
|
1066 | + ) |
|
1067 | + ); |
|
1068 | + } |
|
1069 | + $e->get_error(); |
|
1070 | + } |
|
1071 | + return $class_obj; |
|
1072 | + } |
|
1073 | + |
|
1074 | + |
|
1075 | + |
|
1076 | + /** |
|
1077 | + * @see http://stackoverflow.com/questions/173400/how-to-check-if-php-array-is-associative-or-sequential |
|
1078 | + * @param array $array |
|
1079 | + * @return bool |
|
1080 | + */ |
|
1081 | + protected function _array_is_numerically_and_sequentially_indexed(array $array) |
|
1082 | + { |
|
1083 | + return ! empty($array) |
|
1084 | + ? array_keys($array) === range(0, count($array) - 1) |
|
1085 | + : true; |
|
1086 | + } |
|
1087 | + |
|
1088 | + |
|
1089 | + |
|
1090 | + /** |
|
1091 | + * getReflectionClass |
|
1092 | + * checks if a ReflectionClass object has already been generated for a class |
|
1093 | + * and returns that instead of creating a new one |
|
1094 | + * |
|
1095 | + * @param string $class_name |
|
1096 | + * @return ReflectionClass |
|
1097 | + * @throws ReflectionException |
|
1098 | + */ |
|
1099 | + public function get_ReflectionClass($class_name) |
|
1100 | + { |
|
1101 | + if ( |
|
1102 | + ! isset($this->_reflectors[$class_name]) |
|
1103 | + || ! $this->_reflectors[$class_name] instanceof ReflectionClass |
|
1104 | + ) { |
|
1105 | + $this->_reflectors[$class_name] = new ReflectionClass($class_name); |
|
1106 | + } |
|
1107 | + return $this->_reflectors[$class_name]; |
|
1108 | + } |
|
1109 | + |
|
1110 | + |
|
1111 | + |
|
1112 | + /** |
|
1113 | + * _resolve_dependencies |
|
1114 | + * examines the constructor for the requested class to determine |
|
1115 | + * if any dependencies exist, and if they can be injected. |
|
1116 | + * If so, then those classes will be added to the array of arguments passed to the constructor |
|
1117 | + * PLZ NOTE: this is achieved by type hinting the constructor params |
|
1118 | + * For example: |
|
1119 | + * if attempting to load a class "Foo" with the following constructor: |
|
1120 | + * __construct( Bar $bar_class, Fighter $grohl_class ) |
|
1121 | + * then $bar_class and $grohl_class will be added to the $arguments array, |
|
1122 | + * but only IF they are NOT already present in the incoming arguments array, |
|
1123 | + * and the correct classes can be loaded |
|
1124 | + * |
|
1125 | + * @param ReflectionClass $reflector |
|
1126 | + * @param string $class_name |
|
1127 | + * @param array $arguments |
|
1128 | + * @return array |
|
1129 | + * @throws EE_Error |
|
1130 | + * @throws ReflectionException |
|
1131 | + */ |
|
1132 | + protected function _resolve_dependencies(ReflectionClass $reflector, $class_name, $arguments = array()) |
|
1133 | + { |
|
1134 | + // let's examine the constructor |
|
1135 | + $constructor = $reflector->getConstructor(); |
|
1136 | + // whu? huh? nothing? |
|
1137 | + if (! $constructor) { |
|
1138 | + return $arguments; |
|
1139 | + } |
|
1140 | + // get constructor parameters |
|
1141 | + $params = $constructor->getParameters(); |
|
1142 | + // and the keys for the incoming arguments array so that we can compare existing arguments with what is expected |
|
1143 | + $argument_keys = array_keys($arguments); |
|
1144 | + // now loop thru all of the constructors expected parameters |
|
1145 | + foreach ($params as $index => $param) { |
|
1146 | + // is this a dependency for a specific class ? |
|
1147 | + $param_class = $param->getClass() |
|
1148 | + ? $param->getClass()->name |
|
1149 | + : null; |
|
1150 | + // BUT WAIT !!! This class may be an alias for something else (or getting replaced at runtime) |
|
1151 | + $param_class = $this->_dependency_map->has_alias($param_class, $class_name) |
|
1152 | + ? $this->_dependency_map->get_alias($param_class, $class_name) |
|
1153 | + : $param_class; |
|
1154 | + if ( |
|
1155 | + // param is not even a class |
|
1156 | + $param_class === null |
|
1157 | + // and something already exists in the incoming arguments for this param |
|
1158 | + && isset($argument_keys[$index], $arguments[$argument_keys[$index]]) |
|
1159 | + ) { |
|
1160 | + // so let's skip this argument and move on to the next |
|
1161 | + continue; |
|
1162 | + } |
|
1163 | + if ( |
|
1164 | + // parameter is type hinted as a class, exists as an incoming argument, AND it's the correct class |
|
1165 | + $param_class !== null |
|
1166 | + && isset($argument_keys[$index], $arguments[$argument_keys[$index]]) |
|
1167 | + && $arguments[$argument_keys[$index]] instanceof $param_class |
|
1168 | + ) { |
|
1169 | + // skip this argument and move on to the next |
|
1170 | + continue; |
|
1171 | + } |
|
1172 | + if ( |
|
1173 | + // parameter is type hinted as a class, and should be injected |
|
1174 | + $param_class !== null |
|
1175 | + && $this->_dependency_map->has_dependency_for_class($class_name, $param_class) |
|
1176 | + ) { |
|
1177 | + $arguments = $this->_resolve_dependency( |
|
1178 | + $class_name, |
|
1179 | + $param_class, |
|
1180 | + $arguments, |
|
1181 | + $index, |
|
1182 | + $argument_keys |
|
1183 | + ); |
|
1184 | + } else { |
|
1185 | + try { |
|
1186 | + $arguments[$index] = $param->isDefaultValueAvailable() |
|
1187 | + ? $param->getDefaultValue() |
|
1188 | + : null; |
|
1189 | + } catch (ReflectionException $e) { |
|
1190 | + throw new ReflectionException( |
|
1191 | + sprintf( |
|
1192 | + esc_html__('%1$s for parameter "$%2$s"', 'event_espresso'), |
|
1193 | + $e->getMessage(), |
|
1194 | + $param->getName() |
|
1195 | + ) |
|
1196 | + ); |
|
1197 | + } |
|
1198 | + } |
|
1199 | + } |
|
1200 | + return $arguments; |
|
1201 | + } |
|
1202 | + |
|
1203 | + |
|
1204 | + |
|
1205 | + /** |
|
1206 | + * @param string $class_name |
|
1207 | + * @param string $param_class |
|
1208 | + * @param array $arguments |
|
1209 | + * @param mixed $index |
|
1210 | + * @param array $argument_keys |
|
1211 | + * @return array |
|
1212 | + * @throws EE_Error |
|
1213 | + * @throws ReflectionException |
|
1214 | + * @throws InvalidArgumentException |
|
1215 | + * @throws InvalidInterfaceException |
|
1216 | + * @throws InvalidDataTypeException |
|
1217 | + */ |
|
1218 | + protected function _resolve_dependency($class_name, $param_class, $arguments, $index, array $argument_keys) |
|
1219 | + { |
|
1220 | + $dependency = null; |
|
1221 | + // should dependency be loaded from cache ? |
|
1222 | + $cache_on = $this->_dependency_map->loading_strategy_for_class_dependency( |
|
1223 | + $class_name, |
|
1224 | + $param_class |
|
1225 | + ); |
|
1226 | + $cache_on = $cache_on !== EE_Dependency_Map::load_new_object; |
|
1227 | + // we might have a dependency... |
|
1228 | + // let's MAYBE try and find it in our cache if that's what's been requested |
|
1229 | + $cached_class = $cache_on |
|
1230 | + ? $this->_get_cached_class($param_class) |
|
1231 | + : null; |
|
1232 | + // and grab it if it exists |
|
1233 | + if ($cached_class instanceof $param_class) { |
|
1234 | + $dependency = $cached_class; |
|
1235 | + } else if ($param_class !== $class_name) { |
|
1236 | + // obtain the loader method from the dependency map |
|
1237 | + $loader = $this->_dependency_map->class_loader($param_class); |
|
1238 | + // is loader a custom closure ? |
|
1239 | + if ($loader instanceof Closure) { |
|
1240 | + $dependency = $loader($arguments); |
|
1241 | + } else { |
|
1242 | + // set the cache on property for the recursive loading call |
|
1243 | + $this->_cache_on = $cache_on; |
|
1244 | + // if not, then let's try and load it via the registry |
|
1245 | + if ($loader && method_exists($this, $loader)) { |
|
1246 | + $dependency = $this->{$loader}($param_class); |
|
1247 | + } else { |
|
1248 | + $dependency = LoaderFactory::getLoader()->load( |
|
1249 | + $param_class, |
|
1250 | + array(), |
|
1251 | + $cache_on |
|
1252 | + ); |
|
1253 | + } |
|
1254 | + } |
|
1255 | + } |
|
1256 | + // did we successfully find the correct dependency ? |
|
1257 | + if ($dependency instanceof $param_class) { |
|
1258 | + // then let's inject it into the incoming array of arguments at the correct location |
|
1259 | + $arguments[$index] = $dependency; |
|
1260 | + } |
|
1261 | + return $arguments; |
|
1262 | + } |
|
1263 | + |
|
1264 | + |
|
1265 | + |
|
1266 | + /** |
|
1267 | + * _set_cached_class |
|
1268 | + * attempts to cache the instantiated class locally |
|
1269 | + * in one of the following places, in the following order: |
|
1270 | + * $this->{class_abbreviation} ie: $this->CART |
|
1271 | + * $this->{$class_name} ie: $this->Some_Class |
|
1272 | + * $this->addon->{$$class_name} ie: $this->addon->Some_Addon_Class |
|
1273 | + * $this->LIB->{$class_name} ie: $this->LIB->Some_Class |
|
1274 | + * |
|
1275 | + * @param object $class_obj |
|
1276 | + * @param string $class_name |
|
1277 | + * @param string $class_prefix |
|
1278 | + * @param bool $from_db |
|
1279 | + * @return void |
|
1280 | + */ |
|
1281 | + protected function _set_cached_class($class_obj, $class_name, $class_prefix = '', $from_db = false) |
|
1282 | + { |
|
1283 | + if ($class_name === 'EE_Registry' || empty($class_obj)) { |
|
1284 | + return; |
|
1285 | + } |
|
1286 | + // return newly instantiated class |
|
1287 | + $class_abbreviation = $this->get_class_abbreviation($class_name, ''); |
|
1288 | + if ($class_abbreviation) { |
|
1289 | + $this->{$class_abbreviation} = $class_obj; |
|
1290 | + return; |
|
1291 | + } |
|
1292 | + $class_name = str_replace('\\', '_', $class_name); |
|
1293 | + if (property_exists($this, $class_name)) { |
|
1294 | + $this->{$class_name} = $class_obj; |
|
1295 | + return; |
|
1296 | + } |
|
1297 | + if ($class_prefix === 'addon') { |
|
1298 | + $this->addons->{$class_name} = $class_obj; |
|
1299 | + return; |
|
1300 | + } |
|
1301 | + if (! $from_db) { |
|
1302 | + $this->LIB->{$class_name} = $class_obj; |
|
1303 | + } |
|
1304 | + } |
|
1305 | + |
|
1306 | + |
|
1307 | + |
|
1308 | + /** |
|
1309 | + * call any loader that's been registered in the EE_Dependency_Map::$_class_loaders array |
|
1310 | + * |
|
1311 | + * @param string $classname PLEASE NOTE: the class name needs to match what's registered |
|
1312 | + * in the EE_Dependency_Map::$_class_loaders array, |
|
1313 | + * including the class prefix, ie: "EE_", "EEM_", "EEH_", etc |
|
1314 | + * @param array $arguments |
|
1315 | + * @return object |
|
1316 | + */ |
|
1317 | + public static function factory($classname, $arguments = array()) |
|
1318 | + { |
|
1319 | + $loader = self::instance()->_dependency_map->class_loader($classname); |
|
1320 | + if ($loader instanceof Closure) { |
|
1321 | + return $loader($arguments); |
|
1322 | + } |
|
1323 | + if (method_exists(self::instance(), $loader)) { |
|
1324 | + return self::instance()->{$loader}($classname, $arguments); |
|
1325 | + } |
|
1326 | + return null; |
|
1327 | + } |
|
1328 | + |
|
1329 | + |
|
1330 | + |
|
1331 | + /** |
|
1332 | + * Gets the addon by its name/slug (not classname. For that, just |
|
1333 | + * use the classname as the property name on EE_Config::instance()->addons) |
|
1334 | + * |
|
1335 | + * @param string $name |
|
1336 | + * @return EE_Addon |
|
1337 | + */ |
|
1338 | + public function get_addon_by_name($name) |
|
1339 | + { |
|
1340 | + foreach ($this->addons as $addon) { |
|
1341 | + if ($addon->name() === $name) { |
|
1342 | + return $addon; |
|
1343 | + } |
|
1344 | + } |
|
1345 | + return null; |
|
1346 | + } |
|
1347 | + |
|
1348 | + |
|
1349 | + |
|
1350 | + /** |
|
1351 | + * Gets an array of all the registered addons, where the keys are their names. (ie, what each returns for their |
|
1352 | + * name() function) They're already available on EE_Config::instance()->addons as properties, where each property's |
|
1353 | + * name is the addon's classname. So if you just want to get the addon by classname, use |
|
1354 | + * EE_Config::instance()->addons->{classname} |
|
1355 | + * |
|
1356 | + * @return EE_Addon[] where the KEYS are the addon's name() |
|
1357 | + */ |
|
1358 | + public function get_addons_by_name() |
|
1359 | + { |
|
1360 | + $addons = array(); |
|
1361 | + foreach ($this->addons as $addon) { |
|
1362 | + $addons[$addon->name()] = $addon; |
|
1363 | + } |
|
1364 | + return $addons; |
|
1365 | + } |
|
1366 | + |
|
1367 | + |
|
1368 | + |
|
1369 | + /** |
|
1370 | + * Resets the specified model's instance AND makes sure EE_Registry doesn't keep |
|
1371 | + * a stale copy of it around |
|
1372 | + * |
|
1373 | + * @param string $model_name |
|
1374 | + * @return \EEM_Base |
|
1375 | + * @throws \EE_Error |
|
1376 | + */ |
|
1377 | + public function reset_model($model_name) |
|
1378 | + { |
|
1379 | + $model_class_name = strpos($model_name, 'EEM_') !== 0 |
|
1380 | + ? "EEM_{$model_name}" |
|
1381 | + : $model_name; |
|
1382 | + if (! isset($this->LIB->{$model_class_name}) || ! $this->LIB->{$model_class_name} instanceof EEM_Base) { |
|
1383 | + return null; |
|
1384 | + } |
|
1385 | + //get that model reset it and make sure we nuke the old reference to it |
|
1386 | + if ($this->LIB->{$model_class_name} instanceof $model_class_name |
|
1387 | + && is_callable( |
|
1388 | + array($model_class_name, 'reset') |
|
1389 | + )) { |
|
1390 | + $this->LIB->{$model_class_name} = $this->LIB->{$model_class_name}->reset(); |
|
1391 | + } else { |
|
1392 | + throw new EE_Error(sprintf(esc_html__('Model %s does not have a method "reset"', 'event_espresso'), $model_name)); |
|
1393 | + } |
|
1394 | + return $this->LIB->{$model_class_name}; |
|
1395 | + } |
|
1396 | + |
|
1397 | + |
|
1398 | + |
|
1399 | + /** |
|
1400 | + * Resets the registry. |
|
1401 | + * The criteria for what gets reset is based on what can be shared between sites on the same request when |
|
1402 | + * switch_to_blog is used in a multisite install. Here is a list of things that are NOT reset. |
|
1403 | + * - $_dependency_map |
|
1404 | + * - $_class_abbreviations |
|
1405 | + * - $NET_CFG (EE_Network_Config): The config is shared network wide so no need to reset. |
|
1406 | + * - $REQ: Still on the same request so no need to change. |
|
1407 | + * - $CAP: There is no site specific state in the EE_Capability class. |
|
1408 | + * - $SSN: Although ideally, the session should not be shared between site switches, we can't reset it because only |
|
1409 | + * one Session can be active in a single request. Resetting could resolve in "headers already sent" errors. |
|
1410 | + * - $addons: In multisite, the state of the addons is something controlled via hooks etc in a normal request. So |
|
1411 | + * for now, we won't reset the addons because it could break calls to an add-ons class/methods in the |
|
1412 | + * switch or on the restore. |
|
1413 | + * - $modules |
|
1414 | + * - $shortcodes |
|
1415 | + * - $widgets |
|
1416 | + * |
|
1417 | + * @param boolean $hard [deprecated] |
|
1418 | + * @param boolean $reinstantiate whether to create new instances of EE_Registry's singletons too, |
|
1419 | + * or just reset without re-instantiating (handy to set to FALSE if you're not |
|
1420 | + * sure if you CAN currently reinstantiate the singletons at the moment) |
|
1421 | + * @param bool $reset_models Defaults to true. When false, then the models are not reset. This is so |
|
1422 | + * client |
|
1423 | + * code instead can just change the model context to a different blog id if |
|
1424 | + * necessary |
|
1425 | + * @return EE_Registry |
|
1426 | + * @throws EE_Error |
|
1427 | + * @throws ReflectionException |
|
1428 | + */ |
|
1429 | + public static function reset($hard = false, $reinstantiate = true, $reset_models = true) |
|
1430 | + { |
|
1431 | + $instance = self::instance(); |
|
1432 | + $instance->_cache_on = true; |
|
1433 | + // reset some "special" classes |
|
1434 | + EEH_Activation::reset(); |
|
1435 | + $hard = apply_filters( 'FHEE__EE_Registry__reset__hard', $hard); |
|
1436 | + $instance->CFG = EE_Config::reset($hard, $reinstantiate); |
|
1437 | + $instance->CART = null; |
|
1438 | + $instance->MRM = null; |
|
1439 | + $instance->AssetsRegistry = $instance->create('EventEspresso\core\services\assets\Registry'); |
|
1440 | + //messages reset |
|
1441 | + EED_Messages::reset(); |
|
1442 | + //handle of objects cached on LIB |
|
1443 | + foreach (array('LIB', 'modules', 'shortcodes') as $cache) { |
|
1444 | + foreach ($instance->{$cache} as $class_name => $class) { |
|
1445 | + if (self::_reset_and_unset_object($class, $reset_models)) { |
|
1446 | + unset($instance->{$cache}->{$class_name}); |
|
1447 | + } |
|
1448 | + } |
|
1449 | + } |
|
1450 | + return $instance; |
|
1451 | + } |
|
1452 | + |
|
1453 | + |
|
1454 | + |
|
1455 | + /** |
|
1456 | + * if passed object implements ResettableInterface, then call it's reset() method |
|
1457 | + * if passed object implements InterminableInterface, then return false, |
|
1458 | + * to indicate that it should NOT be cleared from the Registry cache |
|
1459 | + * |
|
1460 | + * @param $object |
|
1461 | + * @param bool $reset_models |
|
1462 | + * @return bool returns true if cached object should be unset |
|
1463 | + */ |
|
1464 | + private static function _reset_and_unset_object($object, $reset_models) |
|
1465 | + { |
|
1466 | + if ($object instanceof ResettableInterface) { |
|
1467 | + if ($object instanceof EEM_Base) { |
|
1468 | + if ($reset_models) { |
|
1469 | + $object->reset(); |
|
1470 | + return true; |
|
1471 | + } |
|
1472 | + return false; |
|
1473 | + } |
|
1474 | + $object->reset(); |
|
1475 | + return true; |
|
1476 | + } |
|
1477 | + if (! $object instanceof InterminableInterface) { |
|
1478 | + return true; |
|
1479 | + } |
|
1480 | + return false; |
|
1481 | + } |
|
1482 | + |
|
1483 | + |
|
1484 | + |
|
1485 | + /** |
|
1486 | + * Gets all the custom post type models defined |
|
1487 | + * |
|
1488 | + * @return array keys are model "short names" (Eg "Event") and keys are classnames (eg "EEM_Event") |
|
1489 | + */ |
|
1490 | + public function cpt_models() |
|
1491 | + { |
|
1492 | + $cpt_models = array(); |
|
1493 | + foreach ($this->non_abstract_db_models as $short_name => $classname) { |
|
1494 | + if (is_subclass_of($classname, 'EEM_CPT_Base')) { |
|
1495 | + $cpt_models[$short_name] = $classname; |
|
1496 | + } |
|
1497 | + } |
|
1498 | + return $cpt_models; |
|
1499 | + } |
|
1500 | + |
|
1501 | + |
|
1502 | + |
|
1503 | + /** |
|
1504 | + * @return \EE_Config |
|
1505 | + */ |
|
1506 | + public static function CFG() |
|
1507 | + { |
|
1508 | + return self::instance()->CFG; |
|
1509 | + } |
|
1510 | 1510 | |
1511 | 1511 | |
1512 | 1512 | } |
@@ -176,7 +176,7 @@ discard block |
||
176 | 176 | public static function instance(EE_Dependency_Map $dependency_map = null) |
177 | 177 | { |
178 | 178 | // check if class object is instantiated |
179 | - if (! self::$_instance instanceof EE_Registry) { |
|
179 | + if ( ! self::$_instance instanceof EE_Registry) { |
|
180 | 180 | self::$_instance = new self($dependency_map); |
181 | 181 | } |
182 | 182 | return self::$_instance; |
@@ -266,13 +266,13 @@ discard block |
||
266 | 266 | */ |
267 | 267 | public static function localize_i18n_js_strings() |
268 | 268 | { |
269 | - $i18n_js_strings = (array)self::$i18n_js_strings; |
|
269 | + $i18n_js_strings = (array) self::$i18n_js_strings; |
|
270 | 270 | foreach ($i18n_js_strings as $key => $value) { |
271 | 271 | if (is_scalar($value)) { |
272 | - $i18n_js_strings[$key] = html_entity_decode((string)$value, ENT_QUOTES, 'UTF-8'); |
|
272 | + $i18n_js_strings[$key] = html_entity_decode((string) $value, ENT_QUOTES, 'UTF-8'); |
|
273 | 273 | } |
274 | 274 | } |
275 | - return '/* <![CDATA[ */ var eei18n = ' . wp_json_encode($i18n_js_strings) . '; /* ]]> */'; |
|
275 | + return '/* <![CDATA[ */ var eei18n = '.wp_json_encode($i18n_js_strings).'; /* ]]> */'; |
|
276 | 276 | } |
277 | 277 | |
278 | 278 | |
@@ -288,7 +288,7 @@ discard block |
||
288 | 288 | $module_class = get_class($module); |
289 | 289 | $this->modules->{$module_class} = $module; |
290 | 290 | } else { |
291 | - if (! class_exists('EE_Module_Request_Router')) { |
|
291 | + if ( ! class_exists('EE_Module_Request_Router')) { |
|
292 | 292 | $this->load_core('Module_Request_Router'); |
293 | 293 | } |
294 | 294 | $this->modules->{$module} = EE_Module_Request_Router::module_factory($module); |
@@ -328,9 +328,9 @@ discard block |
||
328 | 328 | EE_CORE, |
329 | 329 | EE_ADMIN, |
330 | 330 | EE_CPTS, |
331 | - EE_CORE . 'data_migration_scripts' . DS, |
|
332 | - EE_CORE . 'request_stack' . DS, |
|
333 | - EE_CORE . 'middleware' . DS, |
|
331 | + EE_CORE.'data_migration_scripts'.DS, |
|
332 | + EE_CORE.'request_stack'.DS, |
|
333 | + EE_CORE.'middleware'.DS, |
|
334 | 334 | ) |
335 | 335 | ); |
336 | 336 | // retrieve instantiated class |
@@ -363,7 +363,7 @@ discard block |
||
363 | 363 | $service_paths = apply_filters( |
364 | 364 | 'FHEE__EE_Registry__load_service__service_paths', |
365 | 365 | array( |
366 | - EE_CORE . 'services' . DS, |
|
366 | + EE_CORE.'services'.DS, |
|
367 | 367 | ) |
368 | 368 | ); |
369 | 369 | // retrieve instantiated class |
@@ -489,10 +489,10 @@ discard block |
||
489 | 489 | { |
490 | 490 | $paths = array( |
491 | 491 | EE_LIBRARIES, |
492 | - EE_LIBRARIES . 'messages' . DS, |
|
493 | - EE_LIBRARIES . 'shortcodes' . DS, |
|
494 | - EE_LIBRARIES . 'qtips' . DS, |
|
495 | - EE_LIBRARIES . 'payment_methods' . DS, |
|
492 | + EE_LIBRARIES.'messages'.DS, |
|
493 | + EE_LIBRARIES.'shortcodes'.DS, |
|
494 | + EE_LIBRARIES.'qtips'.DS, |
|
495 | + EE_LIBRARIES.'payment_methods'.DS, |
|
496 | 496 | ); |
497 | 497 | // retrieve instantiated class |
498 | 498 | return $this->_load( |
@@ -555,10 +555,10 @@ discard block |
||
555 | 555 | public function load_model_class($class_name, $arguments = array(), $load_only = true) |
556 | 556 | { |
557 | 557 | $paths = array( |
558 | - EE_MODELS . 'fields' . DS, |
|
559 | - EE_MODELS . 'helpers' . DS, |
|
560 | - EE_MODELS . 'relations' . DS, |
|
561 | - EE_MODELS . 'strategies' . DS, |
|
558 | + EE_MODELS.'fields'.DS, |
|
559 | + EE_MODELS.'helpers'.DS, |
|
560 | + EE_MODELS.'relations'.DS, |
|
561 | + EE_MODELS.'strategies'.DS, |
|
562 | 562 | ); |
563 | 563 | // retrieve instantiated class |
564 | 564 | return $this->_load( |
@@ -674,13 +674,13 @@ discard block |
||
674 | 674 | ) { |
675 | 675 | $class_name = ltrim($class_name, '\\'); |
676 | 676 | $class_name = $this->_dependency_map->get_alias($class_name); |
677 | - if (! class_exists($class_name)) { |
|
677 | + if ( ! class_exists($class_name)) { |
|
678 | 678 | // maybe the class is registered with a preceding \ |
679 | 679 | $class_name = strpos($class_name, '\\') !== 0 |
680 | - ? '\\' . $class_name |
|
680 | + ? '\\'.$class_name |
|
681 | 681 | : $class_name; |
682 | 682 | // still doesn't exist ? |
683 | - if (! class_exists($class_name)) { |
|
683 | + if ( ! class_exists($class_name)) { |
|
684 | 684 | return null; |
685 | 685 | } |
686 | 686 | } |
@@ -753,11 +753,11 @@ discard block |
||
753 | 753 | // strip php file extension |
754 | 754 | $class_name = str_replace('.php', '', trim($class_name)); |
755 | 755 | // does the class have a prefix ? |
756 | - if (! empty($class_prefix) && $class_prefix !== 'addon') { |
|
756 | + if ( ! empty($class_prefix) && $class_prefix !== 'addon') { |
|
757 | 757 | // make sure $class_prefix is uppercase |
758 | 758 | $class_prefix = strtoupper(trim($class_prefix)); |
759 | 759 | // add class prefix ONCE!!! |
760 | - $class_name = $class_prefix . str_replace($class_prefix, '', $class_name); |
|
760 | + $class_name = $class_prefix.str_replace($class_prefix, '', $class_name); |
|
761 | 761 | } |
762 | 762 | $class_name = $this->_dependency_map->get_alias($class_name); |
763 | 763 | $class_exists = class_exists($class_name); |
@@ -776,13 +776,13 @@ discard block |
||
776 | 776 | } |
777 | 777 | } |
778 | 778 | // if the class doesn't already exist.. then we need to try and find the file and load it |
779 | - if (! $class_exists) { |
|
779 | + if ( ! $class_exists) { |
|
780 | 780 | // get full path to file |
781 | 781 | $path = $this->_resolve_path($class_name, $type, $file_paths); |
782 | 782 | // load the file |
783 | 783 | $loaded = $this->_require_file($path, $class_name, $type, $file_paths); |
784 | 784 | // if loading failed, or we are only loading a file but NOT instantiating an object |
785 | - if (! $loaded || $load_only) { |
|
785 | + if ( ! $loaded || $load_only) { |
|
786 | 786 | // return boolean if only loading, or null if an object was expected |
787 | 787 | return $load_only |
788 | 788 | ? $loaded |
@@ -907,10 +907,10 @@ discard block |
||
907 | 907 | : EE_CLASSES; |
908 | 908 | // prep file type |
909 | 909 | $type = ! empty($type) |
910 | - ? trim($type, '.') . '.' |
|
910 | + ? trim($type, '.').'.' |
|
911 | 911 | : ''; |
912 | 912 | // build full file path |
913 | - $file_paths[$key] = rtrim($file_path, DS) . DS . $class_name . '.' . $type . 'php'; |
|
913 | + $file_paths[$key] = rtrim($file_path, DS).DS.$class_name.'.'.$type.'php'; |
|
914 | 914 | //does the file exist and can be read ? |
915 | 915 | if (is_readable($file_paths[$key])) { |
916 | 916 | return $file_paths[$key]; |
@@ -938,9 +938,9 @@ discard block |
||
938 | 938 | // don't give up! you gotta... |
939 | 939 | try { |
940 | 940 | //does the file exist and can it be read ? |
941 | - if (! $path) { |
|
941 | + if ( ! $path) { |
|
942 | 942 | // so sorry, can't find the file |
943 | - throw new EE_Error ( |
|
943 | + throw new EE_Error( |
|
944 | 944 | sprintf( |
945 | 945 | esc_html__( |
946 | 946 | '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', |
@@ -948,7 +948,7 @@ discard block |
||
948 | 948 | ), |
949 | 949 | trim($type, '.'), |
950 | 950 | $class_name, |
951 | - '<br />' . implode(',<br />', $file_paths) |
|
951 | + '<br />'.implode(',<br />', $file_paths) |
|
952 | 952 | ) |
953 | 953 | ); |
954 | 954 | } |
@@ -1052,7 +1052,7 @@ discard block |
||
1052 | 1052 | ); |
1053 | 1053 | } |
1054 | 1054 | } catch (Exception $e) { |
1055 | - if (! $e instanceof EE_Error) { |
|
1055 | + if ( ! $e instanceof EE_Error) { |
|
1056 | 1056 | $e = new EE_Error( |
1057 | 1057 | sprintf( |
1058 | 1058 | esc_html__( |
@@ -1134,7 +1134,7 @@ discard block |
||
1134 | 1134 | // let's examine the constructor |
1135 | 1135 | $constructor = $reflector->getConstructor(); |
1136 | 1136 | // whu? huh? nothing? |
1137 | - if (! $constructor) { |
|
1137 | + if ( ! $constructor) { |
|
1138 | 1138 | return $arguments; |
1139 | 1139 | } |
1140 | 1140 | // get constructor parameters |
@@ -1298,7 +1298,7 @@ discard block |
||
1298 | 1298 | $this->addons->{$class_name} = $class_obj; |
1299 | 1299 | return; |
1300 | 1300 | } |
1301 | - if (! $from_db) { |
|
1301 | + if ( ! $from_db) { |
|
1302 | 1302 | $this->LIB->{$class_name} = $class_obj; |
1303 | 1303 | } |
1304 | 1304 | } |
@@ -1379,7 +1379,7 @@ discard block |
||
1379 | 1379 | $model_class_name = strpos($model_name, 'EEM_') !== 0 |
1380 | 1380 | ? "EEM_{$model_name}" |
1381 | 1381 | : $model_name; |
1382 | - if (! isset($this->LIB->{$model_class_name}) || ! $this->LIB->{$model_class_name} instanceof EEM_Base) { |
|
1382 | + if ( ! isset($this->LIB->{$model_class_name}) || ! $this->LIB->{$model_class_name} instanceof EEM_Base) { |
|
1383 | 1383 | return null; |
1384 | 1384 | } |
1385 | 1385 | //get that model reset it and make sure we nuke the old reference to it |
@@ -1432,7 +1432,7 @@ discard block |
||
1432 | 1432 | $instance->_cache_on = true; |
1433 | 1433 | // reset some "special" classes |
1434 | 1434 | EEH_Activation::reset(); |
1435 | - $hard = apply_filters( 'FHEE__EE_Registry__reset__hard', $hard); |
|
1435 | + $hard = apply_filters('FHEE__EE_Registry__reset__hard', $hard); |
|
1436 | 1436 | $instance->CFG = EE_Config::reset($hard, $reinstantiate); |
1437 | 1437 | $instance->CART = null; |
1438 | 1438 | $instance->MRM = null; |
@@ -1474,7 +1474,7 @@ discard block |
||
1474 | 1474 | $object->reset(); |
1475 | 1475 | return true; |
1476 | 1476 | } |
1477 | - if (! $object instanceof InterminableInterface) { |
|
1477 | + if ( ! $object instanceof InterminableInterface) { |
|
1478 | 1478 | return true; |
1479 | 1479 | } |
1480 | 1480 | return false; |
@@ -22,103 +22,103 @@ |
||
22 | 22 | { |
23 | 23 | |
24 | 24 | |
25 | - /** |
|
26 | - * @var LoaderDecoratorInterface $new_loader |
|
27 | - */ |
|
28 | - private $new_loader; |
|
29 | - |
|
30 | - |
|
31 | - /** |
|
32 | - * @var LoaderDecoratorInterface $shared_loader |
|
33 | - */ |
|
34 | - private $shared_loader; |
|
35 | - |
|
36 | - |
|
37 | - |
|
38 | - /** |
|
39 | - * Loader constructor. |
|
40 | - * |
|
41 | - * @param LoaderDecoratorInterface|null $new_loader |
|
42 | - * @param LoaderDecoratorInterface|null $shared_loader |
|
43 | - * @throws InvalidInterfaceException |
|
44 | - * @throws InvalidArgumentException |
|
45 | - * @throws InvalidDataTypeException |
|
46 | - */ |
|
47 | - public function __construct(LoaderDecoratorInterface $new_loader, LoaderDecoratorInterface $shared_loader) |
|
48 | - { |
|
49 | - $this->new_loader = $new_loader; |
|
50 | - $this->shared_loader = $shared_loader; |
|
51 | - } |
|
52 | - |
|
53 | - |
|
54 | - |
|
55 | - /** |
|
56 | - * @return LoaderDecoratorInterface |
|
57 | - */ |
|
58 | - public function getNewLoader() |
|
59 | - { |
|
60 | - return $this->new_loader; |
|
61 | - } |
|
62 | - |
|
63 | - |
|
64 | - |
|
65 | - /** |
|
66 | - * @return LoaderDecoratorInterface |
|
67 | - */ |
|
68 | - public function getSharedLoader() |
|
69 | - { |
|
70 | - return $this->shared_loader; |
|
71 | - } |
|
72 | - |
|
73 | - |
|
74 | - |
|
75 | - /** |
|
76 | - * @param string $fqcn |
|
77 | - * @param array $arguments |
|
78 | - * @param bool $shared |
|
79 | - * @return mixed |
|
80 | - */ |
|
81 | - public function load($fqcn, $arguments = array(), $shared = true) |
|
82 | - { |
|
83 | - return $shared |
|
84 | - ? $this->getSharedLoader()->load($fqcn, $arguments) |
|
85 | - : $this->getNewLoader()->load($fqcn, $arguments); |
|
86 | - } |
|
87 | - |
|
88 | - |
|
89 | - |
|
90 | - /** |
|
91 | - * @param string $fqcn |
|
92 | - * @param array $arguments |
|
93 | - * @return mixed |
|
94 | - */ |
|
95 | - public function getNew($fqcn, $arguments = array()) |
|
96 | - { |
|
97 | - return $this->getNewLoader()->load($fqcn, $arguments); |
|
98 | - } |
|
99 | - |
|
100 | - |
|
101 | - |
|
102 | - /** |
|
103 | - * @param string $fqcn |
|
104 | - * @param array $arguments |
|
105 | - * @return mixed |
|
106 | - */ |
|
107 | - public function getShared($fqcn, $arguments = array()) |
|
108 | - { |
|
109 | - return $this->getSharedLoader()->load($fqcn, $arguments); |
|
110 | - } |
|
111 | - |
|
112 | - |
|
113 | - |
|
114 | - /** |
|
115 | - * calls reset() on loaders if that method exists |
|
116 | - */ |
|
117 | - public function reset() |
|
118 | - { |
|
119 | - $this->new_loader->reset(); |
|
120 | - $this->shared_loader->reset(); |
|
121 | - } |
|
25 | + /** |
|
26 | + * @var LoaderDecoratorInterface $new_loader |
|
27 | + */ |
|
28 | + private $new_loader; |
|
29 | + |
|
30 | + |
|
31 | + /** |
|
32 | + * @var LoaderDecoratorInterface $shared_loader |
|
33 | + */ |
|
34 | + private $shared_loader; |
|
35 | + |
|
36 | + |
|
37 | + |
|
38 | + /** |
|
39 | + * Loader constructor. |
|
40 | + * |
|
41 | + * @param LoaderDecoratorInterface|null $new_loader |
|
42 | + * @param LoaderDecoratorInterface|null $shared_loader |
|
43 | + * @throws InvalidInterfaceException |
|
44 | + * @throws InvalidArgumentException |
|
45 | + * @throws InvalidDataTypeException |
|
46 | + */ |
|
47 | + public function __construct(LoaderDecoratorInterface $new_loader, LoaderDecoratorInterface $shared_loader) |
|
48 | + { |
|
49 | + $this->new_loader = $new_loader; |
|
50 | + $this->shared_loader = $shared_loader; |
|
51 | + } |
|
52 | + |
|
53 | + |
|
54 | + |
|
55 | + /** |
|
56 | + * @return LoaderDecoratorInterface |
|
57 | + */ |
|
58 | + public function getNewLoader() |
|
59 | + { |
|
60 | + return $this->new_loader; |
|
61 | + } |
|
62 | + |
|
63 | + |
|
64 | + |
|
65 | + /** |
|
66 | + * @return LoaderDecoratorInterface |
|
67 | + */ |
|
68 | + public function getSharedLoader() |
|
69 | + { |
|
70 | + return $this->shared_loader; |
|
71 | + } |
|
72 | + |
|
73 | + |
|
74 | + |
|
75 | + /** |
|
76 | + * @param string $fqcn |
|
77 | + * @param array $arguments |
|
78 | + * @param bool $shared |
|
79 | + * @return mixed |
|
80 | + */ |
|
81 | + public function load($fqcn, $arguments = array(), $shared = true) |
|
82 | + { |
|
83 | + return $shared |
|
84 | + ? $this->getSharedLoader()->load($fqcn, $arguments) |
|
85 | + : $this->getNewLoader()->load($fqcn, $arguments); |
|
86 | + } |
|
87 | + |
|
88 | + |
|
89 | + |
|
90 | + /** |
|
91 | + * @param string $fqcn |
|
92 | + * @param array $arguments |
|
93 | + * @return mixed |
|
94 | + */ |
|
95 | + public function getNew($fqcn, $arguments = array()) |
|
96 | + { |
|
97 | + return $this->getNewLoader()->load($fqcn, $arguments); |
|
98 | + } |
|
99 | + |
|
100 | + |
|
101 | + |
|
102 | + /** |
|
103 | + * @param string $fqcn |
|
104 | + * @param array $arguments |
|
105 | + * @return mixed |
|
106 | + */ |
|
107 | + public function getShared($fqcn, $arguments = array()) |
|
108 | + { |
|
109 | + return $this->getSharedLoader()->load($fqcn, $arguments); |
|
110 | + } |
|
111 | + |
|
112 | + |
|
113 | + |
|
114 | + /** |
|
115 | + * calls reset() on loaders if that method exists |
|
116 | + */ |
|
117 | + public function reset() |
|
118 | + { |
|
119 | + $this->new_loader->reset(); |
|
120 | + $this->shared_loader->reset(); |
|
121 | + } |
|
122 | 122 | |
123 | 123 | } |
124 | 124 | // End of file Loader.php |
@@ -1,5 +1,5 @@ discard block |
||
1 | 1 | <?php if (! defined('EVENT_ESPRESSO_VERSION')) { |
2 | - exit('No direct script access allowed'); |
|
2 | + exit('No direct script access allowed'); |
|
3 | 3 | } |
4 | 4 | |
5 | 5 | /** |
@@ -23,104 +23,104 @@ discard block |
||
23 | 23 | { |
24 | 24 | |
25 | 25 | |
26 | - /** |
|
27 | - * REG_date |
|
28 | - */ |
|
29 | - function column_REG_date(EE_Registration $item) |
|
30 | - { |
|
26 | + /** |
|
27 | + * REG_date |
|
28 | + */ |
|
29 | + function column_REG_date(EE_Registration $item) |
|
30 | + { |
|
31 | 31 | |
32 | - //Build row actions |
|
33 | - $actions = array(); |
|
32 | + //Build row actions |
|
33 | + $actions = array(); |
|
34 | 34 | |
35 | - //Build row actions |
|
36 | - $check_in_url = EE_Admin_Page::add_query_args_and_nonce(array( |
|
37 | - 'action' => 'event_registrations', |
|
38 | - 'event_id' => $item->event_ID(), |
|
39 | - ), REG_ADMIN_URL); |
|
40 | - $actions['check_in'] = EE_Registry::instance()->CAP->current_user_can('ee_read_checkin', |
|
41 | - 'espresso_registrations_registration_checkins', $item->ID()) ? ' |
|
35 | + //Build row actions |
|
36 | + $check_in_url = EE_Admin_Page::add_query_args_and_nonce(array( |
|
37 | + 'action' => 'event_registrations', |
|
38 | + 'event_id' => $item->event_ID(), |
|
39 | + ), REG_ADMIN_URL); |
|
40 | + $actions['check_in'] = EE_Registry::instance()->CAP->current_user_can('ee_read_checkin', |
|
41 | + 'espresso_registrations_registration_checkins', $item->ID()) ? ' |
|
42 | 42 | <a href="' . $check_in_url . '" title="' . esc_attr__('The Check-In List allows you to easily toggle check-in status for this event', |
43 | - 'event_espresso') . '">' . __('View Check-ins', 'event_espresso') . '</a>' : __('View Check-ins', |
|
44 | - 'event_espresso'); |
|
43 | + 'event_espresso') . '">' . __('View Check-ins', 'event_espresso') . '</a>' : __('View Check-ins', |
|
44 | + 'event_espresso'); |
|
45 | 45 | |
46 | - $view_lnk_url = EE_Admin_Page::add_query_args_and_nonce(array( |
|
47 | - 'action' => 'view_transaction', |
|
48 | - 'TXN_ID' => $item->transaction()->ID(), |
|
49 | - ), TXN_ADMIN_URL); |
|
50 | - $REG_date = EE_Regisry::instance()->CAP->current_user_can('ee_read_transaction', |
|
51 | - 'espresso_transactions_view_transaction') ? '<a href="' . $view_lnk_url . '" title="' . esc_attr__('View Transaction Details', |
|
52 | - 'event_espresso') . '">' . $item->get_i18n_datetime('REG_date') . '</a>' : $item->get_i18n_datetime('REG_date'); |
|
46 | + $view_lnk_url = EE_Admin_Page::add_query_args_and_nonce(array( |
|
47 | + 'action' => 'view_transaction', |
|
48 | + 'TXN_ID' => $item->transaction()->ID(), |
|
49 | + ), TXN_ADMIN_URL); |
|
50 | + $REG_date = EE_Regisry::instance()->CAP->current_user_can('ee_read_transaction', |
|
51 | + 'espresso_transactions_view_transaction') ? '<a href="' . $view_lnk_url . '" title="' . esc_attr__('View Transaction Details', |
|
52 | + 'event_espresso') . '">' . $item->get_i18n_datetime('REG_date') . '</a>' : $item->get_i18n_datetime('REG_date'); |
|
53 | 53 | |
54 | - return sprintf('%1$s %2$s', $REG_date, $this->row_actions($actions)); |
|
54 | + return sprintf('%1$s %2$s', $REG_date, $this->row_actions($actions)); |
|
55 | 55 | |
56 | - } |
|
56 | + } |
|
57 | 57 | |
58 | 58 | |
59 | - /** |
|
60 | - * column_default |
|
61 | - * |
|
62 | - * @param \EE_Registration $item |
|
63 | - * @return string |
|
64 | - */ |
|
65 | - public function column_DTT_EVT_start(EE_Registration $item) |
|
66 | - { |
|
67 | - $remove_defaults = array('default_where_conditions' => 'none'); |
|
68 | - $ticket = $item->ticket(); |
|
69 | - $datetimes = $ticket instanceof EE_Ticket ? $ticket->datetimes($remove_defaults) : array(); |
|
70 | - $EVT_ID = $item->event_ID(); |
|
71 | - $datetime_string = ''; |
|
72 | - foreach ($datetimes as $datetime) { |
|
73 | - if ( |
|
74 | - EE_Registry::instance()->CAP->current_user_can( |
|
75 | - 'ee_read_checkin', |
|
76 | - 'espresso_registrations_registration_checkins', |
|
77 | - $item->ID() |
|
78 | - ) |
|
79 | - ) { |
|
80 | - // open "a" tag and "href" |
|
81 | - $datetime_string .= '<a href="'; |
|
82 | - // checkin URL |
|
83 | - $datetime_string .= EE_Admin_Page::add_query_args_and_nonce( |
|
84 | - array( |
|
85 | - 'action' => 'event_registrations', |
|
86 | - 'event_id' => $EVT_ID, |
|
87 | - 'DTT_ID' => $datetime->ID(), |
|
88 | - ), |
|
89 | - REG_ADMIN_URL |
|
90 | - ); |
|
91 | - // close "href" |
|
92 | - $datetime_string .= '"'; |
|
93 | - // open "title" tag |
|
94 | - $datetime_string .= ' title="'; |
|
95 | - // link title text |
|
96 | - $datetime_string .= esc_attr__('View Checkins for this Event', 'event_espresso'); |
|
97 | - // close "title" tag and end of "a" tag opening |
|
98 | - $datetime_string .= '">'; |
|
99 | - // link text |
|
100 | - $datetime_string .= $datetime->get_i18n_datetime('DTT_EVT_start'); |
|
101 | - // close "a" tag |
|
102 | - $datetime_string .= '</a>'; |
|
103 | - } else { |
|
104 | - $datetime_string .= $datetime->get_i18n_datetime('DTT_EVT_start'); |
|
105 | - } |
|
106 | - // add a "View Registrations" link that filters list by event AND datetime |
|
107 | - $datetime_string .= $this->row_actions( |
|
108 | - array( |
|
109 | - 'event_datetime_filter' => '<a href="' . EE_Admin_Page::add_query_args_and_nonce( |
|
110 | - array('event_id' => $EVT_ID, 'datetime_id' => $datetime->ID()), |
|
111 | - REG_ADMIN_URL |
|
112 | - ) . '" title="' . sprintf( |
|
113 | - esc_attr__( |
|
114 | - 'Filter this list to only show registrations for this datetime %s', |
|
115 | - 'event_espresso' |
|
116 | - ), |
|
117 | - $datetime->name() |
|
118 | - ) . '">' . __('View Registrations', 'event_espresso') . '</a>', |
|
119 | - ) |
|
120 | - ); |
|
121 | - } |
|
122 | - return $datetime_string; |
|
123 | - } |
|
59 | + /** |
|
60 | + * column_default |
|
61 | + * |
|
62 | + * @param \EE_Registration $item |
|
63 | + * @return string |
|
64 | + */ |
|
65 | + public function column_DTT_EVT_start(EE_Registration $item) |
|
66 | + { |
|
67 | + $remove_defaults = array('default_where_conditions' => 'none'); |
|
68 | + $ticket = $item->ticket(); |
|
69 | + $datetimes = $ticket instanceof EE_Ticket ? $ticket->datetimes($remove_defaults) : array(); |
|
70 | + $EVT_ID = $item->event_ID(); |
|
71 | + $datetime_string = ''; |
|
72 | + foreach ($datetimes as $datetime) { |
|
73 | + if ( |
|
74 | + EE_Registry::instance()->CAP->current_user_can( |
|
75 | + 'ee_read_checkin', |
|
76 | + 'espresso_registrations_registration_checkins', |
|
77 | + $item->ID() |
|
78 | + ) |
|
79 | + ) { |
|
80 | + // open "a" tag and "href" |
|
81 | + $datetime_string .= '<a href="'; |
|
82 | + // checkin URL |
|
83 | + $datetime_string .= EE_Admin_Page::add_query_args_and_nonce( |
|
84 | + array( |
|
85 | + 'action' => 'event_registrations', |
|
86 | + 'event_id' => $EVT_ID, |
|
87 | + 'DTT_ID' => $datetime->ID(), |
|
88 | + ), |
|
89 | + REG_ADMIN_URL |
|
90 | + ); |
|
91 | + // close "href" |
|
92 | + $datetime_string .= '"'; |
|
93 | + // open "title" tag |
|
94 | + $datetime_string .= ' title="'; |
|
95 | + // link title text |
|
96 | + $datetime_string .= esc_attr__('View Checkins for this Event', 'event_espresso'); |
|
97 | + // close "title" tag and end of "a" tag opening |
|
98 | + $datetime_string .= '">'; |
|
99 | + // link text |
|
100 | + $datetime_string .= $datetime->get_i18n_datetime('DTT_EVT_start'); |
|
101 | + // close "a" tag |
|
102 | + $datetime_string .= '</a>'; |
|
103 | + } else { |
|
104 | + $datetime_string .= $datetime->get_i18n_datetime('DTT_EVT_start'); |
|
105 | + } |
|
106 | + // add a "View Registrations" link that filters list by event AND datetime |
|
107 | + $datetime_string .= $this->row_actions( |
|
108 | + array( |
|
109 | + 'event_datetime_filter' => '<a href="' . EE_Admin_Page::add_query_args_and_nonce( |
|
110 | + array('event_id' => $EVT_ID, 'datetime_id' => $datetime->ID()), |
|
111 | + REG_ADMIN_URL |
|
112 | + ) . '" title="' . sprintf( |
|
113 | + esc_attr__( |
|
114 | + 'Filter this list to only show registrations for this datetime %s', |
|
115 | + 'event_espresso' |
|
116 | + ), |
|
117 | + $datetime->name() |
|
118 | + ) . '">' . __('View Registrations', 'event_espresso') . '</a>', |
|
119 | + ) |
|
120 | + ); |
|
121 | + } |
|
122 | + return $datetime_string; |
|
123 | + } |
|
124 | 124 | |
125 | 125 | |
126 | 126 | } //end Extend_EE_Registrations_List_Table |
@@ -1,4 +1,4 @@ discard block |
||
1 | -<?php if (! defined('EVENT_ESPRESSO_VERSION')) { |
|
1 | +<?php if ( ! defined('EVENT_ESPRESSO_VERSION')) { |
|
2 | 2 | exit('No direct script access allowed'); |
3 | 3 | } |
4 | 4 | |
@@ -33,23 +33,23 @@ discard block |
||
33 | 33 | $actions = array(); |
34 | 34 | |
35 | 35 | //Build row actions |
36 | - $check_in_url = EE_Admin_Page::add_query_args_and_nonce(array( |
|
36 | + $check_in_url = EE_Admin_Page::add_query_args_and_nonce(array( |
|
37 | 37 | 'action' => 'event_registrations', |
38 | 38 | 'event_id' => $item->event_ID(), |
39 | 39 | ), REG_ADMIN_URL); |
40 | 40 | $actions['check_in'] = EE_Registry::instance()->CAP->current_user_can('ee_read_checkin', |
41 | 41 | 'espresso_registrations_registration_checkins', $item->ID()) ? ' |
42 | - <a href="' . $check_in_url . '" title="' . esc_attr__('The Check-In List allows you to easily toggle check-in status for this event', |
|
43 | - 'event_espresso') . '">' . __('View Check-ins', 'event_espresso') . '</a>' : __('View Check-ins', |
|
42 | + <a href="' . $check_in_url.'" title="'.esc_attr__('The Check-In List allows you to easily toggle check-in status for this event', |
|
43 | + 'event_espresso').'">'.__('View Check-ins', 'event_espresso').'</a>' : __('View Check-ins', |
|
44 | 44 | 'event_espresso'); |
45 | 45 | |
46 | 46 | $view_lnk_url = EE_Admin_Page::add_query_args_and_nonce(array( |
47 | 47 | 'action' => 'view_transaction', |
48 | 48 | 'TXN_ID' => $item->transaction()->ID(), |
49 | 49 | ), TXN_ADMIN_URL); |
50 | - $REG_date = EE_Regisry::instance()->CAP->current_user_can('ee_read_transaction', |
|
51 | - 'espresso_transactions_view_transaction') ? '<a href="' . $view_lnk_url . '" title="' . esc_attr__('View Transaction Details', |
|
52 | - 'event_espresso') . '">' . $item->get_i18n_datetime('REG_date') . '</a>' : $item->get_i18n_datetime('REG_date'); |
|
50 | + $REG_date = EE_Regisry::instance()->CAP->current_user_can('ee_read_transaction', |
|
51 | + 'espresso_transactions_view_transaction') ? '<a href="'.$view_lnk_url.'" title="'.esc_attr__('View Transaction Details', |
|
52 | + 'event_espresso').'">'.$item->get_i18n_datetime('REG_date').'</a>' : $item->get_i18n_datetime('REG_date'); |
|
53 | 53 | |
54 | 54 | return sprintf('%1$s %2$s', $REG_date, $this->row_actions($actions)); |
55 | 55 | |
@@ -106,16 +106,16 @@ discard block |
||
106 | 106 | // add a "View Registrations" link that filters list by event AND datetime |
107 | 107 | $datetime_string .= $this->row_actions( |
108 | 108 | array( |
109 | - 'event_datetime_filter' => '<a href="' . EE_Admin_Page::add_query_args_and_nonce( |
|
109 | + 'event_datetime_filter' => '<a href="'.EE_Admin_Page::add_query_args_and_nonce( |
|
110 | 110 | array('event_id' => $EVT_ID, 'datetime_id' => $datetime->ID()), |
111 | 111 | REG_ADMIN_URL |
112 | - ) . '" title="' . sprintf( |
|
112 | + ).'" title="'.sprintf( |
|
113 | 113 | esc_attr__( |
114 | 114 | 'Filter this list to only show registrations for this datetime %s', |
115 | 115 | 'event_espresso' |
116 | 116 | ), |
117 | 117 | $datetime->name() |
118 | - ) . '">' . __('View Registrations', 'event_espresso') . '</a>', |
|
118 | + ).'">'.__('View Registrations', 'event_espresso').'</a>', |
|
119 | 119 | ) |
120 | 120 | ); |
121 | 121 | } |
@@ -1,5 +1,5 @@ discard block |
||
1 | 1 | <?php if ( ! defined('ABSPATH')) { |
2 | - exit('No direct script access allowed'); |
|
2 | + exit('No direct script access allowed'); |
|
3 | 3 | } |
4 | 4 | /* |
5 | 5 | Plugin Name: Event Espresso |
@@ -40,243 +40,243 @@ discard block |
||
40 | 40 | * @since 4.0 |
41 | 41 | */ |
42 | 42 | if (function_exists('espresso_version')) { |
43 | - /** |
|
44 | - * espresso_duplicate_plugin_error |
|
45 | - * displays if more than one version of EE is activated at the same time |
|
46 | - */ |
|
47 | - function espresso_duplicate_plugin_error() |
|
48 | - { |
|
49 | - ?> |
|
43 | + /** |
|
44 | + * espresso_duplicate_plugin_error |
|
45 | + * displays if more than one version of EE is activated at the same time |
|
46 | + */ |
|
47 | + function espresso_duplicate_plugin_error() |
|
48 | + { |
|
49 | + ?> |
|
50 | 50 | <div class="error"> |
51 | 51 | <p> |
52 | 52 | <?php echo esc_html__( |
53 | - 'Can not run multiple versions of Event Espresso! One version has been automatically deactivated. Please verify that you have the correct version you want still active.', |
|
54 | - 'event_espresso' |
|
55 | - ); ?> |
|
53 | + 'Can not run multiple versions of Event Espresso! One version has been automatically deactivated. Please verify that you have the correct version you want still active.', |
|
54 | + 'event_espresso' |
|
55 | + ); ?> |
|
56 | 56 | </p> |
57 | 57 | </div> |
58 | 58 | <?php |
59 | - espresso_deactivate_plugin(plugin_basename(__FILE__)); |
|
60 | - } |
|
59 | + espresso_deactivate_plugin(plugin_basename(__FILE__)); |
|
60 | + } |
|
61 | 61 | |
62 | - add_action('admin_notices', 'espresso_duplicate_plugin_error', 1); |
|
62 | + add_action('admin_notices', 'espresso_duplicate_plugin_error', 1); |
|
63 | 63 | } else { |
64 | - define('EE_MIN_PHP_VER_REQUIRED', '5.3.9'); |
|
65 | - if ( ! version_compare(PHP_VERSION, EE_MIN_PHP_VER_REQUIRED, '>=')) { |
|
66 | - /** |
|
67 | - * espresso_minimum_php_version_error |
|
68 | - * |
|
69 | - * @return void |
|
70 | - */ |
|
71 | - function espresso_minimum_php_version_error() |
|
72 | - { |
|
73 | - ?> |
|
64 | + define('EE_MIN_PHP_VER_REQUIRED', '5.3.9'); |
|
65 | + if ( ! version_compare(PHP_VERSION, EE_MIN_PHP_VER_REQUIRED, '>=')) { |
|
66 | + /** |
|
67 | + * espresso_minimum_php_version_error |
|
68 | + * |
|
69 | + * @return void |
|
70 | + */ |
|
71 | + function espresso_minimum_php_version_error() |
|
72 | + { |
|
73 | + ?> |
|
74 | 74 | <div class="error"> |
75 | 75 | <p> |
76 | 76 | <?php |
77 | - printf( |
|
78 | - esc_html__( |
|
79 | - 'We\'re sorry, but Event Espresso requires PHP version %1$s or greater in order to operate. You are currently running version %2$s.%3$sIn order to update your version of PHP, you will need to contact your current hosting provider.%3$sFor information on stable PHP versions, please go to %4$s.', |
|
80 | - 'event_espresso' |
|
81 | - ), |
|
82 | - EE_MIN_PHP_VER_REQUIRED, |
|
83 | - PHP_VERSION, |
|
84 | - '<br/>', |
|
85 | - '<a href="http://php.net/downloads.php">http://php.net/downloads.php</a>' |
|
86 | - ); |
|
87 | - ?> |
|
77 | + printf( |
|
78 | + esc_html__( |
|
79 | + 'We\'re sorry, but Event Espresso requires PHP version %1$s or greater in order to operate. You are currently running version %2$s.%3$sIn order to update your version of PHP, you will need to contact your current hosting provider.%3$sFor information on stable PHP versions, please go to %4$s.', |
|
80 | + 'event_espresso' |
|
81 | + ), |
|
82 | + EE_MIN_PHP_VER_REQUIRED, |
|
83 | + PHP_VERSION, |
|
84 | + '<br/>', |
|
85 | + '<a href="http://php.net/downloads.php">http://php.net/downloads.php</a>' |
|
86 | + ); |
|
87 | + ?> |
|
88 | 88 | </p> |
89 | 89 | </div> |
90 | 90 | <?php |
91 | - espresso_deactivate_plugin(plugin_basename(__FILE__)); |
|
92 | - } |
|
91 | + espresso_deactivate_plugin(plugin_basename(__FILE__)); |
|
92 | + } |
|
93 | 93 | |
94 | - add_action('admin_notices', 'espresso_minimum_php_version_error', 1); |
|
95 | - } else { |
|
96 | - /** |
|
97 | - * espresso_version |
|
98 | - * Returns the plugin version |
|
99 | - * |
|
100 | - * @return string |
|
101 | - */ |
|
102 | - function espresso_version() |
|
103 | - { |
|
104 | - return apply_filters('FHEE__espresso__espresso_version', '4.9.46.rc.013'); |
|
105 | - } |
|
94 | + add_action('admin_notices', 'espresso_minimum_php_version_error', 1); |
|
95 | + } else { |
|
96 | + /** |
|
97 | + * espresso_version |
|
98 | + * Returns the plugin version |
|
99 | + * |
|
100 | + * @return string |
|
101 | + */ |
|
102 | + function espresso_version() |
|
103 | + { |
|
104 | + return apply_filters('FHEE__espresso__espresso_version', '4.9.46.rc.013'); |
|
105 | + } |
|
106 | 106 | |
107 | - // define versions |
|
108 | - define('EVENT_ESPRESSO_VERSION', espresso_version()); |
|
109 | - define('EE_MIN_WP_VER_REQUIRED', '4.1'); |
|
110 | - define('EE_MIN_WP_VER_RECOMMENDED', '4.4.2'); |
|
111 | - define('EE_MIN_PHP_VER_RECOMMENDED', '5.4.44'); |
|
112 | - define('EVENT_ESPRESSO_MAIN_FILE', __FILE__); |
|
113 | - //used to be DIRECTORY_SEPARATOR, but that caused issues on windows |
|
114 | - if ( ! defined('DS')) { |
|
115 | - define('DS', '/'); |
|
116 | - } |
|
117 | - if ( ! defined('PS')) { |
|
118 | - define('PS', PATH_SEPARATOR); |
|
119 | - } |
|
120 | - if ( ! defined('SP')) { |
|
121 | - define('SP', ' '); |
|
122 | - } |
|
123 | - if ( ! defined('EENL')) { |
|
124 | - define('EENL', "\n"); |
|
125 | - } |
|
126 | - define('EE_SUPPORT_EMAIL', '[email protected]'); |
|
127 | - // define the plugin directory and URL |
|
128 | - define('EE_PLUGIN_BASENAME', plugin_basename(EVENT_ESPRESSO_MAIN_FILE)); |
|
129 | - define('EE_PLUGIN_DIR_PATH', plugin_dir_path(EVENT_ESPRESSO_MAIN_FILE)); |
|
130 | - define('EE_PLUGIN_DIR_URL', plugin_dir_url(EVENT_ESPRESSO_MAIN_FILE)); |
|
131 | - // main root folder paths |
|
132 | - define('EE_ADMIN_PAGES', EE_PLUGIN_DIR_PATH . 'admin_pages' . DS); |
|
133 | - define('EE_CORE', EE_PLUGIN_DIR_PATH . 'core' . DS); |
|
134 | - define('EE_MODULES', EE_PLUGIN_DIR_PATH . 'modules' . DS); |
|
135 | - define('EE_PUBLIC', EE_PLUGIN_DIR_PATH . 'public' . DS); |
|
136 | - define('EE_SHORTCODES', EE_PLUGIN_DIR_PATH . 'shortcodes' . DS); |
|
137 | - define('EE_WIDGETS', EE_PLUGIN_DIR_PATH . 'widgets' . DS); |
|
138 | - define('EE_PAYMENT_METHODS', EE_PLUGIN_DIR_PATH . 'payment_methods' . DS); |
|
139 | - define('EE_CAFF_PATH', EE_PLUGIN_DIR_PATH . 'caffeinated' . DS); |
|
140 | - // core system paths |
|
141 | - define('EE_ADMIN', EE_CORE . 'admin' . DS); |
|
142 | - define('EE_CPTS', EE_CORE . 'CPTs' . DS); |
|
143 | - define('EE_CLASSES', EE_CORE . 'db_classes' . DS); |
|
144 | - define('EE_INTERFACES', EE_CORE . 'interfaces' . DS); |
|
145 | - define('EE_BUSINESS', EE_CORE . 'business' . DS); |
|
146 | - define('EE_MODELS', EE_CORE . 'db_models' . DS); |
|
147 | - define('EE_HELPERS', EE_CORE . 'helpers' . DS); |
|
148 | - define('EE_LIBRARIES', EE_CORE . 'libraries' . DS); |
|
149 | - define('EE_TEMPLATES', EE_CORE . 'templates' . DS); |
|
150 | - define('EE_THIRD_PARTY', EE_CORE . 'third_party_libs' . DS); |
|
151 | - define('EE_GLOBAL_ASSETS', EE_TEMPLATES . 'global_assets' . DS); |
|
152 | - define('EE_FORM_SECTIONS', EE_LIBRARIES . 'form_sections' . DS); |
|
153 | - // gateways |
|
154 | - define('EE_GATEWAYS', EE_MODULES . 'gateways' . DS); |
|
155 | - define('EE_GATEWAYS_URL', EE_PLUGIN_DIR_URL . 'modules' . DS . 'gateways' . DS); |
|
156 | - // asset URL paths |
|
157 | - define('EE_TEMPLATES_URL', EE_PLUGIN_DIR_URL . 'core' . DS . 'templates' . DS); |
|
158 | - define('EE_GLOBAL_ASSETS_URL', EE_TEMPLATES_URL . 'global_assets' . DS); |
|
159 | - define('EE_IMAGES_URL', EE_GLOBAL_ASSETS_URL . 'images' . DS); |
|
160 | - define('EE_THIRD_PARTY_URL', EE_PLUGIN_DIR_URL . 'core' . DS . 'third_party_libs' . DS); |
|
161 | - define('EE_HELPERS_ASSETS', EE_PLUGIN_DIR_URL . 'core/helpers/assets/'); |
|
162 | - define('EE_LIBRARIES_URL', EE_PLUGIN_DIR_URL . 'core/libraries/'); |
|
163 | - // define upload paths |
|
164 | - $uploads = wp_upload_dir(); |
|
165 | - // define the uploads directory and URL |
|
166 | - define('EVENT_ESPRESSO_UPLOAD_DIR', $uploads['basedir'] . DS . 'espresso' . DS); |
|
167 | - define('EVENT_ESPRESSO_UPLOAD_URL', $uploads['baseurl'] . DS . 'espresso' . DS); |
|
168 | - // define the templates directory and URL |
|
169 | - define('EVENT_ESPRESSO_TEMPLATE_DIR', $uploads['basedir'] . DS . 'espresso' . DS . 'templates' . DS); |
|
170 | - define('EVENT_ESPRESSO_TEMPLATE_URL', $uploads['baseurl'] . DS . 'espresso' . DS . 'templates' . DS); |
|
171 | - // define the gateway directory and URL |
|
172 | - define('EVENT_ESPRESSO_GATEWAY_DIR', $uploads['basedir'] . DS . 'espresso' . DS . 'gateways' . DS); |
|
173 | - define('EVENT_ESPRESSO_GATEWAY_URL', $uploads['baseurl'] . DS . 'espresso' . DS . 'gateways' . DS); |
|
174 | - // languages folder/path |
|
175 | - define('EE_LANGUAGES_SAFE_LOC', '..' . DS . 'uploads' . DS . 'espresso' . DS . 'languages' . DS); |
|
176 | - define('EE_LANGUAGES_SAFE_DIR', EVENT_ESPRESSO_UPLOAD_DIR . 'languages' . DS); |
|
177 | - //check for dompdf fonts in uploads |
|
178 | - if (file_exists(EVENT_ESPRESSO_UPLOAD_DIR . 'fonts' . DS)) { |
|
179 | - define('DOMPDF_FONT_DIR', EVENT_ESPRESSO_UPLOAD_DIR . 'fonts' . DS); |
|
180 | - } |
|
181 | - //ajax constants |
|
182 | - define( |
|
183 | - 'EE_FRONT_AJAX', |
|
184 | - isset($_REQUEST['ee_front_ajax']) || isset($_REQUEST['data']['ee_front_ajax']) ? true : false |
|
185 | - ); |
|
186 | - define( |
|
187 | - 'EE_ADMIN_AJAX', |
|
188 | - isset($_REQUEST['ee_admin_ajax']) || isset($_REQUEST['data']['ee_admin_ajax']) ? true : false |
|
189 | - ); |
|
190 | - //just a handy constant occasionally needed for finding values representing infinity in the DB |
|
191 | - //you're better to use this than its straight value (currently -1) in case you ever |
|
192 | - //want to change its default value! or find when -1 means infinity |
|
193 | - define('EE_INF_IN_DB', -1); |
|
194 | - define('EE_INF', INF > (float)PHP_INT_MAX ? INF : PHP_INT_MAX); |
|
195 | - define('EE_DEBUG', false); |
|
196 | - // for older WP versions |
|
197 | - if ( ! defined('MONTH_IN_SECONDS')) { |
|
198 | - define('MONTH_IN_SECONDS', DAY_IN_SECONDS * 30); |
|
199 | - } |
|
200 | - /** |
|
201 | - * espresso_plugin_activation |
|
202 | - * adds a wp-option to indicate that EE has been activated via the WP admin plugins page |
|
203 | - */ |
|
204 | - function espresso_plugin_activation() |
|
205 | - { |
|
206 | - update_option('ee_espresso_activation', true); |
|
207 | - } |
|
107 | + // define versions |
|
108 | + define('EVENT_ESPRESSO_VERSION', espresso_version()); |
|
109 | + define('EE_MIN_WP_VER_REQUIRED', '4.1'); |
|
110 | + define('EE_MIN_WP_VER_RECOMMENDED', '4.4.2'); |
|
111 | + define('EE_MIN_PHP_VER_RECOMMENDED', '5.4.44'); |
|
112 | + define('EVENT_ESPRESSO_MAIN_FILE', __FILE__); |
|
113 | + //used to be DIRECTORY_SEPARATOR, but that caused issues on windows |
|
114 | + if ( ! defined('DS')) { |
|
115 | + define('DS', '/'); |
|
116 | + } |
|
117 | + if ( ! defined('PS')) { |
|
118 | + define('PS', PATH_SEPARATOR); |
|
119 | + } |
|
120 | + if ( ! defined('SP')) { |
|
121 | + define('SP', ' '); |
|
122 | + } |
|
123 | + if ( ! defined('EENL')) { |
|
124 | + define('EENL', "\n"); |
|
125 | + } |
|
126 | + define('EE_SUPPORT_EMAIL', '[email protected]'); |
|
127 | + // define the plugin directory and URL |
|
128 | + define('EE_PLUGIN_BASENAME', plugin_basename(EVENT_ESPRESSO_MAIN_FILE)); |
|
129 | + define('EE_PLUGIN_DIR_PATH', plugin_dir_path(EVENT_ESPRESSO_MAIN_FILE)); |
|
130 | + define('EE_PLUGIN_DIR_URL', plugin_dir_url(EVENT_ESPRESSO_MAIN_FILE)); |
|
131 | + // main root folder paths |
|
132 | + define('EE_ADMIN_PAGES', EE_PLUGIN_DIR_PATH . 'admin_pages' . DS); |
|
133 | + define('EE_CORE', EE_PLUGIN_DIR_PATH . 'core' . DS); |
|
134 | + define('EE_MODULES', EE_PLUGIN_DIR_PATH . 'modules' . DS); |
|
135 | + define('EE_PUBLIC', EE_PLUGIN_DIR_PATH . 'public' . DS); |
|
136 | + define('EE_SHORTCODES', EE_PLUGIN_DIR_PATH . 'shortcodes' . DS); |
|
137 | + define('EE_WIDGETS', EE_PLUGIN_DIR_PATH . 'widgets' . DS); |
|
138 | + define('EE_PAYMENT_METHODS', EE_PLUGIN_DIR_PATH . 'payment_methods' . DS); |
|
139 | + define('EE_CAFF_PATH', EE_PLUGIN_DIR_PATH . 'caffeinated' . DS); |
|
140 | + // core system paths |
|
141 | + define('EE_ADMIN', EE_CORE . 'admin' . DS); |
|
142 | + define('EE_CPTS', EE_CORE . 'CPTs' . DS); |
|
143 | + define('EE_CLASSES', EE_CORE . 'db_classes' . DS); |
|
144 | + define('EE_INTERFACES', EE_CORE . 'interfaces' . DS); |
|
145 | + define('EE_BUSINESS', EE_CORE . 'business' . DS); |
|
146 | + define('EE_MODELS', EE_CORE . 'db_models' . DS); |
|
147 | + define('EE_HELPERS', EE_CORE . 'helpers' . DS); |
|
148 | + define('EE_LIBRARIES', EE_CORE . 'libraries' . DS); |
|
149 | + define('EE_TEMPLATES', EE_CORE . 'templates' . DS); |
|
150 | + define('EE_THIRD_PARTY', EE_CORE . 'third_party_libs' . DS); |
|
151 | + define('EE_GLOBAL_ASSETS', EE_TEMPLATES . 'global_assets' . DS); |
|
152 | + define('EE_FORM_SECTIONS', EE_LIBRARIES . 'form_sections' . DS); |
|
153 | + // gateways |
|
154 | + define('EE_GATEWAYS', EE_MODULES . 'gateways' . DS); |
|
155 | + define('EE_GATEWAYS_URL', EE_PLUGIN_DIR_URL . 'modules' . DS . 'gateways' . DS); |
|
156 | + // asset URL paths |
|
157 | + define('EE_TEMPLATES_URL', EE_PLUGIN_DIR_URL . 'core' . DS . 'templates' . DS); |
|
158 | + define('EE_GLOBAL_ASSETS_URL', EE_TEMPLATES_URL . 'global_assets' . DS); |
|
159 | + define('EE_IMAGES_URL', EE_GLOBAL_ASSETS_URL . 'images' . DS); |
|
160 | + define('EE_THIRD_PARTY_URL', EE_PLUGIN_DIR_URL . 'core' . DS . 'third_party_libs' . DS); |
|
161 | + define('EE_HELPERS_ASSETS', EE_PLUGIN_DIR_URL . 'core/helpers/assets/'); |
|
162 | + define('EE_LIBRARIES_URL', EE_PLUGIN_DIR_URL . 'core/libraries/'); |
|
163 | + // define upload paths |
|
164 | + $uploads = wp_upload_dir(); |
|
165 | + // define the uploads directory and URL |
|
166 | + define('EVENT_ESPRESSO_UPLOAD_DIR', $uploads['basedir'] . DS . 'espresso' . DS); |
|
167 | + define('EVENT_ESPRESSO_UPLOAD_URL', $uploads['baseurl'] . DS . 'espresso' . DS); |
|
168 | + // define the templates directory and URL |
|
169 | + define('EVENT_ESPRESSO_TEMPLATE_DIR', $uploads['basedir'] . DS . 'espresso' . DS . 'templates' . DS); |
|
170 | + define('EVENT_ESPRESSO_TEMPLATE_URL', $uploads['baseurl'] . DS . 'espresso' . DS . 'templates' . DS); |
|
171 | + // define the gateway directory and URL |
|
172 | + define('EVENT_ESPRESSO_GATEWAY_DIR', $uploads['basedir'] . DS . 'espresso' . DS . 'gateways' . DS); |
|
173 | + define('EVENT_ESPRESSO_GATEWAY_URL', $uploads['baseurl'] . DS . 'espresso' . DS . 'gateways' . DS); |
|
174 | + // languages folder/path |
|
175 | + define('EE_LANGUAGES_SAFE_LOC', '..' . DS . 'uploads' . DS . 'espresso' . DS . 'languages' . DS); |
|
176 | + define('EE_LANGUAGES_SAFE_DIR', EVENT_ESPRESSO_UPLOAD_DIR . 'languages' . DS); |
|
177 | + //check for dompdf fonts in uploads |
|
178 | + if (file_exists(EVENT_ESPRESSO_UPLOAD_DIR . 'fonts' . DS)) { |
|
179 | + define('DOMPDF_FONT_DIR', EVENT_ESPRESSO_UPLOAD_DIR . 'fonts' . DS); |
|
180 | + } |
|
181 | + //ajax constants |
|
182 | + define( |
|
183 | + 'EE_FRONT_AJAX', |
|
184 | + isset($_REQUEST['ee_front_ajax']) || isset($_REQUEST['data']['ee_front_ajax']) ? true : false |
|
185 | + ); |
|
186 | + define( |
|
187 | + 'EE_ADMIN_AJAX', |
|
188 | + isset($_REQUEST['ee_admin_ajax']) || isset($_REQUEST['data']['ee_admin_ajax']) ? true : false |
|
189 | + ); |
|
190 | + //just a handy constant occasionally needed for finding values representing infinity in the DB |
|
191 | + //you're better to use this than its straight value (currently -1) in case you ever |
|
192 | + //want to change its default value! or find when -1 means infinity |
|
193 | + define('EE_INF_IN_DB', -1); |
|
194 | + define('EE_INF', INF > (float)PHP_INT_MAX ? INF : PHP_INT_MAX); |
|
195 | + define('EE_DEBUG', false); |
|
196 | + // for older WP versions |
|
197 | + if ( ! defined('MONTH_IN_SECONDS')) { |
|
198 | + define('MONTH_IN_SECONDS', DAY_IN_SECONDS * 30); |
|
199 | + } |
|
200 | + /** |
|
201 | + * espresso_plugin_activation |
|
202 | + * adds a wp-option to indicate that EE has been activated via the WP admin plugins page |
|
203 | + */ |
|
204 | + function espresso_plugin_activation() |
|
205 | + { |
|
206 | + update_option('ee_espresso_activation', true); |
|
207 | + } |
|
208 | 208 | |
209 | - register_activation_hook(EVENT_ESPRESSO_MAIN_FILE, 'espresso_plugin_activation'); |
|
210 | - /** |
|
211 | - * espresso_load_error_handling |
|
212 | - * this function loads EE's class for handling exceptions and errors |
|
213 | - */ |
|
214 | - function espresso_load_error_handling() |
|
215 | - { |
|
216 | - // load debugging tools |
|
217 | - if (WP_DEBUG === true && is_readable(EE_HELPERS . 'EEH_Debug_Tools.helper.php')) { |
|
218 | - require_once(EE_HELPERS . 'EEH_Debug_Tools.helper.php'); |
|
219 | - EEH_Debug_Tools::instance(); |
|
220 | - } |
|
221 | - // load error handling |
|
222 | - if (is_readable(EE_CORE . 'EE_Error.core.php')) { |
|
223 | - require_once(EE_CORE . 'EE_Error.core.php'); |
|
224 | - } else { |
|
225 | - wp_die(esc_html__('The EE_Error core class could not be loaded.', 'event_espresso')); |
|
226 | - } |
|
227 | - } |
|
209 | + register_activation_hook(EVENT_ESPRESSO_MAIN_FILE, 'espresso_plugin_activation'); |
|
210 | + /** |
|
211 | + * espresso_load_error_handling |
|
212 | + * this function loads EE's class for handling exceptions and errors |
|
213 | + */ |
|
214 | + function espresso_load_error_handling() |
|
215 | + { |
|
216 | + // load debugging tools |
|
217 | + if (WP_DEBUG === true && is_readable(EE_HELPERS . 'EEH_Debug_Tools.helper.php')) { |
|
218 | + require_once(EE_HELPERS . 'EEH_Debug_Tools.helper.php'); |
|
219 | + EEH_Debug_Tools::instance(); |
|
220 | + } |
|
221 | + // load error handling |
|
222 | + if (is_readable(EE_CORE . 'EE_Error.core.php')) { |
|
223 | + require_once(EE_CORE . 'EE_Error.core.php'); |
|
224 | + } else { |
|
225 | + wp_die(esc_html__('The EE_Error core class could not be loaded.', 'event_espresso')); |
|
226 | + } |
|
227 | + } |
|
228 | 228 | |
229 | - /** |
|
230 | - * espresso_load_required |
|
231 | - * given a class name and path, this function will load that file or throw an exception |
|
232 | - * |
|
233 | - * @param string $classname |
|
234 | - * @param string $full_path_to_file |
|
235 | - * @throws EE_Error |
|
236 | - */ |
|
237 | - function espresso_load_required($classname, $full_path_to_file) |
|
238 | - { |
|
239 | - static $error_handling_loaded = false; |
|
240 | - if ( ! $error_handling_loaded) { |
|
241 | - espresso_load_error_handling(); |
|
242 | - $error_handling_loaded = true; |
|
243 | - } |
|
244 | - if (is_readable($full_path_to_file)) { |
|
245 | - require_once($full_path_to_file); |
|
246 | - } else { |
|
247 | - throw new EE_Error ( |
|
248 | - sprintf( |
|
249 | - esc_html__( |
|
250 | - 'The %s class file could not be located or is not readable due to file permissions.', |
|
251 | - 'event_espresso' |
|
252 | - ), |
|
253 | - $classname |
|
254 | - ) |
|
255 | - ); |
|
256 | - } |
|
257 | - } |
|
229 | + /** |
|
230 | + * espresso_load_required |
|
231 | + * given a class name and path, this function will load that file or throw an exception |
|
232 | + * |
|
233 | + * @param string $classname |
|
234 | + * @param string $full_path_to_file |
|
235 | + * @throws EE_Error |
|
236 | + */ |
|
237 | + function espresso_load_required($classname, $full_path_to_file) |
|
238 | + { |
|
239 | + static $error_handling_loaded = false; |
|
240 | + if ( ! $error_handling_loaded) { |
|
241 | + espresso_load_error_handling(); |
|
242 | + $error_handling_loaded = true; |
|
243 | + } |
|
244 | + if (is_readable($full_path_to_file)) { |
|
245 | + require_once($full_path_to_file); |
|
246 | + } else { |
|
247 | + throw new EE_Error ( |
|
248 | + sprintf( |
|
249 | + esc_html__( |
|
250 | + 'The %s class file could not be located or is not readable due to file permissions.', |
|
251 | + 'event_espresso' |
|
252 | + ), |
|
253 | + $classname |
|
254 | + ) |
|
255 | + ); |
|
256 | + } |
|
257 | + } |
|
258 | 258 | |
259 | - espresso_load_required('EEH_Base', EE_CORE . 'helpers' . DS . 'EEH_Base.helper.php'); |
|
260 | - espresso_load_required('EEH_File', EE_CORE . 'helpers' . DS . 'EEH_File.helper.php'); |
|
261 | - espresso_load_required('EE_Bootstrap', EE_CORE . 'EE_Bootstrap.core.php'); |
|
262 | - new EE_Bootstrap(); |
|
263 | - } |
|
259 | + espresso_load_required('EEH_Base', EE_CORE . 'helpers' . DS . 'EEH_Base.helper.php'); |
|
260 | + espresso_load_required('EEH_File', EE_CORE . 'helpers' . DS . 'EEH_File.helper.php'); |
|
261 | + espresso_load_required('EE_Bootstrap', EE_CORE . 'EE_Bootstrap.core.php'); |
|
262 | + new EE_Bootstrap(); |
|
263 | + } |
|
264 | 264 | } |
265 | 265 | if ( ! function_exists('espresso_deactivate_plugin')) { |
266 | - /** |
|
267 | - * deactivate_plugin |
|
268 | - * usage: espresso_deactivate_plugin( plugin_basename( __FILE__ )); |
|
269 | - * |
|
270 | - * @access public |
|
271 | - * @param string $plugin_basename - the results of plugin_basename( __FILE__ ) for the plugin's main file |
|
272 | - * @return void |
|
273 | - */ |
|
274 | - function espresso_deactivate_plugin($plugin_basename = '') |
|
275 | - { |
|
276 | - if ( ! function_exists('deactivate_plugins')) { |
|
277 | - require_once(ABSPATH . 'wp-admin/includes/plugin.php'); |
|
278 | - } |
|
279 | - unset($_GET['activate'], $_REQUEST['activate']); |
|
280 | - deactivate_plugins($plugin_basename); |
|
281 | - } |
|
266 | + /** |
|
267 | + * deactivate_plugin |
|
268 | + * usage: espresso_deactivate_plugin( plugin_basename( __FILE__ )); |
|
269 | + * |
|
270 | + * @access public |
|
271 | + * @param string $plugin_basename - the results of plugin_basename( __FILE__ ) for the plugin's main file |
|
272 | + * @return void |
|
273 | + */ |
|
274 | + function espresso_deactivate_plugin($plugin_basename = '') |
|
275 | + { |
|
276 | + if ( ! function_exists('deactivate_plugins')) { |
|
277 | + require_once(ABSPATH . 'wp-admin/includes/plugin.php'); |
|
278 | + } |
|
279 | + unset($_GET['activate'], $_REQUEST['activate']); |
|
280 | + deactivate_plugins($plugin_basename); |
|
281 | + } |
|
282 | 282 | } |
283 | 283 | \ No newline at end of file |
@@ -17,303 +17,303 @@ |
||
17 | 17 | class Benchmark |
18 | 18 | { |
19 | 19 | |
20 | - /** |
|
21 | - * array containing the start time for the timers |
|
22 | - */ |
|
23 | - private static $start_times; |
|
24 | - |
|
25 | - /** |
|
26 | - * array containing all the timer'd times, which can be outputted via show_times() |
|
27 | - */ |
|
28 | - private static $times = array(); |
|
29 | - |
|
30 | - /** |
|
31 | - * @var array |
|
32 | - */ |
|
33 | - protected static $memory_usage = array(); |
|
34 | - |
|
35 | - |
|
36 | - |
|
37 | - /** |
|
38 | - * whether to benchmark code or not |
|
39 | - */ |
|
40 | - public static function doNotRun() |
|
41 | - { |
|
42 | - return ! WP_DEBUG || (defined('DOING_AJAX') && DOING_AJAX); |
|
43 | - } |
|
44 | - |
|
45 | - |
|
46 | - |
|
47 | - /** |
|
48 | - * resetTimes |
|
49 | - */ |
|
50 | - public static function resetTimes() |
|
51 | - { |
|
52 | - Benchmark::$times = array(); |
|
53 | - } |
|
54 | - |
|
55 | - |
|
56 | - |
|
57 | - /** |
|
58 | - * Add Benchmark::startTimer() before a block of code you want to measure the performance of |
|
59 | - * |
|
60 | - * @param null $timer_name |
|
61 | - */ |
|
62 | - public static function startTimer($timer_name = null) |
|
63 | - { |
|
64 | - if (Benchmark::doNotRun()) { |
|
65 | - return; |
|
66 | - } |
|
67 | - $timer_name = $timer_name !== '' ? $timer_name : get_called_class(); |
|
68 | - Benchmark::$start_times[$timer_name] = microtime(true); |
|
69 | - } |
|
70 | - |
|
71 | - |
|
72 | - |
|
73 | - /** |
|
74 | - * Add Benchmark::stopTimer() after a block of code you want to measure the performance of |
|
75 | - * |
|
76 | - * @param string $timer_name |
|
77 | - */ |
|
78 | - public static function stopTimer($timer_name = '') |
|
79 | - { |
|
80 | - if (Benchmark::doNotRun()) { |
|
81 | - return; |
|
82 | - } |
|
83 | - $timer_name = $timer_name !== '' ? $timer_name : get_called_class(); |
|
84 | - if (isset(Benchmark::$start_times[$timer_name])) { |
|
85 | - $start_time = Benchmark::$start_times[$timer_name]; |
|
86 | - unset(Benchmark::$start_times[$timer_name]); |
|
87 | - } else { |
|
88 | - $start_time = array_pop(Benchmark::$start_times); |
|
89 | - } |
|
90 | - Benchmark::$times[$timer_name] = number_format(microtime(true) - $start_time, 8); |
|
91 | - } |
|
92 | - |
|
93 | - |
|
94 | - |
|
95 | - /** |
|
96 | - * Measure the memory usage by PHP so far. |
|
97 | - * |
|
98 | - * @param string $label The label to show for this time eg "Start of calling Some_Class::some_function" |
|
99 | - * @param boolean $output_now whether to echo now, or wait until EEH_Debug_Tools::show_times() is called |
|
100 | - * @param bool $formatted |
|
101 | - * @return void |
|
102 | - */ |
|
103 | - public static function measureMemory($label = 'memory usage', $output_now = false, $formatted = true) |
|
104 | - { |
|
105 | - if (Benchmark::doNotRun()) { |
|
106 | - return; |
|
107 | - } |
|
108 | - $memory_used = Benchmark::convert(memory_get_peak_usage(true)); |
|
109 | - Benchmark::$memory_usage[$label] = $memory_used; |
|
110 | - if ($output_now) { |
|
111 | - echo $formatted |
|
112 | - ? "<br>{$label} : {$memory_used}" |
|
113 | - : "\n {$label} : {$memory_used}"; |
|
114 | - } |
|
115 | - } |
|
116 | - |
|
117 | - |
|
118 | - |
|
119 | - /** |
|
120 | - * will display the benchmarking results at shutdown |
|
121 | - * |
|
122 | - * @param bool $formatted |
|
123 | - * @return void |
|
124 | - */ |
|
125 | - public static function displayResultsAtShutdown($formatted = true) |
|
126 | - { |
|
127 | - add_action( |
|
128 | - 'shutdown', |
|
129 | - function () use ($formatted) { |
|
130 | - Benchmark::displayResults(true, $formatted); |
|
131 | - } |
|
132 | - ); |
|
133 | - } |
|
134 | - |
|
135 | - |
|
136 | - |
|
137 | - /** |
|
138 | - * will display the benchmarking results at shutdown |
|
139 | - * |
|
140 | - * @param string $filepath |
|
141 | - * @param bool $formatted |
|
142 | - * @param bool $append |
|
143 | - * @return void |
|
144 | - */ |
|
145 | - public static function writeResultsAtShutdown($filepath = '', $formatted = true, $append = true) |
|
146 | - { |
|
147 | - add_action( |
|
148 | - 'shutdown', |
|
149 | - function () use ($filepath, $formatted, $append) { |
|
150 | - Benchmark::writeResultsToFile($filepath, $formatted, $append); |
|
151 | - } |
|
152 | - ); |
|
153 | - } |
|
154 | - |
|
155 | - |
|
156 | - |
|
157 | - /** |
|
158 | - * @param bool $formatted |
|
159 | - * @return string |
|
160 | - */ |
|
161 | - private static function generateResults($formatted = true) |
|
162 | - { |
|
163 | - if (Benchmark::doNotRun()) { |
|
164 | - return ''; |
|
165 | - } |
|
166 | - $output = ''; |
|
167 | - if (! empty(Benchmark::$times)) { |
|
168 | - $total = 0; |
|
169 | - $output .= $formatted |
|
170 | - ? '<span style="color:#999999; font-size:.8em;">( time in milliseconds )</span><br />' |
|
171 | - : ''; |
|
172 | - foreach (Benchmark::$times as $timer_name => $total_time) { |
|
173 | - $output .= Benchmark::formatTime($timer_name, $total_time, $formatted); |
|
174 | - $output .= $formatted ? '<br />' : "\n"; |
|
175 | - $total += $total_time; |
|
176 | - } |
|
177 | - if($formatted) { |
|
178 | - $output .= '<br />'; |
|
179 | - $output .= '<h4>TOTAL TIME</h4>'; |
|
180 | - $output .= Benchmark::formatTime('', $total, $formatted); |
|
181 | - $output .= '<span style="color:#999999; font-size:.8em;"> milliseconds</span><br />'; |
|
182 | - $output .= '<br />'; |
|
183 | - $output .= '<h5>Performance scale (from best to worse)</h5>'; |
|
184 | - $output .= '<span style="color:mediumpurple">Like wow! How about a Scooby snack?</span><br />'; |
|
185 | - $output .= '<span style="color:deepskyblue">Like...no way man!</span><br />'; |
|
186 | - $output .= '<span style="color:limegreen">Like...groovy!</span><br />'; |
|
187 | - $output .= '<span style="color:gold">Ruh Oh</span><br />'; |
|
188 | - $output .= '<span style="color:darkorange">Zoinks!</span><br />'; |
|
189 | - $output .= '<span style="color:red">Like...HEEELLLP</span><br />'; |
|
190 | - } |
|
191 | - } |
|
192 | - if (! empty(Benchmark::$memory_usage)) { |
|
193 | - $output .= $formatted |
|
194 | - ? '<h5>Memory</h5>' . implode('<br />', Benchmark::$memory_usage) |
|
195 | - : implode("\n", Benchmark::$memory_usage); |
|
196 | - } |
|
197 | - if (empty($output)) { |
|
198 | - return ''; |
|
199 | - } |
|
200 | - $output = $formatted |
|
201 | - ? '<div style="border:1px solid #dddddd; background-color:#ffffff;' |
|
202 | - . (is_admin() |
|
203 | - ? ' margin:2em 2em 2em 180px;' |
|
204 | - : ' margin:2em;') |
|
205 | - . ' padding:2em;">' |
|
206 | - . '<h4>BENCHMARKING</h4>' |
|
207 | - . $output |
|
208 | - . '</div>' |
|
209 | - : $output; |
|
210 | - return $output; |
|
211 | - } |
|
212 | - |
|
213 | - |
|
214 | - |
|
215 | - /** |
|
216 | - * @param bool $echo |
|
217 | - * @param bool $formatted |
|
218 | - * @return string |
|
219 | - */ |
|
220 | - public static function displayResults($echo = true, $formatted = true) |
|
221 | - { |
|
222 | - $results = Benchmark::generateResults($formatted); |
|
223 | - if ($echo) { |
|
224 | - echo $results; |
|
225 | - $results = ''; |
|
226 | - } |
|
227 | - return $results; |
|
228 | - } |
|
229 | - |
|
230 | - |
|
231 | - |
|
232 | - /** |
|
233 | - * @param string $filepath |
|
234 | - * @param bool $formatted |
|
235 | - * @param bool $append |
|
236 | - */ |
|
237 | - public static function writeResultsToFile($filepath = '', $formatted = true, $append = true) |
|
238 | - { |
|
239 | - $filepath = ! empty($filepath) && is_readable(dirname($filepath)) |
|
240 | - ? $filepath |
|
241 | - : ''; |
|
242 | - if( empty($filepath)) { |
|
243 | - $filepath = EVENT_ESPRESSO_UPLOAD_DIR . 'logs/benchmarking-' . date('Y-m-d') . '.html'; |
|
244 | - } |
|
245 | - file_put_contents( |
|
246 | - $filepath, |
|
247 | - "\n" . date('Y-m-d H:i:s') . Benchmark::generateResults($formatted), |
|
248 | - $append ? FILE_APPEND | LOCK_EX : LOCK_EX |
|
249 | - ); |
|
250 | - } |
|
251 | - |
|
252 | - |
|
253 | - |
|
254 | - /** |
|
255 | - * Converts a measure of memory bytes into the most logical units (eg kb, mb, etc) |
|
256 | - * |
|
257 | - * @param int $size |
|
258 | - * @return string |
|
259 | - */ |
|
260 | - public static function convert($size) |
|
261 | - { |
|
262 | - $unit = array('b', 'kb', 'mb', 'gb', 'tb', 'pb'); |
|
263 | - return round( |
|
264 | - $size / pow(1024, $i = floor(log($size, 1024))), |
|
265 | - 2 |
|
266 | - ) . ' ' . $unit[absint($i)]; |
|
267 | - } |
|
268 | - |
|
269 | - |
|
270 | - |
|
271 | - /** |
|
272 | - * @param string $timer_name |
|
273 | - * @param float $total_time |
|
274 | - * @param bool $formatted |
|
275 | - * @return string |
|
276 | - */ |
|
277 | - public static function formatTime($timer_name, $total_time, $formatted = true) |
|
278 | - { |
|
279 | - $total_time *= 1000; |
|
280 | - switch ($total_time) { |
|
281 | - case $total_time > 12500 : |
|
282 | - $color = 'red'; |
|
283 | - $bold = 'bold'; |
|
284 | - break; |
|
285 | - case $total_time > 2500 : |
|
286 | - $color = 'darkorange'; |
|
287 | - $bold = 'bold'; |
|
288 | - break; |
|
289 | - case $total_time > 500 : |
|
290 | - $color = 'gold'; |
|
291 | - $bold = 'bold'; |
|
292 | - break; |
|
293 | - case $total_time > 100 : |
|
294 | - $color = 'limegreen'; |
|
295 | - $bold = 'normal'; |
|
296 | - break; |
|
297 | - case $total_time > 20 : |
|
298 | - $color = 'deepskyblue'; |
|
299 | - $bold = 'normal'; |
|
300 | - break; |
|
301 | - default : |
|
302 | - $color = 'mediumpurple'; |
|
303 | - $bold = 'normal'; |
|
304 | - break; |
|
305 | - } |
|
306 | - return $formatted |
|
307 | - ? '<span style="min-width: 10px; margin:0 1em; color:' |
|
308 | - . $color |
|
309 | - . '; font-weight:' |
|
310 | - . $bold |
|
311 | - . '; font-size:1.2em;">' |
|
312 | - . str_pad(number_format($total_time, 3), 9, '0', STR_PAD_LEFT) |
|
313 | - . '</span> ' |
|
314 | - . $timer_name |
|
315 | - : str_pad(number_format($total_time, 3), 9, '0', STR_PAD_LEFT); |
|
316 | - } |
|
20 | + /** |
|
21 | + * array containing the start time for the timers |
|
22 | + */ |
|
23 | + private static $start_times; |
|
24 | + |
|
25 | + /** |
|
26 | + * array containing all the timer'd times, which can be outputted via show_times() |
|
27 | + */ |
|
28 | + private static $times = array(); |
|
29 | + |
|
30 | + /** |
|
31 | + * @var array |
|
32 | + */ |
|
33 | + protected static $memory_usage = array(); |
|
34 | + |
|
35 | + |
|
36 | + |
|
37 | + /** |
|
38 | + * whether to benchmark code or not |
|
39 | + */ |
|
40 | + public static function doNotRun() |
|
41 | + { |
|
42 | + return ! WP_DEBUG || (defined('DOING_AJAX') && DOING_AJAX); |
|
43 | + } |
|
44 | + |
|
45 | + |
|
46 | + |
|
47 | + /** |
|
48 | + * resetTimes |
|
49 | + */ |
|
50 | + public static function resetTimes() |
|
51 | + { |
|
52 | + Benchmark::$times = array(); |
|
53 | + } |
|
54 | + |
|
55 | + |
|
56 | + |
|
57 | + /** |
|
58 | + * Add Benchmark::startTimer() before a block of code you want to measure the performance of |
|
59 | + * |
|
60 | + * @param null $timer_name |
|
61 | + */ |
|
62 | + public static function startTimer($timer_name = null) |
|
63 | + { |
|
64 | + if (Benchmark::doNotRun()) { |
|
65 | + return; |
|
66 | + } |
|
67 | + $timer_name = $timer_name !== '' ? $timer_name : get_called_class(); |
|
68 | + Benchmark::$start_times[$timer_name] = microtime(true); |
|
69 | + } |
|
70 | + |
|
71 | + |
|
72 | + |
|
73 | + /** |
|
74 | + * Add Benchmark::stopTimer() after a block of code you want to measure the performance of |
|
75 | + * |
|
76 | + * @param string $timer_name |
|
77 | + */ |
|
78 | + public static function stopTimer($timer_name = '') |
|
79 | + { |
|
80 | + if (Benchmark::doNotRun()) { |
|
81 | + return; |
|
82 | + } |
|
83 | + $timer_name = $timer_name !== '' ? $timer_name : get_called_class(); |
|
84 | + if (isset(Benchmark::$start_times[$timer_name])) { |
|
85 | + $start_time = Benchmark::$start_times[$timer_name]; |
|
86 | + unset(Benchmark::$start_times[$timer_name]); |
|
87 | + } else { |
|
88 | + $start_time = array_pop(Benchmark::$start_times); |
|
89 | + } |
|
90 | + Benchmark::$times[$timer_name] = number_format(microtime(true) - $start_time, 8); |
|
91 | + } |
|
92 | + |
|
93 | + |
|
94 | + |
|
95 | + /** |
|
96 | + * Measure the memory usage by PHP so far. |
|
97 | + * |
|
98 | + * @param string $label The label to show for this time eg "Start of calling Some_Class::some_function" |
|
99 | + * @param boolean $output_now whether to echo now, or wait until EEH_Debug_Tools::show_times() is called |
|
100 | + * @param bool $formatted |
|
101 | + * @return void |
|
102 | + */ |
|
103 | + public static function measureMemory($label = 'memory usage', $output_now = false, $formatted = true) |
|
104 | + { |
|
105 | + if (Benchmark::doNotRun()) { |
|
106 | + return; |
|
107 | + } |
|
108 | + $memory_used = Benchmark::convert(memory_get_peak_usage(true)); |
|
109 | + Benchmark::$memory_usage[$label] = $memory_used; |
|
110 | + if ($output_now) { |
|
111 | + echo $formatted |
|
112 | + ? "<br>{$label} : {$memory_used}" |
|
113 | + : "\n {$label} : {$memory_used}"; |
|
114 | + } |
|
115 | + } |
|
116 | + |
|
117 | + |
|
118 | + |
|
119 | + /** |
|
120 | + * will display the benchmarking results at shutdown |
|
121 | + * |
|
122 | + * @param bool $formatted |
|
123 | + * @return void |
|
124 | + */ |
|
125 | + public static function displayResultsAtShutdown($formatted = true) |
|
126 | + { |
|
127 | + add_action( |
|
128 | + 'shutdown', |
|
129 | + function () use ($formatted) { |
|
130 | + Benchmark::displayResults(true, $formatted); |
|
131 | + } |
|
132 | + ); |
|
133 | + } |
|
134 | + |
|
135 | + |
|
136 | + |
|
137 | + /** |
|
138 | + * will display the benchmarking results at shutdown |
|
139 | + * |
|
140 | + * @param string $filepath |
|
141 | + * @param bool $formatted |
|
142 | + * @param bool $append |
|
143 | + * @return void |
|
144 | + */ |
|
145 | + public static function writeResultsAtShutdown($filepath = '', $formatted = true, $append = true) |
|
146 | + { |
|
147 | + add_action( |
|
148 | + 'shutdown', |
|
149 | + function () use ($filepath, $formatted, $append) { |
|
150 | + Benchmark::writeResultsToFile($filepath, $formatted, $append); |
|
151 | + } |
|
152 | + ); |
|
153 | + } |
|
154 | + |
|
155 | + |
|
156 | + |
|
157 | + /** |
|
158 | + * @param bool $formatted |
|
159 | + * @return string |
|
160 | + */ |
|
161 | + private static function generateResults($formatted = true) |
|
162 | + { |
|
163 | + if (Benchmark::doNotRun()) { |
|
164 | + return ''; |
|
165 | + } |
|
166 | + $output = ''; |
|
167 | + if (! empty(Benchmark::$times)) { |
|
168 | + $total = 0; |
|
169 | + $output .= $formatted |
|
170 | + ? '<span style="color:#999999; font-size:.8em;">( time in milliseconds )</span><br />' |
|
171 | + : ''; |
|
172 | + foreach (Benchmark::$times as $timer_name => $total_time) { |
|
173 | + $output .= Benchmark::formatTime($timer_name, $total_time, $formatted); |
|
174 | + $output .= $formatted ? '<br />' : "\n"; |
|
175 | + $total += $total_time; |
|
176 | + } |
|
177 | + if($formatted) { |
|
178 | + $output .= '<br />'; |
|
179 | + $output .= '<h4>TOTAL TIME</h4>'; |
|
180 | + $output .= Benchmark::formatTime('', $total, $formatted); |
|
181 | + $output .= '<span style="color:#999999; font-size:.8em;"> milliseconds</span><br />'; |
|
182 | + $output .= '<br />'; |
|
183 | + $output .= '<h5>Performance scale (from best to worse)</h5>'; |
|
184 | + $output .= '<span style="color:mediumpurple">Like wow! How about a Scooby snack?</span><br />'; |
|
185 | + $output .= '<span style="color:deepskyblue">Like...no way man!</span><br />'; |
|
186 | + $output .= '<span style="color:limegreen">Like...groovy!</span><br />'; |
|
187 | + $output .= '<span style="color:gold">Ruh Oh</span><br />'; |
|
188 | + $output .= '<span style="color:darkorange">Zoinks!</span><br />'; |
|
189 | + $output .= '<span style="color:red">Like...HEEELLLP</span><br />'; |
|
190 | + } |
|
191 | + } |
|
192 | + if (! empty(Benchmark::$memory_usage)) { |
|
193 | + $output .= $formatted |
|
194 | + ? '<h5>Memory</h5>' . implode('<br />', Benchmark::$memory_usage) |
|
195 | + : implode("\n", Benchmark::$memory_usage); |
|
196 | + } |
|
197 | + if (empty($output)) { |
|
198 | + return ''; |
|
199 | + } |
|
200 | + $output = $formatted |
|
201 | + ? '<div style="border:1px solid #dddddd; background-color:#ffffff;' |
|
202 | + . (is_admin() |
|
203 | + ? ' margin:2em 2em 2em 180px;' |
|
204 | + : ' margin:2em;') |
|
205 | + . ' padding:2em;">' |
|
206 | + . '<h4>BENCHMARKING</h4>' |
|
207 | + . $output |
|
208 | + . '</div>' |
|
209 | + : $output; |
|
210 | + return $output; |
|
211 | + } |
|
212 | + |
|
213 | + |
|
214 | + |
|
215 | + /** |
|
216 | + * @param bool $echo |
|
217 | + * @param bool $formatted |
|
218 | + * @return string |
|
219 | + */ |
|
220 | + public static function displayResults($echo = true, $formatted = true) |
|
221 | + { |
|
222 | + $results = Benchmark::generateResults($formatted); |
|
223 | + if ($echo) { |
|
224 | + echo $results; |
|
225 | + $results = ''; |
|
226 | + } |
|
227 | + return $results; |
|
228 | + } |
|
229 | + |
|
230 | + |
|
231 | + |
|
232 | + /** |
|
233 | + * @param string $filepath |
|
234 | + * @param bool $formatted |
|
235 | + * @param bool $append |
|
236 | + */ |
|
237 | + public static function writeResultsToFile($filepath = '', $formatted = true, $append = true) |
|
238 | + { |
|
239 | + $filepath = ! empty($filepath) && is_readable(dirname($filepath)) |
|
240 | + ? $filepath |
|
241 | + : ''; |
|
242 | + if( empty($filepath)) { |
|
243 | + $filepath = EVENT_ESPRESSO_UPLOAD_DIR . 'logs/benchmarking-' . date('Y-m-d') . '.html'; |
|
244 | + } |
|
245 | + file_put_contents( |
|
246 | + $filepath, |
|
247 | + "\n" . date('Y-m-d H:i:s') . Benchmark::generateResults($formatted), |
|
248 | + $append ? FILE_APPEND | LOCK_EX : LOCK_EX |
|
249 | + ); |
|
250 | + } |
|
251 | + |
|
252 | + |
|
253 | + |
|
254 | + /** |
|
255 | + * Converts a measure of memory bytes into the most logical units (eg kb, mb, etc) |
|
256 | + * |
|
257 | + * @param int $size |
|
258 | + * @return string |
|
259 | + */ |
|
260 | + public static function convert($size) |
|
261 | + { |
|
262 | + $unit = array('b', 'kb', 'mb', 'gb', 'tb', 'pb'); |
|
263 | + return round( |
|
264 | + $size / pow(1024, $i = floor(log($size, 1024))), |
|
265 | + 2 |
|
266 | + ) . ' ' . $unit[absint($i)]; |
|
267 | + } |
|
268 | + |
|
269 | + |
|
270 | + |
|
271 | + /** |
|
272 | + * @param string $timer_name |
|
273 | + * @param float $total_time |
|
274 | + * @param bool $formatted |
|
275 | + * @return string |
|
276 | + */ |
|
277 | + public static function formatTime($timer_name, $total_time, $formatted = true) |
|
278 | + { |
|
279 | + $total_time *= 1000; |
|
280 | + switch ($total_time) { |
|
281 | + case $total_time > 12500 : |
|
282 | + $color = 'red'; |
|
283 | + $bold = 'bold'; |
|
284 | + break; |
|
285 | + case $total_time > 2500 : |
|
286 | + $color = 'darkorange'; |
|
287 | + $bold = 'bold'; |
|
288 | + break; |
|
289 | + case $total_time > 500 : |
|
290 | + $color = 'gold'; |
|
291 | + $bold = 'bold'; |
|
292 | + break; |
|
293 | + case $total_time > 100 : |
|
294 | + $color = 'limegreen'; |
|
295 | + $bold = 'normal'; |
|
296 | + break; |
|
297 | + case $total_time > 20 : |
|
298 | + $color = 'deepskyblue'; |
|
299 | + $bold = 'normal'; |
|
300 | + break; |
|
301 | + default : |
|
302 | + $color = 'mediumpurple'; |
|
303 | + $bold = 'normal'; |
|
304 | + break; |
|
305 | + } |
|
306 | + return $formatted |
|
307 | + ? '<span style="min-width: 10px; margin:0 1em; color:' |
|
308 | + . $color |
|
309 | + . '; font-weight:' |
|
310 | + . $bold |
|
311 | + . '; font-size:1.2em;">' |
|
312 | + . str_pad(number_format($total_time, 3), 9, '0', STR_PAD_LEFT) |
|
313 | + . '</span> ' |
|
314 | + . $timer_name |
|
315 | + : str_pad(number_format($total_time, 3), 9, '0', STR_PAD_LEFT); |
|
316 | + } |
|
317 | 317 | |
318 | 318 | |
319 | 319 |
@@ -126,7 +126,7 @@ discard block |
||
126 | 126 | { |
127 | 127 | add_action( |
128 | 128 | 'shutdown', |
129 | - function () use ($formatted) { |
|
129 | + function() use ($formatted) { |
|
130 | 130 | Benchmark::displayResults(true, $formatted); |
131 | 131 | } |
132 | 132 | ); |
@@ -146,7 +146,7 @@ discard block |
||
146 | 146 | { |
147 | 147 | add_action( |
148 | 148 | 'shutdown', |
149 | - function () use ($filepath, $formatted, $append) { |
|
149 | + function() use ($filepath, $formatted, $append) { |
|
150 | 150 | Benchmark::writeResultsToFile($filepath, $formatted, $append); |
151 | 151 | } |
152 | 152 | ); |
@@ -164,17 +164,17 @@ discard block |
||
164 | 164 | return ''; |
165 | 165 | } |
166 | 166 | $output = ''; |
167 | - if (! empty(Benchmark::$times)) { |
|
167 | + if ( ! empty(Benchmark::$times)) { |
|
168 | 168 | $total = 0; |
169 | 169 | $output .= $formatted |
170 | 170 | ? '<span style="color:#999999; font-size:.8em;">( time in milliseconds )</span><br />' |
171 | 171 | : ''; |
172 | 172 | foreach (Benchmark::$times as $timer_name => $total_time) { |
173 | 173 | $output .= Benchmark::formatTime($timer_name, $total_time, $formatted); |
174 | - $output .= $formatted ? '<br />' : "\n"; |
|
174 | + $output .= $formatted ? '<br />' : "\n"; |
|
175 | 175 | $total += $total_time; |
176 | 176 | } |
177 | - if($formatted) { |
|
177 | + if ($formatted) { |
|
178 | 178 | $output .= '<br />'; |
179 | 179 | $output .= '<h4>TOTAL TIME</h4>'; |
180 | 180 | $output .= Benchmark::formatTime('', $total, $formatted); |
@@ -189,9 +189,9 @@ discard block |
||
189 | 189 | $output .= '<span style="color:red">Like...HEEELLLP</span><br />'; |
190 | 190 | } |
191 | 191 | } |
192 | - if (! empty(Benchmark::$memory_usage)) { |
|
192 | + if ( ! empty(Benchmark::$memory_usage)) { |
|
193 | 193 | $output .= $formatted |
194 | - ? '<h5>Memory</h5>' . implode('<br />', Benchmark::$memory_usage) |
|
194 | + ? '<h5>Memory</h5>'.implode('<br />', Benchmark::$memory_usage) |
|
195 | 195 | : implode("\n", Benchmark::$memory_usage); |
196 | 196 | } |
197 | 197 | if (empty($output)) { |
@@ -239,12 +239,12 @@ discard block |
||
239 | 239 | $filepath = ! empty($filepath) && is_readable(dirname($filepath)) |
240 | 240 | ? $filepath |
241 | 241 | : ''; |
242 | - if( empty($filepath)) { |
|
243 | - $filepath = EVENT_ESPRESSO_UPLOAD_DIR . 'logs/benchmarking-' . date('Y-m-d') . '.html'; |
|
242 | + if (empty($filepath)) { |
|
243 | + $filepath = EVENT_ESPRESSO_UPLOAD_DIR.'logs/benchmarking-'.date('Y-m-d').'.html'; |
|
244 | 244 | } |
245 | 245 | file_put_contents( |
246 | 246 | $filepath, |
247 | - "\n" . date('Y-m-d H:i:s') . Benchmark::generateResults($formatted), |
|
247 | + "\n".date('Y-m-d H:i:s').Benchmark::generateResults($formatted), |
|
248 | 248 | $append ? FILE_APPEND | LOCK_EX : LOCK_EX |
249 | 249 | ); |
250 | 250 | } |
@@ -263,7 +263,7 @@ discard block |
||
263 | 263 | return round( |
264 | 264 | $size / pow(1024, $i = floor(log($size, 1024))), |
265 | 265 | 2 |
266 | - ) . ' ' . $unit[absint($i)]; |
|
266 | + ).' '.$unit[absint($i)]; |
|
267 | 267 | } |
268 | 268 | |
269 | 269 |
@@ -22,116 +22,116 @@ |
||
22 | 22 | class CachingLoader extends LoaderDecorator |
23 | 23 | { |
24 | 24 | |
25 | - /** |
|
26 | - * @var CollectionInterface $cache |
|
27 | - */ |
|
28 | - protected $cache; |
|
29 | - |
|
30 | - /** |
|
31 | - * @var string $identifier |
|
32 | - */ |
|
33 | - protected $identifier; |
|
34 | - |
|
35 | - |
|
36 | - |
|
37 | - /** |
|
38 | - * CachingLoader constructor. |
|
39 | - * |
|
40 | - * @param LoaderDecoratorInterface $loader |
|
41 | - * @param CollectionInterface $cache |
|
42 | - * @param string $identifier |
|
43 | - * @throws InvalidDataTypeException |
|
44 | - */ |
|
45 | - public function __construct(LoaderDecoratorInterface $loader, CollectionInterface $cache, $identifier = '') |
|
46 | - { |
|
47 | - parent::__construct($loader); |
|
48 | - $this->cache = $cache; |
|
49 | - $this->setIdentifier($identifier); |
|
50 | - if ($this->identifier !== '') { |
|
51 | - // to only clear this cache, and assuming an identifier has been set, simply do the following: |
|
52 | - // do_action('AHEE__EventEspresso\core\services\loaders\CachingLoader__resetCache__IDENTIFIER'); |
|
53 | - // where "IDENTIFIER" = the string that was set during construction |
|
54 | - add_action( |
|
55 | - "AHEE__EventEspresso_core_services_loaders_CachingLoader__resetCache__{$identifier}", |
|
56 | - array($this, 'reset') |
|
57 | - ); |
|
58 | - } |
|
59 | - // to clear ALL caches, simply do the following: |
|
60 | - // do_action('AHEE__EventEspresso_core_services_loaders_CachingLoader__resetCache'); |
|
61 | - add_action( |
|
62 | - 'AHEE__EventEspresso_core_services_loaders_CachingLoader__resetCache', |
|
63 | - array($this, 'reset') |
|
64 | - ); |
|
65 | - } |
|
66 | - |
|
67 | - |
|
68 | - |
|
69 | - /** |
|
70 | - * @return string |
|
71 | - */ |
|
72 | - public function identifier() |
|
73 | - { |
|
74 | - return $this->identifier; |
|
75 | - } |
|
76 | - |
|
77 | - |
|
78 | - |
|
79 | - /** |
|
80 | - * @param string $identifier |
|
81 | - * @throws InvalidDataTypeException |
|
82 | - */ |
|
83 | - private function setIdentifier($identifier) |
|
84 | - { |
|
85 | - if ( ! is_string($identifier)) { |
|
86 | - throw new InvalidDataTypeException('$identifier', $identifier, 'string'); |
|
87 | - } |
|
88 | - $this->identifier = $identifier; |
|
89 | - } |
|
90 | - |
|
91 | - |
|
92 | - |
|
93 | - /** |
|
94 | - * @param string $fqcn |
|
95 | - * @param array $arguments |
|
96 | - * @return mixed |
|
97 | - * @throws InvalidEntityException |
|
98 | - * @throws ServiceNotFoundException |
|
99 | - */ |
|
100 | - public function load($fqcn, $arguments = array()) |
|
101 | - { |
|
102 | - $fqcn = ltrim($fqcn, '\\'); |
|
103 | - // caching can be turned off via the following code: |
|
104 | - // add_filter('FHEE__EventEspresso_core_services_loaders_CachingLoader__load__bypass_cache', '__return_true'); |
|
105 | - if( |
|
106 | - apply_filters( |
|
107 | - 'FHEE__EventEspresso_core_services_loaders_CachingLoader__load__bypass_cache', |
|
108 | - false, |
|
109 | - $this |
|
110 | - ) |
|
111 | - ){ |
|
112 | - return $this->loader->load($fqcn, $arguments); |
|
113 | - } |
|
114 | - $identifier = md5($fqcn . serialize($arguments)); |
|
115 | - if($this->cache->has($identifier)){ |
|
116 | - return $this->cache->get($identifier); |
|
117 | - } |
|
118 | - $object = $this->loader->load($fqcn, $arguments); |
|
119 | - if($object instanceof $fqcn){ |
|
120 | - $this->cache->add($object, $identifier); |
|
121 | - } |
|
122 | - return $object; |
|
123 | - } |
|
124 | - |
|
125 | - |
|
126 | - |
|
127 | - /** |
|
128 | - * empties cache and calls reset() on loader if method exists |
|
129 | - */ |
|
130 | - public function reset() |
|
131 | - { |
|
132 | - $this->cache->trashAndDetachAll(); |
|
133 | - $this->loader->reset(); |
|
134 | - } |
|
25 | + /** |
|
26 | + * @var CollectionInterface $cache |
|
27 | + */ |
|
28 | + protected $cache; |
|
29 | + |
|
30 | + /** |
|
31 | + * @var string $identifier |
|
32 | + */ |
|
33 | + protected $identifier; |
|
34 | + |
|
35 | + |
|
36 | + |
|
37 | + /** |
|
38 | + * CachingLoader constructor. |
|
39 | + * |
|
40 | + * @param LoaderDecoratorInterface $loader |
|
41 | + * @param CollectionInterface $cache |
|
42 | + * @param string $identifier |
|
43 | + * @throws InvalidDataTypeException |
|
44 | + */ |
|
45 | + public function __construct(LoaderDecoratorInterface $loader, CollectionInterface $cache, $identifier = '') |
|
46 | + { |
|
47 | + parent::__construct($loader); |
|
48 | + $this->cache = $cache; |
|
49 | + $this->setIdentifier($identifier); |
|
50 | + if ($this->identifier !== '') { |
|
51 | + // to only clear this cache, and assuming an identifier has been set, simply do the following: |
|
52 | + // do_action('AHEE__EventEspresso\core\services\loaders\CachingLoader__resetCache__IDENTIFIER'); |
|
53 | + // where "IDENTIFIER" = the string that was set during construction |
|
54 | + add_action( |
|
55 | + "AHEE__EventEspresso_core_services_loaders_CachingLoader__resetCache__{$identifier}", |
|
56 | + array($this, 'reset') |
|
57 | + ); |
|
58 | + } |
|
59 | + // to clear ALL caches, simply do the following: |
|
60 | + // do_action('AHEE__EventEspresso_core_services_loaders_CachingLoader__resetCache'); |
|
61 | + add_action( |
|
62 | + 'AHEE__EventEspresso_core_services_loaders_CachingLoader__resetCache', |
|
63 | + array($this, 'reset') |
|
64 | + ); |
|
65 | + } |
|
66 | + |
|
67 | + |
|
68 | + |
|
69 | + /** |
|
70 | + * @return string |
|
71 | + */ |
|
72 | + public function identifier() |
|
73 | + { |
|
74 | + return $this->identifier; |
|
75 | + } |
|
76 | + |
|
77 | + |
|
78 | + |
|
79 | + /** |
|
80 | + * @param string $identifier |
|
81 | + * @throws InvalidDataTypeException |
|
82 | + */ |
|
83 | + private function setIdentifier($identifier) |
|
84 | + { |
|
85 | + if ( ! is_string($identifier)) { |
|
86 | + throw new InvalidDataTypeException('$identifier', $identifier, 'string'); |
|
87 | + } |
|
88 | + $this->identifier = $identifier; |
|
89 | + } |
|
90 | + |
|
91 | + |
|
92 | + |
|
93 | + /** |
|
94 | + * @param string $fqcn |
|
95 | + * @param array $arguments |
|
96 | + * @return mixed |
|
97 | + * @throws InvalidEntityException |
|
98 | + * @throws ServiceNotFoundException |
|
99 | + */ |
|
100 | + public function load($fqcn, $arguments = array()) |
|
101 | + { |
|
102 | + $fqcn = ltrim($fqcn, '\\'); |
|
103 | + // caching can be turned off via the following code: |
|
104 | + // add_filter('FHEE__EventEspresso_core_services_loaders_CachingLoader__load__bypass_cache', '__return_true'); |
|
105 | + if( |
|
106 | + apply_filters( |
|
107 | + 'FHEE__EventEspresso_core_services_loaders_CachingLoader__load__bypass_cache', |
|
108 | + false, |
|
109 | + $this |
|
110 | + ) |
|
111 | + ){ |
|
112 | + return $this->loader->load($fqcn, $arguments); |
|
113 | + } |
|
114 | + $identifier = md5($fqcn . serialize($arguments)); |
|
115 | + if($this->cache->has($identifier)){ |
|
116 | + return $this->cache->get($identifier); |
|
117 | + } |
|
118 | + $object = $this->loader->load($fqcn, $arguments); |
|
119 | + if($object instanceof $fqcn){ |
|
120 | + $this->cache->add($object, $identifier); |
|
121 | + } |
|
122 | + return $object; |
|
123 | + } |
|
124 | + |
|
125 | + |
|
126 | + |
|
127 | + /** |
|
128 | + * empties cache and calls reset() on loader if method exists |
|
129 | + */ |
|
130 | + public function reset() |
|
131 | + { |
|
132 | + $this->cache->trashAndDetachAll(); |
|
133 | + $this->loader->reset(); |
|
134 | + } |
|
135 | 135 | |
136 | 136 | |
137 | 137 | } |