@@ -1,6 +1,5 @@ |
||
1 | 1 | <?php |
2 | 2 | use EventEspresso\core\domain\services\validation\EmailValidationException; |
3 | -use EventEspresso\core\entities\models\JsonModelSchema; |
|
4 | 3 | use EventEspresso\core\libraries\rest_api\CalculatedModelFields; |
5 | 4 | use EventEspresso\core\libraries\rest_api\controllers\model\Read as ModelRead; |
6 | 5 | use EventEspresso\core\libraries\rest_api\changes\ChangesInBase; |
@@ -22,1200 +22,1200 @@ |
||
22 | 22 | class EED_Core_Rest_Api extends \EED_Module |
23 | 23 | { |
24 | 24 | |
25 | - const ee_api_namespace = 'ee/v'; |
|
25 | + const ee_api_namespace = 'ee/v'; |
|
26 | 26 | |
27 | - const ee_api_namespace_for_regex = 'ee\/v([^/]*)\/'; |
|
28 | - |
|
29 | - const saved_routes_option_names = 'ee_core_routes'; |
|
30 | - |
|
31 | - /** |
|
32 | - * string used in _links response bodies to make them globally unique. |
|
33 | - * |
|
34 | - * @see http://v2.wp-api.org/extending/linking/ |
|
35 | - */ |
|
36 | - const ee_api_link_namespace = 'https://api.eventespresso.com/'; |
|
37 | - |
|
38 | - /** |
|
39 | - * @var CalculatedModelFields |
|
40 | - */ |
|
41 | - protected static $_field_calculator = null; |
|
42 | - |
|
43 | - |
|
44 | - |
|
45 | - /** |
|
46 | - * @return EED_Core_Rest_Api |
|
47 | - */ |
|
48 | - public static function instance() |
|
49 | - { |
|
50 | - self::$_field_calculator = new CalculatedModelFields(); |
|
51 | - return parent::get_instance(__CLASS__); |
|
52 | - } |
|
53 | - |
|
54 | - |
|
55 | - |
|
56 | - /** |
|
57 | - * set_hooks - for hooking into EE Core, other modules, etc |
|
58 | - * |
|
59 | - * @access public |
|
60 | - * @return void |
|
61 | - */ |
|
62 | - public static function set_hooks() |
|
63 | - { |
|
64 | - self::set_hooks_both(); |
|
65 | - } |
|
66 | - |
|
67 | - |
|
68 | - |
|
69 | - /** |
|
70 | - * set_hooks_admin - for hooking into EE Admin Core, other modules, etc |
|
71 | - * |
|
72 | - * @access public |
|
73 | - * @return void |
|
74 | - */ |
|
75 | - public static function set_hooks_admin() |
|
76 | - { |
|
77 | - self::set_hooks_both(); |
|
78 | - } |
|
79 | - |
|
80 | - |
|
81 | - |
|
82 | - public static function set_hooks_both() |
|
83 | - { |
|
84 | - add_action('rest_api_init', array('EED_Core_Rest_Api', 'register_routes'), 10); |
|
85 | - add_action('rest_api_init', array('EED_Core_Rest_Api', 'set_hooks_rest_api'), 5); |
|
86 | - add_filter('rest_route_data', array('EED_Core_Rest_Api', 'hide_old_endpoints'), 10, 2); |
|
87 | - add_filter('rest_index', |
|
88 | - array('EventEspresso\core\libraries\rest_api\controllers\model\Meta', 'filterEeMetadataIntoIndex')); |
|
89 | - EED_Core_Rest_Api::invalidate_cached_route_data_on_version_change(); |
|
90 | - } |
|
91 | - |
|
92 | - |
|
93 | - |
|
94 | - /** |
|
95 | - * sets up hooks which only need to be included as part of REST API requests; |
|
96 | - * other requests like to the frontend or admin etc don't need them |
|
97 | - */ |
|
98 | - public static function set_hooks_rest_api() |
|
99 | - { |
|
100 | - //set hooks which account for changes made to the API |
|
101 | - EED_Core_Rest_Api::_set_hooks_for_changes(); |
|
102 | - } |
|
103 | - |
|
104 | - |
|
105 | - |
|
106 | - /** |
|
107 | - * public wrapper of _set_hooks_for_changes. |
|
108 | - * Loads all the hooks which make requests to old versions of the API |
|
109 | - * appear the same as they always did |
|
110 | - */ |
|
111 | - public static function set_hooks_for_changes() |
|
112 | - { |
|
113 | - self::_set_hooks_for_changes(); |
|
114 | - } |
|
115 | - |
|
116 | - |
|
117 | - |
|
118 | - /** |
|
119 | - * Loads all the hooks which make requests to old versions of the API |
|
120 | - * appear the same as they always did |
|
121 | - */ |
|
122 | - protected static function _set_hooks_for_changes() |
|
123 | - { |
|
124 | - $folder_contents = EEH_File::get_contents_of_folders(array(EE_LIBRARIES . 'rest_api' . DS . 'changes'), false); |
|
125 | - foreach ($folder_contents as $classname_in_namespace => $filepath) { |
|
126 | - //ignore the base parent class |
|
127 | - //and legacy named classes |
|
128 | - if ($classname_in_namespace === 'ChangesInBase' |
|
129 | - || strpos($classname_in_namespace, 'Changes_In_') === 0 |
|
130 | - ) { |
|
131 | - continue; |
|
132 | - } |
|
133 | - $full_classname = 'EventEspresso\core\libraries\rest_api\changes\\' . $classname_in_namespace; |
|
134 | - if (class_exists($full_classname)) { |
|
135 | - $instance_of_class = new $full_classname; |
|
136 | - if ($instance_of_class instanceof ChangesInBase) { |
|
137 | - $instance_of_class->setHooks(); |
|
138 | - } |
|
139 | - } |
|
140 | - } |
|
141 | - } |
|
142 | - |
|
143 | - |
|
144 | - |
|
145 | - /** |
|
146 | - * Filters the WP routes to add our EE-related ones. This takes a bit of time |
|
147 | - * so we actually prefer to only do it when an EE plugin is activated or upgraded |
|
148 | - */ |
|
149 | - public static function register_routes() |
|
150 | - { |
|
151 | - foreach (EED_Core_Rest_Api::get_ee_route_data() as $namespace => $relative_routes) { |
|
152 | - foreach ($relative_routes as $relative_route => $data_for_multiple_endpoints) { |
|
153 | - /** |
|
154 | - * @var array $data_for_multiple_endpoints numerically indexed array |
|
155 | - * but can also contain route options like { |
|
156 | - * @type array $schema { |
|
157 | - * @type callable $schema_callback |
|
158 | - * @type array $callback_args arguments that will be passed to the callback, after the |
|
159 | - * WP_REST_Request of course |
|
160 | - * } |
|
161 | - * } |
|
162 | - */ |
|
163 | - //when registering routes, register all the endpoints' data at the same time |
|
164 | - $multiple_endpoint_args = array(); |
|
165 | - foreach ($data_for_multiple_endpoints as $endpoint_key => $data_for_single_endpoint) { |
|
166 | - /** |
|
167 | - * @var array $data_for_single_endpoint { |
|
168 | - * @type callable $callback |
|
169 | - * @type string methods |
|
170 | - * @type array args |
|
171 | - * @type array _links |
|
172 | - * @type array $callback_args arguments that will be passed to the callback, after the |
|
173 | - * WP_REST_Request of course |
|
174 | - * } |
|
175 | - */ |
|
176 | - //skip route options |
|
177 | - if (! is_numeric($endpoint_key)) { |
|
178 | - continue; |
|
179 | - } |
|
180 | - if (! isset($data_for_single_endpoint['callback'], $data_for_single_endpoint['methods'])) { |
|
181 | - throw new EE_Error( |
|
182 | - esc_html__( |
|
183 | - // @codingStandardsIgnoreStart |
|
184 | - 'Endpoint configuration data needs to have entries "callback" (callable) and "methods" (comma-separated list of accepts HTTP methods).', |
|
185 | - // @codingStandardsIgnoreEnd |
|
186 | - 'event_espresso') |
|
187 | - ); |
|
188 | - } |
|
189 | - $callback = $data_for_single_endpoint['callback']; |
|
190 | - $single_endpoint_args = array( |
|
191 | - 'methods' => $data_for_single_endpoint['methods'], |
|
192 | - 'args' => isset($data_for_single_endpoint['args']) ? $data_for_single_endpoint['args'] |
|
193 | - : array(), |
|
194 | - ); |
|
195 | - if (isset($data_for_single_endpoint['_links'])) { |
|
196 | - $single_endpoint_args['_links'] = $data_for_single_endpoint['_links']; |
|
197 | - } |
|
198 | - if (isset($data_for_single_endpoint['callback_args'])) { |
|
199 | - $callback_args = $data_for_single_endpoint['callback_args']; |
|
200 | - $single_endpoint_args['callback'] = function (\WP_REST_Request $request) use ( |
|
201 | - $callback, |
|
202 | - $callback_args |
|
203 | - ) { |
|
204 | - array_unshift($callback_args, $request); |
|
205 | - return call_user_func_array( |
|
206 | - $callback, |
|
207 | - $callback_args |
|
208 | - ); |
|
209 | - }; |
|
210 | - } else { |
|
211 | - $single_endpoint_args['callback'] = $data_for_single_endpoint['callback']; |
|
212 | - } |
|
213 | - $multiple_endpoint_args[] = $single_endpoint_args; |
|
214 | - } |
|
215 | - if (isset($data_for_multiple_endpoints['schema'])) { |
|
216 | - $schema_route_data = $data_for_multiple_endpoints['schema']; |
|
217 | - $schema_callback = $schema_route_data['schema_callback']; |
|
218 | - $callback_args = $schema_route_data['callback_args']; |
|
219 | - $multiple_endpoint_args['schema'] = function () use ($schema_callback, $callback_args) { |
|
220 | - return call_user_func_array( |
|
221 | - $schema_callback, |
|
222 | - $callback_args |
|
223 | - ); |
|
224 | - }; |
|
225 | - } |
|
226 | - register_rest_route( |
|
227 | - $namespace, |
|
228 | - $relative_route, |
|
229 | - $multiple_endpoint_args |
|
230 | - ); |
|
231 | - } |
|
232 | - } |
|
233 | - } |
|
234 | - |
|
235 | - |
|
236 | - |
|
237 | - /** |
|
238 | - * Checks if there was a version change or something that merits invalidating the cached |
|
239 | - * route data. If so, invalidates the cached route data so that it gets refreshed |
|
240 | - * next time the WP API is used |
|
241 | - */ |
|
242 | - public static function invalidate_cached_route_data_on_version_change() |
|
243 | - { |
|
244 | - if (EE_System::instance()->detect_req_type() != EE_System::req_type_normal) { |
|
245 | - EED_Core_Rest_Api::invalidate_cached_route_data(); |
|
246 | - } |
|
247 | - foreach (EE_Registry::instance()->addons as $addon) { |
|
248 | - if ($addon instanceof EE_Addon && $addon->detect_req_type() != EE_System::req_type_normal) { |
|
249 | - EED_Core_Rest_Api::invalidate_cached_route_data(); |
|
250 | - } |
|
251 | - } |
|
252 | - } |
|
253 | - |
|
254 | - |
|
255 | - |
|
256 | - /** |
|
257 | - * Removes the cached route data so it will get refreshed next time the WP API is used |
|
258 | - */ |
|
259 | - public static function invalidate_cached_route_data() |
|
260 | - { |
|
261 | - //delete the saved EE REST API routes |
|
262 | - foreach (EED_Core_Rest_Api::versions_served() as $version => $hidden) { |
|
263 | - delete_option(EED_Core_Rest_Api::saved_routes_option_names . $version); |
|
264 | - } |
|
265 | - } |
|
266 | - |
|
267 | - |
|
268 | - |
|
269 | - /** |
|
270 | - * Gets the EE route data |
|
271 | - * |
|
272 | - * @return array top-level key is the namespace, next-level key is the route and its value is array{ |
|
273 | - * @type string|array $callback |
|
274 | - * @type string $methods |
|
275 | - * @type boolean $hidden_endpoint |
|
276 | - * } |
|
277 | - */ |
|
278 | - public static function get_ee_route_data() |
|
279 | - { |
|
280 | - $ee_routes = array(); |
|
281 | - foreach (self::versions_served() as $version => $hidden_endpoints) { |
|
282 | - $ee_routes[self::ee_api_namespace . $version] = self::_get_ee_route_data_for_version( |
|
283 | - $version, |
|
284 | - $hidden_endpoints |
|
285 | - ); |
|
286 | - } |
|
287 | - return $ee_routes; |
|
288 | - } |
|
289 | - |
|
290 | - |
|
291 | - |
|
292 | - /** |
|
293 | - * Gets the EE route data from the wp options if it exists already, |
|
294 | - * otherwise re-generates it and saves it to the option |
|
295 | - * |
|
296 | - * @param string $version |
|
297 | - * @param boolean $hidden_endpoints |
|
298 | - * @return array |
|
299 | - */ |
|
300 | - protected static function _get_ee_route_data_for_version($version, $hidden_endpoints = false) |
|
301 | - { |
|
302 | - $ee_routes = get_option(self::saved_routes_option_names . $version, null); |
|
303 | - if (! $ee_routes || (defined('EE_REST_API_DEBUG_MODE') && EE_REST_API_DEBUG_MODE)) { |
|
304 | - $ee_routes = self::_save_ee_route_data_for_version($version, $hidden_endpoints); |
|
305 | - } |
|
306 | - return $ee_routes; |
|
307 | - } |
|
308 | - |
|
309 | - |
|
310 | - |
|
311 | - /** |
|
312 | - * Saves the EE REST API route data to a wp option and returns it |
|
313 | - * |
|
314 | - * @param string $version |
|
315 | - * @param boolean $hidden_endpoints |
|
316 | - * @return mixed|null |
|
317 | - */ |
|
318 | - protected static function _save_ee_route_data_for_version($version, $hidden_endpoints = false) |
|
319 | - { |
|
320 | - $instance = self::instance(); |
|
321 | - $routes = apply_filters( |
|
322 | - 'EED_Core_Rest_Api__save_ee_route_data_for_version__routes', |
|
323 | - array_replace_recursive( |
|
324 | - $instance->_get_config_route_data_for_version($version, $hidden_endpoints), |
|
325 | - $instance->_get_meta_route_data_for_version($version, $hidden_endpoints), |
|
326 | - $instance->_get_model_route_data_for_version($version, $hidden_endpoints), |
|
327 | - $instance->_get_rpc_route_data_for_version($version, $hidden_endpoints) |
|
328 | - ) |
|
329 | - ); |
|
330 | - $option_name = self::saved_routes_option_names . $version; |
|
331 | - if (get_option($option_name)) { |
|
332 | - update_option($option_name, $routes, true); |
|
333 | - } else { |
|
334 | - add_option($option_name, $routes, null, 'no'); |
|
335 | - } |
|
336 | - return $routes; |
|
337 | - } |
|
338 | - |
|
339 | - |
|
340 | - |
|
341 | - /** |
|
342 | - * Calculates all the EE routes and saves it to a WordPress option so we don't |
|
343 | - * need to calculate it on every request |
|
344 | - * |
|
345 | - * @deprecated since version 4.9.1 |
|
346 | - * @return void |
|
347 | - */ |
|
348 | - public static function save_ee_routes() |
|
349 | - { |
|
350 | - if (EE_Maintenance_Mode::instance()->models_can_query()) { |
|
351 | - $instance = self::instance(); |
|
352 | - $routes = apply_filters( |
|
353 | - 'EED_Core_Rest_Api__save_ee_routes__routes', |
|
354 | - array_replace_recursive( |
|
355 | - $instance->_register_config_routes(), |
|
356 | - $instance->_register_meta_routes(), |
|
357 | - $instance->_register_model_routes(), |
|
358 | - $instance->_register_rpc_routes() |
|
359 | - ) |
|
360 | - ); |
|
361 | - update_option(self::saved_routes_option_names, $routes, true); |
|
362 | - } |
|
363 | - } |
|
364 | - |
|
365 | - |
|
366 | - |
|
367 | - /** |
|
368 | - * Gets all the route information relating to EE models |
|
369 | - * |
|
370 | - * @return array @see get_ee_route_data |
|
371 | - * @deprecated since version 4.9.1 |
|
372 | - */ |
|
373 | - protected function _register_model_routes() |
|
374 | - { |
|
375 | - $model_routes = array(); |
|
376 | - foreach (self::versions_served() as $version => $hidden_endpoint) { |
|
377 | - $model_routes[EED_Core_Rest_Api::ee_api_namespace |
|
378 | - . $version] = $this->_get_config_route_data_for_version($version, $hidden_endpoint); |
|
379 | - } |
|
380 | - return $model_routes; |
|
381 | - } |
|
382 | - |
|
383 | - |
|
384 | - |
|
385 | - /** |
|
386 | - * Decides whether or not to add write endpoints for this model. |
|
387 | - * |
|
388 | - * Currently, this defaults to exclude all global tables and models |
|
389 | - * which would allow inserting WP core data (we don't want to duplicate |
|
390 | - * what WP API does, as it's unnecessary, extra work, and potentially extra bugs) |
|
391 | - * @param EEM_Base $model |
|
392 | - * @return bool |
|
393 | - */ |
|
394 | - public static function should_have_write_endpoints(EEM_Base $model) |
|
395 | - { |
|
396 | - if ($model->is_wp_core_model()){ |
|
397 | - return false; |
|
398 | - } |
|
399 | - foreach($model->get_tables() as $table){ |
|
400 | - if( $table->is_global()){ |
|
401 | - return false; |
|
402 | - } |
|
403 | - } |
|
404 | - return true; |
|
405 | - } |
|
406 | - |
|
407 | - |
|
408 | - |
|
409 | - /** |
|
410 | - * Gets the names of all models which should have plural routes (eg `ee/v4.8.36/events`) |
|
411 | - * in this versioned namespace of EE4 |
|
412 | - * @param $version |
|
413 | - * @return array keys are model names (eg 'Event') and values ar etheir classnames (eg 'EEM_Event') |
|
414 | - */ |
|
415 | - public static function model_names_with_plural_routes($version){ |
|
416 | - $model_version_info = new ModelVersionInfo($version); |
|
417 | - $models_to_register = $model_version_info->modelsForRequestedVersion(); |
|
418 | - //let's not bother having endpoints for extra metas |
|
419 | - unset($models_to_register['Extra_Meta']); |
|
420 | - unset($models_to_register['Extra_Join']); |
|
421 | - unset($models_to_register['Post_Meta']); |
|
422 | - return apply_filters( |
|
423 | - 'FHEE__EED_Core_REST_API___register_model_routes', |
|
424 | - $models_to_register |
|
425 | - ); |
|
426 | - } |
|
427 | - |
|
428 | - |
|
429 | - |
|
430 | - /** |
|
431 | - * Gets the route data for EE models in the specified version |
|
432 | - * |
|
433 | - * @param string $version |
|
434 | - * @param boolean $hidden_endpoint |
|
435 | - * @return array |
|
436 | - */ |
|
437 | - protected function _get_model_route_data_for_version($version, $hidden_endpoint = false) |
|
438 | - { |
|
439 | - $model_routes = array(); |
|
440 | - $model_version_info = new ModelVersionInfo($version); |
|
441 | - foreach ($this->model_names_with_plural_routes($version) as $model_name => $model_classname) { |
|
442 | - $model = \EE_Registry::instance()->load_model($model_name); |
|
443 | - //if this isn't a valid model then let's skip iterate to the next item in the loop. |
|
444 | - if (! $model instanceof EEM_Base) { |
|
445 | - continue; |
|
446 | - } |
|
447 | - //yes we could just register one route for ALL models, but then they wouldn't show up in the index |
|
448 | - $plural_model_route = EED_Core_Rest_Api::get_collection_route($model_name); |
|
449 | - $singular_model_route = EED_Core_Rest_Api::get_entity_route($model_name, '(?P<id>[^\/]+)'); |
|
450 | - $model_routes[$plural_model_route] = array( |
|
451 | - array( |
|
452 | - 'callback' => array( |
|
453 | - 'EventEspresso\core\libraries\rest_api\controllers\model\Read', |
|
454 | - 'handleRequestGetAll', |
|
455 | - ), |
|
456 | - 'callback_args' => array($version, $model_name), |
|
457 | - 'methods' => WP_REST_Server::READABLE, |
|
458 | - 'hidden_endpoint' => $hidden_endpoint, |
|
459 | - 'args' => $this->_get_read_query_params($model, $version), |
|
460 | - '_links' => array( |
|
461 | - 'self' => rest_url(EED_Core_Rest_Api::ee_api_namespace . $version . $singular_model_route), |
|
462 | - ), |
|
463 | - ), |
|
464 | - 'schema' => array( |
|
465 | - 'schema_callback' => array( |
|
466 | - 'EventEspresso\core\libraries\rest_api\controllers\model\Read', |
|
467 | - 'handleSchemaRequest', |
|
468 | - ), |
|
469 | - 'callback_args' => array($version, $model_name), |
|
470 | - ), |
|
471 | - ); |
|
472 | - $model_routes[$singular_model_route] = array( |
|
473 | - array( |
|
474 | - 'callback' => array( |
|
475 | - 'EventEspresso\core\libraries\rest_api\controllers\model\Read', |
|
476 | - 'handleRequestGetOne', |
|
477 | - ), |
|
478 | - 'callback_args' => array($version, $model_name), |
|
479 | - 'methods' => WP_REST_Server::READABLE, |
|
480 | - 'hidden_endpoint' => $hidden_endpoint, |
|
481 | - 'args' => $this->_get_response_selection_query_params($model, $version), |
|
482 | - ), |
|
483 | - ); |
|
484 | - if( apply_filters( |
|
485 | - 'FHEE__EED_Core_Rest_Api___get_model_route_data_for_version__add_write_endpoints', |
|
486 | - $this->should_have_write_endpoints($model), |
|
487 | - $model |
|
488 | - )){ |
|
489 | - $model_routes[$plural_model_route][] = array( |
|
490 | - 'callback' => array( |
|
491 | - 'EventEspresso\core\libraries\rest_api\controllers\model\Write', |
|
492 | - 'handleRequestInsert', |
|
493 | - ), |
|
494 | - 'callback_args' => array($version, $model_name), |
|
495 | - 'methods' => WP_REST_Server::CREATABLE, |
|
496 | - 'hidden_endpoint' => $hidden_endpoint, |
|
497 | - 'args' => $this->_get_write_params($model_name, $model_version_info, true), |
|
498 | - ); |
|
499 | - $model_routes[$singular_model_route] = array_merge( |
|
500 | - $model_routes[$singular_model_route], |
|
501 | - array( |
|
502 | - array( |
|
503 | - 'callback' => array( |
|
504 | - 'EventEspresso\core\libraries\rest_api\controllers\model\Write', |
|
505 | - 'handleRequestUpdate', |
|
506 | - ), |
|
507 | - 'callback_args' => array($version, $model_name), |
|
508 | - 'methods' => WP_REST_Server::EDITABLE, |
|
509 | - 'hidden_endpoint' => $hidden_endpoint, |
|
510 | - 'args' => $this->_get_write_params($model_name, $model_version_info, false), |
|
511 | - ), |
|
512 | - array( |
|
513 | - 'callback' => array( |
|
514 | - 'EventEspresso\core\libraries\rest_api\controllers\model\Write', |
|
515 | - 'handleRequestDelete', |
|
516 | - ), |
|
517 | - 'callback_args' => array($version, $model_name), |
|
518 | - 'methods' => WP_REST_Server::DELETABLE, |
|
519 | - 'hidden_endpoint' => $hidden_endpoint, |
|
520 | - 'args' => $this->_get_delete_query_params($model, $version), |
|
521 | - ) |
|
522 | - ) |
|
523 | - ); |
|
524 | - } |
|
525 | - foreach ($model->relation_settings() as $relation_name => $relation_obj) { |
|
526 | - |
|
527 | - $related_route = EED_Core_Rest_Api::get_relation_route_via( |
|
528 | - $model_name, |
|
529 | - '(?P<id>[^\/]+)', |
|
530 | - $relation_obj |
|
531 | - ); |
|
532 | - $endpoints = array( |
|
533 | - array( |
|
534 | - 'callback' => array( |
|
535 | - 'EventEspresso\core\libraries\rest_api\controllers\model\Read', |
|
536 | - 'handleRequestGetRelated', |
|
537 | - ), |
|
538 | - 'callback_args' => array($version, $model_name, $relation_name), |
|
539 | - 'methods' => WP_REST_Server::READABLE, |
|
540 | - 'hidden_endpoint' => $hidden_endpoint, |
|
541 | - 'args' => $this->_get_read_query_params($relation_obj->get_other_model(), $version), |
|
542 | - ), |
|
543 | - ); |
|
544 | - $model_routes[$related_route] = $endpoints; |
|
545 | - } |
|
546 | - } |
|
547 | - return $model_routes; |
|
548 | - } |
|
549 | - |
|
550 | - |
|
551 | - |
|
552 | - /** |
|
553 | - * Gets the relative URI to a model's REST API plural route, after the EE4 versioned namespace, |
|
554 | - * excluding the preceding slash. |
|
555 | - * Eg you pass get_plural_route_to('Event') = 'events' |
|
556 | - * @param string $model_name eg Event or Venue |
|
557 | - * @return string |
|
558 | - */ |
|
559 | - public static function get_collection_route($model_name) |
|
560 | - { |
|
561 | - return EEH_Inflector::pluralize_and_lower($model_name); |
|
562 | - } |
|
563 | - |
|
564 | - |
|
565 | - |
|
566 | - /** |
|
567 | - * Gets the relative URI to a model's REST API singular route, after the EE4 versioned namespace, |
|
568 | - * excluding the preceding slash. |
|
569 | - * Eg you pass get_plural_route_to('Event', 12) = 'events/12' |
|
570 | - * @param string $model_name eg Event or Venue |
|
571 | - * @param string $id |
|
572 | - * @return string |
|
573 | - */ |
|
574 | - public static function get_entity_route($model_name, $id) |
|
575 | - { |
|
576 | - return EEH_Inflector::pluralize_and_lower($model_name) . '/' . $id; |
|
577 | - } |
|
578 | - |
|
579 | - |
|
580 | - /** |
|
581 | - * Gets the relative URI to a model's REST API singular route, after the EE4 versioned namespace, |
|
582 | - * excluding the preceding slash. |
|
583 | - * Eg you pass get_plural_route_to('Event', 12) = 'events/12' |
|
584 | - * |
|
585 | - * @param string $model_name eg Event or Venue |
|
586 | - * @param string $id |
|
587 | - * @param EE_Model_Relation_Base $relation_obj |
|
588 | - * @return string |
|
589 | - */ |
|
590 | - public static function get_relation_route_via($model_name, $id, $relation_obj) |
|
591 | - { |
|
592 | - $related_model_name_endpoint_part = ModelRead::getRelatedEntityName( |
|
593 | - $relation_obj->get_other_model()->get_this_model_name(), |
|
594 | - $relation_obj |
|
595 | - ); |
|
596 | - return EED_Core_Rest_Api::get_entity_route($model_name, $id) . '/' . $related_model_name_endpoint_part; |
|
597 | - } |
|
598 | - |
|
599 | - |
|
600 | - |
|
601 | - /** |
|
602 | - * Adds onto the $relative_route the EE4 REST API versioned namespace. |
|
603 | - * Eg if given '4.8.36' and 'events', will return 'ee/v4.8.36/events' |
|
604 | - * @param string $relative_route |
|
605 | - * @param string $version |
|
606 | - * @return string |
|
607 | - */ |
|
608 | - public static function get_versioned_route_to($relative_route, $version = '4.8.36'){ |
|
609 | - return '/' . EED_Core_Rest_Api::ee_api_namespace . $version . '/' . $relative_route; |
|
610 | - } |
|
611 | - |
|
612 | - |
|
613 | - |
|
614 | - /** |
|
615 | - * Adds all the RPC-style routes (remote procedure call-like routes, ie |
|
616 | - * routes that don't conform to the traditional REST CRUD-style). |
|
617 | - * |
|
618 | - * @deprecated since 4.9.1 |
|
619 | - */ |
|
620 | - protected function _register_rpc_routes() |
|
621 | - { |
|
622 | - $routes = array(); |
|
623 | - foreach (self::versions_served() as $version => $hidden_endpoint) { |
|
624 | - $routes[self::ee_api_namespace . $version] = $this->_get_rpc_route_data_for_version( |
|
625 | - $version, |
|
626 | - $hidden_endpoint |
|
627 | - ); |
|
628 | - } |
|
629 | - return $routes; |
|
630 | - } |
|
631 | - |
|
632 | - |
|
633 | - |
|
634 | - /** |
|
635 | - * @param string $version |
|
636 | - * @param boolean $hidden_endpoint |
|
637 | - * @return array |
|
638 | - */ |
|
639 | - protected function _get_rpc_route_data_for_version($version, $hidden_endpoint = false) |
|
640 | - { |
|
641 | - $this_versions_routes = array(); |
|
642 | - //checkin endpoint |
|
643 | - $this_versions_routes['registrations/(?P<REG_ID>\d+)/toggle_checkin_for_datetime/(?P<DTT_ID>\d+)'] = array( |
|
644 | - array( |
|
645 | - 'callback' => array( |
|
646 | - 'EventEspresso\core\libraries\rest_api\controllers\rpc\Checkin', |
|
647 | - 'handleRequestToggleCheckin', |
|
648 | - ), |
|
649 | - 'methods' => WP_REST_Server::CREATABLE, |
|
650 | - 'hidden_endpoint' => $hidden_endpoint, |
|
651 | - 'args' => array( |
|
652 | - 'force' => array( |
|
653 | - 'required' => false, |
|
654 | - 'default' => false, |
|
655 | - 'description' => __( |
|
656 | - // @codingStandardsIgnoreStart |
|
657 | - 'Whether to force toggle checkin, or to verify the registration status and allowed ticket uses', |
|
658 | - // @codingStandardsIgnoreEnd |
|
659 | - 'event_espresso' |
|
660 | - ), |
|
661 | - ), |
|
662 | - ), |
|
663 | - 'callback_args' => array($version), |
|
664 | - ), |
|
665 | - ); |
|
666 | - return apply_filters( |
|
667 | - 'FHEE__EED_Core_Rest_Api___register_rpc_routes__this_versions_routes', |
|
668 | - $this_versions_routes, |
|
669 | - $version, |
|
670 | - $hidden_endpoint |
|
671 | - ); |
|
672 | - } |
|
673 | - |
|
674 | - |
|
675 | - |
|
676 | - /** |
|
677 | - * Gets the query params that can be used when request one or many |
|
678 | - * |
|
679 | - * @param EEM_Base $model |
|
680 | - * @param string $version |
|
681 | - * @return array |
|
682 | - */ |
|
683 | - protected function _get_response_selection_query_params(\EEM_Base $model, $version) |
|
684 | - { |
|
685 | - return apply_filters( |
|
686 | - 'FHEE__EED_Core_Rest_Api___get_response_selection_query_params', |
|
687 | - array( |
|
688 | - 'include' => array( |
|
689 | - 'required' => false, |
|
690 | - 'default' => '*', |
|
691 | - 'type' => 'string', |
|
692 | - ), |
|
693 | - 'calculate' => array( |
|
694 | - 'required' => false, |
|
695 | - 'default' => '', |
|
696 | - 'enum' => self::$_field_calculator->retrieveCalculatedFieldsForModel($model), |
|
697 | - 'type' => 'string', |
|
698 | - //because we accept a CSV'd list of the enumerated strings, WP core validation and sanitization |
|
699 | - //freaks out. We'll just validate this argument while handling the request |
|
700 | - 'validate_callback' => null, |
|
701 | - 'sanitize_callback' => null, |
|
702 | - ), |
|
703 | - ), |
|
704 | - $model, |
|
705 | - $version |
|
706 | - ); |
|
707 | - } |
|
708 | - |
|
709 | - |
|
710 | - |
|
711 | - /** |
|
712 | - * Gets the parameters acceptable for delete requests |
|
713 | - * |
|
714 | - * @param \EEM_Base $model |
|
715 | - * @param string $version |
|
716 | - * @return array |
|
717 | - */ |
|
718 | - protected function _get_delete_query_params(\EEM_Base $model, $version) |
|
719 | - { |
|
720 | - $params_for_delete = array( |
|
721 | - 'allow_blocking' => array( |
|
722 | - 'required' => false, |
|
723 | - 'default' => true, |
|
724 | - 'type' => 'boolean', |
|
725 | - ), |
|
726 | - ); |
|
727 | - $params_for_delete['force'] = array( |
|
728 | - 'required' => false, |
|
729 | - 'default' => false, |
|
730 | - 'type' => 'boolean', |
|
731 | - ); |
|
732 | - return apply_filters( |
|
733 | - 'FHEE__EED_Core_Rest_Api___get_delete_query_params', |
|
734 | - $params_for_delete, |
|
735 | - $model, |
|
736 | - $version |
|
737 | - ); |
|
738 | - } |
|
739 | - |
|
740 | - |
|
741 | - |
|
742 | - /** |
|
743 | - * Gets info about reading query params that are acceptable |
|
744 | - * |
|
745 | - * @param \EEM_Base $model eg 'Event' or 'Venue' |
|
746 | - * @param string $version |
|
747 | - * @return array describing the args acceptable when querying this model |
|
748 | - * @throws \EE_Error |
|
749 | - */ |
|
750 | - protected function _get_read_query_params(\EEM_Base $model, $version) |
|
751 | - { |
|
752 | - $default_orderby = array(); |
|
753 | - foreach ($model->get_combined_primary_key_fields() as $key_field) { |
|
754 | - $default_orderby[$key_field->get_name()] = 'ASC'; |
|
755 | - } |
|
756 | - return array_merge( |
|
757 | - $this->_get_response_selection_query_params($model, $version), |
|
758 | - array( |
|
759 | - 'where' => array( |
|
760 | - 'required' => false, |
|
761 | - 'default' => array(), |
|
762 | - 'type' => 'object', |
|
763 | - ), |
|
764 | - 'limit' => array( |
|
765 | - 'required' => false, |
|
766 | - 'default' => EED_Core_Rest_Api::get_default_query_limit(), |
|
767 | - 'type' => array( |
|
768 | - 'object', |
|
769 | - 'string', |
|
770 | - 'integer', |
|
771 | - ), |
|
772 | - ), |
|
773 | - 'order_by' => array( |
|
774 | - 'required' => false, |
|
775 | - 'default' => $default_orderby, |
|
776 | - 'type' => array( |
|
777 | - 'object', |
|
778 | - 'string', |
|
779 | - ), |
|
780 | - ), |
|
781 | - 'group_by' => array( |
|
782 | - 'required' => false, |
|
783 | - 'default' => null, |
|
784 | - 'type' => array( |
|
785 | - 'object', |
|
786 | - 'string', |
|
787 | - ), |
|
788 | - ), |
|
789 | - 'having' => array( |
|
790 | - 'required' => false, |
|
791 | - 'default' => null, |
|
792 | - 'type' => 'object', |
|
793 | - ), |
|
794 | - 'caps' => array( |
|
795 | - 'required' => false, |
|
796 | - 'default' => EEM_Base::caps_read, |
|
797 | - 'type' => 'string', |
|
798 | - ), |
|
799 | - ) |
|
800 | - ); |
|
801 | - } |
|
802 | - |
|
803 | - |
|
804 | - |
|
805 | - /** |
|
806 | - * Gets parameter information for a model regarding writing data |
|
807 | - * |
|
808 | - * @param string $model_name |
|
809 | - * @param ModelVersionInfo $model_version_info |
|
810 | - * @param boolean $create whether this is for request to create (in which case we need |
|
811 | - * all required params) or just to update (in which case we don't need those on every request) |
|
812 | - * @return array |
|
813 | - */ |
|
814 | - protected function _get_write_params( |
|
815 | - $model_name, |
|
816 | - ModelVersionInfo $model_version_info, |
|
817 | - $create = false |
|
818 | - ) { |
|
819 | - $model = EE_Registry::instance()->load_model($model_name); |
|
820 | - $fields = $model_version_info->fieldsOnModelInThisVersion($model); |
|
821 | - $args_info = array(); |
|
822 | - foreach ($fields as $field_name => $field_obj) { |
|
823 | - if ($field_obj->is_auto_increment()) { |
|
824 | - //totally ignore auto increment IDs |
|
825 | - continue; |
|
826 | - } |
|
827 | - $arg_info = $field_obj->getSchema(); |
|
828 | - $required = $create && ! $field_obj->is_nullable() && $field_obj->get_default_value() === null; |
|
829 | - $arg_info['required'] = $required; |
|
830 | - //remove the read-only flag. If it were read-only we wouldn't list it as an argument while writing, right? |
|
831 | - unset($arg_info['readonly']); |
|
832 | - $schema_properties = $field_obj->getSchemaProperties(); |
|
833 | - if ($field_obj->getSchemaType() === 'object' |
|
834 | - && isset($schema_properties['raw']) |
|
835 | - ) { |
|
836 | - //if there's a "raw" form of this argument, use those properties instead |
|
837 | - $arg_info = array_replace( |
|
838 | - $arg_info, |
|
839 | - $schema_properties['raw'] |
|
840 | - ); |
|
841 | - } |
|
842 | - $arg_info['default'] = ModelDataTranslator::prepareFieldValueForJson( |
|
843 | - $field_obj, |
|
844 | - $field_obj->get_default_value(), |
|
845 | - $model_version_info->requestedVersion() |
|
846 | - ); |
|
847 | - //we do our own validation and sanitization within the controller |
|
848 | - $arg_info['sanitize_callback'] = |
|
849 | - array( |
|
850 | - 'EED_Core_Rest_Api', |
|
851 | - 'default_sanitize_callback', |
|
852 | - ); |
|
853 | - $args_info[$field_name] = $arg_info; |
|
854 | - if ($field_obj instanceof EE_Datetime_Field) { |
|
855 | - $gmt_arg_info = $arg_info; |
|
856 | - $gmt_arg_info['description'] = sprintf( |
|
857 | - esc_html__( |
|
858 | - '%1$s - the value for this field in UTC. Ignored if %2$s is provided.', |
|
859 | - 'event_espresso' |
|
860 | - ), |
|
861 | - $field_obj->get_nicename(), |
|
862 | - $field_name |
|
863 | - ); |
|
864 | - $args_info[$field_name . '_gmt'] = $gmt_arg_info; |
|
865 | - } |
|
866 | - } |
|
867 | - return $args_info; |
|
868 | - } |
|
869 | - |
|
870 | - |
|
871 | - |
|
872 | - /** |
|
873 | - * Replacement for WP API's 'rest_parse_request_arg'. If the value is blank but not required, don't bother validating it. |
|
874 | - * Also, it uses our email validation instead of WP API's default. |
|
875 | - * @param $value |
|
876 | - * @param WP_REST_Request $request |
|
877 | - * @param $param |
|
878 | - * @return bool|true|WP_Error |
|
879 | - */ |
|
880 | - public static function default_sanitize_callback( $value, WP_REST_Request $request, $param) |
|
881 | - { |
|
882 | - $attributes = $request->get_attributes(); |
|
883 | - if (! isset($attributes['args'][$param]) |
|
884 | - || ! is_array($attributes['args'][$param])) { |
|
885 | - $validation_result = true; |
|
886 | - } else { |
|
887 | - $args = $attributes['args'][$param]; |
|
888 | - if (( |
|
889 | - $value === '' |
|
890 | - || $value === null |
|
891 | - ) |
|
892 | - && (! isset($args['required']) |
|
893 | - || $args['required'] === false |
|
894 | - ) |
|
895 | - ) { |
|
896 | - //not required and not provided? that's cool |
|
897 | - $validation_result = true; |
|
898 | - } elseif (isset($args['format']) |
|
899 | - && $args['format'] === 'email' |
|
900 | - ) { |
|
901 | - if (! self::_validate_email($value)) { |
|
902 | - $validation_result = new WP_Error( |
|
903 | - 'rest_invalid_param', |
|
904 | - esc_html__('The email address is not valid or does not exist.', 'event_espresso') |
|
905 | - ); |
|
906 | - } else { |
|
907 | - $validation_result = true; |
|
908 | - } |
|
909 | - } else { |
|
910 | - $validation_result = rest_validate_value_from_schema($value, $args, $param); |
|
911 | - } |
|
912 | - } |
|
913 | - if (is_wp_error($validation_result)) { |
|
914 | - return $validation_result; |
|
915 | - } |
|
916 | - return rest_sanitize_request_arg($value, $request, $param); |
|
917 | - } |
|
918 | - |
|
919 | - |
|
920 | - |
|
921 | - /** |
|
922 | - * Returns whether or not this email address is vaild. Copied from EE_Email_Valdiation_Strategy::_validate_email() |
|
923 | - * @param $email |
|
924 | - * @return bool |
|
925 | - */ |
|
926 | - protected static function _validate_email($email){ |
|
927 | - $loader = new Loader(); |
|
928 | - $validation_service = $loader->getShared( |
|
929 | - 'EventEspresso\core\domain\services\validation\EmailValidationServiceInterface' |
|
930 | - ); |
|
931 | - try { |
|
932 | - $validation_service->validate($email); |
|
933 | - return true; |
|
934 | - } catch (EmailValidationException $e) { |
|
935 | - return false; |
|
936 | - } |
|
937 | - } |
|
938 | - |
|
939 | - |
|
940 | - |
|
941 | - /** |
|
942 | - * Gets routes for the config |
|
943 | - * |
|
944 | - * @return array @see _register_model_routes |
|
945 | - * @deprecated since version 4.9.1 |
|
946 | - */ |
|
947 | - protected function _register_config_routes() |
|
948 | - { |
|
949 | - $config_routes = array(); |
|
950 | - foreach (self::versions_served() as $version => $hidden_endpoint) { |
|
951 | - $config_routes[self::ee_api_namespace . $version] = $this->_get_config_route_data_for_version( |
|
952 | - $version, |
|
953 | - $hidden_endpoint |
|
954 | - ); |
|
955 | - } |
|
956 | - return $config_routes; |
|
957 | - } |
|
958 | - |
|
959 | - |
|
960 | - |
|
961 | - /** |
|
962 | - * Gets routes for the config for the specified version |
|
963 | - * |
|
964 | - * @param string $version |
|
965 | - * @param boolean $hidden_endpoint |
|
966 | - * @return array |
|
967 | - */ |
|
968 | - protected function _get_config_route_data_for_version($version, $hidden_endpoint) |
|
969 | - { |
|
970 | - return array( |
|
971 | - 'config' => array( |
|
972 | - array( |
|
973 | - 'callback' => array( |
|
974 | - 'EventEspresso\core\libraries\rest_api\controllers\config\Read', |
|
975 | - 'handleRequest', |
|
976 | - ), |
|
977 | - 'methods' => WP_REST_Server::READABLE, |
|
978 | - 'hidden_endpoint' => $hidden_endpoint, |
|
979 | - 'callback_args' => array($version), |
|
980 | - ), |
|
981 | - ), |
|
982 | - 'site_info' => array( |
|
983 | - array( |
|
984 | - 'callback' => array( |
|
985 | - 'EventEspresso\core\libraries\rest_api\controllers\config\Read', |
|
986 | - 'handleRequestSiteInfo', |
|
987 | - ), |
|
988 | - 'methods' => WP_REST_Server::READABLE, |
|
989 | - 'hidden_endpoint' => $hidden_endpoint, |
|
990 | - 'callback_args' => array($version), |
|
991 | - ), |
|
992 | - ), |
|
993 | - ); |
|
994 | - } |
|
995 | - |
|
996 | - |
|
997 | - |
|
998 | - /** |
|
999 | - * Gets the meta info routes |
|
1000 | - * |
|
1001 | - * @return array @see _register_model_routes |
|
1002 | - * @deprecated since version 4.9.1 |
|
1003 | - */ |
|
1004 | - protected function _register_meta_routes() |
|
1005 | - { |
|
1006 | - $meta_routes = array(); |
|
1007 | - foreach (self::versions_served() as $version => $hidden_endpoint) { |
|
1008 | - $meta_routes[self::ee_api_namespace . $version] = $this->_get_meta_route_data_for_version( |
|
1009 | - $version, |
|
1010 | - $hidden_endpoint |
|
1011 | - ); |
|
1012 | - } |
|
1013 | - return $meta_routes; |
|
1014 | - } |
|
1015 | - |
|
1016 | - |
|
1017 | - |
|
1018 | - /** |
|
1019 | - * @param string $version |
|
1020 | - * @param boolean $hidden_endpoint |
|
1021 | - * @return array |
|
1022 | - */ |
|
1023 | - protected function _get_meta_route_data_for_version($version, $hidden_endpoint = false) |
|
1024 | - { |
|
1025 | - return array( |
|
1026 | - 'resources' => array( |
|
1027 | - array( |
|
1028 | - 'callback' => array( |
|
1029 | - 'EventEspresso\core\libraries\rest_api\controllers\model\Meta', |
|
1030 | - 'handleRequestModelsMeta', |
|
1031 | - ), |
|
1032 | - 'methods' => WP_REST_Server::READABLE, |
|
1033 | - 'hidden_endpoint' => $hidden_endpoint, |
|
1034 | - 'callback_args' => array($version), |
|
1035 | - ), |
|
1036 | - ), |
|
1037 | - ); |
|
1038 | - } |
|
1039 | - |
|
1040 | - |
|
1041 | - |
|
1042 | - /** |
|
1043 | - * Tries to hide old 4.6 endpoints from the |
|
1044 | - * |
|
1045 | - * @param array $route_data |
|
1046 | - * @return array |
|
1047 | - */ |
|
1048 | - public static function hide_old_endpoints($route_data) |
|
1049 | - { |
|
1050 | - //allow API clients to override which endpoints get hidden, in case |
|
1051 | - //they want to discover particular endpoints |
|
1052 | - //also, we don't have access to the request so we have to just grab it from the superglobal |
|
1053 | - $force_show_ee_namespace = ltrim( |
|
1054 | - EEH_Array::is_set($_REQUEST, 'force_show_ee_namespace', ''), |
|
1055 | - '/' |
|
1056 | - ); |
|
1057 | - foreach (EED_Core_Rest_Api::get_ee_route_data() as $namespace => $relative_urls) { |
|
1058 | - foreach ($relative_urls as $resource_name => $endpoints) { |
|
1059 | - foreach ($endpoints as $key => $endpoint) { |
|
1060 | - //skip schema and other route options |
|
1061 | - if (! is_numeric($key)) { |
|
1062 | - continue; |
|
1063 | - } |
|
1064 | - //by default, hide "hidden_endpoint"s, unless the request indicates |
|
1065 | - //to $force_show_ee_namespace, in which case only show that one |
|
1066 | - //namespace's endpoints (and hide all others) |
|
1067 | - if (($endpoint['hidden_endpoint'] && $force_show_ee_namespace === '') |
|
1068 | - || ($force_show_ee_namespace !== '' && $force_show_ee_namespace !== $namespace) |
|
1069 | - ) { |
|
1070 | - $full_route = '/' . ltrim($namespace, '/') . '/' . ltrim($resource_name, '/'); |
|
1071 | - unset($route_data[$full_route]); |
|
1072 | - } |
|
1073 | - } |
|
1074 | - } |
|
1075 | - } |
|
1076 | - return $route_data; |
|
1077 | - } |
|
1078 | - |
|
1079 | - |
|
1080 | - |
|
1081 | - /** |
|
1082 | - * Returns an array describing which versions of core support serving requests for. |
|
1083 | - * Keys are core versions' major and minor version, and values are the |
|
1084 | - * LOWEST requested version they can serve. Eg, 4.7 can serve requests for 4.6-like |
|
1085 | - * data by just removing a few models and fields from the responses. However, 4.15 might remove |
|
1086 | - * the answers table entirely, in which case it would be very difficult for |
|
1087 | - * it to serve 4.6-style responses. |
|
1088 | - * Versions of core that are missing from this array are unknowns. |
|
1089 | - * previous ver |
|
1090 | - * |
|
1091 | - * @return array |
|
1092 | - */ |
|
1093 | - public static function version_compatibilities() |
|
1094 | - { |
|
1095 | - return apply_filters( |
|
1096 | - 'FHEE__EED_Core_REST_API__version_compatibilities', |
|
1097 | - array( |
|
1098 | - '4.8.29' => '4.8.29', |
|
1099 | - '4.8.33' => '4.8.29', |
|
1100 | - '4.8.34' => '4.8.29', |
|
1101 | - '4.8.36' => '4.8.29', |
|
1102 | - ) |
|
1103 | - ); |
|
1104 | - } |
|
1105 | - |
|
1106 | - |
|
1107 | - |
|
1108 | - /** |
|
1109 | - * Gets the latest API version served. Eg if there |
|
1110 | - * are two versions served of the API, 4.8.29 and 4.8.32, and |
|
1111 | - * we are on core version 4.8.34, it will return the string "4.8.32" |
|
1112 | - * |
|
1113 | - * @return string |
|
1114 | - */ |
|
1115 | - public static function latest_rest_api_version() |
|
1116 | - { |
|
1117 | - $versions_served = \EED_Core_Rest_Api::versions_served(); |
|
1118 | - $versions_served_keys = array_keys($versions_served); |
|
1119 | - return end($versions_served_keys); |
|
1120 | - } |
|
1121 | - |
|
1122 | - |
|
1123 | - |
|
1124 | - /** |
|
1125 | - * Using EED_Core_Rest_Api::version_compatibilities(), determines what version of |
|
1126 | - * EE the API can serve requests for. Eg, if we are on 4.15 of core, and |
|
1127 | - * we can serve requests from 4.12 or later, this will return array( '4.12', '4.13', '4.14', '4.15' ). |
|
1128 | - * We also indicate whether or not this version should be put in the index or not |
|
1129 | - * |
|
1130 | - * @return array keys are API version numbers (just major and minor numbers), and values |
|
1131 | - * are whether or not they should be hidden |
|
1132 | - */ |
|
1133 | - public static function versions_served() |
|
1134 | - { |
|
1135 | - $versions_served = array(); |
|
1136 | - $possibly_served_versions = EED_Core_Rest_Api::version_compatibilities(); |
|
1137 | - $lowest_compatible_version = end($possibly_served_versions); |
|
1138 | - reset($possibly_served_versions); |
|
1139 | - $versions_served_historically = array_keys($possibly_served_versions); |
|
1140 | - $latest_version = end($versions_served_historically); |
|
1141 | - reset($versions_served_historically); |
|
1142 | - //for each version of core we have ever served: |
|
1143 | - foreach ($versions_served_historically as $key_versioned_endpoint) { |
|
1144 | - //if it's not above the current core version, and it's compatible with the current version of core |
|
1145 | - if ($key_versioned_endpoint == $latest_version) { |
|
1146 | - //don't hide the latest version in the index |
|
1147 | - $versions_served[$key_versioned_endpoint] = false; |
|
1148 | - } elseif ($key_versioned_endpoint < EED_Core_Rest_Api::core_version() |
|
1149 | - && $key_versioned_endpoint >= $lowest_compatible_version |
|
1150 | - ) { |
|
1151 | - //include, but hide, previous versions which are still supported |
|
1152 | - $versions_served[$key_versioned_endpoint] = true; |
|
1153 | - } elseif (apply_filters( |
|
1154 | - 'FHEE__EED_Core_Rest_Api__versions_served__include_incompatible_versions', |
|
1155 | - false, |
|
1156 | - $possibly_served_versions |
|
1157 | - )) { |
|
1158 | - //if a version is no longer supported, don't include it in index or list of versions served |
|
1159 | - $versions_served[$key_versioned_endpoint] = true; |
|
1160 | - } |
|
1161 | - } |
|
1162 | - return $versions_served; |
|
1163 | - } |
|
1164 | - |
|
1165 | - |
|
1166 | - |
|
1167 | - /** |
|
1168 | - * Gets the major and minor version of EE core's version string |
|
1169 | - * |
|
1170 | - * @return string |
|
1171 | - */ |
|
1172 | - public static function core_version() |
|
1173 | - { |
|
1174 | - return apply_filters( |
|
1175 | - 'FHEE__EED_Core_REST_API__core_version', |
|
1176 | - implode( |
|
1177 | - '.', |
|
1178 | - array_slice( |
|
1179 | - explode( |
|
1180 | - '.', |
|
1181 | - espresso_version() |
|
1182 | - ), |
|
1183 | - 0, |
|
1184 | - 3 |
|
1185 | - ) |
|
1186 | - ) |
|
1187 | - ); |
|
1188 | - } |
|
1189 | - |
|
1190 | - |
|
1191 | - |
|
1192 | - /** |
|
1193 | - * Gets the default limit that should be used when querying for resources |
|
1194 | - * |
|
1195 | - * @return int |
|
1196 | - */ |
|
1197 | - public static function get_default_query_limit() |
|
1198 | - { |
|
1199 | - //we actually don't use a const because we want folks to always use |
|
1200 | - //this method, not the const directly |
|
1201 | - return apply_filters( |
|
1202 | - 'FHEE__EED_Core_Rest_Api__get_default_query_limit', |
|
1203 | - 50 |
|
1204 | - ); |
|
1205 | - } |
|
1206 | - |
|
1207 | - |
|
1208 | - |
|
1209 | - /** |
|
1210 | - * run - initial module setup |
|
1211 | - * |
|
1212 | - * @access public |
|
1213 | - * @param WP $WP |
|
1214 | - * @return void |
|
1215 | - */ |
|
1216 | - public function run($WP) |
|
1217 | - { |
|
1218 | - } |
|
27 | + const ee_api_namespace_for_regex = 'ee\/v([^/]*)\/'; |
|
28 | + |
|
29 | + const saved_routes_option_names = 'ee_core_routes'; |
|
30 | + |
|
31 | + /** |
|
32 | + * string used in _links response bodies to make them globally unique. |
|
33 | + * |
|
34 | + * @see http://v2.wp-api.org/extending/linking/ |
|
35 | + */ |
|
36 | + const ee_api_link_namespace = 'https://api.eventespresso.com/'; |
|
37 | + |
|
38 | + /** |
|
39 | + * @var CalculatedModelFields |
|
40 | + */ |
|
41 | + protected static $_field_calculator = null; |
|
42 | + |
|
43 | + |
|
44 | + |
|
45 | + /** |
|
46 | + * @return EED_Core_Rest_Api |
|
47 | + */ |
|
48 | + public static function instance() |
|
49 | + { |
|
50 | + self::$_field_calculator = new CalculatedModelFields(); |
|
51 | + return parent::get_instance(__CLASS__); |
|
52 | + } |
|
53 | + |
|
54 | + |
|
55 | + |
|
56 | + /** |
|
57 | + * set_hooks - for hooking into EE Core, other modules, etc |
|
58 | + * |
|
59 | + * @access public |
|
60 | + * @return void |
|
61 | + */ |
|
62 | + public static function set_hooks() |
|
63 | + { |
|
64 | + self::set_hooks_both(); |
|
65 | + } |
|
66 | + |
|
67 | + |
|
68 | + |
|
69 | + /** |
|
70 | + * set_hooks_admin - for hooking into EE Admin Core, other modules, etc |
|
71 | + * |
|
72 | + * @access public |
|
73 | + * @return void |
|
74 | + */ |
|
75 | + public static function set_hooks_admin() |
|
76 | + { |
|
77 | + self::set_hooks_both(); |
|
78 | + } |
|
79 | + |
|
80 | + |
|
81 | + |
|
82 | + public static function set_hooks_both() |
|
83 | + { |
|
84 | + add_action('rest_api_init', array('EED_Core_Rest_Api', 'register_routes'), 10); |
|
85 | + add_action('rest_api_init', array('EED_Core_Rest_Api', 'set_hooks_rest_api'), 5); |
|
86 | + add_filter('rest_route_data', array('EED_Core_Rest_Api', 'hide_old_endpoints'), 10, 2); |
|
87 | + add_filter('rest_index', |
|
88 | + array('EventEspresso\core\libraries\rest_api\controllers\model\Meta', 'filterEeMetadataIntoIndex')); |
|
89 | + EED_Core_Rest_Api::invalidate_cached_route_data_on_version_change(); |
|
90 | + } |
|
91 | + |
|
92 | + |
|
93 | + |
|
94 | + /** |
|
95 | + * sets up hooks which only need to be included as part of REST API requests; |
|
96 | + * other requests like to the frontend or admin etc don't need them |
|
97 | + */ |
|
98 | + public static function set_hooks_rest_api() |
|
99 | + { |
|
100 | + //set hooks which account for changes made to the API |
|
101 | + EED_Core_Rest_Api::_set_hooks_for_changes(); |
|
102 | + } |
|
103 | + |
|
104 | + |
|
105 | + |
|
106 | + /** |
|
107 | + * public wrapper of _set_hooks_for_changes. |
|
108 | + * Loads all the hooks which make requests to old versions of the API |
|
109 | + * appear the same as they always did |
|
110 | + */ |
|
111 | + public static function set_hooks_for_changes() |
|
112 | + { |
|
113 | + self::_set_hooks_for_changes(); |
|
114 | + } |
|
115 | + |
|
116 | + |
|
117 | + |
|
118 | + /** |
|
119 | + * Loads all the hooks which make requests to old versions of the API |
|
120 | + * appear the same as they always did |
|
121 | + */ |
|
122 | + protected static function _set_hooks_for_changes() |
|
123 | + { |
|
124 | + $folder_contents = EEH_File::get_contents_of_folders(array(EE_LIBRARIES . 'rest_api' . DS . 'changes'), false); |
|
125 | + foreach ($folder_contents as $classname_in_namespace => $filepath) { |
|
126 | + //ignore the base parent class |
|
127 | + //and legacy named classes |
|
128 | + if ($classname_in_namespace === 'ChangesInBase' |
|
129 | + || strpos($classname_in_namespace, 'Changes_In_') === 0 |
|
130 | + ) { |
|
131 | + continue; |
|
132 | + } |
|
133 | + $full_classname = 'EventEspresso\core\libraries\rest_api\changes\\' . $classname_in_namespace; |
|
134 | + if (class_exists($full_classname)) { |
|
135 | + $instance_of_class = new $full_classname; |
|
136 | + if ($instance_of_class instanceof ChangesInBase) { |
|
137 | + $instance_of_class->setHooks(); |
|
138 | + } |
|
139 | + } |
|
140 | + } |
|
141 | + } |
|
142 | + |
|
143 | + |
|
144 | + |
|
145 | + /** |
|
146 | + * Filters the WP routes to add our EE-related ones. This takes a bit of time |
|
147 | + * so we actually prefer to only do it when an EE plugin is activated or upgraded |
|
148 | + */ |
|
149 | + public static function register_routes() |
|
150 | + { |
|
151 | + foreach (EED_Core_Rest_Api::get_ee_route_data() as $namespace => $relative_routes) { |
|
152 | + foreach ($relative_routes as $relative_route => $data_for_multiple_endpoints) { |
|
153 | + /** |
|
154 | + * @var array $data_for_multiple_endpoints numerically indexed array |
|
155 | + * but can also contain route options like { |
|
156 | + * @type array $schema { |
|
157 | + * @type callable $schema_callback |
|
158 | + * @type array $callback_args arguments that will be passed to the callback, after the |
|
159 | + * WP_REST_Request of course |
|
160 | + * } |
|
161 | + * } |
|
162 | + */ |
|
163 | + //when registering routes, register all the endpoints' data at the same time |
|
164 | + $multiple_endpoint_args = array(); |
|
165 | + foreach ($data_for_multiple_endpoints as $endpoint_key => $data_for_single_endpoint) { |
|
166 | + /** |
|
167 | + * @var array $data_for_single_endpoint { |
|
168 | + * @type callable $callback |
|
169 | + * @type string methods |
|
170 | + * @type array args |
|
171 | + * @type array _links |
|
172 | + * @type array $callback_args arguments that will be passed to the callback, after the |
|
173 | + * WP_REST_Request of course |
|
174 | + * } |
|
175 | + */ |
|
176 | + //skip route options |
|
177 | + if (! is_numeric($endpoint_key)) { |
|
178 | + continue; |
|
179 | + } |
|
180 | + if (! isset($data_for_single_endpoint['callback'], $data_for_single_endpoint['methods'])) { |
|
181 | + throw new EE_Error( |
|
182 | + esc_html__( |
|
183 | + // @codingStandardsIgnoreStart |
|
184 | + 'Endpoint configuration data needs to have entries "callback" (callable) and "methods" (comma-separated list of accepts HTTP methods).', |
|
185 | + // @codingStandardsIgnoreEnd |
|
186 | + 'event_espresso') |
|
187 | + ); |
|
188 | + } |
|
189 | + $callback = $data_for_single_endpoint['callback']; |
|
190 | + $single_endpoint_args = array( |
|
191 | + 'methods' => $data_for_single_endpoint['methods'], |
|
192 | + 'args' => isset($data_for_single_endpoint['args']) ? $data_for_single_endpoint['args'] |
|
193 | + : array(), |
|
194 | + ); |
|
195 | + if (isset($data_for_single_endpoint['_links'])) { |
|
196 | + $single_endpoint_args['_links'] = $data_for_single_endpoint['_links']; |
|
197 | + } |
|
198 | + if (isset($data_for_single_endpoint['callback_args'])) { |
|
199 | + $callback_args = $data_for_single_endpoint['callback_args']; |
|
200 | + $single_endpoint_args['callback'] = function (\WP_REST_Request $request) use ( |
|
201 | + $callback, |
|
202 | + $callback_args |
|
203 | + ) { |
|
204 | + array_unshift($callback_args, $request); |
|
205 | + return call_user_func_array( |
|
206 | + $callback, |
|
207 | + $callback_args |
|
208 | + ); |
|
209 | + }; |
|
210 | + } else { |
|
211 | + $single_endpoint_args['callback'] = $data_for_single_endpoint['callback']; |
|
212 | + } |
|
213 | + $multiple_endpoint_args[] = $single_endpoint_args; |
|
214 | + } |
|
215 | + if (isset($data_for_multiple_endpoints['schema'])) { |
|
216 | + $schema_route_data = $data_for_multiple_endpoints['schema']; |
|
217 | + $schema_callback = $schema_route_data['schema_callback']; |
|
218 | + $callback_args = $schema_route_data['callback_args']; |
|
219 | + $multiple_endpoint_args['schema'] = function () use ($schema_callback, $callback_args) { |
|
220 | + return call_user_func_array( |
|
221 | + $schema_callback, |
|
222 | + $callback_args |
|
223 | + ); |
|
224 | + }; |
|
225 | + } |
|
226 | + register_rest_route( |
|
227 | + $namespace, |
|
228 | + $relative_route, |
|
229 | + $multiple_endpoint_args |
|
230 | + ); |
|
231 | + } |
|
232 | + } |
|
233 | + } |
|
234 | + |
|
235 | + |
|
236 | + |
|
237 | + /** |
|
238 | + * Checks if there was a version change or something that merits invalidating the cached |
|
239 | + * route data. If so, invalidates the cached route data so that it gets refreshed |
|
240 | + * next time the WP API is used |
|
241 | + */ |
|
242 | + public static function invalidate_cached_route_data_on_version_change() |
|
243 | + { |
|
244 | + if (EE_System::instance()->detect_req_type() != EE_System::req_type_normal) { |
|
245 | + EED_Core_Rest_Api::invalidate_cached_route_data(); |
|
246 | + } |
|
247 | + foreach (EE_Registry::instance()->addons as $addon) { |
|
248 | + if ($addon instanceof EE_Addon && $addon->detect_req_type() != EE_System::req_type_normal) { |
|
249 | + EED_Core_Rest_Api::invalidate_cached_route_data(); |
|
250 | + } |
|
251 | + } |
|
252 | + } |
|
253 | + |
|
254 | + |
|
255 | + |
|
256 | + /** |
|
257 | + * Removes the cached route data so it will get refreshed next time the WP API is used |
|
258 | + */ |
|
259 | + public static function invalidate_cached_route_data() |
|
260 | + { |
|
261 | + //delete the saved EE REST API routes |
|
262 | + foreach (EED_Core_Rest_Api::versions_served() as $version => $hidden) { |
|
263 | + delete_option(EED_Core_Rest_Api::saved_routes_option_names . $version); |
|
264 | + } |
|
265 | + } |
|
266 | + |
|
267 | + |
|
268 | + |
|
269 | + /** |
|
270 | + * Gets the EE route data |
|
271 | + * |
|
272 | + * @return array top-level key is the namespace, next-level key is the route and its value is array{ |
|
273 | + * @type string|array $callback |
|
274 | + * @type string $methods |
|
275 | + * @type boolean $hidden_endpoint |
|
276 | + * } |
|
277 | + */ |
|
278 | + public static function get_ee_route_data() |
|
279 | + { |
|
280 | + $ee_routes = array(); |
|
281 | + foreach (self::versions_served() as $version => $hidden_endpoints) { |
|
282 | + $ee_routes[self::ee_api_namespace . $version] = self::_get_ee_route_data_for_version( |
|
283 | + $version, |
|
284 | + $hidden_endpoints |
|
285 | + ); |
|
286 | + } |
|
287 | + return $ee_routes; |
|
288 | + } |
|
289 | + |
|
290 | + |
|
291 | + |
|
292 | + /** |
|
293 | + * Gets the EE route data from the wp options if it exists already, |
|
294 | + * otherwise re-generates it and saves it to the option |
|
295 | + * |
|
296 | + * @param string $version |
|
297 | + * @param boolean $hidden_endpoints |
|
298 | + * @return array |
|
299 | + */ |
|
300 | + protected static function _get_ee_route_data_for_version($version, $hidden_endpoints = false) |
|
301 | + { |
|
302 | + $ee_routes = get_option(self::saved_routes_option_names . $version, null); |
|
303 | + if (! $ee_routes || (defined('EE_REST_API_DEBUG_MODE') && EE_REST_API_DEBUG_MODE)) { |
|
304 | + $ee_routes = self::_save_ee_route_data_for_version($version, $hidden_endpoints); |
|
305 | + } |
|
306 | + return $ee_routes; |
|
307 | + } |
|
308 | + |
|
309 | + |
|
310 | + |
|
311 | + /** |
|
312 | + * Saves the EE REST API route data to a wp option and returns it |
|
313 | + * |
|
314 | + * @param string $version |
|
315 | + * @param boolean $hidden_endpoints |
|
316 | + * @return mixed|null |
|
317 | + */ |
|
318 | + protected static function _save_ee_route_data_for_version($version, $hidden_endpoints = false) |
|
319 | + { |
|
320 | + $instance = self::instance(); |
|
321 | + $routes = apply_filters( |
|
322 | + 'EED_Core_Rest_Api__save_ee_route_data_for_version__routes', |
|
323 | + array_replace_recursive( |
|
324 | + $instance->_get_config_route_data_for_version($version, $hidden_endpoints), |
|
325 | + $instance->_get_meta_route_data_for_version($version, $hidden_endpoints), |
|
326 | + $instance->_get_model_route_data_for_version($version, $hidden_endpoints), |
|
327 | + $instance->_get_rpc_route_data_for_version($version, $hidden_endpoints) |
|
328 | + ) |
|
329 | + ); |
|
330 | + $option_name = self::saved_routes_option_names . $version; |
|
331 | + if (get_option($option_name)) { |
|
332 | + update_option($option_name, $routes, true); |
|
333 | + } else { |
|
334 | + add_option($option_name, $routes, null, 'no'); |
|
335 | + } |
|
336 | + return $routes; |
|
337 | + } |
|
338 | + |
|
339 | + |
|
340 | + |
|
341 | + /** |
|
342 | + * Calculates all the EE routes and saves it to a WordPress option so we don't |
|
343 | + * need to calculate it on every request |
|
344 | + * |
|
345 | + * @deprecated since version 4.9.1 |
|
346 | + * @return void |
|
347 | + */ |
|
348 | + public static function save_ee_routes() |
|
349 | + { |
|
350 | + if (EE_Maintenance_Mode::instance()->models_can_query()) { |
|
351 | + $instance = self::instance(); |
|
352 | + $routes = apply_filters( |
|
353 | + 'EED_Core_Rest_Api__save_ee_routes__routes', |
|
354 | + array_replace_recursive( |
|
355 | + $instance->_register_config_routes(), |
|
356 | + $instance->_register_meta_routes(), |
|
357 | + $instance->_register_model_routes(), |
|
358 | + $instance->_register_rpc_routes() |
|
359 | + ) |
|
360 | + ); |
|
361 | + update_option(self::saved_routes_option_names, $routes, true); |
|
362 | + } |
|
363 | + } |
|
364 | + |
|
365 | + |
|
366 | + |
|
367 | + /** |
|
368 | + * Gets all the route information relating to EE models |
|
369 | + * |
|
370 | + * @return array @see get_ee_route_data |
|
371 | + * @deprecated since version 4.9.1 |
|
372 | + */ |
|
373 | + protected function _register_model_routes() |
|
374 | + { |
|
375 | + $model_routes = array(); |
|
376 | + foreach (self::versions_served() as $version => $hidden_endpoint) { |
|
377 | + $model_routes[EED_Core_Rest_Api::ee_api_namespace |
|
378 | + . $version] = $this->_get_config_route_data_for_version($version, $hidden_endpoint); |
|
379 | + } |
|
380 | + return $model_routes; |
|
381 | + } |
|
382 | + |
|
383 | + |
|
384 | + |
|
385 | + /** |
|
386 | + * Decides whether or not to add write endpoints for this model. |
|
387 | + * |
|
388 | + * Currently, this defaults to exclude all global tables and models |
|
389 | + * which would allow inserting WP core data (we don't want to duplicate |
|
390 | + * what WP API does, as it's unnecessary, extra work, and potentially extra bugs) |
|
391 | + * @param EEM_Base $model |
|
392 | + * @return bool |
|
393 | + */ |
|
394 | + public static function should_have_write_endpoints(EEM_Base $model) |
|
395 | + { |
|
396 | + if ($model->is_wp_core_model()){ |
|
397 | + return false; |
|
398 | + } |
|
399 | + foreach($model->get_tables() as $table){ |
|
400 | + if( $table->is_global()){ |
|
401 | + return false; |
|
402 | + } |
|
403 | + } |
|
404 | + return true; |
|
405 | + } |
|
406 | + |
|
407 | + |
|
408 | + |
|
409 | + /** |
|
410 | + * Gets the names of all models which should have plural routes (eg `ee/v4.8.36/events`) |
|
411 | + * in this versioned namespace of EE4 |
|
412 | + * @param $version |
|
413 | + * @return array keys are model names (eg 'Event') and values ar etheir classnames (eg 'EEM_Event') |
|
414 | + */ |
|
415 | + public static function model_names_with_plural_routes($version){ |
|
416 | + $model_version_info = new ModelVersionInfo($version); |
|
417 | + $models_to_register = $model_version_info->modelsForRequestedVersion(); |
|
418 | + //let's not bother having endpoints for extra metas |
|
419 | + unset($models_to_register['Extra_Meta']); |
|
420 | + unset($models_to_register['Extra_Join']); |
|
421 | + unset($models_to_register['Post_Meta']); |
|
422 | + return apply_filters( |
|
423 | + 'FHEE__EED_Core_REST_API___register_model_routes', |
|
424 | + $models_to_register |
|
425 | + ); |
|
426 | + } |
|
427 | + |
|
428 | + |
|
429 | + |
|
430 | + /** |
|
431 | + * Gets the route data for EE models in the specified version |
|
432 | + * |
|
433 | + * @param string $version |
|
434 | + * @param boolean $hidden_endpoint |
|
435 | + * @return array |
|
436 | + */ |
|
437 | + protected function _get_model_route_data_for_version($version, $hidden_endpoint = false) |
|
438 | + { |
|
439 | + $model_routes = array(); |
|
440 | + $model_version_info = new ModelVersionInfo($version); |
|
441 | + foreach ($this->model_names_with_plural_routes($version) as $model_name => $model_classname) { |
|
442 | + $model = \EE_Registry::instance()->load_model($model_name); |
|
443 | + //if this isn't a valid model then let's skip iterate to the next item in the loop. |
|
444 | + if (! $model instanceof EEM_Base) { |
|
445 | + continue; |
|
446 | + } |
|
447 | + //yes we could just register one route for ALL models, but then they wouldn't show up in the index |
|
448 | + $plural_model_route = EED_Core_Rest_Api::get_collection_route($model_name); |
|
449 | + $singular_model_route = EED_Core_Rest_Api::get_entity_route($model_name, '(?P<id>[^\/]+)'); |
|
450 | + $model_routes[$plural_model_route] = array( |
|
451 | + array( |
|
452 | + 'callback' => array( |
|
453 | + 'EventEspresso\core\libraries\rest_api\controllers\model\Read', |
|
454 | + 'handleRequestGetAll', |
|
455 | + ), |
|
456 | + 'callback_args' => array($version, $model_name), |
|
457 | + 'methods' => WP_REST_Server::READABLE, |
|
458 | + 'hidden_endpoint' => $hidden_endpoint, |
|
459 | + 'args' => $this->_get_read_query_params($model, $version), |
|
460 | + '_links' => array( |
|
461 | + 'self' => rest_url(EED_Core_Rest_Api::ee_api_namespace . $version . $singular_model_route), |
|
462 | + ), |
|
463 | + ), |
|
464 | + 'schema' => array( |
|
465 | + 'schema_callback' => array( |
|
466 | + 'EventEspresso\core\libraries\rest_api\controllers\model\Read', |
|
467 | + 'handleSchemaRequest', |
|
468 | + ), |
|
469 | + 'callback_args' => array($version, $model_name), |
|
470 | + ), |
|
471 | + ); |
|
472 | + $model_routes[$singular_model_route] = array( |
|
473 | + array( |
|
474 | + 'callback' => array( |
|
475 | + 'EventEspresso\core\libraries\rest_api\controllers\model\Read', |
|
476 | + 'handleRequestGetOne', |
|
477 | + ), |
|
478 | + 'callback_args' => array($version, $model_name), |
|
479 | + 'methods' => WP_REST_Server::READABLE, |
|
480 | + 'hidden_endpoint' => $hidden_endpoint, |
|
481 | + 'args' => $this->_get_response_selection_query_params($model, $version), |
|
482 | + ), |
|
483 | + ); |
|
484 | + if( apply_filters( |
|
485 | + 'FHEE__EED_Core_Rest_Api___get_model_route_data_for_version__add_write_endpoints', |
|
486 | + $this->should_have_write_endpoints($model), |
|
487 | + $model |
|
488 | + )){ |
|
489 | + $model_routes[$plural_model_route][] = array( |
|
490 | + 'callback' => array( |
|
491 | + 'EventEspresso\core\libraries\rest_api\controllers\model\Write', |
|
492 | + 'handleRequestInsert', |
|
493 | + ), |
|
494 | + 'callback_args' => array($version, $model_name), |
|
495 | + 'methods' => WP_REST_Server::CREATABLE, |
|
496 | + 'hidden_endpoint' => $hidden_endpoint, |
|
497 | + 'args' => $this->_get_write_params($model_name, $model_version_info, true), |
|
498 | + ); |
|
499 | + $model_routes[$singular_model_route] = array_merge( |
|
500 | + $model_routes[$singular_model_route], |
|
501 | + array( |
|
502 | + array( |
|
503 | + 'callback' => array( |
|
504 | + 'EventEspresso\core\libraries\rest_api\controllers\model\Write', |
|
505 | + 'handleRequestUpdate', |
|
506 | + ), |
|
507 | + 'callback_args' => array($version, $model_name), |
|
508 | + 'methods' => WP_REST_Server::EDITABLE, |
|
509 | + 'hidden_endpoint' => $hidden_endpoint, |
|
510 | + 'args' => $this->_get_write_params($model_name, $model_version_info, false), |
|
511 | + ), |
|
512 | + array( |
|
513 | + 'callback' => array( |
|
514 | + 'EventEspresso\core\libraries\rest_api\controllers\model\Write', |
|
515 | + 'handleRequestDelete', |
|
516 | + ), |
|
517 | + 'callback_args' => array($version, $model_name), |
|
518 | + 'methods' => WP_REST_Server::DELETABLE, |
|
519 | + 'hidden_endpoint' => $hidden_endpoint, |
|
520 | + 'args' => $this->_get_delete_query_params($model, $version), |
|
521 | + ) |
|
522 | + ) |
|
523 | + ); |
|
524 | + } |
|
525 | + foreach ($model->relation_settings() as $relation_name => $relation_obj) { |
|
526 | + |
|
527 | + $related_route = EED_Core_Rest_Api::get_relation_route_via( |
|
528 | + $model_name, |
|
529 | + '(?P<id>[^\/]+)', |
|
530 | + $relation_obj |
|
531 | + ); |
|
532 | + $endpoints = array( |
|
533 | + array( |
|
534 | + 'callback' => array( |
|
535 | + 'EventEspresso\core\libraries\rest_api\controllers\model\Read', |
|
536 | + 'handleRequestGetRelated', |
|
537 | + ), |
|
538 | + 'callback_args' => array($version, $model_name, $relation_name), |
|
539 | + 'methods' => WP_REST_Server::READABLE, |
|
540 | + 'hidden_endpoint' => $hidden_endpoint, |
|
541 | + 'args' => $this->_get_read_query_params($relation_obj->get_other_model(), $version), |
|
542 | + ), |
|
543 | + ); |
|
544 | + $model_routes[$related_route] = $endpoints; |
|
545 | + } |
|
546 | + } |
|
547 | + return $model_routes; |
|
548 | + } |
|
549 | + |
|
550 | + |
|
551 | + |
|
552 | + /** |
|
553 | + * Gets the relative URI to a model's REST API plural route, after the EE4 versioned namespace, |
|
554 | + * excluding the preceding slash. |
|
555 | + * Eg you pass get_plural_route_to('Event') = 'events' |
|
556 | + * @param string $model_name eg Event or Venue |
|
557 | + * @return string |
|
558 | + */ |
|
559 | + public static function get_collection_route($model_name) |
|
560 | + { |
|
561 | + return EEH_Inflector::pluralize_and_lower($model_name); |
|
562 | + } |
|
563 | + |
|
564 | + |
|
565 | + |
|
566 | + /** |
|
567 | + * Gets the relative URI to a model's REST API singular route, after the EE4 versioned namespace, |
|
568 | + * excluding the preceding slash. |
|
569 | + * Eg you pass get_plural_route_to('Event', 12) = 'events/12' |
|
570 | + * @param string $model_name eg Event or Venue |
|
571 | + * @param string $id |
|
572 | + * @return string |
|
573 | + */ |
|
574 | + public static function get_entity_route($model_name, $id) |
|
575 | + { |
|
576 | + return EEH_Inflector::pluralize_and_lower($model_name) . '/' . $id; |
|
577 | + } |
|
578 | + |
|
579 | + |
|
580 | + /** |
|
581 | + * Gets the relative URI to a model's REST API singular route, after the EE4 versioned namespace, |
|
582 | + * excluding the preceding slash. |
|
583 | + * Eg you pass get_plural_route_to('Event', 12) = 'events/12' |
|
584 | + * |
|
585 | + * @param string $model_name eg Event or Venue |
|
586 | + * @param string $id |
|
587 | + * @param EE_Model_Relation_Base $relation_obj |
|
588 | + * @return string |
|
589 | + */ |
|
590 | + public static function get_relation_route_via($model_name, $id, $relation_obj) |
|
591 | + { |
|
592 | + $related_model_name_endpoint_part = ModelRead::getRelatedEntityName( |
|
593 | + $relation_obj->get_other_model()->get_this_model_name(), |
|
594 | + $relation_obj |
|
595 | + ); |
|
596 | + return EED_Core_Rest_Api::get_entity_route($model_name, $id) . '/' . $related_model_name_endpoint_part; |
|
597 | + } |
|
598 | + |
|
599 | + |
|
600 | + |
|
601 | + /** |
|
602 | + * Adds onto the $relative_route the EE4 REST API versioned namespace. |
|
603 | + * Eg if given '4.8.36' and 'events', will return 'ee/v4.8.36/events' |
|
604 | + * @param string $relative_route |
|
605 | + * @param string $version |
|
606 | + * @return string |
|
607 | + */ |
|
608 | + public static function get_versioned_route_to($relative_route, $version = '4.8.36'){ |
|
609 | + return '/' . EED_Core_Rest_Api::ee_api_namespace . $version . '/' . $relative_route; |
|
610 | + } |
|
611 | + |
|
612 | + |
|
613 | + |
|
614 | + /** |
|
615 | + * Adds all the RPC-style routes (remote procedure call-like routes, ie |
|
616 | + * routes that don't conform to the traditional REST CRUD-style). |
|
617 | + * |
|
618 | + * @deprecated since 4.9.1 |
|
619 | + */ |
|
620 | + protected function _register_rpc_routes() |
|
621 | + { |
|
622 | + $routes = array(); |
|
623 | + foreach (self::versions_served() as $version => $hidden_endpoint) { |
|
624 | + $routes[self::ee_api_namespace . $version] = $this->_get_rpc_route_data_for_version( |
|
625 | + $version, |
|
626 | + $hidden_endpoint |
|
627 | + ); |
|
628 | + } |
|
629 | + return $routes; |
|
630 | + } |
|
631 | + |
|
632 | + |
|
633 | + |
|
634 | + /** |
|
635 | + * @param string $version |
|
636 | + * @param boolean $hidden_endpoint |
|
637 | + * @return array |
|
638 | + */ |
|
639 | + protected function _get_rpc_route_data_for_version($version, $hidden_endpoint = false) |
|
640 | + { |
|
641 | + $this_versions_routes = array(); |
|
642 | + //checkin endpoint |
|
643 | + $this_versions_routes['registrations/(?P<REG_ID>\d+)/toggle_checkin_for_datetime/(?P<DTT_ID>\d+)'] = array( |
|
644 | + array( |
|
645 | + 'callback' => array( |
|
646 | + 'EventEspresso\core\libraries\rest_api\controllers\rpc\Checkin', |
|
647 | + 'handleRequestToggleCheckin', |
|
648 | + ), |
|
649 | + 'methods' => WP_REST_Server::CREATABLE, |
|
650 | + 'hidden_endpoint' => $hidden_endpoint, |
|
651 | + 'args' => array( |
|
652 | + 'force' => array( |
|
653 | + 'required' => false, |
|
654 | + 'default' => false, |
|
655 | + 'description' => __( |
|
656 | + // @codingStandardsIgnoreStart |
|
657 | + 'Whether to force toggle checkin, or to verify the registration status and allowed ticket uses', |
|
658 | + // @codingStandardsIgnoreEnd |
|
659 | + 'event_espresso' |
|
660 | + ), |
|
661 | + ), |
|
662 | + ), |
|
663 | + 'callback_args' => array($version), |
|
664 | + ), |
|
665 | + ); |
|
666 | + return apply_filters( |
|
667 | + 'FHEE__EED_Core_Rest_Api___register_rpc_routes__this_versions_routes', |
|
668 | + $this_versions_routes, |
|
669 | + $version, |
|
670 | + $hidden_endpoint |
|
671 | + ); |
|
672 | + } |
|
673 | + |
|
674 | + |
|
675 | + |
|
676 | + /** |
|
677 | + * Gets the query params that can be used when request one or many |
|
678 | + * |
|
679 | + * @param EEM_Base $model |
|
680 | + * @param string $version |
|
681 | + * @return array |
|
682 | + */ |
|
683 | + protected function _get_response_selection_query_params(\EEM_Base $model, $version) |
|
684 | + { |
|
685 | + return apply_filters( |
|
686 | + 'FHEE__EED_Core_Rest_Api___get_response_selection_query_params', |
|
687 | + array( |
|
688 | + 'include' => array( |
|
689 | + 'required' => false, |
|
690 | + 'default' => '*', |
|
691 | + 'type' => 'string', |
|
692 | + ), |
|
693 | + 'calculate' => array( |
|
694 | + 'required' => false, |
|
695 | + 'default' => '', |
|
696 | + 'enum' => self::$_field_calculator->retrieveCalculatedFieldsForModel($model), |
|
697 | + 'type' => 'string', |
|
698 | + //because we accept a CSV'd list of the enumerated strings, WP core validation and sanitization |
|
699 | + //freaks out. We'll just validate this argument while handling the request |
|
700 | + 'validate_callback' => null, |
|
701 | + 'sanitize_callback' => null, |
|
702 | + ), |
|
703 | + ), |
|
704 | + $model, |
|
705 | + $version |
|
706 | + ); |
|
707 | + } |
|
708 | + |
|
709 | + |
|
710 | + |
|
711 | + /** |
|
712 | + * Gets the parameters acceptable for delete requests |
|
713 | + * |
|
714 | + * @param \EEM_Base $model |
|
715 | + * @param string $version |
|
716 | + * @return array |
|
717 | + */ |
|
718 | + protected function _get_delete_query_params(\EEM_Base $model, $version) |
|
719 | + { |
|
720 | + $params_for_delete = array( |
|
721 | + 'allow_blocking' => array( |
|
722 | + 'required' => false, |
|
723 | + 'default' => true, |
|
724 | + 'type' => 'boolean', |
|
725 | + ), |
|
726 | + ); |
|
727 | + $params_for_delete['force'] = array( |
|
728 | + 'required' => false, |
|
729 | + 'default' => false, |
|
730 | + 'type' => 'boolean', |
|
731 | + ); |
|
732 | + return apply_filters( |
|
733 | + 'FHEE__EED_Core_Rest_Api___get_delete_query_params', |
|
734 | + $params_for_delete, |
|
735 | + $model, |
|
736 | + $version |
|
737 | + ); |
|
738 | + } |
|
739 | + |
|
740 | + |
|
741 | + |
|
742 | + /** |
|
743 | + * Gets info about reading query params that are acceptable |
|
744 | + * |
|
745 | + * @param \EEM_Base $model eg 'Event' or 'Venue' |
|
746 | + * @param string $version |
|
747 | + * @return array describing the args acceptable when querying this model |
|
748 | + * @throws \EE_Error |
|
749 | + */ |
|
750 | + protected function _get_read_query_params(\EEM_Base $model, $version) |
|
751 | + { |
|
752 | + $default_orderby = array(); |
|
753 | + foreach ($model->get_combined_primary_key_fields() as $key_field) { |
|
754 | + $default_orderby[$key_field->get_name()] = 'ASC'; |
|
755 | + } |
|
756 | + return array_merge( |
|
757 | + $this->_get_response_selection_query_params($model, $version), |
|
758 | + array( |
|
759 | + 'where' => array( |
|
760 | + 'required' => false, |
|
761 | + 'default' => array(), |
|
762 | + 'type' => 'object', |
|
763 | + ), |
|
764 | + 'limit' => array( |
|
765 | + 'required' => false, |
|
766 | + 'default' => EED_Core_Rest_Api::get_default_query_limit(), |
|
767 | + 'type' => array( |
|
768 | + 'object', |
|
769 | + 'string', |
|
770 | + 'integer', |
|
771 | + ), |
|
772 | + ), |
|
773 | + 'order_by' => array( |
|
774 | + 'required' => false, |
|
775 | + 'default' => $default_orderby, |
|
776 | + 'type' => array( |
|
777 | + 'object', |
|
778 | + 'string', |
|
779 | + ), |
|
780 | + ), |
|
781 | + 'group_by' => array( |
|
782 | + 'required' => false, |
|
783 | + 'default' => null, |
|
784 | + 'type' => array( |
|
785 | + 'object', |
|
786 | + 'string', |
|
787 | + ), |
|
788 | + ), |
|
789 | + 'having' => array( |
|
790 | + 'required' => false, |
|
791 | + 'default' => null, |
|
792 | + 'type' => 'object', |
|
793 | + ), |
|
794 | + 'caps' => array( |
|
795 | + 'required' => false, |
|
796 | + 'default' => EEM_Base::caps_read, |
|
797 | + 'type' => 'string', |
|
798 | + ), |
|
799 | + ) |
|
800 | + ); |
|
801 | + } |
|
802 | + |
|
803 | + |
|
804 | + |
|
805 | + /** |
|
806 | + * Gets parameter information for a model regarding writing data |
|
807 | + * |
|
808 | + * @param string $model_name |
|
809 | + * @param ModelVersionInfo $model_version_info |
|
810 | + * @param boolean $create whether this is for request to create (in which case we need |
|
811 | + * all required params) or just to update (in which case we don't need those on every request) |
|
812 | + * @return array |
|
813 | + */ |
|
814 | + protected function _get_write_params( |
|
815 | + $model_name, |
|
816 | + ModelVersionInfo $model_version_info, |
|
817 | + $create = false |
|
818 | + ) { |
|
819 | + $model = EE_Registry::instance()->load_model($model_name); |
|
820 | + $fields = $model_version_info->fieldsOnModelInThisVersion($model); |
|
821 | + $args_info = array(); |
|
822 | + foreach ($fields as $field_name => $field_obj) { |
|
823 | + if ($field_obj->is_auto_increment()) { |
|
824 | + //totally ignore auto increment IDs |
|
825 | + continue; |
|
826 | + } |
|
827 | + $arg_info = $field_obj->getSchema(); |
|
828 | + $required = $create && ! $field_obj->is_nullable() && $field_obj->get_default_value() === null; |
|
829 | + $arg_info['required'] = $required; |
|
830 | + //remove the read-only flag. If it were read-only we wouldn't list it as an argument while writing, right? |
|
831 | + unset($arg_info['readonly']); |
|
832 | + $schema_properties = $field_obj->getSchemaProperties(); |
|
833 | + if ($field_obj->getSchemaType() === 'object' |
|
834 | + && isset($schema_properties['raw']) |
|
835 | + ) { |
|
836 | + //if there's a "raw" form of this argument, use those properties instead |
|
837 | + $arg_info = array_replace( |
|
838 | + $arg_info, |
|
839 | + $schema_properties['raw'] |
|
840 | + ); |
|
841 | + } |
|
842 | + $arg_info['default'] = ModelDataTranslator::prepareFieldValueForJson( |
|
843 | + $field_obj, |
|
844 | + $field_obj->get_default_value(), |
|
845 | + $model_version_info->requestedVersion() |
|
846 | + ); |
|
847 | + //we do our own validation and sanitization within the controller |
|
848 | + $arg_info['sanitize_callback'] = |
|
849 | + array( |
|
850 | + 'EED_Core_Rest_Api', |
|
851 | + 'default_sanitize_callback', |
|
852 | + ); |
|
853 | + $args_info[$field_name] = $arg_info; |
|
854 | + if ($field_obj instanceof EE_Datetime_Field) { |
|
855 | + $gmt_arg_info = $arg_info; |
|
856 | + $gmt_arg_info['description'] = sprintf( |
|
857 | + esc_html__( |
|
858 | + '%1$s - the value for this field in UTC. Ignored if %2$s is provided.', |
|
859 | + 'event_espresso' |
|
860 | + ), |
|
861 | + $field_obj->get_nicename(), |
|
862 | + $field_name |
|
863 | + ); |
|
864 | + $args_info[$field_name . '_gmt'] = $gmt_arg_info; |
|
865 | + } |
|
866 | + } |
|
867 | + return $args_info; |
|
868 | + } |
|
869 | + |
|
870 | + |
|
871 | + |
|
872 | + /** |
|
873 | + * Replacement for WP API's 'rest_parse_request_arg'. If the value is blank but not required, don't bother validating it. |
|
874 | + * Also, it uses our email validation instead of WP API's default. |
|
875 | + * @param $value |
|
876 | + * @param WP_REST_Request $request |
|
877 | + * @param $param |
|
878 | + * @return bool|true|WP_Error |
|
879 | + */ |
|
880 | + public static function default_sanitize_callback( $value, WP_REST_Request $request, $param) |
|
881 | + { |
|
882 | + $attributes = $request->get_attributes(); |
|
883 | + if (! isset($attributes['args'][$param]) |
|
884 | + || ! is_array($attributes['args'][$param])) { |
|
885 | + $validation_result = true; |
|
886 | + } else { |
|
887 | + $args = $attributes['args'][$param]; |
|
888 | + if (( |
|
889 | + $value === '' |
|
890 | + || $value === null |
|
891 | + ) |
|
892 | + && (! isset($args['required']) |
|
893 | + || $args['required'] === false |
|
894 | + ) |
|
895 | + ) { |
|
896 | + //not required and not provided? that's cool |
|
897 | + $validation_result = true; |
|
898 | + } elseif (isset($args['format']) |
|
899 | + && $args['format'] === 'email' |
|
900 | + ) { |
|
901 | + if (! self::_validate_email($value)) { |
|
902 | + $validation_result = new WP_Error( |
|
903 | + 'rest_invalid_param', |
|
904 | + esc_html__('The email address is not valid or does not exist.', 'event_espresso') |
|
905 | + ); |
|
906 | + } else { |
|
907 | + $validation_result = true; |
|
908 | + } |
|
909 | + } else { |
|
910 | + $validation_result = rest_validate_value_from_schema($value, $args, $param); |
|
911 | + } |
|
912 | + } |
|
913 | + if (is_wp_error($validation_result)) { |
|
914 | + return $validation_result; |
|
915 | + } |
|
916 | + return rest_sanitize_request_arg($value, $request, $param); |
|
917 | + } |
|
918 | + |
|
919 | + |
|
920 | + |
|
921 | + /** |
|
922 | + * Returns whether or not this email address is vaild. Copied from EE_Email_Valdiation_Strategy::_validate_email() |
|
923 | + * @param $email |
|
924 | + * @return bool |
|
925 | + */ |
|
926 | + protected static function _validate_email($email){ |
|
927 | + $loader = new Loader(); |
|
928 | + $validation_service = $loader->getShared( |
|
929 | + 'EventEspresso\core\domain\services\validation\EmailValidationServiceInterface' |
|
930 | + ); |
|
931 | + try { |
|
932 | + $validation_service->validate($email); |
|
933 | + return true; |
|
934 | + } catch (EmailValidationException $e) { |
|
935 | + return false; |
|
936 | + } |
|
937 | + } |
|
938 | + |
|
939 | + |
|
940 | + |
|
941 | + /** |
|
942 | + * Gets routes for the config |
|
943 | + * |
|
944 | + * @return array @see _register_model_routes |
|
945 | + * @deprecated since version 4.9.1 |
|
946 | + */ |
|
947 | + protected function _register_config_routes() |
|
948 | + { |
|
949 | + $config_routes = array(); |
|
950 | + foreach (self::versions_served() as $version => $hidden_endpoint) { |
|
951 | + $config_routes[self::ee_api_namespace . $version] = $this->_get_config_route_data_for_version( |
|
952 | + $version, |
|
953 | + $hidden_endpoint |
|
954 | + ); |
|
955 | + } |
|
956 | + return $config_routes; |
|
957 | + } |
|
958 | + |
|
959 | + |
|
960 | + |
|
961 | + /** |
|
962 | + * Gets routes for the config for the specified version |
|
963 | + * |
|
964 | + * @param string $version |
|
965 | + * @param boolean $hidden_endpoint |
|
966 | + * @return array |
|
967 | + */ |
|
968 | + protected function _get_config_route_data_for_version($version, $hidden_endpoint) |
|
969 | + { |
|
970 | + return array( |
|
971 | + 'config' => array( |
|
972 | + array( |
|
973 | + 'callback' => array( |
|
974 | + 'EventEspresso\core\libraries\rest_api\controllers\config\Read', |
|
975 | + 'handleRequest', |
|
976 | + ), |
|
977 | + 'methods' => WP_REST_Server::READABLE, |
|
978 | + 'hidden_endpoint' => $hidden_endpoint, |
|
979 | + 'callback_args' => array($version), |
|
980 | + ), |
|
981 | + ), |
|
982 | + 'site_info' => array( |
|
983 | + array( |
|
984 | + 'callback' => array( |
|
985 | + 'EventEspresso\core\libraries\rest_api\controllers\config\Read', |
|
986 | + 'handleRequestSiteInfo', |
|
987 | + ), |
|
988 | + 'methods' => WP_REST_Server::READABLE, |
|
989 | + 'hidden_endpoint' => $hidden_endpoint, |
|
990 | + 'callback_args' => array($version), |
|
991 | + ), |
|
992 | + ), |
|
993 | + ); |
|
994 | + } |
|
995 | + |
|
996 | + |
|
997 | + |
|
998 | + /** |
|
999 | + * Gets the meta info routes |
|
1000 | + * |
|
1001 | + * @return array @see _register_model_routes |
|
1002 | + * @deprecated since version 4.9.1 |
|
1003 | + */ |
|
1004 | + protected function _register_meta_routes() |
|
1005 | + { |
|
1006 | + $meta_routes = array(); |
|
1007 | + foreach (self::versions_served() as $version => $hidden_endpoint) { |
|
1008 | + $meta_routes[self::ee_api_namespace . $version] = $this->_get_meta_route_data_for_version( |
|
1009 | + $version, |
|
1010 | + $hidden_endpoint |
|
1011 | + ); |
|
1012 | + } |
|
1013 | + return $meta_routes; |
|
1014 | + } |
|
1015 | + |
|
1016 | + |
|
1017 | + |
|
1018 | + /** |
|
1019 | + * @param string $version |
|
1020 | + * @param boolean $hidden_endpoint |
|
1021 | + * @return array |
|
1022 | + */ |
|
1023 | + protected function _get_meta_route_data_for_version($version, $hidden_endpoint = false) |
|
1024 | + { |
|
1025 | + return array( |
|
1026 | + 'resources' => array( |
|
1027 | + array( |
|
1028 | + 'callback' => array( |
|
1029 | + 'EventEspresso\core\libraries\rest_api\controllers\model\Meta', |
|
1030 | + 'handleRequestModelsMeta', |
|
1031 | + ), |
|
1032 | + 'methods' => WP_REST_Server::READABLE, |
|
1033 | + 'hidden_endpoint' => $hidden_endpoint, |
|
1034 | + 'callback_args' => array($version), |
|
1035 | + ), |
|
1036 | + ), |
|
1037 | + ); |
|
1038 | + } |
|
1039 | + |
|
1040 | + |
|
1041 | + |
|
1042 | + /** |
|
1043 | + * Tries to hide old 4.6 endpoints from the |
|
1044 | + * |
|
1045 | + * @param array $route_data |
|
1046 | + * @return array |
|
1047 | + */ |
|
1048 | + public static function hide_old_endpoints($route_data) |
|
1049 | + { |
|
1050 | + //allow API clients to override which endpoints get hidden, in case |
|
1051 | + //they want to discover particular endpoints |
|
1052 | + //also, we don't have access to the request so we have to just grab it from the superglobal |
|
1053 | + $force_show_ee_namespace = ltrim( |
|
1054 | + EEH_Array::is_set($_REQUEST, 'force_show_ee_namespace', ''), |
|
1055 | + '/' |
|
1056 | + ); |
|
1057 | + foreach (EED_Core_Rest_Api::get_ee_route_data() as $namespace => $relative_urls) { |
|
1058 | + foreach ($relative_urls as $resource_name => $endpoints) { |
|
1059 | + foreach ($endpoints as $key => $endpoint) { |
|
1060 | + //skip schema and other route options |
|
1061 | + if (! is_numeric($key)) { |
|
1062 | + continue; |
|
1063 | + } |
|
1064 | + //by default, hide "hidden_endpoint"s, unless the request indicates |
|
1065 | + //to $force_show_ee_namespace, in which case only show that one |
|
1066 | + //namespace's endpoints (and hide all others) |
|
1067 | + if (($endpoint['hidden_endpoint'] && $force_show_ee_namespace === '') |
|
1068 | + || ($force_show_ee_namespace !== '' && $force_show_ee_namespace !== $namespace) |
|
1069 | + ) { |
|
1070 | + $full_route = '/' . ltrim($namespace, '/') . '/' . ltrim($resource_name, '/'); |
|
1071 | + unset($route_data[$full_route]); |
|
1072 | + } |
|
1073 | + } |
|
1074 | + } |
|
1075 | + } |
|
1076 | + return $route_data; |
|
1077 | + } |
|
1078 | + |
|
1079 | + |
|
1080 | + |
|
1081 | + /** |
|
1082 | + * Returns an array describing which versions of core support serving requests for. |
|
1083 | + * Keys are core versions' major and minor version, and values are the |
|
1084 | + * LOWEST requested version they can serve. Eg, 4.7 can serve requests for 4.6-like |
|
1085 | + * data by just removing a few models and fields from the responses. However, 4.15 might remove |
|
1086 | + * the answers table entirely, in which case it would be very difficult for |
|
1087 | + * it to serve 4.6-style responses. |
|
1088 | + * Versions of core that are missing from this array are unknowns. |
|
1089 | + * previous ver |
|
1090 | + * |
|
1091 | + * @return array |
|
1092 | + */ |
|
1093 | + public static function version_compatibilities() |
|
1094 | + { |
|
1095 | + return apply_filters( |
|
1096 | + 'FHEE__EED_Core_REST_API__version_compatibilities', |
|
1097 | + array( |
|
1098 | + '4.8.29' => '4.8.29', |
|
1099 | + '4.8.33' => '4.8.29', |
|
1100 | + '4.8.34' => '4.8.29', |
|
1101 | + '4.8.36' => '4.8.29', |
|
1102 | + ) |
|
1103 | + ); |
|
1104 | + } |
|
1105 | + |
|
1106 | + |
|
1107 | + |
|
1108 | + /** |
|
1109 | + * Gets the latest API version served. Eg if there |
|
1110 | + * are two versions served of the API, 4.8.29 and 4.8.32, and |
|
1111 | + * we are on core version 4.8.34, it will return the string "4.8.32" |
|
1112 | + * |
|
1113 | + * @return string |
|
1114 | + */ |
|
1115 | + public static function latest_rest_api_version() |
|
1116 | + { |
|
1117 | + $versions_served = \EED_Core_Rest_Api::versions_served(); |
|
1118 | + $versions_served_keys = array_keys($versions_served); |
|
1119 | + return end($versions_served_keys); |
|
1120 | + } |
|
1121 | + |
|
1122 | + |
|
1123 | + |
|
1124 | + /** |
|
1125 | + * Using EED_Core_Rest_Api::version_compatibilities(), determines what version of |
|
1126 | + * EE the API can serve requests for. Eg, if we are on 4.15 of core, and |
|
1127 | + * we can serve requests from 4.12 or later, this will return array( '4.12', '4.13', '4.14', '4.15' ). |
|
1128 | + * We also indicate whether or not this version should be put in the index or not |
|
1129 | + * |
|
1130 | + * @return array keys are API version numbers (just major and minor numbers), and values |
|
1131 | + * are whether or not they should be hidden |
|
1132 | + */ |
|
1133 | + public static function versions_served() |
|
1134 | + { |
|
1135 | + $versions_served = array(); |
|
1136 | + $possibly_served_versions = EED_Core_Rest_Api::version_compatibilities(); |
|
1137 | + $lowest_compatible_version = end($possibly_served_versions); |
|
1138 | + reset($possibly_served_versions); |
|
1139 | + $versions_served_historically = array_keys($possibly_served_versions); |
|
1140 | + $latest_version = end($versions_served_historically); |
|
1141 | + reset($versions_served_historically); |
|
1142 | + //for each version of core we have ever served: |
|
1143 | + foreach ($versions_served_historically as $key_versioned_endpoint) { |
|
1144 | + //if it's not above the current core version, and it's compatible with the current version of core |
|
1145 | + if ($key_versioned_endpoint == $latest_version) { |
|
1146 | + //don't hide the latest version in the index |
|
1147 | + $versions_served[$key_versioned_endpoint] = false; |
|
1148 | + } elseif ($key_versioned_endpoint < EED_Core_Rest_Api::core_version() |
|
1149 | + && $key_versioned_endpoint >= $lowest_compatible_version |
|
1150 | + ) { |
|
1151 | + //include, but hide, previous versions which are still supported |
|
1152 | + $versions_served[$key_versioned_endpoint] = true; |
|
1153 | + } elseif (apply_filters( |
|
1154 | + 'FHEE__EED_Core_Rest_Api__versions_served__include_incompatible_versions', |
|
1155 | + false, |
|
1156 | + $possibly_served_versions |
|
1157 | + )) { |
|
1158 | + //if a version is no longer supported, don't include it in index or list of versions served |
|
1159 | + $versions_served[$key_versioned_endpoint] = true; |
|
1160 | + } |
|
1161 | + } |
|
1162 | + return $versions_served; |
|
1163 | + } |
|
1164 | + |
|
1165 | + |
|
1166 | + |
|
1167 | + /** |
|
1168 | + * Gets the major and minor version of EE core's version string |
|
1169 | + * |
|
1170 | + * @return string |
|
1171 | + */ |
|
1172 | + public static function core_version() |
|
1173 | + { |
|
1174 | + return apply_filters( |
|
1175 | + 'FHEE__EED_Core_REST_API__core_version', |
|
1176 | + implode( |
|
1177 | + '.', |
|
1178 | + array_slice( |
|
1179 | + explode( |
|
1180 | + '.', |
|
1181 | + espresso_version() |
|
1182 | + ), |
|
1183 | + 0, |
|
1184 | + 3 |
|
1185 | + ) |
|
1186 | + ) |
|
1187 | + ); |
|
1188 | + } |
|
1189 | + |
|
1190 | + |
|
1191 | + |
|
1192 | + /** |
|
1193 | + * Gets the default limit that should be used when querying for resources |
|
1194 | + * |
|
1195 | + * @return int |
|
1196 | + */ |
|
1197 | + public static function get_default_query_limit() |
|
1198 | + { |
|
1199 | + //we actually don't use a const because we want folks to always use |
|
1200 | + //this method, not the const directly |
|
1201 | + return apply_filters( |
|
1202 | + 'FHEE__EED_Core_Rest_Api__get_default_query_limit', |
|
1203 | + 50 |
|
1204 | + ); |
|
1205 | + } |
|
1206 | + |
|
1207 | + |
|
1208 | + |
|
1209 | + /** |
|
1210 | + * run - initial module setup |
|
1211 | + * |
|
1212 | + * @access public |
|
1213 | + * @param WP $WP |
|
1214 | + * @return void |
|
1215 | + */ |
|
1216 | + public function run($WP) |
|
1217 | + { |
|
1218 | + } |
|
1219 | 1219 | } |
1220 | 1220 | |
1221 | 1221 | // End of file EED_Core_Rest_Api.module.php |
@@ -121,7 +121,7 @@ discard block |
||
121 | 121 | */ |
122 | 122 | protected static function _set_hooks_for_changes() |
123 | 123 | { |
124 | - $folder_contents = EEH_File::get_contents_of_folders(array(EE_LIBRARIES . 'rest_api' . DS . 'changes'), false); |
|
124 | + $folder_contents = EEH_File::get_contents_of_folders(array(EE_LIBRARIES.'rest_api'.DS.'changes'), false); |
|
125 | 125 | foreach ($folder_contents as $classname_in_namespace => $filepath) { |
126 | 126 | //ignore the base parent class |
127 | 127 | //and legacy named classes |
@@ -130,7 +130,7 @@ discard block |
||
130 | 130 | ) { |
131 | 131 | continue; |
132 | 132 | } |
133 | - $full_classname = 'EventEspresso\core\libraries\rest_api\changes\\' . $classname_in_namespace; |
|
133 | + $full_classname = 'EventEspresso\core\libraries\rest_api\changes\\'.$classname_in_namespace; |
|
134 | 134 | if (class_exists($full_classname)) { |
135 | 135 | $instance_of_class = new $full_classname; |
136 | 136 | if ($instance_of_class instanceof ChangesInBase) { |
@@ -174,10 +174,10 @@ discard block |
||
174 | 174 | * } |
175 | 175 | */ |
176 | 176 | //skip route options |
177 | - if (! is_numeric($endpoint_key)) { |
|
177 | + if ( ! is_numeric($endpoint_key)) { |
|
178 | 178 | continue; |
179 | 179 | } |
180 | - if (! isset($data_for_single_endpoint['callback'], $data_for_single_endpoint['methods'])) { |
|
180 | + if ( ! isset($data_for_single_endpoint['callback'], $data_for_single_endpoint['methods'])) { |
|
181 | 181 | throw new EE_Error( |
182 | 182 | esc_html__( |
183 | 183 | // @codingStandardsIgnoreStart |
@@ -197,7 +197,7 @@ discard block |
||
197 | 197 | } |
198 | 198 | if (isset($data_for_single_endpoint['callback_args'])) { |
199 | 199 | $callback_args = $data_for_single_endpoint['callback_args']; |
200 | - $single_endpoint_args['callback'] = function (\WP_REST_Request $request) use ( |
|
200 | + $single_endpoint_args['callback'] = function(\WP_REST_Request $request) use ( |
|
201 | 201 | $callback, |
202 | 202 | $callback_args |
203 | 203 | ) { |
@@ -216,7 +216,7 @@ discard block |
||
216 | 216 | $schema_route_data = $data_for_multiple_endpoints['schema']; |
217 | 217 | $schema_callback = $schema_route_data['schema_callback']; |
218 | 218 | $callback_args = $schema_route_data['callback_args']; |
219 | - $multiple_endpoint_args['schema'] = function () use ($schema_callback, $callback_args) { |
|
219 | + $multiple_endpoint_args['schema'] = function() use ($schema_callback, $callback_args) { |
|
220 | 220 | return call_user_func_array( |
221 | 221 | $schema_callback, |
222 | 222 | $callback_args |
@@ -260,7 +260,7 @@ discard block |
||
260 | 260 | { |
261 | 261 | //delete the saved EE REST API routes |
262 | 262 | foreach (EED_Core_Rest_Api::versions_served() as $version => $hidden) { |
263 | - delete_option(EED_Core_Rest_Api::saved_routes_option_names . $version); |
|
263 | + delete_option(EED_Core_Rest_Api::saved_routes_option_names.$version); |
|
264 | 264 | } |
265 | 265 | } |
266 | 266 | |
@@ -279,7 +279,7 @@ discard block |
||
279 | 279 | { |
280 | 280 | $ee_routes = array(); |
281 | 281 | foreach (self::versions_served() as $version => $hidden_endpoints) { |
282 | - $ee_routes[self::ee_api_namespace . $version] = self::_get_ee_route_data_for_version( |
|
282 | + $ee_routes[self::ee_api_namespace.$version] = self::_get_ee_route_data_for_version( |
|
283 | 283 | $version, |
284 | 284 | $hidden_endpoints |
285 | 285 | ); |
@@ -299,8 +299,8 @@ discard block |
||
299 | 299 | */ |
300 | 300 | protected static function _get_ee_route_data_for_version($version, $hidden_endpoints = false) |
301 | 301 | { |
302 | - $ee_routes = get_option(self::saved_routes_option_names . $version, null); |
|
303 | - if (! $ee_routes || (defined('EE_REST_API_DEBUG_MODE') && EE_REST_API_DEBUG_MODE)) { |
|
302 | + $ee_routes = get_option(self::saved_routes_option_names.$version, null); |
|
303 | + if ( ! $ee_routes || (defined('EE_REST_API_DEBUG_MODE') && EE_REST_API_DEBUG_MODE)) { |
|
304 | 304 | $ee_routes = self::_save_ee_route_data_for_version($version, $hidden_endpoints); |
305 | 305 | } |
306 | 306 | return $ee_routes; |
@@ -327,7 +327,7 @@ discard block |
||
327 | 327 | $instance->_get_rpc_route_data_for_version($version, $hidden_endpoints) |
328 | 328 | ) |
329 | 329 | ); |
330 | - $option_name = self::saved_routes_option_names . $version; |
|
330 | + $option_name = self::saved_routes_option_names.$version; |
|
331 | 331 | if (get_option($option_name)) { |
332 | 332 | update_option($option_name, $routes, true); |
333 | 333 | } else { |
@@ -393,11 +393,11 @@ discard block |
||
393 | 393 | */ |
394 | 394 | public static function should_have_write_endpoints(EEM_Base $model) |
395 | 395 | { |
396 | - if ($model->is_wp_core_model()){ |
|
396 | + if ($model->is_wp_core_model()) { |
|
397 | 397 | return false; |
398 | 398 | } |
399 | - foreach($model->get_tables() as $table){ |
|
400 | - if( $table->is_global()){ |
|
399 | + foreach ($model->get_tables() as $table) { |
|
400 | + if ($table->is_global()) { |
|
401 | 401 | return false; |
402 | 402 | } |
403 | 403 | } |
@@ -412,7 +412,7 @@ discard block |
||
412 | 412 | * @param $version |
413 | 413 | * @return array keys are model names (eg 'Event') and values ar etheir classnames (eg 'EEM_Event') |
414 | 414 | */ |
415 | - public static function model_names_with_plural_routes($version){ |
|
415 | + public static function model_names_with_plural_routes($version) { |
|
416 | 416 | $model_version_info = new ModelVersionInfo($version); |
417 | 417 | $models_to_register = $model_version_info->modelsForRequestedVersion(); |
418 | 418 | //let's not bother having endpoints for extra metas |
@@ -441,7 +441,7 @@ discard block |
||
441 | 441 | foreach ($this->model_names_with_plural_routes($version) as $model_name => $model_classname) { |
442 | 442 | $model = \EE_Registry::instance()->load_model($model_name); |
443 | 443 | //if this isn't a valid model then let's skip iterate to the next item in the loop. |
444 | - if (! $model instanceof EEM_Base) { |
|
444 | + if ( ! $model instanceof EEM_Base) { |
|
445 | 445 | continue; |
446 | 446 | } |
447 | 447 | //yes we could just register one route for ALL models, but then they wouldn't show up in the index |
@@ -458,7 +458,7 @@ discard block |
||
458 | 458 | 'hidden_endpoint' => $hidden_endpoint, |
459 | 459 | 'args' => $this->_get_read_query_params($model, $version), |
460 | 460 | '_links' => array( |
461 | - 'self' => rest_url(EED_Core_Rest_Api::ee_api_namespace . $version . $singular_model_route), |
|
461 | + 'self' => rest_url(EED_Core_Rest_Api::ee_api_namespace.$version.$singular_model_route), |
|
462 | 462 | ), |
463 | 463 | ), |
464 | 464 | 'schema' => array( |
@@ -481,11 +481,11 @@ discard block |
||
481 | 481 | 'args' => $this->_get_response_selection_query_params($model, $version), |
482 | 482 | ), |
483 | 483 | ); |
484 | - if( apply_filters( |
|
484 | + if (apply_filters( |
|
485 | 485 | 'FHEE__EED_Core_Rest_Api___get_model_route_data_for_version__add_write_endpoints', |
486 | 486 | $this->should_have_write_endpoints($model), |
487 | 487 | $model |
488 | - )){ |
|
488 | + )) { |
|
489 | 489 | $model_routes[$plural_model_route][] = array( |
490 | 490 | 'callback' => array( |
491 | 491 | 'EventEspresso\core\libraries\rest_api\controllers\model\Write', |
@@ -573,7 +573,7 @@ discard block |
||
573 | 573 | */ |
574 | 574 | public static function get_entity_route($model_name, $id) |
575 | 575 | { |
576 | - return EEH_Inflector::pluralize_and_lower($model_name) . '/' . $id; |
|
576 | + return EEH_Inflector::pluralize_and_lower($model_name).'/'.$id; |
|
577 | 577 | } |
578 | 578 | |
579 | 579 | |
@@ -593,7 +593,7 @@ discard block |
||
593 | 593 | $relation_obj->get_other_model()->get_this_model_name(), |
594 | 594 | $relation_obj |
595 | 595 | ); |
596 | - return EED_Core_Rest_Api::get_entity_route($model_name, $id) . '/' . $related_model_name_endpoint_part; |
|
596 | + return EED_Core_Rest_Api::get_entity_route($model_name, $id).'/'.$related_model_name_endpoint_part; |
|
597 | 597 | } |
598 | 598 | |
599 | 599 | |
@@ -605,8 +605,8 @@ discard block |
||
605 | 605 | * @param string $version |
606 | 606 | * @return string |
607 | 607 | */ |
608 | - public static function get_versioned_route_to($relative_route, $version = '4.8.36'){ |
|
609 | - return '/' . EED_Core_Rest_Api::ee_api_namespace . $version . '/' . $relative_route; |
|
608 | + public static function get_versioned_route_to($relative_route, $version = '4.8.36') { |
|
609 | + return '/'.EED_Core_Rest_Api::ee_api_namespace.$version.'/'.$relative_route; |
|
610 | 610 | } |
611 | 611 | |
612 | 612 | |
@@ -621,7 +621,7 @@ discard block |
||
621 | 621 | { |
622 | 622 | $routes = array(); |
623 | 623 | foreach (self::versions_served() as $version => $hidden_endpoint) { |
624 | - $routes[self::ee_api_namespace . $version] = $this->_get_rpc_route_data_for_version( |
|
624 | + $routes[self::ee_api_namespace.$version] = $this->_get_rpc_route_data_for_version( |
|
625 | 625 | $version, |
626 | 626 | $hidden_endpoint |
627 | 627 | ); |
@@ -861,7 +861,7 @@ discard block |
||
861 | 861 | $field_obj->get_nicename(), |
862 | 862 | $field_name |
863 | 863 | ); |
864 | - $args_info[$field_name . '_gmt'] = $gmt_arg_info; |
|
864 | + $args_info[$field_name.'_gmt'] = $gmt_arg_info; |
|
865 | 865 | } |
866 | 866 | } |
867 | 867 | return $args_info; |
@@ -877,10 +877,10 @@ discard block |
||
877 | 877 | * @param $param |
878 | 878 | * @return bool|true|WP_Error |
879 | 879 | */ |
880 | - public static function default_sanitize_callback( $value, WP_REST_Request $request, $param) |
|
880 | + public static function default_sanitize_callback($value, WP_REST_Request $request, $param) |
|
881 | 881 | { |
882 | 882 | $attributes = $request->get_attributes(); |
883 | - if (! isset($attributes['args'][$param]) |
|
883 | + if ( ! isset($attributes['args'][$param]) |
|
884 | 884 | || ! is_array($attributes['args'][$param])) { |
885 | 885 | $validation_result = true; |
886 | 886 | } else { |
@@ -889,7 +889,7 @@ discard block |
||
889 | 889 | $value === '' |
890 | 890 | || $value === null |
891 | 891 | ) |
892 | - && (! isset($args['required']) |
|
892 | + && ( ! isset($args['required']) |
|
893 | 893 | || $args['required'] === false |
894 | 894 | ) |
895 | 895 | ) { |
@@ -898,7 +898,7 @@ discard block |
||
898 | 898 | } elseif (isset($args['format']) |
899 | 899 | && $args['format'] === 'email' |
900 | 900 | ) { |
901 | - if (! self::_validate_email($value)) { |
|
901 | + if ( ! self::_validate_email($value)) { |
|
902 | 902 | $validation_result = new WP_Error( |
903 | 903 | 'rest_invalid_param', |
904 | 904 | esc_html__('The email address is not valid or does not exist.', 'event_espresso') |
@@ -923,7 +923,7 @@ discard block |
||
923 | 923 | * @param $email |
924 | 924 | * @return bool |
925 | 925 | */ |
926 | - protected static function _validate_email($email){ |
|
926 | + protected static function _validate_email($email) { |
|
927 | 927 | $loader = new Loader(); |
928 | 928 | $validation_service = $loader->getShared( |
929 | 929 | 'EventEspresso\core\domain\services\validation\EmailValidationServiceInterface' |
@@ -948,7 +948,7 @@ discard block |
||
948 | 948 | { |
949 | 949 | $config_routes = array(); |
950 | 950 | foreach (self::versions_served() as $version => $hidden_endpoint) { |
951 | - $config_routes[self::ee_api_namespace . $version] = $this->_get_config_route_data_for_version( |
|
951 | + $config_routes[self::ee_api_namespace.$version] = $this->_get_config_route_data_for_version( |
|
952 | 952 | $version, |
953 | 953 | $hidden_endpoint |
954 | 954 | ); |
@@ -1005,7 +1005,7 @@ discard block |
||
1005 | 1005 | { |
1006 | 1006 | $meta_routes = array(); |
1007 | 1007 | foreach (self::versions_served() as $version => $hidden_endpoint) { |
1008 | - $meta_routes[self::ee_api_namespace . $version] = $this->_get_meta_route_data_for_version( |
|
1008 | + $meta_routes[self::ee_api_namespace.$version] = $this->_get_meta_route_data_for_version( |
|
1009 | 1009 | $version, |
1010 | 1010 | $hidden_endpoint |
1011 | 1011 | ); |
@@ -1058,7 +1058,7 @@ discard block |
||
1058 | 1058 | foreach ($relative_urls as $resource_name => $endpoints) { |
1059 | 1059 | foreach ($endpoints as $key => $endpoint) { |
1060 | 1060 | //skip schema and other route options |
1061 | - if (! is_numeric($key)) { |
|
1061 | + if ( ! is_numeric($key)) { |
|
1062 | 1062 | continue; |
1063 | 1063 | } |
1064 | 1064 | //by default, hide "hidden_endpoint"s, unless the request indicates |
@@ -1067,7 +1067,7 @@ discard block |
||
1067 | 1067 | if (($endpoint['hidden_endpoint'] && $force_show_ee_namespace === '') |
1068 | 1068 | || ($force_show_ee_namespace !== '' && $force_show_ee_namespace !== $namespace) |
1069 | 1069 | ) { |
1070 | - $full_route = '/' . ltrim($namespace, '/') . '/' . ltrim($resource_name, '/'); |
|
1070 | + $full_route = '/'.ltrim($namespace, '/').'/'.ltrim($resource_name, '/'); |
|
1071 | 1071 | unset($route_data[$full_route]); |
1072 | 1072 | } |
1073 | 1073 | } |
@@ -4,7 +4,7 @@ discard block |
||
4 | 4 | use EventEspresso\core\services\loaders\LoaderInterface; |
5 | 5 | |
6 | 6 | if (! defined('EVENT_ESPRESSO_VERSION')) { |
7 | - exit('No direct script access allowed'); |
|
7 | + exit('No direct script access allowed'); |
|
8 | 8 | } |
9 | 9 | |
10 | 10 | |
@@ -22,658 +22,658 @@ discard block |
||
22 | 22 | { |
23 | 23 | |
24 | 24 | |
25 | - /** |
|
26 | - * This means that the requested class dependency is not present in the dependency map |
|
27 | - */ |
|
28 | - const not_registered = 0; |
|
29 | - |
|
30 | - |
|
31 | - /** |
|
32 | - * This instructs class loaders to ALWAYS return a newly instantiated object for the requested class. |
|
33 | - */ |
|
34 | - const load_new_object = 1; |
|
35 | - |
|
36 | - /** |
|
37 | - * This instructs class loaders to return a previously instantiated and cached object for the requested class. |
|
38 | - * IF a previously instantiated object does not exist, a new one will be created and added to the cache. |
|
39 | - */ |
|
40 | - const load_from_cache = 2; |
|
41 | - |
|
42 | - /** |
|
43 | - * @type EE_Dependency_Map $_instance |
|
44 | - */ |
|
45 | - protected static $_instance; |
|
46 | - |
|
47 | - /** |
|
48 | - * @type EE_Request $request |
|
49 | - */ |
|
50 | - protected $_request; |
|
51 | - |
|
52 | - /** |
|
53 | - * @type EE_Response $response |
|
54 | - */ |
|
55 | - protected $_response; |
|
56 | - |
|
57 | - /** |
|
58 | - * @type LoaderInterface $loader |
|
59 | - */ |
|
60 | - protected $loader; |
|
61 | - |
|
62 | - /** |
|
63 | - * @type array $_dependency_map |
|
64 | - */ |
|
65 | - protected $_dependency_map = array(); |
|
66 | - |
|
67 | - /** |
|
68 | - * @type array $_class_loaders |
|
69 | - */ |
|
70 | - protected $_class_loaders = array(); |
|
71 | - |
|
72 | - /** |
|
73 | - * @type array $_aliases |
|
74 | - */ |
|
75 | - protected $_aliases = array(); |
|
76 | - |
|
77 | - |
|
78 | - |
|
79 | - /** |
|
80 | - * EE_Dependency_Map constructor. |
|
81 | - * |
|
82 | - * @param EE_Request $request |
|
83 | - * @param EE_Response $response |
|
84 | - */ |
|
85 | - protected function __construct(EE_Request $request, EE_Response $response) |
|
86 | - { |
|
87 | - $this->_request = $request; |
|
88 | - $this->_response = $response; |
|
89 | - add_action('EE_Load_Espresso_Core__handle_request__initialize_core_loading', array($this, 'initialize')); |
|
90 | - do_action('EE_Dependency_Map____construct'); |
|
91 | - } |
|
92 | - |
|
93 | - |
|
94 | - |
|
95 | - /** |
|
96 | - * @throws InvalidDataTypeException |
|
97 | - * @throws InvalidInterfaceException |
|
98 | - * @throws InvalidArgumentException |
|
99 | - */ |
|
100 | - public function initialize() |
|
101 | - { |
|
102 | - $this->_register_core_dependencies(); |
|
103 | - $this->_register_core_class_loaders(); |
|
104 | - $this->_register_core_aliases(); |
|
105 | - } |
|
106 | - |
|
107 | - |
|
108 | - |
|
109 | - /** |
|
110 | - * @singleton method used to instantiate class object |
|
111 | - * @access public |
|
112 | - * @param EE_Request $request |
|
113 | - * @param EE_Response $response |
|
114 | - * @return EE_Dependency_Map |
|
115 | - */ |
|
116 | - public static function instance(EE_Request $request = null, EE_Response $response = null) |
|
117 | - { |
|
118 | - // check if class object is instantiated, and instantiated properly |
|
119 | - if (! self::$_instance instanceof EE_Dependency_Map) { |
|
120 | - self::$_instance = new EE_Dependency_Map($request, $response); |
|
121 | - } |
|
122 | - return self::$_instance; |
|
123 | - } |
|
124 | - |
|
125 | - |
|
126 | - |
|
127 | - /** |
|
128 | - * @param LoaderInterface $loader |
|
129 | - */ |
|
130 | - public function setLoader(LoaderInterface $loader) |
|
131 | - { |
|
132 | - $this->loader = $loader; |
|
133 | - } |
|
134 | - |
|
135 | - |
|
136 | - |
|
137 | - /** |
|
138 | - * @param string $class |
|
139 | - * @param array $dependencies |
|
140 | - * @return boolean |
|
141 | - */ |
|
142 | - public static function register_dependencies($class, $dependencies) |
|
143 | - { |
|
144 | - if (! isset(self::$_instance->_dependency_map[$class])) { |
|
145 | - // we need to make sure that any aliases used when registering a dependency |
|
146 | - // get resolved to the correct class name |
|
147 | - foreach ((array)$dependencies as $dependency => $load_source) { |
|
148 | - $alias = self::$_instance->get_alias($dependency); |
|
149 | - unset($dependencies[$dependency]); |
|
150 | - $dependencies[$alias] = $load_source; |
|
151 | - } |
|
152 | - self::$_instance->_dependency_map[$class] = (array)$dependencies; |
|
153 | - return true; |
|
154 | - } |
|
155 | - return false; |
|
156 | - } |
|
157 | - |
|
158 | - |
|
159 | - |
|
160 | - /** |
|
161 | - * @param string $class_name |
|
162 | - * @param string $loader |
|
163 | - * @return bool |
|
164 | - * @throws EE_Error |
|
165 | - */ |
|
166 | - public static function register_class_loader($class_name, $loader = 'load_core') |
|
167 | - { |
|
168 | - // check that loader is callable or method starts with "load_" and exists in EE_Registry |
|
169 | - if ( |
|
170 | - ! is_callable($loader) |
|
171 | - && ( |
|
172 | - strpos($loader, 'load_') !== 0 |
|
173 | - || ! method_exists('EE_Registry', $loader) |
|
174 | - ) |
|
175 | - ) { |
|
176 | - throw new EE_Error( |
|
177 | - sprintf( |
|
178 | - esc_html__('"%1$s" is not a valid loader method on EE_Registry.', 'event_espresso'), |
|
179 | - $loader |
|
180 | - ) |
|
181 | - ); |
|
182 | - } |
|
183 | - $class_name = self::$_instance->get_alias($class_name); |
|
184 | - if (! isset(self::$_instance->_class_loaders[$class_name])) { |
|
185 | - self::$_instance->_class_loaders[$class_name] = $loader; |
|
186 | - return true; |
|
187 | - } |
|
188 | - return false; |
|
189 | - } |
|
190 | - |
|
191 | - |
|
192 | - |
|
193 | - /** |
|
194 | - * @return array |
|
195 | - */ |
|
196 | - public function dependency_map() |
|
197 | - { |
|
198 | - return $this->_dependency_map; |
|
199 | - } |
|
200 | - |
|
201 | - |
|
202 | - |
|
203 | - /** |
|
204 | - * returns TRUE if dependency map contains a listing for the provided class name |
|
205 | - * |
|
206 | - * @param string $class_name |
|
207 | - * @return boolean |
|
208 | - */ |
|
209 | - public function has($class_name = '') |
|
210 | - { |
|
211 | - return isset($this->_dependency_map[$class_name]) ? true : false; |
|
212 | - } |
|
213 | - |
|
214 | - |
|
215 | - |
|
216 | - /** |
|
217 | - * returns TRUE if dependency map contains a listing for the provided class name AND dependency |
|
218 | - * |
|
219 | - * @param string $class_name |
|
220 | - * @param string $dependency |
|
221 | - * @return bool |
|
222 | - */ |
|
223 | - public function has_dependency_for_class($class_name = '', $dependency = '') |
|
224 | - { |
|
225 | - $dependency = $this->get_alias($dependency); |
|
226 | - return isset($this->_dependency_map[$class_name], $this->_dependency_map[$class_name][$dependency]) |
|
227 | - ? true |
|
228 | - : false; |
|
229 | - } |
|
230 | - |
|
231 | - |
|
232 | - |
|
233 | - /** |
|
234 | - * returns loading strategy for whether a previously cached dependency should be loaded or a new instance returned |
|
235 | - * |
|
236 | - * @param string $class_name |
|
237 | - * @param string $dependency |
|
238 | - * @return int |
|
239 | - */ |
|
240 | - public function loading_strategy_for_class_dependency($class_name = '', $dependency = '') |
|
241 | - { |
|
242 | - $dependency = $this->get_alias($dependency); |
|
243 | - return $this->has_dependency_for_class($class_name, $dependency) |
|
244 | - ? $this->_dependency_map[$class_name][$dependency] |
|
245 | - : EE_Dependency_Map::not_registered; |
|
246 | - } |
|
247 | - |
|
248 | - |
|
249 | - |
|
250 | - /** |
|
251 | - * @param string $class_name |
|
252 | - * @return string | Closure |
|
253 | - */ |
|
254 | - public function class_loader($class_name) |
|
255 | - { |
|
256 | - $class_name = $this->get_alias($class_name); |
|
257 | - return isset($this->_class_loaders[$class_name]) ? $this->_class_loaders[$class_name] : ''; |
|
258 | - } |
|
259 | - |
|
260 | - |
|
261 | - |
|
262 | - /** |
|
263 | - * @return array |
|
264 | - */ |
|
265 | - public function class_loaders() |
|
266 | - { |
|
267 | - return $this->_class_loaders; |
|
268 | - } |
|
269 | - |
|
270 | - |
|
271 | - |
|
272 | - /** |
|
273 | - * adds an alias for a classname |
|
274 | - * |
|
275 | - * @param string $class_name the class name that should be used (concrete class to replace interface) |
|
276 | - * @param string $alias the class name that would be type hinted for (abstract parent or interface) |
|
277 | - * @param string $for_class the class that has the dependency (is type hinting for the interface) |
|
278 | - */ |
|
279 | - public function add_alias($class_name, $alias, $for_class = '') |
|
280 | - { |
|
281 | - if ($for_class !== '') { |
|
282 | - if (! isset($this->_aliases[$for_class])) { |
|
283 | - $this->_aliases[$for_class] = array(); |
|
284 | - } |
|
285 | - $this->_aliases[$for_class][$class_name] = $alias; |
|
286 | - } |
|
287 | - $this->_aliases[$class_name] = $alias; |
|
288 | - } |
|
289 | - |
|
290 | - |
|
291 | - |
|
292 | - /** |
|
293 | - * returns TRUE if the provided class name has an alias |
|
294 | - * |
|
295 | - * @param string $class_name |
|
296 | - * @param string $for_class |
|
297 | - * @return bool |
|
298 | - */ |
|
299 | - public function has_alias($class_name = '', $for_class = '') |
|
300 | - { |
|
301 | - return isset($this->_aliases[$for_class], $this->_aliases[$for_class][$class_name]) |
|
302 | - || ( |
|
303 | - isset($this->_aliases[$class_name]) |
|
304 | - && ! is_array($this->_aliases[$class_name]) |
|
305 | - ); |
|
306 | - } |
|
307 | - |
|
308 | - |
|
309 | - |
|
310 | - /** |
|
311 | - * returns alias for class name if one exists, otherwise returns the original classname |
|
312 | - * functions recursively, so that multiple aliases can be used to drill down to a classname |
|
313 | - * for example: |
|
314 | - * if the following two entries were added to the _aliases array: |
|
315 | - * array( |
|
316 | - * 'interface_alias' => 'some\namespace\interface' |
|
317 | - * 'some\namespace\interface' => 'some\namespace\classname' |
|
318 | - * ) |
|
319 | - * then one could use EE_Registry::instance()->create( 'interface_alias' ) |
|
320 | - * to load an instance of 'some\namespace\classname' |
|
321 | - * |
|
322 | - * @param string $class_name |
|
323 | - * @param string $for_class |
|
324 | - * @return string |
|
325 | - */ |
|
326 | - public function get_alias($class_name = '', $for_class = '') |
|
327 | - { |
|
328 | - if (! $this->has_alias($class_name, $for_class)) { |
|
329 | - return $class_name; |
|
330 | - } |
|
331 | - if ($for_class !== '') { |
|
332 | - return $this->get_alias($this->_aliases[$for_class][$class_name], $for_class); |
|
333 | - } |
|
334 | - return $this->get_alias($this->_aliases[$class_name]); |
|
335 | - } |
|
336 | - |
|
337 | - |
|
338 | - |
|
339 | - /** |
|
340 | - * Registers the core dependencies and whether a previously instantiated object should be loaded from the cache, |
|
341 | - * if one exists, or whether a new object should be generated every time the requested class is loaded. |
|
342 | - * This is done by using the following class constants: |
|
343 | - * EE_Dependency_Map::load_from_cache - loads previously instantiated object |
|
344 | - * EE_Dependency_Map::load_new_object - generates a new object every time |
|
345 | - */ |
|
346 | - protected function _register_core_dependencies() |
|
347 | - { |
|
348 | - $this->_dependency_map = array( |
|
349 | - 'EE_Request_Handler' => array( |
|
350 | - 'EE_Request' => EE_Dependency_Map::load_from_cache, |
|
351 | - ), |
|
352 | - 'EE_System' => array( |
|
353 | - 'EE_Registry' => EE_Dependency_Map::load_from_cache, |
|
354 | - ), |
|
355 | - 'EE_Session' => array( |
|
356 | - 'EventEspresso\core\services\cache\TransientCacheStorage' => EE_Dependency_Map::load_from_cache, |
|
357 | - 'EE_Encryption' => EE_Dependency_Map::load_from_cache, |
|
358 | - ), |
|
359 | - 'EE_Cart' => array( |
|
360 | - 'EE_Session' => EE_Dependency_Map::load_from_cache, |
|
361 | - ), |
|
362 | - 'EE_Front_Controller' => array( |
|
363 | - 'EE_Registry' => EE_Dependency_Map::load_from_cache, |
|
364 | - 'EE_Request_Handler' => EE_Dependency_Map::load_from_cache, |
|
365 | - 'EE_Module_Request_Router' => EE_Dependency_Map::load_from_cache, |
|
366 | - ), |
|
367 | - 'EE_Messenger_Collection_Loader' => array( |
|
368 | - 'EE_Messenger_Collection' => EE_Dependency_Map::load_new_object, |
|
369 | - ), |
|
370 | - 'EE_Message_Type_Collection_Loader' => array( |
|
371 | - 'EE_Message_Type_Collection' => EE_Dependency_Map::load_new_object, |
|
372 | - ), |
|
373 | - 'EE_Message_Resource_Manager' => array( |
|
374 | - 'EE_Messenger_Collection_Loader' => EE_Dependency_Map::load_new_object, |
|
375 | - 'EE_Message_Type_Collection_Loader' => EE_Dependency_Map::load_new_object, |
|
376 | - 'EEM_Message_Template_Group' => EE_Dependency_Map::load_from_cache, |
|
377 | - ), |
|
378 | - 'EE_Message_Factory' => array( |
|
379 | - 'EE_Message_Resource_Manager' => EE_Dependency_Map::load_from_cache, |
|
380 | - ), |
|
381 | - 'EE_messages' => array( |
|
382 | - 'EE_Message_Resource_Manager' => EE_Dependency_Map::load_from_cache, |
|
383 | - ), |
|
384 | - 'EE_Messages_Generator' => array( |
|
385 | - 'EE_Messages_Queue' => EE_Dependency_Map::load_new_object, |
|
386 | - 'EE_Messages_Data_Handler_Collection' => EE_Dependency_Map::load_new_object, |
|
387 | - 'EE_Message_Template_Group_Collection' => EE_Dependency_Map::load_new_object, |
|
388 | - 'EEH_Parse_Shortcodes' => EE_Dependency_Map::load_from_cache, |
|
389 | - ), |
|
390 | - 'EE_Messages_Processor' => array( |
|
391 | - 'EE_Message_Resource_Manager' => EE_Dependency_Map::load_from_cache, |
|
392 | - ), |
|
393 | - 'EE_Messages_Queue' => array( |
|
394 | - 'EE_Message_Repository' => EE_Dependency_Map::load_new_object, |
|
395 | - ), |
|
396 | - 'EE_Messages_Template_Defaults' => array( |
|
397 | - 'EEM_Message_Template_Group' => EE_Dependency_Map::load_from_cache, |
|
398 | - 'EEM_Message_Template' => EE_Dependency_Map::load_from_cache, |
|
399 | - ), |
|
400 | - 'EE_Message_To_Generate_From_Request' => array( |
|
401 | - 'EE_Message_Resource_Manager' => EE_Dependency_Map::load_from_cache, |
|
402 | - 'EE_Request_Handler' => EE_Dependency_Map::load_from_cache, |
|
403 | - ), |
|
404 | - 'EventEspresso\core\services\commands\CommandBus' => array( |
|
405 | - 'EventEspresso\core\services\commands\CommandHandlerManager' => EE_Dependency_Map::load_from_cache, |
|
406 | - ), |
|
407 | - 'EventEspresso\services\commands\CommandHandler' => array( |
|
408 | - 'EE_Registry' => EE_Dependency_Map::load_from_cache, |
|
409 | - 'CommandBusInterface' => EE_Dependency_Map::load_from_cache, |
|
410 | - ), |
|
411 | - 'EventEspresso\core\services\commands\CommandHandlerManager' => array( |
|
412 | - 'EventEspresso\core\services\loaders\Loader' => EE_Dependency_Map::load_from_cache, |
|
413 | - ), |
|
414 | - 'EventEspresso\core\services\commands\CompositeCommandHandler' => array( |
|
415 | - 'EventEspresso\core\services\commands\CommandBus' => EE_Dependency_Map::load_from_cache, |
|
416 | - 'EventEspresso\core\services\commands\CommandFactory' => EE_Dependency_Map::load_from_cache, |
|
417 | - ), |
|
418 | - 'EventEspresso\core\services\commands\CommandFactory' => array( |
|
419 | - 'EventEspresso\core\services\loaders\Loader' => EE_Dependency_Map::load_from_cache, |
|
420 | - ), |
|
421 | - 'EventEspresso\core\services\commands\middleware\CapChecker' => array( |
|
422 | - 'EventEspresso\core\domain\services\capabilities\CapabilitiesChecker' => EE_Dependency_Map::load_from_cache, |
|
423 | - ), |
|
424 | - 'EventEspresso\core\domain\services\capabilities\CapabilitiesChecker' => array( |
|
425 | - 'EE_Capabilities' => EE_Dependency_Map::load_from_cache, |
|
426 | - ), |
|
427 | - 'EventEspresso\core\domain\services\capabilities\RegistrationsCapChecker' => array( |
|
428 | - 'EE_Capabilities' => EE_Dependency_Map::load_from_cache, |
|
429 | - ), |
|
430 | - 'EventEspresso\core\services\commands\registration\CreateRegistrationCommandHandler' => array( |
|
431 | - 'EventEspresso\core\domain\services\registration\CreateRegistrationService' => EE_Dependency_Map::load_from_cache, |
|
432 | - ), |
|
433 | - 'EventEspresso\core\services\commands\registration\CopyRegistrationDetailsCommandHandler' => array( |
|
434 | - 'EventEspresso\core\domain\services\registration\CopyRegistrationService' => EE_Dependency_Map::load_from_cache, |
|
435 | - ), |
|
436 | - 'EventEspresso\core\services\commands\registration\CopyRegistrationPaymentsCommandHandler' => array( |
|
437 | - 'EventEspresso\core\domain\services\registration\CopyRegistrationService' => EE_Dependency_Map::load_from_cache, |
|
438 | - ), |
|
439 | - 'EventEspresso\core\services\commands\registration\CancelRegistrationAndTicketLineItemCommandHandler' => array( |
|
440 | - 'EventEspresso\core\domain\services\registration\CancelTicketLineItemService' => EE_Dependency_Map::load_from_cache, |
|
441 | - ), |
|
442 | - 'EventEspresso\core\services\commands\registration\UpdateRegistrationAndTransactionAfterChangeCommandHandler' => array( |
|
443 | - 'EventEspresso\core\domain\services\registration\UpdateRegistrationService' => EE_Dependency_Map::load_from_cache, |
|
444 | - ), |
|
445 | - 'EventEspresso\core\services\commands\ticket\CreateTicketLineItemCommandHandler' => array( |
|
446 | - 'EventEspresso\core\domain\services\ticket\CreateTicketLineItemService' => EE_Dependency_Map::load_from_cache, |
|
447 | - ), |
|
448 | - 'EventEspresso\core\services\commands\ticket\CancelTicketLineItemCommandHandler' => array( |
|
449 | - 'EventEspresso\core\domain\services\ticket\CancelTicketLineItemService' => EE_Dependency_Map::load_from_cache, |
|
450 | - ), |
|
451 | - 'EventEspresso\core\domain\services\registration\CancelRegistrationService' => array( |
|
452 | - 'EventEspresso\core\domain\services\ticket\CancelTicketLineItemService' => EE_Dependency_Map::load_from_cache, |
|
453 | - ), |
|
454 | - 'EventEspresso\core\services\database\TableManager' => array( |
|
455 | - 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
456 | - ), |
|
457 | - 'EE_Data_Migration_Class_Base' => array( |
|
458 | - 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
459 | - 'EventEspresso\core\services\database\TableManager' => EE_Dependency_Map::load_from_cache, |
|
460 | - ), |
|
461 | - 'EE_DMS_Core_4_1_0' => array( |
|
462 | - 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
463 | - 'EventEspresso\core\services\database\TableManager' => EE_Dependency_Map::load_from_cache, |
|
464 | - ), |
|
465 | - 'EE_DMS_Core_4_2_0' => array( |
|
466 | - 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
467 | - 'EventEspresso\core\services\database\TableManager' => EE_Dependency_Map::load_from_cache, |
|
468 | - ), |
|
469 | - 'EE_DMS_Core_4_3_0' => array( |
|
470 | - 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
471 | - 'EventEspresso\core\services\database\TableManager' => EE_Dependency_Map::load_from_cache, |
|
472 | - ), |
|
473 | - 'EE_DMS_Core_4_4_0' => array( |
|
474 | - 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
475 | - 'EventEspresso\core\services\database\TableManager' => EE_Dependency_Map::load_from_cache, |
|
476 | - ), |
|
477 | - 'EE_DMS_Core_4_5_0' => array( |
|
478 | - 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
479 | - 'EventEspresso\core\services\database\TableManager' => EE_Dependency_Map::load_from_cache, |
|
480 | - ), |
|
481 | - 'EE_DMS_Core_4_6_0' => array( |
|
482 | - 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
483 | - 'EventEspresso\core\services\database\TableManager' => EE_Dependency_Map::load_from_cache, |
|
484 | - ), |
|
485 | - 'EE_DMS_Core_4_7_0' => array( |
|
486 | - 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
487 | - 'EventEspresso\core\services\database\TableManager' => EE_Dependency_Map::load_from_cache, |
|
488 | - ), |
|
489 | - 'EE_DMS_Core_4_8_0' => array( |
|
490 | - 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
491 | - 'EventEspresso\core\services\database\TableManager' => EE_Dependency_Map::load_from_cache, |
|
492 | - ), |
|
493 | - 'EE_DMS_Core_4_9_0' => array( |
|
494 | - 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
495 | - 'EventEspresso\core\services\database\TableManager' => EE_Dependency_Map::load_from_cache, |
|
496 | - ), |
|
497 | - 'EventEspresso\core\services\assets\Registry' => array( |
|
498 | - 'EE_Template_Config' => EE_Dependency_Map::load_from_cache, |
|
499 | - 'EE_Currency_Config' => EE_Dependency_Map::load_from_cache, |
|
500 | - ), |
|
501 | - 'EventEspresso\core\domain\entities\shortcodes\EspressoCancelled' => array( |
|
502 | - 'EventEspresso\core\services\cache\PostRelatedCacheManager' => EE_Dependency_Map::load_from_cache, |
|
503 | - ), |
|
504 | - 'EventEspresso\core\domain\entities\shortcodes\EspressoCheckout' => array( |
|
505 | - 'EventEspresso\core\services\cache\PostRelatedCacheManager' => EE_Dependency_Map::load_from_cache, |
|
506 | - ), |
|
507 | - 'EventEspresso\core\domain\entities\shortcodes\EspressoEventAttendees' => array( |
|
508 | - 'EventEspresso\core\services\cache\PostRelatedCacheManager' => EE_Dependency_Map::load_from_cache, |
|
509 | - ), |
|
510 | - 'EventEspresso\core\domain\entities\shortcodes\EspressoEvents' => array( |
|
511 | - 'EventEspresso\core\services\cache\PostRelatedCacheManager' => EE_Dependency_Map::load_from_cache, |
|
512 | - ), |
|
513 | - 'EventEspresso\core\domain\entities\shortcodes\EspressoThankYou' => array( |
|
514 | - 'EventEspresso\core\services\cache\PostRelatedCacheManager' => EE_Dependency_Map::load_from_cache, |
|
515 | - ), |
|
516 | - 'EventEspresso\core\domain\entities\shortcodes\EspressoTicketSelector' => array( |
|
517 | - 'EventEspresso\core\services\cache\PostRelatedCacheManager' => EE_Dependency_Map::load_from_cache, |
|
518 | - ), |
|
519 | - 'EventEspresso\core\domain\entities\shortcodes\EspressoTxnPage' => array( |
|
520 | - 'EventEspresso\core\services\cache\PostRelatedCacheManager' => EE_Dependency_Map::load_from_cache, |
|
521 | - ), |
|
522 | - 'EventEspresso\core\services\cache\BasicCacheManager' => array( |
|
523 | - 'EventEspresso\core\services\cache\TransientCacheStorage' => EE_Dependency_Map::load_from_cache, |
|
524 | - ), |
|
525 | - 'EventEspresso\core\services\cache\PostRelatedCacheManager' => array( |
|
526 | - 'EventEspresso\core\services\cache\TransientCacheStorage' => EE_Dependency_Map::load_from_cache, |
|
527 | - ), |
|
528 | - 'EventEspresso\core\domain\services\validation\EmailValidationServiceInterface' => array( |
|
529 | - 'EE_Config' => EE_Dependency_Map::load_from_cache, |
|
530 | - 'EventEspresso\core\services\loaders\Loader' => EE_Dependency_Map::load_from_cache |
|
531 | - ) |
|
532 | - ); |
|
533 | - } |
|
534 | - |
|
535 | - |
|
536 | - |
|
537 | - /** |
|
538 | - * Registers how core classes are loaded. |
|
539 | - * This can either be done by simply providing the name of one of the EE_Registry loader methods such as: |
|
540 | - * 'EE_Request_Handler' => 'load_core' |
|
541 | - * 'EE_Messages_Queue' => 'load_lib' |
|
542 | - * 'EEH_Debug_Tools' => 'load_helper' |
|
543 | - * or, if greater control is required, by providing a custom closure. For example: |
|
544 | - * 'Some_Class' => function () { |
|
545 | - * return new Some_Class(); |
|
546 | - * }, |
|
547 | - * This is required for instantiating dependencies |
|
548 | - * where an interface has been type hinted in a class constructor. For example: |
|
549 | - * 'Required_Interface' => function () { |
|
550 | - * return new A_Class_That_Implements_Required_Interface(); |
|
551 | - * }, |
|
552 | - */ |
|
553 | - protected function _register_core_class_loaders() |
|
554 | - { |
|
555 | - //for PHP5.3 compat, we need to register any properties called here in a variable because `$this` cannot |
|
556 | - //be used in a closure. |
|
557 | - $request = &$this->_request; |
|
558 | - $response = &$this->_response; |
|
559 | - $loader = &$this->loader; |
|
560 | - $this->_class_loaders = array( |
|
561 | - //load_core |
|
562 | - 'EE_Capabilities' => 'load_core', |
|
563 | - 'EE_Encryption' => 'load_core', |
|
564 | - 'EE_Front_Controller' => 'load_core', |
|
565 | - 'EE_Module_Request_Router' => 'load_core', |
|
566 | - 'EE_Registry' => 'load_core', |
|
567 | - 'EE_Request' => function () use (&$request) { |
|
568 | - return $request; |
|
569 | - }, |
|
570 | - 'EE_Response' => function () use (&$response) { |
|
571 | - return $response; |
|
572 | - }, |
|
573 | - 'EE_Request_Handler' => 'load_core', |
|
574 | - 'EE_Session' => 'load_core', |
|
575 | - 'EE_System' => 'load_core', |
|
576 | - //load_lib |
|
577 | - 'EE_Message_Resource_Manager' => 'load_lib', |
|
578 | - 'EE_Message_Type_Collection' => 'load_lib', |
|
579 | - 'EE_Message_Type_Collection_Loader' => 'load_lib', |
|
580 | - 'EE_Messenger_Collection' => 'load_lib', |
|
581 | - 'EE_Messenger_Collection_Loader' => 'load_lib', |
|
582 | - 'EE_Messages_Processor' => 'load_lib', |
|
583 | - 'EE_Message_Repository' => 'load_lib', |
|
584 | - 'EE_Messages_Queue' => 'load_lib', |
|
585 | - 'EE_Messages_Data_Handler_Collection' => 'load_lib', |
|
586 | - 'EE_Message_Template_Group_Collection' => 'load_lib', |
|
587 | - 'EE_Messages_Generator' => function () { |
|
588 | - return EE_Registry::instance()->load_lib( |
|
589 | - 'Messages_Generator', |
|
590 | - array(), |
|
591 | - false, |
|
592 | - false |
|
593 | - ); |
|
594 | - }, |
|
595 | - 'EE_Messages_Template_Defaults' => function ($arguments = array()) { |
|
596 | - return EE_Registry::instance()->load_lib( |
|
597 | - 'Messages_Template_Defaults', |
|
598 | - $arguments, |
|
599 | - false, |
|
600 | - false |
|
601 | - ); |
|
602 | - }, |
|
603 | - //load_model |
|
604 | - 'EEM_Message_Template_Group' => 'load_model', |
|
605 | - 'EEM_Message_Template' => 'load_model', |
|
606 | - //load_helper |
|
607 | - 'EEH_Parse_Shortcodes' => function () { |
|
608 | - if (EE_Registry::instance()->load_helper('Parse_Shortcodes')) { |
|
609 | - return new EEH_Parse_Shortcodes(); |
|
610 | - } |
|
611 | - return null; |
|
612 | - }, |
|
613 | - 'EE_Template_Config' => function () { |
|
614 | - return EE_Config::instance()->template_settings; |
|
615 | - }, |
|
616 | - 'EE_Currency_Config' => function () { |
|
617 | - return EE_Config::instance()->currency; |
|
618 | - }, |
|
619 | - 'EventEspresso\core\services\loaders\Loader' => function () use (&$loader) { |
|
620 | - return $loader; |
|
621 | - }, |
|
622 | - ); |
|
623 | - } |
|
624 | - |
|
625 | - |
|
626 | - |
|
627 | - /** |
|
628 | - * can be used for supplying alternate names for classes, |
|
629 | - * or for connecting interface names to instantiable classes |
|
630 | - */ |
|
631 | - protected function _register_core_aliases() |
|
632 | - { |
|
633 | - $this->_aliases = array( |
|
634 | - 'CommandBusInterface' => 'EventEspresso\core\services\commands\CommandBusInterface', |
|
635 | - 'EventEspresso\core\services\commands\CommandBusInterface' => 'EventEspresso\core\services\commands\CommandBus', |
|
636 | - 'CommandHandlerManagerInterface' => 'EventEspresso\core\services\commands\CommandHandlerManagerInterface', |
|
637 | - 'EventEspresso\core\services\commands\CommandHandlerManagerInterface' => 'EventEspresso\core\services\commands\CommandHandlerManager', |
|
638 | - 'CapChecker' => 'EventEspresso\core\services\commands\middleware\CapChecker', |
|
639 | - 'AddActionHook' => 'EventEspresso\core\services\commands\middleware\AddActionHook', |
|
640 | - 'CapabilitiesChecker' => 'EventEspresso\core\domain\services\capabilities\CapabilitiesChecker', |
|
641 | - 'CapabilitiesCheckerInterface' => 'EventEspresso\core\domain\services\capabilities\CapabilitiesCheckerInterface', |
|
642 | - 'EventEspresso\core\domain\services\capabilities\CapabilitiesCheckerInterface' => 'EventEspresso\core\domain\services\capabilities\CapabilitiesChecker', |
|
643 | - 'CreateRegistrationService' => 'EventEspresso\core\domain\services\registration\CreateRegistrationService', |
|
644 | - 'CreateRegCodeCommandHandler' => 'EventEspresso\core\services\commands\registration\CreateRegCodeCommand', |
|
645 | - 'CreateRegUrlLinkCommandHandler' => 'EventEspresso\core\services\commands\registration\CreateRegUrlLinkCommand', |
|
646 | - 'CreateRegistrationCommandHandler' => 'EventEspresso\core\services\commands\registration\CreateRegistrationCommand', |
|
647 | - 'CopyRegistrationDetailsCommandHandler' => 'EventEspresso\core\services\commands\registration\CopyRegistrationDetailsCommand', |
|
648 | - 'CopyRegistrationPaymentsCommandHandler' => 'EventEspresso\core\services\commands\registration\CopyRegistrationPaymentsCommand', |
|
649 | - 'CancelRegistrationAndTicketLineItemCommandHandler' => 'EventEspresso\core\services\commands\registration\CancelRegistrationAndTicketLineItemCommandHandler', |
|
650 | - 'UpdateRegistrationAndTransactionAfterChangeCommandHandler' => 'EventEspresso\core\services\commands\registration\UpdateRegistrationAndTransactionAfterChangeCommandHandler', |
|
651 | - 'CreateTicketLineItemCommandHandler' => 'EventEspresso\core\services\commands\ticket\CreateTicketLineItemCommand', |
|
652 | - 'TableManager' => 'EventEspresso\core\services\database\TableManager', |
|
653 | - 'TableAnalysis' => 'EventEspresso\core\services\database\TableAnalysis', |
|
654 | - 'EspressoShortcode' => 'EventEspresso\core\services\shortcodes\EspressoShortcode', |
|
655 | - 'ShortcodeInterface' => 'EventEspresso\core\services\shortcodes\ShortcodeInterface', |
|
656 | - 'EventEspresso\core\services\shortcodes\ShortcodeInterface' => 'EventEspresso\core\services\shortcodes\EspressoShortcode', |
|
657 | - 'EventEspresso\core\services\cache\CacheStorageInterface' => 'EventEspresso\core\services\cache\TransientCacheStorage', |
|
658 | - 'LoaderInterface' => 'EventEspresso\core\services\loaders\LoaderInterface', |
|
659 | - 'EventEspresso\core\services\loaders\LoaderInterface' => 'EventEspresso\core\services\loaders\Loader', |
|
660 | - 'CommandFactoryInterface' => 'EventEspresso\core\services\commands\CommandFactoryInterface', |
|
661 | - 'EventEspresso\core\services\commands\CommandFactoryInterface' => 'EventEspresso\core\services\commands\CommandFactory', |
|
662 | - 'EventEspresso\core\domain\services\validation\EmailValidationServiceInterface' => 'EventEspresso\core\services\validation\EmailValidationService', |
|
663 | - ); |
|
664 | - } |
|
665 | - |
|
666 | - |
|
667 | - |
|
668 | - /** |
|
669 | - * This is used to reset the internal map and class_loaders to their original default state at the beginning of the |
|
670 | - * request Primarily used by unit tests. |
|
671 | - */ |
|
672 | - public function reset() |
|
673 | - { |
|
674 | - $this->_register_core_class_loaders(); |
|
675 | - $this->_register_core_dependencies(); |
|
676 | - } |
|
25 | + /** |
|
26 | + * This means that the requested class dependency is not present in the dependency map |
|
27 | + */ |
|
28 | + const not_registered = 0; |
|
29 | + |
|
30 | + |
|
31 | + /** |
|
32 | + * This instructs class loaders to ALWAYS return a newly instantiated object for the requested class. |
|
33 | + */ |
|
34 | + const load_new_object = 1; |
|
35 | + |
|
36 | + /** |
|
37 | + * This instructs class loaders to return a previously instantiated and cached object for the requested class. |
|
38 | + * IF a previously instantiated object does not exist, a new one will be created and added to the cache. |
|
39 | + */ |
|
40 | + const load_from_cache = 2; |
|
41 | + |
|
42 | + /** |
|
43 | + * @type EE_Dependency_Map $_instance |
|
44 | + */ |
|
45 | + protected static $_instance; |
|
46 | + |
|
47 | + /** |
|
48 | + * @type EE_Request $request |
|
49 | + */ |
|
50 | + protected $_request; |
|
51 | + |
|
52 | + /** |
|
53 | + * @type EE_Response $response |
|
54 | + */ |
|
55 | + protected $_response; |
|
56 | + |
|
57 | + /** |
|
58 | + * @type LoaderInterface $loader |
|
59 | + */ |
|
60 | + protected $loader; |
|
61 | + |
|
62 | + /** |
|
63 | + * @type array $_dependency_map |
|
64 | + */ |
|
65 | + protected $_dependency_map = array(); |
|
66 | + |
|
67 | + /** |
|
68 | + * @type array $_class_loaders |
|
69 | + */ |
|
70 | + protected $_class_loaders = array(); |
|
71 | + |
|
72 | + /** |
|
73 | + * @type array $_aliases |
|
74 | + */ |
|
75 | + protected $_aliases = array(); |
|
76 | + |
|
77 | + |
|
78 | + |
|
79 | + /** |
|
80 | + * EE_Dependency_Map constructor. |
|
81 | + * |
|
82 | + * @param EE_Request $request |
|
83 | + * @param EE_Response $response |
|
84 | + */ |
|
85 | + protected function __construct(EE_Request $request, EE_Response $response) |
|
86 | + { |
|
87 | + $this->_request = $request; |
|
88 | + $this->_response = $response; |
|
89 | + add_action('EE_Load_Espresso_Core__handle_request__initialize_core_loading', array($this, 'initialize')); |
|
90 | + do_action('EE_Dependency_Map____construct'); |
|
91 | + } |
|
92 | + |
|
93 | + |
|
94 | + |
|
95 | + /** |
|
96 | + * @throws InvalidDataTypeException |
|
97 | + * @throws InvalidInterfaceException |
|
98 | + * @throws InvalidArgumentException |
|
99 | + */ |
|
100 | + public function initialize() |
|
101 | + { |
|
102 | + $this->_register_core_dependencies(); |
|
103 | + $this->_register_core_class_loaders(); |
|
104 | + $this->_register_core_aliases(); |
|
105 | + } |
|
106 | + |
|
107 | + |
|
108 | + |
|
109 | + /** |
|
110 | + * @singleton method used to instantiate class object |
|
111 | + * @access public |
|
112 | + * @param EE_Request $request |
|
113 | + * @param EE_Response $response |
|
114 | + * @return EE_Dependency_Map |
|
115 | + */ |
|
116 | + public static function instance(EE_Request $request = null, EE_Response $response = null) |
|
117 | + { |
|
118 | + // check if class object is instantiated, and instantiated properly |
|
119 | + if (! self::$_instance instanceof EE_Dependency_Map) { |
|
120 | + self::$_instance = new EE_Dependency_Map($request, $response); |
|
121 | + } |
|
122 | + return self::$_instance; |
|
123 | + } |
|
124 | + |
|
125 | + |
|
126 | + |
|
127 | + /** |
|
128 | + * @param LoaderInterface $loader |
|
129 | + */ |
|
130 | + public function setLoader(LoaderInterface $loader) |
|
131 | + { |
|
132 | + $this->loader = $loader; |
|
133 | + } |
|
134 | + |
|
135 | + |
|
136 | + |
|
137 | + /** |
|
138 | + * @param string $class |
|
139 | + * @param array $dependencies |
|
140 | + * @return boolean |
|
141 | + */ |
|
142 | + public static function register_dependencies($class, $dependencies) |
|
143 | + { |
|
144 | + if (! isset(self::$_instance->_dependency_map[$class])) { |
|
145 | + // we need to make sure that any aliases used when registering a dependency |
|
146 | + // get resolved to the correct class name |
|
147 | + foreach ((array)$dependencies as $dependency => $load_source) { |
|
148 | + $alias = self::$_instance->get_alias($dependency); |
|
149 | + unset($dependencies[$dependency]); |
|
150 | + $dependencies[$alias] = $load_source; |
|
151 | + } |
|
152 | + self::$_instance->_dependency_map[$class] = (array)$dependencies; |
|
153 | + return true; |
|
154 | + } |
|
155 | + return false; |
|
156 | + } |
|
157 | + |
|
158 | + |
|
159 | + |
|
160 | + /** |
|
161 | + * @param string $class_name |
|
162 | + * @param string $loader |
|
163 | + * @return bool |
|
164 | + * @throws EE_Error |
|
165 | + */ |
|
166 | + public static function register_class_loader($class_name, $loader = 'load_core') |
|
167 | + { |
|
168 | + // check that loader is callable or method starts with "load_" and exists in EE_Registry |
|
169 | + if ( |
|
170 | + ! is_callable($loader) |
|
171 | + && ( |
|
172 | + strpos($loader, 'load_') !== 0 |
|
173 | + || ! method_exists('EE_Registry', $loader) |
|
174 | + ) |
|
175 | + ) { |
|
176 | + throw new EE_Error( |
|
177 | + sprintf( |
|
178 | + esc_html__('"%1$s" is not a valid loader method on EE_Registry.', 'event_espresso'), |
|
179 | + $loader |
|
180 | + ) |
|
181 | + ); |
|
182 | + } |
|
183 | + $class_name = self::$_instance->get_alias($class_name); |
|
184 | + if (! isset(self::$_instance->_class_loaders[$class_name])) { |
|
185 | + self::$_instance->_class_loaders[$class_name] = $loader; |
|
186 | + return true; |
|
187 | + } |
|
188 | + return false; |
|
189 | + } |
|
190 | + |
|
191 | + |
|
192 | + |
|
193 | + /** |
|
194 | + * @return array |
|
195 | + */ |
|
196 | + public function dependency_map() |
|
197 | + { |
|
198 | + return $this->_dependency_map; |
|
199 | + } |
|
200 | + |
|
201 | + |
|
202 | + |
|
203 | + /** |
|
204 | + * returns TRUE if dependency map contains a listing for the provided class name |
|
205 | + * |
|
206 | + * @param string $class_name |
|
207 | + * @return boolean |
|
208 | + */ |
|
209 | + public function has($class_name = '') |
|
210 | + { |
|
211 | + return isset($this->_dependency_map[$class_name]) ? true : false; |
|
212 | + } |
|
213 | + |
|
214 | + |
|
215 | + |
|
216 | + /** |
|
217 | + * returns TRUE if dependency map contains a listing for the provided class name AND dependency |
|
218 | + * |
|
219 | + * @param string $class_name |
|
220 | + * @param string $dependency |
|
221 | + * @return bool |
|
222 | + */ |
|
223 | + public function has_dependency_for_class($class_name = '', $dependency = '') |
|
224 | + { |
|
225 | + $dependency = $this->get_alias($dependency); |
|
226 | + return isset($this->_dependency_map[$class_name], $this->_dependency_map[$class_name][$dependency]) |
|
227 | + ? true |
|
228 | + : false; |
|
229 | + } |
|
230 | + |
|
231 | + |
|
232 | + |
|
233 | + /** |
|
234 | + * returns loading strategy for whether a previously cached dependency should be loaded or a new instance returned |
|
235 | + * |
|
236 | + * @param string $class_name |
|
237 | + * @param string $dependency |
|
238 | + * @return int |
|
239 | + */ |
|
240 | + public function loading_strategy_for_class_dependency($class_name = '', $dependency = '') |
|
241 | + { |
|
242 | + $dependency = $this->get_alias($dependency); |
|
243 | + return $this->has_dependency_for_class($class_name, $dependency) |
|
244 | + ? $this->_dependency_map[$class_name][$dependency] |
|
245 | + : EE_Dependency_Map::not_registered; |
|
246 | + } |
|
247 | + |
|
248 | + |
|
249 | + |
|
250 | + /** |
|
251 | + * @param string $class_name |
|
252 | + * @return string | Closure |
|
253 | + */ |
|
254 | + public function class_loader($class_name) |
|
255 | + { |
|
256 | + $class_name = $this->get_alias($class_name); |
|
257 | + return isset($this->_class_loaders[$class_name]) ? $this->_class_loaders[$class_name] : ''; |
|
258 | + } |
|
259 | + |
|
260 | + |
|
261 | + |
|
262 | + /** |
|
263 | + * @return array |
|
264 | + */ |
|
265 | + public function class_loaders() |
|
266 | + { |
|
267 | + return $this->_class_loaders; |
|
268 | + } |
|
269 | + |
|
270 | + |
|
271 | + |
|
272 | + /** |
|
273 | + * adds an alias for a classname |
|
274 | + * |
|
275 | + * @param string $class_name the class name that should be used (concrete class to replace interface) |
|
276 | + * @param string $alias the class name that would be type hinted for (abstract parent or interface) |
|
277 | + * @param string $for_class the class that has the dependency (is type hinting for the interface) |
|
278 | + */ |
|
279 | + public function add_alias($class_name, $alias, $for_class = '') |
|
280 | + { |
|
281 | + if ($for_class !== '') { |
|
282 | + if (! isset($this->_aliases[$for_class])) { |
|
283 | + $this->_aliases[$for_class] = array(); |
|
284 | + } |
|
285 | + $this->_aliases[$for_class][$class_name] = $alias; |
|
286 | + } |
|
287 | + $this->_aliases[$class_name] = $alias; |
|
288 | + } |
|
289 | + |
|
290 | + |
|
291 | + |
|
292 | + /** |
|
293 | + * returns TRUE if the provided class name has an alias |
|
294 | + * |
|
295 | + * @param string $class_name |
|
296 | + * @param string $for_class |
|
297 | + * @return bool |
|
298 | + */ |
|
299 | + public function has_alias($class_name = '', $for_class = '') |
|
300 | + { |
|
301 | + return isset($this->_aliases[$for_class], $this->_aliases[$for_class][$class_name]) |
|
302 | + || ( |
|
303 | + isset($this->_aliases[$class_name]) |
|
304 | + && ! is_array($this->_aliases[$class_name]) |
|
305 | + ); |
|
306 | + } |
|
307 | + |
|
308 | + |
|
309 | + |
|
310 | + /** |
|
311 | + * returns alias for class name if one exists, otherwise returns the original classname |
|
312 | + * functions recursively, so that multiple aliases can be used to drill down to a classname |
|
313 | + * for example: |
|
314 | + * if the following two entries were added to the _aliases array: |
|
315 | + * array( |
|
316 | + * 'interface_alias' => 'some\namespace\interface' |
|
317 | + * 'some\namespace\interface' => 'some\namespace\classname' |
|
318 | + * ) |
|
319 | + * then one could use EE_Registry::instance()->create( 'interface_alias' ) |
|
320 | + * to load an instance of 'some\namespace\classname' |
|
321 | + * |
|
322 | + * @param string $class_name |
|
323 | + * @param string $for_class |
|
324 | + * @return string |
|
325 | + */ |
|
326 | + public function get_alias($class_name = '', $for_class = '') |
|
327 | + { |
|
328 | + if (! $this->has_alias($class_name, $for_class)) { |
|
329 | + return $class_name; |
|
330 | + } |
|
331 | + if ($for_class !== '') { |
|
332 | + return $this->get_alias($this->_aliases[$for_class][$class_name], $for_class); |
|
333 | + } |
|
334 | + return $this->get_alias($this->_aliases[$class_name]); |
|
335 | + } |
|
336 | + |
|
337 | + |
|
338 | + |
|
339 | + /** |
|
340 | + * Registers the core dependencies and whether a previously instantiated object should be loaded from the cache, |
|
341 | + * if one exists, or whether a new object should be generated every time the requested class is loaded. |
|
342 | + * This is done by using the following class constants: |
|
343 | + * EE_Dependency_Map::load_from_cache - loads previously instantiated object |
|
344 | + * EE_Dependency_Map::load_new_object - generates a new object every time |
|
345 | + */ |
|
346 | + protected function _register_core_dependencies() |
|
347 | + { |
|
348 | + $this->_dependency_map = array( |
|
349 | + 'EE_Request_Handler' => array( |
|
350 | + 'EE_Request' => EE_Dependency_Map::load_from_cache, |
|
351 | + ), |
|
352 | + 'EE_System' => array( |
|
353 | + 'EE_Registry' => EE_Dependency_Map::load_from_cache, |
|
354 | + ), |
|
355 | + 'EE_Session' => array( |
|
356 | + 'EventEspresso\core\services\cache\TransientCacheStorage' => EE_Dependency_Map::load_from_cache, |
|
357 | + 'EE_Encryption' => EE_Dependency_Map::load_from_cache, |
|
358 | + ), |
|
359 | + 'EE_Cart' => array( |
|
360 | + 'EE_Session' => EE_Dependency_Map::load_from_cache, |
|
361 | + ), |
|
362 | + 'EE_Front_Controller' => array( |
|
363 | + 'EE_Registry' => EE_Dependency_Map::load_from_cache, |
|
364 | + 'EE_Request_Handler' => EE_Dependency_Map::load_from_cache, |
|
365 | + 'EE_Module_Request_Router' => EE_Dependency_Map::load_from_cache, |
|
366 | + ), |
|
367 | + 'EE_Messenger_Collection_Loader' => array( |
|
368 | + 'EE_Messenger_Collection' => EE_Dependency_Map::load_new_object, |
|
369 | + ), |
|
370 | + 'EE_Message_Type_Collection_Loader' => array( |
|
371 | + 'EE_Message_Type_Collection' => EE_Dependency_Map::load_new_object, |
|
372 | + ), |
|
373 | + 'EE_Message_Resource_Manager' => array( |
|
374 | + 'EE_Messenger_Collection_Loader' => EE_Dependency_Map::load_new_object, |
|
375 | + 'EE_Message_Type_Collection_Loader' => EE_Dependency_Map::load_new_object, |
|
376 | + 'EEM_Message_Template_Group' => EE_Dependency_Map::load_from_cache, |
|
377 | + ), |
|
378 | + 'EE_Message_Factory' => array( |
|
379 | + 'EE_Message_Resource_Manager' => EE_Dependency_Map::load_from_cache, |
|
380 | + ), |
|
381 | + 'EE_messages' => array( |
|
382 | + 'EE_Message_Resource_Manager' => EE_Dependency_Map::load_from_cache, |
|
383 | + ), |
|
384 | + 'EE_Messages_Generator' => array( |
|
385 | + 'EE_Messages_Queue' => EE_Dependency_Map::load_new_object, |
|
386 | + 'EE_Messages_Data_Handler_Collection' => EE_Dependency_Map::load_new_object, |
|
387 | + 'EE_Message_Template_Group_Collection' => EE_Dependency_Map::load_new_object, |
|
388 | + 'EEH_Parse_Shortcodes' => EE_Dependency_Map::load_from_cache, |
|
389 | + ), |
|
390 | + 'EE_Messages_Processor' => array( |
|
391 | + 'EE_Message_Resource_Manager' => EE_Dependency_Map::load_from_cache, |
|
392 | + ), |
|
393 | + 'EE_Messages_Queue' => array( |
|
394 | + 'EE_Message_Repository' => EE_Dependency_Map::load_new_object, |
|
395 | + ), |
|
396 | + 'EE_Messages_Template_Defaults' => array( |
|
397 | + 'EEM_Message_Template_Group' => EE_Dependency_Map::load_from_cache, |
|
398 | + 'EEM_Message_Template' => EE_Dependency_Map::load_from_cache, |
|
399 | + ), |
|
400 | + 'EE_Message_To_Generate_From_Request' => array( |
|
401 | + 'EE_Message_Resource_Manager' => EE_Dependency_Map::load_from_cache, |
|
402 | + 'EE_Request_Handler' => EE_Dependency_Map::load_from_cache, |
|
403 | + ), |
|
404 | + 'EventEspresso\core\services\commands\CommandBus' => array( |
|
405 | + 'EventEspresso\core\services\commands\CommandHandlerManager' => EE_Dependency_Map::load_from_cache, |
|
406 | + ), |
|
407 | + 'EventEspresso\services\commands\CommandHandler' => array( |
|
408 | + 'EE_Registry' => EE_Dependency_Map::load_from_cache, |
|
409 | + 'CommandBusInterface' => EE_Dependency_Map::load_from_cache, |
|
410 | + ), |
|
411 | + 'EventEspresso\core\services\commands\CommandHandlerManager' => array( |
|
412 | + 'EventEspresso\core\services\loaders\Loader' => EE_Dependency_Map::load_from_cache, |
|
413 | + ), |
|
414 | + 'EventEspresso\core\services\commands\CompositeCommandHandler' => array( |
|
415 | + 'EventEspresso\core\services\commands\CommandBus' => EE_Dependency_Map::load_from_cache, |
|
416 | + 'EventEspresso\core\services\commands\CommandFactory' => EE_Dependency_Map::load_from_cache, |
|
417 | + ), |
|
418 | + 'EventEspresso\core\services\commands\CommandFactory' => array( |
|
419 | + 'EventEspresso\core\services\loaders\Loader' => EE_Dependency_Map::load_from_cache, |
|
420 | + ), |
|
421 | + 'EventEspresso\core\services\commands\middleware\CapChecker' => array( |
|
422 | + 'EventEspresso\core\domain\services\capabilities\CapabilitiesChecker' => EE_Dependency_Map::load_from_cache, |
|
423 | + ), |
|
424 | + 'EventEspresso\core\domain\services\capabilities\CapabilitiesChecker' => array( |
|
425 | + 'EE_Capabilities' => EE_Dependency_Map::load_from_cache, |
|
426 | + ), |
|
427 | + 'EventEspresso\core\domain\services\capabilities\RegistrationsCapChecker' => array( |
|
428 | + 'EE_Capabilities' => EE_Dependency_Map::load_from_cache, |
|
429 | + ), |
|
430 | + 'EventEspresso\core\services\commands\registration\CreateRegistrationCommandHandler' => array( |
|
431 | + 'EventEspresso\core\domain\services\registration\CreateRegistrationService' => EE_Dependency_Map::load_from_cache, |
|
432 | + ), |
|
433 | + 'EventEspresso\core\services\commands\registration\CopyRegistrationDetailsCommandHandler' => array( |
|
434 | + 'EventEspresso\core\domain\services\registration\CopyRegistrationService' => EE_Dependency_Map::load_from_cache, |
|
435 | + ), |
|
436 | + 'EventEspresso\core\services\commands\registration\CopyRegistrationPaymentsCommandHandler' => array( |
|
437 | + 'EventEspresso\core\domain\services\registration\CopyRegistrationService' => EE_Dependency_Map::load_from_cache, |
|
438 | + ), |
|
439 | + 'EventEspresso\core\services\commands\registration\CancelRegistrationAndTicketLineItemCommandHandler' => array( |
|
440 | + 'EventEspresso\core\domain\services\registration\CancelTicketLineItemService' => EE_Dependency_Map::load_from_cache, |
|
441 | + ), |
|
442 | + 'EventEspresso\core\services\commands\registration\UpdateRegistrationAndTransactionAfterChangeCommandHandler' => array( |
|
443 | + 'EventEspresso\core\domain\services\registration\UpdateRegistrationService' => EE_Dependency_Map::load_from_cache, |
|
444 | + ), |
|
445 | + 'EventEspresso\core\services\commands\ticket\CreateTicketLineItemCommandHandler' => array( |
|
446 | + 'EventEspresso\core\domain\services\ticket\CreateTicketLineItemService' => EE_Dependency_Map::load_from_cache, |
|
447 | + ), |
|
448 | + 'EventEspresso\core\services\commands\ticket\CancelTicketLineItemCommandHandler' => array( |
|
449 | + 'EventEspresso\core\domain\services\ticket\CancelTicketLineItemService' => EE_Dependency_Map::load_from_cache, |
|
450 | + ), |
|
451 | + 'EventEspresso\core\domain\services\registration\CancelRegistrationService' => array( |
|
452 | + 'EventEspresso\core\domain\services\ticket\CancelTicketLineItemService' => EE_Dependency_Map::load_from_cache, |
|
453 | + ), |
|
454 | + 'EventEspresso\core\services\database\TableManager' => array( |
|
455 | + 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
456 | + ), |
|
457 | + 'EE_Data_Migration_Class_Base' => array( |
|
458 | + 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
459 | + 'EventEspresso\core\services\database\TableManager' => EE_Dependency_Map::load_from_cache, |
|
460 | + ), |
|
461 | + 'EE_DMS_Core_4_1_0' => array( |
|
462 | + 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
463 | + 'EventEspresso\core\services\database\TableManager' => EE_Dependency_Map::load_from_cache, |
|
464 | + ), |
|
465 | + 'EE_DMS_Core_4_2_0' => array( |
|
466 | + 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
467 | + 'EventEspresso\core\services\database\TableManager' => EE_Dependency_Map::load_from_cache, |
|
468 | + ), |
|
469 | + 'EE_DMS_Core_4_3_0' => array( |
|
470 | + 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
471 | + 'EventEspresso\core\services\database\TableManager' => EE_Dependency_Map::load_from_cache, |
|
472 | + ), |
|
473 | + 'EE_DMS_Core_4_4_0' => array( |
|
474 | + 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
475 | + 'EventEspresso\core\services\database\TableManager' => EE_Dependency_Map::load_from_cache, |
|
476 | + ), |
|
477 | + 'EE_DMS_Core_4_5_0' => array( |
|
478 | + 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
479 | + 'EventEspresso\core\services\database\TableManager' => EE_Dependency_Map::load_from_cache, |
|
480 | + ), |
|
481 | + 'EE_DMS_Core_4_6_0' => array( |
|
482 | + 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
483 | + 'EventEspresso\core\services\database\TableManager' => EE_Dependency_Map::load_from_cache, |
|
484 | + ), |
|
485 | + 'EE_DMS_Core_4_7_0' => array( |
|
486 | + 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
487 | + 'EventEspresso\core\services\database\TableManager' => EE_Dependency_Map::load_from_cache, |
|
488 | + ), |
|
489 | + 'EE_DMS_Core_4_8_0' => array( |
|
490 | + 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
491 | + 'EventEspresso\core\services\database\TableManager' => EE_Dependency_Map::load_from_cache, |
|
492 | + ), |
|
493 | + 'EE_DMS_Core_4_9_0' => array( |
|
494 | + 'EventEspresso\core\services\database\TableAnalysis' => EE_Dependency_Map::load_from_cache, |
|
495 | + 'EventEspresso\core\services\database\TableManager' => EE_Dependency_Map::load_from_cache, |
|
496 | + ), |
|
497 | + 'EventEspresso\core\services\assets\Registry' => array( |
|
498 | + 'EE_Template_Config' => EE_Dependency_Map::load_from_cache, |
|
499 | + 'EE_Currency_Config' => EE_Dependency_Map::load_from_cache, |
|
500 | + ), |
|
501 | + 'EventEspresso\core\domain\entities\shortcodes\EspressoCancelled' => array( |
|
502 | + 'EventEspresso\core\services\cache\PostRelatedCacheManager' => EE_Dependency_Map::load_from_cache, |
|
503 | + ), |
|
504 | + 'EventEspresso\core\domain\entities\shortcodes\EspressoCheckout' => array( |
|
505 | + 'EventEspresso\core\services\cache\PostRelatedCacheManager' => EE_Dependency_Map::load_from_cache, |
|
506 | + ), |
|
507 | + 'EventEspresso\core\domain\entities\shortcodes\EspressoEventAttendees' => array( |
|
508 | + 'EventEspresso\core\services\cache\PostRelatedCacheManager' => EE_Dependency_Map::load_from_cache, |
|
509 | + ), |
|
510 | + 'EventEspresso\core\domain\entities\shortcodes\EspressoEvents' => array( |
|
511 | + 'EventEspresso\core\services\cache\PostRelatedCacheManager' => EE_Dependency_Map::load_from_cache, |
|
512 | + ), |
|
513 | + 'EventEspresso\core\domain\entities\shortcodes\EspressoThankYou' => array( |
|
514 | + 'EventEspresso\core\services\cache\PostRelatedCacheManager' => EE_Dependency_Map::load_from_cache, |
|
515 | + ), |
|
516 | + 'EventEspresso\core\domain\entities\shortcodes\EspressoTicketSelector' => array( |
|
517 | + 'EventEspresso\core\services\cache\PostRelatedCacheManager' => EE_Dependency_Map::load_from_cache, |
|
518 | + ), |
|
519 | + 'EventEspresso\core\domain\entities\shortcodes\EspressoTxnPage' => array( |
|
520 | + 'EventEspresso\core\services\cache\PostRelatedCacheManager' => EE_Dependency_Map::load_from_cache, |
|
521 | + ), |
|
522 | + 'EventEspresso\core\services\cache\BasicCacheManager' => array( |
|
523 | + 'EventEspresso\core\services\cache\TransientCacheStorage' => EE_Dependency_Map::load_from_cache, |
|
524 | + ), |
|
525 | + 'EventEspresso\core\services\cache\PostRelatedCacheManager' => array( |
|
526 | + 'EventEspresso\core\services\cache\TransientCacheStorage' => EE_Dependency_Map::load_from_cache, |
|
527 | + ), |
|
528 | + 'EventEspresso\core\domain\services\validation\EmailValidationServiceInterface' => array( |
|
529 | + 'EE_Config' => EE_Dependency_Map::load_from_cache, |
|
530 | + 'EventEspresso\core\services\loaders\Loader' => EE_Dependency_Map::load_from_cache |
|
531 | + ) |
|
532 | + ); |
|
533 | + } |
|
534 | + |
|
535 | + |
|
536 | + |
|
537 | + /** |
|
538 | + * Registers how core classes are loaded. |
|
539 | + * This can either be done by simply providing the name of one of the EE_Registry loader methods such as: |
|
540 | + * 'EE_Request_Handler' => 'load_core' |
|
541 | + * 'EE_Messages_Queue' => 'load_lib' |
|
542 | + * 'EEH_Debug_Tools' => 'load_helper' |
|
543 | + * or, if greater control is required, by providing a custom closure. For example: |
|
544 | + * 'Some_Class' => function () { |
|
545 | + * return new Some_Class(); |
|
546 | + * }, |
|
547 | + * This is required for instantiating dependencies |
|
548 | + * where an interface has been type hinted in a class constructor. For example: |
|
549 | + * 'Required_Interface' => function () { |
|
550 | + * return new A_Class_That_Implements_Required_Interface(); |
|
551 | + * }, |
|
552 | + */ |
|
553 | + protected function _register_core_class_loaders() |
|
554 | + { |
|
555 | + //for PHP5.3 compat, we need to register any properties called here in a variable because `$this` cannot |
|
556 | + //be used in a closure. |
|
557 | + $request = &$this->_request; |
|
558 | + $response = &$this->_response; |
|
559 | + $loader = &$this->loader; |
|
560 | + $this->_class_loaders = array( |
|
561 | + //load_core |
|
562 | + 'EE_Capabilities' => 'load_core', |
|
563 | + 'EE_Encryption' => 'load_core', |
|
564 | + 'EE_Front_Controller' => 'load_core', |
|
565 | + 'EE_Module_Request_Router' => 'load_core', |
|
566 | + 'EE_Registry' => 'load_core', |
|
567 | + 'EE_Request' => function () use (&$request) { |
|
568 | + return $request; |
|
569 | + }, |
|
570 | + 'EE_Response' => function () use (&$response) { |
|
571 | + return $response; |
|
572 | + }, |
|
573 | + 'EE_Request_Handler' => 'load_core', |
|
574 | + 'EE_Session' => 'load_core', |
|
575 | + 'EE_System' => 'load_core', |
|
576 | + //load_lib |
|
577 | + 'EE_Message_Resource_Manager' => 'load_lib', |
|
578 | + 'EE_Message_Type_Collection' => 'load_lib', |
|
579 | + 'EE_Message_Type_Collection_Loader' => 'load_lib', |
|
580 | + 'EE_Messenger_Collection' => 'load_lib', |
|
581 | + 'EE_Messenger_Collection_Loader' => 'load_lib', |
|
582 | + 'EE_Messages_Processor' => 'load_lib', |
|
583 | + 'EE_Message_Repository' => 'load_lib', |
|
584 | + 'EE_Messages_Queue' => 'load_lib', |
|
585 | + 'EE_Messages_Data_Handler_Collection' => 'load_lib', |
|
586 | + 'EE_Message_Template_Group_Collection' => 'load_lib', |
|
587 | + 'EE_Messages_Generator' => function () { |
|
588 | + return EE_Registry::instance()->load_lib( |
|
589 | + 'Messages_Generator', |
|
590 | + array(), |
|
591 | + false, |
|
592 | + false |
|
593 | + ); |
|
594 | + }, |
|
595 | + 'EE_Messages_Template_Defaults' => function ($arguments = array()) { |
|
596 | + return EE_Registry::instance()->load_lib( |
|
597 | + 'Messages_Template_Defaults', |
|
598 | + $arguments, |
|
599 | + false, |
|
600 | + false |
|
601 | + ); |
|
602 | + }, |
|
603 | + //load_model |
|
604 | + 'EEM_Message_Template_Group' => 'load_model', |
|
605 | + 'EEM_Message_Template' => 'load_model', |
|
606 | + //load_helper |
|
607 | + 'EEH_Parse_Shortcodes' => function () { |
|
608 | + if (EE_Registry::instance()->load_helper('Parse_Shortcodes')) { |
|
609 | + return new EEH_Parse_Shortcodes(); |
|
610 | + } |
|
611 | + return null; |
|
612 | + }, |
|
613 | + 'EE_Template_Config' => function () { |
|
614 | + return EE_Config::instance()->template_settings; |
|
615 | + }, |
|
616 | + 'EE_Currency_Config' => function () { |
|
617 | + return EE_Config::instance()->currency; |
|
618 | + }, |
|
619 | + 'EventEspresso\core\services\loaders\Loader' => function () use (&$loader) { |
|
620 | + return $loader; |
|
621 | + }, |
|
622 | + ); |
|
623 | + } |
|
624 | + |
|
625 | + |
|
626 | + |
|
627 | + /** |
|
628 | + * can be used for supplying alternate names for classes, |
|
629 | + * or for connecting interface names to instantiable classes |
|
630 | + */ |
|
631 | + protected function _register_core_aliases() |
|
632 | + { |
|
633 | + $this->_aliases = array( |
|
634 | + 'CommandBusInterface' => 'EventEspresso\core\services\commands\CommandBusInterface', |
|
635 | + 'EventEspresso\core\services\commands\CommandBusInterface' => 'EventEspresso\core\services\commands\CommandBus', |
|
636 | + 'CommandHandlerManagerInterface' => 'EventEspresso\core\services\commands\CommandHandlerManagerInterface', |
|
637 | + 'EventEspresso\core\services\commands\CommandHandlerManagerInterface' => 'EventEspresso\core\services\commands\CommandHandlerManager', |
|
638 | + 'CapChecker' => 'EventEspresso\core\services\commands\middleware\CapChecker', |
|
639 | + 'AddActionHook' => 'EventEspresso\core\services\commands\middleware\AddActionHook', |
|
640 | + 'CapabilitiesChecker' => 'EventEspresso\core\domain\services\capabilities\CapabilitiesChecker', |
|
641 | + 'CapabilitiesCheckerInterface' => 'EventEspresso\core\domain\services\capabilities\CapabilitiesCheckerInterface', |
|
642 | + 'EventEspresso\core\domain\services\capabilities\CapabilitiesCheckerInterface' => 'EventEspresso\core\domain\services\capabilities\CapabilitiesChecker', |
|
643 | + 'CreateRegistrationService' => 'EventEspresso\core\domain\services\registration\CreateRegistrationService', |
|
644 | + 'CreateRegCodeCommandHandler' => 'EventEspresso\core\services\commands\registration\CreateRegCodeCommand', |
|
645 | + 'CreateRegUrlLinkCommandHandler' => 'EventEspresso\core\services\commands\registration\CreateRegUrlLinkCommand', |
|
646 | + 'CreateRegistrationCommandHandler' => 'EventEspresso\core\services\commands\registration\CreateRegistrationCommand', |
|
647 | + 'CopyRegistrationDetailsCommandHandler' => 'EventEspresso\core\services\commands\registration\CopyRegistrationDetailsCommand', |
|
648 | + 'CopyRegistrationPaymentsCommandHandler' => 'EventEspresso\core\services\commands\registration\CopyRegistrationPaymentsCommand', |
|
649 | + 'CancelRegistrationAndTicketLineItemCommandHandler' => 'EventEspresso\core\services\commands\registration\CancelRegistrationAndTicketLineItemCommandHandler', |
|
650 | + 'UpdateRegistrationAndTransactionAfterChangeCommandHandler' => 'EventEspresso\core\services\commands\registration\UpdateRegistrationAndTransactionAfterChangeCommandHandler', |
|
651 | + 'CreateTicketLineItemCommandHandler' => 'EventEspresso\core\services\commands\ticket\CreateTicketLineItemCommand', |
|
652 | + 'TableManager' => 'EventEspresso\core\services\database\TableManager', |
|
653 | + 'TableAnalysis' => 'EventEspresso\core\services\database\TableAnalysis', |
|
654 | + 'EspressoShortcode' => 'EventEspresso\core\services\shortcodes\EspressoShortcode', |
|
655 | + 'ShortcodeInterface' => 'EventEspresso\core\services\shortcodes\ShortcodeInterface', |
|
656 | + 'EventEspresso\core\services\shortcodes\ShortcodeInterface' => 'EventEspresso\core\services\shortcodes\EspressoShortcode', |
|
657 | + 'EventEspresso\core\services\cache\CacheStorageInterface' => 'EventEspresso\core\services\cache\TransientCacheStorage', |
|
658 | + 'LoaderInterface' => 'EventEspresso\core\services\loaders\LoaderInterface', |
|
659 | + 'EventEspresso\core\services\loaders\LoaderInterface' => 'EventEspresso\core\services\loaders\Loader', |
|
660 | + 'CommandFactoryInterface' => 'EventEspresso\core\services\commands\CommandFactoryInterface', |
|
661 | + 'EventEspresso\core\services\commands\CommandFactoryInterface' => 'EventEspresso\core\services\commands\CommandFactory', |
|
662 | + 'EventEspresso\core\domain\services\validation\EmailValidationServiceInterface' => 'EventEspresso\core\services\validation\EmailValidationService', |
|
663 | + ); |
|
664 | + } |
|
665 | + |
|
666 | + |
|
667 | + |
|
668 | + /** |
|
669 | + * This is used to reset the internal map and class_loaders to their original default state at the beginning of the |
|
670 | + * request Primarily used by unit tests. |
|
671 | + */ |
|
672 | + public function reset() |
|
673 | + { |
|
674 | + $this->_register_core_class_loaders(); |
|
675 | + $this->_register_core_dependencies(); |
|
676 | + } |
|
677 | 677 | |
678 | 678 | |
679 | 679 | } |
@@ -25,69 +25,69 @@ |
||
25 | 25 | |
26 | 26 | |
27 | 27 | |
28 | - /** |
|
29 | - * @var EE_Config $config |
|
30 | - */ |
|
31 | - protected $config; |
|
32 | - |
|
33 | - /** |
|
34 | - * @var Loader $loader |
|
35 | - */ |
|
36 | - protected $loader; |
|
37 | - |
|
38 | - |
|
39 | - |
|
40 | - /** |
|
41 | - * EmailValidationService constructor. |
|
42 | - * Accepts an \EE_Config as an argument. |
|
43 | - * |
|
44 | - * @param EE_Config $config |
|
45 | - * @param Loader $loader |
|
46 | - */ |
|
47 | - public function __construct(EE_Config $config, Loader $loader) |
|
48 | - { |
|
49 | - $this->config = $config; |
|
50 | - $this->loader = $loader; |
|
51 | - } |
|
52 | - |
|
53 | - |
|
54 | - |
|
55 | - /** |
|
56 | - * Validates the email address. If it's invalid, an EmailValidationException |
|
57 | - * is thrown that describes why its invalid. |
|
58 | - * |
|
59 | - * @param $input |
|
60 | - * @return boolean |
|
61 | - * @throws EmailValidationException |
|
62 | - */ |
|
63 | - public function validate($input) |
|
64 | - { |
|
65 | - //pick the correct validator according to the config |
|
66 | - switch ($this->config->registration->email_validation_level) { |
|
67 | - case 'basic': |
|
68 | - $validator = $this->loader->getShared( |
|
69 | - 'EventEspresso\core\services\validation\strategies\Basic' |
|
70 | - ); |
|
71 | - break; |
|
72 | - case 'i18n': |
|
73 | - $validator = $this->loader->getShared( |
|
74 | - 'EventEspresso\core\services\validation\strategies\International' |
|
75 | - ) ; |
|
76 | - break; |
|
77 | - case 'i18n_dns': |
|
78 | - $validator = $this->loader->getShared( |
|
79 | - 'EventEspresso\core\services\validation\strategies\InternationalDNS' |
|
80 | - ) ; |
|
81 | - break; |
|
82 | - case 'wp_default': |
|
83 | - default: |
|
84 | - $validator = $this->loader->getShared( |
|
85 | - 'EventEspresso\core\services\validation\strategies\WordPress' |
|
86 | - ) ; |
|
87 | - break; |
|
88 | - } |
|
89 | - return $validator->validate($input); |
|
90 | - } |
|
28 | + /** |
|
29 | + * @var EE_Config $config |
|
30 | + */ |
|
31 | + protected $config; |
|
32 | + |
|
33 | + /** |
|
34 | + * @var Loader $loader |
|
35 | + */ |
|
36 | + protected $loader; |
|
37 | + |
|
38 | + |
|
39 | + |
|
40 | + /** |
|
41 | + * EmailValidationService constructor. |
|
42 | + * Accepts an \EE_Config as an argument. |
|
43 | + * |
|
44 | + * @param EE_Config $config |
|
45 | + * @param Loader $loader |
|
46 | + */ |
|
47 | + public function __construct(EE_Config $config, Loader $loader) |
|
48 | + { |
|
49 | + $this->config = $config; |
|
50 | + $this->loader = $loader; |
|
51 | + } |
|
52 | + |
|
53 | + |
|
54 | + |
|
55 | + /** |
|
56 | + * Validates the email address. If it's invalid, an EmailValidationException |
|
57 | + * is thrown that describes why its invalid. |
|
58 | + * |
|
59 | + * @param $input |
|
60 | + * @return boolean |
|
61 | + * @throws EmailValidationException |
|
62 | + */ |
|
63 | + public function validate($input) |
|
64 | + { |
|
65 | + //pick the correct validator according to the config |
|
66 | + switch ($this->config->registration->email_validation_level) { |
|
67 | + case 'basic': |
|
68 | + $validator = $this->loader->getShared( |
|
69 | + 'EventEspresso\core\services\validation\strategies\Basic' |
|
70 | + ); |
|
71 | + break; |
|
72 | + case 'i18n': |
|
73 | + $validator = $this->loader->getShared( |
|
74 | + 'EventEspresso\core\services\validation\strategies\International' |
|
75 | + ) ; |
|
76 | + break; |
|
77 | + case 'i18n_dns': |
|
78 | + $validator = $this->loader->getShared( |
|
79 | + 'EventEspresso\core\services\validation\strategies\InternationalDNS' |
|
80 | + ) ; |
|
81 | + break; |
|
82 | + case 'wp_default': |
|
83 | + default: |
|
84 | + $validator = $this->loader->getShared( |
|
85 | + 'EventEspresso\core\services\validation\strategies\WordPress' |
|
86 | + ) ; |
|
87 | + break; |
|
88 | + } |
|
89 | + return $validator->validate($input); |
|
90 | + } |
|
91 | 91 | } |
92 | 92 | // End of file EmailValidator.php |
93 | 93 | // Location: core\services\validation/EmailValidator.php |
@@ -72,18 +72,18 @@ |
||
72 | 72 | case 'i18n': |
73 | 73 | $validator = $this->loader->getShared( |
74 | 74 | 'EventEspresso\core\services\validation\strategies\International' |
75 | - ) ; |
|
75 | + ); |
|
76 | 76 | break; |
77 | 77 | case 'i18n_dns': |
78 | 78 | $validator = $this->loader->getShared( |
79 | 79 | 'EventEspresso\core\services\validation\strategies\InternationalDNS' |
80 | - ) ; |
|
80 | + ); |
|
81 | 81 | break; |
82 | 82 | case 'wp_default': |
83 | 83 | default: |
84 | 84 | $validator = $this->loader->getShared( |
85 | 85 | 'EventEspresso\core\services\validation\strategies\WordPress' |
86 | - ) ; |
|
86 | + ); |
|
87 | 87 | break; |
88 | 88 | } |
89 | 89 | return $validator->validate($input); |
@@ -19,13 +19,13 @@ |
||
19 | 19 | interface EmailValidationInterface |
20 | 20 | { |
21 | 21 | |
22 | - /** |
|
23 | - * Validates the email. If it is invalid, throws EmailValidationException |
|
24 | - * @param string $input |
|
25 | - * @return boolean |
|
26 | - * @throws EmailValidationException |
|
27 | - */ |
|
28 | - public function validate($input); |
|
22 | + /** |
|
23 | + * Validates the email. If it is invalid, throws EmailValidationException |
|
24 | + * @param string $input |
|
25 | + * @return boolean |
|
26 | + * @throws EmailValidationException |
|
27 | + */ |
|
28 | + public function validate($input); |
|
29 | 29 | } |
30 | 30 | // End of file EmailValidationInterface.php |
31 | 31 | // Location: core\services\validation\strategies/EmailValidationInterface.php |
@@ -19,22 +19,22 @@ |
||
19 | 19 | class WordPress extends Basic |
20 | 20 | { |
21 | 21 | |
22 | - /** |
|
23 | - * |
|
24 | - * @param $input |
|
25 | - * @return boolean |
|
26 | - * @throws EmailValidationException |
|
27 | - */ |
|
28 | - public function validate($input) |
|
29 | - { |
|
30 | - parent::validate($input); |
|
31 | - if( ! is_email($input)){ |
|
32 | - throw new EmailValidationException( |
|
33 | - esc_html__('The email address provided is not valid.', 'event_espresso') |
|
34 | - ); |
|
35 | - } |
|
36 | - return true; |
|
37 | - } |
|
22 | + /** |
|
23 | + * |
|
24 | + * @param $input |
|
25 | + * @return boolean |
|
26 | + * @throws EmailValidationException |
|
27 | + */ |
|
28 | + public function validate($input) |
|
29 | + { |
|
30 | + parent::validate($input); |
|
31 | + if( ! is_email($input)){ |
|
32 | + throw new EmailValidationException( |
|
33 | + esc_html__('The email address provided is not valid.', 'event_espresso') |
|
34 | + ); |
|
35 | + } |
|
36 | + return true; |
|
37 | + } |
|
38 | 38 | |
39 | 39 | |
40 | 40 | } |
@@ -28,7 +28,7 @@ |
||
28 | 28 | public function validate($input) |
29 | 29 | { |
30 | 30 | parent::validate($input); |
31 | - if( ! is_email($input)){ |
|
31 | + if ( ! is_email($input)) { |
|
32 | 32 | throw new EmailValidationException( |
33 | 33 | esc_html__('The email address provided is not valid.', 'event_espresso') |
34 | 34 | ); |
@@ -20,41 +20,41 @@ |
||
20 | 20 | class InternationalDNS extends International |
21 | 21 | { |
22 | 22 | |
23 | - /** |
|
24 | - * Validates the email in teh same way as the parent, but also |
|
25 | - * verifies the domain exists. |
|
26 | - * @param string $input |
|
27 | - * @return bool |
|
28 | - * @throws EmailValidationException |
|
29 | - */ |
|
30 | - public function validate($input) |
|
31 | - { |
|
32 | - parent::validate($input); |
|
33 | - $domain = $this->getDomainPartOfEmail($input); |
|
34 | - if (! checkdnsrr($domain, "MX")) { |
|
35 | - // domain not found in MX records |
|
36 | - throw new EmailValidationException( |
|
37 | - __( |
|
38 | - // @codingStandardsIgnoreStart |
|
39 | - 'Although the email address provided is formatted correctly, a valid "MX record" could not be located for that address and domain. Please enter a valid email address.', |
|
40 | - // @codingStandardsIgnoreEnd |
|
41 | - 'event_espresso' |
|
42 | - ) |
|
43 | - ); |
|
44 | - } |
|
45 | - if (! checkdnsrr($domain, "A")) { |
|
46 | - // domain not found in A records |
|
47 | - throw new EmailValidationException( |
|
48 | - __( |
|
49 | - // @codingStandardsIgnoreStart |
|
50 | - 'Although the email address provided is formatted correctly, a valid "A record" could not be located for that address and domain. Please enter a valid email address.', |
|
51 | - // @codingStandardsIgnoreEnd |
|
52 | - 'event_espresso' |
|
53 | - ) |
|
54 | - ); |
|
55 | - } |
|
56 | - return true; |
|
57 | - } |
|
23 | + /** |
|
24 | + * Validates the email in teh same way as the parent, but also |
|
25 | + * verifies the domain exists. |
|
26 | + * @param string $input |
|
27 | + * @return bool |
|
28 | + * @throws EmailValidationException |
|
29 | + */ |
|
30 | + public function validate($input) |
|
31 | + { |
|
32 | + parent::validate($input); |
|
33 | + $domain = $this->getDomainPartOfEmail($input); |
|
34 | + if (! checkdnsrr($domain, "MX")) { |
|
35 | + // domain not found in MX records |
|
36 | + throw new EmailValidationException( |
|
37 | + __( |
|
38 | + // @codingStandardsIgnoreStart |
|
39 | + 'Although the email address provided is formatted correctly, a valid "MX record" could not be located for that address and domain. Please enter a valid email address.', |
|
40 | + // @codingStandardsIgnoreEnd |
|
41 | + 'event_espresso' |
|
42 | + ) |
|
43 | + ); |
|
44 | + } |
|
45 | + if (! checkdnsrr($domain, "A")) { |
|
46 | + // domain not found in A records |
|
47 | + throw new EmailValidationException( |
|
48 | + __( |
|
49 | + // @codingStandardsIgnoreStart |
|
50 | + 'Although the email address provided is formatted correctly, a valid "A record" could not be located for that address and domain. Please enter a valid email address.', |
|
51 | + // @codingStandardsIgnoreEnd |
|
52 | + 'event_espresso' |
|
53 | + ) |
|
54 | + ); |
|
55 | + } |
|
56 | + return true; |
|
57 | + } |
|
58 | 58 | } |
59 | 59 | // End of file EmailValidationInternationalDNS.php |
60 | 60 | // Location: core\services\validation/EmailValidationInternationalDNS.php |
61 | 61 | \ No newline at end of file |
@@ -31,7 +31,7 @@ discard block |
||
31 | 31 | { |
32 | 32 | parent::validate($input); |
33 | 33 | $domain = $this->getDomainPartOfEmail($input); |
34 | - if (! checkdnsrr($domain, "MX")) { |
|
34 | + if ( ! checkdnsrr($domain, "MX")) { |
|
35 | 35 | // domain not found in MX records |
36 | 36 | throw new EmailValidationException( |
37 | 37 | __( |
@@ -42,7 +42,7 @@ discard block |
||
42 | 42 | ) |
43 | 43 | ); |
44 | 44 | } |
45 | - if (! checkdnsrr($domain, "A")) { |
|
45 | + if ( ! checkdnsrr($domain, "A")) { |
|
46 | 46 | // domain not found in A records |
47 | 47 | throw new EmailValidationException( |
48 | 48 | __( |
@@ -20,102 +20,102 @@ |
||
20 | 20 | class Basic implements EmailValidationInterface |
21 | 21 | { |
22 | 22 | |
23 | - /** |
|
24 | - * @param string $input |
|
25 | - * @return bool |
|
26 | - * @throws EmailValidationException |
|
27 | - */ |
|
28 | - public function validate($input) |
|
29 | - { |
|
30 | - if (! preg_match('/^.+\@\S+$/', $input)) { |
|
31 | - // email not in correct {string}@{string} format |
|
32 | - throw new EmailValidationException( |
|
33 | - esc_html__('Email does not have the required @ sign.', 'event_espresso') |
|
34 | - ); |
|
35 | - } else { |
|
36 | - $domain = $this->getDomainPartOfEmail($input); |
|
37 | - $local = $this->getLocalPartOfEmail($input); |
|
38 | - $localLen = strlen($local); |
|
39 | - $domainLen = strlen($domain); |
|
40 | - if ($localLen < 1) { |
|
41 | - //no local part |
|
42 | - throw new EmailValidationException( |
|
43 | - esc_html__('Email local-part (before the @) is required.', 'event_espresso') |
|
44 | - ); |
|
45 | - } |
|
46 | - if ($localLen > 64) { |
|
47 | - // local part length exceeded |
|
48 | - throw new EmailValidationException( |
|
49 | - esc_html__('Email local-part (before the @) is too long.', 'event_espresso') |
|
50 | - ); |
|
51 | - } |
|
52 | - if ($domainLen < 1) { |
|
53 | - throw new EmailValidationException( |
|
54 | - esc_html__('Email domain (after the @) is required.', 'event_espresso') |
|
55 | - ); |
|
56 | - } |
|
57 | - if ($domainLen > 255) { |
|
58 | - // domain part length exceeded |
|
59 | - throw new EmailValidationException( |
|
60 | - esc_html__('Email domain (after the @) is too long.', 'event_espresso') |
|
61 | - ); |
|
62 | - } |
|
63 | - if ($local[0] === '.') { |
|
64 | - // local part starts with '.' |
|
65 | - throw new EmailValidationException( |
|
66 | - esc_html__('Email local-part (before the @) must not begin with a period.', 'event_espresso') |
|
67 | - ); |
|
68 | - } |
|
69 | - if ($local[$localLen - 1] === '.') { |
|
70 | - // local part starts or ends with '.' |
|
71 | - throw new EmailValidationException( |
|
72 | - esc_html__('Email local-part (before the @) must not end with a period.', 'event_espresso') |
|
73 | - ); |
|
74 | - } |
|
75 | - if (preg_match('/\\.\\./', $local)) { |
|
76 | - // local part has two consecutive dots |
|
77 | - throw new EmailValidationException( |
|
78 | - esc_html__( |
|
79 | - 'Email local-part (before the @) must not have two consecutive periods.', |
|
80 | - 'event_espresso' |
|
81 | - ) |
|
82 | - ); |
|
83 | - } |
|
84 | - if (preg_match('/\\.\\./', $domain)) { |
|
85 | - // domain part has two consecutive dots |
|
86 | - throw new EmailValidationException( |
|
87 | - esc_html__('Email domain (after the @) must not have two consecutive periods.', 'event_espresso') |
|
88 | - ); |
|
89 | - } |
|
90 | - } |
|
91 | - return true; |
|
92 | - } |
|
23 | + /** |
|
24 | + * @param string $input |
|
25 | + * @return bool |
|
26 | + * @throws EmailValidationException |
|
27 | + */ |
|
28 | + public function validate($input) |
|
29 | + { |
|
30 | + if (! preg_match('/^.+\@\S+$/', $input)) { |
|
31 | + // email not in correct {string}@{string} format |
|
32 | + throw new EmailValidationException( |
|
33 | + esc_html__('Email does not have the required @ sign.', 'event_espresso') |
|
34 | + ); |
|
35 | + } else { |
|
36 | + $domain = $this->getDomainPartOfEmail($input); |
|
37 | + $local = $this->getLocalPartOfEmail($input); |
|
38 | + $localLen = strlen($local); |
|
39 | + $domainLen = strlen($domain); |
|
40 | + if ($localLen < 1) { |
|
41 | + //no local part |
|
42 | + throw new EmailValidationException( |
|
43 | + esc_html__('Email local-part (before the @) is required.', 'event_espresso') |
|
44 | + ); |
|
45 | + } |
|
46 | + if ($localLen > 64) { |
|
47 | + // local part length exceeded |
|
48 | + throw new EmailValidationException( |
|
49 | + esc_html__('Email local-part (before the @) is too long.', 'event_espresso') |
|
50 | + ); |
|
51 | + } |
|
52 | + if ($domainLen < 1) { |
|
53 | + throw new EmailValidationException( |
|
54 | + esc_html__('Email domain (after the @) is required.', 'event_espresso') |
|
55 | + ); |
|
56 | + } |
|
57 | + if ($domainLen > 255) { |
|
58 | + // domain part length exceeded |
|
59 | + throw new EmailValidationException( |
|
60 | + esc_html__('Email domain (after the @) is too long.', 'event_espresso') |
|
61 | + ); |
|
62 | + } |
|
63 | + if ($local[0] === '.') { |
|
64 | + // local part starts with '.' |
|
65 | + throw new EmailValidationException( |
|
66 | + esc_html__('Email local-part (before the @) must not begin with a period.', 'event_espresso') |
|
67 | + ); |
|
68 | + } |
|
69 | + if ($local[$localLen - 1] === '.') { |
|
70 | + // local part starts or ends with '.' |
|
71 | + throw new EmailValidationException( |
|
72 | + esc_html__('Email local-part (before the @) must not end with a period.', 'event_espresso') |
|
73 | + ); |
|
74 | + } |
|
75 | + if (preg_match('/\\.\\./', $local)) { |
|
76 | + // local part has two consecutive dots |
|
77 | + throw new EmailValidationException( |
|
78 | + esc_html__( |
|
79 | + 'Email local-part (before the @) must not have two consecutive periods.', |
|
80 | + 'event_espresso' |
|
81 | + ) |
|
82 | + ); |
|
83 | + } |
|
84 | + if (preg_match('/\\.\\./', $domain)) { |
|
85 | + // domain part has two consecutive dots |
|
86 | + throw new EmailValidationException( |
|
87 | + esc_html__('Email domain (after the @) must not have two consecutive periods.', 'event_espresso') |
|
88 | + ); |
|
89 | + } |
|
90 | + } |
|
91 | + return true; |
|
92 | + } |
|
93 | 93 | |
94 | 94 | |
95 | 95 | |
96 | - /** |
|
97 | - * Gets the local part of the email |
|
98 | - * @param $input |
|
99 | - * @return bool|string |
|
100 | - */ |
|
101 | - protected function getLocalPartOfEmail($input) |
|
102 | - { |
|
103 | - $atIndex = strrpos($input, '@'); |
|
104 | - return substr($input, 0, $atIndex); |
|
105 | - } |
|
96 | + /** |
|
97 | + * Gets the local part of the email |
|
98 | + * @param $input |
|
99 | + * @return bool|string |
|
100 | + */ |
|
101 | + protected function getLocalPartOfEmail($input) |
|
102 | + { |
|
103 | + $atIndex = strrpos($input, '@'); |
|
104 | + return substr($input, 0, $atIndex); |
|
105 | + } |
|
106 | 106 | |
107 | 107 | |
108 | 108 | |
109 | - /** |
|
110 | - * Gets the domain part of the email |
|
111 | - * @param $input |
|
112 | - * @return bool|string |
|
113 | - */ |
|
114 | - protected function getDomainPartOfEmail($input) |
|
115 | - { |
|
116 | - $atIndex = strrpos($input, '@'); |
|
117 | - return substr($input, $atIndex + 1); |
|
118 | - } |
|
109 | + /** |
|
110 | + * Gets the domain part of the email |
|
111 | + * @param $input |
|
112 | + * @return bool|string |
|
113 | + */ |
|
114 | + protected function getDomainPartOfEmail($input) |
|
115 | + { |
|
116 | + $atIndex = strrpos($input, '@'); |
|
117 | + return substr($input, $atIndex + 1); |
|
118 | + } |
|
119 | 119 | } |
120 | 120 | // End of file Basic.php |
121 | 121 | // Location: core\services\validation/Basic.php |
122 | 122 | \ No newline at end of file |
@@ -27,7 +27,7 @@ |
||
27 | 27 | */ |
28 | 28 | public function validate($input) |
29 | 29 | { |
30 | - if (! preg_match('/^.+\@\S+$/', $input)) { |
|
30 | + if ( ! preg_match('/^.+\@\S+$/', $input)) { |
|
31 | 31 | // email not in correct {string}@{string} format |
32 | 32 | throw new EmailValidationException( |
33 | 33 | esc_html__('Email does not have the required @ sign.', 'event_espresso') |
@@ -20,28 +20,28 @@ |
||
20 | 20 | class International extends Basic |
21 | 21 | { |
22 | 22 | |
23 | - /** |
|
24 | - * @param string $input |
|
25 | - * @return bool |
|
26 | - * @throws EmailValidationException |
|
27 | - */ |
|
28 | - public function validate($input) |
|
29 | - { |
|
30 | - parent::validate($input); |
|
31 | - if (// plz see http://stackoverflow.com/a/24817336 re: the following regex |
|
32 | - ! preg_match( |
|
33 | - // @codingStandardsIgnoreStart |
|
34 | - '/^(?!\.)((?!.*\.{2})[a-zA-Z0-9\x{0080}-\x{00FF}\x{0100}-\x{017F}\x{0180}-\x{024F}\x{0250}-\x{02AF}\x{0300}-\x{036F}\x{0370}-\x{03FF}\x{0400}-\x{04FF}\x{0500}-\x{052F}\x{0530}-\x{058F}\x{0590}-\x{05FF}\x{0600}-\x{06FF}\x{0700}-\x{074F}\x{0750}-\x{077F}\x{0780}-\x{07BF}\x{07C0}-\x{07FF}\x{0900}-\x{097F}\x{0980}-\x{09FF}\x{0A00}-\x{0A7F}\x{0A80}-\x{0AFF}\x{0B00}-\x{0B7F}\x{0B80}-\x{0BFF}\x{0C00}-\x{0C7F}\x{0C80}-\x{0CFF}\x{0D00}-\x{0D7F}\x{0D80}-\x{0DFF}\x{0E00}-\x{0E7F}\x{0E80}-\x{0EFF}\x{0F00}-\x{0FFF}\x{1000}-\x{109F}\x{10A0}-\x{10FF}\x{1100}-\x{11FF}\x{1200}-\x{137F}\x{1380}-\x{139F}\x{13A0}-\x{13FF}\x{1400}-\x{167F}\x{1680}-\x{169F}\x{16A0}-\x{16FF}\x{1700}-\x{171F}\x{1720}-\x{173F}\x{1740}-\x{175F}\x{1760}-\x{177F}\x{1780}-\x{17FF}\x{1800}-\x{18AF}\x{1900}-\x{194F}\x{1950}-\x{197F}\x{1980}-\x{19DF}\x{19E0}-\x{19FF}\x{1A00}-\x{1A1F}\x{1B00}-\x{1B7F}\x{1D00}-\x{1D7F}\x{1D80}-\x{1DBF}\x{1DC0}-\x{1DFF}\x{1E00}-\x{1EFF}\x{1F00}-\x{1FFF}\x{20D0}-\x{20FF}\x{2100}-\x{214F}\x{2C00}-\x{2C5F}\x{2C60}-\x{2C7F}\x{2C80}-\x{2CFF}\x{2D00}-\x{2D2F}\x{2D30}-\x{2D7F}\x{2D80}-\x{2DDF}\x{2F00}-\x{2FDF}\x{2FF0}-\x{2FFF}\x{3040}-\x{309F}\x{30A0}-\x{30FF}\x{3100}-\x{312F}\x{3130}-\x{318F}\x{3190}-\x{319F}\x{31C0}-\x{31EF}\x{31F0}-\x{31FF}\x{3200}-\x{32FF}\x{3300}-\x{33FF}\x{3400}-\x{4DBF}\x{4DC0}-\x{4DFF}\x{4E00}-\x{9FFF}\x{A000}-\x{A48F}\x{A490}-\x{A4CF}\x{A700}-\x{A71F}\x{A800}-\x{A82F}\x{A840}-\x{A87F}\x{AC00}-\x{D7AF}\x{F900}-\x{FAFF}\.!#$%&\'*+-\/=?^_`{|}~\-\d]+)@(?!\.)([a-zA-Z0-9\x{0080}-\x{00FF}\x{0100}-\x{017F}\x{0180}-\x{024F}\x{0250}-\x{02AF}\x{0300}-\x{036F}\x{0370}-\x{03FF}\x{0400}-\x{04FF}\x{0500}-\x{052F}\x{0530}-\x{058F}\x{0590}-\x{05FF}\x{0600}-\x{06FF}\x{0700}-\x{074F}\x{0750}-\x{077F}\x{0780}-\x{07BF}\x{07C0}-\x{07FF}\x{0900}-\x{097F}\x{0980}-\x{09FF}\x{0A00}-\x{0A7F}\x{0A80}-\x{0AFF}\x{0B00}-\x{0B7F}\x{0B80}-\x{0BFF}\x{0C00}-\x{0C7F}\x{0C80}-\x{0CFF}\x{0D00}-\x{0D7F}\x{0D80}-\x{0DFF}\x{0E00}-\x{0E7F}\x{0E80}-\x{0EFF}\x{0F00}-\x{0FFF}\x{1000}-\x{109F}\x{10A0}-\x{10FF}\x{1100}-\x{11FF}\x{1200}-\x{137F}\x{1380}-\x{139F}\x{13A0}-\x{13FF}\x{1400}-\x{167F}\x{1680}-\x{169F}\x{16A0}-\x{16FF}\x{1700}-\x{171F}\x{1720}-\x{173F}\x{1740}-\x{175F}\x{1760}-\x{177F}\x{1780}-\x{17FF}\x{1800}-\x{18AF}\x{1900}-\x{194F}\x{1950}-\x{197F}\x{1980}-\x{19DF}\x{19E0}-\x{19FF}\x{1A00}-\x{1A1F}\x{1B00}-\x{1B7F}\x{1D00}-\x{1D7F}\x{1D80}-\x{1DBF}\x{1DC0}-\x{1DFF}\x{1E00}-\x{1EFF}\x{1F00}-\x{1FFF}\x{20D0}-\x{20FF}\x{2100}-\x{214F}\x{2C00}-\x{2C5F}\x{2C60}-\x{2C7F}\x{2C80}-\x{2CFF}\x{2D00}-\x{2D2F}\x{2D30}-\x{2D7F}\x{2D80}-\x{2DDF}\x{2F00}-\x{2FDF}\x{2FF0}-\x{2FFF}\x{3040}-\x{309F}\x{30A0}-\x{30FF}\x{3100}-\x{312F}\x{3130}-\x{318F}\x{3190}-\x{319F}\x{31C0}-\x{31EF}\x{31F0}-\x{31FF}\x{3200}-\x{32FF}\x{3300}-\x{33FF}\x{3400}-\x{4DBF}\x{4DC0}-\x{4DFF}\x{4E00}-\x{9FFF}\x{A000}-\x{A48F}\x{A490}-\x{A4CF}\x{A700}-\x{A71F}\x{A800}-\x{A82F}\x{A840}-\x{A87F}\x{AC00}-\x{D7AF}\x{F900}-\x{FAFF}\-\.\d]+)((\.([a-zA-Z\x{0080}-\x{00FF}\x{0100}-\x{017F}\x{0180}-\x{024F}\x{0250}-\x{02AF}\x{0300}-\x{036F}\x{0370}-\x{03FF}\x{0400}-\x{04FF}\x{0500}-\x{052F}\x{0530}-\x{058F}\x{0590}-\x{05FF}\x{0600}-\x{06FF}\x{0700}-\x{074F}\x{0750}-\x{077F}\x{0780}-\x{07BF}\x{07C0}-\x{07FF}\x{0900}-\x{097F}\x{0980}-\x{09FF}\x{0A00}-\x{0A7F}\x{0A80}-\x{0AFF}\x{0B00}-\x{0B7F}\x{0B80}-\x{0BFF}\x{0C00}-\x{0C7F}\x{0C80}-\x{0CFF}\x{0D00}-\x{0D7F}\x{0D80}-\x{0DFF}\x{0E00}-\x{0E7F}\x{0E80}-\x{0EFF}\x{0F00}-\x{0FFF}\x{1000}-\x{109F}\x{10A0}-\x{10FF}\x{1100}-\x{11FF}\x{1200}-\x{137F}\x{1380}-\x{139F}\x{13A0}-\x{13FF}\x{1400}-\x{167F}\x{1680}-\x{169F}\x{16A0}-\x{16FF}\x{1700}-\x{171F}\x{1720}-\x{173F}\x{1740}-\x{175F}\x{1760}-\x{177F}\x{1780}-\x{17FF}\x{1800}-\x{18AF}\x{1900}-\x{194F}\x{1950}-\x{197F}\x{1980}-\x{19DF}\x{19E0}-\x{19FF}\x{1A00}-\x{1A1F}\x{1B00}-\x{1B7F}\x{1D00}-\x{1D7F}\x{1D80}-\x{1DBF}\x{1DC0}-\x{1DFF}\x{1E00}-\x{1EFF}\x{1F00}-\x{1FFF}\x{20D0}-\x{20FF}\x{2100}-\x{214F}\x{2C00}-\x{2C5F}\x{2C60}-\x{2C7F}\x{2C80}-\x{2CFF}\x{2D00}-\x{2D2F}\x{2D30}-\x{2D7F}\x{2D80}-\x{2DDF}\x{2F00}-\x{2FDF}\x{2FF0}-\x{2FFF}\x{3040}-\x{309F}\x{30A0}-\x{30FF}\x{3100}-\x{312F}\x{3130}-\x{318F}\x{3190}-\x{319F}\x{31C0}-\x{31EF}\x{31F0}-\x{31FF}\x{3200}-\x{32FF}\x{3300}-\x{33FF}\x{3400}-\x{4DBF}\x{4DC0}-\x{4DFF}\x{4E00}-\x{9FFF}\x{A000}-\x{A48F}\x{A490}-\x{A4CF}\x{A700}-\x{A71F}\x{A800}-\x{A82F}\x{A840}-\x{A87F}\x{AC00}-\x{D7AF}\x{F900}-\x{FAFF}]){2,63})+)$/u', |
|
35 | - // @codingStandardsIgnoreEnd |
|
36 | - $input |
|
37 | - ) |
|
38 | - ) { |
|
39 | - throw new EmailValidationException( |
|
40 | - esc_html__('Email address is invalid.', 'event_espresso') |
|
41 | - ); |
|
42 | - } |
|
43 | - return true; |
|
44 | - } |
|
23 | + /** |
|
24 | + * @param string $input |
|
25 | + * @return bool |
|
26 | + * @throws EmailValidationException |
|
27 | + */ |
|
28 | + public function validate($input) |
|
29 | + { |
|
30 | + parent::validate($input); |
|
31 | + if (// plz see http://stackoverflow.com/a/24817336 re: the following regex |
|
32 | + ! preg_match( |
|
33 | + // @codingStandardsIgnoreStart |
|
34 | + '/^(?!\.)((?!.*\.{2})[a-zA-Z0-9\x{0080}-\x{00FF}\x{0100}-\x{017F}\x{0180}-\x{024F}\x{0250}-\x{02AF}\x{0300}-\x{036F}\x{0370}-\x{03FF}\x{0400}-\x{04FF}\x{0500}-\x{052F}\x{0530}-\x{058F}\x{0590}-\x{05FF}\x{0600}-\x{06FF}\x{0700}-\x{074F}\x{0750}-\x{077F}\x{0780}-\x{07BF}\x{07C0}-\x{07FF}\x{0900}-\x{097F}\x{0980}-\x{09FF}\x{0A00}-\x{0A7F}\x{0A80}-\x{0AFF}\x{0B00}-\x{0B7F}\x{0B80}-\x{0BFF}\x{0C00}-\x{0C7F}\x{0C80}-\x{0CFF}\x{0D00}-\x{0D7F}\x{0D80}-\x{0DFF}\x{0E00}-\x{0E7F}\x{0E80}-\x{0EFF}\x{0F00}-\x{0FFF}\x{1000}-\x{109F}\x{10A0}-\x{10FF}\x{1100}-\x{11FF}\x{1200}-\x{137F}\x{1380}-\x{139F}\x{13A0}-\x{13FF}\x{1400}-\x{167F}\x{1680}-\x{169F}\x{16A0}-\x{16FF}\x{1700}-\x{171F}\x{1720}-\x{173F}\x{1740}-\x{175F}\x{1760}-\x{177F}\x{1780}-\x{17FF}\x{1800}-\x{18AF}\x{1900}-\x{194F}\x{1950}-\x{197F}\x{1980}-\x{19DF}\x{19E0}-\x{19FF}\x{1A00}-\x{1A1F}\x{1B00}-\x{1B7F}\x{1D00}-\x{1D7F}\x{1D80}-\x{1DBF}\x{1DC0}-\x{1DFF}\x{1E00}-\x{1EFF}\x{1F00}-\x{1FFF}\x{20D0}-\x{20FF}\x{2100}-\x{214F}\x{2C00}-\x{2C5F}\x{2C60}-\x{2C7F}\x{2C80}-\x{2CFF}\x{2D00}-\x{2D2F}\x{2D30}-\x{2D7F}\x{2D80}-\x{2DDF}\x{2F00}-\x{2FDF}\x{2FF0}-\x{2FFF}\x{3040}-\x{309F}\x{30A0}-\x{30FF}\x{3100}-\x{312F}\x{3130}-\x{318F}\x{3190}-\x{319F}\x{31C0}-\x{31EF}\x{31F0}-\x{31FF}\x{3200}-\x{32FF}\x{3300}-\x{33FF}\x{3400}-\x{4DBF}\x{4DC0}-\x{4DFF}\x{4E00}-\x{9FFF}\x{A000}-\x{A48F}\x{A490}-\x{A4CF}\x{A700}-\x{A71F}\x{A800}-\x{A82F}\x{A840}-\x{A87F}\x{AC00}-\x{D7AF}\x{F900}-\x{FAFF}\.!#$%&\'*+-\/=?^_`{|}~\-\d]+)@(?!\.)([a-zA-Z0-9\x{0080}-\x{00FF}\x{0100}-\x{017F}\x{0180}-\x{024F}\x{0250}-\x{02AF}\x{0300}-\x{036F}\x{0370}-\x{03FF}\x{0400}-\x{04FF}\x{0500}-\x{052F}\x{0530}-\x{058F}\x{0590}-\x{05FF}\x{0600}-\x{06FF}\x{0700}-\x{074F}\x{0750}-\x{077F}\x{0780}-\x{07BF}\x{07C0}-\x{07FF}\x{0900}-\x{097F}\x{0980}-\x{09FF}\x{0A00}-\x{0A7F}\x{0A80}-\x{0AFF}\x{0B00}-\x{0B7F}\x{0B80}-\x{0BFF}\x{0C00}-\x{0C7F}\x{0C80}-\x{0CFF}\x{0D00}-\x{0D7F}\x{0D80}-\x{0DFF}\x{0E00}-\x{0E7F}\x{0E80}-\x{0EFF}\x{0F00}-\x{0FFF}\x{1000}-\x{109F}\x{10A0}-\x{10FF}\x{1100}-\x{11FF}\x{1200}-\x{137F}\x{1380}-\x{139F}\x{13A0}-\x{13FF}\x{1400}-\x{167F}\x{1680}-\x{169F}\x{16A0}-\x{16FF}\x{1700}-\x{171F}\x{1720}-\x{173F}\x{1740}-\x{175F}\x{1760}-\x{177F}\x{1780}-\x{17FF}\x{1800}-\x{18AF}\x{1900}-\x{194F}\x{1950}-\x{197F}\x{1980}-\x{19DF}\x{19E0}-\x{19FF}\x{1A00}-\x{1A1F}\x{1B00}-\x{1B7F}\x{1D00}-\x{1D7F}\x{1D80}-\x{1DBF}\x{1DC0}-\x{1DFF}\x{1E00}-\x{1EFF}\x{1F00}-\x{1FFF}\x{20D0}-\x{20FF}\x{2100}-\x{214F}\x{2C00}-\x{2C5F}\x{2C60}-\x{2C7F}\x{2C80}-\x{2CFF}\x{2D00}-\x{2D2F}\x{2D30}-\x{2D7F}\x{2D80}-\x{2DDF}\x{2F00}-\x{2FDF}\x{2FF0}-\x{2FFF}\x{3040}-\x{309F}\x{30A0}-\x{30FF}\x{3100}-\x{312F}\x{3130}-\x{318F}\x{3190}-\x{319F}\x{31C0}-\x{31EF}\x{31F0}-\x{31FF}\x{3200}-\x{32FF}\x{3300}-\x{33FF}\x{3400}-\x{4DBF}\x{4DC0}-\x{4DFF}\x{4E00}-\x{9FFF}\x{A000}-\x{A48F}\x{A490}-\x{A4CF}\x{A700}-\x{A71F}\x{A800}-\x{A82F}\x{A840}-\x{A87F}\x{AC00}-\x{D7AF}\x{F900}-\x{FAFF}\-\.\d]+)((\.([a-zA-Z\x{0080}-\x{00FF}\x{0100}-\x{017F}\x{0180}-\x{024F}\x{0250}-\x{02AF}\x{0300}-\x{036F}\x{0370}-\x{03FF}\x{0400}-\x{04FF}\x{0500}-\x{052F}\x{0530}-\x{058F}\x{0590}-\x{05FF}\x{0600}-\x{06FF}\x{0700}-\x{074F}\x{0750}-\x{077F}\x{0780}-\x{07BF}\x{07C0}-\x{07FF}\x{0900}-\x{097F}\x{0980}-\x{09FF}\x{0A00}-\x{0A7F}\x{0A80}-\x{0AFF}\x{0B00}-\x{0B7F}\x{0B80}-\x{0BFF}\x{0C00}-\x{0C7F}\x{0C80}-\x{0CFF}\x{0D00}-\x{0D7F}\x{0D80}-\x{0DFF}\x{0E00}-\x{0E7F}\x{0E80}-\x{0EFF}\x{0F00}-\x{0FFF}\x{1000}-\x{109F}\x{10A0}-\x{10FF}\x{1100}-\x{11FF}\x{1200}-\x{137F}\x{1380}-\x{139F}\x{13A0}-\x{13FF}\x{1400}-\x{167F}\x{1680}-\x{169F}\x{16A0}-\x{16FF}\x{1700}-\x{171F}\x{1720}-\x{173F}\x{1740}-\x{175F}\x{1760}-\x{177F}\x{1780}-\x{17FF}\x{1800}-\x{18AF}\x{1900}-\x{194F}\x{1950}-\x{197F}\x{1980}-\x{19DF}\x{19E0}-\x{19FF}\x{1A00}-\x{1A1F}\x{1B00}-\x{1B7F}\x{1D00}-\x{1D7F}\x{1D80}-\x{1DBF}\x{1DC0}-\x{1DFF}\x{1E00}-\x{1EFF}\x{1F00}-\x{1FFF}\x{20D0}-\x{20FF}\x{2100}-\x{214F}\x{2C00}-\x{2C5F}\x{2C60}-\x{2C7F}\x{2C80}-\x{2CFF}\x{2D00}-\x{2D2F}\x{2D30}-\x{2D7F}\x{2D80}-\x{2DDF}\x{2F00}-\x{2FDF}\x{2FF0}-\x{2FFF}\x{3040}-\x{309F}\x{30A0}-\x{30FF}\x{3100}-\x{312F}\x{3130}-\x{318F}\x{3190}-\x{319F}\x{31C0}-\x{31EF}\x{31F0}-\x{31FF}\x{3200}-\x{32FF}\x{3300}-\x{33FF}\x{3400}-\x{4DBF}\x{4DC0}-\x{4DFF}\x{4E00}-\x{9FFF}\x{A000}-\x{A48F}\x{A490}-\x{A4CF}\x{A700}-\x{A71F}\x{A800}-\x{A82F}\x{A840}-\x{A87F}\x{AC00}-\x{D7AF}\x{F900}-\x{FAFF}]){2,63})+)$/u', |
|
35 | + // @codingStandardsIgnoreEnd |
|
36 | + $input |
|
37 | + ) |
|
38 | + ) { |
|
39 | + throw new EmailValidationException( |
|
40 | + esc_html__('Email address is invalid.', 'event_espresso') |
|
41 | + ); |
|
42 | + } |
|
43 | + return true; |
|
44 | + } |
|
45 | 45 | } |
46 | 46 | // End of file International.php |
47 | 47 | // Location: core\services\validation/International.php |
@@ -14,70 +14,70 @@ |
||
14 | 14 | class EE_Email_Validation_Strategy extends EE_Text_Validation_Strategy |
15 | 15 | { |
16 | 16 | |
17 | - /** |
|
18 | - * @param null $validation_error_message |
|
19 | - */ |
|
20 | - public function __construct($validation_error_message = null) |
|
21 | - { |
|
22 | - if (! $validation_error_message) { |
|
23 | - $validation_error_message = __("Please enter a valid email address.", "event_espresso"); |
|
24 | - } |
|
25 | - parent::__construct($validation_error_message); |
|
26 | - } |
|
17 | + /** |
|
18 | + * @param null $validation_error_message |
|
19 | + */ |
|
20 | + public function __construct($validation_error_message = null) |
|
21 | + { |
|
22 | + if (! $validation_error_message) { |
|
23 | + $validation_error_message = __("Please enter a valid email address.", "event_espresso"); |
|
24 | + } |
|
25 | + parent::__construct($validation_error_message); |
|
26 | + } |
|
27 | 27 | |
28 | 28 | |
29 | 29 | |
30 | - /** |
|
31 | - * just checks the field isn't blank |
|
32 | - * |
|
33 | - * @param $normalized_value |
|
34 | - * @return bool |
|
35 | - * @throws \EE_Validation_Error |
|
36 | - */ |
|
37 | - public function validate($normalized_value) |
|
38 | - { |
|
39 | - if ($normalized_value && ! $this->_validate_email($normalized_value)) { |
|
40 | - throw new EE_Validation_Error($this->get_validation_error_message(), 'required'); |
|
41 | - } |
|
42 | - return true; |
|
43 | - } |
|
30 | + /** |
|
31 | + * just checks the field isn't blank |
|
32 | + * |
|
33 | + * @param $normalized_value |
|
34 | + * @return bool |
|
35 | + * @throws \EE_Validation_Error |
|
36 | + */ |
|
37 | + public function validate($normalized_value) |
|
38 | + { |
|
39 | + if ($normalized_value && ! $this->_validate_email($normalized_value)) { |
|
40 | + throw new EE_Validation_Error($this->get_validation_error_message(), 'required'); |
|
41 | + } |
|
42 | + return true; |
|
43 | + } |
|
44 | 44 | |
45 | 45 | |
46 | 46 | |
47 | - /** |
|
48 | - * @return array |
|
49 | - */ |
|
50 | - public function get_jquery_validation_rule_array() |
|
51 | - { |
|
52 | - return array('email' => true, 'messages' => array('email' => $this->get_validation_error_message())); |
|
53 | - } |
|
47 | + /** |
|
48 | + * @return array |
|
49 | + */ |
|
50 | + public function get_jquery_validation_rule_array() |
|
51 | + { |
|
52 | + return array('email' => true, 'messages' => array('email' => $this->get_validation_error_message())); |
|
53 | + } |
|
54 | 54 | |
55 | 55 | |
56 | 56 | |
57 | - /** |
|
58 | - * Validate an email address. |
|
59 | - * Provide email address (raw input) |
|
60 | - * |
|
61 | - * @param $email |
|
62 | - * @return bool of whether the email is valid or not |
|
63 | - * @throws \EE_Validation_Error |
|
64 | - */ |
|
65 | - private function _validate_email($email) |
|
66 | - { |
|
67 | - $loader = new Loader(); |
|
68 | - $validation_service = $loader->getShared( |
|
69 | - 'EventEspresso\core\domain\services\validation\EmailValidationServiceInterface' |
|
70 | - ); |
|
71 | - try { |
|
72 | - $validation_service->validate($email); |
|
73 | - } catch (EmailValidationException $e) { |
|
74 | - throw new EE_Validation_Error( |
|
75 | - $e->getMessage(), |
|
76 | - 'invalid_email', |
|
77 | - $this->_input, |
|
78 | - $e |
|
79 | - ); |
|
80 | - } |
|
81 | - return true; |
|
82 | - } |
|
57 | + /** |
|
58 | + * Validate an email address. |
|
59 | + * Provide email address (raw input) |
|
60 | + * |
|
61 | + * @param $email |
|
62 | + * @return bool of whether the email is valid or not |
|
63 | + * @throws \EE_Validation_Error |
|
64 | + */ |
|
65 | + private function _validate_email($email) |
|
66 | + { |
|
67 | + $loader = new Loader(); |
|
68 | + $validation_service = $loader->getShared( |
|
69 | + 'EventEspresso\core\domain\services\validation\EmailValidationServiceInterface' |
|
70 | + ); |
|
71 | + try { |
|
72 | + $validation_service->validate($email); |
|
73 | + } catch (EmailValidationException $e) { |
|
74 | + throw new EE_Validation_Error( |
|
75 | + $e->getMessage(), |
|
76 | + 'invalid_email', |
|
77 | + $this->_input, |
|
78 | + $e |
|
79 | + ); |
|
80 | + } |
|
81 | + return true; |
|
82 | + } |
|
83 | 83 | } |
@@ -19,7 +19,7 @@ |
||
19 | 19 | */ |
20 | 20 | public function __construct($validation_error_message = null) |
21 | 21 | { |
22 | - if (! $validation_error_message) { |
|
22 | + if ( ! $validation_error_message) { |
|
23 | 23 | $validation_error_message = __("Please enter a valid email address.", "event_espresso"); |
24 | 24 | } |
25 | 25 | parent::__construct($validation_error_message); |