@@ -1,7 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | 3 | use EventEspresso\core\domain\Domain; |
4 | -use EventEspresso\core\domain\entities\notifications\PersistentAdminNotice; |
|
5 | 4 | use EventEspresso\core\domain\services\factories\EmailAddressFactory; |
6 | 5 | use EventEspresso\core\domain\services\validation\email\EmailValidationException; |
7 | 6 | use EventEspresso\core\exceptions\InvalidDataTypeException; |
@@ -26,1256 +26,1256 @@ |
||
26 | 26 | class EED_Core_Rest_Api extends \EED_Module |
27 | 27 | { |
28 | 28 | |
29 | - const ee_api_namespace = Domain::API_NAMESPACE; |
|
29 | + const ee_api_namespace = Domain::API_NAMESPACE; |
|
30 | 30 | |
31 | - const ee_api_namespace_for_regex = 'ee\/v([^/]*)\/'; |
|
32 | - |
|
33 | - const saved_routes_option_names = 'ee_core_routes'; |
|
34 | - |
|
35 | - /** |
|
36 | - * string used in _links response bodies to make them globally unique. |
|
37 | - * |
|
38 | - * @see http://v2.wp-api.org/extending/linking/ |
|
39 | - */ |
|
40 | - const ee_api_link_namespace = 'https://api.eventespresso.com/'; |
|
41 | - |
|
42 | - /** |
|
43 | - * @var CalculatedModelFields |
|
44 | - */ |
|
45 | - protected static $_field_calculator; |
|
46 | - |
|
47 | - |
|
48 | - |
|
49 | - /** |
|
50 | - * @return EED_Core_Rest_Api|EED_Module |
|
51 | - */ |
|
52 | - public static function instance() |
|
53 | - { |
|
54 | - self::$_field_calculator = new CalculatedModelFields(); |
|
55 | - return parent::get_instance(__CLASS__); |
|
56 | - } |
|
57 | - |
|
58 | - |
|
59 | - |
|
60 | - /** |
|
61 | - * set_hooks - for hooking into EE Core, other modules, etc |
|
62 | - * |
|
63 | - * @access public |
|
64 | - * @return void |
|
65 | - */ |
|
66 | - public static function set_hooks() |
|
67 | - { |
|
68 | - self::set_hooks_both(); |
|
69 | - } |
|
70 | - |
|
71 | - |
|
72 | - |
|
73 | - /** |
|
74 | - * set_hooks_admin - for hooking into EE Admin Core, other modules, etc |
|
75 | - * |
|
76 | - * @access public |
|
77 | - * @return void |
|
78 | - */ |
|
79 | - public static function set_hooks_admin() |
|
80 | - { |
|
81 | - self::set_hooks_both(); |
|
82 | - } |
|
83 | - |
|
84 | - |
|
85 | - |
|
86 | - public static function set_hooks_both() |
|
87 | - { |
|
88 | - add_action('rest_api_init', array('EED_Core_Rest_Api', 'register_routes'), 10); |
|
89 | - add_action('rest_api_init', array('EED_Core_Rest_Api', 'set_hooks_rest_api'), 5); |
|
90 | - add_filter('rest_route_data', array('EED_Core_Rest_Api', 'hide_old_endpoints'), 10, 2); |
|
91 | - add_filter('rest_index', |
|
92 | - array('EventEspresso\core\libraries\rest_api\controllers\model\Meta', 'filterEeMetadataIntoIndex')); |
|
93 | - EED_Core_Rest_Api::invalidate_cached_route_data_on_version_change(); |
|
94 | - } |
|
95 | - |
|
96 | - |
|
97 | - |
|
98 | - /** |
|
99 | - * sets up hooks which only need to be included as part of REST API requests; |
|
100 | - * other requests like to the frontend or admin etc don't need them |
|
101 | - * |
|
102 | - * @throws \EE_Error |
|
103 | - */ |
|
104 | - public static function set_hooks_rest_api() |
|
105 | - { |
|
106 | - //set hooks which account for changes made to the API |
|
107 | - EED_Core_Rest_Api::_set_hooks_for_changes(); |
|
108 | - } |
|
109 | - |
|
110 | - |
|
111 | - |
|
112 | - /** |
|
113 | - * public wrapper of _set_hooks_for_changes. |
|
114 | - * Loads all the hooks which make requests to old versions of the API |
|
115 | - * appear the same as they always did |
|
116 | - * |
|
117 | - * @throws EE_Error |
|
118 | - */ |
|
119 | - public static function set_hooks_for_changes() |
|
120 | - { |
|
121 | - self::_set_hooks_for_changes(); |
|
122 | - } |
|
123 | - |
|
124 | - |
|
125 | - |
|
126 | - /** |
|
127 | - * Loads all the hooks which make requests to old versions of the API |
|
128 | - * appear the same as they always did |
|
129 | - * |
|
130 | - * @throws EE_Error |
|
131 | - */ |
|
132 | - protected static function _set_hooks_for_changes() |
|
133 | - { |
|
134 | - $folder_contents = EEH_File::get_contents_of_folders(array(EE_LIBRARIES . 'rest_api' . DS . 'changes'), false); |
|
135 | - foreach ($folder_contents as $classname_in_namespace => $filepath) { |
|
136 | - //ignore the base parent class |
|
137 | - //and legacy named classes |
|
138 | - if ($classname_in_namespace === 'ChangesInBase' |
|
139 | - || strpos($classname_in_namespace, 'Changes_In_') === 0 |
|
140 | - ) { |
|
141 | - continue; |
|
142 | - } |
|
143 | - $full_classname = 'EventEspresso\core\libraries\rest_api\changes\\' . $classname_in_namespace; |
|
144 | - if (class_exists($full_classname)) { |
|
145 | - $instance_of_class = new $full_classname; |
|
146 | - if ($instance_of_class instanceof ChangesInBase) { |
|
147 | - $instance_of_class->setHooks(); |
|
148 | - } |
|
149 | - } |
|
150 | - } |
|
151 | - } |
|
152 | - |
|
153 | - |
|
154 | - |
|
155 | - /** |
|
156 | - * Filters the WP routes to add our EE-related ones. This takes a bit of time |
|
157 | - * so we actually prefer to only do it when an EE plugin is activated or upgraded |
|
158 | - * |
|
159 | - * @throws \EE_Error |
|
160 | - */ |
|
161 | - public static function register_routes() |
|
162 | - { |
|
163 | - foreach (EED_Core_Rest_Api::get_ee_route_data() as $namespace => $relative_routes) { |
|
164 | - foreach ($relative_routes as $relative_route => $data_for_multiple_endpoints) { |
|
165 | - /** |
|
166 | - * @var array $data_for_multiple_endpoints numerically indexed array |
|
167 | - * but can also contain route options like { |
|
168 | - * @type array $schema { |
|
169 | - * @type callable $schema_callback |
|
170 | - * @type array $callback_args arguments that will be passed to the callback, after the |
|
171 | - * WP_REST_Request of course |
|
172 | - * } |
|
173 | - * } |
|
174 | - */ |
|
175 | - //when registering routes, register all the endpoints' data at the same time |
|
176 | - $multiple_endpoint_args = array(); |
|
177 | - foreach ($data_for_multiple_endpoints as $endpoint_key => $data_for_single_endpoint) { |
|
178 | - /** |
|
179 | - * @var array $data_for_single_endpoint { |
|
180 | - * @type callable $callback |
|
181 | - * @type string methods |
|
182 | - * @type array args |
|
183 | - * @type array _links |
|
184 | - * @type array $callback_args arguments that will be passed to the callback, after the |
|
185 | - * WP_REST_Request of course |
|
186 | - * } |
|
187 | - */ |
|
188 | - //skip route options |
|
189 | - if (! is_numeric($endpoint_key)) { |
|
190 | - continue; |
|
191 | - } |
|
192 | - if (! isset($data_for_single_endpoint['callback'], $data_for_single_endpoint['methods'])) { |
|
193 | - throw new EE_Error( |
|
194 | - esc_html__( |
|
195 | - // @codingStandardsIgnoreStart |
|
196 | - 'Endpoint configuration data needs to have entries "callback" (callable) and "methods" (comma-separated list of accepts HTTP methods).', |
|
197 | - // @codingStandardsIgnoreEnd |
|
198 | - 'event_espresso') |
|
199 | - ); |
|
200 | - } |
|
201 | - $callback = $data_for_single_endpoint['callback']; |
|
202 | - $single_endpoint_args = array( |
|
203 | - 'methods' => $data_for_single_endpoint['methods'], |
|
204 | - 'args' => isset($data_for_single_endpoint['args']) ? $data_for_single_endpoint['args'] |
|
205 | - : array(), |
|
206 | - ); |
|
207 | - if (isset($data_for_single_endpoint['_links'])) { |
|
208 | - $single_endpoint_args['_links'] = $data_for_single_endpoint['_links']; |
|
209 | - } |
|
210 | - if (isset($data_for_single_endpoint['callback_args'])) { |
|
211 | - $callback_args = $data_for_single_endpoint['callback_args']; |
|
212 | - $single_endpoint_args['callback'] = function (\WP_REST_Request $request) use ( |
|
213 | - $callback, |
|
214 | - $callback_args |
|
215 | - ) { |
|
216 | - array_unshift($callback_args, $request); |
|
217 | - return call_user_func_array( |
|
218 | - $callback, |
|
219 | - $callback_args |
|
220 | - ); |
|
221 | - }; |
|
222 | - } else { |
|
223 | - $single_endpoint_args['callback'] = $data_for_single_endpoint['callback']; |
|
224 | - } |
|
225 | - $multiple_endpoint_args[] = $single_endpoint_args; |
|
226 | - } |
|
227 | - if (isset($data_for_multiple_endpoints['schema'])) { |
|
228 | - $schema_route_data = $data_for_multiple_endpoints['schema']; |
|
229 | - $schema_callback = $schema_route_data['schema_callback']; |
|
230 | - $callback_args = $schema_route_data['callback_args']; |
|
231 | - $multiple_endpoint_args['schema'] = function () use ($schema_callback, $callback_args) { |
|
232 | - return call_user_func_array( |
|
233 | - $schema_callback, |
|
234 | - $callback_args |
|
235 | - ); |
|
236 | - }; |
|
237 | - } |
|
238 | - register_rest_route( |
|
239 | - $namespace, |
|
240 | - $relative_route, |
|
241 | - $multiple_endpoint_args |
|
242 | - ); |
|
243 | - } |
|
244 | - } |
|
245 | - } |
|
246 | - |
|
247 | - |
|
248 | - |
|
249 | - /** |
|
250 | - * Checks if there was a version change or something that merits invalidating the cached |
|
251 | - * route data. If so, invalidates the cached route data so that it gets refreshed |
|
252 | - * next time the WP API is used |
|
253 | - */ |
|
254 | - public static function invalidate_cached_route_data_on_version_change() |
|
255 | - { |
|
256 | - if (EE_System::instance()->detect_req_type() !== EE_System::req_type_normal) { |
|
257 | - EED_Core_Rest_Api::invalidate_cached_route_data(); |
|
258 | - } |
|
259 | - foreach (EE_Registry::instance()->addons as $addon) { |
|
260 | - if ($addon instanceof EE_Addon && $addon->detect_req_type() !== EE_System::req_type_normal) { |
|
261 | - EED_Core_Rest_Api::invalidate_cached_route_data(); |
|
262 | - } |
|
263 | - } |
|
264 | - } |
|
265 | - |
|
266 | - |
|
267 | - |
|
268 | - /** |
|
269 | - * Removes the cached route data so it will get refreshed next time the WP API is used |
|
270 | - */ |
|
271 | - public static function invalidate_cached_route_data() |
|
272 | - { |
|
273 | - //delete the saved EE REST API routes |
|
274 | - foreach (EED_Core_Rest_Api::versions_served() as $version => $hidden) { |
|
275 | - delete_option(EED_Core_Rest_Api::saved_routes_option_names . $version); |
|
276 | - } |
|
277 | - } |
|
278 | - |
|
279 | - |
|
280 | - |
|
281 | - /** |
|
282 | - * Gets the EE route data |
|
283 | - * |
|
284 | - * @return array top-level key is the namespace, next-level key is the route and its value is array{ |
|
285 | - * @throws \EE_Error |
|
286 | - * @type string|array $callback |
|
287 | - * @type string $methods |
|
288 | - * @type boolean $hidden_endpoint |
|
289 | - * } |
|
290 | - */ |
|
291 | - public static function get_ee_route_data() |
|
292 | - { |
|
293 | - $ee_routes = array(); |
|
294 | - foreach (self::versions_served() as $version => $hidden_endpoints) { |
|
295 | - $ee_routes[self::ee_api_namespace . $version] = self::_get_ee_route_data_for_version( |
|
296 | - $version, |
|
297 | - $hidden_endpoints |
|
298 | - ); |
|
299 | - } |
|
300 | - return $ee_routes; |
|
301 | - } |
|
302 | - |
|
303 | - |
|
304 | - |
|
305 | - /** |
|
306 | - * Gets the EE route data from the wp options if it exists already, |
|
307 | - * otherwise re-generates it and saves it to the option |
|
308 | - * |
|
309 | - * @param string $version |
|
310 | - * @param boolean $hidden_endpoints |
|
311 | - * @return array |
|
312 | - * @throws \EE_Error |
|
313 | - */ |
|
314 | - protected static function _get_ee_route_data_for_version($version, $hidden_endpoints = false) |
|
315 | - { |
|
316 | - $ee_routes = get_option(self::saved_routes_option_names . $version, null); |
|
317 | - if (! $ee_routes || (defined('EE_REST_API_DEBUG_MODE') && EE_REST_API_DEBUG_MODE)) { |
|
318 | - $ee_routes = self::_save_ee_route_data_for_version($version, $hidden_endpoints); |
|
319 | - } |
|
320 | - return $ee_routes; |
|
321 | - } |
|
322 | - |
|
323 | - |
|
324 | - |
|
325 | - /** |
|
326 | - * Saves the EE REST API route data to a wp option and returns it |
|
327 | - * |
|
328 | - * @param string $version |
|
329 | - * @param boolean $hidden_endpoints |
|
330 | - * @return mixed|null |
|
331 | - * @throws \EE_Error |
|
332 | - */ |
|
333 | - protected static function _save_ee_route_data_for_version($version, $hidden_endpoints = false) |
|
334 | - { |
|
335 | - $instance = self::instance(); |
|
336 | - $routes = apply_filters( |
|
337 | - 'EED_Core_Rest_Api__save_ee_route_data_for_version__routes', |
|
338 | - array_replace_recursive( |
|
339 | - $instance->_get_config_route_data_for_version($version, $hidden_endpoints), |
|
340 | - $instance->_get_meta_route_data_for_version($version, $hidden_endpoints), |
|
341 | - $instance->_get_model_route_data_for_version($version, $hidden_endpoints), |
|
342 | - $instance->_get_rpc_route_data_for_version($version, $hidden_endpoints) |
|
343 | - ) |
|
344 | - ); |
|
345 | - $option_name = self::saved_routes_option_names . $version; |
|
346 | - if (get_option($option_name)) { |
|
347 | - update_option($option_name, $routes, true); |
|
348 | - } else { |
|
349 | - add_option($option_name, $routes, null, 'no'); |
|
350 | - } |
|
351 | - return $routes; |
|
352 | - } |
|
353 | - |
|
354 | - |
|
355 | - |
|
356 | - /** |
|
357 | - * Calculates all the EE routes and saves it to a WordPress option so we don't |
|
358 | - * need to calculate it on every request |
|
359 | - * |
|
360 | - * @deprecated since version 4.9.1 |
|
361 | - * @return void |
|
362 | - */ |
|
363 | - public static function save_ee_routes() |
|
364 | - { |
|
365 | - if (EE_Maintenance_Mode::instance()->models_can_query()) { |
|
366 | - $instance = self::instance(); |
|
367 | - $routes = apply_filters( |
|
368 | - 'EED_Core_Rest_Api__save_ee_routes__routes', |
|
369 | - array_replace_recursive( |
|
370 | - $instance->_register_config_routes(), |
|
371 | - $instance->_register_meta_routes(), |
|
372 | - $instance->_register_model_routes(), |
|
373 | - $instance->_register_rpc_routes() |
|
374 | - ) |
|
375 | - ); |
|
376 | - update_option(self::saved_routes_option_names, $routes, true); |
|
377 | - } |
|
378 | - } |
|
379 | - |
|
380 | - |
|
381 | - |
|
382 | - /** |
|
383 | - * Gets all the route information relating to EE models |
|
384 | - * |
|
385 | - * @return array @see get_ee_route_data |
|
386 | - * @deprecated since version 4.9.1 |
|
387 | - */ |
|
388 | - protected function _register_model_routes() |
|
389 | - { |
|
390 | - $model_routes = array(); |
|
391 | - foreach (self::versions_served() as $version => $hidden_endpoint) { |
|
392 | - $model_routes[EED_Core_Rest_Api::ee_api_namespace |
|
393 | - . $version] = $this->_get_config_route_data_for_version($version, $hidden_endpoint); |
|
394 | - } |
|
395 | - return $model_routes; |
|
396 | - } |
|
397 | - |
|
398 | - |
|
399 | - |
|
400 | - /** |
|
401 | - * Decides whether or not to add write endpoints for this model. |
|
402 | - * |
|
403 | - * Currently, this defaults to exclude all global tables and models |
|
404 | - * which would allow inserting WP core data (we don't want to duplicate |
|
405 | - * what WP API does, as it's unnecessary, extra work, and potentially extra bugs) |
|
406 | - * @param EEM_Base $model |
|
407 | - * @return bool |
|
408 | - */ |
|
409 | - public static function should_have_write_endpoints(EEM_Base $model) |
|
410 | - { |
|
411 | - if ($model->is_wp_core_model()){ |
|
412 | - return false; |
|
413 | - } |
|
414 | - foreach($model->get_tables() as $table){ |
|
415 | - if( $table->is_global()){ |
|
416 | - return false; |
|
417 | - } |
|
418 | - } |
|
419 | - return true; |
|
420 | - } |
|
421 | - |
|
422 | - |
|
423 | - |
|
424 | - /** |
|
425 | - * Gets the names of all models which should have plural routes (eg `ee/v4.8.36/events`) |
|
426 | - * in this versioned namespace of EE4 |
|
427 | - * @param $version |
|
428 | - * @return array keys are model names (eg 'Event') and values ar either classnames (eg 'EEM_Event') |
|
429 | - */ |
|
430 | - public static function model_names_with_plural_routes($version){ |
|
431 | - $model_version_info = new ModelVersionInfo($version); |
|
432 | - $models_to_register = $model_version_info->modelsForRequestedVersion(); |
|
433 | - //let's not bother having endpoints for extra metas |
|
434 | - unset( |
|
435 | - $models_to_register['Extra_Meta'], |
|
436 | - $models_to_register['Extra_Join'], |
|
437 | - $models_to_register['Post_Meta'] |
|
438 | - ); |
|
439 | - return apply_filters( |
|
440 | - 'FHEE__EED_Core_REST_API___register_model_routes', |
|
441 | - $models_to_register |
|
442 | - ); |
|
443 | - } |
|
444 | - |
|
445 | - |
|
446 | - |
|
447 | - /** |
|
448 | - * Gets the route data for EE models in the specified version |
|
449 | - * |
|
450 | - * @param string $version |
|
451 | - * @param boolean $hidden_endpoint |
|
452 | - * @return array |
|
453 | - * @throws EE_Error |
|
454 | - */ |
|
455 | - protected function _get_model_route_data_for_version($version, $hidden_endpoint = false) |
|
456 | - { |
|
457 | - $model_routes = array(); |
|
458 | - $model_version_info = new ModelVersionInfo($version); |
|
459 | - foreach (EED_Core_Rest_Api::model_names_with_plural_routes($version) as $model_name => $model_classname) { |
|
460 | - $model = \EE_Registry::instance()->load_model($model_name); |
|
461 | - //if this isn't a valid model then let's skip iterate to the next item in the loop. |
|
462 | - if (! $model instanceof EEM_Base) { |
|
463 | - continue; |
|
464 | - } |
|
465 | - //yes we could just register one route for ALL models, but then they wouldn't show up in the index |
|
466 | - $plural_model_route = EED_Core_Rest_Api::get_collection_route($model); |
|
467 | - $singular_model_route = EED_Core_Rest_Api::get_entity_route($model, '(?P<id>[^\/]+)'); |
|
468 | - $model_routes[$plural_model_route] = array( |
|
469 | - array( |
|
470 | - 'callback' => array( |
|
471 | - 'EventEspresso\core\libraries\rest_api\controllers\model\Read', |
|
472 | - 'handleRequestGetAll', |
|
473 | - ), |
|
474 | - 'callback_args' => array($version, $model_name), |
|
475 | - 'methods' => WP_REST_Server::READABLE, |
|
476 | - 'hidden_endpoint' => $hidden_endpoint, |
|
477 | - 'args' => $this->_get_read_query_params($model, $version), |
|
478 | - '_links' => array( |
|
479 | - 'self' => rest_url(EED_Core_Rest_Api::ee_api_namespace . $version . $singular_model_route), |
|
480 | - ), |
|
481 | - ), |
|
482 | - 'schema' => array( |
|
483 | - 'schema_callback' => array( |
|
484 | - 'EventEspresso\core\libraries\rest_api\controllers\model\Read', |
|
485 | - 'handleSchemaRequest', |
|
486 | - ), |
|
487 | - 'callback_args' => array($version, $model_name), |
|
488 | - ), |
|
489 | - ); |
|
490 | - $model_routes[$singular_model_route] = array( |
|
491 | - array( |
|
492 | - 'callback' => array( |
|
493 | - 'EventEspresso\core\libraries\rest_api\controllers\model\Read', |
|
494 | - 'handleRequestGetOne', |
|
495 | - ), |
|
496 | - 'callback_args' => array($version, $model_name), |
|
497 | - 'methods' => WP_REST_Server::READABLE, |
|
498 | - 'hidden_endpoint' => $hidden_endpoint, |
|
499 | - 'args' => $this->_get_response_selection_query_params($model, $version), |
|
500 | - ), |
|
501 | - ); |
|
502 | - if( apply_filters( |
|
503 | - 'FHEE__EED_Core_Rest_Api___get_model_route_data_for_version__add_write_endpoints', |
|
504 | - EED_Core_Rest_Api::should_have_write_endpoints($model), |
|
505 | - $model |
|
506 | - )){ |
|
507 | - $model_routes[$plural_model_route][] = array( |
|
508 | - 'callback' => array( |
|
509 | - 'EventEspresso\core\libraries\rest_api\controllers\model\Write', |
|
510 | - 'handleRequestInsert', |
|
511 | - ), |
|
512 | - 'callback_args' => array($version, $model_name), |
|
513 | - 'methods' => WP_REST_Server::CREATABLE, |
|
514 | - 'hidden_endpoint' => $hidden_endpoint, |
|
515 | - 'args' => $this->_get_write_params($model_name, $model_version_info, true), |
|
516 | - ); |
|
517 | - $model_routes[$singular_model_route] = array_merge( |
|
518 | - $model_routes[$singular_model_route], |
|
519 | - array( |
|
520 | - array( |
|
521 | - 'callback' => array( |
|
522 | - 'EventEspresso\core\libraries\rest_api\controllers\model\Write', |
|
523 | - 'handleRequestUpdate', |
|
524 | - ), |
|
525 | - 'callback_args' => array($version, $model_name), |
|
526 | - 'methods' => WP_REST_Server::EDITABLE, |
|
527 | - 'hidden_endpoint' => $hidden_endpoint, |
|
528 | - 'args' => $this->_get_write_params($model_name, $model_version_info), |
|
529 | - ), |
|
530 | - array( |
|
531 | - 'callback' => array( |
|
532 | - 'EventEspresso\core\libraries\rest_api\controllers\model\Write', |
|
533 | - 'handleRequestDelete', |
|
534 | - ), |
|
535 | - 'callback_args' => array($version, $model_name), |
|
536 | - 'methods' => WP_REST_Server::DELETABLE, |
|
537 | - 'hidden_endpoint' => $hidden_endpoint, |
|
538 | - 'args' => $this->_get_delete_query_params($model, $version), |
|
539 | - ) |
|
540 | - ) |
|
541 | - ); |
|
542 | - } |
|
543 | - foreach ($model->relation_settings() as $relation_name => $relation_obj) { |
|
544 | - |
|
545 | - $related_route = EED_Core_Rest_Api::get_relation_route_via( |
|
546 | - $model, |
|
547 | - '(?P<id>[^\/]+)', |
|
548 | - $relation_obj |
|
549 | - ); |
|
550 | - $endpoints = array( |
|
551 | - array( |
|
552 | - 'callback' => array( |
|
553 | - 'EventEspresso\core\libraries\rest_api\controllers\model\Read', |
|
554 | - 'handleRequestGetRelated', |
|
555 | - ), |
|
556 | - 'callback_args' => array($version, $model_name, $relation_name), |
|
557 | - 'methods' => WP_REST_Server::READABLE, |
|
558 | - 'hidden_endpoint' => $hidden_endpoint, |
|
559 | - 'args' => $this->_get_read_query_params($relation_obj->get_other_model(), $version), |
|
560 | - ), |
|
561 | - ); |
|
562 | - $model_routes[$related_route] = $endpoints; |
|
563 | - } |
|
564 | - } |
|
565 | - return $model_routes; |
|
566 | - } |
|
567 | - |
|
568 | - |
|
569 | - |
|
570 | - /** |
|
571 | - * Gets the relative URI to a model's REST API plural route, after the EE4 versioned namespace, |
|
572 | - * excluding the preceding slash. |
|
573 | - * Eg you pass get_plural_route_to('Event') = 'events' |
|
574 | - * |
|
575 | - * @param EEM_Base $model |
|
576 | - * @return string |
|
577 | - */ |
|
578 | - public static function get_collection_route(EEM_Base $model) |
|
579 | - { |
|
580 | - return EEH_Inflector::pluralize_and_lower($model->get_this_model_name()); |
|
581 | - } |
|
582 | - |
|
583 | - |
|
584 | - |
|
585 | - /** |
|
586 | - * Gets the relative URI to a model's REST API singular route, after the EE4 versioned namespace, |
|
587 | - * excluding the preceding slash. |
|
588 | - * Eg you pass get_plural_route_to('Event', 12) = 'events/12' |
|
589 | - * |
|
590 | - * @param EEM_Base $model eg Event or Venue |
|
591 | - * @param string $id |
|
592 | - * @return string |
|
593 | - */ |
|
594 | - public static function get_entity_route($model, $id) |
|
595 | - { |
|
596 | - return EED_Core_Rest_Api::get_collection_route($model). '/' . $id; |
|
597 | - } |
|
598 | - |
|
599 | - |
|
600 | - /** |
|
601 | - * Gets the relative URI to a model's REST API singular route, after the EE4 versioned namespace, |
|
602 | - * excluding the preceding slash. |
|
603 | - * Eg you pass get_plural_route_to('Event', 12) = 'events/12' |
|
604 | - * |
|
605 | - * @param EEM_Base $model eg Event or Venue |
|
606 | - * @param string $id |
|
607 | - * @param EE_Model_Relation_Base $relation_obj |
|
608 | - * @return string |
|
609 | - */ |
|
610 | - public static function get_relation_route_via(EEM_Base $model, $id, EE_Model_Relation_Base $relation_obj) |
|
611 | - { |
|
612 | - $related_model_name_endpoint_part = ModelRead::getRelatedEntityName( |
|
613 | - $relation_obj->get_other_model()->get_this_model_name(), |
|
614 | - $relation_obj |
|
615 | - ); |
|
616 | - return EED_Core_Rest_Api::get_entity_route($model, $id) . '/' . $related_model_name_endpoint_part; |
|
617 | - } |
|
618 | - |
|
619 | - |
|
620 | - |
|
621 | - /** |
|
622 | - * Adds onto the $relative_route the EE4 REST API versioned namespace. |
|
623 | - * Eg if given '4.8.36' and 'events', will return 'ee/v4.8.36/events' |
|
624 | - * @param string $relative_route |
|
625 | - * @param string $version |
|
626 | - * @return string |
|
627 | - */ |
|
628 | - public static function get_versioned_route_to($relative_route, $version = '4.8.36'){ |
|
629 | - return '/' . EED_Core_Rest_Api::ee_api_namespace . $version . '/' . $relative_route; |
|
630 | - } |
|
631 | - |
|
632 | - |
|
633 | - |
|
634 | - /** |
|
635 | - * Adds all the RPC-style routes (remote procedure call-like routes, ie |
|
636 | - * routes that don't conform to the traditional REST CRUD-style). |
|
637 | - * |
|
638 | - * @deprecated since 4.9.1 |
|
639 | - */ |
|
640 | - protected function _register_rpc_routes() |
|
641 | - { |
|
642 | - $routes = array(); |
|
643 | - foreach (self::versions_served() as $version => $hidden_endpoint) { |
|
644 | - $routes[self::ee_api_namespace . $version] = $this->_get_rpc_route_data_for_version( |
|
645 | - $version, |
|
646 | - $hidden_endpoint |
|
647 | - ); |
|
648 | - } |
|
649 | - return $routes; |
|
650 | - } |
|
651 | - |
|
652 | - |
|
653 | - |
|
654 | - /** |
|
655 | - * @param string $version |
|
656 | - * @param boolean $hidden_endpoint |
|
657 | - * @return array |
|
658 | - */ |
|
659 | - protected function _get_rpc_route_data_for_version($version, $hidden_endpoint = false) |
|
660 | - { |
|
661 | - $this_versions_routes = array(); |
|
662 | - //checkin endpoint |
|
663 | - $this_versions_routes['registrations/(?P<REG_ID>\d+)/toggle_checkin_for_datetime/(?P<DTT_ID>\d+)'] = array( |
|
664 | - array( |
|
665 | - 'callback' => array( |
|
666 | - 'EventEspresso\core\libraries\rest_api\controllers\rpc\Checkin', |
|
667 | - 'handleRequestToggleCheckin', |
|
668 | - ), |
|
669 | - 'methods' => WP_REST_Server::CREATABLE, |
|
670 | - 'hidden_endpoint' => $hidden_endpoint, |
|
671 | - 'args' => array( |
|
672 | - 'force' => array( |
|
673 | - 'required' => false, |
|
674 | - 'default' => false, |
|
675 | - 'description' => __( |
|
676 | - // @codingStandardsIgnoreStart |
|
677 | - 'Whether to force toggle checkin, or to verify the registration status and allowed ticket uses', |
|
678 | - // @codingStandardsIgnoreEnd |
|
679 | - 'event_espresso' |
|
680 | - ), |
|
681 | - ), |
|
682 | - ), |
|
683 | - 'callback_args' => array($version), |
|
684 | - ), |
|
685 | - ); |
|
686 | - return apply_filters( |
|
687 | - 'FHEE__EED_Core_Rest_Api___register_rpc_routes__this_versions_routes', |
|
688 | - $this_versions_routes, |
|
689 | - $version, |
|
690 | - $hidden_endpoint |
|
691 | - ); |
|
692 | - } |
|
693 | - |
|
694 | - |
|
695 | - |
|
696 | - /** |
|
697 | - * Gets the query params that can be used when request one or many |
|
698 | - * |
|
699 | - * @param EEM_Base $model |
|
700 | - * @param string $version |
|
701 | - * @return array |
|
702 | - */ |
|
703 | - protected function _get_response_selection_query_params(\EEM_Base $model, $version) |
|
704 | - { |
|
705 | - return apply_filters( |
|
706 | - 'FHEE__EED_Core_Rest_Api___get_response_selection_query_params', |
|
707 | - array( |
|
708 | - 'include' => array( |
|
709 | - 'required' => false, |
|
710 | - 'default' => '*', |
|
711 | - 'type' => 'string', |
|
712 | - ), |
|
713 | - 'calculate' => array( |
|
714 | - 'required' => false, |
|
715 | - 'default' => '', |
|
716 | - 'enum' => self::$_field_calculator->retrieveCalculatedFieldsForModel($model), |
|
717 | - 'type' => 'string', |
|
718 | - //because we accept a CSV'd list of the enumerated strings, WP core validation and sanitization |
|
719 | - //freaks out. We'll just validate this argument while handling the request |
|
720 | - 'validate_callback' => null, |
|
721 | - 'sanitize_callback' => null, |
|
722 | - ), |
|
723 | - ), |
|
724 | - $model, |
|
725 | - $version |
|
726 | - ); |
|
727 | - } |
|
728 | - |
|
729 | - |
|
730 | - |
|
731 | - /** |
|
732 | - * Gets the parameters acceptable for delete requests |
|
733 | - * |
|
734 | - * @param \EEM_Base $model |
|
735 | - * @param string $version |
|
736 | - * @return array |
|
737 | - */ |
|
738 | - protected function _get_delete_query_params(\EEM_Base $model, $version) |
|
739 | - { |
|
740 | - $params_for_delete = array( |
|
741 | - 'allow_blocking' => array( |
|
742 | - 'required' => false, |
|
743 | - 'default' => true, |
|
744 | - 'type' => 'boolean', |
|
745 | - ), |
|
746 | - ); |
|
747 | - $params_for_delete['force'] = array( |
|
748 | - 'required' => false, |
|
749 | - 'default' => false, |
|
750 | - 'type' => 'boolean', |
|
751 | - ); |
|
752 | - return apply_filters( |
|
753 | - 'FHEE__EED_Core_Rest_Api___get_delete_query_params', |
|
754 | - $params_for_delete, |
|
755 | - $model, |
|
756 | - $version |
|
757 | - ); |
|
758 | - } |
|
759 | - |
|
760 | - |
|
761 | - |
|
762 | - /** |
|
763 | - * Gets info about reading query params that are acceptable |
|
764 | - * |
|
765 | - * @param \EEM_Base $model eg 'Event' or 'Venue' |
|
766 | - * @param string $version |
|
767 | - * @return array describing the args acceptable when querying this model |
|
768 | - * @throws EE_Error |
|
769 | - */ |
|
770 | - protected function _get_read_query_params(\EEM_Base $model, $version) |
|
771 | - { |
|
772 | - $default_orderby = array(); |
|
773 | - foreach ($model->get_combined_primary_key_fields() as $key_field) { |
|
774 | - $default_orderby[$key_field->get_name()] = 'ASC'; |
|
775 | - } |
|
776 | - return array_merge( |
|
777 | - $this->_get_response_selection_query_params($model, $version), |
|
778 | - array( |
|
779 | - 'where' => array( |
|
780 | - 'required' => false, |
|
781 | - 'default' => array(), |
|
782 | - 'type' => 'object', |
|
783 | - //because we accept an almost infinite list of possible where conditions, WP |
|
784 | - // core validation and sanitization freaks out. We'll just validate this argument |
|
785 | - // while handling the request |
|
786 | - 'validate_callback' => null, |
|
787 | - 'sanitize_callback' => null, |
|
788 | - ), |
|
789 | - 'limit' => array( |
|
790 | - 'required' => false, |
|
791 | - 'default' => EED_Core_Rest_Api::get_default_query_limit(), |
|
792 | - 'type' => array( |
|
793 | - 'array', |
|
794 | - 'string', |
|
795 | - 'integer', |
|
796 | - ), |
|
797 | - //because we accept a variety of types, WP core validation and sanitization |
|
798 | - //freaks out. We'll just validate this argument while handling the request |
|
799 | - 'validate_callback' => null, |
|
800 | - 'sanitize_callback' => null, |
|
801 | - ), |
|
802 | - 'order_by' => array( |
|
803 | - 'required' => false, |
|
804 | - 'default' => $default_orderby, |
|
805 | - 'type' => array( |
|
806 | - 'object', |
|
807 | - 'string', |
|
808 | - ),//because we accept a variety of types, WP core validation and sanitization |
|
809 | - //freaks out. We'll just validate this argument while handling the request |
|
810 | - 'validate_callback' => null, |
|
811 | - 'sanitize_callback' => null, |
|
812 | - ), |
|
813 | - 'group_by' => array( |
|
814 | - 'required' => false, |
|
815 | - 'default' => null, |
|
816 | - 'type' => array( |
|
817 | - 'object', |
|
818 | - 'string', |
|
819 | - ), |
|
820 | - //because we accept an almost infinite list of possible groupings, |
|
821 | - // WP core validation and sanitization |
|
822 | - //freaks out. We'll just validate this argument while handling the request |
|
823 | - 'validate_callback' => null, |
|
824 | - 'sanitize_callback' => null, |
|
825 | - ), |
|
826 | - 'having' => array( |
|
827 | - 'required' => false, |
|
828 | - 'default' => null, |
|
829 | - 'type' => 'object', |
|
830 | - //because we accept an almost infinite list of possible where conditions, WP |
|
831 | - // core validation and sanitization freaks out. We'll just validate this argument |
|
832 | - // while handling the request |
|
833 | - 'validate_callback' => null, |
|
834 | - 'sanitize_callback' => null, |
|
835 | - ), |
|
836 | - 'caps' => array( |
|
837 | - 'required' => false, |
|
838 | - 'default' => EEM_Base::caps_read, |
|
839 | - 'type' => 'string', |
|
840 | - 'enum' => array( |
|
841 | - EEM_Base::caps_read, |
|
842 | - EEM_Base::caps_read_admin, |
|
843 | - EEM_Base::caps_edit, |
|
844 | - EEM_Base::caps_delete |
|
845 | - ) |
|
846 | - ), |
|
847 | - ) |
|
848 | - ); |
|
849 | - } |
|
850 | - |
|
851 | - |
|
852 | - |
|
853 | - /** |
|
854 | - * Gets parameter information for a model regarding writing data |
|
855 | - * |
|
856 | - * @param string $model_name |
|
857 | - * @param ModelVersionInfo $model_version_info |
|
858 | - * @param boolean $create whether this is for request to create (in which case we need |
|
859 | - * all required params) or just to update (in which case we don't need those on every request) |
|
860 | - * @return array |
|
861 | - */ |
|
862 | - protected function _get_write_params( |
|
863 | - $model_name, |
|
864 | - ModelVersionInfo $model_version_info, |
|
865 | - $create = false |
|
866 | - ) { |
|
867 | - $model = EE_Registry::instance()->load_model($model_name); |
|
868 | - $fields = $model_version_info->fieldsOnModelInThisVersion($model); |
|
869 | - $args_info = array(); |
|
870 | - foreach ($fields as $field_name => $field_obj) { |
|
871 | - if ($field_obj->is_auto_increment()) { |
|
872 | - //totally ignore auto increment IDs |
|
873 | - continue; |
|
874 | - } |
|
875 | - $arg_info = $field_obj->getSchema(); |
|
876 | - $required = $create && ! $field_obj->is_nullable() && $field_obj->get_default_value() === null; |
|
877 | - $arg_info['required'] = $required; |
|
878 | - //remove the read-only flag. If it were read-only we wouldn't list it as an argument while writing, right? |
|
879 | - unset($arg_info['readonly']); |
|
880 | - $schema_properties = $field_obj->getSchemaProperties(); |
|
881 | - if ( |
|
882 | - isset($schema_properties['raw']) |
|
883 | - && $field_obj->getSchemaType() === 'object' |
|
884 | - ) { |
|
885 | - //if there's a "raw" form of this argument, use those properties instead |
|
886 | - $arg_info = array_replace( |
|
887 | - $arg_info, |
|
888 | - $schema_properties['raw'] |
|
889 | - ); |
|
890 | - } |
|
891 | - $arg_info['default'] = ModelDataTranslator::prepareFieldValueForJson( |
|
892 | - $field_obj, |
|
893 | - $field_obj->get_default_value(), |
|
894 | - $model_version_info->requestedVersion() |
|
895 | - ); |
|
896 | - //we do our own validation and sanitization within the controller |
|
897 | - $arg_info['sanitize_callback'] = |
|
898 | - array( |
|
899 | - 'EED_Core_Rest_Api', |
|
900 | - 'default_sanitize_callback', |
|
901 | - ); |
|
902 | - $args_info[$field_name] = $arg_info; |
|
903 | - if ($field_obj instanceof EE_Datetime_Field) { |
|
904 | - $gmt_arg_info = $arg_info; |
|
905 | - $gmt_arg_info['description'] = sprintf( |
|
906 | - esc_html__( |
|
907 | - '%1$s - the value for this field in UTC. Ignored if %2$s is provided.', |
|
908 | - 'event_espresso' |
|
909 | - ), |
|
910 | - $field_obj->get_nicename(), |
|
911 | - $field_name |
|
912 | - ); |
|
913 | - $args_info[$field_name . '_gmt'] = $gmt_arg_info; |
|
914 | - } |
|
915 | - } |
|
916 | - return $args_info; |
|
917 | - } |
|
918 | - |
|
919 | - |
|
920 | - |
|
921 | - /** |
|
922 | - * Replacement for WP API's 'rest_parse_request_arg'. |
|
923 | - * If the value is blank but not required, don't bother validating it. |
|
924 | - * Also, it uses our email validation instead of WP API's default. |
|
925 | - * |
|
926 | - * @param $value |
|
927 | - * @param WP_REST_Request $request |
|
928 | - * @param $param |
|
929 | - * @return bool|true|WP_Error |
|
930 | - * @throws InvalidArgumentException |
|
931 | - * @throws InvalidInterfaceException |
|
932 | - * @throws InvalidDataTypeException |
|
933 | - */ |
|
934 | - public static function default_sanitize_callback( $value, WP_REST_Request $request, $param) |
|
935 | - { |
|
936 | - $attributes = $request->get_attributes(); |
|
937 | - if (! isset($attributes['args'][$param]) |
|
938 | - || ! is_array($attributes['args'][$param])) { |
|
939 | - $validation_result = true; |
|
940 | - } else { |
|
941 | - $args = $attributes['args'][$param]; |
|
942 | - if (( |
|
943 | - $value === '' |
|
944 | - || $value === null |
|
945 | - ) |
|
946 | - && (! isset($args['required']) |
|
947 | - || $args['required'] === false |
|
948 | - ) |
|
949 | - ) { |
|
950 | - //not required and not provided? that's cool |
|
951 | - $validation_result = true; |
|
952 | - } elseif (isset($args['format']) |
|
953 | - && $args['format'] === 'email' |
|
954 | - ) { |
|
955 | - $validation_result = true; |
|
956 | - if (! self::_validate_email($value)) { |
|
957 | - $validation_result = new WP_Error( |
|
958 | - 'rest_invalid_param', |
|
959 | - esc_html__( |
|
960 | - 'The email address is not valid or does not exist.', |
|
961 | - 'event_espresso' |
|
962 | - ) |
|
963 | - ); |
|
964 | - } |
|
965 | - } else { |
|
966 | - $validation_result = rest_validate_value_from_schema($value, $args, $param); |
|
967 | - } |
|
968 | - } |
|
969 | - if (is_wp_error($validation_result)) { |
|
970 | - return $validation_result; |
|
971 | - } |
|
972 | - return rest_sanitize_request_arg($value, $request, $param); |
|
973 | - } |
|
974 | - |
|
975 | - |
|
976 | - |
|
977 | - /** |
|
978 | - * Returns whether or not this email address is valid. Copied from EE_Email_Validation_Strategy::_validate_email() |
|
979 | - * |
|
980 | - * @param $email |
|
981 | - * @return bool |
|
982 | - * @throws InvalidArgumentException |
|
983 | - * @throws InvalidInterfaceException |
|
984 | - * @throws InvalidDataTypeException |
|
985 | - */ |
|
986 | - protected static function _validate_email($email){ |
|
987 | - try { |
|
988 | - EmailAddressFactory::create($email); |
|
989 | - return true; |
|
990 | - } catch (EmailValidationException $e) { |
|
991 | - return false; |
|
992 | - } |
|
993 | - } |
|
994 | - |
|
995 | - |
|
996 | - |
|
997 | - /** |
|
998 | - * Gets routes for the config |
|
999 | - * |
|
1000 | - * @return array @see _register_model_routes |
|
1001 | - * @deprecated since version 4.9.1 |
|
1002 | - */ |
|
1003 | - protected function _register_config_routes() |
|
1004 | - { |
|
1005 | - $config_routes = array(); |
|
1006 | - foreach (self::versions_served() as $version => $hidden_endpoint) { |
|
1007 | - $config_routes[self::ee_api_namespace . $version] = $this->_get_config_route_data_for_version( |
|
1008 | - $version, |
|
1009 | - $hidden_endpoint |
|
1010 | - ); |
|
1011 | - } |
|
1012 | - return $config_routes; |
|
1013 | - } |
|
1014 | - |
|
1015 | - |
|
1016 | - |
|
1017 | - /** |
|
1018 | - * Gets routes for the config for the specified version |
|
1019 | - * |
|
1020 | - * @param string $version |
|
1021 | - * @param boolean $hidden_endpoint |
|
1022 | - * @return array |
|
1023 | - */ |
|
1024 | - protected function _get_config_route_data_for_version($version, $hidden_endpoint) |
|
1025 | - { |
|
1026 | - return array( |
|
1027 | - 'config' => array( |
|
1028 | - array( |
|
1029 | - 'callback' => array( |
|
1030 | - 'EventEspresso\core\libraries\rest_api\controllers\config\Read', |
|
1031 | - 'handleRequest', |
|
1032 | - ), |
|
1033 | - 'methods' => WP_REST_Server::READABLE, |
|
1034 | - 'hidden_endpoint' => $hidden_endpoint, |
|
1035 | - 'callback_args' => array($version), |
|
1036 | - ), |
|
1037 | - ), |
|
1038 | - 'site_info' => array( |
|
1039 | - array( |
|
1040 | - 'callback' => array( |
|
1041 | - 'EventEspresso\core\libraries\rest_api\controllers\config\Read', |
|
1042 | - 'handleRequestSiteInfo', |
|
1043 | - ), |
|
1044 | - 'methods' => WP_REST_Server::READABLE, |
|
1045 | - 'hidden_endpoint' => $hidden_endpoint, |
|
1046 | - 'callback_args' => array($version), |
|
1047 | - ), |
|
1048 | - ), |
|
1049 | - ); |
|
1050 | - } |
|
1051 | - |
|
1052 | - |
|
1053 | - |
|
1054 | - /** |
|
1055 | - * Gets the meta info routes |
|
1056 | - * |
|
1057 | - * @return array @see _register_model_routes |
|
1058 | - * @deprecated since version 4.9.1 |
|
1059 | - */ |
|
1060 | - protected function _register_meta_routes() |
|
1061 | - { |
|
1062 | - $meta_routes = array(); |
|
1063 | - foreach (self::versions_served() as $version => $hidden_endpoint) { |
|
1064 | - $meta_routes[self::ee_api_namespace . $version] = $this->_get_meta_route_data_for_version( |
|
1065 | - $version, |
|
1066 | - $hidden_endpoint |
|
1067 | - ); |
|
1068 | - } |
|
1069 | - return $meta_routes; |
|
1070 | - } |
|
1071 | - |
|
1072 | - |
|
1073 | - |
|
1074 | - /** |
|
1075 | - * @param string $version |
|
1076 | - * @param boolean $hidden_endpoint |
|
1077 | - * @return array |
|
1078 | - */ |
|
1079 | - protected function _get_meta_route_data_for_version($version, $hidden_endpoint = false) |
|
1080 | - { |
|
1081 | - return array( |
|
1082 | - 'resources' => array( |
|
1083 | - array( |
|
1084 | - 'callback' => array( |
|
1085 | - 'EventEspresso\core\libraries\rest_api\controllers\model\Meta', |
|
1086 | - 'handleRequestModelsMeta', |
|
1087 | - ), |
|
1088 | - 'methods' => WP_REST_Server::READABLE, |
|
1089 | - 'hidden_endpoint' => $hidden_endpoint, |
|
1090 | - 'callback_args' => array($version), |
|
1091 | - ), |
|
1092 | - ), |
|
1093 | - ); |
|
1094 | - } |
|
1095 | - |
|
1096 | - |
|
1097 | - |
|
1098 | - /** |
|
1099 | - * Tries to hide old 4.6 endpoints from the |
|
1100 | - * |
|
1101 | - * @param array $route_data |
|
1102 | - * @return array |
|
1103 | - * @throws \EE_Error |
|
1104 | - */ |
|
1105 | - public static function hide_old_endpoints($route_data) |
|
1106 | - { |
|
1107 | - //allow API clients to override which endpoints get hidden, in case |
|
1108 | - //they want to discover particular endpoints |
|
1109 | - //also, we don't have access to the request so we have to just grab it from the superglobal |
|
1110 | - $force_show_ee_namespace = ltrim( |
|
1111 | - EEH_Array::is_set($_REQUEST, 'force_show_ee_namespace', ''), |
|
1112 | - '/' |
|
1113 | - ); |
|
1114 | - foreach (EED_Core_Rest_Api::get_ee_route_data() as $namespace => $relative_urls) { |
|
1115 | - foreach ($relative_urls as $resource_name => $endpoints) { |
|
1116 | - foreach ($endpoints as $key => $endpoint) { |
|
1117 | - //skip schema and other route options |
|
1118 | - if (! is_numeric($key)) { |
|
1119 | - continue; |
|
1120 | - } |
|
1121 | - //by default, hide "hidden_endpoint"s, unless the request indicates |
|
1122 | - //to $force_show_ee_namespace, in which case only show that one |
|
1123 | - //namespace's endpoints (and hide all others) |
|
1124 | - if ( |
|
1125 | - ($force_show_ee_namespace !== '' && $force_show_ee_namespace !== $namespace) |
|
1126 | - || ($endpoint['hidden_endpoint'] && $force_show_ee_namespace === '') |
|
1127 | - ) { |
|
1128 | - $full_route = '/' . ltrim($namespace, '/'); |
|
1129 | - $full_route .= '/' . ltrim($resource_name, '/'); |
|
1130 | - unset($route_data[$full_route]); |
|
1131 | - } |
|
1132 | - } |
|
1133 | - } |
|
1134 | - } |
|
1135 | - return $route_data; |
|
1136 | - } |
|
1137 | - |
|
1138 | - |
|
1139 | - |
|
1140 | - /** |
|
1141 | - * Returns an array describing which versions of core support serving requests for. |
|
1142 | - * Keys are core versions' major and minor version, and values are the |
|
1143 | - * LOWEST requested version they can serve. Eg, 4.7 can serve requests for 4.6-like |
|
1144 | - * data by just removing a few models and fields from the responses. However, 4.15 might remove |
|
1145 | - * the answers table entirely, in which case it would be very difficult for |
|
1146 | - * it to serve 4.6-style responses. |
|
1147 | - * Versions of core that are missing from this array are unknowns. |
|
1148 | - * previous ver |
|
1149 | - * |
|
1150 | - * @return array |
|
1151 | - */ |
|
1152 | - public static function version_compatibilities() |
|
1153 | - { |
|
1154 | - return apply_filters( |
|
1155 | - 'FHEE__EED_Core_REST_API__version_compatibilities', |
|
1156 | - array( |
|
1157 | - '4.8.29' => '4.8.29', |
|
1158 | - '4.8.33' => '4.8.29', |
|
1159 | - '4.8.34' => '4.8.29', |
|
1160 | - '4.8.36' => '4.8.29', |
|
1161 | - ) |
|
1162 | - ); |
|
1163 | - } |
|
1164 | - |
|
1165 | - |
|
1166 | - |
|
1167 | - /** |
|
1168 | - * Gets the latest API version served. Eg if there |
|
1169 | - * are two versions served of the API, 4.8.29 and 4.8.32, and |
|
1170 | - * we are on core version 4.8.34, it will return the string "4.8.32" |
|
1171 | - * |
|
1172 | - * @return string |
|
1173 | - */ |
|
1174 | - public static function latest_rest_api_version() |
|
1175 | - { |
|
1176 | - $versions_served = \EED_Core_Rest_Api::versions_served(); |
|
1177 | - $versions_served_keys = array_keys($versions_served); |
|
1178 | - return end($versions_served_keys); |
|
1179 | - } |
|
1180 | - |
|
1181 | - |
|
1182 | - |
|
1183 | - /** |
|
1184 | - * Using EED_Core_Rest_Api::version_compatibilities(), determines what version of |
|
1185 | - * EE the API can serve requests for. Eg, if we are on 4.15 of core, and |
|
1186 | - * we can serve requests from 4.12 or later, this will return array( '4.12', '4.13', '4.14', '4.15' ). |
|
1187 | - * We also indicate whether or not this version should be put in the index or not |
|
1188 | - * |
|
1189 | - * @return array keys are API version numbers (just major and minor numbers), and values |
|
1190 | - * are whether or not they should be hidden |
|
1191 | - */ |
|
1192 | - public static function versions_served() |
|
1193 | - { |
|
1194 | - $versions_served = array(); |
|
1195 | - $possibly_served_versions = EED_Core_Rest_Api::version_compatibilities(); |
|
1196 | - $lowest_compatible_version = end($possibly_served_versions); |
|
1197 | - reset($possibly_served_versions); |
|
1198 | - $versions_served_historically = array_keys($possibly_served_versions); |
|
1199 | - $latest_version = end($versions_served_historically); |
|
1200 | - reset($versions_served_historically); |
|
1201 | - //for each version of core we have ever served: |
|
1202 | - foreach ($versions_served_historically as $key_versioned_endpoint) { |
|
1203 | - //if it's not above the current core version, and it's compatible with the current version of core |
|
1204 | - if ($key_versioned_endpoint === $latest_version) { |
|
1205 | - //don't hide the latest version in the index |
|
1206 | - $versions_served[$key_versioned_endpoint] = false; |
|
1207 | - } elseif ( |
|
1208 | - $key_versioned_endpoint >= $lowest_compatible_version |
|
1209 | - && $key_versioned_endpoint < EED_Core_Rest_Api::core_version() |
|
1210 | - ) { |
|
1211 | - //include, but hide, previous versions which are still supported |
|
1212 | - $versions_served[$key_versioned_endpoint] = true; |
|
1213 | - } elseif (apply_filters( |
|
1214 | - 'FHEE__EED_Core_Rest_Api__versions_served__include_incompatible_versions', |
|
1215 | - false, |
|
1216 | - $possibly_served_versions |
|
1217 | - )) { |
|
1218 | - //if a version is no longer supported, don't include it in index or list of versions served |
|
1219 | - $versions_served[$key_versioned_endpoint] = true; |
|
1220 | - } |
|
1221 | - } |
|
1222 | - return $versions_served; |
|
1223 | - } |
|
1224 | - |
|
1225 | - |
|
1226 | - |
|
1227 | - /** |
|
1228 | - * Gets the major and minor version of EE core's version string |
|
1229 | - * |
|
1230 | - * @return string |
|
1231 | - */ |
|
1232 | - public static function core_version() |
|
1233 | - { |
|
1234 | - return apply_filters( |
|
1235 | - 'FHEE__EED_Core_REST_API__core_version', |
|
1236 | - implode( |
|
1237 | - '.', |
|
1238 | - array_slice( |
|
1239 | - explode( |
|
1240 | - '.', |
|
1241 | - espresso_version() |
|
1242 | - ), |
|
1243 | - 0, |
|
1244 | - 3 |
|
1245 | - ) |
|
1246 | - ) |
|
1247 | - ); |
|
1248 | - } |
|
1249 | - |
|
1250 | - |
|
1251 | - |
|
1252 | - /** |
|
1253 | - * Gets the default limit that should be used when querying for resources |
|
1254 | - * |
|
1255 | - * @return int |
|
1256 | - */ |
|
1257 | - public static function get_default_query_limit() |
|
1258 | - { |
|
1259 | - //we actually don't use a const because we want folks to always use |
|
1260 | - //this method, not the const directly |
|
1261 | - return apply_filters( |
|
1262 | - 'FHEE__EED_Core_Rest_Api__get_default_query_limit', |
|
1263 | - 50 |
|
1264 | - ); |
|
1265 | - } |
|
1266 | - |
|
1267 | - |
|
1268 | - |
|
1269 | - /** |
|
1270 | - * run - initial module setup |
|
1271 | - * |
|
1272 | - * @access public |
|
1273 | - * @param WP $WP |
|
1274 | - * @return void |
|
1275 | - */ |
|
1276 | - public function run($WP) |
|
1277 | - { |
|
1278 | - } |
|
31 | + const ee_api_namespace_for_regex = 'ee\/v([^/]*)\/'; |
|
32 | + |
|
33 | + const saved_routes_option_names = 'ee_core_routes'; |
|
34 | + |
|
35 | + /** |
|
36 | + * string used in _links response bodies to make them globally unique. |
|
37 | + * |
|
38 | + * @see http://v2.wp-api.org/extending/linking/ |
|
39 | + */ |
|
40 | + const ee_api_link_namespace = 'https://api.eventespresso.com/'; |
|
41 | + |
|
42 | + /** |
|
43 | + * @var CalculatedModelFields |
|
44 | + */ |
|
45 | + protected static $_field_calculator; |
|
46 | + |
|
47 | + |
|
48 | + |
|
49 | + /** |
|
50 | + * @return EED_Core_Rest_Api|EED_Module |
|
51 | + */ |
|
52 | + public static function instance() |
|
53 | + { |
|
54 | + self::$_field_calculator = new CalculatedModelFields(); |
|
55 | + return parent::get_instance(__CLASS__); |
|
56 | + } |
|
57 | + |
|
58 | + |
|
59 | + |
|
60 | + /** |
|
61 | + * set_hooks - for hooking into EE Core, other modules, etc |
|
62 | + * |
|
63 | + * @access public |
|
64 | + * @return void |
|
65 | + */ |
|
66 | + public static function set_hooks() |
|
67 | + { |
|
68 | + self::set_hooks_both(); |
|
69 | + } |
|
70 | + |
|
71 | + |
|
72 | + |
|
73 | + /** |
|
74 | + * set_hooks_admin - for hooking into EE Admin Core, other modules, etc |
|
75 | + * |
|
76 | + * @access public |
|
77 | + * @return void |
|
78 | + */ |
|
79 | + public static function set_hooks_admin() |
|
80 | + { |
|
81 | + self::set_hooks_both(); |
|
82 | + } |
|
83 | + |
|
84 | + |
|
85 | + |
|
86 | + public static function set_hooks_both() |
|
87 | + { |
|
88 | + add_action('rest_api_init', array('EED_Core_Rest_Api', 'register_routes'), 10); |
|
89 | + add_action('rest_api_init', array('EED_Core_Rest_Api', 'set_hooks_rest_api'), 5); |
|
90 | + add_filter('rest_route_data', array('EED_Core_Rest_Api', 'hide_old_endpoints'), 10, 2); |
|
91 | + add_filter('rest_index', |
|
92 | + array('EventEspresso\core\libraries\rest_api\controllers\model\Meta', 'filterEeMetadataIntoIndex')); |
|
93 | + EED_Core_Rest_Api::invalidate_cached_route_data_on_version_change(); |
|
94 | + } |
|
95 | + |
|
96 | + |
|
97 | + |
|
98 | + /** |
|
99 | + * sets up hooks which only need to be included as part of REST API requests; |
|
100 | + * other requests like to the frontend or admin etc don't need them |
|
101 | + * |
|
102 | + * @throws \EE_Error |
|
103 | + */ |
|
104 | + public static function set_hooks_rest_api() |
|
105 | + { |
|
106 | + //set hooks which account for changes made to the API |
|
107 | + EED_Core_Rest_Api::_set_hooks_for_changes(); |
|
108 | + } |
|
109 | + |
|
110 | + |
|
111 | + |
|
112 | + /** |
|
113 | + * public wrapper of _set_hooks_for_changes. |
|
114 | + * Loads all the hooks which make requests to old versions of the API |
|
115 | + * appear the same as they always did |
|
116 | + * |
|
117 | + * @throws EE_Error |
|
118 | + */ |
|
119 | + public static function set_hooks_for_changes() |
|
120 | + { |
|
121 | + self::_set_hooks_for_changes(); |
|
122 | + } |
|
123 | + |
|
124 | + |
|
125 | + |
|
126 | + /** |
|
127 | + * Loads all the hooks which make requests to old versions of the API |
|
128 | + * appear the same as they always did |
|
129 | + * |
|
130 | + * @throws EE_Error |
|
131 | + */ |
|
132 | + protected static function _set_hooks_for_changes() |
|
133 | + { |
|
134 | + $folder_contents = EEH_File::get_contents_of_folders(array(EE_LIBRARIES . 'rest_api' . DS . 'changes'), false); |
|
135 | + foreach ($folder_contents as $classname_in_namespace => $filepath) { |
|
136 | + //ignore the base parent class |
|
137 | + //and legacy named classes |
|
138 | + if ($classname_in_namespace === 'ChangesInBase' |
|
139 | + || strpos($classname_in_namespace, 'Changes_In_') === 0 |
|
140 | + ) { |
|
141 | + continue; |
|
142 | + } |
|
143 | + $full_classname = 'EventEspresso\core\libraries\rest_api\changes\\' . $classname_in_namespace; |
|
144 | + if (class_exists($full_classname)) { |
|
145 | + $instance_of_class = new $full_classname; |
|
146 | + if ($instance_of_class instanceof ChangesInBase) { |
|
147 | + $instance_of_class->setHooks(); |
|
148 | + } |
|
149 | + } |
|
150 | + } |
|
151 | + } |
|
152 | + |
|
153 | + |
|
154 | + |
|
155 | + /** |
|
156 | + * Filters the WP routes to add our EE-related ones. This takes a bit of time |
|
157 | + * so we actually prefer to only do it when an EE plugin is activated or upgraded |
|
158 | + * |
|
159 | + * @throws \EE_Error |
|
160 | + */ |
|
161 | + public static function register_routes() |
|
162 | + { |
|
163 | + foreach (EED_Core_Rest_Api::get_ee_route_data() as $namespace => $relative_routes) { |
|
164 | + foreach ($relative_routes as $relative_route => $data_for_multiple_endpoints) { |
|
165 | + /** |
|
166 | + * @var array $data_for_multiple_endpoints numerically indexed array |
|
167 | + * but can also contain route options like { |
|
168 | + * @type array $schema { |
|
169 | + * @type callable $schema_callback |
|
170 | + * @type array $callback_args arguments that will be passed to the callback, after the |
|
171 | + * WP_REST_Request of course |
|
172 | + * } |
|
173 | + * } |
|
174 | + */ |
|
175 | + //when registering routes, register all the endpoints' data at the same time |
|
176 | + $multiple_endpoint_args = array(); |
|
177 | + foreach ($data_for_multiple_endpoints as $endpoint_key => $data_for_single_endpoint) { |
|
178 | + /** |
|
179 | + * @var array $data_for_single_endpoint { |
|
180 | + * @type callable $callback |
|
181 | + * @type string methods |
|
182 | + * @type array args |
|
183 | + * @type array _links |
|
184 | + * @type array $callback_args arguments that will be passed to the callback, after the |
|
185 | + * WP_REST_Request of course |
|
186 | + * } |
|
187 | + */ |
|
188 | + //skip route options |
|
189 | + if (! is_numeric($endpoint_key)) { |
|
190 | + continue; |
|
191 | + } |
|
192 | + if (! isset($data_for_single_endpoint['callback'], $data_for_single_endpoint['methods'])) { |
|
193 | + throw new EE_Error( |
|
194 | + esc_html__( |
|
195 | + // @codingStandardsIgnoreStart |
|
196 | + 'Endpoint configuration data needs to have entries "callback" (callable) and "methods" (comma-separated list of accepts HTTP methods).', |
|
197 | + // @codingStandardsIgnoreEnd |
|
198 | + 'event_espresso') |
|
199 | + ); |
|
200 | + } |
|
201 | + $callback = $data_for_single_endpoint['callback']; |
|
202 | + $single_endpoint_args = array( |
|
203 | + 'methods' => $data_for_single_endpoint['methods'], |
|
204 | + 'args' => isset($data_for_single_endpoint['args']) ? $data_for_single_endpoint['args'] |
|
205 | + : array(), |
|
206 | + ); |
|
207 | + if (isset($data_for_single_endpoint['_links'])) { |
|
208 | + $single_endpoint_args['_links'] = $data_for_single_endpoint['_links']; |
|
209 | + } |
|
210 | + if (isset($data_for_single_endpoint['callback_args'])) { |
|
211 | + $callback_args = $data_for_single_endpoint['callback_args']; |
|
212 | + $single_endpoint_args['callback'] = function (\WP_REST_Request $request) use ( |
|
213 | + $callback, |
|
214 | + $callback_args |
|
215 | + ) { |
|
216 | + array_unshift($callback_args, $request); |
|
217 | + return call_user_func_array( |
|
218 | + $callback, |
|
219 | + $callback_args |
|
220 | + ); |
|
221 | + }; |
|
222 | + } else { |
|
223 | + $single_endpoint_args['callback'] = $data_for_single_endpoint['callback']; |
|
224 | + } |
|
225 | + $multiple_endpoint_args[] = $single_endpoint_args; |
|
226 | + } |
|
227 | + if (isset($data_for_multiple_endpoints['schema'])) { |
|
228 | + $schema_route_data = $data_for_multiple_endpoints['schema']; |
|
229 | + $schema_callback = $schema_route_data['schema_callback']; |
|
230 | + $callback_args = $schema_route_data['callback_args']; |
|
231 | + $multiple_endpoint_args['schema'] = function () use ($schema_callback, $callback_args) { |
|
232 | + return call_user_func_array( |
|
233 | + $schema_callback, |
|
234 | + $callback_args |
|
235 | + ); |
|
236 | + }; |
|
237 | + } |
|
238 | + register_rest_route( |
|
239 | + $namespace, |
|
240 | + $relative_route, |
|
241 | + $multiple_endpoint_args |
|
242 | + ); |
|
243 | + } |
|
244 | + } |
|
245 | + } |
|
246 | + |
|
247 | + |
|
248 | + |
|
249 | + /** |
|
250 | + * Checks if there was a version change or something that merits invalidating the cached |
|
251 | + * route data. If so, invalidates the cached route data so that it gets refreshed |
|
252 | + * next time the WP API is used |
|
253 | + */ |
|
254 | + public static function invalidate_cached_route_data_on_version_change() |
|
255 | + { |
|
256 | + if (EE_System::instance()->detect_req_type() !== EE_System::req_type_normal) { |
|
257 | + EED_Core_Rest_Api::invalidate_cached_route_data(); |
|
258 | + } |
|
259 | + foreach (EE_Registry::instance()->addons as $addon) { |
|
260 | + if ($addon instanceof EE_Addon && $addon->detect_req_type() !== EE_System::req_type_normal) { |
|
261 | + EED_Core_Rest_Api::invalidate_cached_route_data(); |
|
262 | + } |
|
263 | + } |
|
264 | + } |
|
265 | + |
|
266 | + |
|
267 | + |
|
268 | + /** |
|
269 | + * Removes the cached route data so it will get refreshed next time the WP API is used |
|
270 | + */ |
|
271 | + public static function invalidate_cached_route_data() |
|
272 | + { |
|
273 | + //delete the saved EE REST API routes |
|
274 | + foreach (EED_Core_Rest_Api::versions_served() as $version => $hidden) { |
|
275 | + delete_option(EED_Core_Rest_Api::saved_routes_option_names . $version); |
|
276 | + } |
|
277 | + } |
|
278 | + |
|
279 | + |
|
280 | + |
|
281 | + /** |
|
282 | + * Gets the EE route data |
|
283 | + * |
|
284 | + * @return array top-level key is the namespace, next-level key is the route and its value is array{ |
|
285 | + * @throws \EE_Error |
|
286 | + * @type string|array $callback |
|
287 | + * @type string $methods |
|
288 | + * @type boolean $hidden_endpoint |
|
289 | + * } |
|
290 | + */ |
|
291 | + public static function get_ee_route_data() |
|
292 | + { |
|
293 | + $ee_routes = array(); |
|
294 | + foreach (self::versions_served() as $version => $hidden_endpoints) { |
|
295 | + $ee_routes[self::ee_api_namespace . $version] = self::_get_ee_route_data_for_version( |
|
296 | + $version, |
|
297 | + $hidden_endpoints |
|
298 | + ); |
|
299 | + } |
|
300 | + return $ee_routes; |
|
301 | + } |
|
302 | + |
|
303 | + |
|
304 | + |
|
305 | + /** |
|
306 | + * Gets the EE route data from the wp options if it exists already, |
|
307 | + * otherwise re-generates it and saves it to the option |
|
308 | + * |
|
309 | + * @param string $version |
|
310 | + * @param boolean $hidden_endpoints |
|
311 | + * @return array |
|
312 | + * @throws \EE_Error |
|
313 | + */ |
|
314 | + protected static function _get_ee_route_data_for_version($version, $hidden_endpoints = false) |
|
315 | + { |
|
316 | + $ee_routes = get_option(self::saved_routes_option_names . $version, null); |
|
317 | + if (! $ee_routes || (defined('EE_REST_API_DEBUG_MODE') && EE_REST_API_DEBUG_MODE)) { |
|
318 | + $ee_routes = self::_save_ee_route_data_for_version($version, $hidden_endpoints); |
|
319 | + } |
|
320 | + return $ee_routes; |
|
321 | + } |
|
322 | + |
|
323 | + |
|
324 | + |
|
325 | + /** |
|
326 | + * Saves the EE REST API route data to a wp option and returns it |
|
327 | + * |
|
328 | + * @param string $version |
|
329 | + * @param boolean $hidden_endpoints |
|
330 | + * @return mixed|null |
|
331 | + * @throws \EE_Error |
|
332 | + */ |
|
333 | + protected static function _save_ee_route_data_for_version($version, $hidden_endpoints = false) |
|
334 | + { |
|
335 | + $instance = self::instance(); |
|
336 | + $routes = apply_filters( |
|
337 | + 'EED_Core_Rest_Api__save_ee_route_data_for_version__routes', |
|
338 | + array_replace_recursive( |
|
339 | + $instance->_get_config_route_data_for_version($version, $hidden_endpoints), |
|
340 | + $instance->_get_meta_route_data_for_version($version, $hidden_endpoints), |
|
341 | + $instance->_get_model_route_data_for_version($version, $hidden_endpoints), |
|
342 | + $instance->_get_rpc_route_data_for_version($version, $hidden_endpoints) |
|
343 | + ) |
|
344 | + ); |
|
345 | + $option_name = self::saved_routes_option_names . $version; |
|
346 | + if (get_option($option_name)) { |
|
347 | + update_option($option_name, $routes, true); |
|
348 | + } else { |
|
349 | + add_option($option_name, $routes, null, 'no'); |
|
350 | + } |
|
351 | + return $routes; |
|
352 | + } |
|
353 | + |
|
354 | + |
|
355 | + |
|
356 | + /** |
|
357 | + * Calculates all the EE routes and saves it to a WordPress option so we don't |
|
358 | + * need to calculate it on every request |
|
359 | + * |
|
360 | + * @deprecated since version 4.9.1 |
|
361 | + * @return void |
|
362 | + */ |
|
363 | + public static function save_ee_routes() |
|
364 | + { |
|
365 | + if (EE_Maintenance_Mode::instance()->models_can_query()) { |
|
366 | + $instance = self::instance(); |
|
367 | + $routes = apply_filters( |
|
368 | + 'EED_Core_Rest_Api__save_ee_routes__routes', |
|
369 | + array_replace_recursive( |
|
370 | + $instance->_register_config_routes(), |
|
371 | + $instance->_register_meta_routes(), |
|
372 | + $instance->_register_model_routes(), |
|
373 | + $instance->_register_rpc_routes() |
|
374 | + ) |
|
375 | + ); |
|
376 | + update_option(self::saved_routes_option_names, $routes, true); |
|
377 | + } |
|
378 | + } |
|
379 | + |
|
380 | + |
|
381 | + |
|
382 | + /** |
|
383 | + * Gets all the route information relating to EE models |
|
384 | + * |
|
385 | + * @return array @see get_ee_route_data |
|
386 | + * @deprecated since version 4.9.1 |
|
387 | + */ |
|
388 | + protected function _register_model_routes() |
|
389 | + { |
|
390 | + $model_routes = array(); |
|
391 | + foreach (self::versions_served() as $version => $hidden_endpoint) { |
|
392 | + $model_routes[EED_Core_Rest_Api::ee_api_namespace |
|
393 | + . $version] = $this->_get_config_route_data_for_version($version, $hidden_endpoint); |
|
394 | + } |
|
395 | + return $model_routes; |
|
396 | + } |
|
397 | + |
|
398 | + |
|
399 | + |
|
400 | + /** |
|
401 | + * Decides whether or not to add write endpoints for this model. |
|
402 | + * |
|
403 | + * Currently, this defaults to exclude all global tables and models |
|
404 | + * which would allow inserting WP core data (we don't want to duplicate |
|
405 | + * what WP API does, as it's unnecessary, extra work, and potentially extra bugs) |
|
406 | + * @param EEM_Base $model |
|
407 | + * @return bool |
|
408 | + */ |
|
409 | + public static function should_have_write_endpoints(EEM_Base $model) |
|
410 | + { |
|
411 | + if ($model->is_wp_core_model()){ |
|
412 | + return false; |
|
413 | + } |
|
414 | + foreach($model->get_tables() as $table){ |
|
415 | + if( $table->is_global()){ |
|
416 | + return false; |
|
417 | + } |
|
418 | + } |
|
419 | + return true; |
|
420 | + } |
|
421 | + |
|
422 | + |
|
423 | + |
|
424 | + /** |
|
425 | + * Gets the names of all models which should have plural routes (eg `ee/v4.8.36/events`) |
|
426 | + * in this versioned namespace of EE4 |
|
427 | + * @param $version |
|
428 | + * @return array keys are model names (eg 'Event') and values ar either classnames (eg 'EEM_Event') |
|
429 | + */ |
|
430 | + public static function model_names_with_plural_routes($version){ |
|
431 | + $model_version_info = new ModelVersionInfo($version); |
|
432 | + $models_to_register = $model_version_info->modelsForRequestedVersion(); |
|
433 | + //let's not bother having endpoints for extra metas |
|
434 | + unset( |
|
435 | + $models_to_register['Extra_Meta'], |
|
436 | + $models_to_register['Extra_Join'], |
|
437 | + $models_to_register['Post_Meta'] |
|
438 | + ); |
|
439 | + return apply_filters( |
|
440 | + 'FHEE__EED_Core_REST_API___register_model_routes', |
|
441 | + $models_to_register |
|
442 | + ); |
|
443 | + } |
|
444 | + |
|
445 | + |
|
446 | + |
|
447 | + /** |
|
448 | + * Gets the route data for EE models in the specified version |
|
449 | + * |
|
450 | + * @param string $version |
|
451 | + * @param boolean $hidden_endpoint |
|
452 | + * @return array |
|
453 | + * @throws EE_Error |
|
454 | + */ |
|
455 | + protected function _get_model_route_data_for_version($version, $hidden_endpoint = false) |
|
456 | + { |
|
457 | + $model_routes = array(); |
|
458 | + $model_version_info = new ModelVersionInfo($version); |
|
459 | + foreach (EED_Core_Rest_Api::model_names_with_plural_routes($version) as $model_name => $model_classname) { |
|
460 | + $model = \EE_Registry::instance()->load_model($model_name); |
|
461 | + //if this isn't a valid model then let's skip iterate to the next item in the loop. |
|
462 | + if (! $model instanceof EEM_Base) { |
|
463 | + continue; |
|
464 | + } |
|
465 | + //yes we could just register one route for ALL models, but then they wouldn't show up in the index |
|
466 | + $plural_model_route = EED_Core_Rest_Api::get_collection_route($model); |
|
467 | + $singular_model_route = EED_Core_Rest_Api::get_entity_route($model, '(?P<id>[^\/]+)'); |
|
468 | + $model_routes[$plural_model_route] = array( |
|
469 | + array( |
|
470 | + 'callback' => array( |
|
471 | + 'EventEspresso\core\libraries\rest_api\controllers\model\Read', |
|
472 | + 'handleRequestGetAll', |
|
473 | + ), |
|
474 | + 'callback_args' => array($version, $model_name), |
|
475 | + 'methods' => WP_REST_Server::READABLE, |
|
476 | + 'hidden_endpoint' => $hidden_endpoint, |
|
477 | + 'args' => $this->_get_read_query_params($model, $version), |
|
478 | + '_links' => array( |
|
479 | + 'self' => rest_url(EED_Core_Rest_Api::ee_api_namespace . $version . $singular_model_route), |
|
480 | + ), |
|
481 | + ), |
|
482 | + 'schema' => array( |
|
483 | + 'schema_callback' => array( |
|
484 | + 'EventEspresso\core\libraries\rest_api\controllers\model\Read', |
|
485 | + 'handleSchemaRequest', |
|
486 | + ), |
|
487 | + 'callback_args' => array($version, $model_name), |
|
488 | + ), |
|
489 | + ); |
|
490 | + $model_routes[$singular_model_route] = array( |
|
491 | + array( |
|
492 | + 'callback' => array( |
|
493 | + 'EventEspresso\core\libraries\rest_api\controllers\model\Read', |
|
494 | + 'handleRequestGetOne', |
|
495 | + ), |
|
496 | + 'callback_args' => array($version, $model_name), |
|
497 | + 'methods' => WP_REST_Server::READABLE, |
|
498 | + 'hidden_endpoint' => $hidden_endpoint, |
|
499 | + 'args' => $this->_get_response_selection_query_params($model, $version), |
|
500 | + ), |
|
501 | + ); |
|
502 | + if( apply_filters( |
|
503 | + 'FHEE__EED_Core_Rest_Api___get_model_route_data_for_version__add_write_endpoints', |
|
504 | + EED_Core_Rest_Api::should_have_write_endpoints($model), |
|
505 | + $model |
|
506 | + )){ |
|
507 | + $model_routes[$plural_model_route][] = array( |
|
508 | + 'callback' => array( |
|
509 | + 'EventEspresso\core\libraries\rest_api\controllers\model\Write', |
|
510 | + 'handleRequestInsert', |
|
511 | + ), |
|
512 | + 'callback_args' => array($version, $model_name), |
|
513 | + 'methods' => WP_REST_Server::CREATABLE, |
|
514 | + 'hidden_endpoint' => $hidden_endpoint, |
|
515 | + 'args' => $this->_get_write_params($model_name, $model_version_info, true), |
|
516 | + ); |
|
517 | + $model_routes[$singular_model_route] = array_merge( |
|
518 | + $model_routes[$singular_model_route], |
|
519 | + array( |
|
520 | + array( |
|
521 | + 'callback' => array( |
|
522 | + 'EventEspresso\core\libraries\rest_api\controllers\model\Write', |
|
523 | + 'handleRequestUpdate', |
|
524 | + ), |
|
525 | + 'callback_args' => array($version, $model_name), |
|
526 | + 'methods' => WP_REST_Server::EDITABLE, |
|
527 | + 'hidden_endpoint' => $hidden_endpoint, |
|
528 | + 'args' => $this->_get_write_params($model_name, $model_version_info), |
|
529 | + ), |
|
530 | + array( |
|
531 | + 'callback' => array( |
|
532 | + 'EventEspresso\core\libraries\rest_api\controllers\model\Write', |
|
533 | + 'handleRequestDelete', |
|
534 | + ), |
|
535 | + 'callback_args' => array($version, $model_name), |
|
536 | + 'methods' => WP_REST_Server::DELETABLE, |
|
537 | + 'hidden_endpoint' => $hidden_endpoint, |
|
538 | + 'args' => $this->_get_delete_query_params($model, $version), |
|
539 | + ) |
|
540 | + ) |
|
541 | + ); |
|
542 | + } |
|
543 | + foreach ($model->relation_settings() as $relation_name => $relation_obj) { |
|
544 | + |
|
545 | + $related_route = EED_Core_Rest_Api::get_relation_route_via( |
|
546 | + $model, |
|
547 | + '(?P<id>[^\/]+)', |
|
548 | + $relation_obj |
|
549 | + ); |
|
550 | + $endpoints = array( |
|
551 | + array( |
|
552 | + 'callback' => array( |
|
553 | + 'EventEspresso\core\libraries\rest_api\controllers\model\Read', |
|
554 | + 'handleRequestGetRelated', |
|
555 | + ), |
|
556 | + 'callback_args' => array($version, $model_name, $relation_name), |
|
557 | + 'methods' => WP_REST_Server::READABLE, |
|
558 | + 'hidden_endpoint' => $hidden_endpoint, |
|
559 | + 'args' => $this->_get_read_query_params($relation_obj->get_other_model(), $version), |
|
560 | + ), |
|
561 | + ); |
|
562 | + $model_routes[$related_route] = $endpoints; |
|
563 | + } |
|
564 | + } |
|
565 | + return $model_routes; |
|
566 | + } |
|
567 | + |
|
568 | + |
|
569 | + |
|
570 | + /** |
|
571 | + * Gets the relative URI to a model's REST API plural route, after the EE4 versioned namespace, |
|
572 | + * excluding the preceding slash. |
|
573 | + * Eg you pass get_plural_route_to('Event') = 'events' |
|
574 | + * |
|
575 | + * @param EEM_Base $model |
|
576 | + * @return string |
|
577 | + */ |
|
578 | + public static function get_collection_route(EEM_Base $model) |
|
579 | + { |
|
580 | + return EEH_Inflector::pluralize_and_lower($model->get_this_model_name()); |
|
581 | + } |
|
582 | + |
|
583 | + |
|
584 | + |
|
585 | + /** |
|
586 | + * Gets the relative URI to a model's REST API singular route, after the EE4 versioned namespace, |
|
587 | + * excluding the preceding slash. |
|
588 | + * Eg you pass get_plural_route_to('Event', 12) = 'events/12' |
|
589 | + * |
|
590 | + * @param EEM_Base $model eg Event or Venue |
|
591 | + * @param string $id |
|
592 | + * @return string |
|
593 | + */ |
|
594 | + public static function get_entity_route($model, $id) |
|
595 | + { |
|
596 | + return EED_Core_Rest_Api::get_collection_route($model). '/' . $id; |
|
597 | + } |
|
598 | + |
|
599 | + |
|
600 | + /** |
|
601 | + * Gets the relative URI to a model's REST API singular route, after the EE4 versioned namespace, |
|
602 | + * excluding the preceding slash. |
|
603 | + * Eg you pass get_plural_route_to('Event', 12) = 'events/12' |
|
604 | + * |
|
605 | + * @param EEM_Base $model eg Event or Venue |
|
606 | + * @param string $id |
|
607 | + * @param EE_Model_Relation_Base $relation_obj |
|
608 | + * @return string |
|
609 | + */ |
|
610 | + public static function get_relation_route_via(EEM_Base $model, $id, EE_Model_Relation_Base $relation_obj) |
|
611 | + { |
|
612 | + $related_model_name_endpoint_part = ModelRead::getRelatedEntityName( |
|
613 | + $relation_obj->get_other_model()->get_this_model_name(), |
|
614 | + $relation_obj |
|
615 | + ); |
|
616 | + return EED_Core_Rest_Api::get_entity_route($model, $id) . '/' . $related_model_name_endpoint_part; |
|
617 | + } |
|
618 | + |
|
619 | + |
|
620 | + |
|
621 | + /** |
|
622 | + * Adds onto the $relative_route the EE4 REST API versioned namespace. |
|
623 | + * Eg if given '4.8.36' and 'events', will return 'ee/v4.8.36/events' |
|
624 | + * @param string $relative_route |
|
625 | + * @param string $version |
|
626 | + * @return string |
|
627 | + */ |
|
628 | + public static function get_versioned_route_to($relative_route, $version = '4.8.36'){ |
|
629 | + return '/' . EED_Core_Rest_Api::ee_api_namespace . $version . '/' . $relative_route; |
|
630 | + } |
|
631 | + |
|
632 | + |
|
633 | + |
|
634 | + /** |
|
635 | + * Adds all the RPC-style routes (remote procedure call-like routes, ie |
|
636 | + * routes that don't conform to the traditional REST CRUD-style). |
|
637 | + * |
|
638 | + * @deprecated since 4.9.1 |
|
639 | + */ |
|
640 | + protected function _register_rpc_routes() |
|
641 | + { |
|
642 | + $routes = array(); |
|
643 | + foreach (self::versions_served() as $version => $hidden_endpoint) { |
|
644 | + $routes[self::ee_api_namespace . $version] = $this->_get_rpc_route_data_for_version( |
|
645 | + $version, |
|
646 | + $hidden_endpoint |
|
647 | + ); |
|
648 | + } |
|
649 | + return $routes; |
|
650 | + } |
|
651 | + |
|
652 | + |
|
653 | + |
|
654 | + /** |
|
655 | + * @param string $version |
|
656 | + * @param boolean $hidden_endpoint |
|
657 | + * @return array |
|
658 | + */ |
|
659 | + protected function _get_rpc_route_data_for_version($version, $hidden_endpoint = false) |
|
660 | + { |
|
661 | + $this_versions_routes = array(); |
|
662 | + //checkin endpoint |
|
663 | + $this_versions_routes['registrations/(?P<REG_ID>\d+)/toggle_checkin_for_datetime/(?P<DTT_ID>\d+)'] = array( |
|
664 | + array( |
|
665 | + 'callback' => array( |
|
666 | + 'EventEspresso\core\libraries\rest_api\controllers\rpc\Checkin', |
|
667 | + 'handleRequestToggleCheckin', |
|
668 | + ), |
|
669 | + 'methods' => WP_REST_Server::CREATABLE, |
|
670 | + 'hidden_endpoint' => $hidden_endpoint, |
|
671 | + 'args' => array( |
|
672 | + 'force' => array( |
|
673 | + 'required' => false, |
|
674 | + 'default' => false, |
|
675 | + 'description' => __( |
|
676 | + // @codingStandardsIgnoreStart |
|
677 | + 'Whether to force toggle checkin, or to verify the registration status and allowed ticket uses', |
|
678 | + // @codingStandardsIgnoreEnd |
|
679 | + 'event_espresso' |
|
680 | + ), |
|
681 | + ), |
|
682 | + ), |
|
683 | + 'callback_args' => array($version), |
|
684 | + ), |
|
685 | + ); |
|
686 | + return apply_filters( |
|
687 | + 'FHEE__EED_Core_Rest_Api___register_rpc_routes__this_versions_routes', |
|
688 | + $this_versions_routes, |
|
689 | + $version, |
|
690 | + $hidden_endpoint |
|
691 | + ); |
|
692 | + } |
|
693 | + |
|
694 | + |
|
695 | + |
|
696 | + /** |
|
697 | + * Gets the query params that can be used when request one or many |
|
698 | + * |
|
699 | + * @param EEM_Base $model |
|
700 | + * @param string $version |
|
701 | + * @return array |
|
702 | + */ |
|
703 | + protected function _get_response_selection_query_params(\EEM_Base $model, $version) |
|
704 | + { |
|
705 | + return apply_filters( |
|
706 | + 'FHEE__EED_Core_Rest_Api___get_response_selection_query_params', |
|
707 | + array( |
|
708 | + 'include' => array( |
|
709 | + 'required' => false, |
|
710 | + 'default' => '*', |
|
711 | + 'type' => 'string', |
|
712 | + ), |
|
713 | + 'calculate' => array( |
|
714 | + 'required' => false, |
|
715 | + 'default' => '', |
|
716 | + 'enum' => self::$_field_calculator->retrieveCalculatedFieldsForModel($model), |
|
717 | + 'type' => 'string', |
|
718 | + //because we accept a CSV'd list of the enumerated strings, WP core validation and sanitization |
|
719 | + //freaks out. We'll just validate this argument while handling the request |
|
720 | + 'validate_callback' => null, |
|
721 | + 'sanitize_callback' => null, |
|
722 | + ), |
|
723 | + ), |
|
724 | + $model, |
|
725 | + $version |
|
726 | + ); |
|
727 | + } |
|
728 | + |
|
729 | + |
|
730 | + |
|
731 | + /** |
|
732 | + * Gets the parameters acceptable for delete requests |
|
733 | + * |
|
734 | + * @param \EEM_Base $model |
|
735 | + * @param string $version |
|
736 | + * @return array |
|
737 | + */ |
|
738 | + protected function _get_delete_query_params(\EEM_Base $model, $version) |
|
739 | + { |
|
740 | + $params_for_delete = array( |
|
741 | + 'allow_blocking' => array( |
|
742 | + 'required' => false, |
|
743 | + 'default' => true, |
|
744 | + 'type' => 'boolean', |
|
745 | + ), |
|
746 | + ); |
|
747 | + $params_for_delete['force'] = array( |
|
748 | + 'required' => false, |
|
749 | + 'default' => false, |
|
750 | + 'type' => 'boolean', |
|
751 | + ); |
|
752 | + return apply_filters( |
|
753 | + 'FHEE__EED_Core_Rest_Api___get_delete_query_params', |
|
754 | + $params_for_delete, |
|
755 | + $model, |
|
756 | + $version |
|
757 | + ); |
|
758 | + } |
|
759 | + |
|
760 | + |
|
761 | + |
|
762 | + /** |
|
763 | + * Gets info about reading query params that are acceptable |
|
764 | + * |
|
765 | + * @param \EEM_Base $model eg 'Event' or 'Venue' |
|
766 | + * @param string $version |
|
767 | + * @return array describing the args acceptable when querying this model |
|
768 | + * @throws EE_Error |
|
769 | + */ |
|
770 | + protected function _get_read_query_params(\EEM_Base $model, $version) |
|
771 | + { |
|
772 | + $default_orderby = array(); |
|
773 | + foreach ($model->get_combined_primary_key_fields() as $key_field) { |
|
774 | + $default_orderby[$key_field->get_name()] = 'ASC'; |
|
775 | + } |
|
776 | + return array_merge( |
|
777 | + $this->_get_response_selection_query_params($model, $version), |
|
778 | + array( |
|
779 | + 'where' => array( |
|
780 | + 'required' => false, |
|
781 | + 'default' => array(), |
|
782 | + 'type' => 'object', |
|
783 | + //because we accept an almost infinite list of possible where conditions, WP |
|
784 | + // core validation and sanitization freaks out. We'll just validate this argument |
|
785 | + // while handling the request |
|
786 | + 'validate_callback' => null, |
|
787 | + 'sanitize_callback' => null, |
|
788 | + ), |
|
789 | + 'limit' => array( |
|
790 | + 'required' => false, |
|
791 | + 'default' => EED_Core_Rest_Api::get_default_query_limit(), |
|
792 | + 'type' => array( |
|
793 | + 'array', |
|
794 | + 'string', |
|
795 | + 'integer', |
|
796 | + ), |
|
797 | + //because we accept a variety of types, WP core validation and sanitization |
|
798 | + //freaks out. We'll just validate this argument while handling the request |
|
799 | + 'validate_callback' => null, |
|
800 | + 'sanitize_callback' => null, |
|
801 | + ), |
|
802 | + 'order_by' => array( |
|
803 | + 'required' => false, |
|
804 | + 'default' => $default_orderby, |
|
805 | + 'type' => array( |
|
806 | + 'object', |
|
807 | + 'string', |
|
808 | + ),//because we accept a variety of types, WP core validation and sanitization |
|
809 | + //freaks out. We'll just validate this argument while handling the request |
|
810 | + 'validate_callback' => null, |
|
811 | + 'sanitize_callback' => null, |
|
812 | + ), |
|
813 | + 'group_by' => array( |
|
814 | + 'required' => false, |
|
815 | + 'default' => null, |
|
816 | + 'type' => array( |
|
817 | + 'object', |
|
818 | + 'string', |
|
819 | + ), |
|
820 | + //because we accept an almost infinite list of possible groupings, |
|
821 | + // WP core validation and sanitization |
|
822 | + //freaks out. We'll just validate this argument while handling the request |
|
823 | + 'validate_callback' => null, |
|
824 | + 'sanitize_callback' => null, |
|
825 | + ), |
|
826 | + 'having' => array( |
|
827 | + 'required' => false, |
|
828 | + 'default' => null, |
|
829 | + 'type' => 'object', |
|
830 | + //because we accept an almost infinite list of possible where conditions, WP |
|
831 | + // core validation and sanitization freaks out. We'll just validate this argument |
|
832 | + // while handling the request |
|
833 | + 'validate_callback' => null, |
|
834 | + 'sanitize_callback' => null, |
|
835 | + ), |
|
836 | + 'caps' => array( |
|
837 | + 'required' => false, |
|
838 | + 'default' => EEM_Base::caps_read, |
|
839 | + 'type' => 'string', |
|
840 | + 'enum' => array( |
|
841 | + EEM_Base::caps_read, |
|
842 | + EEM_Base::caps_read_admin, |
|
843 | + EEM_Base::caps_edit, |
|
844 | + EEM_Base::caps_delete |
|
845 | + ) |
|
846 | + ), |
|
847 | + ) |
|
848 | + ); |
|
849 | + } |
|
850 | + |
|
851 | + |
|
852 | + |
|
853 | + /** |
|
854 | + * Gets parameter information for a model regarding writing data |
|
855 | + * |
|
856 | + * @param string $model_name |
|
857 | + * @param ModelVersionInfo $model_version_info |
|
858 | + * @param boolean $create whether this is for request to create (in which case we need |
|
859 | + * all required params) or just to update (in which case we don't need those on every request) |
|
860 | + * @return array |
|
861 | + */ |
|
862 | + protected function _get_write_params( |
|
863 | + $model_name, |
|
864 | + ModelVersionInfo $model_version_info, |
|
865 | + $create = false |
|
866 | + ) { |
|
867 | + $model = EE_Registry::instance()->load_model($model_name); |
|
868 | + $fields = $model_version_info->fieldsOnModelInThisVersion($model); |
|
869 | + $args_info = array(); |
|
870 | + foreach ($fields as $field_name => $field_obj) { |
|
871 | + if ($field_obj->is_auto_increment()) { |
|
872 | + //totally ignore auto increment IDs |
|
873 | + continue; |
|
874 | + } |
|
875 | + $arg_info = $field_obj->getSchema(); |
|
876 | + $required = $create && ! $field_obj->is_nullable() && $field_obj->get_default_value() === null; |
|
877 | + $arg_info['required'] = $required; |
|
878 | + //remove the read-only flag. If it were read-only we wouldn't list it as an argument while writing, right? |
|
879 | + unset($arg_info['readonly']); |
|
880 | + $schema_properties = $field_obj->getSchemaProperties(); |
|
881 | + if ( |
|
882 | + isset($schema_properties['raw']) |
|
883 | + && $field_obj->getSchemaType() === 'object' |
|
884 | + ) { |
|
885 | + //if there's a "raw" form of this argument, use those properties instead |
|
886 | + $arg_info = array_replace( |
|
887 | + $arg_info, |
|
888 | + $schema_properties['raw'] |
|
889 | + ); |
|
890 | + } |
|
891 | + $arg_info['default'] = ModelDataTranslator::prepareFieldValueForJson( |
|
892 | + $field_obj, |
|
893 | + $field_obj->get_default_value(), |
|
894 | + $model_version_info->requestedVersion() |
|
895 | + ); |
|
896 | + //we do our own validation and sanitization within the controller |
|
897 | + $arg_info['sanitize_callback'] = |
|
898 | + array( |
|
899 | + 'EED_Core_Rest_Api', |
|
900 | + 'default_sanitize_callback', |
|
901 | + ); |
|
902 | + $args_info[$field_name] = $arg_info; |
|
903 | + if ($field_obj instanceof EE_Datetime_Field) { |
|
904 | + $gmt_arg_info = $arg_info; |
|
905 | + $gmt_arg_info['description'] = sprintf( |
|
906 | + esc_html__( |
|
907 | + '%1$s - the value for this field in UTC. Ignored if %2$s is provided.', |
|
908 | + 'event_espresso' |
|
909 | + ), |
|
910 | + $field_obj->get_nicename(), |
|
911 | + $field_name |
|
912 | + ); |
|
913 | + $args_info[$field_name . '_gmt'] = $gmt_arg_info; |
|
914 | + } |
|
915 | + } |
|
916 | + return $args_info; |
|
917 | + } |
|
918 | + |
|
919 | + |
|
920 | + |
|
921 | + /** |
|
922 | + * Replacement for WP API's 'rest_parse_request_arg'. |
|
923 | + * If the value is blank but not required, don't bother validating it. |
|
924 | + * Also, it uses our email validation instead of WP API's default. |
|
925 | + * |
|
926 | + * @param $value |
|
927 | + * @param WP_REST_Request $request |
|
928 | + * @param $param |
|
929 | + * @return bool|true|WP_Error |
|
930 | + * @throws InvalidArgumentException |
|
931 | + * @throws InvalidInterfaceException |
|
932 | + * @throws InvalidDataTypeException |
|
933 | + */ |
|
934 | + public static function default_sanitize_callback( $value, WP_REST_Request $request, $param) |
|
935 | + { |
|
936 | + $attributes = $request->get_attributes(); |
|
937 | + if (! isset($attributes['args'][$param]) |
|
938 | + || ! is_array($attributes['args'][$param])) { |
|
939 | + $validation_result = true; |
|
940 | + } else { |
|
941 | + $args = $attributes['args'][$param]; |
|
942 | + if (( |
|
943 | + $value === '' |
|
944 | + || $value === null |
|
945 | + ) |
|
946 | + && (! isset($args['required']) |
|
947 | + || $args['required'] === false |
|
948 | + ) |
|
949 | + ) { |
|
950 | + //not required and not provided? that's cool |
|
951 | + $validation_result = true; |
|
952 | + } elseif (isset($args['format']) |
|
953 | + && $args['format'] === 'email' |
|
954 | + ) { |
|
955 | + $validation_result = true; |
|
956 | + if (! self::_validate_email($value)) { |
|
957 | + $validation_result = new WP_Error( |
|
958 | + 'rest_invalid_param', |
|
959 | + esc_html__( |
|
960 | + 'The email address is not valid or does not exist.', |
|
961 | + 'event_espresso' |
|
962 | + ) |
|
963 | + ); |
|
964 | + } |
|
965 | + } else { |
|
966 | + $validation_result = rest_validate_value_from_schema($value, $args, $param); |
|
967 | + } |
|
968 | + } |
|
969 | + if (is_wp_error($validation_result)) { |
|
970 | + return $validation_result; |
|
971 | + } |
|
972 | + return rest_sanitize_request_arg($value, $request, $param); |
|
973 | + } |
|
974 | + |
|
975 | + |
|
976 | + |
|
977 | + /** |
|
978 | + * Returns whether or not this email address is valid. Copied from EE_Email_Validation_Strategy::_validate_email() |
|
979 | + * |
|
980 | + * @param $email |
|
981 | + * @return bool |
|
982 | + * @throws InvalidArgumentException |
|
983 | + * @throws InvalidInterfaceException |
|
984 | + * @throws InvalidDataTypeException |
|
985 | + */ |
|
986 | + protected static function _validate_email($email){ |
|
987 | + try { |
|
988 | + EmailAddressFactory::create($email); |
|
989 | + return true; |
|
990 | + } catch (EmailValidationException $e) { |
|
991 | + return false; |
|
992 | + } |
|
993 | + } |
|
994 | + |
|
995 | + |
|
996 | + |
|
997 | + /** |
|
998 | + * Gets routes for the config |
|
999 | + * |
|
1000 | + * @return array @see _register_model_routes |
|
1001 | + * @deprecated since version 4.9.1 |
|
1002 | + */ |
|
1003 | + protected function _register_config_routes() |
|
1004 | + { |
|
1005 | + $config_routes = array(); |
|
1006 | + foreach (self::versions_served() as $version => $hidden_endpoint) { |
|
1007 | + $config_routes[self::ee_api_namespace . $version] = $this->_get_config_route_data_for_version( |
|
1008 | + $version, |
|
1009 | + $hidden_endpoint |
|
1010 | + ); |
|
1011 | + } |
|
1012 | + return $config_routes; |
|
1013 | + } |
|
1014 | + |
|
1015 | + |
|
1016 | + |
|
1017 | + /** |
|
1018 | + * Gets routes for the config for the specified version |
|
1019 | + * |
|
1020 | + * @param string $version |
|
1021 | + * @param boolean $hidden_endpoint |
|
1022 | + * @return array |
|
1023 | + */ |
|
1024 | + protected function _get_config_route_data_for_version($version, $hidden_endpoint) |
|
1025 | + { |
|
1026 | + return array( |
|
1027 | + 'config' => array( |
|
1028 | + array( |
|
1029 | + 'callback' => array( |
|
1030 | + 'EventEspresso\core\libraries\rest_api\controllers\config\Read', |
|
1031 | + 'handleRequest', |
|
1032 | + ), |
|
1033 | + 'methods' => WP_REST_Server::READABLE, |
|
1034 | + 'hidden_endpoint' => $hidden_endpoint, |
|
1035 | + 'callback_args' => array($version), |
|
1036 | + ), |
|
1037 | + ), |
|
1038 | + 'site_info' => array( |
|
1039 | + array( |
|
1040 | + 'callback' => array( |
|
1041 | + 'EventEspresso\core\libraries\rest_api\controllers\config\Read', |
|
1042 | + 'handleRequestSiteInfo', |
|
1043 | + ), |
|
1044 | + 'methods' => WP_REST_Server::READABLE, |
|
1045 | + 'hidden_endpoint' => $hidden_endpoint, |
|
1046 | + 'callback_args' => array($version), |
|
1047 | + ), |
|
1048 | + ), |
|
1049 | + ); |
|
1050 | + } |
|
1051 | + |
|
1052 | + |
|
1053 | + |
|
1054 | + /** |
|
1055 | + * Gets the meta info routes |
|
1056 | + * |
|
1057 | + * @return array @see _register_model_routes |
|
1058 | + * @deprecated since version 4.9.1 |
|
1059 | + */ |
|
1060 | + protected function _register_meta_routes() |
|
1061 | + { |
|
1062 | + $meta_routes = array(); |
|
1063 | + foreach (self::versions_served() as $version => $hidden_endpoint) { |
|
1064 | + $meta_routes[self::ee_api_namespace . $version] = $this->_get_meta_route_data_for_version( |
|
1065 | + $version, |
|
1066 | + $hidden_endpoint |
|
1067 | + ); |
|
1068 | + } |
|
1069 | + return $meta_routes; |
|
1070 | + } |
|
1071 | + |
|
1072 | + |
|
1073 | + |
|
1074 | + /** |
|
1075 | + * @param string $version |
|
1076 | + * @param boolean $hidden_endpoint |
|
1077 | + * @return array |
|
1078 | + */ |
|
1079 | + protected function _get_meta_route_data_for_version($version, $hidden_endpoint = false) |
|
1080 | + { |
|
1081 | + return array( |
|
1082 | + 'resources' => array( |
|
1083 | + array( |
|
1084 | + 'callback' => array( |
|
1085 | + 'EventEspresso\core\libraries\rest_api\controllers\model\Meta', |
|
1086 | + 'handleRequestModelsMeta', |
|
1087 | + ), |
|
1088 | + 'methods' => WP_REST_Server::READABLE, |
|
1089 | + 'hidden_endpoint' => $hidden_endpoint, |
|
1090 | + 'callback_args' => array($version), |
|
1091 | + ), |
|
1092 | + ), |
|
1093 | + ); |
|
1094 | + } |
|
1095 | + |
|
1096 | + |
|
1097 | + |
|
1098 | + /** |
|
1099 | + * Tries to hide old 4.6 endpoints from the |
|
1100 | + * |
|
1101 | + * @param array $route_data |
|
1102 | + * @return array |
|
1103 | + * @throws \EE_Error |
|
1104 | + */ |
|
1105 | + public static function hide_old_endpoints($route_data) |
|
1106 | + { |
|
1107 | + //allow API clients to override which endpoints get hidden, in case |
|
1108 | + //they want to discover particular endpoints |
|
1109 | + //also, we don't have access to the request so we have to just grab it from the superglobal |
|
1110 | + $force_show_ee_namespace = ltrim( |
|
1111 | + EEH_Array::is_set($_REQUEST, 'force_show_ee_namespace', ''), |
|
1112 | + '/' |
|
1113 | + ); |
|
1114 | + foreach (EED_Core_Rest_Api::get_ee_route_data() as $namespace => $relative_urls) { |
|
1115 | + foreach ($relative_urls as $resource_name => $endpoints) { |
|
1116 | + foreach ($endpoints as $key => $endpoint) { |
|
1117 | + //skip schema and other route options |
|
1118 | + if (! is_numeric($key)) { |
|
1119 | + continue; |
|
1120 | + } |
|
1121 | + //by default, hide "hidden_endpoint"s, unless the request indicates |
|
1122 | + //to $force_show_ee_namespace, in which case only show that one |
|
1123 | + //namespace's endpoints (and hide all others) |
|
1124 | + if ( |
|
1125 | + ($force_show_ee_namespace !== '' && $force_show_ee_namespace !== $namespace) |
|
1126 | + || ($endpoint['hidden_endpoint'] && $force_show_ee_namespace === '') |
|
1127 | + ) { |
|
1128 | + $full_route = '/' . ltrim($namespace, '/'); |
|
1129 | + $full_route .= '/' . ltrim($resource_name, '/'); |
|
1130 | + unset($route_data[$full_route]); |
|
1131 | + } |
|
1132 | + } |
|
1133 | + } |
|
1134 | + } |
|
1135 | + return $route_data; |
|
1136 | + } |
|
1137 | + |
|
1138 | + |
|
1139 | + |
|
1140 | + /** |
|
1141 | + * Returns an array describing which versions of core support serving requests for. |
|
1142 | + * Keys are core versions' major and minor version, and values are the |
|
1143 | + * LOWEST requested version they can serve. Eg, 4.7 can serve requests for 4.6-like |
|
1144 | + * data by just removing a few models and fields from the responses. However, 4.15 might remove |
|
1145 | + * the answers table entirely, in which case it would be very difficult for |
|
1146 | + * it to serve 4.6-style responses. |
|
1147 | + * Versions of core that are missing from this array are unknowns. |
|
1148 | + * previous ver |
|
1149 | + * |
|
1150 | + * @return array |
|
1151 | + */ |
|
1152 | + public static function version_compatibilities() |
|
1153 | + { |
|
1154 | + return apply_filters( |
|
1155 | + 'FHEE__EED_Core_REST_API__version_compatibilities', |
|
1156 | + array( |
|
1157 | + '4.8.29' => '4.8.29', |
|
1158 | + '4.8.33' => '4.8.29', |
|
1159 | + '4.8.34' => '4.8.29', |
|
1160 | + '4.8.36' => '4.8.29', |
|
1161 | + ) |
|
1162 | + ); |
|
1163 | + } |
|
1164 | + |
|
1165 | + |
|
1166 | + |
|
1167 | + /** |
|
1168 | + * Gets the latest API version served. Eg if there |
|
1169 | + * are two versions served of the API, 4.8.29 and 4.8.32, and |
|
1170 | + * we are on core version 4.8.34, it will return the string "4.8.32" |
|
1171 | + * |
|
1172 | + * @return string |
|
1173 | + */ |
|
1174 | + public static function latest_rest_api_version() |
|
1175 | + { |
|
1176 | + $versions_served = \EED_Core_Rest_Api::versions_served(); |
|
1177 | + $versions_served_keys = array_keys($versions_served); |
|
1178 | + return end($versions_served_keys); |
|
1179 | + } |
|
1180 | + |
|
1181 | + |
|
1182 | + |
|
1183 | + /** |
|
1184 | + * Using EED_Core_Rest_Api::version_compatibilities(), determines what version of |
|
1185 | + * EE the API can serve requests for. Eg, if we are on 4.15 of core, and |
|
1186 | + * we can serve requests from 4.12 or later, this will return array( '4.12', '4.13', '4.14', '4.15' ). |
|
1187 | + * We also indicate whether or not this version should be put in the index or not |
|
1188 | + * |
|
1189 | + * @return array keys are API version numbers (just major and minor numbers), and values |
|
1190 | + * are whether or not they should be hidden |
|
1191 | + */ |
|
1192 | + public static function versions_served() |
|
1193 | + { |
|
1194 | + $versions_served = array(); |
|
1195 | + $possibly_served_versions = EED_Core_Rest_Api::version_compatibilities(); |
|
1196 | + $lowest_compatible_version = end($possibly_served_versions); |
|
1197 | + reset($possibly_served_versions); |
|
1198 | + $versions_served_historically = array_keys($possibly_served_versions); |
|
1199 | + $latest_version = end($versions_served_historically); |
|
1200 | + reset($versions_served_historically); |
|
1201 | + //for each version of core we have ever served: |
|
1202 | + foreach ($versions_served_historically as $key_versioned_endpoint) { |
|
1203 | + //if it's not above the current core version, and it's compatible with the current version of core |
|
1204 | + if ($key_versioned_endpoint === $latest_version) { |
|
1205 | + //don't hide the latest version in the index |
|
1206 | + $versions_served[$key_versioned_endpoint] = false; |
|
1207 | + } elseif ( |
|
1208 | + $key_versioned_endpoint >= $lowest_compatible_version |
|
1209 | + && $key_versioned_endpoint < EED_Core_Rest_Api::core_version() |
|
1210 | + ) { |
|
1211 | + //include, but hide, previous versions which are still supported |
|
1212 | + $versions_served[$key_versioned_endpoint] = true; |
|
1213 | + } elseif (apply_filters( |
|
1214 | + 'FHEE__EED_Core_Rest_Api__versions_served__include_incompatible_versions', |
|
1215 | + false, |
|
1216 | + $possibly_served_versions |
|
1217 | + )) { |
|
1218 | + //if a version is no longer supported, don't include it in index or list of versions served |
|
1219 | + $versions_served[$key_versioned_endpoint] = true; |
|
1220 | + } |
|
1221 | + } |
|
1222 | + return $versions_served; |
|
1223 | + } |
|
1224 | + |
|
1225 | + |
|
1226 | + |
|
1227 | + /** |
|
1228 | + * Gets the major and minor version of EE core's version string |
|
1229 | + * |
|
1230 | + * @return string |
|
1231 | + */ |
|
1232 | + public static function core_version() |
|
1233 | + { |
|
1234 | + return apply_filters( |
|
1235 | + 'FHEE__EED_Core_REST_API__core_version', |
|
1236 | + implode( |
|
1237 | + '.', |
|
1238 | + array_slice( |
|
1239 | + explode( |
|
1240 | + '.', |
|
1241 | + espresso_version() |
|
1242 | + ), |
|
1243 | + 0, |
|
1244 | + 3 |
|
1245 | + ) |
|
1246 | + ) |
|
1247 | + ); |
|
1248 | + } |
|
1249 | + |
|
1250 | + |
|
1251 | + |
|
1252 | + /** |
|
1253 | + * Gets the default limit that should be used when querying for resources |
|
1254 | + * |
|
1255 | + * @return int |
|
1256 | + */ |
|
1257 | + public static function get_default_query_limit() |
|
1258 | + { |
|
1259 | + //we actually don't use a const because we want folks to always use |
|
1260 | + //this method, not the const directly |
|
1261 | + return apply_filters( |
|
1262 | + 'FHEE__EED_Core_Rest_Api__get_default_query_limit', |
|
1263 | + 50 |
|
1264 | + ); |
|
1265 | + } |
|
1266 | + |
|
1267 | + |
|
1268 | + |
|
1269 | + /** |
|
1270 | + * run - initial module setup |
|
1271 | + * |
|
1272 | + * @access public |
|
1273 | + * @param WP $WP |
|
1274 | + * @return void |
|
1275 | + */ |
|
1276 | + public function run($WP) |
|
1277 | + { |
|
1278 | + } |
|
1279 | 1279 | } |
1280 | 1280 | |
1281 | 1281 | // End of file EED_Core_Rest_Api.module.php |
@@ -22,49 +22,49 @@ |
||
22 | 22 | class RequestStackBuilder extends SplDoublyLinkedList |
23 | 23 | { |
24 | 24 | |
25 | - /** |
|
26 | - * @type LoaderInterface $loader |
|
27 | - */ |
|
28 | - private $loader; |
|
25 | + /** |
|
26 | + * @type LoaderInterface $loader |
|
27 | + */ |
|
28 | + private $loader; |
|
29 | 29 | |
30 | 30 | |
31 | - /** |
|
32 | - * RequestStackBuilder constructor. |
|
33 | - * |
|
34 | - * @param LoaderInterface $loader |
|
35 | - */ |
|
36 | - public function __construct(LoaderInterface $loader) |
|
37 | - { |
|
38 | - $this->loader = $loader; |
|
39 | - $this->setIteratorMode(SplDoublyLinkedList::IT_MODE_LIFO | SplDoublyLinkedList::IT_MODE_KEEP); |
|
40 | - } |
|
31 | + /** |
|
32 | + * RequestStackBuilder constructor. |
|
33 | + * |
|
34 | + * @param LoaderInterface $loader |
|
35 | + */ |
|
36 | + public function __construct(LoaderInterface $loader) |
|
37 | + { |
|
38 | + $this->loader = $loader; |
|
39 | + $this->setIteratorMode(SplDoublyLinkedList::IT_MODE_LIFO | SplDoublyLinkedList::IT_MODE_KEEP); |
|
40 | + } |
|
41 | 41 | |
42 | 42 | |
43 | - /** |
|
44 | - * builds decorated middleware stack |
|
45 | - * by continuously injecting previous middleware app into the next |
|
46 | - * |
|
47 | - * @param RequestStackCoreAppInterface $application |
|
48 | - * @return RequestStack |
|
49 | - */ |
|
50 | - public function resolve(RequestStackCoreAppInterface $application) |
|
51 | - { |
|
52 | - $core_app = $application; |
|
53 | - // NOW... because the RequestStack is following the decorator pattern, |
|
54 | - // the first stack app we add will end up at the center of the stack, |
|
55 | - // and will end up being the last item to actually run, but we don't want that! |
|
56 | - // Basically we're dealing with TWO stacks, and transferring items from one to the other, |
|
57 | - // BUT... we want the final stack to be in the same order as the first. |
|
58 | - // So we need to reverse the iterator mode when transferring items, |
|
59 | - // because if we don't, the second stack will end up in the incorrect order. |
|
60 | - $this->setIteratorMode(SplDoublyLinkedList::IT_MODE_FIFO | SplDoublyLinkedList::IT_MODE_KEEP); |
|
61 | - for ($this->rewind(); $this->valid(); $this->next()) { |
|
62 | - $middleware_app = $this->current(); |
|
63 | - $middleware_app_class = array_shift($middleware_app); |
|
64 | - $middleware_app_args = is_array($middleware_app) ? $middleware_app : array(); |
|
65 | - $middleware_app_args = array($application, $this->loader) + $middleware_app_args; |
|
66 | - $application = $this->loader->getShared($middleware_app_class, $middleware_app_args); |
|
67 | - } |
|
68 | - return new RequestStack($application, $core_app); |
|
69 | - } |
|
43 | + /** |
|
44 | + * builds decorated middleware stack |
|
45 | + * by continuously injecting previous middleware app into the next |
|
46 | + * |
|
47 | + * @param RequestStackCoreAppInterface $application |
|
48 | + * @return RequestStack |
|
49 | + */ |
|
50 | + public function resolve(RequestStackCoreAppInterface $application) |
|
51 | + { |
|
52 | + $core_app = $application; |
|
53 | + // NOW... because the RequestStack is following the decorator pattern, |
|
54 | + // the first stack app we add will end up at the center of the stack, |
|
55 | + // and will end up being the last item to actually run, but we don't want that! |
|
56 | + // Basically we're dealing with TWO stacks, and transferring items from one to the other, |
|
57 | + // BUT... we want the final stack to be in the same order as the first. |
|
58 | + // So we need to reverse the iterator mode when transferring items, |
|
59 | + // because if we don't, the second stack will end up in the incorrect order. |
|
60 | + $this->setIteratorMode(SplDoublyLinkedList::IT_MODE_FIFO | SplDoublyLinkedList::IT_MODE_KEEP); |
|
61 | + for ($this->rewind(); $this->valid(); $this->next()) { |
|
62 | + $middleware_app = $this->current(); |
|
63 | + $middleware_app_class = array_shift($middleware_app); |
|
64 | + $middleware_app_args = is_array($middleware_app) ? $middleware_app : array(); |
|
65 | + $middleware_app_args = array($application, $this->loader) + $middleware_app_args; |
|
66 | + $application = $this->loader->getShared($middleware_app_class, $middleware_app_args); |
|
67 | + } |
|
68 | + return new RequestStack($application, $core_app); |
|
69 | + } |
|
70 | 70 | } |
@@ -169,7 +169,7 @@ discard block |
||
169 | 169 | // load interfaces |
170 | 170 | espresso_load_required( |
171 | 171 | 'EEH_Autoloader', |
172 | - EE_CORE . 'helpers' . DS . 'EEH_Autoloader.helper.php' |
|
172 | + EE_CORE.'helpers'.DS.'EEH_Autoloader.helper.php' |
|
173 | 173 | ); |
174 | 174 | EEH_Autoloader::instance(); |
175 | 175 | } |
@@ -184,13 +184,13 @@ discard block |
||
184 | 184 | protected function setAutoloadersForRequiredFiles() |
185 | 185 | { |
186 | 186 | // load interfaces |
187 | - EEH_Autoloader::register_autoloaders_for_each_file_in_folder(EE_CORE . 'interfaces', true); |
|
187 | + EEH_Autoloader::register_autoloaders_for_each_file_in_folder(EE_CORE.'interfaces', true); |
|
188 | 188 | // load helpers |
189 | 189 | EEH_Autoloader::register_autoloaders_for_each_file_in_folder(EE_HELPERS); |
190 | 190 | // load request stack |
191 | - EEH_Autoloader::register_autoloaders_for_each_file_in_folder(EE_CORE . 'request_stack' . DS); |
|
191 | + EEH_Autoloader::register_autoloaders_for_each_file_in_folder(EE_CORE.'request_stack'.DS); |
|
192 | 192 | // load middleware |
193 | - EEH_Autoloader::register_autoloaders_for_each_file_in_folder(EE_CORE . 'middleware' . DS); |
|
193 | + EEH_Autoloader::register_autoloaders_for_each_file_in_folder(EE_CORE.'middleware'.DS); |
|
194 | 194 | } |
195 | 195 | |
196 | 196 | |
@@ -208,7 +208,7 @@ discard block |
||
208 | 208 | * so items at the beginning of the final middleware stack will run last. |
209 | 209 | * First parameter is the middleware classname, second is an array of arguments |
210 | 210 | */ |
211 | - $stack_apps = apply_filters( |
|
211 | + $stack_apps = apply_filters( |
|
212 | 212 | 'FHEE__EventEspresso_core_services_bootstrap_BootstrapCore__buildRequestStack__stack_apps', |
213 | 213 | array( |
214 | 214 | // first in last out |
@@ -220,7 +220,7 @@ discard block |
||
220 | 220 | ) |
221 | 221 | ); |
222 | 222 | // legacy filter for backwards compatibility |
223 | - $stack_apps = apply_filters( |
|
223 | + $stack_apps = apply_filters( |
|
224 | 224 | 'FHEE__EE_Bootstrap__build_request_stack__stack_apps', |
225 | 225 | $stack_apps |
226 | 226 | ); |
@@ -44,205 +44,205 @@ |
||
44 | 44 | class BootstrapCore |
45 | 45 | { |
46 | 46 | |
47 | - /** |
|
48 | - * @type LoaderInterface $loader |
|
49 | - */ |
|
50 | - private $loader; |
|
51 | - |
|
52 | - /** |
|
53 | - * @var RequestInterface $request |
|
54 | - */ |
|
55 | - protected $request; |
|
56 | - |
|
57 | - /** |
|
58 | - * @var ResponseInterface $response |
|
59 | - */ |
|
60 | - protected $response; |
|
61 | - |
|
62 | - /** |
|
63 | - * @var RequestStackBuilder $request_stack_builder |
|
64 | - */ |
|
65 | - protected $request_stack_builder; |
|
66 | - |
|
67 | - /** |
|
68 | - * @var RequestStack $request_stack |
|
69 | - */ |
|
70 | - protected $request_stack; |
|
71 | - |
|
72 | - |
|
73 | - |
|
74 | - public function __construct() |
|
75 | - { |
|
76 | - // construct request stack and run middleware apps as soon as all WP plugins are loaded |
|
77 | - add_action('plugins_loaded', array($this, 'initialize'), 0); |
|
78 | - } |
|
79 | - |
|
80 | - |
|
81 | - /** |
|
82 | - * @throws EE_Error |
|
83 | - * @throws InvalidArgumentException |
|
84 | - * @throws InvalidDataTypeException |
|
85 | - * @throws InvalidInterfaceException |
|
86 | - * @throws ReflectionException |
|
87 | - */ |
|
88 | - public function initialize() |
|
89 | - { |
|
90 | - $this->bootstrapDependencyInjectionContainer(); |
|
91 | - $bootstrap_request = $this->bootstrapRequestResponseObjects(); |
|
92 | - add_action( |
|
93 | - 'EE_Load_Espresso_Core__handle_request__initialize_core_loading', |
|
94 | - array($bootstrap_request, 'setupLegacyRequest') |
|
95 | - ); |
|
96 | - $this->runRequestStack(); |
|
97 | - } |
|
98 | - |
|
99 | - |
|
100 | - /** |
|
101 | - * @throws ReflectionException |
|
102 | - * @throws EE_Error |
|
103 | - * @throws InvalidArgumentException |
|
104 | - * @throws InvalidDataTypeException |
|
105 | - * @throws InvalidInterfaceException |
|
106 | - */ |
|
107 | - private function bootstrapDependencyInjectionContainer() |
|
108 | - { |
|
109 | - $bootstrap_di = new BootstrapDependencyInjectionContainer(); |
|
110 | - $bootstrap_di->buildLegacyDependencyInjectionContainer(); |
|
111 | - $bootstrap_di->buildLoader(); |
|
112 | - $registry = $bootstrap_di->getRegistry(); |
|
113 | - $dependency_map = $bootstrap_di->getDependencyMap(); |
|
114 | - $dependency_map->initialize(); |
|
115 | - $registry->initialize(); |
|
116 | - $this->loader = $bootstrap_di->getLoader(); |
|
117 | - } |
|
118 | - |
|
119 | - |
|
120 | - /** |
|
121 | - * sets up the request and response objects |
|
122 | - * |
|
123 | - * @return BootstrapRequestResponseObjects |
|
124 | - * @throws InvalidArgumentException |
|
125 | - */ |
|
126 | - private function bootstrapRequestResponseObjects() |
|
127 | - { |
|
128 | - /** @var BootstrapRequestResponseObjects $bootstrap_request */ |
|
129 | - $bootstrap_request = $this->loader->getShared( |
|
130 | - 'EventEspresso\core\services\bootstrap\BootstrapRequestResponseObjects', |
|
131 | - array($this->loader) |
|
132 | - ); |
|
133 | - $bootstrap_request->buildRequestResponse(); |
|
134 | - $bootstrap_request->shareRequestResponse(); |
|
135 | - $this->request = $this->loader->getShared('EventEspresso\core\services\request\Request'); |
|
136 | - $this->response = $this->loader->getShared('EventEspresso\core\services\request\Response'); |
|
137 | - return $bootstrap_request; |
|
138 | - } |
|
139 | - |
|
140 | - |
|
141 | - |
|
142 | - /** |
|
143 | - * run_request_stack |
|
144 | - * construct request stack and run middleware apps |
|
145 | - * |
|
146 | - * @throws InvalidInterfaceException |
|
147 | - * @throws InvalidDataTypeException |
|
148 | - * @throws EE_Error |
|
149 | - * @throws InvalidArgumentException |
|
150 | - * @throws ReflectionException |
|
151 | - */ |
|
152 | - public function runRequestStack() |
|
153 | - { |
|
154 | - $this->loadAutoloader(); |
|
155 | - $this->setAutoloadersForRequiredFiles(); |
|
156 | - $this->request_stack_builder = $this->buildRequestStack(); |
|
157 | - $this->request_stack = $this->request_stack_builder->resolve( |
|
158 | - new RequestStackCoreApp() |
|
159 | - ); |
|
160 | - $this->request_stack->handleRequest($this->request, $this->response); |
|
161 | - $this->request_stack->handleResponse(); |
|
162 | - } |
|
163 | - |
|
164 | - |
|
165 | - /** |
|
166 | - * load_autoloader |
|
167 | - * |
|
168 | - * @throws EE_Error |
|
169 | - */ |
|
170 | - protected function loadAutoloader() |
|
171 | - { |
|
172 | - // load interfaces |
|
173 | - espresso_load_required( |
|
174 | - 'EEH_Autoloader', |
|
175 | - EE_CORE . 'helpers' . DS . 'EEH_Autoloader.helper.php' |
|
176 | - ); |
|
177 | - EEH_Autoloader::instance(); |
|
178 | - } |
|
179 | - |
|
180 | - |
|
181 | - |
|
182 | - /** |
|
183 | - * load_required_files |
|
184 | - * |
|
185 | - * @throws EE_Error |
|
186 | - */ |
|
187 | - protected function setAutoloadersForRequiredFiles() |
|
188 | - { |
|
189 | - // load interfaces |
|
190 | - EEH_Autoloader::register_autoloaders_for_each_file_in_folder(EE_CORE . 'interfaces', true); |
|
191 | - // load helpers |
|
192 | - EEH_Autoloader::register_autoloaders_for_each_file_in_folder(EE_HELPERS); |
|
193 | - // load request stack |
|
194 | - EEH_Autoloader::register_autoloaders_for_each_file_in_folder(EE_CORE . 'request_stack' . DS); |
|
195 | - // load middleware |
|
196 | - EEH_Autoloader::register_autoloaders_for_each_file_in_folder(EE_CORE . 'middleware' . DS); |
|
197 | - } |
|
198 | - |
|
199 | - |
|
200 | - |
|
201 | - /** |
|
202 | - * build_request_stack |
|
203 | - * |
|
204 | - * @return RequestStackBuilder |
|
205 | - */ |
|
206 | - public function buildRequestStack() |
|
207 | - { |
|
208 | - $request_stack_builder = new RequestStackBuilder($this->loader); |
|
209 | - /** |
|
210 | - * ! IMPORTANT ! The middleware stack operates FILO : FIRST IN LAST OUT |
|
211 | - * so items at the beginning of the final middleware stack will run last. |
|
212 | - * First parameter is the middleware classname, second is an array of arguments |
|
213 | - */ |
|
214 | - $stack_apps = apply_filters( |
|
215 | - 'FHEE__EventEspresso_core_services_bootstrap_BootstrapCore__buildRequestStack__stack_apps', |
|
216 | - array( |
|
217 | - // first in last out |
|
218 | - 'EventEspresso\core\services\request\middleware\BotDetector' => array(), |
|
219 | - 'EventEspresso\core\services\request\middleware\DetectFileEditorRequest' => array(), |
|
220 | - 'EventEspresso\core\services\request\middleware\PreProductionVersionWarning' => array(), |
|
221 | - 'EventEspresso\core\services\request\middleware\RecommendedVersions' => array(), |
|
222 | - // last in first out |
|
223 | - 'EventEspresso\core\services\request\middleware\DetectLogin' => array(), |
|
224 | - ) |
|
225 | - ); |
|
226 | - // legacy filter for backwards compatibility |
|
227 | - $stack_apps = apply_filters( |
|
228 | - 'FHEE__EE_Bootstrap__build_request_stack__stack_apps', |
|
229 | - $stack_apps |
|
230 | - ); |
|
231 | - // load middleware onto stack : FILO (First In Last Out) |
|
232 | - // items at the beginning of the $stack_apps array will run last |
|
233 | - foreach ((array) $stack_apps as $stack_app => $stack_app_args) { |
|
234 | - $request_stack_builder->push(array($stack_app, $stack_app_args)); |
|
235 | - } |
|
236 | - // finally, we'll add this on its own because we need it to always be part of the stack |
|
237 | - // and we also need it to always run first because the rest of the system relies on it |
|
238 | - $request_stack_builder->push( |
|
239 | - array('EventEspresso\core\services\request\middleware\SetRequestTypeContextChecker', array()) |
|
240 | - ); |
|
241 | - return apply_filters( |
|
242 | - 'FHEE__EE_Bootstrap__build_request_stack__request_stack_builder', |
|
243 | - $request_stack_builder |
|
244 | - ); |
|
245 | - } |
|
47 | + /** |
|
48 | + * @type LoaderInterface $loader |
|
49 | + */ |
|
50 | + private $loader; |
|
51 | + |
|
52 | + /** |
|
53 | + * @var RequestInterface $request |
|
54 | + */ |
|
55 | + protected $request; |
|
56 | + |
|
57 | + /** |
|
58 | + * @var ResponseInterface $response |
|
59 | + */ |
|
60 | + protected $response; |
|
61 | + |
|
62 | + /** |
|
63 | + * @var RequestStackBuilder $request_stack_builder |
|
64 | + */ |
|
65 | + protected $request_stack_builder; |
|
66 | + |
|
67 | + /** |
|
68 | + * @var RequestStack $request_stack |
|
69 | + */ |
|
70 | + protected $request_stack; |
|
71 | + |
|
72 | + |
|
73 | + |
|
74 | + public function __construct() |
|
75 | + { |
|
76 | + // construct request stack and run middleware apps as soon as all WP plugins are loaded |
|
77 | + add_action('plugins_loaded', array($this, 'initialize'), 0); |
|
78 | + } |
|
79 | + |
|
80 | + |
|
81 | + /** |
|
82 | + * @throws EE_Error |
|
83 | + * @throws InvalidArgumentException |
|
84 | + * @throws InvalidDataTypeException |
|
85 | + * @throws InvalidInterfaceException |
|
86 | + * @throws ReflectionException |
|
87 | + */ |
|
88 | + public function initialize() |
|
89 | + { |
|
90 | + $this->bootstrapDependencyInjectionContainer(); |
|
91 | + $bootstrap_request = $this->bootstrapRequestResponseObjects(); |
|
92 | + add_action( |
|
93 | + 'EE_Load_Espresso_Core__handle_request__initialize_core_loading', |
|
94 | + array($bootstrap_request, 'setupLegacyRequest') |
|
95 | + ); |
|
96 | + $this->runRequestStack(); |
|
97 | + } |
|
98 | + |
|
99 | + |
|
100 | + /** |
|
101 | + * @throws ReflectionException |
|
102 | + * @throws EE_Error |
|
103 | + * @throws InvalidArgumentException |
|
104 | + * @throws InvalidDataTypeException |
|
105 | + * @throws InvalidInterfaceException |
|
106 | + */ |
|
107 | + private function bootstrapDependencyInjectionContainer() |
|
108 | + { |
|
109 | + $bootstrap_di = new BootstrapDependencyInjectionContainer(); |
|
110 | + $bootstrap_di->buildLegacyDependencyInjectionContainer(); |
|
111 | + $bootstrap_di->buildLoader(); |
|
112 | + $registry = $bootstrap_di->getRegistry(); |
|
113 | + $dependency_map = $bootstrap_di->getDependencyMap(); |
|
114 | + $dependency_map->initialize(); |
|
115 | + $registry->initialize(); |
|
116 | + $this->loader = $bootstrap_di->getLoader(); |
|
117 | + } |
|
118 | + |
|
119 | + |
|
120 | + /** |
|
121 | + * sets up the request and response objects |
|
122 | + * |
|
123 | + * @return BootstrapRequestResponseObjects |
|
124 | + * @throws InvalidArgumentException |
|
125 | + */ |
|
126 | + private function bootstrapRequestResponseObjects() |
|
127 | + { |
|
128 | + /** @var BootstrapRequestResponseObjects $bootstrap_request */ |
|
129 | + $bootstrap_request = $this->loader->getShared( |
|
130 | + 'EventEspresso\core\services\bootstrap\BootstrapRequestResponseObjects', |
|
131 | + array($this->loader) |
|
132 | + ); |
|
133 | + $bootstrap_request->buildRequestResponse(); |
|
134 | + $bootstrap_request->shareRequestResponse(); |
|
135 | + $this->request = $this->loader->getShared('EventEspresso\core\services\request\Request'); |
|
136 | + $this->response = $this->loader->getShared('EventEspresso\core\services\request\Response'); |
|
137 | + return $bootstrap_request; |
|
138 | + } |
|
139 | + |
|
140 | + |
|
141 | + |
|
142 | + /** |
|
143 | + * run_request_stack |
|
144 | + * construct request stack and run middleware apps |
|
145 | + * |
|
146 | + * @throws InvalidInterfaceException |
|
147 | + * @throws InvalidDataTypeException |
|
148 | + * @throws EE_Error |
|
149 | + * @throws InvalidArgumentException |
|
150 | + * @throws ReflectionException |
|
151 | + */ |
|
152 | + public function runRequestStack() |
|
153 | + { |
|
154 | + $this->loadAutoloader(); |
|
155 | + $this->setAutoloadersForRequiredFiles(); |
|
156 | + $this->request_stack_builder = $this->buildRequestStack(); |
|
157 | + $this->request_stack = $this->request_stack_builder->resolve( |
|
158 | + new RequestStackCoreApp() |
|
159 | + ); |
|
160 | + $this->request_stack->handleRequest($this->request, $this->response); |
|
161 | + $this->request_stack->handleResponse(); |
|
162 | + } |
|
163 | + |
|
164 | + |
|
165 | + /** |
|
166 | + * load_autoloader |
|
167 | + * |
|
168 | + * @throws EE_Error |
|
169 | + */ |
|
170 | + protected function loadAutoloader() |
|
171 | + { |
|
172 | + // load interfaces |
|
173 | + espresso_load_required( |
|
174 | + 'EEH_Autoloader', |
|
175 | + EE_CORE . 'helpers' . DS . 'EEH_Autoloader.helper.php' |
|
176 | + ); |
|
177 | + EEH_Autoloader::instance(); |
|
178 | + } |
|
179 | + |
|
180 | + |
|
181 | + |
|
182 | + /** |
|
183 | + * load_required_files |
|
184 | + * |
|
185 | + * @throws EE_Error |
|
186 | + */ |
|
187 | + protected function setAutoloadersForRequiredFiles() |
|
188 | + { |
|
189 | + // load interfaces |
|
190 | + EEH_Autoloader::register_autoloaders_for_each_file_in_folder(EE_CORE . 'interfaces', true); |
|
191 | + // load helpers |
|
192 | + EEH_Autoloader::register_autoloaders_for_each_file_in_folder(EE_HELPERS); |
|
193 | + // load request stack |
|
194 | + EEH_Autoloader::register_autoloaders_for_each_file_in_folder(EE_CORE . 'request_stack' . DS); |
|
195 | + // load middleware |
|
196 | + EEH_Autoloader::register_autoloaders_for_each_file_in_folder(EE_CORE . 'middleware' . DS); |
|
197 | + } |
|
198 | + |
|
199 | + |
|
200 | + |
|
201 | + /** |
|
202 | + * build_request_stack |
|
203 | + * |
|
204 | + * @return RequestStackBuilder |
|
205 | + */ |
|
206 | + public function buildRequestStack() |
|
207 | + { |
|
208 | + $request_stack_builder = new RequestStackBuilder($this->loader); |
|
209 | + /** |
|
210 | + * ! IMPORTANT ! The middleware stack operates FILO : FIRST IN LAST OUT |
|
211 | + * so items at the beginning of the final middleware stack will run last. |
|
212 | + * First parameter is the middleware classname, second is an array of arguments |
|
213 | + */ |
|
214 | + $stack_apps = apply_filters( |
|
215 | + 'FHEE__EventEspresso_core_services_bootstrap_BootstrapCore__buildRequestStack__stack_apps', |
|
216 | + array( |
|
217 | + // first in last out |
|
218 | + 'EventEspresso\core\services\request\middleware\BotDetector' => array(), |
|
219 | + 'EventEspresso\core\services\request\middleware\DetectFileEditorRequest' => array(), |
|
220 | + 'EventEspresso\core\services\request\middleware\PreProductionVersionWarning' => array(), |
|
221 | + 'EventEspresso\core\services\request\middleware\RecommendedVersions' => array(), |
|
222 | + // last in first out |
|
223 | + 'EventEspresso\core\services\request\middleware\DetectLogin' => array(), |
|
224 | + ) |
|
225 | + ); |
|
226 | + // legacy filter for backwards compatibility |
|
227 | + $stack_apps = apply_filters( |
|
228 | + 'FHEE__EE_Bootstrap__build_request_stack__stack_apps', |
|
229 | + $stack_apps |
|
230 | + ); |
|
231 | + // load middleware onto stack : FILO (First In Last Out) |
|
232 | + // items at the beginning of the $stack_apps array will run last |
|
233 | + foreach ((array) $stack_apps as $stack_app => $stack_app_args) { |
|
234 | + $request_stack_builder->push(array($stack_app, $stack_app_args)); |
|
235 | + } |
|
236 | + // finally, we'll add this on its own because we need it to always be part of the stack |
|
237 | + // and we also need it to always run first because the rest of the system relies on it |
|
238 | + $request_stack_builder->push( |
|
239 | + array('EventEspresso\core\services\request\middleware\SetRequestTypeContextChecker', array()) |
|
240 | + ); |
|
241 | + return apply_filters( |
|
242 | + 'FHEE__EE_Bootstrap__build_request_stack__request_stack_builder', |
|
243 | + $request_stack_builder |
|
244 | + ); |
|
245 | + } |
|
246 | 246 | |
247 | 247 | |
248 | 248 | } |
@@ -22,114 +22,114 @@ |
||
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 $new_loader |
|
42 | - * @param CachingLoaderDecoratorInterface $shared_loader |
|
43 | - * @throws InvalidInterfaceException |
|
44 | - * @throws InvalidArgumentException |
|
45 | - * @throws InvalidDataTypeException |
|
46 | - */ |
|
47 | - public function __construct(LoaderDecoratorInterface $new_loader, CachingLoaderDecoratorInterface $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 CachingLoaderDecoratorInterface |
|
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, $shared) |
|
85 | - : $this->getNewLoader()->load($fqcn, $arguments, $shared); |
|
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, false); |
|
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 | - * @param string $fqcn |
|
115 | - * @param mixed $object |
|
116 | - * @return bool |
|
117 | - * @throws InvalidArgumentException |
|
118 | - */ |
|
119 | - public function share($fqcn, $object) |
|
120 | - { |
|
121 | - return $this->getSharedLoader()->share($fqcn, $object); |
|
122 | - } |
|
123 | - |
|
124 | - |
|
125 | - |
|
126 | - /** |
|
127 | - * calls reset() on loaders if that method exists |
|
128 | - */ |
|
129 | - public function reset() |
|
130 | - { |
|
131 | - $this->shared_loader->reset(); |
|
132 | - } |
|
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 $new_loader |
|
42 | + * @param CachingLoaderDecoratorInterface $shared_loader |
|
43 | + * @throws InvalidInterfaceException |
|
44 | + * @throws InvalidArgumentException |
|
45 | + * @throws InvalidDataTypeException |
|
46 | + */ |
|
47 | + public function __construct(LoaderDecoratorInterface $new_loader, CachingLoaderDecoratorInterface $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 CachingLoaderDecoratorInterface |
|
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, $shared) |
|
85 | + : $this->getNewLoader()->load($fqcn, $arguments, $shared); |
|
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, false); |
|
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 | + * @param string $fqcn |
|
115 | + * @param mixed $object |
|
116 | + * @return bool |
|
117 | + * @throws InvalidArgumentException |
|
118 | + */ |
|
119 | + public function share($fqcn, $object) |
|
120 | + { |
|
121 | + return $this->getSharedLoader()->share($fqcn, $object); |
|
122 | + } |
|
123 | + |
|
124 | + |
|
125 | + |
|
126 | + /** |
|
127 | + * calls reset() on loaders if that method exists |
|
128 | + */ |
|
129 | + public function reset() |
|
130 | + { |
|
131 | + $this->shared_loader->reset(); |
|
132 | + } |
|
133 | 133 | |
134 | 134 | } |
135 | 135 | // End of file Loader.php |
@@ -21,1183 +21,1183 @@ discard block |
||
21 | 21 | class EE_Session implements SessionIdentifierInterface |
22 | 22 | { |
23 | 23 | |
24 | - const session_id_prefix = 'ee_ssn_'; |
|
25 | - |
|
26 | - const hash_check_prefix = 'ee_shc_'; |
|
27 | - |
|
28 | - const OPTION_NAME_SETTINGS = 'ee_session_settings'; |
|
29 | - |
|
30 | - /** |
|
31 | - * instance of the EE_Session object |
|
32 | - * |
|
33 | - * @var EE_Session |
|
34 | - */ |
|
35 | - private static $_instance; |
|
36 | - |
|
37 | - /** |
|
38 | - * @var CacheStorageInterface $cache_storage |
|
39 | - */ |
|
40 | - protected $cache_storage; |
|
41 | - |
|
42 | - /** |
|
43 | - * EE_Encryption object |
|
44 | - * |
|
45 | - * @var EE_Encryption |
|
46 | - */ |
|
47 | - protected $encryption; |
|
48 | - |
|
49 | - /** |
|
50 | - * the session id |
|
51 | - * |
|
52 | - * @var string |
|
53 | - */ |
|
54 | - private $_sid; |
|
55 | - |
|
56 | - /** |
|
57 | - * session id salt |
|
58 | - * |
|
59 | - * @var string |
|
60 | - */ |
|
61 | - private $_sid_salt; |
|
62 | - |
|
63 | - /** |
|
64 | - * session data |
|
65 | - * |
|
66 | - * @var array |
|
67 | - */ |
|
68 | - private $_session_data = array(); |
|
69 | - |
|
70 | - /** |
|
71 | - * how long an EE session lasts |
|
72 | - * default session lifespan of 2 hours (for not so instant IPNs) |
|
73 | - * |
|
74 | - * @var int |
|
75 | - */ |
|
76 | - private $_lifespan; |
|
77 | - |
|
78 | - /** |
|
79 | - * session expiration time as Unix timestamp in GMT |
|
80 | - * |
|
81 | - * @var int |
|
82 | - */ |
|
83 | - private $_expiration; |
|
84 | - |
|
85 | - /** |
|
86 | - * whether or not session has expired at some point |
|
87 | - * |
|
88 | - * @var boolean |
|
89 | - */ |
|
90 | - private $_expired = false; |
|
91 | - |
|
92 | - /** |
|
93 | - * current time as Unix timestamp in GMT |
|
94 | - * |
|
95 | - * @var int |
|
96 | - */ |
|
97 | - private $_time; |
|
98 | - |
|
99 | - /** |
|
100 | - * whether to encrypt session data |
|
101 | - * |
|
102 | - * @var bool |
|
103 | - */ |
|
104 | - private $_use_encryption; |
|
105 | - |
|
106 | - /** |
|
107 | - * well... according to the server... |
|
108 | - * |
|
109 | - * @var null |
|
110 | - */ |
|
111 | - private $_user_agent; |
|
112 | - |
|
113 | - /** |
|
114 | - * do you really trust the server ? |
|
115 | - * |
|
116 | - * @var null |
|
117 | - */ |
|
118 | - private $_ip_address; |
|
119 | - |
|
120 | - /** |
|
121 | - * current WP user_id |
|
122 | - * |
|
123 | - * @var null |
|
124 | - */ |
|
125 | - private $_wp_user_id; |
|
126 | - |
|
127 | - /** |
|
128 | - * array for defining default session vars |
|
129 | - * |
|
130 | - * @var array |
|
131 | - */ |
|
132 | - private $_default_session_vars = array( |
|
133 | - 'id' => null, |
|
134 | - 'user_id' => null, |
|
135 | - 'ip_address' => null, |
|
136 | - 'user_agent' => null, |
|
137 | - 'init_access' => null, |
|
138 | - 'last_access' => null, |
|
139 | - 'expiration' => null, |
|
140 | - 'pages_visited' => array(), |
|
141 | - ); |
|
142 | - |
|
143 | - /** |
|
144 | - * timestamp for when last garbage collection cycle was performed |
|
145 | - * |
|
146 | - * @var int $_last_gc |
|
147 | - */ |
|
148 | - private $_last_gc; |
|
149 | - |
|
150 | - /** |
|
151 | - * @var RequestInterface $request |
|
152 | - */ |
|
153 | - protected $request; |
|
154 | - |
|
155 | - |
|
156 | - |
|
157 | - /** |
|
158 | - * @singleton method used to instantiate class object |
|
159 | - * @param CacheStorageInterface $cache_storage |
|
160 | - * @param RequestInterface $request |
|
161 | - * @param EE_Encryption $encryption |
|
162 | - * @return EE_Session |
|
163 | - * @throws InvalidArgumentException |
|
164 | - * @throws InvalidDataTypeException |
|
165 | - * @throws InvalidInterfaceException |
|
166 | - */ |
|
167 | - public static function instance( |
|
168 | - CacheStorageInterface $cache_storage = null, |
|
169 | - RequestInterface $request = null, |
|
170 | - EE_Encryption $encryption = null |
|
171 | - ) { |
|
172 | - // check if class object is instantiated |
|
173 | - // session loading is turned ON by default, but prior to the init hook, can be turned back OFF via: |
|
174 | - // add_filter( 'FHEE_load_EE_Session', '__return_false' ); |
|
175 | - if (! self::$_instance instanceof EE_Session && apply_filters('FHEE_load_EE_Session', true)) { |
|
176 | - self::$_instance = new self($cache_storage, $request, $encryption); |
|
177 | - } |
|
178 | - return self::$_instance; |
|
179 | - } |
|
180 | - |
|
181 | - |
|
182 | - |
|
183 | - /** |
|
184 | - * protected constructor to prevent direct creation |
|
185 | - * |
|
186 | - * @param CacheStorageInterface $cache_storage |
|
187 | - * @param RequestInterface $request |
|
188 | - * @param EE_Encryption $encryption |
|
189 | - * @throws InvalidArgumentException |
|
190 | - * @throws InvalidDataTypeException |
|
191 | - * @throws InvalidInterfaceException |
|
192 | - */ |
|
193 | - protected function __construct( |
|
194 | - CacheStorageInterface $cache_storage, |
|
195 | - RequestInterface $request, |
|
196 | - EE_Encryption $encryption = null |
|
197 | - ) { |
|
198 | - // session loading is turned ON by default, but prior to the init hook, can be turned back OFF via: add_filter( 'FHEE_load_EE_Session', '__return_false' ); |
|
199 | - if (! apply_filters('FHEE_load_EE_Session', true)) { |
|
200 | - return; |
|
201 | - } |
|
202 | - $this->request = $request; |
|
203 | - do_action('AHEE_log', __FILE__, __FUNCTION__, ''); |
|
204 | - if (! defined('ESPRESSO_SESSION')) { |
|
205 | - define('ESPRESSO_SESSION', true); |
|
206 | - } |
|
207 | - // default session lifespan in seconds |
|
208 | - $this->_lifespan = apply_filters( |
|
209 | - 'FHEE__EE_Session__construct___lifespan', |
|
210 | - 60 * MINUTE_IN_SECONDS |
|
211 | - ) + 1; |
|
212 | - /* |
|
24 | + const session_id_prefix = 'ee_ssn_'; |
|
25 | + |
|
26 | + const hash_check_prefix = 'ee_shc_'; |
|
27 | + |
|
28 | + const OPTION_NAME_SETTINGS = 'ee_session_settings'; |
|
29 | + |
|
30 | + /** |
|
31 | + * instance of the EE_Session object |
|
32 | + * |
|
33 | + * @var EE_Session |
|
34 | + */ |
|
35 | + private static $_instance; |
|
36 | + |
|
37 | + /** |
|
38 | + * @var CacheStorageInterface $cache_storage |
|
39 | + */ |
|
40 | + protected $cache_storage; |
|
41 | + |
|
42 | + /** |
|
43 | + * EE_Encryption object |
|
44 | + * |
|
45 | + * @var EE_Encryption |
|
46 | + */ |
|
47 | + protected $encryption; |
|
48 | + |
|
49 | + /** |
|
50 | + * the session id |
|
51 | + * |
|
52 | + * @var string |
|
53 | + */ |
|
54 | + private $_sid; |
|
55 | + |
|
56 | + /** |
|
57 | + * session id salt |
|
58 | + * |
|
59 | + * @var string |
|
60 | + */ |
|
61 | + private $_sid_salt; |
|
62 | + |
|
63 | + /** |
|
64 | + * session data |
|
65 | + * |
|
66 | + * @var array |
|
67 | + */ |
|
68 | + private $_session_data = array(); |
|
69 | + |
|
70 | + /** |
|
71 | + * how long an EE session lasts |
|
72 | + * default session lifespan of 2 hours (for not so instant IPNs) |
|
73 | + * |
|
74 | + * @var int |
|
75 | + */ |
|
76 | + private $_lifespan; |
|
77 | + |
|
78 | + /** |
|
79 | + * session expiration time as Unix timestamp in GMT |
|
80 | + * |
|
81 | + * @var int |
|
82 | + */ |
|
83 | + private $_expiration; |
|
84 | + |
|
85 | + /** |
|
86 | + * whether or not session has expired at some point |
|
87 | + * |
|
88 | + * @var boolean |
|
89 | + */ |
|
90 | + private $_expired = false; |
|
91 | + |
|
92 | + /** |
|
93 | + * current time as Unix timestamp in GMT |
|
94 | + * |
|
95 | + * @var int |
|
96 | + */ |
|
97 | + private $_time; |
|
98 | + |
|
99 | + /** |
|
100 | + * whether to encrypt session data |
|
101 | + * |
|
102 | + * @var bool |
|
103 | + */ |
|
104 | + private $_use_encryption; |
|
105 | + |
|
106 | + /** |
|
107 | + * well... according to the server... |
|
108 | + * |
|
109 | + * @var null |
|
110 | + */ |
|
111 | + private $_user_agent; |
|
112 | + |
|
113 | + /** |
|
114 | + * do you really trust the server ? |
|
115 | + * |
|
116 | + * @var null |
|
117 | + */ |
|
118 | + private $_ip_address; |
|
119 | + |
|
120 | + /** |
|
121 | + * current WP user_id |
|
122 | + * |
|
123 | + * @var null |
|
124 | + */ |
|
125 | + private $_wp_user_id; |
|
126 | + |
|
127 | + /** |
|
128 | + * array for defining default session vars |
|
129 | + * |
|
130 | + * @var array |
|
131 | + */ |
|
132 | + private $_default_session_vars = array( |
|
133 | + 'id' => null, |
|
134 | + 'user_id' => null, |
|
135 | + 'ip_address' => null, |
|
136 | + 'user_agent' => null, |
|
137 | + 'init_access' => null, |
|
138 | + 'last_access' => null, |
|
139 | + 'expiration' => null, |
|
140 | + 'pages_visited' => array(), |
|
141 | + ); |
|
142 | + |
|
143 | + /** |
|
144 | + * timestamp for when last garbage collection cycle was performed |
|
145 | + * |
|
146 | + * @var int $_last_gc |
|
147 | + */ |
|
148 | + private $_last_gc; |
|
149 | + |
|
150 | + /** |
|
151 | + * @var RequestInterface $request |
|
152 | + */ |
|
153 | + protected $request; |
|
154 | + |
|
155 | + |
|
156 | + |
|
157 | + /** |
|
158 | + * @singleton method used to instantiate class object |
|
159 | + * @param CacheStorageInterface $cache_storage |
|
160 | + * @param RequestInterface $request |
|
161 | + * @param EE_Encryption $encryption |
|
162 | + * @return EE_Session |
|
163 | + * @throws InvalidArgumentException |
|
164 | + * @throws InvalidDataTypeException |
|
165 | + * @throws InvalidInterfaceException |
|
166 | + */ |
|
167 | + public static function instance( |
|
168 | + CacheStorageInterface $cache_storage = null, |
|
169 | + RequestInterface $request = null, |
|
170 | + EE_Encryption $encryption = null |
|
171 | + ) { |
|
172 | + // check if class object is instantiated |
|
173 | + // session loading is turned ON by default, but prior to the init hook, can be turned back OFF via: |
|
174 | + // add_filter( 'FHEE_load_EE_Session', '__return_false' ); |
|
175 | + if (! self::$_instance instanceof EE_Session && apply_filters('FHEE_load_EE_Session', true)) { |
|
176 | + self::$_instance = new self($cache_storage, $request, $encryption); |
|
177 | + } |
|
178 | + return self::$_instance; |
|
179 | + } |
|
180 | + |
|
181 | + |
|
182 | + |
|
183 | + /** |
|
184 | + * protected constructor to prevent direct creation |
|
185 | + * |
|
186 | + * @param CacheStorageInterface $cache_storage |
|
187 | + * @param RequestInterface $request |
|
188 | + * @param EE_Encryption $encryption |
|
189 | + * @throws InvalidArgumentException |
|
190 | + * @throws InvalidDataTypeException |
|
191 | + * @throws InvalidInterfaceException |
|
192 | + */ |
|
193 | + protected function __construct( |
|
194 | + CacheStorageInterface $cache_storage, |
|
195 | + RequestInterface $request, |
|
196 | + EE_Encryption $encryption = null |
|
197 | + ) { |
|
198 | + // session loading is turned ON by default, but prior to the init hook, can be turned back OFF via: add_filter( 'FHEE_load_EE_Session', '__return_false' ); |
|
199 | + if (! apply_filters('FHEE_load_EE_Session', true)) { |
|
200 | + return; |
|
201 | + } |
|
202 | + $this->request = $request; |
|
203 | + do_action('AHEE_log', __FILE__, __FUNCTION__, ''); |
|
204 | + if (! defined('ESPRESSO_SESSION')) { |
|
205 | + define('ESPRESSO_SESSION', true); |
|
206 | + } |
|
207 | + // default session lifespan in seconds |
|
208 | + $this->_lifespan = apply_filters( |
|
209 | + 'FHEE__EE_Session__construct___lifespan', |
|
210 | + 60 * MINUTE_IN_SECONDS |
|
211 | + ) + 1; |
|
212 | + /* |
|
213 | 213 | * do something like the following to adjust the session lifespan: |
214 | 214 | * public static function session_lifespan() { |
215 | 215 | * return 15 * MINUTE_IN_SECONDS; |
216 | 216 | * } |
217 | 217 | */ |
218 | - // retrieve session options from db |
|
219 | - $session_settings = (array) get_option(EE_Session::OPTION_NAME_SETTINGS, array()); |
|
220 | - if (! empty($session_settings)) { |
|
221 | - // cycle though existing session options |
|
222 | - foreach ($session_settings as $var_name => $session_setting) { |
|
223 | - // set values for class properties |
|
224 | - $var_name = '_' . $var_name; |
|
225 | - $this->{$var_name} = $session_setting; |
|
226 | - } |
|
227 | - } |
|
228 | - $this->cache_storage = $cache_storage; |
|
229 | - // are we using encryption? |
|
230 | - $this->_use_encryption = $encryption instanceof EE_Encryption |
|
231 | - && EE_Registry::instance()->CFG->admin->encode_session_data(); |
|
232 | - // \EEH_Debug_Tools::printr($this->_use_encryption, '$this->_use_encryption', __FILE__, __LINE__); |
|
233 | - // encrypt data via: $this->encryption->encrypt(); |
|
234 | - $this->encryption = $encryption; |
|
235 | - // filter hook allows outside functions/classes/plugins to change default empty cart |
|
236 | - $extra_default_session_vars = apply_filters('FHEE__EE_Session__construct__extra_default_session_vars', array()); |
|
237 | - array_merge($this->_default_session_vars, $extra_default_session_vars); |
|
238 | - // apply default session vars |
|
239 | - $this->_set_defaults(); |
|
240 | - add_action('AHEE__EE_System__initialize', array($this, 'open_session')); |
|
241 | - // check request for 'clear_session' param |
|
242 | - add_action('AHEE__EE_Request_Handler__construct__complete', array($this, 'wp_loaded')); |
|
243 | - // once everything is all said and done, |
|
244 | - add_action('shutdown', array($this, 'update'), 100); |
|
245 | - add_action('shutdown', array($this, 'garbageCollection'), 1000); |
|
246 | - $this->configure_garbage_collection_filters(); |
|
247 | - } |
|
248 | - |
|
249 | - |
|
250 | - |
|
251 | - /** |
|
252 | - * @return void |
|
253 | - * @throws EE_Error |
|
254 | - * @throws InvalidArgumentException |
|
255 | - * @throws InvalidDataTypeException |
|
256 | - * @throws InvalidInterfaceException |
|
257 | - * @throws InvalidSessionDataException |
|
258 | - */ |
|
259 | - public function open_session() |
|
260 | - { |
|
261 | - // check for existing session and retrieve it from db |
|
262 | - if (! $this->_espresso_session()) { |
|
263 | - // or just start a new one |
|
264 | - $this->_create_espresso_session(); |
|
265 | - } |
|
266 | - } |
|
267 | - |
|
268 | - |
|
269 | - |
|
270 | - /** |
|
271 | - * @return bool |
|
272 | - */ |
|
273 | - public function expired() |
|
274 | - { |
|
275 | - return $this->_expired; |
|
276 | - } |
|
277 | - |
|
278 | - |
|
279 | - |
|
280 | - /** |
|
281 | - * @return void |
|
282 | - */ |
|
283 | - public function reset_expired() |
|
284 | - { |
|
285 | - $this->_expired = false; |
|
286 | - } |
|
287 | - |
|
288 | - |
|
289 | - /** |
|
290 | - * @return int |
|
291 | - */ |
|
292 | - public function expiration() |
|
293 | - { |
|
294 | - return $this->_expiration; |
|
295 | - } |
|
296 | - |
|
297 | - |
|
298 | - |
|
299 | - /** |
|
300 | - * @return int |
|
301 | - */ |
|
302 | - public function extension() |
|
303 | - { |
|
304 | - return apply_filters('FHEE__EE_Session__extend_expiration__seconds_added', 10 * MINUTE_IN_SECONDS); |
|
305 | - } |
|
306 | - |
|
307 | - |
|
308 | - |
|
309 | - /** |
|
310 | - * @param int $time number of seconds to add to session expiration |
|
311 | - */ |
|
312 | - public function extend_expiration($time = 0) |
|
313 | - { |
|
314 | - $time = $time ? $time : $this->extension(); |
|
315 | - $this->_expiration += absint($time); |
|
316 | - } |
|
317 | - |
|
318 | - |
|
319 | - |
|
320 | - /** |
|
321 | - * @return int |
|
322 | - */ |
|
323 | - public function lifespan() |
|
324 | - { |
|
325 | - return $this->_lifespan; |
|
326 | - } |
|
327 | - |
|
328 | - |
|
329 | - |
|
330 | - /** |
|
331 | - * This just sets some defaults for the _session data property |
|
332 | - * |
|
333 | - * @access private |
|
334 | - * @return void |
|
335 | - */ |
|
336 | - private function _set_defaults() |
|
337 | - { |
|
338 | - // set some defaults |
|
339 | - foreach ($this->_default_session_vars as $key => $default_var) { |
|
340 | - if (is_array($default_var)) { |
|
341 | - $this->_session_data[ $key ] = array(); |
|
342 | - } else { |
|
343 | - $this->_session_data[ $key ] = ''; |
|
344 | - } |
|
345 | - } |
|
346 | - } |
|
218 | + // retrieve session options from db |
|
219 | + $session_settings = (array) get_option(EE_Session::OPTION_NAME_SETTINGS, array()); |
|
220 | + if (! empty($session_settings)) { |
|
221 | + // cycle though existing session options |
|
222 | + foreach ($session_settings as $var_name => $session_setting) { |
|
223 | + // set values for class properties |
|
224 | + $var_name = '_' . $var_name; |
|
225 | + $this->{$var_name} = $session_setting; |
|
226 | + } |
|
227 | + } |
|
228 | + $this->cache_storage = $cache_storage; |
|
229 | + // are we using encryption? |
|
230 | + $this->_use_encryption = $encryption instanceof EE_Encryption |
|
231 | + && EE_Registry::instance()->CFG->admin->encode_session_data(); |
|
232 | + // \EEH_Debug_Tools::printr($this->_use_encryption, '$this->_use_encryption', __FILE__, __LINE__); |
|
233 | + // encrypt data via: $this->encryption->encrypt(); |
|
234 | + $this->encryption = $encryption; |
|
235 | + // filter hook allows outside functions/classes/plugins to change default empty cart |
|
236 | + $extra_default_session_vars = apply_filters('FHEE__EE_Session__construct__extra_default_session_vars', array()); |
|
237 | + array_merge($this->_default_session_vars, $extra_default_session_vars); |
|
238 | + // apply default session vars |
|
239 | + $this->_set_defaults(); |
|
240 | + add_action('AHEE__EE_System__initialize', array($this, 'open_session')); |
|
241 | + // check request for 'clear_session' param |
|
242 | + add_action('AHEE__EE_Request_Handler__construct__complete', array($this, 'wp_loaded')); |
|
243 | + // once everything is all said and done, |
|
244 | + add_action('shutdown', array($this, 'update'), 100); |
|
245 | + add_action('shutdown', array($this, 'garbageCollection'), 1000); |
|
246 | + $this->configure_garbage_collection_filters(); |
|
247 | + } |
|
248 | + |
|
249 | + |
|
250 | + |
|
251 | + /** |
|
252 | + * @return void |
|
253 | + * @throws EE_Error |
|
254 | + * @throws InvalidArgumentException |
|
255 | + * @throws InvalidDataTypeException |
|
256 | + * @throws InvalidInterfaceException |
|
257 | + * @throws InvalidSessionDataException |
|
258 | + */ |
|
259 | + public function open_session() |
|
260 | + { |
|
261 | + // check for existing session and retrieve it from db |
|
262 | + if (! $this->_espresso_session()) { |
|
263 | + // or just start a new one |
|
264 | + $this->_create_espresso_session(); |
|
265 | + } |
|
266 | + } |
|
267 | + |
|
268 | + |
|
269 | + |
|
270 | + /** |
|
271 | + * @return bool |
|
272 | + */ |
|
273 | + public function expired() |
|
274 | + { |
|
275 | + return $this->_expired; |
|
276 | + } |
|
277 | + |
|
278 | + |
|
279 | + |
|
280 | + /** |
|
281 | + * @return void |
|
282 | + */ |
|
283 | + public function reset_expired() |
|
284 | + { |
|
285 | + $this->_expired = false; |
|
286 | + } |
|
287 | + |
|
288 | + |
|
289 | + /** |
|
290 | + * @return int |
|
291 | + */ |
|
292 | + public function expiration() |
|
293 | + { |
|
294 | + return $this->_expiration; |
|
295 | + } |
|
296 | + |
|
297 | + |
|
298 | + |
|
299 | + /** |
|
300 | + * @return int |
|
301 | + */ |
|
302 | + public function extension() |
|
303 | + { |
|
304 | + return apply_filters('FHEE__EE_Session__extend_expiration__seconds_added', 10 * MINUTE_IN_SECONDS); |
|
305 | + } |
|
306 | + |
|
307 | + |
|
308 | + |
|
309 | + /** |
|
310 | + * @param int $time number of seconds to add to session expiration |
|
311 | + */ |
|
312 | + public function extend_expiration($time = 0) |
|
313 | + { |
|
314 | + $time = $time ? $time : $this->extension(); |
|
315 | + $this->_expiration += absint($time); |
|
316 | + } |
|
317 | + |
|
318 | + |
|
319 | + |
|
320 | + /** |
|
321 | + * @return int |
|
322 | + */ |
|
323 | + public function lifespan() |
|
324 | + { |
|
325 | + return $this->_lifespan; |
|
326 | + } |
|
327 | + |
|
328 | + |
|
329 | + |
|
330 | + /** |
|
331 | + * This just sets some defaults for the _session data property |
|
332 | + * |
|
333 | + * @access private |
|
334 | + * @return void |
|
335 | + */ |
|
336 | + private function _set_defaults() |
|
337 | + { |
|
338 | + // set some defaults |
|
339 | + foreach ($this->_default_session_vars as $key => $default_var) { |
|
340 | + if (is_array($default_var)) { |
|
341 | + $this->_session_data[ $key ] = array(); |
|
342 | + } else { |
|
343 | + $this->_session_data[ $key ] = ''; |
|
344 | + } |
|
345 | + } |
|
346 | + } |
|
347 | 347 | |
348 | 348 | |
349 | - |
|
350 | - /** |
|
351 | - * @retrieve session data |
|
352 | - * @access public |
|
353 | - * @return string |
|
354 | - */ |
|
355 | - public function id() |
|
356 | - { |
|
357 | - return $this->_sid; |
|
358 | - } |
|
349 | + |
|
350 | + /** |
|
351 | + * @retrieve session data |
|
352 | + * @access public |
|
353 | + * @return string |
|
354 | + */ |
|
355 | + public function id() |
|
356 | + { |
|
357 | + return $this->_sid; |
|
358 | + } |
|
359 | 359 | |
360 | 360 | |
361 | 361 | |
362 | - /** |
|
363 | - * @param \EE_Cart $cart |
|
364 | - * @return bool |
|
365 | - */ |
|
366 | - public function set_cart(EE_Cart $cart) |
|
367 | - { |
|
368 | - $this->_session_data['cart'] = $cart; |
|
369 | - return true; |
|
370 | - } |
|
362 | + /** |
|
363 | + * @param \EE_Cart $cart |
|
364 | + * @return bool |
|
365 | + */ |
|
366 | + public function set_cart(EE_Cart $cart) |
|
367 | + { |
|
368 | + $this->_session_data['cart'] = $cart; |
|
369 | + return true; |
|
370 | + } |
|
371 | 371 | |
372 | 372 | |
373 | 373 | |
374 | - /** |
|
375 | - * reset_cart |
|
376 | - */ |
|
377 | - public function reset_cart() |
|
378 | - { |
|
379 | - do_action('AHEE__EE_Session__reset_cart__before_reset', $this); |
|
380 | - $this->_session_data['cart'] = null; |
|
381 | - } |
|
382 | - |
|
383 | - |
|
384 | - |
|
385 | - /** |
|
386 | - * @return \EE_Cart |
|
387 | - */ |
|
388 | - public function cart() |
|
389 | - { |
|
390 | - return isset($this->_session_data['cart']) && $this->_session_data['cart'] instanceof EE_Cart |
|
391 | - ? $this->_session_data['cart'] |
|
392 | - : null; |
|
393 | - } |
|
394 | - |
|
395 | - |
|
396 | - |
|
397 | - /** |
|
398 | - * @param \EE_Checkout $checkout |
|
399 | - * @return bool |
|
400 | - */ |
|
401 | - public function set_checkout(EE_Checkout $checkout) |
|
402 | - { |
|
403 | - $this->_session_data['checkout'] = $checkout; |
|
404 | - return true; |
|
405 | - } |
|
406 | - |
|
407 | - |
|
408 | - |
|
409 | - /** |
|
410 | - * reset_checkout |
|
411 | - */ |
|
412 | - public function reset_checkout() |
|
413 | - { |
|
414 | - do_action('AHEE__EE_Session__reset_checkout__before_reset', $this); |
|
415 | - $this->_session_data['checkout'] = null; |
|
416 | - } |
|
417 | - |
|
418 | - |
|
419 | - |
|
420 | - /** |
|
421 | - * @return \EE_Checkout |
|
422 | - */ |
|
423 | - public function checkout() |
|
424 | - { |
|
425 | - return isset($this->_session_data['checkout']) && $this->_session_data['checkout'] instanceof EE_Checkout |
|
426 | - ? $this->_session_data['checkout'] |
|
427 | - : null; |
|
428 | - } |
|
429 | - |
|
430 | - |
|
431 | - |
|
432 | - /** |
|
433 | - * @param \EE_Transaction $transaction |
|
434 | - * @return bool |
|
435 | - * @throws EE_Error |
|
436 | - */ |
|
437 | - public function set_transaction(EE_Transaction $transaction) |
|
438 | - { |
|
439 | - // first remove the session from the transaction before we save the transaction in the session |
|
440 | - $transaction->set_txn_session_data(null); |
|
441 | - $this->_session_data['transaction'] = $transaction; |
|
442 | - return true; |
|
443 | - } |
|
444 | - |
|
445 | - |
|
446 | - |
|
447 | - /** |
|
448 | - * reset_transaction |
|
449 | - */ |
|
450 | - public function reset_transaction() |
|
451 | - { |
|
452 | - do_action('AHEE__EE_Session__reset_transaction__before_reset', $this); |
|
453 | - $this->_session_data['transaction'] = null; |
|
454 | - } |
|
455 | - |
|
456 | - |
|
457 | - |
|
458 | - /** |
|
459 | - * @return \EE_Transaction |
|
460 | - */ |
|
461 | - public function transaction() |
|
462 | - { |
|
463 | - return isset($this->_session_data['transaction']) |
|
464 | - && $this->_session_data['transaction'] instanceof EE_Transaction |
|
465 | - ? $this->_session_data['transaction'] |
|
466 | - : null; |
|
467 | - } |
|
468 | - |
|
469 | - |
|
470 | - |
|
471 | - /** |
|
472 | - * retrieve session data |
|
473 | - * |
|
474 | - * @access public |
|
475 | - * @param null $key |
|
476 | - * @param bool $reset_cache |
|
477 | - * @return array |
|
478 | - */ |
|
479 | - public function get_session_data($key = null, $reset_cache = false) |
|
480 | - { |
|
481 | - if ($reset_cache) { |
|
482 | - $this->reset_cart(); |
|
483 | - $this->reset_checkout(); |
|
484 | - $this->reset_transaction(); |
|
485 | - } |
|
486 | - if (! empty($key)) { |
|
487 | - return isset($this->_session_data[ $key ]) ? $this->_session_data[ $key ] : null; |
|
488 | - } |
|
489 | - return $this->_session_data; |
|
490 | - } |
|
491 | - |
|
492 | - |
|
493 | - |
|
494 | - /** |
|
495 | - * set session data |
|
496 | - * |
|
497 | - * @access public |
|
498 | - * @param array $data |
|
499 | - * @return TRUE on success, FALSE on fail |
|
500 | - */ |
|
501 | - public function set_session_data($data) |
|
502 | - { |
|
503 | - |
|
504 | - // nothing ??? bad data ??? go home! |
|
505 | - if (empty($data) || ! is_array($data)) { |
|
506 | - EE_Error::add_error(__('No session data or invalid session data was provided.', 'event_espresso'), __FILE__, |
|
507 | - __FUNCTION__, __LINE__); |
|
508 | - return false; |
|
509 | - } |
|
510 | - foreach ($data as $key => $value) { |
|
511 | - if (isset($this->_default_session_vars[ $key ])) { |
|
512 | - EE_Error::add_error(sprintf(__('Sorry! %s is a default session datum and can not be reset.', |
|
513 | - 'event_espresso'), $key), __FILE__, __FUNCTION__, __LINE__); |
|
514 | - return false; |
|
515 | - } |
|
516 | - $this->_session_data[ $key ] = $value; |
|
517 | - } |
|
518 | - return true; |
|
519 | - } |
|
520 | - |
|
521 | - |
|
522 | - |
|
523 | - /** |
|
524 | - * @initiate session |
|
525 | - * @access private |
|
526 | - * @return TRUE on success, FALSE on fail |
|
527 | - * @throws EE_Error |
|
528 | - * @throws InvalidArgumentException |
|
529 | - * @throws InvalidDataTypeException |
|
530 | - * @throws InvalidInterfaceException |
|
531 | - * @throws InvalidSessionDataException |
|
532 | - */ |
|
533 | - private function _espresso_session() |
|
534 | - { |
|
535 | - do_action('AHEE_log', __FILE__, __FUNCTION__, ''); |
|
536 | - // check that session has started |
|
537 | - if (session_id() === '') { |
|
538 | - //starts a new session if one doesn't already exist, or re-initiates an existing one |
|
539 | - session_start(); |
|
540 | - } |
|
541 | - // get our modified session ID |
|
542 | - $this->_sid = $this->_generate_session_id(); |
|
543 | - // and the visitors IP |
|
544 | - $this->_ip_address = $this->request->ipAddress(); |
|
545 | - // set the "user agent" |
|
546 | - $this->_user_agent = $this->request->userAgent(); |
|
547 | - // now let's retrieve what's in the db |
|
548 | - $session_data = $this->_retrieve_session_data(); |
|
549 | - if (! empty($session_data)) { |
|
550 | - // get the current time in UTC |
|
551 | - $this->_time = $this->_time !== null ? $this->_time : time(); |
|
552 | - // and reset the session expiration |
|
553 | - $this->_expiration = isset($session_data['expiration']) |
|
554 | - ? $session_data['expiration'] |
|
555 | - : $this->_time + $this->_lifespan; |
|
556 | - } else { |
|
557 | - // set initial site access time and the session expiration |
|
558 | - $this->_set_init_access_and_expiration(); |
|
559 | - // set referer |
|
560 | - $this->_session_data['pages_visited'][ $this->_session_data['init_access'] ] = isset($_SERVER['HTTP_REFERER']) |
|
561 | - ? esc_attr($_SERVER['HTTP_REFERER']) |
|
562 | - : ''; |
|
563 | - // no previous session = go back and create one (on top of the data above) |
|
564 | - return false; |
|
565 | - } |
|
566 | - // now the user agent |
|
567 | - if ($session_data['user_agent'] !== $this->_user_agent) { |
|
568 | - return false; |
|
569 | - } |
|
570 | - // wait a minute... how old are you? |
|
571 | - if ($this->_time > $this->_expiration) { |
|
572 | - // yer too old fer me! |
|
573 | - $this->_expired = true; |
|
574 | - // wipe out everything that isn't a default session datum |
|
575 | - $this->clear_session(__CLASS__, __FUNCTION__); |
|
576 | - } |
|
577 | - // make event espresso session data available to plugin |
|
578 | - $this->_session_data = array_merge($this->_session_data, $session_data); |
|
579 | - return true; |
|
580 | - } |
|
581 | - |
|
582 | - |
|
583 | - |
|
584 | - /** |
|
585 | - * _get_session_data |
|
586 | - * Retrieves the session data, and attempts to correct any encoding issues that can occur due to improperly setup |
|
587 | - * databases |
|
588 | - * |
|
589 | - * @return array |
|
590 | - * @throws EE_Error |
|
591 | - * @throws InvalidArgumentException |
|
592 | - * @throws InvalidSessionDataException |
|
593 | - * @throws InvalidDataTypeException |
|
594 | - * @throws InvalidInterfaceException |
|
595 | - */ |
|
596 | - protected function _retrieve_session_data() |
|
597 | - { |
|
598 | - $ssn_key = EE_Session::session_id_prefix . $this->_sid; |
|
599 | - try { |
|
600 | - // we're using WP's Transient API to store session data using the PHP session ID as the option name |
|
601 | - $session_data = $this->cache_storage->get($ssn_key, false); |
|
602 | - if (empty($session_data)) { |
|
603 | - return array(); |
|
604 | - } |
|
605 | - if (apply_filters('FHEE__EE_Session___perform_session_id_hash_check', WP_DEBUG)) { |
|
606 | - $hash_check = $this->cache_storage->get( |
|
607 | - EE_Session::hash_check_prefix . $this->_sid, |
|
608 | - false |
|
609 | - ); |
|
610 | - if ($hash_check && $hash_check !== md5($session_data)) { |
|
611 | - EE_Error::add_error( |
|
612 | - sprintf( |
|
613 | - __( |
|
614 | - 'The stored data for session %1$s failed to pass a hash check and therefore appears to be invalid.', |
|
615 | - 'event_espresso' |
|
616 | - ), |
|
617 | - EE_Session::session_id_prefix . $this->_sid |
|
618 | - ), |
|
619 | - __FILE__, __FUNCTION__, __LINE__ |
|
620 | - ); |
|
621 | - } |
|
622 | - } |
|
623 | - } catch (Exception $e) { |
|
624 | - // let's just eat that error for now and attempt to correct any corrupted data |
|
625 | - global $wpdb; |
|
626 | - $row = $wpdb->get_row( |
|
627 | - $wpdb->prepare( |
|
628 | - "SELECT option_value FROM {$wpdb->options} WHERE option_name = %s LIMIT 1", |
|
629 | - '_transient_' . $ssn_key |
|
630 | - ) |
|
631 | - ); |
|
632 | - $session_data = is_object($row) ? $row->option_value : null; |
|
633 | - if ($session_data) { |
|
634 | - $session_data = preg_replace_callback( |
|
635 | - '!s:(d+):"(.*?)";!', |
|
636 | - function ($match) |
|
637 | - { |
|
638 | - return $match[1] === strlen($match[2]) |
|
639 | - ? $match[0] |
|
640 | - : 's:' . strlen($match[2]) . ':"' . $match[2] . '";'; |
|
641 | - }, |
|
642 | - $session_data |
|
643 | - ); |
|
644 | - } |
|
645 | - $session_data = maybe_unserialize($session_data); |
|
646 | - } |
|
647 | - // in case the data is encoded... try to decode it |
|
648 | - $session_data = $this->encryption instanceof EE_Encryption |
|
649 | - ? $this->encryption->base64_string_decode($session_data) |
|
650 | - : $session_data; |
|
651 | - if (! is_array($session_data)) { |
|
652 | - try { |
|
653 | - $session_data = maybe_unserialize($session_data); |
|
654 | - } catch (Exception $e) { |
|
655 | - $msg = esc_html__( |
|
656 | - 'An error occurred while attempting to unserialize the session data.', |
|
657 | - 'event_espresso' |
|
658 | - ); |
|
659 | - $msg .= WP_DEBUG |
|
660 | - ? '<br><pre>' |
|
661 | - . print_r($session_data, true) |
|
662 | - . '</pre><br>' |
|
663 | - . $this->find_serialize_error($session_data) |
|
664 | - : ''; |
|
665 | - $this->cache_storage->delete(EE_Session::session_id_prefix . $this->_sid); |
|
666 | - throw new InvalidSessionDataException($msg, 0, $e); |
|
667 | - } |
|
668 | - } |
|
669 | - // just a check to make sure the session array is indeed an array |
|
670 | - if (! is_array($session_data)) { |
|
671 | - // no?!?! then something is wrong |
|
672 | - $msg = esc_html__( |
|
673 | - 'The session data is missing, invalid, or corrupted.', |
|
674 | - 'event_espresso' |
|
675 | - ); |
|
676 | - $msg .= WP_DEBUG |
|
677 | - ? '<br><pre>' . print_r($session_data, true) . '</pre><br>' . $this->find_serialize_error($session_data) |
|
678 | - : ''; |
|
679 | - $this->cache_storage->delete(EE_Session::session_id_prefix . $this->_sid); |
|
680 | - throw new InvalidSessionDataException($msg); |
|
681 | - } |
|
682 | - if (isset($session_data['transaction']) && absint($session_data['transaction']) !== 0) { |
|
683 | - $session_data['transaction'] = EEM_Transaction::instance()->get_one_by_ID( |
|
684 | - $session_data['transaction'] |
|
685 | - ); |
|
686 | - } |
|
687 | - return $session_data; |
|
688 | - } |
|
689 | - |
|
690 | - |
|
691 | - |
|
692 | - /** |
|
693 | - * _generate_session_id |
|
694 | - * Retrieves the PHP session id either directly from the PHP session, |
|
695 | - * or from the $_REQUEST array if it was passed in from an AJAX request. |
|
696 | - * The session id is then salted and hashed (mmm sounds tasty) |
|
697 | - * so that it can be safely used as a $_REQUEST param |
|
698 | - * |
|
699 | - * @return string |
|
700 | - */ |
|
701 | - protected function _generate_session_id() |
|
702 | - { |
|
703 | - // check if the SID was passed explicitly, otherwise get from session, then add salt and hash it to reduce length |
|
704 | - if (isset($_REQUEST['EESID'])) { |
|
705 | - $session_id = sanitize_text_field($_REQUEST['EESID']); |
|
706 | - } else { |
|
707 | - $session_id = md5(session_id() . get_current_blog_id() . $this->_get_sid_salt()); |
|
708 | - } |
|
709 | - return apply_filters('FHEE__EE_Session___generate_session_id__session_id', $session_id); |
|
710 | - } |
|
711 | - |
|
712 | - |
|
713 | - |
|
714 | - /** |
|
715 | - * _get_sid_salt |
|
716 | - * |
|
717 | - * @return string |
|
718 | - */ |
|
719 | - protected function _get_sid_salt() |
|
720 | - { |
|
721 | - // was session id salt already saved to db ? |
|
722 | - if (empty($this->_sid_salt)) { |
|
723 | - // no? then maybe use WP defined constant |
|
724 | - if (defined('AUTH_SALT')) { |
|
725 | - $this->_sid_salt = AUTH_SALT; |
|
726 | - } |
|
727 | - // if salt doesn't exist or is too short |
|
728 | - if (strlen($this->_sid_salt) < 32) { |
|
729 | - // create a new one |
|
730 | - $this->_sid_salt = wp_generate_password(64); |
|
731 | - } |
|
732 | - // and save it as a permanent session setting |
|
733 | - $this->updateSessionSettings(array('sid_salt' => $this->_sid_salt)); |
|
734 | - } |
|
735 | - return $this->_sid_salt; |
|
736 | - } |
|
737 | - |
|
738 | - |
|
739 | - |
|
740 | - /** |
|
741 | - * _set_init_access_and_expiration |
|
742 | - * |
|
743 | - * @return void |
|
744 | - */ |
|
745 | - protected function _set_init_access_and_expiration() |
|
746 | - { |
|
747 | - $this->_time = time(); |
|
748 | - $this->_expiration = $this->_time + $this->_lifespan; |
|
749 | - // set initial site access time |
|
750 | - $this->_session_data['init_access'] = $this->_time; |
|
751 | - // and the session expiration |
|
752 | - $this->_session_data['expiration'] = $this->_expiration; |
|
753 | - } |
|
754 | - |
|
755 | - |
|
756 | - |
|
757 | - /** |
|
758 | - * @update session data prior to saving to the db |
|
759 | - * @access public |
|
760 | - * @param bool $new_session |
|
761 | - * @return TRUE on success, FALSE on fail |
|
762 | - * @throws EE_Error |
|
763 | - * @throws InvalidArgumentException |
|
764 | - * @throws InvalidDataTypeException |
|
765 | - * @throws InvalidInterfaceException |
|
766 | - */ |
|
767 | - public function update($new_session = false) |
|
768 | - { |
|
769 | - $this->_session_data = $this->_session_data !== null |
|
770 | - && is_array($this->_session_data) |
|
771 | - && isset($this->_session_data['id']) |
|
772 | - ? $this->_session_data |
|
773 | - : array(); |
|
774 | - if (empty($this->_session_data)) { |
|
775 | - $this->_set_defaults(); |
|
776 | - } |
|
777 | - $session_data = array(); |
|
778 | - foreach ($this->_session_data as $key => $value) { |
|
779 | - |
|
780 | - switch ($key) { |
|
781 | - |
|
782 | - case 'id' : |
|
783 | - // session ID |
|
784 | - $session_data['id'] = $this->_sid; |
|
785 | - break; |
|
786 | - case 'ip_address' : |
|
787 | - // visitor ip address |
|
788 | - $session_data['ip_address'] = $this->request->ipAddress(); |
|
789 | - break; |
|
790 | - case 'user_agent' : |
|
791 | - // visitor user_agent |
|
792 | - $session_data['user_agent'] = $this->_user_agent; |
|
793 | - break; |
|
794 | - case 'init_access' : |
|
795 | - $session_data['init_access'] = absint($value); |
|
796 | - break; |
|
797 | - case 'last_access' : |
|
798 | - // current access time |
|
799 | - $session_data['last_access'] = $this->_time; |
|
800 | - break; |
|
801 | - case 'expiration' : |
|
802 | - // when the session expires |
|
803 | - $session_data['expiration'] = ! empty($this->_expiration) |
|
804 | - ? $this->_expiration |
|
805 | - : $session_data['init_access'] + $this->_lifespan; |
|
806 | - break; |
|
807 | - case 'user_id' : |
|
808 | - // current user if logged in |
|
809 | - $session_data['user_id'] = $this->_wp_user_id(); |
|
810 | - break; |
|
811 | - case 'pages_visited' : |
|
812 | - $page_visit = $this->_get_page_visit(); |
|
813 | - if ($page_visit) { |
|
814 | - // set pages visited where the first will be the http referrer |
|
815 | - $this->_session_data['pages_visited'][ $this->_time ] = $page_visit; |
|
816 | - // we'll only save the last 10 page visits. |
|
817 | - $session_data['pages_visited'] = array_slice($this->_session_data['pages_visited'], -10); |
|
818 | - } |
|
819 | - break; |
|
820 | - default : |
|
821 | - // carry any other data over |
|
822 | - $session_data[ $key ] = $this->_session_data[ $key ]; |
|
823 | - } |
|
824 | - } |
|
825 | - $this->_session_data = $session_data; |
|
826 | - // creating a new session does not require saving to the db just yet |
|
827 | - if (! $new_session) { |
|
828 | - // ready? let's save |
|
829 | - if ($this->_save_session_to_db()) { |
|
830 | - return true; |
|
831 | - } |
|
832 | - return false; |
|
833 | - } |
|
834 | - // meh, why not? |
|
835 | - return true; |
|
836 | - } |
|
837 | - |
|
838 | - |
|
839 | - |
|
840 | - /** |
|
841 | - * @create session data array |
|
842 | - * @access public |
|
843 | - * @return bool |
|
844 | - * @throws EE_Error |
|
845 | - * @throws InvalidArgumentException |
|
846 | - * @throws InvalidDataTypeException |
|
847 | - * @throws InvalidInterfaceException |
|
848 | - */ |
|
849 | - private function _create_espresso_session() |
|
850 | - { |
|
851 | - do_action('AHEE_log', __CLASS__, __FUNCTION__, ''); |
|
852 | - // use the update function for now with $new_session arg set to TRUE |
|
853 | - return $this->update(true) ? true : false; |
|
854 | - } |
|
855 | - |
|
856 | - |
|
857 | - |
|
858 | - /** |
|
859 | - * _save_session_to_db |
|
860 | - * |
|
861 | - * @param bool $clear_session |
|
862 | - * @return string |
|
863 | - * @throws EE_Error |
|
864 | - * @throws InvalidArgumentException |
|
865 | - * @throws InvalidDataTypeException |
|
866 | - * @throws InvalidInterfaceException |
|
867 | - */ |
|
868 | - private function _save_session_to_db($clear_session = false) |
|
869 | - { |
|
870 | - // don't save sessions for crawlers |
|
871 | - // and unless we're deleting the session data, don't save anything if there isn't a cart |
|
872 | - if ($this->request->isBot() || (! $clear_session && ! $this->cart() instanceof EE_Cart)) { |
|
873 | - return false; |
|
874 | - } |
|
875 | - $transaction = $this->transaction(); |
|
876 | - if ($transaction instanceof EE_Transaction) { |
|
877 | - if (! $transaction->ID()) { |
|
878 | - $transaction->save(); |
|
879 | - } |
|
880 | - $this->_session_data['transaction'] = $transaction->ID(); |
|
881 | - } |
|
882 | - // then serialize all of our session data |
|
883 | - $session_data = serialize($this->_session_data); |
|
884 | - // do we need to also encode it to avoid corrupted data when saved to the db? |
|
885 | - $session_data = $this->_use_encryption |
|
886 | - ? $this->encryption->base64_string_encode($session_data) |
|
887 | - : $session_data; |
|
888 | - // maybe save hash check |
|
889 | - if (apply_filters('FHEE__EE_Session___perform_session_id_hash_check', WP_DEBUG)) { |
|
890 | - $this->cache_storage->add( |
|
891 | - EE_Session::hash_check_prefix . $this->_sid, |
|
892 | - md5($session_data), |
|
893 | - $this->_lifespan |
|
894 | - ); |
|
895 | - } |
|
896 | - // we're using the Transient API for storing session data, |
|
897 | - return $this->cache_storage->add( |
|
898 | - EE_Session::session_id_prefix . $this->_sid, |
|
899 | - $session_data, |
|
900 | - $this->_lifespan |
|
901 | - ); |
|
902 | - } |
|
903 | - |
|
904 | - |
|
905 | - /** |
|
906 | - * @get the full page request the visitor is accessing |
|
907 | - * @access public |
|
908 | - * @return string |
|
909 | - */ |
|
910 | - public function _get_page_visit() |
|
911 | - { |
|
912 | - $page_visit = home_url('/') . 'wp-admin/admin-ajax.php'; |
|
913 | - // check for request url |
|
914 | - if (isset($_SERVER['REQUEST_URI'])) { |
|
915 | - $http_host = ''; |
|
916 | - $page_id = '?'; |
|
917 | - $e_reg = ''; |
|
918 | - $request_uri = esc_url($_SERVER['REQUEST_URI']); |
|
919 | - $ru_bits = explode('?', $request_uri); |
|
920 | - $request_uri = $ru_bits[0]; |
|
921 | - // check for and grab host as well |
|
922 | - if (isset($_SERVER['HTTP_HOST'])) { |
|
923 | - $http_host = esc_url($_SERVER['HTTP_HOST']); |
|
924 | - } |
|
925 | - // check for page_id in SERVER REQUEST |
|
926 | - if (isset($_REQUEST['page_id'])) { |
|
927 | - // rebuild $e_reg without any of the extra parameters |
|
928 | - $page_id = '?page_id=' . esc_attr($_REQUEST['page_id']) . '&'; |
|
929 | - } |
|
930 | - // check for $e_reg in SERVER REQUEST |
|
931 | - if (isset($_REQUEST['ee'])) { |
|
932 | - // rebuild $e_reg without any of the extra parameters |
|
933 | - $e_reg = 'ee=' . esc_attr($_REQUEST['ee']); |
|
934 | - } |
|
935 | - $page_visit = rtrim($http_host . $request_uri . $page_id . $e_reg, '?'); |
|
936 | - } |
|
937 | - return $page_visit !== home_url('/wp-admin/admin-ajax.php') ? $page_visit : ''; |
|
938 | - } |
|
939 | - |
|
940 | - |
|
941 | - |
|
942 | - /** |
|
943 | - * @the current wp user id |
|
944 | - * @access public |
|
945 | - * @return int |
|
946 | - */ |
|
947 | - public function _wp_user_id() |
|
948 | - { |
|
949 | - // if I need to explain the following lines of code, then you shouldn't be looking at this! |
|
950 | - $this->_wp_user_id = get_current_user_id(); |
|
951 | - return $this->_wp_user_id; |
|
952 | - } |
|
953 | - |
|
954 | - |
|
955 | - |
|
956 | - /** |
|
957 | - * Clear EE_Session data |
|
958 | - * |
|
959 | - * @access public |
|
960 | - * @param string $class |
|
961 | - * @param string $function |
|
962 | - * @return void |
|
963 | - * @throws EE_Error |
|
964 | - * @throws InvalidArgumentException |
|
965 | - * @throws InvalidDataTypeException |
|
966 | - * @throws InvalidInterfaceException |
|
967 | - */ |
|
968 | - public function clear_session($class = '', $function = '') |
|
969 | - { |
|
970 | - do_action('AHEE_log', __FILE__, __FUNCTION__, 'session cleared by : ' . $class . '::' . $function . '()'); |
|
971 | - $this->reset_cart(); |
|
972 | - $this->reset_checkout(); |
|
973 | - $this->reset_transaction(); |
|
974 | - // wipe out everything that isn't a default session datum |
|
975 | - $this->reset_data(array_keys($this->_session_data)); |
|
976 | - // reset initial site access time and the session expiration |
|
977 | - $this->_set_init_access_and_expiration(); |
|
978 | - $this->_save_session_to_db(true); |
|
979 | - } |
|
980 | - |
|
981 | - |
|
982 | - |
|
983 | - /** |
|
984 | - * @resets all non-default session vars |
|
985 | - * @access public |
|
986 | - * @param array|mixed $data_to_reset |
|
987 | - * @param bool $show_all_notices |
|
988 | - * @return TRUE on success, FALSE on fail |
|
989 | - */ |
|
990 | - public function reset_data($data_to_reset = array(), $show_all_notices = false) |
|
991 | - { |
|
992 | - // if $data_to_reset is not in an array, then put it in one |
|
993 | - if (! is_array($data_to_reset)) { |
|
994 | - $data_to_reset = array($data_to_reset); |
|
995 | - } |
|
996 | - // nothing ??? go home! |
|
997 | - if (empty($data_to_reset)) { |
|
998 | - EE_Error::add_error(__('No session data could be reset, because no session var name was provided.', |
|
999 | - 'event_espresso'), __FILE__, __FUNCTION__, __LINE__); |
|
1000 | - return false; |
|
1001 | - } |
|
1002 | - $return_value = true; |
|
1003 | - // since $data_to_reset is an array, cycle through the values |
|
1004 | - foreach ($data_to_reset as $reset) { |
|
1005 | - |
|
1006 | - // first check to make sure it is a valid session var |
|
1007 | - if (isset($this->_session_data[ $reset ])) { |
|
1008 | - // then check to make sure it is not a default var |
|
1009 | - if (! array_key_exists($reset, $this->_default_session_vars)) { |
|
1010 | - // remove session var |
|
1011 | - unset($this->_session_data[ $reset ]); |
|
1012 | - if ($show_all_notices) { |
|
1013 | - EE_Error::add_success(sprintf(__('The session variable %s was removed.', 'event_espresso'), |
|
1014 | - $reset), __FILE__, __FUNCTION__, __LINE__); |
|
1015 | - } |
|
1016 | - } else { |
|
1017 | - // yeeeeeeeeerrrrrrrrrrr OUT !!!! |
|
1018 | - if ($show_all_notices) { |
|
1019 | - EE_Error::add_error(sprintf(__('Sorry! %s is a default session datum and can not be reset.', |
|
1020 | - 'event_espresso'), $reset), __FILE__, __FUNCTION__, __LINE__); |
|
1021 | - } |
|
1022 | - $return_value = false; |
|
1023 | - } |
|
1024 | - } elseif ($show_all_notices) { |
|
1025 | - // oops! that session var does not exist! |
|
1026 | - EE_Error::add_error(sprintf(__('The session item provided, %s, is invalid or does not exist.', |
|
1027 | - 'event_espresso'), $reset), __FILE__, __FUNCTION__, __LINE__); |
|
1028 | - $return_value = false; |
|
1029 | - } |
|
1030 | - } // end of foreach |
|
1031 | - return $return_value; |
|
1032 | - } |
|
1033 | - |
|
1034 | - |
|
1035 | - |
|
1036 | - /** |
|
1037 | - * wp_loaded |
|
1038 | - * |
|
1039 | - * @access public |
|
1040 | - * @throws EE_Error |
|
1041 | - * @throws InvalidDataTypeException |
|
1042 | - * @throws InvalidInterfaceException |
|
1043 | - * @throws InvalidArgumentException |
|
1044 | - */ |
|
1045 | - public function wp_loaded() |
|
1046 | - { |
|
1047 | - if ($this->request->requestParamIsSet('clear_session')) { |
|
1048 | - $this->clear_session(__CLASS__, __FUNCTION__); |
|
1049 | - } |
|
1050 | - } |
|
1051 | - |
|
1052 | - |
|
1053 | - |
|
1054 | - /** |
|
1055 | - * Used to reset the entire object (for tests). |
|
1056 | - * |
|
1057 | - * @since 4.3.0 |
|
1058 | - * @throws EE_Error |
|
1059 | - * @throws InvalidDataTypeException |
|
1060 | - * @throws InvalidInterfaceException |
|
1061 | - * @throws InvalidArgumentException |
|
1062 | - */ |
|
1063 | - public function reset_instance() |
|
1064 | - { |
|
1065 | - $this->clear_session(); |
|
1066 | - self::$_instance = null; |
|
1067 | - } |
|
1068 | - |
|
1069 | - |
|
1070 | - |
|
1071 | - public function configure_garbage_collection_filters() |
|
1072 | - { |
|
1073 | - // run old filter we had for controlling session cleanup |
|
1074 | - $expired_session_transient_delete_query_limit = absint( |
|
1075 | - apply_filters( |
|
1076 | - 'FHEE__EE_Session__garbage_collection___expired_session_transient_delete_query_limit', |
|
1077 | - 50 |
|
1078 | - ) |
|
1079 | - ); |
|
1080 | - // is there a value? or one that is different than the default 50 records? |
|
1081 | - if ($expired_session_transient_delete_query_limit === 0) { |
|
1082 | - // hook into TransientCacheStorage in case Session cleanup was turned off |
|
1083 | - add_filter('FHEE__TransientCacheStorage__transient_cleanup_schedule', '__return_zero'); |
|
1084 | - } elseif ($expired_session_transient_delete_query_limit !== 50) { |
|
1085 | - // or use that for the new transient cleanup query limit |
|
1086 | - add_filter( |
|
1087 | - 'FHEE__TransientCacheStorage__clearExpiredTransients__limit', |
|
1088 | - function () use ($expired_session_transient_delete_query_limit) |
|
1089 | - { |
|
1090 | - return $expired_session_transient_delete_query_limit; |
|
1091 | - } |
|
1092 | - ); |
|
1093 | - } |
|
1094 | - } |
|
1095 | - |
|
1096 | - |
|
1097 | - |
|
1098 | - /** |
|
1099 | - * @see http://stackoverflow.com/questions/10152904/unserialize-function-unserialize-error-at-offset/21389439#10152996 |
|
1100 | - * @param $data1 |
|
1101 | - * @return string |
|
1102 | - */ |
|
1103 | - private function find_serialize_error($data1) |
|
1104 | - { |
|
1105 | - $error = '<pre>'; |
|
1106 | - $data2 = preg_replace_callback( |
|
1107 | - '!s:(\d+):"(.*?)";!', |
|
1108 | - function ($match) |
|
1109 | - { |
|
1110 | - return ($match[1] === strlen($match[2])) |
|
1111 | - ? $match[0] |
|
1112 | - : 's:' |
|
1113 | - . strlen($match[2]) |
|
1114 | - . ':"' |
|
1115 | - . $match[2] |
|
1116 | - . '";'; |
|
1117 | - }, |
|
1118 | - $data1 |
|
1119 | - ); |
|
1120 | - $max = (strlen($data1) > strlen($data2)) ? strlen($data1) : strlen($data2); |
|
1121 | - $error .= $data1 . PHP_EOL; |
|
1122 | - $error .= $data2 . PHP_EOL; |
|
1123 | - for ($i = 0; $i < $max; $i++) { |
|
1124 | - if (@$data1[ $i ] !== @$data2[ $i ]) { |
|
1125 | - $error .= 'Difference ' . @$data1[ $i ] . ' != ' . @$data2[ $i ] . PHP_EOL; |
|
1126 | - $error .= "\t-> ORD number " . ord(@$data1[ $i ]) . ' != ' . ord(@$data2[ $i ]) . PHP_EOL; |
|
1127 | - $error .= "\t-> Line Number = $i" . PHP_EOL; |
|
1128 | - $start = ($i - 20); |
|
1129 | - $start = ($start < 0) ? 0 : $start; |
|
1130 | - $length = 40; |
|
1131 | - $point = $max - $i; |
|
1132 | - if ($point < 20) { |
|
1133 | - $rlength = 1; |
|
1134 | - $rpoint = -$point; |
|
1135 | - } else { |
|
1136 | - $rpoint = $length - 20; |
|
1137 | - $rlength = 1; |
|
1138 | - } |
|
1139 | - $error .= "\t-> Section Data1 = "; |
|
1140 | - $error .= substr_replace( |
|
1141 | - substr($data1, $start, $length), |
|
1142 | - "<b style=\"color:green\">{$data1[ $i ]}</b>", |
|
1143 | - $rpoint, |
|
1144 | - $rlength |
|
1145 | - ); |
|
1146 | - $error .= PHP_EOL; |
|
1147 | - $error .= "\t-> Section Data2 = "; |
|
1148 | - $error .= substr_replace( |
|
1149 | - substr($data2, $start, $length), |
|
1150 | - "<b style=\"color:red\">{$data2[ $i ]}</b>", |
|
1151 | - $rpoint, |
|
1152 | - $rlength |
|
1153 | - ); |
|
1154 | - $error .= PHP_EOL; |
|
1155 | - } |
|
1156 | - } |
|
1157 | - $error .= '</pre>'; |
|
1158 | - return $error; |
|
1159 | - } |
|
1160 | - |
|
1161 | - |
|
1162 | - /** |
|
1163 | - * Saves an array of settings used for configuring aspects of session behaviour |
|
1164 | - * |
|
1165 | - * @param array $updated_settings |
|
1166 | - */ |
|
1167 | - private function updateSessionSettings(array $updated_settings = array()) |
|
1168 | - { |
|
1169 | - // add existing settings, but only if not included in incoming $updated_settings array |
|
1170 | - $updated_settings += get_option(EE_Session::OPTION_NAME_SETTINGS, array()); |
|
1171 | - update_option(EE_Session::OPTION_NAME_SETTINGS, $updated_settings); |
|
1172 | - } |
|
1173 | - |
|
1174 | - |
|
1175 | - /** |
|
1176 | - * garbage_collection |
|
1177 | - */ |
|
1178 | - public function garbageCollection() |
|
1179 | - { |
|
1180 | - // only perform during regular requests if last garbage collection was over an hour ago |
|
1181 | - if (! (defined('DOING_AJAX') && DOING_AJAX) && (time() - HOUR_IN_SECONDS) >= $this->_last_gc) { |
|
1182 | - $this->_last_gc = time(); |
|
1183 | - $this->updateSessionSettings(array('last_gc' => $this->_last_gc)); |
|
1184 | - /** @type WPDB $wpdb */ |
|
1185 | - global $wpdb; |
|
1186 | - // filter the query limit. Set to 0 to turn off garbage collection |
|
1187 | - $expired_session_transient_delete_query_limit = absint( |
|
1188 | - apply_filters( |
|
1189 | - 'FHEE__EE_Session__garbage_collection___expired_session_transient_delete_query_limit', |
|
1190 | - 50 |
|
1191 | - ) |
|
1192 | - ); |
|
1193 | - // non-zero LIMIT means take out the trash |
|
1194 | - if ($expired_session_transient_delete_query_limit) { |
|
1195 | - $session_key = str_replace('_', '\_', EE_Session::session_id_prefix); |
|
1196 | - $hash_check_key = str_replace('_', '\_', EE_Session::hash_check_prefix); |
|
1197 | - // since transient expiration timestamps are set in the future, we can compare against NOW |
|
1198 | - // but we only want to pick up any trash that's been around for more than a day |
|
1199 | - $expiration = time() - DAY_IN_SECONDS; |
|
1200 | - $SQL = " |
|
374 | + /** |
|
375 | + * reset_cart |
|
376 | + */ |
|
377 | + public function reset_cart() |
|
378 | + { |
|
379 | + do_action('AHEE__EE_Session__reset_cart__before_reset', $this); |
|
380 | + $this->_session_data['cart'] = null; |
|
381 | + } |
|
382 | + |
|
383 | + |
|
384 | + |
|
385 | + /** |
|
386 | + * @return \EE_Cart |
|
387 | + */ |
|
388 | + public function cart() |
|
389 | + { |
|
390 | + return isset($this->_session_data['cart']) && $this->_session_data['cart'] instanceof EE_Cart |
|
391 | + ? $this->_session_data['cart'] |
|
392 | + : null; |
|
393 | + } |
|
394 | + |
|
395 | + |
|
396 | + |
|
397 | + /** |
|
398 | + * @param \EE_Checkout $checkout |
|
399 | + * @return bool |
|
400 | + */ |
|
401 | + public function set_checkout(EE_Checkout $checkout) |
|
402 | + { |
|
403 | + $this->_session_data['checkout'] = $checkout; |
|
404 | + return true; |
|
405 | + } |
|
406 | + |
|
407 | + |
|
408 | + |
|
409 | + /** |
|
410 | + * reset_checkout |
|
411 | + */ |
|
412 | + public function reset_checkout() |
|
413 | + { |
|
414 | + do_action('AHEE__EE_Session__reset_checkout__before_reset', $this); |
|
415 | + $this->_session_data['checkout'] = null; |
|
416 | + } |
|
417 | + |
|
418 | + |
|
419 | + |
|
420 | + /** |
|
421 | + * @return \EE_Checkout |
|
422 | + */ |
|
423 | + public function checkout() |
|
424 | + { |
|
425 | + return isset($this->_session_data['checkout']) && $this->_session_data['checkout'] instanceof EE_Checkout |
|
426 | + ? $this->_session_data['checkout'] |
|
427 | + : null; |
|
428 | + } |
|
429 | + |
|
430 | + |
|
431 | + |
|
432 | + /** |
|
433 | + * @param \EE_Transaction $transaction |
|
434 | + * @return bool |
|
435 | + * @throws EE_Error |
|
436 | + */ |
|
437 | + public function set_transaction(EE_Transaction $transaction) |
|
438 | + { |
|
439 | + // first remove the session from the transaction before we save the transaction in the session |
|
440 | + $transaction->set_txn_session_data(null); |
|
441 | + $this->_session_data['transaction'] = $transaction; |
|
442 | + return true; |
|
443 | + } |
|
444 | + |
|
445 | + |
|
446 | + |
|
447 | + /** |
|
448 | + * reset_transaction |
|
449 | + */ |
|
450 | + public function reset_transaction() |
|
451 | + { |
|
452 | + do_action('AHEE__EE_Session__reset_transaction__before_reset', $this); |
|
453 | + $this->_session_data['transaction'] = null; |
|
454 | + } |
|
455 | + |
|
456 | + |
|
457 | + |
|
458 | + /** |
|
459 | + * @return \EE_Transaction |
|
460 | + */ |
|
461 | + public function transaction() |
|
462 | + { |
|
463 | + return isset($this->_session_data['transaction']) |
|
464 | + && $this->_session_data['transaction'] instanceof EE_Transaction |
|
465 | + ? $this->_session_data['transaction'] |
|
466 | + : null; |
|
467 | + } |
|
468 | + |
|
469 | + |
|
470 | + |
|
471 | + /** |
|
472 | + * retrieve session data |
|
473 | + * |
|
474 | + * @access public |
|
475 | + * @param null $key |
|
476 | + * @param bool $reset_cache |
|
477 | + * @return array |
|
478 | + */ |
|
479 | + public function get_session_data($key = null, $reset_cache = false) |
|
480 | + { |
|
481 | + if ($reset_cache) { |
|
482 | + $this->reset_cart(); |
|
483 | + $this->reset_checkout(); |
|
484 | + $this->reset_transaction(); |
|
485 | + } |
|
486 | + if (! empty($key)) { |
|
487 | + return isset($this->_session_data[ $key ]) ? $this->_session_data[ $key ] : null; |
|
488 | + } |
|
489 | + return $this->_session_data; |
|
490 | + } |
|
491 | + |
|
492 | + |
|
493 | + |
|
494 | + /** |
|
495 | + * set session data |
|
496 | + * |
|
497 | + * @access public |
|
498 | + * @param array $data |
|
499 | + * @return TRUE on success, FALSE on fail |
|
500 | + */ |
|
501 | + public function set_session_data($data) |
|
502 | + { |
|
503 | + |
|
504 | + // nothing ??? bad data ??? go home! |
|
505 | + if (empty($data) || ! is_array($data)) { |
|
506 | + EE_Error::add_error(__('No session data or invalid session data was provided.', 'event_espresso'), __FILE__, |
|
507 | + __FUNCTION__, __LINE__); |
|
508 | + return false; |
|
509 | + } |
|
510 | + foreach ($data as $key => $value) { |
|
511 | + if (isset($this->_default_session_vars[ $key ])) { |
|
512 | + EE_Error::add_error(sprintf(__('Sorry! %s is a default session datum and can not be reset.', |
|
513 | + 'event_espresso'), $key), __FILE__, __FUNCTION__, __LINE__); |
|
514 | + return false; |
|
515 | + } |
|
516 | + $this->_session_data[ $key ] = $value; |
|
517 | + } |
|
518 | + return true; |
|
519 | + } |
|
520 | + |
|
521 | + |
|
522 | + |
|
523 | + /** |
|
524 | + * @initiate session |
|
525 | + * @access private |
|
526 | + * @return TRUE on success, FALSE on fail |
|
527 | + * @throws EE_Error |
|
528 | + * @throws InvalidArgumentException |
|
529 | + * @throws InvalidDataTypeException |
|
530 | + * @throws InvalidInterfaceException |
|
531 | + * @throws InvalidSessionDataException |
|
532 | + */ |
|
533 | + private function _espresso_session() |
|
534 | + { |
|
535 | + do_action('AHEE_log', __FILE__, __FUNCTION__, ''); |
|
536 | + // check that session has started |
|
537 | + if (session_id() === '') { |
|
538 | + //starts a new session if one doesn't already exist, or re-initiates an existing one |
|
539 | + session_start(); |
|
540 | + } |
|
541 | + // get our modified session ID |
|
542 | + $this->_sid = $this->_generate_session_id(); |
|
543 | + // and the visitors IP |
|
544 | + $this->_ip_address = $this->request->ipAddress(); |
|
545 | + // set the "user agent" |
|
546 | + $this->_user_agent = $this->request->userAgent(); |
|
547 | + // now let's retrieve what's in the db |
|
548 | + $session_data = $this->_retrieve_session_data(); |
|
549 | + if (! empty($session_data)) { |
|
550 | + // get the current time in UTC |
|
551 | + $this->_time = $this->_time !== null ? $this->_time : time(); |
|
552 | + // and reset the session expiration |
|
553 | + $this->_expiration = isset($session_data['expiration']) |
|
554 | + ? $session_data['expiration'] |
|
555 | + : $this->_time + $this->_lifespan; |
|
556 | + } else { |
|
557 | + // set initial site access time and the session expiration |
|
558 | + $this->_set_init_access_and_expiration(); |
|
559 | + // set referer |
|
560 | + $this->_session_data['pages_visited'][ $this->_session_data['init_access'] ] = isset($_SERVER['HTTP_REFERER']) |
|
561 | + ? esc_attr($_SERVER['HTTP_REFERER']) |
|
562 | + : ''; |
|
563 | + // no previous session = go back and create one (on top of the data above) |
|
564 | + return false; |
|
565 | + } |
|
566 | + // now the user agent |
|
567 | + if ($session_data['user_agent'] !== $this->_user_agent) { |
|
568 | + return false; |
|
569 | + } |
|
570 | + // wait a minute... how old are you? |
|
571 | + if ($this->_time > $this->_expiration) { |
|
572 | + // yer too old fer me! |
|
573 | + $this->_expired = true; |
|
574 | + // wipe out everything that isn't a default session datum |
|
575 | + $this->clear_session(__CLASS__, __FUNCTION__); |
|
576 | + } |
|
577 | + // make event espresso session data available to plugin |
|
578 | + $this->_session_data = array_merge($this->_session_data, $session_data); |
|
579 | + return true; |
|
580 | + } |
|
581 | + |
|
582 | + |
|
583 | + |
|
584 | + /** |
|
585 | + * _get_session_data |
|
586 | + * Retrieves the session data, and attempts to correct any encoding issues that can occur due to improperly setup |
|
587 | + * databases |
|
588 | + * |
|
589 | + * @return array |
|
590 | + * @throws EE_Error |
|
591 | + * @throws InvalidArgumentException |
|
592 | + * @throws InvalidSessionDataException |
|
593 | + * @throws InvalidDataTypeException |
|
594 | + * @throws InvalidInterfaceException |
|
595 | + */ |
|
596 | + protected function _retrieve_session_data() |
|
597 | + { |
|
598 | + $ssn_key = EE_Session::session_id_prefix . $this->_sid; |
|
599 | + try { |
|
600 | + // we're using WP's Transient API to store session data using the PHP session ID as the option name |
|
601 | + $session_data = $this->cache_storage->get($ssn_key, false); |
|
602 | + if (empty($session_data)) { |
|
603 | + return array(); |
|
604 | + } |
|
605 | + if (apply_filters('FHEE__EE_Session___perform_session_id_hash_check', WP_DEBUG)) { |
|
606 | + $hash_check = $this->cache_storage->get( |
|
607 | + EE_Session::hash_check_prefix . $this->_sid, |
|
608 | + false |
|
609 | + ); |
|
610 | + if ($hash_check && $hash_check !== md5($session_data)) { |
|
611 | + EE_Error::add_error( |
|
612 | + sprintf( |
|
613 | + __( |
|
614 | + 'The stored data for session %1$s failed to pass a hash check and therefore appears to be invalid.', |
|
615 | + 'event_espresso' |
|
616 | + ), |
|
617 | + EE_Session::session_id_prefix . $this->_sid |
|
618 | + ), |
|
619 | + __FILE__, __FUNCTION__, __LINE__ |
|
620 | + ); |
|
621 | + } |
|
622 | + } |
|
623 | + } catch (Exception $e) { |
|
624 | + // let's just eat that error for now and attempt to correct any corrupted data |
|
625 | + global $wpdb; |
|
626 | + $row = $wpdb->get_row( |
|
627 | + $wpdb->prepare( |
|
628 | + "SELECT option_value FROM {$wpdb->options} WHERE option_name = %s LIMIT 1", |
|
629 | + '_transient_' . $ssn_key |
|
630 | + ) |
|
631 | + ); |
|
632 | + $session_data = is_object($row) ? $row->option_value : null; |
|
633 | + if ($session_data) { |
|
634 | + $session_data = preg_replace_callback( |
|
635 | + '!s:(d+):"(.*?)";!', |
|
636 | + function ($match) |
|
637 | + { |
|
638 | + return $match[1] === strlen($match[2]) |
|
639 | + ? $match[0] |
|
640 | + : 's:' . strlen($match[2]) . ':"' . $match[2] . '";'; |
|
641 | + }, |
|
642 | + $session_data |
|
643 | + ); |
|
644 | + } |
|
645 | + $session_data = maybe_unserialize($session_data); |
|
646 | + } |
|
647 | + // in case the data is encoded... try to decode it |
|
648 | + $session_data = $this->encryption instanceof EE_Encryption |
|
649 | + ? $this->encryption->base64_string_decode($session_data) |
|
650 | + : $session_data; |
|
651 | + if (! is_array($session_data)) { |
|
652 | + try { |
|
653 | + $session_data = maybe_unserialize($session_data); |
|
654 | + } catch (Exception $e) { |
|
655 | + $msg = esc_html__( |
|
656 | + 'An error occurred while attempting to unserialize the session data.', |
|
657 | + 'event_espresso' |
|
658 | + ); |
|
659 | + $msg .= WP_DEBUG |
|
660 | + ? '<br><pre>' |
|
661 | + . print_r($session_data, true) |
|
662 | + . '</pre><br>' |
|
663 | + . $this->find_serialize_error($session_data) |
|
664 | + : ''; |
|
665 | + $this->cache_storage->delete(EE_Session::session_id_prefix . $this->_sid); |
|
666 | + throw new InvalidSessionDataException($msg, 0, $e); |
|
667 | + } |
|
668 | + } |
|
669 | + // just a check to make sure the session array is indeed an array |
|
670 | + if (! is_array($session_data)) { |
|
671 | + // no?!?! then something is wrong |
|
672 | + $msg = esc_html__( |
|
673 | + 'The session data is missing, invalid, or corrupted.', |
|
674 | + 'event_espresso' |
|
675 | + ); |
|
676 | + $msg .= WP_DEBUG |
|
677 | + ? '<br><pre>' . print_r($session_data, true) . '</pre><br>' . $this->find_serialize_error($session_data) |
|
678 | + : ''; |
|
679 | + $this->cache_storage->delete(EE_Session::session_id_prefix . $this->_sid); |
|
680 | + throw new InvalidSessionDataException($msg); |
|
681 | + } |
|
682 | + if (isset($session_data['transaction']) && absint($session_data['transaction']) !== 0) { |
|
683 | + $session_data['transaction'] = EEM_Transaction::instance()->get_one_by_ID( |
|
684 | + $session_data['transaction'] |
|
685 | + ); |
|
686 | + } |
|
687 | + return $session_data; |
|
688 | + } |
|
689 | + |
|
690 | + |
|
691 | + |
|
692 | + /** |
|
693 | + * _generate_session_id |
|
694 | + * Retrieves the PHP session id either directly from the PHP session, |
|
695 | + * or from the $_REQUEST array if it was passed in from an AJAX request. |
|
696 | + * The session id is then salted and hashed (mmm sounds tasty) |
|
697 | + * so that it can be safely used as a $_REQUEST param |
|
698 | + * |
|
699 | + * @return string |
|
700 | + */ |
|
701 | + protected function _generate_session_id() |
|
702 | + { |
|
703 | + // check if the SID was passed explicitly, otherwise get from session, then add salt and hash it to reduce length |
|
704 | + if (isset($_REQUEST['EESID'])) { |
|
705 | + $session_id = sanitize_text_field($_REQUEST['EESID']); |
|
706 | + } else { |
|
707 | + $session_id = md5(session_id() . get_current_blog_id() . $this->_get_sid_salt()); |
|
708 | + } |
|
709 | + return apply_filters('FHEE__EE_Session___generate_session_id__session_id', $session_id); |
|
710 | + } |
|
711 | + |
|
712 | + |
|
713 | + |
|
714 | + /** |
|
715 | + * _get_sid_salt |
|
716 | + * |
|
717 | + * @return string |
|
718 | + */ |
|
719 | + protected function _get_sid_salt() |
|
720 | + { |
|
721 | + // was session id salt already saved to db ? |
|
722 | + if (empty($this->_sid_salt)) { |
|
723 | + // no? then maybe use WP defined constant |
|
724 | + if (defined('AUTH_SALT')) { |
|
725 | + $this->_sid_salt = AUTH_SALT; |
|
726 | + } |
|
727 | + // if salt doesn't exist or is too short |
|
728 | + if (strlen($this->_sid_salt) < 32) { |
|
729 | + // create a new one |
|
730 | + $this->_sid_salt = wp_generate_password(64); |
|
731 | + } |
|
732 | + // and save it as a permanent session setting |
|
733 | + $this->updateSessionSettings(array('sid_salt' => $this->_sid_salt)); |
|
734 | + } |
|
735 | + return $this->_sid_salt; |
|
736 | + } |
|
737 | + |
|
738 | + |
|
739 | + |
|
740 | + /** |
|
741 | + * _set_init_access_and_expiration |
|
742 | + * |
|
743 | + * @return void |
|
744 | + */ |
|
745 | + protected function _set_init_access_and_expiration() |
|
746 | + { |
|
747 | + $this->_time = time(); |
|
748 | + $this->_expiration = $this->_time + $this->_lifespan; |
|
749 | + // set initial site access time |
|
750 | + $this->_session_data['init_access'] = $this->_time; |
|
751 | + // and the session expiration |
|
752 | + $this->_session_data['expiration'] = $this->_expiration; |
|
753 | + } |
|
754 | + |
|
755 | + |
|
756 | + |
|
757 | + /** |
|
758 | + * @update session data prior to saving to the db |
|
759 | + * @access public |
|
760 | + * @param bool $new_session |
|
761 | + * @return TRUE on success, FALSE on fail |
|
762 | + * @throws EE_Error |
|
763 | + * @throws InvalidArgumentException |
|
764 | + * @throws InvalidDataTypeException |
|
765 | + * @throws InvalidInterfaceException |
|
766 | + */ |
|
767 | + public function update($new_session = false) |
|
768 | + { |
|
769 | + $this->_session_data = $this->_session_data !== null |
|
770 | + && is_array($this->_session_data) |
|
771 | + && isset($this->_session_data['id']) |
|
772 | + ? $this->_session_data |
|
773 | + : array(); |
|
774 | + if (empty($this->_session_data)) { |
|
775 | + $this->_set_defaults(); |
|
776 | + } |
|
777 | + $session_data = array(); |
|
778 | + foreach ($this->_session_data as $key => $value) { |
|
779 | + |
|
780 | + switch ($key) { |
|
781 | + |
|
782 | + case 'id' : |
|
783 | + // session ID |
|
784 | + $session_data['id'] = $this->_sid; |
|
785 | + break; |
|
786 | + case 'ip_address' : |
|
787 | + // visitor ip address |
|
788 | + $session_data['ip_address'] = $this->request->ipAddress(); |
|
789 | + break; |
|
790 | + case 'user_agent' : |
|
791 | + // visitor user_agent |
|
792 | + $session_data['user_agent'] = $this->_user_agent; |
|
793 | + break; |
|
794 | + case 'init_access' : |
|
795 | + $session_data['init_access'] = absint($value); |
|
796 | + break; |
|
797 | + case 'last_access' : |
|
798 | + // current access time |
|
799 | + $session_data['last_access'] = $this->_time; |
|
800 | + break; |
|
801 | + case 'expiration' : |
|
802 | + // when the session expires |
|
803 | + $session_data['expiration'] = ! empty($this->_expiration) |
|
804 | + ? $this->_expiration |
|
805 | + : $session_data['init_access'] + $this->_lifespan; |
|
806 | + break; |
|
807 | + case 'user_id' : |
|
808 | + // current user if logged in |
|
809 | + $session_data['user_id'] = $this->_wp_user_id(); |
|
810 | + break; |
|
811 | + case 'pages_visited' : |
|
812 | + $page_visit = $this->_get_page_visit(); |
|
813 | + if ($page_visit) { |
|
814 | + // set pages visited where the first will be the http referrer |
|
815 | + $this->_session_data['pages_visited'][ $this->_time ] = $page_visit; |
|
816 | + // we'll only save the last 10 page visits. |
|
817 | + $session_data['pages_visited'] = array_slice($this->_session_data['pages_visited'], -10); |
|
818 | + } |
|
819 | + break; |
|
820 | + default : |
|
821 | + // carry any other data over |
|
822 | + $session_data[ $key ] = $this->_session_data[ $key ]; |
|
823 | + } |
|
824 | + } |
|
825 | + $this->_session_data = $session_data; |
|
826 | + // creating a new session does not require saving to the db just yet |
|
827 | + if (! $new_session) { |
|
828 | + // ready? let's save |
|
829 | + if ($this->_save_session_to_db()) { |
|
830 | + return true; |
|
831 | + } |
|
832 | + return false; |
|
833 | + } |
|
834 | + // meh, why not? |
|
835 | + return true; |
|
836 | + } |
|
837 | + |
|
838 | + |
|
839 | + |
|
840 | + /** |
|
841 | + * @create session data array |
|
842 | + * @access public |
|
843 | + * @return bool |
|
844 | + * @throws EE_Error |
|
845 | + * @throws InvalidArgumentException |
|
846 | + * @throws InvalidDataTypeException |
|
847 | + * @throws InvalidInterfaceException |
|
848 | + */ |
|
849 | + private function _create_espresso_session() |
|
850 | + { |
|
851 | + do_action('AHEE_log', __CLASS__, __FUNCTION__, ''); |
|
852 | + // use the update function for now with $new_session arg set to TRUE |
|
853 | + return $this->update(true) ? true : false; |
|
854 | + } |
|
855 | + |
|
856 | + |
|
857 | + |
|
858 | + /** |
|
859 | + * _save_session_to_db |
|
860 | + * |
|
861 | + * @param bool $clear_session |
|
862 | + * @return string |
|
863 | + * @throws EE_Error |
|
864 | + * @throws InvalidArgumentException |
|
865 | + * @throws InvalidDataTypeException |
|
866 | + * @throws InvalidInterfaceException |
|
867 | + */ |
|
868 | + private function _save_session_to_db($clear_session = false) |
|
869 | + { |
|
870 | + // don't save sessions for crawlers |
|
871 | + // and unless we're deleting the session data, don't save anything if there isn't a cart |
|
872 | + if ($this->request->isBot() || (! $clear_session && ! $this->cart() instanceof EE_Cart)) { |
|
873 | + return false; |
|
874 | + } |
|
875 | + $transaction = $this->transaction(); |
|
876 | + if ($transaction instanceof EE_Transaction) { |
|
877 | + if (! $transaction->ID()) { |
|
878 | + $transaction->save(); |
|
879 | + } |
|
880 | + $this->_session_data['transaction'] = $transaction->ID(); |
|
881 | + } |
|
882 | + // then serialize all of our session data |
|
883 | + $session_data = serialize($this->_session_data); |
|
884 | + // do we need to also encode it to avoid corrupted data when saved to the db? |
|
885 | + $session_data = $this->_use_encryption |
|
886 | + ? $this->encryption->base64_string_encode($session_data) |
|
887 | + : $session_data; |
|
888 | + // maybe save hash check |
|
889 | + if (apply_filters('FHEE__EE_Session___perform_session_id_hash_check', WP_DEBUG)) { |
|
890 | + $this->cache_storage->add( |
|
891 | + EE_Session::hash_check_prefix . $this->_sid, |
|
892 | + md5($session_data), |
|
893 | + $this->_lifespan |
|
894 | + ); |
|
895 | + } |
|
896 | + // we're using the Transient API for storing session data, |
|
897 | + return $this->cache_storage->add( |
|
898 | + EE_Session::session_id_prefix . $this->_sid, |
|
899 | + $session_data, |
|
900 | + $this->_lifespan |
|
901 | + ); |
|
902 | + } |
|
903 | + |
|
904 | + |
|
905 | + /** |
|
906 | + * @get the full page request the visitor is accessing |
|
907 | + * @access public |
|
908 | + * @return string |
|
909 | + */ |
|
910 | + public function _get_page_visit() |
|
911 | + { |
|
912 | + $page_visit = home_url('/') . 'wp-admin/admin-ajax.php'; |
|
913 | + // check for request url |
|
914 | + if (isset($_SERVER['REQUEST_URI'])) { |
|
915 | + $http_host = ''; |
|
916 | + $page_id = '?'; |
|
917 | + $e_reg = ''; |
|
918 | + $request_uri = esc_url($_SERVER['REQUEST_URI']); |
|
919 | + $ru_bits = explode('?', $request_uri); |
|
920 | + $request_uri = $ru_bits[0]; |
|
921 | + // check for and grab host as well |
|
922 | + if (isset($_SERVER['HTTP_HOST'])) { |
|
923 | + $http_host = esc_url($_SERVER['HTTP_HOST']); |
|
924 | + } |
|
925 | + // check for page_id in SERVER REQUEST |
|
926 | + if (isset($_REQUEST['page_id'])) { |
|
927 | + // rebuild $e_reg without any of the extra parameters |
|
928 | + $page_id = '?page_id=' . esc_attr($_REQUEST['page_id']) . '&'; |
|
929 | + } |
|
930 | + // check for $e_reg in SERVER REQUEST |
|
931 | + if (isset($_REQUEST['ee'])) { |
|
932 | + // rebuild $e_reg without any of the extra parameters |
|
933 | + $e_reg = 'ee=' . esc_attr($_REQUEST['ee']); |
|
934 | + } |
|
935 | + $page_visit = rtrim($http_host . $request_uri . $page_id . $e_reg, '?'); |
|
936 | + } |
|
937 | + return $page_visit !== home_url('/wp-admin/admin-ajax.php') ? $page_visit : ''; |
|
938 | + } |
|
939 | + |
|
940 | + |
|
941 | + |
|
942 | + /** |
|
943 | + * @the current wp user id |
|
944 | + * @access public |
|
945 | + * @return int |
|
946 | + */ |
|
947 | + public function _wp_user_id() |
|
948 | + { |
|
949 | + // if I need to explain the following lines of code, then you shouldn't be looking at this! |
|
950 | + $this->_wp_user_id = get_current_user_id(); |
|
951 | + return $this->_wp_user_id; |
|
952 | + } |
|
953 | + |
|
954 | + |
|
955 | + |
|
956 | + /** |
|
957 | + * Clear EE_Session data |
|
958 | + * |
|
959 | + * @access public |
|
960 | + * @param string $class |
|
961 | + * @param string $function |
|
962 | + * @return void |
|
963 | + * @throws EE_Error |
|
964 | + * @throws InvalidArgumentException |
|
965 | + * @throws InvalidDataTypeException |
|
966 | + * @throws InvalidInterfaceException |
|
967 | + */ |
|
968 | + public function clear_session($class = '', $function = '') |
|
969 | + { |
|
970 | + do_action('AHEE_log', __FILE__, __FUNCTION__, 'session cleared by : ' . $class . '::' . $function . '()'); |
|
971 | + $this->reset_cart(); |
|
972 | + $this->reset_checkout(); |
|
973 | + $this->reset_transaction(); |
|
974 | + // wipe out everything that isn't a default session datum |
|
975 | + $this->reset_data(array_keys($this->_session_data)); |
|
976 | + // reset initial site access time and the session expiration |
|
977 | + $this->_set_init_access_and_expiration(); |
|
978 | + $this->_save_session_to_db(true); |
|
979 | + } |
|
980 | + |
|
981 | + |
|
982 | + |
|
983 | + /** |
|
984 | + * @resets all non-default session vars |
|
985 | + * @access public |
|
986 | + * @param array|mixed $data_to_reset |
|
987 | + * @param bool $show_all_notices |
|
988 | + * @return TRUE on success, FALSE on fail |
|
989 | + */ |
|
990 | + public function reset_data($data_to_reset = array(), $show_all_notices = false) |
|
991 | + { |
|
992 | + // if $data_to_reset is not in an array, then put it in one |
|
993 | + if (! is_array($data_to_reset)) { |
|
994 | + $data_to_reset = array($data_to_reset); |
|
995 | + } |
|
996 | + // nothing ??? go home! |
|
997 | + if (empty($data_to_reset)) { |
|
998 | + EE_Error::add_error(__('No session data could be reset, because no session var name was provided.', |
|
999 | + 'event_espresso'), __FILE__, __FUNCTION__, __LINE__); |
|
1000 | + return false; |
|
1001 | + } |
|
1002 | + $return_value = true; |
|
1003 | + // since $data_to_reset is an array, cycle through the values |
|
1004 | + foreach ($data_to_reset as $reset) { |
|
1005 | + |
|
1006 | + // first check to make sure it is a valid session var |
|
1007 | + if (isset($this->_session_data[ $reset ])) { |
|
1008 | + // then check to make sure it is not a default var |
|
1009 | + if (! array_key_exists($reset, $this->_default_session_vars)) { |
|
1010 | + // remove session var |
|
1011 | + unset($this->_session_data[ $reset ]); |
|
1012 | + if ($show_all_notices) { |
|
1013 | + EE_Error::add_success(sprintf(__('The session variable %s was removed.', 'event_espresso'), |
|
1014 | + $reset), __FILE__, __FUNCTION__, __LINE__); |
|
1015 | + } |
|
1016 | + } else { |
|
1017 | + // yeeeeeeeeerrrrrrrrrrr OUT !!!! |
|
1018 | + if ($show_all_notices) { |
|
1019 | + EE_Error::add_error(sprintf(__('Sorry! %s is a default session datum and can not be reset.', |
|
1020 | + 'event_espresso'), $reset), __FILE__, __FUNCTION__, __LINE__); |
|
1021 | + } |
|
1022 | + $return_value = false; |
|
1023 | + } |
|
1024 | + } elseif ($show_all_notices) { |
|
1025 | + // oops! that session var does not exist! |
|
1026 | + EE_Error::add_error(sprintf(__('The session item provided, %s, is invalid or does not exist.', |
|
1027 | + 'event_espresso'), $reset), __FILE__, __FUNCTION__, __LINE__); |
|
1028 | + $return_value = false; |
|
1029 | + } |
|
1030 | + } // end of foreach |
|
1031 | + return $return_value; |
|
1032 | + } |
|
1033 | + |
|
1034 | + |
|
1035 | + |
|
1036 | + /** |
|
1037 | + * wp_loaded |
|
1038 | + * |
|
1039 | + * @access public |
|
1040 | + * @throws EE_Error |
|
1041 | + * @throws InvalidDataTypeException |
|
1042 | + * @throws InvalidInterfaceException |
|
1043 | + * @throws InvalidArgumentException |
|
1044 | + */ |
|
1045 | + public function wp_loaded() |
|
1046 | + { |
|
1047 | + if ($this->request->requestParamIsSet('clear_session')) { |
|
1048 | + $this->clear_session(__CLASS__, __FUNCTION__); |
|
1049 | + } |
|
1050 | + } |
|
1051 | + |
|
1052 | + |
|
1053 | + |
|
1054 | + /** |
|
1055 | + * Used to reset the entire object (for tests). |
|
1056 | + * |
|
1057 | + * @since 4.3.0 |
|
1058 | + * @throws EE_Error |
|
1059 | + * @throws InvalidDataTypeException |
|
1060 | + * @throws InvalidInterfaceException |
|
1061 | + * @throws InvalidArgumentException |
|
1062 | + */ |
|
1063 | + public function reset_instance() |
|
1064 | + { |
|
1065 | + $this->clear_session(); |
|
1066 | + self::$_instance = null; |
|
1067 | + } |
|
1068 | + |
|
1069 | + |
|
1070 | + |
|
1071 | + public function configure_garbage_collection_filters() |
|
1072 | + { |
|
1073 | + // run old filter we had for controlling session cleanup |
|
1074 | + $expired_session_transient_delete_query_limit = absint( |
|
1075 | + apply_filters( |
|
1076 | + 'FHEE__EE_Session__garbage_collection___expired_session_transient_delete_query_limit', |
|
1077 | + 50 |
|
1078 | + ) |
|
1079 | + ); |
|
1080 | + // is there a value? or one that is different than the default 50 records? |
|
1081 | + if ($expired_session_transient_delete_query_limit === 0) { |
|
1082 | + // hook into TransientCacheStorage in case Session cleanup was turned off |
|
1083 | + add_filter('FHEE__TransientCacheStorage__transient_cleanup_schedule', '__return_zero'); |
|
1084 | + } elseif ($expired_session_transient_delete_query_limit !== 50) { |
|
1085 | + // or use that for the new transient cleanup query limit |
|
1086 | + add_filter( |
|
1087 | + 'FHEE__TransientCacheStorage__clearExpiredTransients__limit', |
|
1088 | + function () use ($expired_session_transient_delete_query_limit) |
|
1089 | + { |
|
1090 | + return $expired_session_transient_delete_query_limit; |
|
1091 | + } |
|
1092 | + ); |
|
1093 | + } |
|
1094 | + } |
|
1095 | + |
|
1096 | + |
|
1097 | + |
|
1098 | + /** |
|
1099 | + * @see http://stackoverflow.com/questions/10152904/unserialize-function-unserialize-error-at-offset/21389439#10152996 |
|
1100 | + * @param $data1 |
|
1101 | + * @return string |
|
1102 | + */ |
|
1103 | + private function find_serialize_error($data1) |
|
1104 | + { |
|
1105 | + $error = '<pre>'; |
|
1106 | + $data2 = preg_replace_callback( |
|
1107 | + '!s:(\d+):"(.*?)";!', |
|
1108 | + function ($match) |
|
1109 | + { |
|
1110 | + return ($match[1] === strlen($match[2])) |
|
1111 | + ? $match[0] |
|
1112 | + : 's:' |
|
1113 | + . strlen($match[2]) |
|
1114 | + . ':"' |
|
1115 | + . $match[2] |
|
1116 | + . '";'; |
|
1117 | + }, |
|
1118 | + $data1 |
|
1119 | + ); |
|
1120 | + $max = (strlen($data1) > strlen($data2)) ? strlen($data1) : strlen($data2); |
|
1121 | + $error .= $data1 . PHP_EOL; |
|
1122 | + $error .= $data2 . PHP_EOL; |
|
1123 | + for ($i = 0; $i < $max; $i++) { |
|
1124 | + if (@$data1[ $i ] !== @$data2[ $i ]) { |
|
1125 | + $error .= 'Difference ' . @$data1[ $i ] . ' != ' . @$data2[ $i ] . PHP_EOL; |
|
1126 | + $error .= "\t-> ORD number " . ord(@$data1[ $i ]) . ' != ' . ord(@$data2[ $i ]) . PHP_EOL; |
|
1127 | + $error .= "\t-> Line Number = $i" . PHP_EOL; |
|
1128 | + $start = ($i - 20); |
|
1129 | + $start = ($start < 0) ? 0 : $start; |
|
1130 | + $length = 40; |
|
1131 | + $point = $max - $i; |
|
1132 | + if ($point < 20) { |
|
1133 | + $rlength = 1; |
|
1134 | + $rpoint = -$point; |
|
1135 | + } else { |
|
1136 | + $rpoint = $length - 20; |
|
1137 | + $rlength = 1; |
|
1138 | + } |
|
1139 | + $error .= "\t-> Section Data1 = "; |
|
1140 | + $error .= substr_replace( |
|
1141 | + substr($data1, $start, $length), |
|
1142 | + "<b style=\"color:green\">{$data1[ $i ]}</b>", |
|
1143 | + $rpoint, |
|
1144 | + $rlength |
|
1145 | + ); |
|
1146 | + $error .= PHP_EOL; |
|
1147 | + $error .= "\t-> Section Data2 = "; |
|
1148 | + $error .= substr_replace( |
|
1149 | + substr($data2, $start, $length), |
|
1150 | + "<b style=\"color:red\">{$data2[ $i ]}</b>", |
|
1151 | + $rpoint, |
|
1152 | + $rlength |
|
1153 | + ); |
|
1154 | + $error .= PHP_EOL; |
|
1155 | + } |
|
1156 | + } |
|
1157 | + $error .= '</pre>'; |
|
1158 | + return $error; |
|
1159 | + } |
|
1160 | + |
|
1161 | + |
|
1162 | + /** |
|
1163 | + * Saves an array of settings used for configuring aspects of session behaviour |
|
1164 | + * |
|
1165 | + * @param array $updated_settings |
|
1166 | + */ |
|
1167 | + private function updateSessionSettings(array $updated_settings = array()) |
|
1168 | + { |
|
1169 | + // add existing settings, but only if not included in incoming $updated_settings array |
|
1170 | + $updated_settings += get_option(EE_Session::OPTION_NAME_SETTINGS, array()); |
|
1171 | + update_option(EE_Session::OPTION_NAME_SETTINGS, $updated_settings); |
|
1172 | + } |
|
1173 | + |
|
1174 | + |
|
1175 | + /** |
|
1176 | + * garbage_collection |
|
1177 | + */ |
|
1178 | + public function garbageCollection() |
|
1179 | + { |
|
1180 | + // only perform during regular requests if last garbage collection was over an hour ago |
|
1181 | + if (! (defined('DOING_AJAX') && DOING_AJAX) && (time() - HOUR_IN_SECONDS) >= $this->_last_gc) { |
|
1182 | + $this->_last_gc = time(); |
|
1183 | + $this->updateSessionSettings(array('last_gc' => $this->_last_gc)); |
|
1184 | + /** @type WPDB $wpdb */ |
|
1185 | + global $wpdb; |
|
1186 | + // filter the query limit. Set to 0 to turn off garbage collection |
|
1187 | + $expired_session_transient_delete_query_limit = absint( |
|
1188 | + apply_filters( |
|
1189 | + 'FHEE__EE_Session__garbage_collection___expired_session_transient_delete_query_limit', |
|
1190 | + 50 |
|
1191 | + ) |
|
1192 | + ); |
|
1193 | + // non-zero LIMIT means take out the trash |
|
1194 | + if ($expired_session_transient_delete_query_limit) { |
|
1195 | + $session_key = str_replace('_', '\_', EE_Session::session_id_prefix); |
|
1196 | + $hash_check_key = str_replace('_', '\_', EE_Session::hash_check_prefix); |
|
1197 | + // since transient expiration timestamps are set in the future, we can compare against NOW |
|
1198 | + // but we only want to pick up any trash that's been around for more than a day |
|
1199 | + $expiration = time() - DAY_IN_SECONDS; |
|
1200 | + $SQL = " |
|
1201 | 1201 | SELECT option_name |
1202 | 1202 | FROM {$wpdb->options} |
1203 | 1203 | WHERE |
@@ -1206,19 +1206,19 @@ discard block |
||
1206 | 1206 | AND option_value < {$expiration} |
1207 | 1207 | LIMIT {$expired_session_transient_delete_query_limit} |
1208 | 1208 | "; |
1209 | - // produces something like: |
|
1210 | - // SELECT option_name FROM wp_options |
|
1211 | - // WHERE ( option_name LIKE '\_transient\_timeout\_ee\_ssn\_%' |
|
1212 | - // OR option_name LIKE '\_transient\_timeout\_ee\_shc\_%' ) |
|
1213 | - // AND option_value < 1508368198 LIMIT 50 |
|
1214 | - $expired_sessions = $wpdb->get_col($SQL); |
|
1215 | - // valid results? |
|
1216 | - if (! $expired_sessions instanceof WP_Error && ! empty($expired_sessions)) { |
|
1217 | - $this->cache_storage->deleteMany($expired_sessions, true); |
|
1218 | - } |
|
1219 | - } |
|
1220 | - } |
|
1221 | - } |
|
1209 | + // produces something like: |
|
1210 | + // SELECT option_name FROM wp_options |
|
1211 | + // WHERE ( option_name LIKE '\_transient\_timeout\_ee\_ssn\_%' |
|
1212 | + // OR option_name LIKE '\_transient\_timeout\_ee\_shc\_%' ) |
|
1213 | + // AND option_value < 1508368198 LIMIT 50 |
|
1214 | + $expired_sessions = $wpdb->get_col($SQL); |
|
1215 | + // valid results? |
|
1216 | + if (! $expired_sessions instanceof WP_Error && ! empty($expired_sessions)) { |
|
1217 | + $this->cache_storage->deleteMany($expired_sessions, true); |
|
1218 | + } |
|
1219 | + } |
|
1220 | + } |
|
1221 | + } |
|
1222 | 1222 | |
1223 | 1223 | |
1224 | 1224 |
@@ -172,7 +172,7 @@ discard block |
||
172 | 172 | // check if class object is instantiated |
173 | 173 | // session loading is turned ON by default, but prior to the init hook, can be turned back OFF via: |
174 | 174 | // add_filter( 'FHEE_load_EE_Session', '__return_false' ); |
175 | - if (! self::$_instance instanceof EE_Session && apply_filters('FHEE_load_EE_Session', true)) { |
|
175 | + if ( ! self::$_instance instanceof EE_Session && apply_filters('FHEE_load_EE_Session', true)) { |
|
176 | 176 | self::$_instance = new self($cache_storage, $request, $encryption); |
177 | 177 | } |
178 | 178 | return self::$_instance; |
@@ -196,12 +196,12 @@ discard block |
||
196 | 196 | EE_Encryption $encryption = null |
197 | 197 | ) { |
198 | 198 | // session loading is turned ON by default, but prior to the init hook, can be turned back OFF via: add_filter( 'FHEE_load_EE_Session', '__return_false' ); |
199 | - if (! apply_filters('FHEE_load_EE_Session', true)) { |
|
199 | + if ( ! apply_filters('FHEE_load_EE_Session', true)) { |
|
200 | 200 | return; |
201 | 201 | } |
202 | 202 | $this->request = $request; |
203 | 203 | do_action('AHEE_log', __FILE__, __FUNCTION__, ''); |
204 | - if (! defined('ESPRESSO_SESSION')) { |
|
204 | + if ( ! defined('ESPRESSO_SESSION')) { |
|
205 | 205 | define('ESPRESSO_SESSION', true); |
206 | 206 | } |
207 | 207 | // default session lifespan in seconds |
@@ -217,11 +217,11 @@ discard block |
||
217 | 217 | */ |
218 | 218 | // retrieve session options from db |
219 | 219 | $session_settings = (array) get_option(EE_Session::OPTION_NAME_SETTINGS, array()); |
220 | - if (! empty($session_settings)) { |
|
220 | + if ( ! empty($session_settings)) { |
|
221 | 221 | // cycle though existing session options |
222 | 222 | foreach ($session_settings as $var_name => $session_setting) { |
223 | 223 | // set values for class properties |
224 | - $var_name = '_' . $var_name; |
|
224 | + $var_name = '_'.$var_name; |
|
225 | 225 | $this->{$var_name} = $session_setting; |
226 | 226 | } |
227 | 227 | } |
@@ -259,7 +259,7 @@ discard block |
||
259 | 259 | public function open_session() |
260 | 260 | { |
261 | 261 | // check for existing session and retrieve it from db |
262 | - if (! $this->_espresso_session()) { |
|
262 | + if ( ! $this->_espresso_session()) { |
|
263 | 263 | // or just start a new one |
264 | 264 | $this->_create_espresso_session(); |
265 | 265 | } |
@@ -311,7 +311,7 @@ discard block |
||
311 | 311 | */ |
312 | 312 | public function extend_expiration($time = 0) |
313 | 313 | { |
314 | - $time = $time ? $time : $this->extension(); |
|
314 | + $time = $time ? $time : $this->extension(); |
|
315 | 315 | $this->_expiration += absint($time); |
316 | 316 | } |
317 | 317 | |
@@ -338,9 +338,9 @@ discard block |
||
338 | 338 | // set some defaults |
339 | 339 | foreach ($this->_default_session_vars as $key => $default_var) { |
340 | 340 | if (is_array($default_var)) { |
341 | - $this->_session_data[ $key ] = array(); |
|
341 | + $this->_session_data[$key] = array(); |
|
342 | 342 | } else { |
343 | - $this->_session_data[ $key ] = ''; |
|
343 | + $this->_session_data[$key] = ''; |
|
344 | 344 | } |
345 | 345 | } |
346 | 346 | } |
@@ -483,8 +483,8 @@ discard block |
||
483 | 483 | $this->reset_checkout(); |
484 | 484 | $this->reset_transaction(); |
485 | 485 | } |
486 | - if (! empty($key)) { |
|
487 | - return isset($this->_session_data[ $key ]) ? $this->_session_data[ $key ] : null; |
|
486 | + if ( ! empty($key)) { |
|
487 | + return isset($this->_session_data[$key]) ? $this->_session_data[$key] : null; |
|
488 | 488 | } |
489 | 489 | return $this->_session_data; |
490 | 490 | } |
@@ -508,12 +508,12 @@ discard block |
||
508 | 508 | return false; |
509 | 509 | } |
510 | 510 | foreach ($data as $key => $value) { |
511 | - if (isset($this->_default_session_vars[ $key ])) { |
|
511 | + if (isset($this->_default_session_vars[$key])) { |
|
512 | 512 | EE_Error::add_error(sprintf(__('Sorry! %s is a default session datum and can not be reset.', |
513 | 513 | 'event_espresso'), $key), __FILE__, __FUNCTION__, __LINE__); |
514 | 514 | return false; |
515 | 515 | } |
516 | - $this->_session_data[ $key ] = $value; |
|
516 | + $this->_session_data[$key] = $value; |
|
517 | 517 | } |
518 | 518 | return true; |
519 | 519 | } |
@@ -546,7 +546,7 @@ discard block |
||
546 | 546 | $this->_user_agent = $this->request->userAgent(); |
547 | 547 | // now let's retrieve what's in the db |
548 | 548 | $session_data = $this->_retrieve_session_data(); |
549 | - if (! empty($session_data)) { |
|
549 | + if ( ! empty($session_data)) { |
|
550 | 550 | // get the current time in UTC |
551 | 551 | $this->_time = $this->_time !== null ? $this->_time : time(); |
552 | 552 | // and reset the session expiration |
@@ -557,7 +557,7 @@ discard block |
||
557 | 557 | // set initial site access time and the session expiration |
558 | 558 | $this->_set_init_access_and_expiration(); |
559 | 559 | // set referer |
560 | - $this->_session_data['pages_visited'][ $this->_session_data['init_access'] ] = isset($_SERVER['HTTP_REFERER']) |
|
560 | + $this->_session_data['pages_visited'][$this->_session_data['init_access']] = isset($_SERVER['HTTP_REFERER']) |
|
561 | 561 | ? esc_attr($_SERVER['HTTP_REFERER']) |
562 | 562 | : ''; |
563 | 563 | // no previous session = go back and create one (on top of the data above) |
@@ -595,7 +595,7 @@ discard block |
||
595 | 595 | */ |
596 | 596 | protected function _retrieve_session_data() |
597 | 597 | { |
598 | - $ssn_key = EE_Session::session_id_prefix . $this->_sid; |
|
598 | + $ssn_key = EE_Session::session_id_prefix.$this->_sid; |
|
599 | 599 | try { |
600 | 600 | // we're using WP's Transient API to store session data using the PHP session ID as the option name |
601 | 601 | $session_data = $this->cache_storage->get($ssn_key, false); |
@@ -604,7 +604,7 @@ discard block |
||
604 | 604 | } |
605 | 605 | if (apply_filters('FHEE__EE_Session___perform_session_id_hash_check', WP_DEBUG)) { |
606 | 606 | $hash_check = $this->cache_storage->get( |
607 | - EE_Session::hash_check_prefix . $this->_sid, |
|
607 | + EE_Session::hash_check_prefix.$this->_sid, |
|
608 | 608 | false |
609 | 609 | ); |
610 | 610 | if ($hash_check && $hash_check !== md5($session_data)) { |
@@ -614,7 +614,7 @@ discard block |
||
614 | 614 | 'The stored data for session %1$s failed to pass a hash check and therefore appears to be invalid.', |
615 | 615 | 'event_espresso' |
616 | 616 | ), |
617 | - EE_Session::session_id_prefix . $this->_sid |
|
617 | + EE_Session::session_id_prefix.$this->_sid |
|
618 | 618 | ), |
619 | 619 | __FILE__, __FUNCTION__, __LINE__ |
620 | 620 | ); |
@@ -623,21 +623,21 @@ discard block |
||
623 | 623 | } catch (Exception $e) { |
624 | 624 | // let's just eat that error for now and attempt to correct any corrupted data |
625 | 625 | global $wpdb; |
626 | - $row = $wpdb->get_row( |
|
626 | + $row = $wpdb->get_row( |
|
627 | 627 | $wpdb->prepare( |
628 | 628 | "SELECT option_value FROM {$wpdb->options} WHERE option_name = %s LIMIT 1", |
629 | - '_transient_' . $ssn_key |
|
629 | + '_transient_'.$ssn_key |
|
630 | 630 | ) |
631 | 631 | ); |
632 | 632 | $session_data = is_object($row) ? $row->option_value : null; |
633 | 633 | if ($session_data) { |
634 | 634 | $session_data = preg_replace_callback( |
635 | 635 | '!s:(d+):"(.*?)";!', |
636 | - function ($match) |
|
636 | + function($match) |
|
637 | 637 | { |
638 | 638 | return $match[1] === strlen($match[2]) |
639 | 639 | ? $match[0] |
640 | - : 's:' . strlen($match[2]) . ':"' . $match[2] . '";'; |
|
640 | + : 's:'.strlen($match[2]).':"'.$match[2].'";'; |
|
641 | 641 | }, |
642 | 642 | $session_data |
643 | 643 | ); |
@@ -648,7 +648,7 @@ discard block |
||
648 | 648 | $session_data = $this->encryption instanceof EE_Encryption |
649 | 649 | ? $this->encryption->base64_string_decode($session_data) |
650 | 650 | : $session_data; |
651 | - if (! is_array($session_data)) { |
|
651 | + if ( ! is_array($session_data)) { |
|
652 | 652 | try { |
653 | 653 | $session_data = maybe_unserialize($session_data); |
654 | 654 | } catch (Exception $e) { |
@@ -662,21 +662,21 @@ discard block |
||
662 | 662 | . '</pre><br>' |
663 | 663 | . $this->find_serialize_error($session_data) |
664 | 664 | : ''; |
665 | - $this->cache_storage->delete(EE_Session::session_id_prefix . $this->_sid); |
|
665 | + $this->cache_storage->delete(EE_Session::session_id_prefix.$this->_sid); |
|
666 | 666 | throw new InvalidSessionDataException($msg, 0, $e); |
667 | 667 | } |
668 | 668 | } |
669 | 669 | // just a check to make sure the session array is indeed an array |
670 | - if (! is_array($session_data)) { |
|
670 | + if ( ! is_array($session_data)) { |
|
671 | 671 | // no?!?! then something is wrong |
672 | 672 | $msg = esc_html__( |
673 | 673 | 'The session data is missing, invalid, or corrupted.', |
674 | 674 | 'event_espresso' |
675 | 675 | ); |
676 | 676 | $msg .= WP_DEBUG |
677 | - ? '<br><pre>' . print_r($session_data, true) . '</pre><br>' . $this->find_serialize_error($session_data) |
|
677 | + ? '<br><pre>'.print_r($session_data, true).'</pre><br>'.$this->find_serialize_error($session_data) |
|
678 | 678 | : ''; |
679 | - $this->cache_storage->delete(EE_Session::session_id_prefix . $this->_sid); |
|
679 | + $this->cache_storage->delete(EE_Session::session_id_prefix.$this->_sid); |
|
680 | 680 | throw new InvalidSessionDataException($msg); |
681 | 681 | } |
682 | 682 | if (isset($session_data['transaction']) && absint($session_data['transaction']) !== 0) { |
@@ -704,7 +704,7 @@ discard block |
||
704 | 704 | if (isset($_REQUEST['EESID'])) { |
705 | 705 | $session_id = sanitize_text_field($_REQUEST['EESID']); |
706 | 706 | } else { |
707 | - $session_id = md5(session_id() . get_current_blog_id() . $this->_get_sid_salt()); |
|
707 | + $session_id = md5(session_id().get_current_blog_id().$this->_get_sid_salt()); |
|
708 | 708 | } |
709 | 709 | return apply_filters('FHEE__EE_Session___generate_session_id__session_id', $session_id); |
710 | 710 | } |
@@ -812,19 +812,19 @@ discard block |
||
812 | 812 | $page_visit = $this->_get_page_visit(); |
813 | 813 | if ($page_visit) { |
814 | 814 | // set pages visited where the first will be the http referrer |
815 | - $this->_session_data['pages_visited'][ $this->_time ] = $page_visit; |
|
815 | + $this->_session_data['pages_visited'][$this->_time] = $page_visit; |
|
816 | 816 | // we'll only save the last 10 page visits. |
817 | 817 | $session_data['pages_visited'] = array_slice($this->_session_data['pages_visited'], -10); |
818 | 818 | } |
819 | 819 | break; |
820 | 820 | default : |
821 | 821 | // carry any other data over |
822 | - $session_data[ $key ] = $this->_session_data[ $key ]; |
|
822 | + $session_data[$key] = $this->_session_data[$key]; |
|
823 | 823 | } |
824 | 824 | } |
825 | 825 | $this->_session_data = $session_data; |
826 | 826 | // creating a new session does not require saving to the db just yet |
827 | - if (! $new_session) { |
|
827 | + if ( ! $new_session) { |
|
828 | 828 | // ready? let's save |
829 | 829 | if ($this->_save_session_to_db()) { |
830 | 830 | return true; |
@@ -869,12 +869,12 @@ discard block |
||
869 | 869 | { |
870 | 870 | // don't save sessions for crawlers |
871 | 871 | // and unless we're deleting the session data, don't save anything if there isn't a cart |
872 | - if ($this->request->isBot() || (! $clear_session && ! $this->cart() instanceof EE_Cart)) { |
|
872 | + if ($this->request->isBot() || ( ! $clear_session && ! $this->cart() instanceof EE_Cart)) { |
|
873 | 873 | return false; |
874 | 874 | } |
875 | 875 | $transaction = $this->transaction(); |
876 | 876 | if ($transaction instanceof EE_Transaction) { |
877 | - if (! $transaction->ID()) { |
|
877 | + if ( ! $transaction->ID()) { |
|
878 | 878 | $transaction->save(); |
879 | 879 | } |
880 | 880 | $this->_session_data['transaction'] = $transaction->ID(); |
@@ -888,14 +888,14 @@ discard block |
||
888 | 888 | // maybe save hash check |
889 | 889 | if (apply_filters('FHEE__EE_Session___perform_session_id_hash_check', WP_DEBUG)) { |
890 | 890 | $this->cache_storage->add( |
891 | - EE_Session::hash_check_prefix . $this->_sid, |
|
891 | + EE_Session::hash_check_prefix.$this->_sid, |
|
892 | 892 | md5($session_data), |
893 | 893 | $this->_lifespan |
894 | 894 | ); |
895 | 895 | } |
896 | 896 | // we're using the Transient API for storing session data, |
897 | 897 | return $this->cache_storage->add( |
898 | - EE_Session::session_id_prefix . $this->_sid, |
|
898 | + EE_Session::session_id_prefix.$this->_sid, |
|
899 | 899 | $session_data, |
900 | 900 | $this->_lifespan |
901 | 901 | ); |
@@ -909,7 +909,7 @@ discard block |
||
909 | 909 | */ |
910 | 910 | public function _get_page_visit() |
911 | 911 | { |
912 | - $page_visit = home_url('/') . 'wp-admin/admin-ajax.php'; |
|
912 | + $page_visit = home_url('/').'wp-admin/admin-ajax.php'; |
|
913 | 913 | // check for request url |
914 | 914 | if (isset($_SERVER['REQUEST_URI'])) { |
915 | 915 | $http_host = ''; |
@@ -925,14 +925,14 @@ discard block |
||
925 | 925 | // check for page_id in SERVER REQUEST |
926 | 926 | if (isset($_REQUEST['page_id'])) { |
927 | 927 | // rebuild $e_reg without any of the extra parameters |
928 | - $page_id = '?page_id=' . esc_attr($_REQUEST['page_id']) . '&'; |
|
928 | + $page_id = '?page_id='.esc_attr($_REQUEST['page_id']).'&'; |
|
929 | 929 | } |
930 | 930 | // check for $e_reg in SERVER REQUEST |
931 | 931 | if (isset($_REQUEST['ee'])) { |
932 | 932 | // rebuild $e_reg without any of the extra parameters |
933 | - $e_reg = 'ee=' . esc_attr($_REQUEST['ee']); |
|
933 | + $e_reg = 'ee='.esc_attr($_REQUEST['ee']); |
|
934 | 934 | } |
935 | - $page_visit = rtrim($http_host . $request_uri . $page_id . $e_reg, '?'); |
|
935 | + $page_visit = rtrim($http_host.$request_uri.$page_id.$e_reg, '?'); |
|
936 | 936 | } |
937 | 937 | return $page_visit !== home_url('/wp-admin/admin-ajax.php') ? $page_visit : ''; |
938 | 938 | } |
@@ -967,7 +967,7 @@ discard block |
||
967 | 967 | */ |
968 | 968 | public function clear_session($class = '', $function = '') |
969 | 969 | { |
970 | - do_action('AHEE_log', __FILE__, __FUNCTION__, 'session cleared by : ' . $class . '::' . $function . '()'); |
|
970 | + do_action('AHEE_log', __FILE__, __FUNCTION__, 'session cleared by : '.$class.'::'.$function.'()'); |
|
971 | 971 | $this->reset_cart(); |
972 | 972 | $this->reset_checkout(); |
973 | 973 | $this->reset_transaction(); |
@@ -990,7 +990,7 @@ discard block |
||
990 | 990 | public function reset_data($data_to_reset = array(), $show_all_notices = false) |
991 | 991 | { |
992 | 992 | // if $data_to_reset is not in an array, then put it in one |
993 | - if (! is_array($data_to_reset)) { |
|
993 | + if ( ! is_array($data_to_reset)) { |
|
994 | 994 | $data_to_reset = array($data_to_reset); |
995 | 995 | } |
996 | 996 | // nothing ??? go home! |
@@ -1004,11 +1004,11 @@ discard block |
||
1004 | 1004 | foreach ($data_to_reset as $reset) { |
1005 | 1005 | |
1006 | 1006 | // first check to make sure it is a valid session var |
1007 | - if (isset($this->_session_data[ $reset ])) { |
|
1007 | + if (isset($this->_session_data[$reset])) { |
|
1008 | 1008 | // then check to make sure it is not a default var |
1009 | - if (! array_key_exists($reset, $this->_default_session_vars)) { |
|
1009 | + if ( ! array_key_exists($reset, $this->_default_session_vars)) { |
|
1010 | 1010 | // remove session var |
1011 | - unset($this->_session_data[ $reset ]); |
|
1011 | + unset($this->_session_data[$reset]); |
|
1012 | 1012 | if ($show_all_notices) { |
1013 | 1013 | EE_Error::add_success(sprintf(__('The session variable %s was removed.', 'event_espresso'), |
1014 | 1014 | $reset), __FILE__, __FUNCTION__, __LINE__); |
@@ -1085,7 +1085,7 @@ discard block |
||
1085 | 1085 | // or use that for the new transient cleanup query limit |
1086 | 1086 | add_filter( |
1087 | 1087 | 'FHEE__TransientCacheStorage__clearExpiredTransients__limit', |
1088 | - function () use ($expired_session_transient_delete_query_limit) |
|
1088 | + function() use ($expired_session_transient_delete_query_limit) |
|
1089 | 1089 | { |
1090 | 1090 | return $expired_session_transient_delete_query_limit; |
1091 | 1091 | } |
@@ -1105,7 +1105,7 @@ discard block |
||
1105 | 1105 | $error = '<pre>'; |
1106 | 1106 | $data2 = preg_replace_callback( |
1107 | 1107 | '!s:(\d+):"(.*?)";!', |
1108 | - function ($match) |
|
1108 | + function($match) |
|
1109 | 1109 | { |
1110 | 1110 | return ($match[1] === strlen($match[2])) |
1111 | 1111 | ? $match[0] |
@@ -1117,14 +1117,14 @@ discard block |
||
1117 | 1117 | }, |
1118 | 1118 | $data1 |
1119 | 1119 | ); |
1120 | - $max = (strlen($data1) > strlen($data2)) ? strlen($data1) : strlen($data2); |
|
1121 | - $error .= $data1 . PHP_EOL; |
|
1122 | - $error .= $data2 . PHP_EOL; |
|
1120 | + $max = (strlen($data1) > strlen($data2)) ? strlen($data1) : strlen($data2); |
|
1121 | + $error .= $data1.PHP_EOL; |
|
1122 | + $error .= $data2.PHP_EOL; |
|
1123 | 1123 | for ($i = 0; $i < $max; $i++) { |
1124 | - if (@$data1[ $i ] !== @$data2[ $i ]) { |
|
1125 | - $error .= 'Difference ' . @$data1[ $i ] . ' != ' . @$data2[ $i ] . PHP_EOL; |
|
1126 | - $error .= "\t-> ORD number " . ord(@$data1[ $i ]) . ' != ' . ord(@$data2[ $i ]) . PHP_EOL; |
|
1127 | - $error .= "\t-> Line Number = $i" . PHP_EOL; |
|
1124 | + if (@$data1[$i] !== @$data2[$i]) { |
|
1125 | + $error .= 'Difference '.@$data1[$i].' != '.@$data2[$i].PHP_EOL; |
|
1126 | + $error .= "\t-> ORD number ".ord(@$data1[$i]).' != '.ord(@$data2[$i]).PHP_EOL; |
|
1127 | + $error .= "\t-> Line Number = $i".PHP_EOL; |
|
1128 | 1128 | $start = ($i - 20); |
1129 | 1129 | $start = ($start < 0) ? 0 : $start; |
1130 | 1130 | $length = 40; |
@@ -1139,7 +1139,7 @@ discard block |
||
1139 | 1139 | $error .= "\t-> Section Data1 = "; |
1140 | 1140 | $error .= substr_replace( |
1141 | 1141 | substr($data1, $start, $length), |
1142 | - "<b style=\"color:green\">{$data1[ $i ]}</b>", |
|
1142 | + "<b style=\"color:green\">{$data1[$i]}</b>", |
|
1143 | 1143 | $rpoint, |
1144 | 1144 | $rlength |
1145 | 1145 | ); |
@@ -1147,7 +1147,7 @@ discard block |
||
1147 | 1147 | $error .= "\t-> Section Data2 = "; |
1148 | 1148 | $error .= substr_replace( |
1149 | 1149 | substr($data2, $start, $length), |
1150 | - "<b style=\"color:red\">{$data2[ $i ]}</b>", |
|
1150 | + "<b style=\"color:red\">{$data2[$i]}</b>", |
|
1151 | 1151 | $rpoint, |
1152 | 1152 | $rlength |
1153 | 1153 | ); |
@@ -1178,7 +1178,7 @@ discard block |
||
1178 | 1178 | public function garbageCollection() |
1179 | 1179 | { |
1180 | 1180 | // only perform during regular requests if last garbage collection was over an hour ago |
1181 | - if (! (defined('DOING_AJAX') && DOING_AJAX) && (time() - HOUR_IN_SECONDS) >= $this->_last_gc) { |
|
1181 | + if ( ! (defined('DOING_AJAX') && DOING_AJAX) && (time() - HOUR_IN_SECONDS) >= $this->_last_gc) { |
|
1182 | 1182 | $this->_last_gc = time(); |
1183 | 1183 | $this->updateSessionSettings(array('last_gc' => $this->_last_gc)); |
1184 | 1184 | /** @type WPDB $wpdb */ |
@@ -1213,7 +1213,7 @@ discard block |
||
1213 | 1213 | // AND option_value < 1508368198 LIMIT 50 |
1214 | 1214 | $expired_sessions = $wpdb->get_col($SQL); |
1215 | 1215 | // valid results? |
1216 | - if (! $expired_sessions instanceof WP_Error && ! empty($expired_sessions)) { |
|
1216 | + if ( ! $expired_sessions instanceof WP_Error && ! empty($expired_sessions)) { |
|
1217 | 1217 | $this->cache_storage->deleteMany($expired_sessions, true); |
1218 | 1218 | } |
1219 | 1219 | } |
@@ -1,12 +1,7 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -use EventEspresso\core\domain\DomainFactory; |
|
4 | -use EventEspresso\core\domain\values\FilePath; |
|
5 | -use EventEspresso\core\domain\values\FullyQualifiedName; |
|
6 | -use EventEspresso\core\domain\values\Version; |
|
7 | 3 | use EventEspresso\core\exceptions\InvalidDataTypeException; |
8 | 4 | use EventEspresso\core\exceptions\InvalidInterfaceException; |
9 | -use EventEspresso\core\services\loaders\LoaderFactory; |
|
10 | 5 | use EventEspresso\core\services\request\RequestDecoratorInterface; |
11 | 6 | use EventEspresso\core\services\request\RequestInterface; |
12 | 7 | use EventEspresso\core\services\request\RequestStackCoreAppInterface; |
@@ -31,123 +31,123 @@ |
||
31 | 31 | class EE_Load_Espresso_Core implements RequestDecoratorInterface, RequestStackCoreAppInterface |
32 | 32 | { |
33 | 33 | |
34 | - /** |
|
35 | - * @var RequestInterface $request |
|
36 | - */ |
|
37 | - protected $request; |
|
38 | - |
|
39 | - /** |
|
40 | - * @var ResponseInterface $response |
|
41 | - */ |
|
42 | - protected $response; |
|
43 | - |
|
44 | - /** |
|
45 | - * @var EE_Dependency_Map $dependency_map |
|
46 | - */ |
|
47 | - protected $dependency_map; |
|
48 | - |
|
49 | - /** |
|
50 | - * @var EE_Registry $registry |
|
51 | - */ |
|
52 | - protected $registry; |
|
53 | - |
|
54 | - |
|
55 | - /** |
|
56 | - * EE_Load_Espresso_Core constructor |
|
57 | - * |
|
58 | - * @param EE_Registry $registry |
|
59 | - * @param EE_Dependency_Map $dependency_map |
|
60 | - * @throws EE_Error |
|
61 | - */ |
|
34 | + /** |
|
35 | + * @var RequestInterface $request |
|
36 | + */ |
|
37 | + protected $request; |
|
38 | + |
|
39 | + /** |
|
40 | + * @var ResponseInterface $response |
|
41 | + */ |
|
42 | + protected $response; |
|
43 | + |
|
44 | + /** |
|
45 | + * @var EE_Dependency_Map $dependency_map |
|
46 | + */ |
|
47 | + protected $dependency_map; |
|
48 | + |
|
49 | + /** |
|
50 | + * @var EE_Registry $registry |
|
51 | + */ |
|
52 | + protected $registry; |
|
53 | + |
|
54 | + |
|
55 | + /** |
|
56 | + * EE_Load_Espresso_Core constructor |
|
57 | + * |
|
58 | + * @param EE_Registry $registry |
|
59 | + * @param EE_Dependency_Map $dependency_map |
|
60 | + * @throws EE_Error |
|
61 | + */ |
|
62 | 62 | public function __construct(EE_Registry $registry, EE_Dependency_Map $dependency_map) { |
63 | - EE_Error::doing_it_wrong( |
|
64 | - __METHOD__, |
|
65 | - sprintf( |
|
66 | - esc_html__( |
|
67 | - 'This class is deprecated. Please use %1$s instead. All Event Espresso request stack classes have been moved to %2$s and are now under the %3$s namespace', |
|
68 | - 'event_espresso' |
|
69 | - ), |
|
70 | - 'EventEspresso\core\services\request\RequestStackCoreApp', |
|
71 | - '\core\services\request', |
|
72 | - 'EventEspresso\core\services\request' |
|
73 | - ), |
|
74 | - '4.9.53' |
|
75 | - ); |
|
76 | - } |
|
77 | - |
|
78 | - |
|
79 | - /** |
|
80 | - * handle |
|
81 | - * sets hooks for running rest of system |
|
82 | - * provides "AHEE__EE_System__construct__complete" hook for EE Addons to use as their starting point |
|
83 | - * starting EE Addons from any other point may lead to problems |
|
84 | - * |
|
85 | - * @param RequestInterface $request |
|
86 | - * @param ResponseInterface $response |
|
87 | - * @return ResponseInterface |
|
88 | - * @throws EE_Error |
|
89 | - * @throws InvalidDataTypeException |
|
90 | - * @throws InvalidInterfaceException |
|
91 | - * @throws InvalidArgumentException |
|
92 | - * @throws DomainException |
|
93 | - */ |
|
94 | - public function handleRequest(RequestInterface $request, ResponseInterface $response) |
|
95 | - { |
|
96 | - } |
|
97 | - |
|
98 | - |
|
99 | - |
|
100 | - /** |
|
101 | - * @return RequestInterface |
|
102 | - */ |
|
103 | - public function request() |
|
104 | - { |
|
105 | - } |
|
106 | - |
|
107 | - |
|
108 | - |
|
109 | - /** |
|
110 | - * @return ResponseInterface |
|
111 | - */ |
|
112 | - public function response() |
|
113 | - { |
|
114 | - } |
|
115 | - |
|
116 | - |
|
117 | - |
|
118 | - /** |
|
119 | - * @return EE_Dependency_Map |
|
120 | - * @throws EE_Error |
|
121 | - */ |
|
122 | - public function dependency_map() |
|
123 | - { |
|
124 | - } |
|
125 | - |
|
126 | - |
|
127 | - |
|
128 | - /** |
|
129 | - * @return EE_Registry |
|
130 | - * @throws EE_Error |
|
131 | - */ |
|
132 | - public function registry() |
|
133 | - { |
|
134 | - } |
|
135 | - |
|
136 | - |
|
137 | - |
|
138 | - |
|
139 | - |
|
140 | - |
|
141 | - /** |
|
142 | - * called after the request stack has been fully processed |
|
143 | - * if any of the middleware apps has requested the plugin be deactivated, then we do that now |
|
144 | - * |
|
145 | - * @param RequestInterface $request |
|
146 | - * @param ResponseInterface $response |
|
147 | - */ |
|
148 | - public function handleResponse(RequestInterface $request, ResponseInterface $response) |
|
149 | - { |
|
150 | - } |
|
63 | + EE_Error::doing_it_wrong( |
|
64 | + __METHOD__, |
|
65 | + sprintf( |
|
66 | + esc_html__( |
|
67 | + 'This class is deprecated. Please use %1$s instead. All Event Espresso request stack classes have been moved to %2$s and are now under the %3$s namespace', |
|
68 | + 'event_espresso' |
|
69 | + ), |
|
70 | + 'EventEspresso\core\services\request\RequestStackCoreApp', |
|
71 | + '\core\services\request', |
|
72 | + 'EventEspresso\core\services\request' |
|
73 | + ), |
|
74 | + '4.9.53' |
|
75 | + ); |
|
76 | + } |
|
77 | + |
|
78 | + |
|
79 | + /** |
|
80 | + * handle |
|
81 | + * sets hooks for running rest of system |
|
82 | + * provides "AHEE__EE_System__construct__complete" hook for EE Addons to use as their starting point |
|
83 | + * starting EE Addons from any other point may lead to problems |
|
84 | + * |
|
85 | + * @param RequestInterface $request |
|
86 | + * @param ResponseInterface $response |
|
87 | + * @return ResponseInterface |
|
88 | + * @throws EE_Error |
|
89 | + * @throws InvalidDataTypeException |
|
90 | + * @throws InvalidInterfaceException |
|
91 | + * @throws InvalidArgumentException |
|
92 | + * @throws DomainException |
|
93 | + */ |
|
94 | + public function handleRequest(RequestInterface $request, ResponseInterface $response) |
|
95 | + { |
|
96 | + } |
|
97 | + |
|
98 | + |
|
99 | + |
|
100 | + /** |
|
101 | + * @return RequestInterface |
|
102 | + */ |
|
103 | + public function request() |
|
104 | + { |
|
105 | + } |
|
106 | + |
|
107 | + |
|
108 | + |
|
109 | + /** |
|
110 | + * @return ResponseInterface |
|
111 | + */ |
|
112 | + public function response() |
|
113 | + { |
|
114 | + } |
|
115 | + |
|
116 | + |
|
117 | + |
|
118 | + /** |
|
119 | + * @return EE_Dependency_Map |
|
120 | + * @throws EE_Error |
|
121 | + */ |
|
122 | + public function dependency_map() |
|
123 | + { |
|
124 | + } |
|
125 | + |
|
126 | + |
|
127 | + |
|
128 | + /** |
|
129 | + * @return EE_Registry |
|
130 | + * @throws EE_Error |
|
131 | + */ |
|
132 | + public function registry() |
|
133 | + { |
|
134 | + } |
|
135 | + |
|
136 | + |
|
137 | + |
|
138 | + |
|
139 | + |
|
140 | + |
|
141 | + /** |
|
142 | + * called after the request stack has been fully processed |
|
143 | + * if any of the middleware apps has requested the plugin be deactivated, then we do that now |
|
144 | + * |
|
145 | + * @param RequestInterface $request |
|
146 | + * @param ResponseInterface $response |
|
147 | + */ |
|
148 | + public function handleResponse(RequestInterface $request, ResponseInterface $response) |
|
149 | + { |
|
150 | + } |
|
151 | 151 | |
152 | 152 | |
153 | 153 |
@@ -23,1589 +23,1589 @@ |
||
23 | 23 | class EE_Registry implements ResettableInterface |
24 | 24 | { |
25 | 25 | |
26 | - /** |
|
27 | - * @var EE_Registry $_instance |
|
28 | - */ |
|
29 | - private static $_instance; |
|
30 | - |
|
31 | - /** |
|
32 | - * @var EE_Dependency_Map $_dependency_map |
|
33 | - */ |
|
34 | - protected $_dependency_map; |
|
35 | - |
|
36 | - /** |
|
37 | - * @var array $_class_abbreviations |
|
38 | - */ |
|
39 | - protected $_class_abbreviations = array(); |
|
40 | - |
|
41 | - /** |
|
42 | - * @var CommandBusInterface $BUS |
|
43 | - */ |
|
44 | - public $BUS; |
|
45 | - |
|
46 | - /** |
|
47 | - * @var EE_Cart $CART |
|
48 | - */ |
|
49 | - public $CART; |
|
50 | - |
|
51 | - /** |
|
52 | - * @var EE_Config $CFG |
|
53 | - */ |
|
54 | - public $CFG; |
|
55 | - |
|
56 | - /** |
|
57 | - * @var EE_Network_Config $NET_CFG |
|
58 | - */ |
|
59 | - public $NET_CFG; |
|
60 | - |
|
61 | - /** |
|
62 | - * StdClass object for storing library classes in |
|
63 | - * |
|
64 | - * @var StdClass $LIB |
|
65 | - */ |
|
66 | - public $LIB; |
|
67 | - |
|
68 | - /** |
|
69 | - * @var EE_Request_Handler $REQ |
|
70 | - */ |
|
71 | - public $REQ; |
|
72 | - |
|
73 | - /** |
|
74 | - * @var EE_Session $SSN |
|
75 | - */ |
|
76 | - public $SSN; |
|
77 | - |
|
78 | - /** |
|
79 | - * @since 4.5.0 |
|
80 | - * @var EE_Capabilities $CAP |
|
81 | - */ |
|
82 | - public $CAP; |
|
83 | - |
|
84 | - /** |
|
85 | - * @since 4.9.0 |
|
86 | - * @var EE_Message_Resource_Manager $MRM |
|
87 | - */ |
|
88 | - public $MRM; |
|
89 | - |
|
90 | - |
|
91 | - /** |
|
92 | - * @var Registry $AssetsRegistry |
|
93 | - */ |
|
94 | - public $AssetsRegistry; |
|
95 | - |
|
96 | - /** |
|
97 | - * StdClass object for holding addons which have registered themselves to work with EE core |
|
98 | - * |
|
99 | - * @var EE_Addon[] $addons |
|
100 | - */ |
|
101 | - public $addons; |
|
102 | - |
|
103 | - /** |
|
104 | - * keys are 'short names' (eg Event), values are class names (eg 'EEM_Event') |
|
105 | - * |
|
106 | - * @var EEM_Base[] $models |
|
107 | - */ |
|
108 | - public $models = array(); |
|
109 | - |
|
110 | - /** |
|
111 | - * @var EED_Module[] $modules |
|
112 | - */ |
|
113 | - public $modules; |
|
114 | - |
|
115 | - /** |
|
116 | - * @var EES_Shortcode[] $shortcodes |
|
117 | - */ |
|
118 | - public $shortcodes; |
|
119 | - |
|
120 | - /** |
|
121 | - * @var WP_Widget[] $widgets |
|
122 | - */ |
|
123 | - public $widgets; |
|
124 | - |
|
125 | - /** |
|
126 | - * this is an array of all implemented model names (i.e. not the parent abstract models, or models |
|
127 | - * which don't actually fetch items from the DB in the normal way (ie, are not children of EEM_Base)). |
|
128 | - * Keys are model "short names" (eg "Event") as used in model relations, and values are |
|
129 | - * classnames (eg "EEM_Event") |
|
130 | - * |
|
131 | - * @var array $non_abstract_db_models |
|
132 | - */ |
|
133 | - public $non_abstract_db_models = array(); |
|
134 | - |
|
135 | - |
|
136 | - /** |
|
137 | - * internationalization for JS strings |
|
138 | - * usage: EE_Registry::i18n_js_strings['string_key'] = esc_html__( 'string to translate.', 'event_espresso' ); |
|
139 | - * in js file: var translatedString = eei18n.string_key; |
|
140 | - * |
|
141 | - * @var array $i18n_js_strings |
|
142 | - */ |
|
143 | - public static $i18n_js_strings = array(); |
|
144 | - |
|
145 | - |
|
146 | - /** |
|
147 | - * $main_file - path to espresso.php |
|
148 | - * |
|
149 | - * @var array $main_file |
|
150 | - */ |
|
151 | - public $main_file; |
|
152 | - |
|
153 | - /** |
|
154 | - * array of ReflectionClass objects where the key is the class name |
|
155 | - * |
|
156 | - * @var ReflectionClass[] $_reflectors |
|
157 | - */ |
|
158 | - public $_reflectors; |
|
159 | - |
|
160 | - /** |
|
161 | - * boolean flag to indicate whether or not to load/save dependencies from/to the cache |
|
162 | - * |
|
163 | - * @var boolean $_cache_on |
|
164 | - */ |
|
165 | - protected $_cache_on = true; |
|
166 | - |
|
167 | - |
|
168 | - |
|
169 | - /** |
|
170 | - * @singleton method used to instantiate class object |
|
171 | - * @param EE_Dependency_Map $dependency_map |
|
172 | - * @return EE_Registry instance |
|
173 | - * @throws InvalidArgumentException |
|
174 | - * @throws InvalidInterfaceException |
|
175 | - * @throws InvalidDataTypeException |
|
176 | - */ |
|
177 | - public static function instance(EE_Dependency_Map $dependency_map = null) |
|
178 | - { |
|
179 | - // check if class object is instantiated |
|
180 | - if (! self::$_instance instanceof EE_Registry) { |
|
181 | - self::$_instance = new self($dependency_map); |
|
182 | - } |
|
183 | - return self::$_instance; |
|
184 | - } |
|
185 | - |
|
186 | - |
|
187 | - |
|
188 | - /** |
|
189 | - * protected constructor to prevent direct creation |
|
190 | - * |
|
191 | - * @Constructor |
|
192 | - * @param EE_Dependency_Map $dependency_map |
|
193 | - * @throws InvalidDataTypeException |
|
194 | - * @throws InvalidInterfaceException |
|
195 | - * @throws InvalidArgumentException |
|
196 | - */ |
|
197 | - protected function __construct(EE_Dependency_Map $dependency_map) |
|
198 | - { |
|
199 | - $this->_dependency_map = $dependency_map; |
|
200 | - // $registry_container = new RegistryContainer(); |
|
201 | - $this->LIB = new RegistryContainer(); |
|
202 | - $this->addons = new RegistryContainer(); |
|
203 | - $this->modules = new RegistryContainer(); |
|
204 | - $this->shortcodes = new RegistryContainer(); |
|
205 | - $this->widgets = new RegistryContainer(); |
|
206 | - add_action('EE_Load_Espresso_Core__handle_request__initialize_core_loading', array($this, 'initialize')); |
|
207 | - } |
|
208 | - |
|
209 | - |
|
210 | - |
|
211 | - /** |
|
212 | - * initialize |
|
213 | - * |
|
214 | - * @throws EE_Error |
|
215 | - * @throws ReflectionException |
|
216 | - */ |
|
217 | - public function initialize() |
|
218 | - { |
|
219 | - $this->_class_abbreviations = apply_filters( |
|
220 | - 'FHEE__EE_Registry____construct___class_abbreviations', |
|
221 | - array( |
|
222 | - 'EE_Config' => 'CFG', |
|
223 | - 'EE_Session' => 'SSN', |
|
224 | - 'EE_Capabilities' => 'CAP', |
|
225 | - 'EE_Cart' => 'CART', |
|
226 | - 'EE_Network_Config' => 'NET_CFG', |
|
227 | - 'EE_Request_Handler' => 'REQ', |
|
228 | - 'EE_Message_Resource_Manager' => 'MRM', |
|
229 | - 'EventEspresso\core\services\commands\CommandBus' => 'BUS', |
|
230 | - 'EventEspresso\core\services\assets\Registry' => 'AssetsRegistry', |
|
231 | - ) |
|
232 | - ); |
|
233 | - $this->load_core('Base', array(), true); |
|
234 | - // add our request and response objects to the cache |
|
235 | - $request_loader = $this->_dependency_map->class_loader( |
|
236 | - 'EventEspresso\core\services\request\Request' |
|
237 | - ); |
|
238 | - $this->_set_cached_class( |
|
239 | - $request_loader(), |
|
240 | - 'EventEspresso\core\services\request\Request' |
|
241 | - ); |
|
242 | - $response_loader = $this->_dependency_map->class_loader( |
|
243 | - 'EventEspresso\core\services\request\Response' |
|
244 | - ); |
|
245 | - $this->_set_cached_class( |
|
246 | - $response_loader(), |
|
247 | - 'EventEspresso\core\services\request\Response' |
|
248 | - ); |
|
249 | - add_action('AHEE__EE_System__set_hooks_for_core', array($this, 'init')); |
|
250 | - } |
|
251 | - |
|
252 | - |
|
253 | - |
|
254 | - /** |
|
255 | - * @return void |
|
256 | - */ |
|
257 | - public function init() |
|
258 | - { |
|
259 | - // Get current page protocol |
|
260 | - $protocol = isset($_SERVER['HTTPS']) ? 'https://' : 'http://'; |
|
261 | - // Output admin-ajax.php URL with same protocol as current page |
|
262 | - self::$i18n_js_strings['ajax_url'] = admin_url('admin-ajax.php', $protocol); |
|
263 | - self::$i18n_js_strings['wp_debug'] = defined('WP_DEBUG') ? WP_DEBUG : false; |
|
264 | - } |
|
265 | - |
|
266 | - |
|
267 | - |
|
268 | - /** |
|
269 | - * localize_i18n_js_strings |
|
270 | - * |
|
271 | - * @return string |
|
272 | - */ |
|
273 | - public static function localize_i18n_js_strings() |
|
274 | - { |
|
275 | - $i18n_js_strings = (array)self::$i18n_js_strings; |
|
276 | - foreach ($i18n_js_strings as $key => $value) { |
|
277 | - if (is_scalar($value)) { |
|
278 | - $i18n_js_strings[$key] = html_entity_decode((string)$value, ENT_QUOTES, 'UTF-8'); |
|
279 | - } |
|
280 | - } |
|
281 | - return '/* <![CDATA[ */ var eei18n = ' . wp_json_encode($i18n_js_strings) . '; /* ]]> */'; |
|
282 | - } |
|
283 | - |
|
284 | - |
|
285 | - |
|
286 | - /** |
|
287 | - * @param mixed string | EED_Module $module |
|
288 | - * @throws EE_Error |
|
289 | - * @throws ReflectionException |
|
290 | - */ |
|
291 | - public function add_module($module) |
|
292 | - { |
|
293 | - if ($module instanceof EED_Module) { |
|
294 | - $module_class = get_class($module); |
|
295 | - $this->modules->{$module_class} = $module; |
|
296 | - } else { |
|
297 | - if ( ! class_exists('EE_Module_Request_Router', false)) { |
|
298 | - $this->load_core('Module_Request_Router'); |
|
299 | - } |
|
300 | - EE_Module_Request_Router::module_factory($module); |
|
301 | - } |
|
302 | - } |
|
303 | - |
|
304 | - |
|
305 | - |
|
306 | - /** |
|
307 | - * @param string $module_name |
|
308 | - * @return mixed EED_Module | NULL |
|
309 | - */ |
|
310 | - public function get_module($module_name = '') |
|
311 | - { |
|
312 | - return isset($this->modules->{$module_name}) |
|
313 | - ? $this->modules->{$module_name} |
|
314 | - : null; |
|
315 | - } |
|
316 | - |
|
317 | - |
|
318 | - |
|
319 | - /** |
|
320 | - * loads core classes - must be singletons |
|
321 | - * |
|
322 | - * @param string $class_name - simple class name ie: session |
|
323 | - * @param mixed $arguments |
|
324 | - * @param bool $load_only |
|
325 | - * @return mixed |
|
326 | - * @throws EE_Error |
|
327 | - * @throws ReflectionException |
|
328 | - */ |
|
329 | - public function load_core($class_name, $arguments = array(), $load_only = false) |
|
330 | - { |
|
331 | - $core_paths = apply_filters( |
|
332 | - 'FHEE__EE_Registry__load_core__core_paths', |
|
333 | - array( |
|
334 | - EE_CORE, |
|
335 | - EE_ADMIN, |
|
336 | - EE_CPTS, |
|
337 | - EE_CORE . 'data_migration_scripts' . DS, |
|
338 | - EE_CORE . 'capabilities' . DS, |
|
339 | - EE_CORE . 'request_stack' . DS, |
|
340 | - EE_CORE . 'middleware' . DS, |
|
341 | - ) |
|
342 | - ); |
|
343 | - // retrieve instantiated class |
|
344 | - return $this->_load( |
|
345 | - $core_paths, |
|
346 | - 'EE_', |
|
347 | - $class_name, |
|
348 | - 'core', |
|
349 | - $arguments, |
|
350 | - false, |
|
351 | - true, |
|
352 | - $load_only |
|
353 | - ); |
|
354 | - } |
|
355 | - |
|
356 | - |
|
357 | - |
|
358 | - /** |
|
359 | - * loads service classes |
|
360 | - * |
|
361 | - * @param string $class_name - simple class name ie: session |
|
362 | - * @param mixed $arguments |
|
363 | - * @param bool $load_only |
|
364 | - * @return mixed |
|
365 | - * @throws EE_Error |
|
366 | - * @throws ReflectionException |
|
367 | - */ |
|
368 | - public function load_service($class_name, $arguments = array(), $load_only = false) |
|
369 | - { |
|
370 | - $service_paths = apply_filters( |
|
371 | - 'FHEE__EE_Registry__load_service__service_paths', |
|
372 | - array( |
|
373 | - EE_CORE . 'services' . DS, |
|
374 | - ) |
|
375 | - ); |
|
376 | - // retrieve instantiated class |
|
377 | - return $this->_load( |
|
378 | - $service_paths, |
|
379 | - 'EE_', |
|
380 | - $class_name, |
|
381 | - 'class', |
|
382 | - $arguments, |
|
383 | - false, |
|
384 | - true, |
|
385 | - $load_only |
|
386 | - ); |
|
387 | - } |
|
388 | - |
|
389 | - |
|
390 | - |
|
391 | - /** |
|
392 | - * loads data_migration_scripts |
|
393 | - * |
|
394 | - * @param string $class_name - class name for the DMS ie: EE_DMS_Core_4_2_0 |
|
395 | - * @param mixed $arguments |
|
396 | - * @return EE_Data_Migration_Script_Base|mixed |
|
397 | - * @throws EE_Error |
|
398 | - * @throws ReflectionException |
|
399 | - */ |
|
400 | - public function load_dms($class_name, $arguments = array()) |
|
401 | - { |
|
402 | - // retrieve instantiated class |
|
403 | - return $this->_load( |
|
404 | - EE_Data_Migration_Manager::instance()->get_data_migration_script_folders(), |
|
405 | - 'EE_DMS_', |
|
406 | - $class_name, |
|
407 | - 'dms', |
|
408 | - $arguments, |
|
409 | - false, |
|
410 | - false |
|
411 | - ); |
|
412 | - } |
|
413 | - |
|
414 | - |
|
415 | - |
|
416 | - /** |
|
417 | - * loads object creating classes - must be singletons |
|
418 | - * |
|
419 | - * @param string $class_name - simple class name ie: attendee |
|
420 | - * @param mixed $arguments - an array of arguments to pass to the class |
|
421 | - * @param bool $from_db - some classes are instantiated from the db and thus call a different method to |
|
422 | - * instantiate |
|
423 | - * @param bool $cache if you don't want the class to be stored in the internal cache (non-persistent) then |
|
424 | - * set this to FALSE (ie. when instantiating model objects from client in a loop) |
|
425 | - * @param bool $load_only whether or not to just load the file and NOT instantiate, or load AND instantiate |
|
426 | - * (default) |
|
427 | - * @return EE_Base_Class | bool |
|
428 | - * @throws EE_Error |
|
429 | - * @throws ReflectionException |
|
430 | - */ |
|
431 | - public function load_class($class_name, $arguments = array(), $from_db = false, $cache = true, $load_only = false) |
|
432 | - { |
|
433 | - $paths = apply_filters( |
|
434 | - 'FHEE__EE_Registry__load_class__paths', array( |
|
435 | - EE_CORE, |
|
436 | - EE_CLASSES, |
|
437 | - EE_BUSINESS, |
|
438 | - ) |
|
439 | - ); |
|
440 | - // retrieve instantiated class |
|
441 | - return $this->_load( |
|
442 | - $paths, |
|
443 | - 'EE_', |
|
444 | - $class_name, |
|
445 | - 'class', |
|
446 | - $arguments, |
|
447 | - $from_db, |
|
448 | - $cache, |
|
449 | - $load_only |
|
450 | - ); |
|
451 | - } |
|
452 | - |
|
453 | - |
|
454 | - |
|
455 | - /** |
|
456 | - * loads helper classes - must be singletons |
|
457 | - * |
|
458 | - * @param string $class_name - simple class name ie: price |
|
459 | - * @param mixed $arguments |
|
460 | - * @param bool $load_only |
|
461 | - * @return EEH_Base | bool |
|
462 | - * @throws EE_Error |
|
463 | - * @throws ReflectionException |
|
464 | - */ |
|
465 | - public function load_helper($class_name, $arguments = array(), $load_only = true) |
|
466 | - { |
|
467 | - // todo: add doing_it_wrong() in a few versions after all addons have had calls to this method removed |
|
468 | - $helper_paths = apply_filters('FHEE__EE_Registry__load_helper__helper_paths', array(EE_HELPERS)); |
|
469 | - // retrieve instantiated class |
|
470 | - return $this->_load( |
|
471 | - $helper_paths, |
|
472 | - 'EEH_', |
|
473 | - $class_name, |
|
474 | - 'helper', |
|
475 | - $arguments, |
|
476 | - false, |
|
477 | - true, |
|
478 | - $load_only |
|
479 | - ); |
|
480 | - } |
|
481 | - |
|
482 | - |
|
483 | - |
|
484 | - /** |
|
485 | - * loads core classes - must be singletons |
|
486 | - * |
|
487 | - * @param string $class_name - simple class name ie: session |
|
488 | - * @param mixed $arguments |
|
489 | - * @param bool $load_only |
|
490 | - * @param bool $cache whether to cache the object or not. |
|
491 | - * @return mixed |
|
492 | - * @throws EE_Error |
|
493 | - * @throws ReflectionException |
|
494 | - */ |
|
495 | - public function load_lib($class_name, $arguments = array(), $load_only = false, $cache = true) |
|
496 | - { |
|
497 | - $paths = array( |
|
498 | - EE_LIBRARIES, |
|
499 | - EE_LIBRARIES . 'messages' . DS, |
|
500 | - EE_LIBRARIES . 'shortcodes' . DS, |
|
501 | - EE_LIBRARIES . 'qtips' . DS, |
|
502 | - EE_LIBRARIES . 'payment_methods' . DS, |
|
503 | - ); |
|
504 | - // retrieve instantiated class |
|
505 | - return $this->_load( |
|
506 | - $paths, |
|
507 | - 'EE_', |
|
508 | - $class_name, |
|
509 | - 'lib', |
|
510 | - $arguments, |
|
511 | - false, |
|
512 | - $cache, |
|
513 | - $load_only |
|
514 | - ); |
|
515 | - } |
|
516 | - |
|
517 | - |
|
518 | - |
|
519 | - /** |
|
520 | - * loads model classes - must be singletons |
|
521 | - * |
|
522 | - * @param string $class_name - simple class name ie: price |
|
523 | - * @param mixed $arguments |
|
524 | - * @param bool $load_only |
|
525 | - * @return EEM_Base | bool |
|
526 | - * @throws EE_Error |
|
527 | - * @throws ReflectionException |
|
528 | - */ |
|
529 | - public function load_model($class_name, $arguments = array(), $load_only = false) |
|
530 | - { |
|
531 | - $paths = apply_filters( |
|
532 | - 'FHEE__EE_Registry__load_model__paths', array( |
|
533 | - EE_MODELS, |
|
534 | - EE_CORE, |
|
535 | - ) |
|
536 | - ); |
|
537 | - // retrieve instantiated class |
|
538 | - return $this->_load( |
|
539 | - $paths, |
|
540 | - 'EEM_', |
|
541 | - $class_name, |
|
542 | - 'model', |
|
543 | - $arguments, |
|
544 | - false, |
|
545 | - true, |
|
546 | - $load_only |
|
547 | - ); |
|
548 | - } |
|
549 | - |
|
550 | - |
|
551 | - |
|
552 | - /** |
|
553 | - * loads model classes - must be singletons |
|
554 | - * |
|
555 | - * @param string $class_name - simple class name ie: price |
|
556 | - * @param mixed $arguments |
|
557 | - * @param bool $load_only |
|
558 | - * @return mixed | bool |
|
559 | - * @throws EE_Error |
|
560 | - * @throws ReflectionException |
|
561 | - */ |
|
562 | - public function load_model_class($class_name, $arguments = array(), $load_only = true) |
|
563 | - { |
|
564 | - $paths = array( |
|
565 | - EE_MODELS . 'fields' . DS, |
|
566 | - EE_MODELS . 'helpers' . DS, |
|
567 | - EE_MODELS . 'relations' . DS, |
|
568 | - EE_MODELS . 'strategies' . DS, |
|
569 | - ); |
|
570 | - // retrieve instantiated class |
|
571 | - return $this->_load( |
|
572 | - $paths, |
|
573 | - 'EE_', |
|
574 | - $class_name, |
|
575 | - '', |
|
576 | - $arguments, |
|
577 | - false, |
|
578 | - true, |
|
579 | - $load_only |
|
580 | - ); |
|
581 | - } |
|
582 | - |
|
583 | - |
|
584 | - |
|
585 | - /** |
|
586 | - * Determines if $model_name is the name of an actual EE model. |
|
587 | - * |
|
588 | - * @param string $model_name like Event, Attendee, Question_Group_Question, etc. |
|
589 | - * @return boolean |
|
590 | - */ |
|
591 | - public function is_model_name($model_name) |
|
592 | - { |
|
593 | - return isset($this->models[$model_name]); |
|
594 | - } |
|
595 | - |
|
596 | - |
|
597 | - |
|
598 | - /** |
|
599 | - * generic class loader |
|
600 | - * |
|
601 | - * @param string $path_to_file - directory path to file location, not including filename |
|
602 | - * @param string $file_name - file name ie: my_file.php, including extension |
|
603 | - * @param string $type - file type - core? class? helper? model? |
|
604 | - * @param mixed $arguments |
|
605 | - * @param bool $load_only |
|
606 | - * @return mixed |
|
607 | - * @throws EE_Error |
|
608 | - * @throws ReflectionException |
|
609 | - */ |
|
610 | - public function load_file($path_to_file, $file_name, $type = '', $arguments = array(), $load_only = true) |
|
611 | - { |
|
612 | - // retrieve instantiated class |
|
613 | - return $this->_load( |
|
614 | - $path_to_file, |
|
615 | - '', |
|
616 | - $file_name, |
|
617 | - $type, |
|
618 | - $arguments, |
|
619 | - false, |
|
620 | - true, |
|
621 | - $load_only |
|
622 | - ); |
|
623 | - } |
|
624 | - |
|
625 | - |
|
626 | - |
|
627 | - /** |
|
628 | - * @param string $path_to_file - directory path to file location, not including filename |
|
629 | - * @param string $class_name - full class name ie: My_Class |
|
630 | - * @param string $type - file type - core? class? helper? model? |
|
631 | - * @param mixed $arguments |
|
632 | - * @param bool $load_only |
|
633 | - * @return bool|EE_Addon|object |
|
634 | - * @throws EE_Error |
|
635 | - * @throws ReflectionException |
|
636 | - */ |
|
637 | - public function load_addon($path_to_file, $class_name, $type = 'class', $arguments = array(), $load_only = false) |
|
638 | - { |
|
639 | - // retrieve instantiated class |
|
640 | - return $this->_load( |
|
641 | - $path_to_file, |
|
642 | - 'addon', |
|
643 | - $class_name, |
|
644 | - $type, |
|
645 | - $arguments, |
|
646 | - false, |
|
647 | - true, |
|
648 | - $load_only |
|
649 | - ); |
|
650 | - } |
|
651 | - |
|
652 | - |
|
653 | - |
|
654 | - /** |
|
655 | - * instantiates, caches, and automatically resolves dependencies |
|
656 | - * for classes that use a Fully Qualified Class Name. |
|
657 | - * if the class is not capable of being loaded using PSR-4 autoloading, |
|
658 | - * then you need to use one of the existing load_*() methods |
|
659 | - * which can resolve the classname and filepath from the passed arguments |
|
660 | - * |
|
661 | - * @param bool|string $class_name Fully Qualified Class Name |
|
662 | - * @param array $arguments an argument, or array of arguments to pass to the class upon instantiation |
|
663 | - * @param bool $cache whether to cache the instantiated object for reuse |
|
664 | - * @param bool $from_db some classes are instantiated from the db |
|
665 | - * and thus call a different method to instantiate |
|
666 | - * @param bool $load_only if true, will only load the file, but will NOT instantiate an object |
|
667 | - * @param bool|string $addon if true, will cache the object in the EE_Registry->$addons array |
|
668 | - * @return bool|null|mixed null = failure to load or instantiate class object. |
|
669 | - * object = class loaded and instantiated successfully. |
|
670 | - * bool = fail or success when $load_only is true |
|
671 | - * @throws EE_Error |
|
672 | - * @throws ReflectionException |
|
673 | - */ |
|
674 | - public function create( |
|
675 | - $class_name = false, |
|
676 | - $arguments = array(), |
|
677 | - $cache = false, |
|
678 | - $from_db = false, |
|
679 | - $load_only = false, |
|
680 | - $addon = false |
|
681 | - ) { |
|
682 | - $class_name = ltrim($class_name, '\\'); |
|
683 | - $class_name = $this->_dependency_map->get_alias($class_name); |
|
684 | - $class_exists = $this->loadOrVerifyClassExists($class_name, $arguments); |
|
685 | - // if a non-FQCN was passed, then verifyClassExists() might return an object |
|
686 | - // or it could return null if the class just could not be found anywhere |
|
687 | - if ($class_exists instanceof $class_name || $class_exists === null){ |
|
688 | - // either way, return the results |
|
689 | - return $class_exists; |
|
690 | - } |
|
691 | - $class_name = $class_exists; |
|
692 | - // if we're only loading the class and it already exists, then let's just return true immediately |
|
693 | - if ($load_only) { |
|
694 | - return true; |
|
695 | - } |
|
696 | - $addon = $addon |
|
697 | - ? 'addon' |
|
698 | - : ''; |
|
699 | - // $this->_cache_on is toggled during the recursive loading that can occur with dependency injection |
|
700 | - // $cache is controlled by individual calls to separate Registry loader methods like load_class() |
|
701 | - // $load_only is also controlled by individual calls to separate Registry loader methods like load_file() |
|
702 | - if ($this->_cache_on && $cache && ! $load_only) { |
|
703 | - // return object if it's already cached |
|
704 | - $cached_class = $this->_get_cached_class($class_name, $addon); |
|
705 | - if ($cached_class !== null) { |
|
706 | - return $cached_class; |
|
707 | - } |
|
708 | - } |
|
709 | - // obtain the loader method from the dependency map |
|
710 | - $loader = $this->_dependency_map->class_loader($class_name); |
|
711 | - // instantiate the requested object |
|
712 | - if ($loader instanceof Closure) { |
|
713 | - $class_obj = $loader($arguments); |
|
714 | - } else if ($loader && method_exists($this, $loader)) { |
|
715 | - $class_obj = $this->{$loader}($class_name, $arguments); |
|
716 | - } else { |
|
717 | - $class_obj = $this->_create_object($class_name, $arguments, $addon, $from_db); |
|
718 | - } |
|
719 | - if (($this->_cache_on && $cache) || $this->get_class_abbreviation($class_name, '')) { |
|
720 | - // save it for later... kinda like gum { : $ |
|
721 | - $this->_set_cached_class($class_obj, $class_name, $addon, $from_db); |
|
722 | - } |
|
723 | - $this->_cache_on = true; |
|
724 | - return $class_obj; |
|
725 | - } |
|
726 | - |
|
727 | - |
|
728 | - |
|
729 | - /** |
|
730 | - * Recursively checks that a class exists and potentially attempts to load classes with non-FQCNs |
|
731 | - * |
|
732 | - * @param string $class_name |
|
733 | - * @param array $arguments |
|
734 | - * @param int $attempt |
|
735 | - * @return mixed |
|
736 | - */ |
|
737 | - private function loadOrVerifyClassExists($class_name, array $arguments, $attempt = 1) { |
|
738 | - if (is_object($class_name) || class_exists($class_name)) { |
|
739 | - return $class_name; |
|
740 | - } |
|
741 | - switch ($attempt) { |
|
742 | - case 1: |
|
743 | - // if it's a FQCN then maybe the class is registered with a preceding \ |
|
744 | - $class_name = strpos($class_name, '\\') !== false |
|
745 | - ? '\\' . ltrim($class_name, '\\') |
|
746 | - : $class_name; |
|
747 | - break; |
|
748 | - case 2: |
|
749 | - // |
|
750 | - $loader = $this->_dependency_map->class_loader($class_name); |
|
751 | - if ($loader && method_exists($this, $loader)) { |
|
752 | - return $this->{$loader}($class_name, $arguments); |
|
753 | - } |
|
754 | - break; |
|
755 | - case 3: |
|
756 | - default; |
|
757 | - return null; |
|
758 | - } |
|
759 | - $attempt++; |
|
760 | - return $this->loadOrVerifyClassExists($class_name, $arguments, $attempt); |
|
761 | - } |
|
762 | - |
|
763 | - |
|
764 | - |
|
765 | - /** |
|
766 | - * instantiates, caches, and injects dependencies for classes |
|
767 | - * |
|
768 | - * @param array $file_paths an array of paths to folders to look in |
|
769 | - * @param string $class_prefix EE or EEM or... ??? |
|
770 | - * @param bool|string $class_name $class name |
|
771 | - * @param string $type file type - core? class? helper? model? |
|
772 | - * @param mixed $arguments an argument or array of arguments to pass to the class upon instantiation |
|
773 | - * @param bool $from_db some classes are instantiated from the db |
|
774 | - * and thus call a different method to instantiate |
|
775 | - * @param bool $cache whether to cache the instantiated object for reuse |
|
776 | - * @param bool $load_only if true, will only load the file, but will NOT instantiate an object |
|
777 | - * @return bool|null|object null = failure to load or instantiate class object. |
|
778 | - * object = class loaded and instantiated successfully. |
|
779 | - * bool = fail or success when $load_only is true |
|
780 | - * @throws EE_Error |
|
781 | - * @throws ReflectionException |
|
782 | - */ |
|
783 | - protected function _load( |
|
784 | - $file_paths = array(), |
|
785 | - $class_prefix = 'EE_', |
|
786 | - $class_name = false, |
|
787 | - $type = 'class', |
|
788 | - $arguments = array(), |
|
789 | - $from_db = false, |
|
790 | - $cache = true, |
|
791 | - $load_only = false |
|
792 | - ) { |
|
793 | - $class_name = ltrim($class_name, '\\'); |
|
794 | - // strip php file extension |
|
795 | - $class_name = str_replace('.php', '', trim($class_name)); |
|
796 | - // does the class have a prefix ? |
|
797 | - if (! empty($class_prefix) && $class_prefix !== 'addon') { |
|
798 | - // make sure $class_prefix is uppercase |
|
799 | - $class_prefix = strtoupper(trim($class_prefix)); |
|
800 | - // add class prefix ONCE!!! |
|
801 | - $class_name = $class_prefix . str_replace($class_prefix, '', $class_name); |
|
802 | - } |
|
803 | - $class_name = $this->_dependency_map->get_alias($class_name); |
|
804 | - $class_exists = class_exists($class_name, false); |
|
805 | - // if we're only loading the class and it already exists, then let's just return true immediately |
|
806 | - if ($load_only && $class_exists) { |
|
807 | - return true; |
|
808 | - } |
|
809 | - // $this->_cache_on is toggled during the recursive loading that can occur with dependency injection |
|
810 | - // $cache is controlled by individual calls to separate Registry loader methods like load_class() |
|
811 | - // $load_only is also controlled by individual calls to separate Registry loader methods like load_file() |
|
812 | - if ($this->_cache_on && $cache && ! $load_only) { |
|
813 | - // return object if it's already cached |
|
814 | - $cached_class = $this->_get_cached_class($class_name, $class_prefix); |
|
815 | - if ($cached_class !== null) { |
|
816 | - return $cached_class; |
|
817 | - } |
|
818 | - } |
|
819 | - // if the class doesn't already exist.. then we need to try and find the file and load it |
|
820 | - if (! $class_exists) { |
|
821 | - // get full path to file |
|
822 | - $path = $this->_resolve_path($class_name, $type, $file_paths); |
|
823 | - // load the file |
|
824 | - $loaded = $this->_require_file($path, $class_name, $type, $file_paths); |
|
825 | - // if loading failed, or we are only loading a file but NOT instantiating an object |
|
826 | - if (! $loaded || $load_only) { |
|
827 | - // return boolean if only loading, or null if an object was expected |
|
828 | - return $load_only |
|
829 | - ? $loaded |
|
830 | - : null; |
|
831 | - } |
|
832 | - } |
|
833 | - // instantiate the requested object |
|
834 | - $class_obj = $this->_create_object($class_name, $arguments, $type, $from_db); |
|
835 | - if ($this->_cache_on && $cache) { |
|
836 | - // save it for later... kinda like gum { : $ |
|
837 | - $this->_set_cached_class($class_obj, $class_name, $class_prefix, $from_db); |
|
838 | - } |
|
839 | - $this->_cache_on = true; |
|
840 | - return $class_obj; |
|
841 | - } |
|
842 | - |
|
843 | - |
|
844 | - |
|
845 | - /** |
|
846 | - * @param string $class_name |
|
847 | - * @param string $default have to specify something, but not anything that will conflict |
|
848 | - * @return mixed|string |
|
849 | - */ |
|
850 | - protected function get_class_abbreviation($class_name, $default = 'FANCY_BATMAN_PANTS') |
|
851 | - { |
|
852 | - return isset($this->_class_abbreviations[$class_name]) |
|
853 | - ? $this->_class_abbreviations[$class_name] |
|
854 | - : $default; |
|
855 | - } |
|
856 | - |
|
857 | - /** |
|
858 | - * attempts to find a cached version of the requested class |
|
859 | - * by looking in the following places: |
|
860 | - * $this->{$class_abbreviation} ie: $this->CART |
|
861 | - * $this->{$class_name} ie: $this->Some_Class |
|
862 | - * $this->LIB->{$class_name} ie: $this->LIB->Some_Class |
|
863 | - * $this->addon->{$class_name} ie: $this->addon->Some_Addon_Class |
|
864 | - * |
|
865 | - * @param string $class_name |
|
866 | - * @param string $class_prefix |
|
867 | - * @return mixed |
|
868 | - */ |
|
869 | - protected function _get_cached_class($class_name, $class_prefix = '') |
|
870 | - { |
|
871 | - if ($class_name === 'EE_Registry') { |
|
872 | - return $this; |
|
873 | - } |
|
874 | - $class_abbreviation = $this->get_class_abbreviation($class_name); |
|
875 | - $class_name = str_replace('\\', '_', $class_name); |
|
876 | - // check if class has already been loaded, and return it if it has been |
|
877 | - if (isset($this->{$class_abbreviation})) { |
|
878 | - return $this->{$class_abbreviation}; |
|
879 | - } |
|
880 | - if (isset ($this->{$class_name})) { |
|
881 | - return $this->{$class_name}; |
|
882 | - } |
|
883 | - if (isset ($this->LIB->{$class_name})) { |
|
884 | - return $this->LIB->{$class_name}; |
|
885 | - } |
|
886 | - if ($class_prefix === 'addon' && isset ($this->addons->{$class_name})) { |
|
887 | - return $this->addons->{$class_name}; |
|
888 | - } |
|
889 | - return null; |
|
890 | - } |
|
891 | - |
|
892 | - |
|
893 | - |
|
894 | - /** |
|
895 | - * removes a cached version of the requested class |
|
896 | - * |
|
897 | - * @param string $class_name |
|
898 | - * @param boolean $addon |
|
899 | - * @return boolean |
|
900 | - */ |
|
901 | - public function clear_cached_class($class_name, $addon = false) |
|
902 | - { |
|
903 | - $class_abbreviation = $this->get_class_abbreviation($class_name); |
|
904 | - $class_name = str_replace('\\', '_', $class_name); |
|
905 | - // check if class has already been loaded, and return it if it has been |
|
906 | - if (isset($this->{$class_abbreviation})) { |
|
907 | - $this->{$class_abbreviation} = null; |
|
908 | - return true; |
|
909 | - } |
|
910 | - if (isset($this->{$class_name})) { |
|
911 | - $this->{$class_name} = null; |
|
912 | - return true; |
|
913 | - } |
|
914 | - if (isset($this->LIB->{$class_name})) { |
|
915 | - unset($this->LIB->{$class_name}); |
|
916 | - return true; |
|
917 | - } |
|
918 | - if ($addon && isset($this->addons->{$class_name})) { |
|
919 | - unset($this->addons->{$class_name}); |
|
920 | - return true; |
|
921 | - } |
|
922 | - return false; |
|
923 | - } |
|
924 | - |
|
925 | - |
|
926 | - |
|
927 | - /** |
|
928 | - * attempts to find a full valid filepath for the requested class. |
|
929 | - * loops thru each of the base paths in the $file_paths array and appends : "{classname} . {file type} . php" |
|
930 | - * then returns that path if the target file has been found and is readable |
|
931 | - * |
|
932 | - * @param string $class_name |
|
933 | - * @param string $type |
|
934 | - * @param array $file_paths |
|
935 | - * @return string | bool |
|
936 | - */ |
|
937 | - protected function _resolve_path($class_name, $type = '', $file_paths = array()) |
|
938 | - { |
|
939 | - // make sure $file_paths is an array |
|
940 | - $file_paths = is_array($file_paths) |
|
941 | - ? $file_paths |
|
942 | - : array($file_paths); |
|
943 | - // cycle thru paths |
|
944 | - foreach ($file_paths as $key => $file_path) { |
|
945 | - // convert all separators to proper DS, if no filepath, then use EE_CLASSES |
|
946 | - $file_path = $file_path |
|
947 | - ? str_replace(array('/', '\\'), DS, $file_path) |
|
948 | - : EE_CLASSES; |
|
949 | - // prep file type |
|
950 | - $type = ! empty($type) |
|
951 | - ? trim($type, '.') . '.' |
|
952 | - : ''; |
|
953 | - // build full file path |
|
954 | - $file_paths[$key] = rtrim($file_path, DS) . DS . $class_name . '.' . $type . 'php'; |
|
955 | - //does the file exist and can be read ? |
|
956 | - if (is_readable($file_paths[$key])) { |
|
957 | - return $file_paths[$key]; |
|
958 | - } |
|
959 | - } |
|
960 | - return false; |
|
961 | - } |
|
962 | - |
|
963 | - |
|
964 | - |
|
965 | - /** |
|
966 | - * basically just performs a require_once() |
|
967 | - * but with some error handling |
|
968 | - * |
|
969 | - * @param string $path |
|
970 | - * @param string $class_name |
|
971 | - * @param string $type |
|
972 | - * @param array $file_paths |
|
973 | - * @return bool |
|
974 | - * @throws EE_Error |
|
975 | - * @throws ReflectionException |
|
976 | - */ |
|
977 | - protected function _require_file($path, $class_name, $type = '', $file_paths = array()) |
|
978 | - { |
|
979 | - $this->resolve_legacy_class_parent($class_name); |
|
980 | - // don't give up! you gotta... |
|
981 | - try { |
|
982 | - //does the file exist and can it be read ? |
|
983 | - if (! $path) { |
|
984 | - // just in case the file has already been autoloaded, |
|
985 | - // but discrepancies in the naming schema are preventing it from |
|
986 | - // being loaded via one of the EE_Registry::load_*() methods, |
|
987 | - // then let's try one last hail mary before throwing an exception |
|
988 | - // and call class_exists() again, but with autoloading turned ON |
|
989 | - if(class_exists($class_name)) { |
|
990 | - return true; |
|
991 | - } |
|
992 | - // so sorry, can't find the file |
|
993 | - throw new EE_Error ( |
|
994 | - sprintf( |
|
995 | - esc_html__( |
|
996 | - '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', |
|
997 | - 'event_espresso' |
|
998 | - ), |
|
999 | - trim($type, '.'), |
|
1000 | - $class_name, |
|
1001 | - '<br />' . implode(',<br />', $file_paths) |
|
1002 | - ) |
|
1003 | - ); |
|
1004 | - } |
|
1005 | - // get the file |
|
1006 | - require_once($path); |
|
1007 | - // if the class isn't already declared somewhere |
|
1008 | - if (class_exists($class_name, false) === false) { |
|
1009 | - // so sorry, not a class |
|
1010 | - throw new EE_Error( |
|
1011 | - sprintf( |
|
1012 | - esc_html__('The %s file %s does not appear to contain the %s Class.', 'event_espresso'), |
|
1013 | - $type, |
|
1014 | - $path, |
|
1015 | - $class_name |
|
1016 | - ) |
|
1017 | - ); |
|
1018 | - } |
|
1019 | - } catch (EE_Error $e) { |
|
1020 | - $e->get_error(); |
|
1021 | - return false; |
|
1022 | - } |
|
1023 | - return true; |
|
1024 | - } |
|
1025 | - |
|
1026 | - |
|
1027 | - |
|
1028 | - /** |
|
1029 | - * Some of our legacy classes that extended a parent class would simply use a require() statement |
|
1030 | - * before their class declaration in order to ensure that the parent class was loaded. |
|
1031 | - * This is not ideal, but it's nearly impossible to determine the parent class of a non-namespaced class, |
|
1032 | - * without triggering a fatal error because the parent class has yet to be loaded and therefore doesn't exist. |
|
1033 | - * |
|
1034 | - * @param string $class_name |
|
1035 | - */ |
|
1036 | - protected function resolve_legacy_class_parent($class_name = '') |
|
1037 | - { |
|
1038 | - try { |
|
1039 | - $legacy_parent_class_map = array( |
|
1040 | - 'EE_Payment_Processor' => 'core/business/EE_Processor_Base.class.php' |
|
1041 | - ); |
|
1042 | - if(isset($legacy_parent_class_map[$class_name])) { |
|
1043 | - require_once EE_PLUGIN_DIR_PATH . $legacy_parent_class_map[$class_name]; |
|
1044 | - } |
|
1045 | - } catch (Exception $exception) { |
|
1046 | - } |
|
1047 | - } |
|
1048 | - |
|
1049 | - |
|
1050 | - |
|
1051 | - /** |
|
1052 | - * _create_object |
|
1053 | - * Attempts to instantiate the requested class via any of the |
|
1054 | - * commonly used instantiation methods employed throughout EE. |
|
1055 | - * The priority for instantiation is as follows: |
|
1056 | - * - abstract classes or any class flagged as "load only" (no instantiation occurs) |
|
1057 | - * - model objects via their 'new_instance_from_db' method |
|
1058 | - * - model objects via their 'new_instance' method |
|
1059 | - * - "singleton" classes" via their 'instance' method |
|
1060 | - * - standard instantiable classes via their __constructor |
|
1061 | - * Prior to instantiation, if the classname exists in the dependency_map, |
|
1062 | - * then the constructor for the requested class will be examined to determine |
|
1063 | - * if any dependencies exist, and if they can be injected. |
|
1064 | - * If so, then those classes will be added to the array of arguments passed to the constructor |
|
1065 | - * |
|
1066 | - * @param string $class_name |
|
1067 | - * @param array $arguments |
|
1068 | - * @param string $type |
|
1069 | - * @param bool $from_db |
|
1070 | - * @return null|object |
|
1071 | - * @throws EE_Error |
|
1072 | - * @throws ReflectionException |
|
1073 | - */ |
|
1074 | - protected function _create_object($class_name, $arguments = array(), $type = '', $from_db = false) |
|
1075 | - { |
|
1076 | - // create reflection |
|
1077 | - $reflector = $this->get_ReflectionClass($class_name); |
|
1078 | - // make sure arguments are an array |
|
1079 | - $arguments = is_array($arguments) |
|
1080 | - ? $arguments |
|
1081 | - : array($arguments); |
|
1082 | - // and if arguments array is numerically and sequentially indexed, then we want it to remain as is, |
|
1083 | - // else wrap it in an additional array so that it doesn't get split into multiple parameters |
|
1084 | - $arguments = $this->_array_is_numerically_and_sequentially_indexed($arguments) |
|
1085 | - ? $arguments |
|
1086 | - : array($arguments); |
|
1087 | - // attempt to inject dependencies ? |
|
1088 | - if ($this->_dependency_map->has($class_name)) { |
|
1089 | - $arguments = $this->_resolve_dependencies($reflector, $class_name, $arguments); |
|
1090 | - } |
|
1091 | - // instantiate the class if possible |
|
1092 | - if ($reflector->isAbstract()) { |
|
1093 | - // nothing to instantiate, loading file was enough |
|
1094 | - // does not throw an exception so $instantiation_mode is unused |
|
1095 | - // $instantiation_mode = "1) no constructor abstract class"; |
|
1096 | - return true; |
|
1097 | - } |
|
1098 | - if (empty($arguments) && $reflector->getConstructor() === null && $reflector->isInstantiable()) { |
|
1099 | - // no constructor = static methods only... nothing to instantiate, loading file was enough |
|
1100 | - // $instantiation_mode = "2) no constructor but instantiable"; |
|
1101 | - return $reflector->newInstance(); |
|
1102 | - } |
|
1103 | - if ($from_db && method_exists($class_name, 'new_instance_from_db')) { |
|
1104 | - // $instantiation_mode = "3) new_instance_from_db()"; |
|
1105 | - return call_user_func_array(array($class_name, 'new_instance_from_db'), $arguments); |
|
1106 | - } |
|
1107 | - if (method_exists($class_name, 'new_instance')) { |
|
1108 | - // $instantiation_mode = "4) new_instance()"; |
|
1109 | - return call_user_func_array(array($class_name, 'new_instance'), $arguments); |
|
1110 | - } |
|
1111 | - if (method_exists($class_name, 'instance')) { |
|
1112 | - // $instantiation_mode = "5) instance()"; |
|
1113 | - return call_user_func_array(array($class_name, 'instance'), $arguments); |
|
1114 | - } |
|
1115 | - if ($reflector->isInstantiable()) { |
|
1116 | - // $instantiation_mode = "6) constructor"; |
|
1117 | - return $reflector->newInstanceArgs($arguments); |
|
1118 | - } |
|
1119 | - // heh ? something's not right ! |
|
1120 | - throw new EE_Error( |
|
1121 | - sprintf( |
|
1122 | - __('The %s file %s could not be instantiated.', 'event_espresso'), |
|
1123 | - $type, |
|
1124 | - $class_name |
|
1125 | - ) |
|
1126 | - ); |
|
1127 | - } |
|
1128 | - |
|
1129 | - |
|
1130 | - |
|
1131 | - /** |
|
1132 | - * @see http://stackoverflow.com/questions/173400/how-to-check-if-php-array-is-associative-or-sequential |
|
1133 | - * @param array $array |
|
1134 | - * @return bool |
|
1135 | - */ |
|
1136 | - protected function _array_is_numerically_and_sequentially_indexed(array $array) |
|
1137 | - { |
|
1138 | - return ! empty($array) |
|
1139 | - ? array_keys($array) === range(0, count($array) - 1) |
|
1140 | - : true; |
|
1141 | - } |
|
1142 | - |
|
1143 | - |
|
1144 | - |
|
1145 | - /** |
|
1146 | - * getReflectionClass |
|
1147 | - * checks if a ReflectionClass object has already been generated for a class |
|
1148 | - * and returns that instead of creating a new one |
|
1149 | - * |
|
1150 | - * @param string $class_name |
|
1151 | - * @return ReflectionClass |
|
1152 | - * @throws ReflectionException |
|
1153 | - */ |
|
1154 | - public function get_ReflectionClass($class_name) |
|
1155 | - { |
|
1156 | - if ( |
|
1157 | - ! isset($this->_reflectors[$class_name]) |
|
1158 | - || ! $this->_reflectors[$class_name] instanceof ReflectionClass |
|
1159 | - ) { |
|
1160 | - $this->_reflectors[$class_name] = new ReflectionClass($class_name); |
|
1161 | - } |
|
1162 | - return $this->_reflectors[$class_name]; |
|
1163 | - } |
|
1164 | - |
|
1165 | - |
|
1166 | - |
|
1167 | - /** |
|
1168 | - * _resolve_dependencies |
|
1169 | - * examines the constructor for the requested class to determine |
|
1170 | - * if any dependencies exist, and if they can be injected. |
|
1171 | - * If so, then those classes will be added to the array of arguments passed to the constructor |
|
1172 | - * PLZ NOTE: this is achieved by type hinting the constructor params |
|
1173 | - * For example: |
|
1174 | - * if attempting to load a class "Foo" with the following constructor: |
|
1175 | - * __construct( Bar $bar_class, Fighter $grohl_class ) |
|
1176 | - * then $bar_class and $grohl_class will be added to the $arguments array, |
|
1177 | - * but only IF they are NOT already present in the incoming arguments array, |
|
1178 | - * and the correct classes can be loaded |
|
1179 | - * |
|
1180 | - * @param ReflectionClass $reflector |
|
1181 | - * @param string $class_name |
|
1182 | - * @param array $arguments |
|
1183 | - * @return array |
|
1184 | - * @throws EE_Error |
|
1185 | - * @throws ReflectionException |
|
1186 | - */ |
|
1187 | - protected function _resolve_dependencies(ReflectionClass $reflector, $class_name, $arguments = array()) |
|
1188 | - { |
|
1189 | - // let's examine the constructor |
|
1190 | - $constructor = $reflector->getConstructor(); |
|
1191 | - // whu? huh? nothing? |
|
1192 | - if (! $constructor) { |
|
1193 | - return $arguments; |
|
1194 | - } |
|
1195 | - // get constructor parameters |
|
1196 | - $params = $constructor->getParameters(); |
|
1197 | - // and the keys for the incoming arguments array so that we can compare existing arguments with what is expected |
|
1198 | - $argument_keys = array_keys($arguments); |
|
1199 | - // now loop thru all of the constructors expected parameters |
|
1200 | - foreach ($params as $index => $param) { |
|
1201 | - // is this a dependency for a specific class ? |
|
1202 | - $param_class = $param->getClass() |
|
1203 | - ? $param->getClass()->name |
|
1204 | - : null; |
|
1205 | - // BUT WAIT !!! This class may be an alias for something else (or getting replaced at runtime) |
|
1206 | - $param_class = $this->_dependency_map->has_alias($param_class, $class_name) |
|
1207 | - ? $this->_dependency_map->get_alias($param_class, $class_name) |
|
1208 | - : $param_class; |
|
1209 | - if ( |
|
1210 | - // param is not even a class |
|
1211 | - $param_class === null |
|
1212 | - // and something already exists in the incoming arguments for this param |
|
1213 | - && array_key_exists($index, $argument_keys) |
|
1214 | - && array_key_exists($argument_keys[$index], $arguments) |
|
1215 | - ) { |
|
1216 | - // so let's skip this argument and move on to the next |
|
1217 | - continue; |
|
1218 | - } |
|
1219 | - if ( |
|
1220 | - // parameter is type hinted as a class, exists as an incoming argument, AND it's the correct class |
|
1221 | - $param_class !== null |
|
1222 | - && isset($argument_keys[$index], $arguments[$argument_keys[$index]]) |
|
1223 | - && $arguments[$argument_keys[$index]] instanceof $param_class |
|
1224 | - ) { |
|
1225 | - // skip this argument and move on to the next |
|
1226 | - continue; |
|
1227 | - } |
|
1228 | - if ( |
|
1229 | - // parameter is type hinted as a class, and should be injected |
|
1230 | - $param_class !== null |
|
1231 | - && $this->_dependency_map->has_dependency_for_class($class_name, $param_class) |
|
1232 | - ) { |
|
1233 | - $arguments = $this->_resolve_dependency( |
|
1234 | - $class_name, |
|
1235 | - $param_class, |
|
1236 | - $arguments, |
|
1237 | - $index, |
|
1238 | - $argument_keys |
|
1239 | - ); |
|
1240 | - } else { |
|
1241 | - try { |
|
1242 | - $arguments[$index] = $param->isDefaultValueAvailable() |
|
1243 | - ? $param->getDefaultValue() |
|
1244 | - : null; |
|
1245 | - } catch (ReflectionException $e) { |
|
1246 | - throw new ReflectionException( |
|
1247 | - sprintf( |
|
1248 | - esc_html__('%1$s for parameter "$%2$s on classname "%3$s"', 'event_espresso'), |
|
1249 | - $e->getMessage(), |
|
1250 | - $param->getName(), |
|
1251 | - $class_name |
|
1252 | - ) |
|
1253 | - ); |
|
1254 | - } |
|
1255 | - } |
|
1256 | - } |
|
1257 | - return $arguments; |
|
1258 | - } |
|
1259 | - |
|
1260 | - |
|
1261 | - |
|
1262 | - /** |
|
1263 | - * @param string $class_name |
|
1264 | - * @param string $param_class |
|
1265 | - * @param array $arguments |
|
1266 | - * @param mixed $index |
|
1267 | - * @param array $argument_keys |
|
1268 | - * @return array |
|
1269 | - * @throws EE_Error |
|
1270 | - * @throws ReflectionException |
|
1271 | - * @throws InvalidArgumentException |
|
1272 | - * @throws InvalidInterfaceException |
|
1273 | - * @throws InvalidDataTypeException |
|
1274 | - */ |
|
1275 | - protected function _resolve_dependency($class_name, $param_class, $arguments, $index, array $argument_keys) |
|
1276 | - { |
|
1277 | - $dependency = null; |
|
1278 | - // should dependency be loaded from cache ? |
|
1279 | - $cache_on = $this->_dependency_map->loading_strategy_for_class_dependency( |
|
1280 | - $class_name, |
|
1281 | - $param_class |
|
1282 | - ); |
|
1283 | - $cache_on = $cache_on !== EE_Dependency_Map::load_new_object; |
|
1284 | - // we might have a dependency... |
|
1285 | - // let's MAYBE try and find it in our cache if that's what's been requested |
|
1286 | - $cached_class = $cache_on |
|
1287 | - ? $this->_get_cached_class($param_class) |
|
1288 | - : null; |
|
1289 | - // and grab it if it exists |
|
1290 | - if ($cached_class instanceof $param_class) { |
|
1291 | - $dependency = $cached_class; |
|
1292 | - } else if ($param_class !== $class_name) { |
|
1293 | - // obtain the loader method from the dependency map |
|
1294 | - $loader = $this->_dependency_map->class_loader($param_class); |
|
1295 | - // is loader a custom closure ? |
|
1296 | - if ($loader instanceof Closure) { |
|
1297 | - $dependency = $loader($arguments); |
|
1298 | - } else { |
|
1299 | - // set the cache on property for the recursive loading call |
|
1300 | - $this->_cache_on = $cache_on; |
|
1301 | - // if not, then let's try and load it via the registry |
|
1302 | - if ($loader && method_exists($this, $loader)) { |
|
1303 | - $dependency = $this->{$loader}($param_class); |
|
1304 | - } else { |
|
1305 | - $dependency = LoaderFactory::getLoader()->load( |
|
1306 | - $param_class, |
|
1307 | - array(), |
|
1308 | - $cache_on |
|
1309 | - ); |
|
1310 | - } |
|
1311 | - } |
|
1312 | - } |
|
1313 | - // did we successfully find the correct dependency ? |
|
1314 | - if ($dependency instanceof $param_class) { |
|
1315 | - // then let's inject it into the incoming array of arguments at the correct location |
|
1316 | - $arguments[$index] = $dependency; |
|
1317 | - } |
|
1318 | - return $arguments; |
|
1319 | - } |
|
1320 | - |
|
1321 | - |
|
1322 | - |
|
1323 | - /** |
|
1324 | - * _set_cached_class |
|
1325 | - * attempts to cache the instantiated class locally |
|
1326 | - * in one of the following places, in the following order: |
|
1327 | - * $this->{class_abbreviation} ie: $this->CART |
|
1328 | - * $this->{$class_name} ie: $this->Some_Class |
|
1329 | - * $this->addon->{$$class_name} ie: $this->addon->Some_Addon_Class |
|
1330 | - * $this->LIB->{$class_name} ie: $this->LIB->Some_Class |
|
1331 | - * |
|
1332 | - * @param object $class_obj |
|
1333 | - * @param string $class_name |
|
1334 | - * @param string $class_prefix |
|
1335 | - * @param bool $from_db |
|
1336 | - * @return void |
|
1337 | - */ |
|
1338 | - protected function _set_cached_class($class_obj, $class_name, $class_prefix = '', $from_db = false) |
|
1339 | - { |
|
1340 | - if ($class_name === 'EE_Registry' || empty($class_obj)) { |
|
1341 | - return; |
|
1342 | - } |
|
1343 | - // return newly instantiated class |
|
1344 | - $class_abbreviation = $this->get_class_abbreviation($class_name, ''); |
|
1345 | - if ($class_abbreviation) { |
|
1346 | - $this->{$class_abbreviation} = $class_obj; |
|
1347 | - return; |
|
1348 | - } |
|
1349 | - $class_name = str_replace('\\', '_', $class_name); |
|
1350 | - if (property_exists($this, $class_name)) { |
|
1351 | - $this->{$class_name} = $class_obj; |
|
1352 | - return; |
|
1353 | - } |
|
1354 | - if ($class_prefix === 'addon') { |
|
1355 | - $this->addons->{$class_name} = $class_obj; |
|
1356 | - return; |
|
1357 | - } |
|
1358 | - if (! $from_db) { |
|
1359 | - $this->LIB->{$class_name} = $class_obj; |
|
1360 | - } |
|
1361 | - } |
|
1362 | - |
|
1363 | - |
|
1364 | - |
|
1365 | - /** |
|
1366 | - * call any loader that's been registered in the EE_Dependency_Map::$_class_loaders array |
|
1367 | - * |
|
1368 | - * @param string $classname PLEASE NOTE: the class name needs to match what's registered |
|
1369 | - * in the EE_Dependency_Map::$_class_loaders array, |
|
1370 | - * including the class prefix, ie: "EE_", "EEM_", "EEH_", etc |
|
1371 | - * @param array $arguments |
|
1372 | - * @return object |
|
1373 | - */ |
|
1374 | - public static function factory($classname, $arguments = array()) |
|
1375 | - { |
|
1376 | - $loader = self::instance()->_dependency_map->class_loader($classname); |
|
1377 | - if ($loader instanceof Closure) { |
|
1378 | - return $loader($arguments); |
|
1379 | - } |
|
1380 | - if (method_exists(self::instance(), $loader)) { |
|
1381 | - return self::instance()->{$loader}($classname, $arguments); |
|
1382 | - } |
|
1383 | - return null; |
|
1384 | - } |
|
1385 | - |
|
1386 | - |
|
1387 | - |
|
1388 | - /** |
|
1389 | - * Gets the addon by its class name |
|
1390 | - * |
|
1391 | - * @param string $class_name |
|
1392 | - * @return EE_Addon |
|
1393 | - */ |
|
1394 | - public function getAddon($class_name) |
|
1395 | - { |
|
1396 | - $class_name = str_replace('\\', '_', $class_name); |
|
1397 | - return $this->addons->{$class_name}; |
|
1398 | - } |
|
1399 | - |
|
1400 | - |
|
1401 | - /** |
|
1402 | - * removes the addon from the internal cache |
|
1403 | - * |
|
1404 | - * @param string $class_name |
|
1405 | - * @return void |
|
1406 | - */ |
|
1407 | - public function removeAddon($class_name) |
|
1408 | - { |
|
1409 | - $class_name = str_replace('\\', '_', $class_name); |
|
1410 | - unset($this->addons->{$class_name}); |
|
1411 | - } |
|
1412 | - |
|
1413 | - |
|
1414 | - |
|
1415 | - /** |
|
1416 | - * Gets the addon by its name/slug (not classname. For that, just |
|
1417 | - * use the get_addon() method above |
|
1418 | - * |
|
1419 | - * @param string $name |
|
1420 | - * @return EE_Addon |
|
1421 | - */ |
|
1422 | - public function get_addon_by_name($name) |
|
1423 | - { |
|
1424 | - foreach ($this->addons as $addon) { |
|
1425 | - if ($addon->name() === $name) { |
|
1426 | - return $addon; |
|
1427 | - } |
|
1428 | - } |
|
1429 | - return null; |
|
1430 | - } |
|
1431 | - |
|
1432 | - |
|
1433 | - |
|
1434 | - /** |
|
1435 | - * Gets an array of all the registered addons, where the keys are their names. |
|
1436 | - * (ie, what each returns for their name() function) |
|
1437 | - * They're already available on EE_Registry::instance()->addons as properties, |
|
1438 | - * where each property's name is the addon's classname, |
|
1439 | - * So if you just want to get the addon by classname, |
|
1440 | - * OR use the get_addon() method above. |
|
1441 | - * PLEASE NOTE: |
|
1442 | - * addons with Fully Qualified Class Names |
|
1443 | - * have had the namespace separators converted to underscores, |
|
1444 | - * so a classname like Fully\Qualified\ClassName |
|
1445 | - * would have been converted to Fully_Qualified_ClassName |
|
1446 | - * |
|
1447 | - * @return EE_Addon[] where the KEYS are the addon's name() |
|
1448 | - */ |
|
1449 | - public function get_addons_by_name() |
|
1450 | - { |
|
1451 | - $addons = array(); |
|
1452 | - foreach ($this->addons as $addon) { |
|
1453 | - $addons[$addon->name()] = $addon; |
|
1454 | - } |
|
1455 | - return $addons; |
|
1456 | - } |
|
1457 | - |
|
1458 | - |
|
1459 | - /** |
|
1460 | - * Resets the specified model's instance AND makes sure EE_Registry doesn't keep |
|
1461 | - * a stale copy of it around |
|
1462 | - * |
|
1463 | - * @param string $model_name |
|
1464 | - * @return \EEM_Base |
|
1465 | - * @throws \EE_Error |
|
1466 | - */ |
|
1467 | - public function reset_model($model_name) |
|
1468 | - { |
|
1469 | - $model_class_name = strpos($model_name, 'EEM_') !== 0 |
|
1470 | - ? "EEM_{$model_name}" |
|
1471 | - : $model_name; |
|
1472 | - if (! isset($this->LIB->{$model_class_name}) || ! $this->LIB->{$model_class_name} instanceof EEM_Base) { |
|
1473 | - return null; |
|
1474 | - } |
|
1475 | - //get that model reset it and make sure we nuke the old reference to it |
|
1476 | - if ($this->LIB->{$model_class_name} instanceof $model_class_name |
|
1477 | - && is_callable( |
|
1478 | - array($model_class_name, 'reset') |
|
1479 | - )) { |
|
1480 | - $this->LIB->{$model_class_name} = $this->LIB->{$model_class_name}->reset(); |
|
1481 | - } else { |
|
1482 | - throw new EE_Error(sprintf(esc_html__('Model %s does not have a method "reset"', 'event_espresso'), $model_name)); |
|
1483 | - } |
|
1484 | - return $this->LIB->{$model_class_name}; |
|
1485 | - } |
|
1486 | - |
|
1487 | - |
|
1488 | - |
|
1489 | - /** |
|
1490 | - * Resets the registry. |
|
1491 | - * The criteria for what gets reset is based on what can be shared between sites on the same request when |
|
1492 | - * switch_to_blog is used in a multisite install. Here is a list of things that are NOT reset. |
|
1493 | - * - $_dependency_map |
|
1494 | - * - $_class_abbreviations |
|
1495 | - * - $NET_CFG (EE_Network_Config): The config is shared network wide so no need to reset. |
|
1496 | - * - $REQ: Still on the same request so no need to change. |
|
1497 | - * - $CAP: There is no site specific state in the EE_Capability class. |
|
1498 | - * - $SSN: Although ideally, the session should not be shared between site switches, we can't reset it because only |
|
1499 | - * one Session can be active in a single request. Resetting could resolve in "headers already sent" errors. |
|
1500 | - * - $addons: In multisite, the state of the addons is something controlled via hooks etc in a normal request. So |
|
1501 | - * for now, we won't reset the addons because it could break calls to an add-ons class/methods in the |
|
1502 | - * switch or on the restore. |
|
1503 | - * - $modules |
|
1504 | - * - $shortcodes |
|
1505 | - * - $widgets |
|
1506 | - * |
|
1507 | - * @param boolean $hard [deprecated] |
|
1508 | - * @param boolean $reinstantiate whether to create new instances of EE_Registry's singletons too, |
|
1509 | - * or just reset without re-instantiating (handy to set to FALSE if you're not |
|
1510 | - * sure if you CAN currently reinstantiate the singletons at the moment) |
|
1511 | - * @param bool $reset_models Defaults to true. When false, then the models are not reset. This is so |
|
1512 | - * client |
|
1513 | - * code instead can just change the model context to a different blog id if |
|
1514 | - * necessary |
|
1515 | - * @return EE_Registry |
|
1516 | - * @throws EE_Error |
|
1517 | - * @throws ReflectionException |
|
1518 | - */ |
|
1519 | - public static function reset($hard = false, $reinstantiate = true, $reset_models = true) |
|
1520 | - { |
|
1521 | - $instance = self::instance(); |
|
1522 | - $instance->_cache_on = true; |
|
1523 | - // reset some "special" classes |
|
1524 | - EEH_Activation::reset(); |
|
1525 | - $hard = apply_filters( 'FHEE__EE_Registry__reset__hard', $hard); |
|
1526 | - $instance->CFG = EE_Config::reset($hard, $reinstantiate); |
|
1527 | - $instance->CART = null; |
|
1528 | - $instance->MRM = null; |
|
1529 | - $instance->AssetsRegistry = $instance->create('EventEspresso\core\services\assets\Registry'); |
|
1530 | - //messages reset |
|
1531 | - EED_Messages::reset(); |
|
1532 | - //handle of objects cached on LIB |
|
1533 | - foreach (array('LIB', 'modules') as $cache) { |
|
1534 | - foreach ($instance->{$cache} as $class_name => $class) { |
|
1535 | - if (self::_reset_and_unset_object($class, $reset_models)) { |
|
1536 | - unset($instance->{$cache}->{$class_name}); |
|
1537 | - } |
|
1538 | - } |
|
1539 | - } |
|
1540 | - return $instance; |
|
1541 | - } |
|
1542 | - |
|
1543 | - |
|
1544 | - |
|
1545 | - /** |
|
1546 | - * if passed object implements ResettableInterface, then call it's reset() method |
|
1547 | - * if passed object implements InterminableInterface, then return false, |
|
1548 | - * to indicate that it should NOT be cleared from the Registry cache |
|
1549 | - * |
|
1550 | - * @param $object |
|
1551 | - * @param bool $reset_models |
|
1552 | - * @return bool returns true if cached object should be unset |
|
1553 | - */ |
|
1554 | - private static function _reset_and_unset_object($object, $reset_models) |
|
1555 | - { |
|
1556 | - if (! is_object($object)) { |
|
1557 | - // don't unset anything that's not an object |
|
1558 | - return false; |
|
1559 | - } |
|
1560 | - if ($object instanceof EED_Module) { |
|
1561 | - $object::reset(); |
|
1562 | - // don't unset modules |
|
1563 | - return false; |
|
1564 | - } |
|
1565 | - if ($object instanceof ResettableInterface) { |
|
1566 | - if ($object instanceof EEM_Base) { |
|
1567 | - if ($reset_models) { |
|
1568 | - $object->reset(); |
|
1569 | - return true; |
|
1570 | - } |
|
1571 | - return false; |
|
1572 | - } |
|
1573 | - $object->reset(); |
|
1574 | - return true; |
|
1575 | - } |
|
1576 | - if (! $object instanceof InterminableInterface) { |
|
1577 | - return true; |
|
1578 | - } |
|
1579 | - return false; |
|
1580 | - } |
|
1581 | - |
|
1582 | - |
|
1583 | - |
|
1584 | - /** |
|
1585 | - * Gets all the custom post type models defined |
|
1586 | - * |
|
1587 | - * @return array keys are model "short names" (Eg "Event") and keys are classnames (eg "EEM_Event") |
|
1588 | - */ |
|
1589 | - public function cpt_models() |
|
1590 | - { |
|
1591 | - $cpt_models = array(); |
|
1592 | - foreach ($this->non_abstract_db_models as $short_name => $classname) { |
|
1593 | - if (is_subclass_of($classname, 'EEM_CPT_Base')) { |
|
1594 | - $cpt_models[$short_name] = $classname; |
|
1595 | - } |
|
1596 | - } |
|
1597 | - return $cpt_models; |
|
1598 | - } |
|
1599 | - |
|
1600 | - |
|
1601 | - |
|
1602 | - /** |
|
1603 | - * @return \EE_Config |
|
1604 | - */ |
|
1605 | - public static function CFG() |
|
1606 | - { |
|
1607 | - return self::instance()->CFG; |
|
1608 | - } |
|
26 | + /** |
|
27 | + * @var EE_Registry $_instance |
|
28 | + */ |
|
29 | + private static $_instance; |
|
30 | + |
|
31 | + /** |
|
32 | + * @var EE_Dependency_Map $_dependency_map |
|
33 | + */ |
|
34 | + protected $_dependency_map; |
|
35 | + |
|
36 | + /** |
|
37 | + * @var array $_class_abbreviations |
|
38 | + */ |
|
39 | + protected $_class_abbreviations = array(); |
|
40 | + |
|
41 | + /** |
|
42 | + * @var CommandBusInterface $BUS |
|
43 | + */ |
|
44 | + public $BUS; |
|
45 | + |
|
46 | + /** |
|
47 | + * @var EE_Cart $CART |
|
48 | + */ |
|
49 | + public $CART; |
|
50 | + |
|
51 | + /** |
|
52 | + * @var EE_Config $CFG |
|
53 | + */ |
|
54 | + public $CFG; |
|
55 | + |
|
56 | + /** |
|
57 | + * @var EE_Network_Config $NET_CFG |
|
58 | + */ |
|
59 | + public $NET_CFG; |
|
60 | + |
|
61 | + /** |
|
62 | + * StdClass object for storing library classes in |
|
63 | + * |
|
64 | + * @var StdClass $LIB |
|
65 | + */ |
|
66 | + public $LIB; |
|
67 | + |
|
68 | + /** |
|
69 | + * @var EE_Request_Handler $REQ |
|
70 | + */ |
|
71 | + public $REQ; |
|
72 | + |
|
73 | + /** |
|
74 | + * @var EE_Session $SSN |
|
75 | + */ |
|
76 | + public $SSN; |
|
77 | + |
|
78 | + /** |
|
79 | + * @since 4.5.0 |
|
80 | + * @var EE_Capabilities $CAP |
|
81 | + */ |
|
82 | + public $CAP; |
|
83 | + |
|
84 | + /** |
|
85 | + * @since 4.9.0 |
|
86 | + * @var EE_Message_Resource_Manager $MRM |
|
87 | + */ |
|
88 | + public $MRM; |
|
89 | + |
|
90 | + |
|
91 | + /** |
|
92 | + * @var Registry $AssetsRegistry |
|
93 | + */ |
|
94 | + public $AssetsRegistry; |
|
95 | + |
|
96 | + /** |
|
97 | + * StdClass object for holding addons which have registered themselves to work with EE core |
|
98 | + * |
|
99 | + * @var EE_Addon[] $addons |
|
100 | + */ |
|
101 | + public $addons; |
|
102 | + |
|
103 | + /** |
|
104 | + * keys are 'short names' (eg Event), values are class names (eg 'EEM_Event') |
|
105 | + * |
|
106 | + * @var EEM_Base[] $models |
|
107 | + */ |
|
108 | + public $models = array(); |
|
109 | + |
|
110 | + /** |
|
111 | + * @var EED_Module[] $modules |
|
112 | + */ |
|
113 | + public $modules; |
|
114 | + |
|
115 | + /** |
|
116 | + * @var EES_Shortcode[] $shortcodes |
|
117 | + */ |
|
118 | + public $shortcodes; |
|
119 | + |
|
120 | + /** |
|
121 | + * @var WP_Widget[] $widgets |
|
122 | + */ |
|
123 | + public $widgets; |
|
124 | + |
|
125 | + /** |
|
126 | + * this is an array of all implemented model names (i.e. not the parent abstract models, or models |
|
127 | + * which don't actually fetch items from the DB in the normal way (ie, are not children of EEM_Base)). |
|
128 | + * Keys are model "short names" (eg "Event") as used in model relations, and values are |
|
129 | + * classnames (eg "EEM_Event") |
|
130 | + * |
|
131 | + * @var array $non_abstract_db_models |
|
132 | + */ |
|
133 | + public $non_abstract_db_models = array(); |
|
134 | + |
|
135 | + |
|
136 | + /** |
|
137 | + * internationalization for JS strings |
|
138 | + * usage: EE_Registry::i18n_js_strings['string_key'] = esc_html__( 'string to translate.', 'event_espresso' ); |
|
139 | + * in js file: var translatedString = eei18n.string_key; |
|
140 | + * |
|
141 | + * @var array $i18n_js_strings |
|
142 | + */ |
|
143 | + public static $i18n_js_strings = array(); |
|
144 | + |
|
145 | + |
|
146 | + /** |
|
147 | + * $main_file - path to espresso.php |
|
148 | + * |
|
149 | + * @var array $main_file |
|
150 | + */ |
|
151 | + public $main_file; |
|
152 | + |
|
153 | + /** |
|
154 | + * array of ReflectionClass objects where the key is the class name |
|
155 | + * |
|
156 | + * @var ReflectionClass[] $_reflectors |
|
157 | + */ |
|
158 | + public $_reflectors; |
|
159 | + |
|
160 | + /** |
|
161 | + * boolean flag to indicate whether or not to load/save dependencies from/to the cache |
|
162 | + * |
|
163 | + * @var boolean $_cache_on |
|
164 | + */ |
|
165 | + protected $_cache_on = true; |
|
166 | + |
|
167 | + |
|
168 | + |
|
169 | + /** |
|
170 | + * @singleton method used to instantiate class object |
|
171 | + * @param EE_Dependency_Map $dependency_map |
|
172 | + * @return EE_Registry instance |
|
173 | + * @throws InvalidArgumentException |
|
174 | + * @throws InvalidInterfaceException |
|
175 | + * @throws InvalidDataTypeException |
|
176 | + */ |
|
177 | + public static function instance(EE_Dependency_Map $dependency_map = null) |
|
178 | + { |
|
179 | + // check if class object is instantiated |
|
180 | + if (! self::$_instance instanceof EE_Registry) { |
|
181 | + self::$_instance = new self($dependency_map); |
|
182 | + } |
|
183 | + return self::$_instance; |
|
184 | + } |
|
185 | + |
|
186 | + |
|
187 | + |
|
188 | + /** |
|
189 | + * protected constructor to prevent direct creation |
|
190 | + * |
|
191 | + * @Constructor |
|
192 | + * @param EE_Dependency_Map $dependency_map |
|
193 | + * @throws InvalidDataTypeException |
|
194 | + * @throws InvalidInterfaceException |
|
195 | + * @throws InvalidArgumentException |
|
196 | + */ |
|
197 | + protected function __construct(EE_Dependency_Map $dependency_map) |
|
198 | + { |
|
199 | + $this->_dependency_map = $dependency_map; |
|
200 | + // $registry_container = new RegistryContainer(); |
|
201 | + $this->LIB = new RegistryContainer(); |
|
202 | + $this->addons = new RegistryContainer(); |
|
203 | + $this->modules = new RegistryContainer(); |
|
204 | + $this->shortcodes = new RegistryContainer(); |
|
205 | + $this->widgets = new RegistryContainer(); |
|
206 | + add_action('EE_Load_Espresso_Core__handle_request__initialize_core_loading', array($this, 'initialize')); |
|
207 | + } |
|
208 | + |
|
209 | + |
|
210 | + |
|
211 | + /** |
|
212 | + * initialize |
|
213 | + * |
|
214 | + * @throws EE_Error |
|
215 | + * @throws ReflectionException |
|
216 | + */ |
|
217 | + public function initialize() |
|
218 | + { |
|
219 | + $this->_class_abbreviations = apply_filters( |
|
220 | + 'FHEE__EE_Registry____construct___class_abbreviations', |
|
221 | + array( |
|
222 | + 'EE_Config' => 'CFG', |
|
223 | + 'EE_Session' => 'SSN', |
|
224 | + 'EE_Capabilities' => 'CAP', |
|
225 | + 'EE_Cart' => 'CART', |
|
226 | + 'EE_Network_Config' => 'NET_CFG', |
|
227 | + 'EE_Request_Handler' => 'REQ', |
|
228 | + 'EE_Message_Resource_Manager' => 'MRM', |
|
229 | + 'EventEspresso\core\services\commands\CommandBus' => 'BUS', |
|
230 | + 'EventEspresso\core\services\assets\Registry' => 'AssetsRegistry', |
|
231 | + ) |
|
232 | + ); |
|
233 | + $this->load_core('Base', array(), true); |
|
234 | + // add our request and response objects to the cache |
|
235 | + $request_loader = $this->_dependency_map->class_loader( |
|
236 | + 'EventEspresso\core\services\request\Request' |
|
237 | + ); |
|
238 | + $this->_set_cached_class( |
|
239 | + $request_loader(), |
|
240 | + 'EventEspresso\core\services\request\Request' |
|
241 | + ); |
|
242 | + $response_loader = $this->_dependency_map->class_loader( |
|
243 | + 'EventEspresso\core\services\request\Response' |
|
244 | + ); |
|
245 | + $this->_set_cached_class( |
|
246 | + $response_loader(), |
|
247 | + 'EventEspresso\core\services\request\Response' |
|
248 | + ); |
|
249 | + add_action('AHEE__EE_System__set_hooks_for_core', array($this, 'init')); |
|
250 | + } |
|
251 | + |
|
252 | + |
|
253 | + |
|
254 | + /** |
|
255 | + * @return void |
|
256 | + */ |
|
257 | + public function init() |
|
258 | + { |
|
259 | + // Get current page protocol |
|
260 | + $protocol = isset($_SERVER['HTTPS']) ? 'https://' : 'http://'; |
|
261 | + // Output admin-ajax.php URL with same protocol as current page |
|
262 | + self::$i18n_js_strings['ajax_url'] = admin_url('admin-ajax.php', $protocol); |
|
263 | + self::$i18n_js_strings['wp_debug'] = defined('WP_DEBUG') ? WP_DEBUG : false; |
|
264 | + } |
|
265 | + |
|
266 | + |
|
267 | + |
|
268 | + /** |
|
269 | + * localize_i18n_js_strings |
|
270 | + * |
|
271 | + * @return string |
|
272 | + */ |
|
273 | + public static function localize_i18n_js_strings() |
|
274 | + { |
|
275 | + $i18n_js_strings = (array)self::$i18n_js_strings; |
|
276 | + foreach ($i18n_js_strings as $key => $value) { |
|
277 | + if (is_scalar($value)) { |
|
278 | + $i18n_js_strings[$key] = html_entity_decode((string)$value, ENT_QUOTES, 'UTF-8'); |
|
279 | + } |
|
280 | + } |
|
281 | + return '/* <![CDATA[ */ var eei18n = ' . wp_json_encode($i18n_js_strings) . '; /* ]]> */'; |
|
282 | + } |
|
283 | + |
|
284 | + |
|
285 | + |
|
286 | + /** |
|
287 | + * @param mixed string | EED_Module $module |
|
288 | + * @throws EE_Error |
|
289 | + * @throws ReflectionException |
|
290 | + */ |
|
291 | + public function add_module($module) |
|
292 | + { |
|
293 | + if ($module instanceof EED_Module) { |
|
294 | + $module_class = get_class($module); |
|
295 | + $this->modules->{$module_class} = $module; |
|
296 | + } else { |
|
297 | + if ( ! class_exists('EE_Module_Request_Router', false)) { |
|
298 | + $this->load_core('Module_Request_Router'); |
|
299 | + } |
|
300 | + EE_Module_Request_Router::module_factory($module); |
|
301 | + } |
|
302 | + } |
|
303 | + |
|
304 | + |
|
305 | + |
|
306 | + /** |
|
307 | + * @param string $module_name |
|
308 | + * @return mixed EED_Module | NULL |
|
309 | + */ |
|
310 | + public function get_module($module_name = '') |
|
311 | + { |
|
312 | + return isset($this->modules->{$module_name}) |
|
313 | + ? $this->modules->{$module_name} |
|
314 | + : null; |
|
315 | + } |
|
316 | + |
|
317 | + |
|
318 | + |
|
319 | + /** |
|
320 | + * loads core classes - must be singletons |
|
321 | + * |
|
322 | + * @param string $class_name - simple class name ie: session |
|
323 | + * @param mixed $arguments |
|
324 | + * @param bool $load_only |
|
325 | + * @return mixed |
|
326 | + * @throws EE_Error |
|
327 | + * @throws ReflectionException |
|
328 | + */ |
|
329 | + public function load_core($class_name, $arguments = array(), $load_only = false) |
|
330 | + { |
|
331 | + $core_paths = apply_filters( |
|
332 | + 'FHEE__EE_Registry__load_core__core_paths', |
|
333 | + array( |
|
334 | + EE_CORE, |
|
335 | + EE_ADMIN, |
|
336 | + EE_CPTS, |
|
337 | + EE_CORE . 'data_migration_scripts' . DS, |
|
338 | + EE_CORE . 'capabilities' . DS, |
|
339 | + EE_CORE . 'request_stack' . DS, |
|
340 | + EE_CORE . 'middleware' . DS, |
|
341 | + ) |
|
342 | + ); |
|
343 | + // retrieve instantiated class |
|
344 | + return $this->_load( |
|
345 | + $core_paths, |
|
346 | + 'EE_', |
|
347 | + $class_name, |
|
348 | + 'core', |
|
349 | + $arguments, |
|
350 | + false, |
|
351 | + true, |
|
352 | + $load_only |
|
353 | + ); |
|
354 | + } |
|
355 | + |
|
356 | + |
|
357 | + |
|
358 | + /** |
|
359 | + * loads service classes |
|
360 | + * |
|
361 | + * @param string $class_name - simple class name ie: session |
|
362 | + * @param mixed $arguments |
|
363 | + * @param bool $load_only |
|
364 | + * @return mixed |
|
365 | + * @throws EE_Error |
|
366 | + * @throws ReflectionException |
|
367 | + */ |
|
368 | + public function load_service($class_name, $arguments = array(), $load_only = false) |
|
369 | + { |
|
370 | + $service_paths = apply_filters( |
|
371 | + 'FHEE__EE_Registry__load_service__service_paths', |
|
372 | + array( |
|
373 | + EE_CORE . 'services' . DS, |
|
374 | + ) |
|
375 | + ); |
|
376 | + // retrieve instantiated class |
|
377 | + return $this->_load( |
|
378 | + $service_paths, |
|
379 | + 'EE_', |
|
380 | + $class_name, |
|
381 | + 'class', |
|
382 | + $arguments, |
|
383 | + false, |
|
384 | + true, |
|
385 | + $load_only |
|
386 | + ); |
|
387 | + } |
|
388 | + |
|
389 | + |
|
390 | + |
|
391 | + /** |
|
392 | + * loads data_migration_scripts |
|
393 | + * |
|
394 | + * @param string $class_name - class name for the DMS ie: EE_DMS_Core_4_2_0 |
|
395 | + * @param mixed $arguments |
|
396 | + * @return EE_Data_Migration_Script_Base|mixed |
|
397 | + * @throws EE_Error |
|
398 | + * @throws ReflectionException |
|
399 | + */ |
|
400 | + public function load_dms($class_name, $arguments = array()) |
|
401 | + { |
|
402 | + // retrieve instantiated class |
|
403 | + return $this->_load( |
|
404 | + EE_Data_Migration_Manager::instance()->get_data_migration_script_folders(), |
|
405 | + 'EE_DMS_', |
|
406 | + $class_name, |
|
407 | + 'dms', |
|
408 | + $arguments, |
|
409 | + false, |
|
410 | + false |
|
411 | + ); |
|
412 | + } |
|
413 | + |
|
414 | + |
|
415 | + |
|
416 | + /** |
|
417 | + * loads object creating classes - must be singletons |
|
418 | + * |
|
419 | + * @param string $class_name - simple class name ie: attendee |
|
420 | + * @param mixed $arguments - an array of arguments to pass to the class |
|
421 | + * @param bool $from_db - some classes are instantiated from the db and thus call a different method to |
|
422 | + * instantiate |
|
423 | + * @param bool $cache if you don't want the class to be stored in the internal cache (non-persistent) then |
|
424 | + * set this to FALSE (ie. when instantiating model objects from client in a loop) |
|
425 | + * @param bool $load_only whether or not to just load the file and NOT instantiate, or load AND instantiate |
|
426 | + * (default) |
|
427 | + * @return EE_Base_Class | bool |
|
428 | + * @throws EE_Error |
|
429 | + * @throws ReflectionException |
|
430 | + */ |
|
431 | + public function load_class($class_name, $arguments = array(), $from_db = false, $cache = true, $load_only = false) |
|
432 | + { |
|
433 | + $paths = apply_filters( |
|
434 | + 'FHEE__EE_Registry__load_class__paths', array( |
|
435 | + EE_CORE, |
|
436 | + EE_CLASSES, |
|
437 | + EE_BUSINESS, |
|
438 | + ) |
|
439 | + ); |
|
440 | + // retrieve instantiated class |
|
441 | + return $this->_load( |
|
442 | + $paths, |
|
443 | + 'EE_', |
|
444 | + $class_name, |
|
445 | + 'class', |
|
446 | + $arguments, |
|
447 | + $from_db, |
|
448 | + $cache, |
|
449 | + $load_only |
|
450 | + ); |
|
451 | + } |
|
452 | + |
|
453 | + |
|
454 | + |
|
455 | + /** |
|
456 | + * loads helper classes - must be singletons |
|
457 | + * |
|
458 | + * @param string $class_name - simple class name ie: price |
|
459 | + * @param mixed $arguments |
|
460 | + * @param bool $load_only |
|
461 | + * @return EEH_Base | bool |
|
462 | + * @throws EE_Error |
|
463 | + * @throws ReflectionException |
|
464 | + */ |
|
465 | + public function load_helper($class_name, $arguments = array(), $load_only = true) |
|
466 | + { |
|
467 | + // todo: add doing_it_wrong() in a few versions after all addons have had calls to this method removed |
|
468 | + $helper_paths = apply_filters('FHEE__EE_Registry__load_helper__helper_paths', array(EE_HELPERS)); |
|
469 | + // retrieve instantiated class |
|
470 | + return $this->_load( |
|
471 | + $helper_paths, |
|
472 | + 'EEH_', |
|
473 | + $class_name, |
|
474 | + 'helper', |
|
475 | + $arguments, |
|
476 | + false, |
|
477 | + true, |
|
478 | + $load_only |
|
479 | + ); |
|
480 | + } |
|
481 | + |
|
482 | + |
|
483 | + |
|
484 | + /** |
|
485 | + * loads core classes - must be singletons |
|
486 | + * |
|
487 | + * @param string $class_name - simple class name ie: session |
|
488 | + * @param mixed $arguments |
|
489 | + * @param bool $load_only |
|
490 | + * @param bool $cache whether to cache the object or not. |
|
491 | + * @return mixed |
|
492 | + * @throws EE_Error |
|
493 | + * @throws ReflectionException |
|
494 | + */ |
|
495 | + public function load_lib($class_name, $arguments = array(), $load_only = false, $cache = true) |
|
496 | + { |
|
497 | + $paths = array( |
|
498 | + EE_LIBRARIES, |
|
499 | + EE_LIBRARIES . 'messages' . DS, |
|
500 | + EE_LIBRARIES . 'shortcodes' . DS, |
|
501 | + EE_LIBRARIES . 'qtips' . DS, |
|
502 | + EE_LIBRARIES . 'payment_methods' . DS, |
|
503 | + ); |
|
504 | + // retrieve instantiated class |
|
505 | + return $this->_load( |
|
506 | + $paths, |
|
507 | + 'EE_', |
|
508 | + $class_name, |
|
509 | + 'lib', |
|
510 | + $arguments, |
|
511 | + false, |
|
512 | + $cache, |
|
513 | + $load_only |
|
514 | + ); |
|
515 | + } |
|
516 | + |
|
517 | + |
|
518 | + |
|
519 | + /** |
|
520 | + * loads model classes - must be singletons |
|
521 | + * |
|
522 | + * @param string $class_name - simple class name ie: price |
|
523 | + * @param mixed $arguments |
|
524 | + * @param bool $load_only |
|
525 | + * @return EEM_Base | bool |
|
526 | + * @throws EE_Error |
|
527 | + * @throws ReflectionException |
|
528 | + */ |
|
529 | + public function load_model($class_name, $arguments = array(), $load_only = false) |
|
530 | + { |
|
531 | + $paths = apply_filters( |
|
532 | + 'FHEE__EE_Registry__load_model__paths', array( |
|
533 | + EE_MODELS, |
|
534 | + EE_CORE, |
|
535 | + ) |
|
536 | + ); |
|
537 | + // retrieve instantiated class |
|
538 | + return $this->_load( |
|
539 | + $paths, |
|
540 | + 'EEM_', |
|
541 | + $class_name, |
|
542 | + 'model', |
|
543 | + $arguments, |
|
544 | + false, |
|
545 | + true, |
|
546 | + $load_only |
|
547 | + ); |
|
548 | + } |
|
549 | + |
|
550 | + |
|
551 | + |
|
552 | + /** |
|
553 | + * loads model classes - must be singletons |
|
554 | + * |
|
555 | + * @param string $class_name - simple class name ie: price |
|
556 | + * @param mixed $arguments |
|
557 | + * @param bool $load_only |
|
558 | + * @return mixed | bool |
|
559 | + * @throws EE_Error |
|
560 | + * @throws ReflectionException |
|
561 | + */ |
|
562 | + public function load_model_class($class_name, $arguments = array(), $load_only = true) |
|
563 | + { |
|
564 | + $paths = array( |
|
565 | + EE_MODELS . 'fields' . DS, |
|
566 | + EE_MODELS . 'helpers' . DS, |
|
567 | + EE_MODELS . 'relations' . DS, |
|
568 | + EE_MODELS . 'strategies' . DS, |
|
569 | + ); |
|
570 | + // retrieve instantiated class |
|
571 | + return $this->_load( |
|
572 | + $paths, |
|
573 | + 'EE_', |
|
574 | + $class_name, |
|
575 | + '', |
|
576 | + $arguments, |
|
577 | + false, |
|
578 | + true, |
|
579 | + $load_only |
|
580 | + ); |
|
581 | + } |
|
582 | + |
|
583 | + |
|
584 | + |
|
585 | + /** |
|
586 | + * Determines if $model_name is the name of an actual EE model. |
|
587 | + * |
|
588 | + * @param string $model_name like Event, Attendee, Question_Group_Question, etc. |
|
589 | + * @return boolean |
|
590 | + */ |
|
591 | + public function is_model_name($model_name) |
|
592 | + { |
|
593 | + return isset($this->models[$model_name]); |
|
594 | + } |
|
595 | + |
|
596 | + |
|
597 | + |
|
598 | + /** |
|
599 | + * generic class loader |
|
600 | + * |
|
601 | + * @param string $path_to_file - directory path to file location, not including filename |
|
602 | + * @param string $file_name - file name ie: my_file.php, including extension |
|
603 | + * @param string $type - file type - core? class? helper? model? |
|
604 | + * @param mixed $arguments |
|
605 | + * @param bool $load_only |
|
606 | + * @return mixed |
|
607 | + * @throws EE_Error |
|
608 | + * @throws ReflectionException |
|
609 | + */ |
|
610 | + public function load_file($path_to_file, $file_name, $type = '', $arguments = array(), $load_only = true) |
|
611 | + { |
|
612 | + // retrieve instantiated class |
|
613 | + return $this->_load( |
|
614 | + $path_to_file, |
|
615 | + '', |
|
616 | + $file_name, |
|
617 | + $type, |
|
618 | + $arguments, |
|
619 | + false, |
|
620 | + true, |
|
621 | + $load_only |
|
622 | + ); |
|
623 | + } |
|
624 | + |
|
625 | + |
|
626 | + |
|
627 | + /** |
|
628 | + * @param string $path_to_file - directory path to file location, not including filename |
|
629 | + * @param string $class_name - full class name ie: My_Class |
|
630 | + * @param string $type - file type - core? class? helper? model? |
|
631 | + * @param mixed $arguments |
|
632 | + * @param bool $load_only |
|
633 | + * @return bool|EE_Addon|object |
|
634 | + * @throws EE_Error |
|
635 | + * @throws ReflectionException |
|
636 | + */ |
|
637 | + public function load_addon($path_to_file, $class_name, $type = 'class', $arguments = array(), $load_only = false) |
|
638 | + { |
|
639 | + // retrieve instantiated class |
|
640 | + return $this->_load( |
|
641 | + $path_to_file, |
|
642 | + 'addon', |
|
643 | + $class_name, |
|
644 | + $type, |
|
645 | + $arguments, |
|
646 | + false, |
|
647 | + true, |
|
648 | + $load_only |
|
649 | + ); |
|
650 | + } |
|
651 | + |
|
652 | + |
|
653 | + |
|
654 | + /** |
|
655 | + * instantiates, caches, and automatically resolves dependencies |
|
656 | + * for classes that use a Fully Qualified Class Name. |
|
657 | + * if the class is not capable of being loaded using PSR-4 autoloading, |
|
658 | + * then you need to use one of the existing load_*() methods |
|
659 | + * which can resolve the classname and filepath from the passed arguments |
|
660 | + * |
|
661 | + * @param bool|string $class_name Fully Qualified Class Name |
|
662 | + * @param array $arguments an argument, or array of arguments to pass to the class upon instantiation |
|
663 | + * @param bool $cache whether to cache the instantiated object for reuse |
|
664 | + * @param bool $from_db some classes are instantiated from the db |
|
665 | + * and thus call a different method to instantiate |
|
666 | + * @param bool $load_only if true, will only load the file, but will NOT instantiate an object |
|
667 | + * @param bool|string $addon if true, will cache the object in the EE_Registry->$addons array |
|
668 | + * @return bool|null|mixed null = failure to load or instantiate class object. |
|
669 | + * object = class loaded and instantiated successfully. |
|
670 | + * bool = fail or success when $load_only is true |
|
671 | + * @throws EE_Error |
|
672 | + * @throws ReflectionException |
|
673 | + */ |
|
674 | + public function create( |
|
675 | + $class_name = false, |
|
676 | + $arguments = array(), |
|
677 | + $cache = false, |
|
678 | + $from_db = false, |
|
679 | + $load_only = false, |
|
680 | + $addon = false |
|
681 | + ) { |
|
682 | + $class_name = ltrim($class_name, '\\'); |
|
683 | + $class_name = $this->_dependency_map->get_alias($class_name); |
|
684 | + $class_exists = $this->loadOrVerifyClassExists($class_name, $arguments); |
|
685 | + // if a non-FQCN was passed, then verifyClassExists() might return an object |
|
686 | + // or it could return null if the class just could not be found anywhere |
|
687 | + if ($class_exists instanceof $class_name || $class_exists === null){ |
|
688 | + // either way, return the results |
|
689 | + return $class_exists; |
|
690 | + } |
|
691 | + $class_name = $class_exists; |
|
692 | + // if we're only loading the class and it already exists, then let's just return true immediately |
|
693 | + if ($load_only) { |
|
694 | + return true; |
|
695 | + } |
|
696 | + $addon = $addon |
|
697 | + ? 'addon' |
|
698 | + : ''; |
|
699 | + // $this->_cache_on is toggled during the recursive loading that can occur with dependency injection |
|
700 | + // $cache is controlled by individual calls to separate Registry loader methods like load_class() |
|
701 | + // $load_only is also controlled by individual calls to separate Registry loader methods like load_file() |
|
702 | + if ($this->_cache_on && $cache && ! $load_only) { |
|
703 | + // return object if it's already cached |
|
704 | + $cached_class = $this->_get_cached_class($class_name, $addon); |
|
705 | + if ($cached_class !== null) { |
|
706 | + return $cached_class; |
|
707 | + } |
|
708 | + } |
|
709 | + // obtain the loader method from the dependency map |
|
710 | + $loader = $this->_dependency_map->class_loader($class_name); |
|
711 | + // instantiate the requested object |
|
712 | + if ($loader instanceof Closure) { |
|
713 | + $class_obj = $loader($arguments); |
|
714 | + } else if ($loader && method_exists($this, $loader)) { |
|
715 | + $class_obj = $this->{$loader}($class_name, $arguments); |
|
716 | + } else { |
|
717 | + $class_obj = $this->_create_object($class_name, $arguments, $addon, $from_db); |
|
718 | + } |
|
719 | + if (($this->_cache_on && $cache) || $this->get_class_abbreviation($class_name, '')) { |
|
720 | + // save it for later... kinda like gum { : $ |
|
721 | + $this->_set_cached_class($class_obj, $class_name, $addon, $from_db); |
|
722 | + } |
|
723 | + $this->_cache_on = true; |
|
724 | + return $class_obj; |
|
725 | + } |
|
726 | + |
|
727 | + |
|
728 | + |
|
729 | + /** |
|
730 | + * Recursively checks that a class exists and potentially attempts to load classes with non-FQCNs |
|
731 | + * |
|
732 | + * @param string $class_name |
|
733 | + * @param array $arguments |
|
734 | + * @param int $attempt |
|
735 | + * @return mixed |
|
736 | + */ |
|
737 | + private function loadOrVerifyClassExists($class_name, array $arguments, $attempt = 1) { |
|
738 | + if (is_object($class_name) || class_exists($class_name)) { |
|
739 | + return $class_name; |
|
740 | + } |
|
741 | + switch ($attempt) { |
|
742 | + case 1: |
|
743 | + // if it's a FQCN then maybe the class is registered with a preceding \ |
|
744 | + $class_name = strpos($class_name, '\\') !== false |
|
745 | + ? '\\' . ltrim($class_name, '\\') |
|
746 | + : $class_name; |
|
747 | + break; |
|
748 | + case 2: |
|
749 | + // |
|
750 | + $loader = $this->_dependency_map->class_loader($class_name); |
|
751 | + if ($loader && method_exists($this, $loader)) { |
|
752 | + return $this->{$loader}($class_name, $arguments); |
|
753 | + } |
|
754 | + break; |
|
755 | + case 3: |
|
756 | + default; |
|
757 | + return null; |
|
758 | + } |
|
759 | + $attempt++; |
|
760 | + return $this->loadOrVerifyClassExists($class_name, $arguments, $attempt); |
|
761 | + } |
|
762 | + |
|
763 | + |
|
764 | + |
|
765 | + /** |
|
766 | + * instantiates, caches, and injects dependencies for classes |
|
767 | + * |
|
768 | + * @param array $file_paths an array of paths to folders to look in |
|
769 | + * @param string $class_prefix EE or EEM or... ??? |
|
770 | + * @param bool|string $class_name $class name |
|
771 | + * @param string $type file type - core? class? helper? model? |
|
772 | + * @param mixed $arguments an argument or array of arguments to pass to the class upon instantiation |
|
773 | + * @param bool $from_db some classes are instantiated from the db |
|
774 | + * and thus call a different method to instantiate |
|
775 | + * @param bool $cache whether to cache the instantiated object for reuse |
|
776 | + * @param bool $load_only if true, will only load the file, but will NOT instantiate an object |
|
777 | + * @return bool|null|object null = failure to load or instantiate class object. |
|
778 | + * object = class loaded and instantiated successfully. |
|
779 | + * bool = fail or success when $load_only is true |
|
780 | + * @throws EE_Error |
|
781 | + * @throws ReflectionException |
|
782 | + */ |
|
783 | + protected function _load( |
|
784 | + $file_paths = array(), |
|
785 | + $class_prefix = 'EE_', |
|
786 | + $class_name = false, |
|
787 | + $type = 'class', |
|
788 | + $arguments = array(), |
|
789 | + $from_db = false, |
|
790 | + $cache = true, |
|
791 | + $load_only = false |
|
792 | + ) { |
|
793 | + $class_name = ltrim($class_name, '\\'); |
|
794 | + // strip php file extension |
|
795 | + $class_name = str_replace('.php', '', trim($class_name)); |
|
796 | + // does the class have a prefix ? |
|
797 | + if (! empty($class_prefix) && $class_prefix !== 'addon') { |
|
798 | + // make sure $class_prefix is uppercase |
|
799 | + $class_prefix = strtoupper(trim($class_prefix)); |
|
800 | + // add class prefix ONCE!!! |
|
801 | + $class_name = $class_prefix . str_replace($class_prefix, '', $class_name); |
|
802 | + } |
|
803 | + $class_name = $this->_dependency_map->get_alias($class_name); |
|
804 | + $class_exists = class_exists($class_name, false); |
|
805 | + // if we're only loading the class and it already exists, then let's just return true immediately |
|
806 | + if ($load_only && $class_exists) { |
|
807 | + return true; |
|
808 | + } |
|
809 | + // $this->_cache_on is toggled during the recursive loading that can occur with dependency injection |
|
810 | + // $cache is controlled by individual calls to separate Registry loader methods like load_class() |
|
811 | + // $load_only is also controlled by individual calls to separate Registry loader methods like load_file() |
|
812 | + if ($this->_cache_on && $cache && ! $load_only) { |
|
813 | + // return object if it's already cached |
|
814 | + $cached_class = $this->_get_cached_class($class_name, $class_prefix); |
|
815 | + if ($cached_class !== null) { |
|
816 | + return $cached_class; |
|
817 | + } |
|
818 | + } |
|
819 | + // if the class doesn't already exist.. then we need to try and find the file and load it |
|
820 | + if (! $class_exists) { |
|
821 | + // get full path to file |
|
822 | + $path = $this->_resolve_path($class_name, $type, $file_paths); |
|
823 | + // load the file |
|
824 | + $loaded = $this->_require_file($path, $class_name, $type, $file_paths); |
|
825 | + // if loading failed, or we are only loading a file but NOT instantiating an object |
|
826 | + if (! $loaded || $load_only) { |
|
827 | + // return boolean if only loading, or null if an object was expected |
|
828 | + return $load_only |
|
829 | + ? $loaded |
|
830 | + : null; |
|
831 | + } |
|
832 | + } |
|
833 | + // instantiate the requested object |
|
834 | + $class_obj = $this->_create_object($class_name, $arguments, $type, $from_db); |
|
835 | + if ($this->_cache_on && $cache) { |
|
836 | + // save it for later... kinda like gum { : $ |
|
837 | + $this->_set_cached_class($class_obj, $class_name, $class_prefix, $from_db); |
|
838 | + } |
|
839 | + $this->_cache_on = true; |
|
840 | + return $class_obj; |
|
841 | + } |
|
842 | + |
|
843 | + |
|
844 | + |
|
845 | + /** |
|
846 | + * @param string $class_name |
|
847 | + * @param string $default have to specify something, but not anything that will conflict |
|
848 | + * @return mixed|string |
|
849 | + */ |
|
850 | + protected function get_class_abbreviation($class_name, $default = 'FANCY_BATMAN_PANTS') |
|
851 | + { |
|
852 | + return isset($this->_class_abbreviations[$class_name]) |
|
853 | + ? $this->_class_abbreviations[$class_name] |
|
854 | + : $default; |
|
855 | + } |
|
856 | + |
|
857 | + /** |
|
858 | + * attempts to find a cached version of the requested class |
|
859 | + * by looking in the following places: |
|
860 | + * $this->{$class_abbreviation} ie: $this->CART |
|
861 | + * $this->{$class_name} ie: $this->Some_Class |
|
862 | + * $this->LIB->{$class_name} ie: $this->LIB->Some_Class |
|
863 | + * $this->addon->{$class_name} ie: $this->addon->Some_Addon_Class |
|
864 | + * |
|
865 | + * @param string $class_name |
|
866 | + * @param string $class_prefix |
|
867 | + * @return mixed |
|
868 | + */ |
|
869 | + protected function _get_cached_class($class_name, $class_prefix = '') |
|
870 | + { |
|
871 | + if ($class_name === 'EE_Registry') { |
|
872 | + return $this; |
|
873 | + } |
|
874 | + $class_abbreviation = $this->get_class_abbreviation($class_name); |
|
875 | + $class_name = str_replace('\\', '_', $class_name); |
|
876 | + // check if class has already been loaded, and return it if it has been |
|
877 | + if (isset($this->{$class_abbreviation})) { |
|
878 | + return $this->{$class_abbreviation}; |
|
879 | + } |
|
880 | + if (isset ($this->{$class_name})) { |
|
881 | + return $this->{$class_name}; |
|
882 | + } |
|
883 | + if (isset ($this->LIB->{$class_name})) { |
|
884 | + return $this->LIB->{$class_name}; |
|
885 | + } |
|
886 | + if ($class_prefix === 'addon' && isset ($this->addons->{$class_name})) { |
|
887 | + return $this->addons->{$class_name}; |
|
888 | + } |
|
889 | + return null; |
|
890 | + } |
|
891 | + |
|
892 | + |
|
893 | + |
|
894 | + /** |
|
895 | + * removes a cached version of the requested class |
|
896 | + * |
|
897 | + * @param string $class_name |
|
898 | + * @param boolean $addon |
|
899 | + * @return boolean |
|
900 | + */ |
|
901 | + public function clear_cached_class($class_name, $addon = false) |
|
902 | + { |
|
903 | + $class_abbreviation = $this->get_class_abbreviation($class_name); |
|
904 | + $class_name = str_replace('\\', '_', $class_name); |
|
905 | + // check if class has already been loaded, and return it if it has been |
|
906 | + if (isset($this->{$class_abbreviation})) { |
|
907 | + $this->{$class_abbreviation} = null; |
|
908 | + return true; |
|
909 | + } |
|
910 | + if (isset($this->{$class_name})) { |
|
911 | + $this->{$class_name} = null; |
|
912 | + return true; |
|
913 | + } |
|
914 | + if (isset($this->LIB->{$class_name})) { |
|
915 | + unset($this->LIB->{$class_name}); |
|
916 | + return true; |
|
917 | + } |
|
918 | + if ($addon && isset($this->addons->{$class_name})) { |
|
919 | + unset($this->addons->{$class_name}); |
|
920 | + return true; |
|
921 | + } |
|
922 | + return false; |
|
923 | + } |
|
924 | + |
|
925 | + |
|
926 | + |
|
927 | + /** |
|
928 | + * attempts to find a full valid filepath for the requested class. |
|
929 | + * loops thru each of the base paths in the $file_paths array and appends : "{classname} . {file type} . php" |
|
930 | + * then returns that path if the target file has been found and is readable |
|
931 | + * |
|
932 | + * @param string $class_name |
|
933 | + * @param string $type |
|
934 | + * @param array $file_paths |
|
935 | + * @return string | bool |
|
936 | + */ |
|
937 | + protected function _resolve_path($class_name, $type = '', $file_paths = array()) |
|
938 | + { |
|
939 | + // make sure $file_paths is an array |
|
940 | + $file_paths = is_array($file_paths) |
|
941 | + ? $file_paths |
|
942 | + : array($file_paths); |
|
943 | + // cycle thru paths |
|
944 | + foreach ($file_paths as $key => $file_path) { |
|
945 | + // convert all separators to proper DS, if no filepath, then use EE_CLASSES |
|
946 | + $file_path = $file_path |
|
947 | + ? str_replace(array('/', '\\'), DS, $file_path) |
|
948 | + : EE_CLASSES; |
|
949 | + // prep file type |
|
950 | + $type = ! empty($type) |
|
951 | + ? trim($type, '.') . '.' |
|
952 | + : ''; |
|
953 | + // build full file path |
|
954 | + $file_paths[$key] = rtrim($file_path, DS) . DS . $class_name . '.' . $type . 'php'; |
|
955 | + //does the file exist and can be read ? |
|
956 | + if (is_readable($file_paths[$key])) { |
|
957 | + return $file_paths[$key]; |
|
958 | + } |
|
959 | + } |
|
960 | + return false; |
|
961 | + } |
|
962 | + |
|
963 | + |
|
964 | + |
|
965 | + /** |
|
966 | + * basically just performs a require_once() |
|
967 | + * but with some error handling |
|
968 | + * |
|
969 | + * @param string $path |
|
970 | + * @param string $class_name |
|
971 | + * @param string $type |
|
972 | + * @param array $file_paths |
|
973 | + * @return bool |
|
974 | + * @throws EE_Error |
|
975 | + * @throws ReflectionException |
|
976 | + */ |
|
977 | + protected function _require_file($path, $class_name, $type = '', $file_paths = array()) |
|
978 | + { |
|
979 | + $this->resolve_legacy_class_parent($class_name); |
|
980 | + // don't give up! you gotta... |
|
981 | + try { |
|
982 | + //does the file exist and can it be read ? |
|
983 | + if (! $path) { |
|
984 | + // just in case the file has already been autoloaded, |
|
985 | + // but discrepancies in the naming schema are preventing it from |
|
986 | + // being loaded via one of the EE_Registry::load_*() methods, |
|
987 | + // then let's try one last hail mary before throwing an exception |
|
988 | + // and call class_exists() again, but with autoloading turned ON |
|
989 | + if(class_exists($class_name)) { |
|
990 | + return true; |
|
991 | + } |
|
992 | + // so sorry, can't find the file |
|
993 | + throw new EE_Error ( |
|
994 | + sprintf( |
|
995 | + esc_html__( |
|
996 | + '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', |
|
997 | + 'event_espresso' |
|
998 | + ), |
|
999 | + trim($type, '.'), |
|
1000 | + $class_name, |
|
1001 | + '<br />' . implode(',<br />', $file_paths) |
|
1002 | + ) |
|
1003 | + ); |
|
1004 | + } |
|
1005 | + // get the file |
|
1006 | + require_once($path); |
|
1007 | + // if the class isn't already declared somewhere |
|
1008 | + if (class_exists($class_name, false) === false) { |
|
1009 | + // so sorry, not a class |
|
1010 | + throw new EE_Error( |
|
1011 | + sprintf( |
|
1012 | + esc_html__('The %s file %s does not appear to contain the %s Class.', 'event_espresso'), |
|
1013 | + $type, |
|
1014 | + $path, |
|
1015 | + $class_name |
|
1016 | + ) |
|
1017 | + ); |
|
1018 | + } |
|
1019 | + } catch (EE_Error $e) { |
|
1020 | + $e->get_error(); |
|
1021 | + return false; |
|
1022 | + } |
|
1023 | + return true; |
|
1024 | + } |
|
1025 | + |
|
1026 | + |
|
1027 | + |
|
1028 | + /** |
|
1029 | + * Some of our legacy classes that extended a parent class would simply use a require() statement |
|
1030 | + * before their class declaration in order to ensure that the parent class was loaded. |
|
1031 | + * This is not ideal, but it's nearly impossible to determine the parent class of a non-namespaced class, |
|
1032 | + * without triggering a fatal error because the parent class has yet to be loaded and therefore doesn't exist. |
|
1033 | + * |
|
1034 | + * @param string $class_name |
|
1035 | + */ |
|
1036 | + protected function resolve_legacy_class_parent($class_name = '') |
|
1037 | + { |
|
1038 | + try { |
|
1039 | + $legacy_parent_class_map = array( |
|
1040 | + 'EE_Payment_Processor' => 'core/business/EE_Processor_Base.class.php' |
|
1041 | + ); |
|
1042 | + if(isset($legacy_parent_class_map[$class_name])) { |
|
1043 | + require_once EE_PLUGIN_DIR_PATH . $legacy_parent_class_map[$class_name]; |
|
1044 | + } |
|
1045 | + } catch (Exception $exception) { |
|
1046 | + } |
|
1047 | + } |
|
1048 | + |
|
1049 | + |
|
1050 | + |
|
1051 | + /** |
|
1052 | + * _create_object |
|
1053 | + * Attempts to instantiate the requested class via any of the |
|
1054 | + * commonly used instantiation methods employed throughout EE. |
|
1055 | + * The priority for instantiation is as follows: |
|
1056 | + * - abstract classes or any class flagged as "load only" (no instantiation occurs) |
|
1057 | + * - model objects via their 'new_instance_from_db' method |
|
1058 | + * - model objects via their 'new_instance' method |
|
1059 | + * - "singleton" classes" via their 'instance' method |
|
1060 | + * - standard instantiable classes via their __constructor |
|
1061 | + * Prior to instantiation, if the classname exists in the dependency_map, |
|
1062 | + * then the constructor for the requested class will be examined to determine |
|
1063 | + * if any dependencies exist, and if they can be injected. |
|
1064 | + * If so, then those classes will be added to the array of arguments passed to the constructor |
|
1065 | + * |
|
1066 | + * @param string $class_name |
|
1067 | + * @param array $arguments |
|
1068 | + * @param string $type |
|
1069 | + * @param bool $from_db |
|
1070 | + * @return null|object |
|
1071 | + * @throws EE_Error |
|
1072 | + * @throws ReflectionException |
|
1073 | + */ |
|
1074 | + protected function _create_object($class_name, $arguments = array(), $type = '', $from_db = false) |
|
1075 | + { |
|
1076 | + // create reflection |
|
1077 | + $reflector = $this->get_ReflectionClass($class_name); |
|
1078 | + // make sure arguments are an array |
|
1079 | + $arguments = is_array($arguments) |
|
1080 | + ? $arguments |
|
1081 | + : array($arguments); |
|
1082 | + // and if arguments array is numerically and sequentially indexed, then we want it to remain as is, |
|
1083 | + // else wrap it in an additional array so that it doesn't get split into multiple parameters |
|
1084 | + $arguments = $this->_array_is_numerically_and_sequentially_indexed($arguments) |
|
1085 | + ? $arguments |
|
1086 | + : array($arguments); |
|
1087 | + // attempt to inject dependencies ? |
|
1088 | + if ($this->_dependency_map->has($class_name)) { |
|
1089 | + $arguments = $this->_resolve_dependencies($reflector, $class_name, $arguments); |
|
1090 | + } |
|
1091 | + // instantiate the class if possible |
|
1092 | + if ($reflector->isAbstract()) { |
|
1093 | + // nothing to instantiate, loading file was enough |
|
1094 | + // does not throw an exception so $instantiation_mode is unused |
|
1095 | + // $instantiation_mode = "1) no constructor abstract class"; |
|
1096 | + return true; |
|
1097 | + } |
|
1098 | + if (empty($arguments) && $reflector->getConstructor() === null && $reflector->isInstantiable()) { |
|
1099 | + // no constructor = static methods only... nothing to instantiate, loading file was enough |
|
1100 | + // $instantiation_mode = "2) no constructor but instantiable"; |
|
1101 | + return $reflector->newInstance(); |
|
1102 | + } |
|
1103 | + if ($from_db && method_exists($class_name, 'new_instance_from_db')) { |
|
1104 | + // $instantiation_mode = "3) new_instance_from_db()"; |
|
1105 | + return call_user_func_array(array($class_name, 'new_instance_from_db'), $arguments); |
|
1106 | + } |
|
1107 | + if (method_exists($class_name, 'new_instance')) { |
|
1108 | + // $instantiation_mode = "4) new_instance()"; |
|
1109 | + return call_user_func_array(array($class_name, 'new_instance'), $arguments); |
|
1110 | + } |
|
1111 | + if (method_exists($class_name, 'instance')) { |
|
1112 | + // $instantiation_mode = "5) instance()"; |
|
1113 | + return call_user_func_array(array($class_name, 'instance'), $arguments); |
|
1114 | + } |
|
1115 | + if ($reflector->isInstantiable()) { |
|
1116 | + // $instantiation_mode = "6) constructor"; |
|
1117 | + return $reflector->newInstanceArgs($arguments); |
|
1118 | + } |
|
1119 | + // heh ? something's not right ! |
|
1120 | + throw new EE_Error( |
|
1121 | + sprintf( |
|
1122 | + __('The %s file %s could not be instantiated.', 'event_espresso'), |
|
1123 | + $type, |
|
1124 | + $class_name |
|
1125 | + ) |
|
1126 | + ); |
|
1127 | + } |
|
1128 | + |
|
1129 | + |
|
1130 | + |
|
1131 | + /** |
|
1132 | + * @see http://stackoverflow.com/questions/173400/how-to-check-if-php-array-is-associative-or-sequential |
|
1133 | + * @param array $array |
|
1134 | + * @return bool |
|
1135 | + */ |
|
1136 | + protected function _array_is_numerically_and_sequentially_indexed(array $array) |
|
1137 | + { |
|
1138 | + return ! empty($array) |
|
1139 | + ? array_keys($array) === range(0, count($array) - 1) |
|
1140 | + : true; |
|
1141 | + } |
|
1142 | + |
|
1143 | + |
|
1144 | + |
|
1145 | + /** |
|
1146 | + * getReflectionClass |
|
1147 | + * checks if a ReflectionClass object has already been generated for a class |
|
1148 | + * and returns that instead of creating a new one |
|
1149 | + * |
|
1150 | + * @param string $class_name |
|
1151 | + * @return ReflectionClass |
|
1152 | + * @throws ReflectionException |
|
1153 | + */ |
|
1154 | + public function get_ReflectionClass($class_name) |
|
1155 | + { |
|
1156 | + if ( |
|
1157 | + ! isset($this->_reflectors[$class_name]) |
|
1158 | + || ! $this->_reflectors[$class_name] instanceof ReflectionClass |
|
1159 | + ) { |
|
1160 | + $this->_reflectors[$class_name] = new ReflectionClass($class_name); |
|
1161 | + } |
|
1162 | + return $this->_reflectors[$class_name]; |
|
1163 | + } |
|
1164 | + |
|
1165 | + |
|
1166 | + |
|
1167 | + /** |
|
1168 | + * _resolve_dependencies |
|
1169 | + * examines the constructor for the requested class to determine |
|
1170 | + * if any dependencies exist, and if they can be injected. |
|
1171 | + * If so, then those classes will be added to the array of arguments passed to the constructor |
|
1172 | + * PLZ NOTE: this is achieved by type hinting the constructor params |
|
1173 | + * For example: |
|
1174 | + * if attempting to load a class "Foo" with the following constructor: |
|
1175 | + * __construct( Bar $bar_class, Fighter $grohl_class ) |
|
1176 | + * then $bar_class and $grohl_class will be added to the $arguments array, |
|
1177 | + * but only IF they are NOT already present in the incoming arguments array, |
|
1178 | + * and the correct classes can be loaded |
|
1179 | + * |
|
1180 | + * @param ReflectionClass $reflector |
|
1181 | + * @param string $class_name |
|
1182 | + * @param array $arguments |
|
1183 | + * @return array |
|
1184 | + * @throws EE_Error |
|
1185 | + * @throws ReflectionException |
|
1186 | + */ |
|
1187 | + protected function _resolve_dependencies(ReflectionClass $reflector, $class_name, $arguments = array()) |
|
1188 | + { |
|
1189 | + // let's examine the constructor |
|
1190 | + $constructor = $reflector->getConstructor(); |
|
1191 | + // whu? huh? nothing? |
|
1192 | + if (! $constructor) { |
|
1193 | + return $arguments; |
|
1194 | + } |
|
1195 | + // get constructor parameters |
|
1196 | + $params = $constructor->getParameters(); |
|
1197 | + // and the keys for the incoming arguments array so that we can compare existing arguments with what is expected |
|
1198 | + $argument_keys = array_keys($arguments); |
|
1199 | + // now loop thru all of the constructors expected parameters |
|
1200 | + foreach ($params as $index => $param) { |
|
1201 | + // is this a dependency for a specific class ? |
|
1202 | + $param_class = $param->getClass() |
|
1203 | + ? $param->getClass()->name |
|
1204 | + : null; |
|
1205 | + // BUT WAIT !!! This class may be an alias for something else (or getting replaced at runtime) |
|
1206 | + $param_class = $this->_dependency_map->has_alias($param_class, $class_name) |
|
1207 | + ? $this->_dependency_map->get_alias($param_class, $class_name) |
|
1208 | + : $param_class; |
|
1209 | + if ( |
|
1210 | + // param is not even a class |
|
1211 | + $param_class === null |
|
1212 | + // and something already exists in the incoming arguments for this param |
|
1213 | + && array_key_exists($index, $argument_keys) |
|
1214 | + && array_key_exists($argument_keys[$index], $arguments) |
|
1215 | + ) { |
|
1216 | + // so let's skip this argument and move on to the next |
|
1217 | + continue; |
|
1218 | + } |
|
1219 | + if ( |
|
1220 | + // parameter is type hinted as a class, exists as an incoming argument, AND it's the correct class |
|
1221 | + $param_class !== null |
|
1222 | + && isset($argument_keys[$index], $arguments[$argument_keys[$index]]) |
|
1223 | + && $arguments[$argument_keys[$index]] instanceof $param_class |
|
1224 | + ) { |
|
1225 | + // skip this argument and move on to the next |
|
1226 | + continue; |
|
1227 | + } |
|
1228 | + if ( |
|
1229 | + // parameter is type hinted as a class, and should be injected |
|
1230 | + $param_class !== null |
|
1231 | + && $this->_dependency_map->has_dependency_for_class($class_name, $param_class) |
|
1232 | + ) { |
|
1233 | + $arguments = $this->_resolve_dependency( |
|
1234 | + $class_name, |
|
1235 | + $param_class, |
|
1236 | + $arguments, |
|
1237 | + $index, |
|
1238 | + $argument_keys |
|
1239 | + ); |
|
1240 | + } else { |
|
1241 | + try { |
|
1242 | + $arguments[$index] = $param->isDefaultValueAvailable() |
|
1243 | + ? $param->getDefaultValue() |
|
1244 | + : null; |
|
1245 | + } catch (ReflectionException $e) { |
|
1246 | + throw new ReflectionException( |
|
1247 | + sprintf( |
|
1248 | + esc_html__('%1$s for parameter "$%2$s on classname "%3$s"', 'event_espresso'), |
|
1249 | + $e->getMessage(), |
|
1250 | + $param->getName(), |
|
1251 | + $class_name |
|
1252 | + ) |
|
1253 | + ); |
|
1254 | + } |
|
1255 | + } |
|
1256 | + } |
|
1257 | + return $arguments; |
|
1258 | + } |
|
1259 | + |
|
1260 | + |
|
1261 | + |
|
1262 | + /** |
|
1263 | + * @param string $class_name |
|
1264 | + * @param string $param_class |
|
1265 | + * @param array $arguments |
|
1266 | + * @param mixed $index |
|
1267 | + * @param array $argument_keys |
|
1268 | + * @return array |
|
1269 | + * @throws EE_Error |
|
1270 | + * @throws ReflectionException |
|
1271 | + * @throws InvalidArgumentException |
|
1272 | + * @throws InvalidInterfaceException |
|
1273 | + * @throws InvalidDataTypeException |
|
1274 | + */ |
|
1275 | + protected function _resolve_dependency($class_name, $param_class, $arguments, $index, array $argument_keys) |
|
1276 | + { |
|
1277 | + $dependency = null; |
|
1278 | + // should dependency be loaded from cache ? |
|
1279 | + $cache_on = $this->_dependency_map->loading_strategy_for_class_dependency( |
|
1280 | + $class_name, |
|
1281 | + $param_class |
|
1282 | + ); |
|
1283 | + $cache_on = $cache_on !== EE_Dependency_Map::load_new_object; |
|
1284 | + // we might have a dependency... |
|
1285 | + // let's MAYBE try and find it in our cache if that's what's been requested |
|
1286 | + $cached_class = $cache_on |
|
1287 | + ? $this->_get_cached_class($param_class) |
|
1288 | + : null; |
|
1289 | + // and grab it if it exists |
|
1290 | + if ($cached_class instanceof $param_class) { |
|
1291 | + $dependency = $cached_class; |
|
1292 | + } else if ($param_class !== $class_name) { |
|
1293 | + // obtain the loader method from the dependency map |
|
1294 | + $loader = $this->_dependency_map->class_loader($param_class); |
|
1295 | + // is loader a custom closure ? |
|
1296 | + if ($loader instanceof Closure) { |
|
1297 | + $dependency = $loader($arguments); |
|
1298 | + } else { |
|
1299 | + // set the cache on property for the recursive loading call |
|
1300 | + $this->_cache_on = $cache_on; |
|
1301 | + // if not, then let's try and load it via the registry |
|
1302 | + if ($loader && method_exists($this, $loader)) { |
|
1303 | + $dependency = $this->{$loader}($param_class); |
|
1304 | + } else { |
|
1305 | + $dependency = LoaderFactory::getLoader()->load( |
|
1306 | + $param_class, |
|
1307 | + array(), |
|
1308 | + $cache_on |
|
1309 | + ); |
|
1310 | + } |
|
1311 | + } |
|
1312 | + } |
|
1313 | + // did we successfully find the correct dependency ? |
|
1314 | + if ($dependency instanceof $param_class) { |
|
1315 | + // then let's inject it into the incoming array of arguments at the correct location |
|
1316 | + $arguments[$index] = $dependency; |
|
1317 | + } |
|
1318 | + return $arguments; |
|
1319 | + } |
|
1320 | + |
|
1321 | + |
|
1322 | + |
|
1323 | + /** |
|
1324 | + * _set_cached_class |
|
1325 | + * attempts to cache the instantiated class locally |
|
1326 | + * in one of the following places, in the following order: |
|
1327 | + * $this->{class_abbreviation} ie: $this->CART |
|
1328 | + * $this->{$class_name} ie: $this->Some_Class |
|
1329 | + * $this->addon->{$$class_name} ie: $this->addon->Some_Addon_Class |
|
1330 | + * $this->LIB->{$class_name} ie: $this->LIB->Some_Class |
|
1331 | + * |
|
1332 | + * @param object $class_obj |
|
1333 | + * @param string $class_name |
|
1334 | + * @param string $class_prefix |
|
1335 | + * @param bool $from_db |
|
1336 | + * @return void |
|
1337 | + */ |
|
1338 | + protected function _set_cached_class($class_obj, $class_name, $class_prefix = '', $from_db = false) |
|
1339 | + { |
|
1340 | + if ($class_name === 'EE_Registry' || empty($class_obj)) { |
|
1341 | + return; |
|
1342 | + } |
|
1343 | + // return newly instantiated class |
|
1344 | + $class_abbreviation = $this->get_class_abbreviation($class_name, ''); |
|
1345 | + if ($class_abbreviation) { |
|
1346 | + $this->{$class_abbreviation} = $class_obj; |
|
1347 | + return; |
|
1348 | + } |
|
1349 | + $class_name = str_replace('\\', '_', $class_name); |
|
1350 | + if (property_exists($this, $class_name)) { |
|
1351 | + $this->{$class_name} = $class_obj; |
|
1352 | + return; |
|
1353 | + } |
|
1354 | + if ($class_prefix === 'addon') { |
|
1355 | + $this->addons->{$class_name} = $class_obj; |
|
1356 | + return; |
|
1357 | + } |
|
1358 | + if (! $from_db) { |
|
1359 | + $this->LIB->{$class_name} = $class_obj; |
|
1360 | + } |
|
1361 | + } |
|
1362 | + |
|
1363 | + |
|
1364 | + |
|
1365 | + /** |
|
1366 | + * call any loader that's been registered in the EE_Dependency_Map::$_class_loaders array |
|
1367 | + * |
|
1368 | + * @param string $classname PLEASE NOTE: the class name needs to match what's registered |
|
1369 | + * in the EE_Dependency_Map::$_class_loaders array, |
|
1370 | + * including the class prefix, ie: "EE_", "EEM_", "EEH_", etc |
|
1371 | + * @param array $arguments |
|
1372 | + * @return object |
|
1373 | + */ |
|
1374 | + public static function factory($classname, $arguments = array()) |
|
1375 | + { |
|
1376 | + $loader = self::instance()->_dependency_map->class_loader($classname); |
|
1377 | + if ($loader instanceof Closure) { |
|
1378 | + return $loader($arguments); |
|
1379 | + } |
|
1380 | + if (method_exists(self::instance(), $loader)) { |
|
1381 | + return self::instance()->{$loader}($classname, $arguments); |
|
1382 | + } |
|
1383 | + return null; |
|
1384 | + } |
|
1385 | + |
|
1386 | + |
|
1387 | + |
|
1388 | + /** |
|
1389 | + * Gets the addon by its class name |
|
1390 | + * |
|
1391 | + * @param string $class_name |
|
1392 | + * @return EE_Addon |
|
1393 | + */ |
|
1394 | + public function getAddon($class_name) |
|
1395 | + { |
|
1396 | + $class_name = str_replace('\\', '_', $class_name); |
|
1397 | + return $this->addons->{$class_name}; |
|
1398 | + } |
|
1399 | + |
|
1400 | + |
|
1401 | + /** |
|
1402 | + * removes the addon from the internal cache |
|
1403 | + * |
|
1404 | + * @param string $class_name |
|
1405 | + * @return void |
|
1406 | + */ |
|
1407 | + public function removeAddon($class_name) |
|
1408 | + { |
|
1409 | + $class_name = str_replace('\\', '_', $class_name); |
|
1410 | + unset($this->addons->{$class_name}); |
|
1411 | + } |
|
1412 | + |
|
1413 | + |
|
1414 | + |
|
1415 | + /** |
|
1416 | + * Gets the addon by its name/slug (not classname. For that, just |
|
1417 | + * use the get_addon() method above |
|
1418 | + * |
|
1419 | + * @param string $name |
|
1420 | + * @return EE_Addon |
|
1421 | + */ |
|
1422 | + public function get_addon_by_name($name) |
|
1423 | + { |
|
1424 | + foreach ($this->addons as $addon) { |
|
1425 | + if ($addon->name() === $name) { |
|
1426 | + return $addon; |
|
1427 | + } |
|
1428 | + } |
|
1429 | + return null; |
|
1430 | + } |
|
1431 | + |
|
1432 | + |
|
1433 | + |
|
1434 | + /** |
|
1435 | + * Gets an array of all the registered addons, where the keys are their names. |
|
1436 | + * (ie, what each returns for their name() function) |
|
1437 | + * They're already available on EE_Registry::instance()->addons as properties, |
|
1438 | + * where each property's name is the addon's classname, |
|
1439 | + * So if you just want to get the addon by classname, |
|
1440 | + * OR use the get_addon() method above. |
|
1441 | + * PLEASE NOTE: |
|
1442 | + * addons with Fully Qualified Class Names |
|
1443 | + * have had the namespace separators converted to underscores, |
|
1444 | + * so a classname like Fully\Qualified\ClassName |
|
1445 | + * would have been converted to Fully_Qualified_ClassName |
|
1446 | + * |
|
1447 | + * @return EE_Addon[] where the KEYS are the addon's name() |
|
1448 | + */ |
|
1449 | + public function get_addons_by_name() |
|
1450 | + { |
|
1451 | + $addons = array(); |
|
1452 | + foreach ($this->addons as $addon) { |
|
1453 | + $addons[$addon->name()] = $addon; |
|
1454 | + } |
|
1455 | + return $addons; |
|
1456 | + } |
|
1457 | + |
|
1458 | + |
|
1459 | + /** |
|
1460 | + * Resets the specified model's instance AND makes sure EE_Registry doesn't keep |
|
1461 | + * a stale copy of it around |
|
1462 | + * |
|
1463 | + * @param string $model_name |
|
1464 | + * @return \EEM_Base |
|
1465 | + * @throws \EE_Error |
|
1466 | + */ |
|
1467 | + public function reset_model($model_name) |
|
1468 | + { |
|
1469 | + $model_class_name = strpos($model_name, 'EEM_') !== 0 |
|
1470 | + ? "EEM_{$model_name}" |
|
1471 | + : $model_name; |
|
1472 | + if (! isset($this->LIB->{$model_class_name}) || ! $this->LIB->{$model_class_name} instanceof EEM_Base) { |
|
1473 | + return null; |
|
1474 | + } |
|
1475 | + //get that model reset it and make sure we nuke the old reference to it |
|
1476 | + if ($this->LIB->{$model_class_name} instanceof $model_class_name |
|
1477 | + && is_callable( |
|
1478 | + array($model_class_name, 'reset') |
|
1479 | + )) { |
|
1480 | + $this->LIB->{$model_class_name} = $this->LIB->{$model_class_name}->reset(); |
|
1481 | + } else { |
|
1482 | + throw new EE_Error(sprintf(esc_html__('Model %s does not have a method "reset"', 'event_espresso'), $model_name)); |
|
1483 | + } |
|
1484 | + return $this->LIB->{$model_class_name}; |
|
1485 | + } |
|
1486 | + |
|
1487 | + |
|
1488 | + |
|
1489 | + /** |
|
1490 | + * Resets the registry. |
|
1491 | + * The criteria for what gets reset is based on what can be shared between sites on the same request when |
|
1492 | + * switch_to_blog is used in a multisite install. Here is a list of things that are NOT reset. |
|
1493 | + * - $_dependency_map |
|
1494 | + * - $_class_abbreviations |
|
1495 | + * - $NET_CFG (EE_Network_Config): The config is shared network wide so no need to reset. |
|
1496 | + * - $REQ: Still on the same request so no need to change. |
|
1497 | + * - $CAP: There is no site specific state in the EE_Capability class. |
|
1498 | + * - $SSN: Although ideally, the session should not be shared between site switches, we can't reset it because only |
|
1499 | + * one Session can be active in a single request. Resetting could resolve in "headers already sent" errors. |
|
1500 | + * - $addons: In multisite, the state of the addons is something controlled via hooks etc in a normal request. So |
|
1501 | + * for now, we won't reset the addons because it could break calls to an add-ons class/methods in the |
|
1502 | + * switch or on the restore. |
|
1503 | + * - $modules |
|
1504 | + * - $shortcodes |
|
1505 | + * - $widgets |
|
1506 | + * |
|
1507 | + * @param boolean $hard [deprecated] |
|
1508 | + * @param boolean $reinstantiate whether to create new instances of EE_Registry's singletons too, |
|
1509 | + * or just reset without re-instantiating (handy to set to FALSE if you're not |
|
1510 | + * sure if you CAN currently reinstantiate the singletons at the moment) |
|
1511 | + * @param bool $reset_models Defaults to true. When false, then the models are not reset. This is so |
|
1512 | + * client |
|
1513 | + * code instead can just change the model context to a different blog id if |
|
1514 | + * necessary |
|
1515 | + * @return EE_Registry |
|
1516 | + * @throws EE_Error |
|
1517 | + * @throws ReflectionException |
|
1518 | + */ |
|
1519 | + public static function reset($hard = false, $reinstantiate = true, $reset_models = true) |
|
1520 | + { |
|
1521 | + $instance = self::instance(); |
|
1522 | + $instance->_cache_on = true; |
|
1523 | + // reset some "special" classes |
|
1524 | + EEH_Activation::reset(); |
|
1525 | + $hard = apply_filters( 'FHEE__EE_Registry__reset__hard', $hard); |
|
1526 | + $instance->CFG = EE_Config::reset($hard, $reinstantiate); |
|
1527 | + $instance->CART = null; |
|
1528 | + $instance->MRM = null; |
|
1529 | + $instance->AssetsRegistry = $instance->create('EventEspresso\core\services\assets\Registry'); |
|
1530 | + //messages reset |
|
1531 | + EED_Messages::reset(); |
|
1532 | + //handle of objects cached on LIB |
|
1533 | + foreach (array('LIB', 'modules') as $cache) { |
|
1534 | + foreach ($instance->{$cache} as $class_name => $class) { |
|
1535 | + if (self::_reset_and_unset_object($class, $reset_models)) { |
|
1536 | + unset($instance->{$cache}->{$class_name}); |
|
1537 | + } |
|
1538 | + } |
|
1539 | + } |
|
1540 | + return $instance; |
|
1541 | + } |
|
1542 | + |
|
1543 | + |
|
1544 | + |
|
1545 | + /** |
|
1546 | + * if passed object implements ResettableInterface, then call it's reset() method |
|
1547 | + * if passed object implements InterminableInterface, then return false, |
|
1548 | + * to indicate that it should NOT be cleared from the Registry cache |
|
1549 | + * |
|
1550 | + * @param $object |
|
1551 | + * @param bool $reset_models |
|
1552 | + * @return bool returns true if cached object should be unset |
|
1553 | + */ |
|
1554 | + private static function _reset_and_unset_object($object, $reset_models) |
|
1555 | + { |
|
1556 | + if (! is_object($object)) { |
|
1557 | + // don't unset anything that's not an object |
|
1558 | + return false; |
|
1559 | + } |
|
1560 | + if ($object instanceof EED_Module) { |
|
1561 | + $object::reset(); |
|
1562 | + // don't unset modules |
|
1563 | + return false; |
|
1564 | + } |
|
1565 | + if ($object instanceof ResettableInterface) { |
|
1566 | + if ($object instanceof EEM_Base) { |
|
1567 | + if ($reset_models) { |
|
1568 | + $object->reset(); |
|
1569 | + return true; |
|
1570 | + } |
|
1571 | + return false; |
|
1572 | + } |
|
1573 | + $object->reset(); |
|
1574 | + return true; |
|
1575 | + } |
|
1576 | + if (! $object instanceof InterminableInterface) { |
|
1577 | + return true; |
|
1578 | + } |
|
1579 | + return false; |
|
1580 | + } |
|
1581 | + |
|
1582 | + |
|
1583 | + |
|
1584 | + /** |
|
1585 | + * Gets all the custom post type models defined |
|
1586 | + * |
|
1587 | + * @return array keys are model "short names" (Eg "Event") and keys are classnames (eg "EEM_Event") |
|
1588 | + */ |
|
1589 | + public function cpt_models() |
|
1590 | + { |
|
1591 | + $cpt_models = array(); |
|
1592 | + foreach ($this->non_abstract_db_models as $short_name => $classname) { |
|
1593 | + if (is_subclass_of($classname, 'EEM_CPT_Base')) { |
|
1594 | + $cpt_models[$short_name] = $classname; |
|
1595 | + } |
|
1596 | + } |
|
1597 | + return $cpt_models; |
|
1598 | + } |
|
1599 | + |
|
1600 | + |
|
1601 | + |
|
1602 | + /** |
|
1603 | + * @return \EE_Config |
|
1604 | + */ |
|
1605 | + public static function CFG() |
|
1606 | + { |
|
1607 | + return self::instance()->CFG; |
|
1608 | + } |
|
1609 | 1609 | |
1610 | 1610 | |
1611 | 1611 | } |
@@ -33,115 +33,115 @@ |
||
33 | 33 | class CoreLoader implements LoaderDecoratorInterface |
34 | 34 | { |
35 | 35 | |
36 | - /** |
|
37 | - * @var EE_Registry|CoffeeShop $generator |
|
38 | - */ |
|
39 | - private $generator; |
|
40 | - |
|
41 | - |
|
42 | - |
|
43 | - /** |
|
44 | - * CoreLoader constructor. |
|
45 | - * |
|
46 | - * @param EE_Registry|CoffeeShop $generator |
|
47 | - * @throws InvalidArgumentException |
|
48 | - */ |
|
49 | - public function __construct($generator) |
|
50 | - { |
|
51 | - if(!($generator instanceof EE_Registry || $generator instanceof CoffeeShop)) { |
|
52 | - throw new InvalidArgumentException( |
|
53 | - esc_html__( |
|
54 | - 'The CoreLoader class must receive an instance of EE_Registry or the CoffeeShop DI container.', |
|
55 | - 'event_espresso' |
|
56 | - ) |
|
57 | - ); |
|
58 | - } |
|
59 | - $this->generator = $generator; |
|
60 | - } |
|
61 | - |
|
62 | - |
|
63 | - |
|
64 | - /** |
|
65 | - * Calls the appropriate loading method from the installed generator; |
|
66 | - * If EE_Registry is being used, then the additional parameters for the EE_Registry::create() method |
|
67 | - * can be added to the $arguments array and they will be extracted and passed to EE_Registry::create(), |
|
68 | - * but NOT to the class being instantiated. |
|
69 | - * This is done by adding the parameters to the $arguments array as follows: |
|
70 | - * array( |
|
71 | - * 'EE_Registry::create(from_db)' => true, // boolean value, default = false |
|
72 | - * 'EE_Registry::create(load_only)' => true, // boolean value, default = false |
|
73 | - * 'EE_Registry::create(addon)' => true, // boolean value, default = false |
|
74 | - * ) |
|
75 | - * |
|
76 | - * @param string $fqcn |
|
77 | - * @param array $arguments |
|
78 | - * @param bool $shared |
|
79 | - * @return mixed |
|
80 | - * @throws OutOfBoundsException |
|
81 | - * @throws ServiceExistsException |
|
82 | - * @throws InstantiationException |
|
83 | - * @throws InvalidIdentifierException |
|
84 | - * @throws InvalidDataTypeException |
|
85 | - * @throws InvalidClassException |
|
86 | - * @throws EE_Error |
|
87 | - * @throws ServiceNotFoundException |
|
88 | - * @throws ReflectionException |
|
89 | - */ |
|
90 | - public function load($fqcn, $arguments = array(), $shared = true) |
|
91 | - { |
|
92 | - $shared = filter_var($shared, FILTER_VALIDATE_BOOLEAN); |
|
93 | - if($this->generator instanceof EE_Registry) { |
|
94 | - // check if additional EE_Registry::create() arguments have been passed |
|
95 | - // from_db |
|
96 | - $from_db = isset($arguments['EE_Registry::create(from_db)']) |
|
97 | - ? filter_var($arguments['EE_Registry::create(from_db)'], FILTER_VALIDATE_BOOLEAN) |
|
98 | - : false; |
|
99 | - // load_only |
|
100 | - $load_only = isset($arguments['EE_Registry::create(load_only)']) |
|
101 | - ? filter_var($arguments['EE_Registry::create(load_only)'], FILTER_VALIDATE_BOOLEAN) |
|
102 | - : false; |
|
103 | - // addon |
|
104 | - $addon = isset($arguments['EE_Registry::create(addon)']) |
|
105 | - ? filter_var($arguments['EE_Registry::create(addon)'], FILTER_VALIDATE_BOOLEAN) |
|
106 | - : false; |
|
107 | - unset( |
|
108 | - $arguments['EE_Registry::create(from_db)'], |
|
109 | - $arguments['EE_Registry::create(load_only)'], |
|
110 | - $arguments['EE_Registry::create(addon)'] |
|
111 | - ); |
|
112 | - // addons need to be cached on EE_Registry |
|
113 | - $shared = $addon ? true : $shared; |
|
114 | - return $this->generator->create( |
|
115 | - $fqcn, |
|
116 | - $arguments, |
|
117 | - $shared, |
|
118 | - $from_db, |
|
119 | - $load_only, |
|
120 | - $addon |
|
121 | - ); |
|
122 | - } |
|
123 | - return $this->generator->brew( |
|
124 | - $fqcn, |
|
125 | - $arguments, |
|
126 | - $shared ? CoffeeMaker::BREW_SHARED : CoffeeMaker::BREW_NEW |
|
127 | - ); |
|
128 | - |
|
129 | - } |
|
130 | - |
|
131 | - |
|
132 | - |
|
133 | - /** |
|
134 | - * calls reset() on generator if method exists |
|
135 | - * |
|
136 | - * @throws EE_Error |
|
137 | - * @throws ReflectionException |
|
138 | - */ |
|
139 | - public function reset() |
|
140 | - { |
|
141 | - if ($this->generator instanceof ResettableInterface) { |
|
142 | - $this->generator->reset(); |
|
143 | - } |
|
144 | - } |
|
36 | + /** |
|
37 | + * @var EE_Registry|CoffeeShop $generator |
|
38 | + */ |
|
39 | + private $generator; |
|
40 | + |
|
41 | + |
|
42 | + |
|
43 | + /** |
|
44 | + * CoreLoader constructor. |
|
45 | + * |
|
46 | + * @param EE_Registry|CoffeeShop $generator |
|
47 | + * @throws InvalidArgumentException |
|
48 | + */ |
|
49 | + public function __construct($generator) |
|
50 | + { |
|
51 | + if(!($generator instanceof EE_Registry || $generator instanceof CoffeeShop)) { |
|
52 | + throw new InvalidArgumentException( |
|
53 | + esc_html__( |
|
54 | + 'The CoreLoader class must receive an instance of EE_Registry or the CoffeeShop DI container.', |
|
55 | + 'event_espresso' |
|
56 | + ) |
|
57 | + ); |
|
58 | + } |
|
59 | + $this->generator = $generator; |
|
60 | + } |
|
61 | + |
|
62 | + |
|
63 | + |
|
64 | + /** |
|
65 | + * Calls the appropriate loading method from the installed generator; |
|
66 | + * If EE_Registry is being used, then the additional parameters for the EE_Registry::create() method |
|
67 | + * can be added to the $arguments array and they will be extracted and passed to EE_Registry::create(), |
|
68 | + * but NOT to the class being instantiated. |
|
69 | + * This is done by adding the parameters to the $arguments array as follows: |
|
70 | + * array( |
|
71 | + * 'EE_Registry::create(from_db)' => true, // boolean value, default = false |
|
72 | + * 'EE_Registry::create(load_only)' => true, // boolean value, default = false |
|
73 | + * 'EE_Registry::create(addon)' => true, // boolean value, default = false |
|
74 | + * ) |
|
75 | + * |
|
76 | + * @param string $fqcn |
|
77 | + * @param array $arguments |
|
78 | + * @param bool $shared |
|
79 | + * @return mixed |
|
80 | + * @throws OutOfBoundsException |
|
81 | + * @throws ServiceExistsException |
|
82 | + * @throws InstantiationException |
|
83 | + * @throws InvalidIdentifierException |
|
84 | + * @throws InvalidDataTypeException |
|
85 | + * @throws InvalidClassException |
|
86 | + * @throws EE_Error |
|
87 | + * @throws ServiceNotFoundException |
|
88 | + * @throws ReflectionException |
|
89 | + */ |
|
90 | + public function load($fqcn, $arguments = array(), $shared = true) |
|
91 | + { |
|
92 | + $shared = filter_var($shared, FILTER_VALIDATE_BOOLEAN); |
|
93 | + if($this->generator instanceof EE_Registry) { |
|
94 | + // check if additional EE_Registry::create() arguments have been passed |
|
95 | + // from_db |
|
96 | + $from_db = isset($arguments['EE_Registry::create(from_db)']) |
|
97 | + ? filter_var($arguments['EE_Registry::create(from_db)'], FILTER_VALIDATE_BOOLEAN) |
|
98 | + : false; |
|
99 | + // load_only |
|
100 | + $load_only = isset($arguments['EE_Registry::create(load_only)']) |
|
101 | + ? filter_var($arguments['EE_Registry::create(load_only)'], FILTER_VALIDATE_BOOLEAN) |
|
102 | + : false; |
|
103 | + // addon |
|
104 | + $addon = isset($arguments['EE_Registry::create(addon)']) |
|
105 | + ? filter_var($arguments['EE_Registry::create(addon)'], FILTER_VALIDATE_BOOLEAN) |
|
106 | + : false; |
|
107 | + unset( |
|
108 | + $arguments['EE_Registry::create(from_db)'], |
|
109 | + $arguments['EE_Registry::create(load_only)'], |
|
110 | + $arguments['EE_Registry::create(addon)'] |
|
111 | + ); |
|
112 | + // addons need to be cached on EE_Registry |
|
113 | + $shared = $addon ? true : $shared; |
|
114 | + return $this->generator->create( |
|
115 | + $fqcn, |
|
116 | + $arguments, |
|
117 | + $shared, |
|
118 | + $from_db, |
|
119 | + $load_only, |
|
120 | + $addon |
|
121 | + ); |
|
122 | + } |
|
123 | + return $this->generator->brew( |
|
124 | + $fqcn, |
|
125 | + $arguments, |
|
126 | + $shared ? CoffeeMaker::BREW_SHARED : CoffeeMaker::BREW_NEW |
|
127 | + ); |
|
128 | + |
|
129 | + } |
|
130 | + |
|
131 | + |
|
132 | + |
|
133 | + /** |
|
134 | + * calls reset() on generator if method exists |
|
135 | + * |
|
136 | + * @throws EE_Error |
|
137 | + * @throws ReflectionException |
|
138 | + */ |
|
139 | + public function reset() |
|
140 | + { |
|
141 | + if ($this->generator instanceof ResettableInterface) { |
|
142 | + $this->generator->reset(); |
|
143 | + } |
|
144 | + } |
|
145 | 145 | |
146 | 146 | } |
147 | 147 | // End of file CoreLoader.php |
@@ -22,171 +22,171 @@ |
||
22 | 22 | class CachingLoader extends CachingLoaderDecorator |
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( |
|
46 | - LoaderDecoratorInterface $loader, |
|
47 | - CollectionInterface $cache, |
|
48 | - $identifier = '' |
|
49 | - ) { |
|
50 | - parent::__construct($loader); |
|
51 | - $this->cache = $cache; |
|
52 | - $this->setIdentifier($identifier); |
|
53 | - if ($this->identifier !== '') { |
|
54 | - // to only clear this cache, and assuming an identifier has been set, simply do the following: |
|
55 | - // do_action('AHEE__EventEspresso_core_services_loaders_CachingLoader__resetCache__IDENTIFIER'); |
|
56 | - // where "IDENTIFIER" = the string that was set during construction |
|
57 | - add_action( |
|
58 | - "AHEE__EventEspresso_core_services_loaders_CachingLoader__resetCache__{$identifier}", |
|
59 | - array($this, 'reset') |
|
60 | - ); |
|
61 | - } |
|
62 | - // to clear ALL caches, simply do the following: |
|
63 | - // do_action('AHEE__EventEspresso_core_services_loaders_CachingLoader__resetCache'); |
|
64 | - add_action( |
|
65 | - 'AHEE__EventEspresso_core_services_loaders_CachingLoader__resetCache', |
|
66 | - array($this, 'reset') |
|
67 | - ); |
|
68 | - } |
|
69 | - |
|
70 | - |
|
71 | - |
|
72 | - /** |
|
73 | - * @return string |
|
74 | - */ |
|
75 | - public function identifier() |
|
76 | - { |
|
77 | - return $this->identifier; |
|
78 | - } |
|
79 | - |
|
80 | - |
|
81 | - |
|
82 | - /** |
|
83 | - * @param string $identifier |
|
84 | - * @throws InvalidDataTypeException |
|
85 | - */ |
|
86 | - private function setIdentifier($identifier) |
|
87 | - { |
|
88 | - if (! is_string($identifier)) { |
|
89 | - throw new InvalidDataTypeException('$identifier', $identifier, 'string'); |
|
90 | - } |
|
91 | - $this->identifier = $identifier; |
|
92 | - } |
|
93 | - |
|
94 | - |
|
95 | - /** |
|
96 | - * @param string $fqcn |
|
97 | - * @param mixed $object |
|
98 | - * @return bool |
|
99 | - * @throws InvalidArgumentException |
|
100 | - */ |
|
101 | - public function share($fqcn, $object) |
|
102 | - { |
|
103 | - if ($object instanceof $fqcn) { |
|
104 | - return $this->cache->add($object, md5($fqcn)); |
|
105 | - } |
|
106 | - throw new InvalidArgumentException( |
|
107 | - sprintf( |
|
108 | - esc_html__( |
|
109 | - 'The supplied class name "%1$s" must match the class of the supplied object.', |
|
110 | - 'event_espresso' |
|
111 | - ), |
|
112 | - $fqcn |
|
113 | - ) |
|
114 | - ); |
|
115 | - } |
|
116 | - |
|
117 | - |
|
118 | - /** |
|
119 | - * @param string $fqcn |
|
120 | - * @param array $arguments |
|
121 | - * @param bool $shared |
|
122 | - * @return mixed |
|
123 | - */ |
|
124 | - public function load($fqcn, $arguments = array(), $shared = true) |
|
125 | - { |
|
126 | - $fqcn = ltrim($fqcn, '\\'); |
|
127 | - // caching can be turned off via the following code: |
|
128 | - // add_filter('FHEE__EventEspresso_core_services_loaders_CachingLoader__load__bypass_cache', '__return_true'); |
|
129 | - if( |
|
130 | - apply_filters( |
|
131 | - 'FHEE__EventEspresso_core_services_loaders_CachingLoader__load__bypass_cache', |
|
132 | - false, |
|
133 | - $this |
|
134 | - ) |
|
135 | - ){ |
|
136 | - // even though $shared might be true, caching could be bypassed for whatever reason, |
|
137 | - // so we don't want the core loader to cache anything, therefore caching is turned off |
|
138 | - return $this->loader->load($fqcn, $arguments, false); |
|
139 | - } |
|
140 | - $identifier = md5($fqcn . $this->getIdentifierForArgument($arguments)); |
|
141 | - if ($this->cache->has($identifier)) { |
|
142 | - return $this->cache->get($identifier); |
|
143 | - } |
|
144 | - $object = $this->loader->load($fqcn, $arguments, $shared); |
|
145 | - if ($object instanceof $fqcn) { |
|
146 | - $this->cache->add($object, $identifier); |
|
147 | - } |
|
148 | - return $object; |
|
149 | - } |
|
150 | - |
|
151 | - |
|
152 | - |
|
153 | - /** |
|
154 | - * empties cache and calls reset() on loader if method exists |
|
155 | - */ |
|
156 | - public function reset() |
|
157 | - { |
|
158 | - $this->cache->trashAndDetachAll(); |
|
159 | - $this->loader->reset(); |
|
160 | - } |
|
161 | - |
|
162 | - |
|
163 | - |
|
164 | - /** |
|
165 | - * build a string representation of a class' arguments |
|
166 | - * (mostly because Closures can't be serialized) |
|
167 | - * |
|
168 | - * @param array $arguments |
|
169 | - * @return string |
|
170 | - */ |
|
171 | - private function getIdentifierForArgument(array $arguments) |
|
172 | - { |
|
173 | - $identifier = ''; |
|
174 | - foreach ($arguments as $argument) { |
|
175 | - switch (true) { |
|
176 | - case is_object($argument) : |
|
177 | - case $argument instanceof Closure : |
|
178 | - $identifier .= spl_object_hash($argument); |
|
179 | - break; |
|
180 | - case is_array($argument) : |
|
181 | - $identifier .= $this->getIdentifierForArgument($argument); |
|
182 | - break; |
|
183 | - default : |
|
184 | - $identifier .= $argument; |
|
185 | - break; |
|
186 | - } |
|
187 | - } |
|
188 | - return $identifier; |
|
189 | - } |
|
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( |
|
46 | + LoaderDecoratorInterface $loader, |
|
47 | + CollectionInterface $cache, |
|
48 | + $identifier = '' |
|
49 | + ) { |
|
50 | + parent::__construct($loader); |
|
51 | + $this->cache = $cache; |
|
52 | + $this->setIdentifier($identifier); |
|
53 | + if ($this->identifier !== '') { |
|
54 | + // to only clear this cache, and assuming an identifier has been set, simply do the following: |
|
55 | + // do_action('AHEE__EventEspresso_core_services_loaders_CachingLoader__resetCache__IDENTIFIER'); |
|
56 | + // where "IDENTIFIER" = the string that was set during construction |
|
57 | + add_action( |
|
58 | + "AHEE__EventEspresso_core_services_loaders_CachingLoader__resetCache__{$identifier}", |
|
59 | + array($this, 'reset') |
|
60 | + ); |
|
61 | + } |
|
62 | + // to clear ALL caches, simply do the following: |
|
63 | + // do_action('AHEE__EventEspresso_core_services_loaders_CachingLoader__resetCache'); |
|
64 | + add_action( |
|
65 | + 'AHEE__EventEspresso_core_services_loaders_CachingLoader__resetCache', |
|
66 | + array($this, 'reset') |
|
67 | + ); |
|
68 | + } |
|
69 | + |
|
70 | + |
|
71 | + |
|
72 | + /** |
|
73 | + * @return string |
|
74 | + */ |
|
75 | + public function identifier() |
|
76 | + { |
|
77 | + return $this->identifier; |
|
78 | + } |
|
79 | + |
|
80 | + |
|
81 | + |
|
82 | + /** |
|
83 | + * @param string $identifier |
|
84 | + * @throws InvalidDataTypeException |
|
85 | + */ |
|
86 | + private function setIdentifier($identifier) |
|
87 | + { |
|
88 | + if (! is_string($identifier)) { |
|
89 | + throw new InvalidDataTypeException('$identifier', $identifier, 'string'); |
|
90 | + } |
|
91 | + $this->identifier = $identifier; |
|
92 | + } |
|
93 | + |
|
94 | + |
|
95 | + /** |
|
96 | + * @param string $fqcn |
|
97 | + * @param mixed $object |
|
98 | + * @return bool |
|
99 | + * @throws InvalidArgumentException |
|
100 | + */ |
|
101 | + public function share($fqcn, $object) |
|
102 | + { |
|
103 | + if ($object instanceof $fqcn) { |
|
104 | + return $this->cache->add($object, md5($fqcn)); |
|
105 | + } |
|
106 | + throw new InvalidArgumentException( |
|
107 | + sprintf( |
|
108 | + esc_html__( |
|
109 | + 'The supplied class name "%1$s" must match the class of the supplied object.', |
|
110 | + 'event_espresso' |
|
111 | + ), |
|
112 | + $fqcn |
|
113 | + ) |
|
114 | + ); |
|
115 | + } |
|
116 | + |
|
117 | + |
|
118 | + /** |
|
119 | + * @param string $fqcn |
|
120 | + * @param array $arguments |
|
121 | + * @param bool $shared |
|
122 | + * @return mixed |
|
123 | + */ |
|
124 | + public function load($fqcn, $arguments = array(), $shared = true) |
|
125 | + { |
|
126 | + $fqcn = ltrim($fqcn, '\\'); |
|
127 | + // caching can be turned off via the following code: |
|
128 | + // add_filter('FHEE__EventEspresso_core_services_loaders_CachingLoader__load__bypass_cache', '__return_true'); |
|
129 | + if( |
|
130 | + apply_filters( |
|
131 | + 'FHEE__EventEspresso_core_services_loaders_CachingLoader__load__bypass_cache', |
|
132 | + false, |
|
133 | + $this |
|
134 | + ) |
|
135 | + ){ |
|
136 | + // even though $shared might be true, caching could be bypassed for whatever reason, |
|
137 | + // so we don't want the core loader to cache anything, therefore caching is turned off |
|
138 | + return $this->loader->load($fqcn, $arguments, false); |
|
139 | + } |
|
140 | + $identifier = md5($fqcn . $this->getIdentifierForArgument($arguments)); |
|
141 | + if ($this->cache->has($identifier)) { |
|
142 | + return $this->cache->get($identifier); |
|
143 | + } |
|
144 | + $object = $this->loader->load($fqcn, $arguments, $shared); |
|
145 | + if ($object instanceof $fqcn) { |
|
146 | + $this->cache->add($object, $identifier); |
|
147 | + } |
|
148 | + return $object; |
|
149 | + } |
|
150 | + |
|
151 | + |
|
152 | + |
|
153 | + /** |
|
154 | + * empties cache and calls reset() on loader if method exists |
|
155 | + */ |
|
156 | + public function reset() |
|
157 | + { |
|
158 | + $this->cache->trashAndDetachAll(); |
|
159 | + $this->loader->reset(); |
|
160 | + } |
|
161 | + |
|
162 | + |
|
163 | + |
|
164 | + /** |
|
165 | + * build a string representation of a class' arguments |
|
166 | + * (mostly because Closures can't be serialized) |
|
167 | + * |
|
168 | + * @param array $arguments |
|
169 | + * @return string |
|
170 | + */ |
|
171 | + private function getIdentifierForArgument(array $arguments) |
|
172 | + { |
|
173 | + $identifier = ''; |
|
174 | + foreach ($arguments as $argument) { |
|
175 | + switch (true) { |
|
176 | + case is_object($argument) : |
|
177 | + case $argument instanceof Closure : |
|
178 | + $identifier .= spl_object_hash($argument); |
|
179 | + break; |
|
180 | + case is_array($argument) : |
|
181 | + $identifier .= $this->getIdentifierForArgument($argument); |
|
182 | + break; |
|
183 | + default : |
|
184 | + $identifier .= $argument; |
|
185 | + break; |
|
186 | + } |
|
187 | + } |
|
188 | + return $identifier; |
|
189 | + } |
|
190 | 190 | |
191 | 191 | |
192 | 192 | } |
@@ -85,7 +85,7 @@ discard block |
||
85 | 85 | */ |
86 | 86 | private function setIdentifier($identifier) |
87 | 87 | { |
88 | - if (! is_string($identifier)) { |
|
88 | + if ( ! is_string($identifier)) { |
|
89 | 89 | throw new InvalidDataTypeException('$identifier', $identifier, 'string'); |
90 | 90 | } |
91 | 91 | $this->identifier = $identifier; |
@@ -126,18 +126,18 @@ discard block |
||
126 | 126 | $fqcn = ltrim($fqcn, '\\'); |
127 | 127 | // caching can be turned off via the following code: |
128 | 128 | // add_filter('FHEE__EventEspresso_core_services_loaders_CachingLoader__load__bypass_cache', '__return_true'); |
129 | - if( |
|
129 | + if ( |
|
130 | 130 | apply_filters( |
131 | 131 | 'FHEE__EventEspresso_core_services_loaders_CachingLoader__load__bypass_cache', |
132 | 132 | false, |
133 | 133 | $this |
134 | 134 | ) |
135 | - ){ |
|
135 | + ) { |
|
136 | 136 | // even though $shared might be true, caching could be bypassed for whatever reason, |
137 | 137 | // so we don't want the core loader to cache anything, therefore caching is turned off |
138 | 138 | return $this->loader->load($fqcn, $arguments, false); |
139 | 139 | } |
140 | - $identifier = md5($fqcn . $this->getIdentifierForArgument($arguments)); |
|
140 | + $identifier = md5($fqcn.$this->getIdentifierForArgument($arguments)); |
|
141 | 141 | if ($this->cache->has($identifier)) { |
142 | 142 | return $this->cache->get($identifier); |
143 | 143 | } |