@@ -50,7 +50,7 @@ discard block |
||
50 | 50 | */ |
51 | 51 | public function getModelVersionInfo() |
52 | 52 | { |
53 | - if (! $this->model_version_info) { |
|
53 | + if ( ! $this->model_version_info) { |
|
54 | 54 | throw new EE_Error( |
55 | 55 | sprintf( |
56 | 56 | esc_html__( |
@@ -95,7 +95,7 @@ discard block |
||
95 | 95 | */ |
96 | 96 | protected function validateModel($model_name) |
97 | 97 | { |
98 | - if (! $this->getModelVersionInfo()->isModelNameInThisVersion($model_name)) { |
|
98 | + if ( ! $this->getModelVersionInfo()->isModelNameInThisVersion($model_name)) { |
|
99 | 99 | throw new RestException( |
100 | 100 | 'endpoint_parsing_error', |
101 | 101 | sprintf( |
@@ -18,95 +18,95 @@ |
||
18 | 18 | */ |
19 | 19 | class Base extends Controller_Base |
20 | 20 | { |
21 | - /** |
|
22 | - * Holds reference to the model version info, which knows the requested version |
|
23 | - * |
|
24 | - * @var ModelVersionInfo |
|
25 | - */ |
|
26 | - protected $model_version_info; |
|
21 | + /** |
|
22 | + * Holds reference to the model version info, which knows the requested version |
|
23 | + * |
|
24 | + * @var ModelVersionInfo |
|
25 | + */ |
|
26 | + protected $model_version_info; |
|
27 | 27 | |
28 | 28 | |
29 | 29 | |
30 | - /** |
|
31 | - * Sets the version the user requested |
|
32 | - * |
|
33 | - * @param string $version eg '4.8' |
|
34 | - */ |
|
35 | - public function setRequestedVersion($version) |
|
36 | - { |
|
37 | - parent::setRequestedVersion($version); |
|
38 | - $this->model_version_info = new ModelVersionInfo($version); |
|
39 | - } |
|
30 | + /** |
|
31 | + * Sets the version the user requested |
|
32 | + * |
|
33 | + * @param string $version eg '4.8' |
|
34 | + */ |
|
35 | + public function setRequestedVersion($version) |
|
36 | + { |
|
37 | + parent::setRequestedVersion($version); |
|
38 | + $this->model_version_info = new ModelVersionInfo($version); |
|
39 | + } |
|
40 | 40 | |
41 | 41 | |
42 | 42 | |
43 | - /** |
|
44 | - * Gets the object that should be used for getting any info from the models, |
|
45 | - * because it's takes the requested and current core version into account |
|
46 | - * |
|
47 | - * @return ModelVersionInfo |
|
48 | - * @throws EE_Error |
|
49 | - */ |
|
50 | - public function getModelVersionInfo() |
|
51 | - { |
|
52 | - if (! $this->model_version_info) { |
|
53 | - throw new EE_Error( |
|
54 | - sprintf( |
|
55 | - esc_html__( |
|
56 | - 'Cannot use model version info before setting the requested version in the controller', |
|
57 | - 'event_espresso' |
|
58 | - ) |
|
59 | - ) |
|
60 | - ); |
|
61 | - } |
|
62 | - return $this->model_version_info; |
|
63 | - } |
|
43 | + /** |
|
44 | + * Gets the object that should be used for getting any info from the models, |
|
45 | + * because it's takes the requested and current core version into account |
|
46 | + * |
|
47 | + * @return ModelVersionInfo |
|
48 | + * @throws EE_Error |
|
49 | + */ |
|
50 | + public function getModelVersionInfo() |
|
51 | + { |
|
52 | + if (! $this->model_version_info) { |
|
53 | + throw new EE_Error( |
|
54 | + sprintf( |
|
55 | + esc_html__( |
|
56 | + 'Cannot use model version info before setting the requested version in the controller', |
|
57 | + 'event_espresso' |
|
58 | + ) |
|
59 | + ) |
|
60 | + ); |
|
61 | + } |
|
62 | + return $this->model_version_info; |
|
63 | + } |
|
64 | 64 | |
65 | 65 | |
66 | 66 | |
67 | - /** |
|
68 | - * Determines if $object is of one of the classes of $classes. Similar to |
|
69 | - * in_array(), except this checks if $object is a subclass of the classnames provided |
|
70 | - * in $classnames |
|
71 | - * |
|
72 | - * @param object $object |
|
73 | - * @param array $classnames |
|
74 | - * @return boolean |
|
75 | - */ |
|
76 | - public function isSubclassOfOne($object, $classnames) |
|
77 | - { |
|
78 | - foreach ($classnames as $classname) { |
|
79 | - if (is_a($object, $classname)) { |
|
80 | - return true; |
|
81 | - } |
|
82 | - } |
|
83 | - return false; |
|
84 | - } |
|
67 | + /** |
|
68 | + * Determines if $object is of one of the classes of $classes. Similar to |
|
69 | + * in_array(), except this checks if $object is a subclass of the classnames provided |
|
70 | + * in $classnames |
|
71 | + * |
|
72 | + * @param object $object |
|
73 | + * @param array $classnames |
|
74 | + * @return boolean |
|
75 | + */ |
|
76 | + public function isSubclassOfOne($object, $classnames) |
|
77 | + { |
|
78 | + foreach ($classnames as $classname) { |
|
79 | + if (is_a($object, $classname)) { |
|
80 | + return true; |
|
81 | + } |
|
82 | + } |
|
83 | + return false; |
|
84 | + } |
|
85 | 85 | |
86 | - /** |
|
87 | - * Verifies the model name provided was valid. If so, returns the model (as an object). Otherwise, throws an |
|
88 | - * exception. Must be called after `setRequestedVersion()`. |
|
89 | - * @since 4.9.76.p |
|
90 | - * @param $model_name |
|
91 | - * @return EEM_Base |
|
92 | - * @throws EE_Error |
|
93 | - * @throws RestException |
|
94 | - */ |
|
95 | - protected function validateModel($model_name) |
|
96 | - { |
|
97 | - if (! $this->getModelVersionInfo()->isModelNameInThisVersion($model_name)) { |
|
98 | - throw new RestException( |
|
99 | - 'endpoint_parsing_error', |
|
100 | - sprintf( |
|
101 | - esc_html__( |
|
102 | - 'There is no model for endpoint %s. Please contact event espresso support', |
|
103 | - 'event_espresso' |
|
104 | - ), |
|
105 | - $model_name |
|
106 | - ) |
|
107 | - ); |
|
108 | - } |
|
109 | - return $this->getModelVersionInfo()->loadModel($model_name); |
|
110 | - } |
|
86 | + /** |
|
87 | + * Verifies the model name provided was valid. If so, returns the model (as an object). Otherwise, throws an |
|
88 | + * exception. Must be called after `setRequestedVersion()`. |
|
89 | + * @since 4.9.76.p |
|
90 | + * @param $model_name |
|
91 | + * @return EEM_Base |
|
92 | + * @throws EE_Error |
|
93 | + * @throws RestException |
|
94 | + */ |
|
95 | + protected function validateModel($model_name) |
|
96 | + { |
|
97 | + if (! $this->getModelVersionInfo()->isModelNameInThisVersion($model_name)) { |
|
98 | + throw new RestException( |
|
99 | + 'endpoint_parsing_error', |
|
100 | + sprintf( |
|
101 | + esc_html__( |
|
102 | + 'There is no model for endpoint %s. Please contact event espresso support', |
|
103 | + 'event_espresso' |
|
104 | + ), |
|
105 | + $model_name |
|
106 | + ) |
|
107 | + ); |
|
108 | + } |
|
109 | + return $this->getModelVersionInfo()->loadModel($model_name); |
|
110 | + } |
|
111 | 111 | } |
112 | 112 | // End of file Base.php |
@@ -134,9 +134,9 @@ discard block |
||
134 | 134 | { |
135 | 135 | Capabilities::verifyAtLeastPartialAccessTo($model, EEM_Base::caps_edit, 'create'); |
136 | 136 | $default_cap_to_check_for = EE_Restriction_Generator_Base::get_default_restrictions_cap(); |
137 | - if (! current_user_can($default_cap_to_check_for)) { |
|
137 | + if ( ! current_user_can($default_cap_to_check_for)) { |
|
138 | 138 | throw new RestException( |
139 | - 'rest_cannot_create_' . EEH_Inflector::pluralize_and_lower(($model->get_this_model_name())), |
|
139 | + 'rest_cannot_create_'.EEH_Inflector::pluralize_and_lower(($model->get_this_model_name())), |
|
140 | 140 | sprintf( |
141 | 141 | esc_html__( |
142 | 142 | // @codingStandardsIgnoreStart |
@@ -164,7 +164,7 @@ discard block |
||
164 | 164 | ); |
165 | 165 | $model_obj->save(); |
166 | 166 | $new_id = $model_obj->ID(); |
167 | - if (! $new_id) { |
|
167 | + if ( ! $new_id) { |
|
168 | 168 | throw new RestException( |
169 | 169 | 'rest_insertion_failed', |
170 | 170 | sprintf(esc_html__('Could not insert new %1$s', 'event_espresso'), $model->get_this_model_name()) |
@@ -186,9 +186,9 @@ discard block |
||
186 | 186 | { |
187 | 187 | Capabilities::verifyAtLeastPartialAccessTo($model, EEM_Base::caps_edit, 'edit'); |
188 | 188 | $default_cap_to_check_for = EE_Restriction_Generator_Base::get_default_restrictions_cap(); |
189 | - if (! current_user_can($default_cap_to_check_for)) { |
|
189 | + if ( ! current_user_can($default_cap_to_check_for)) { |
|
190 | 190 | throw new RestException( |
191 | - 'rest_cannot_edit_' . EEH_Inflector::pluralize_and_lower(($model->get_this_model_name())), |
|
191 | + 'rest_cannot_edit_'.EEH_Inflector::pluralize_and_lower(($model->get_this_model_name())), |
|
192 | 192 | sprintf( |
193 | 193 | esc_html__( |
194 | 194 | // @codingStandardsIgnoreStart |
@@ -202,7 +202,7 @@ discard block |
||
202 | 202 | ); |
203 | 203 | } |
204 | 204 | $obj_id = $request->get_param('id'); |
205 | - if (! $obj_id) { |
|
205 | + if ( ! $obj_id) { |
|
206 | 206 | throw new RestException( |
207 | 207 | 'rest_edit_failed', |
208 | 208 | sprintf(esc_html__('Could not edit %1$s', 'event_espresso'), $model->get_this_model_name()) |
@@ -215,7 +215,7 @@ discard block |
||
215 | 215 | true |
216 | 216 | ); |
217 | 217 | $model_obj = $model->get_one_by_ID($obj_id); |
218 | - if (! $model_obj instanceof EE_Base_Class) { |
|
218 | + if ( ! $model_obj instanceof EE_Base_Class) { |
|
219 | 219 | $lowercase_model_name = strtolower($model->get_this_model_name()); |
220 | 220 | throw new RestException( |
221 | 221 | sprintf('rest_%s_invalid_id', $lowercase_model_name), |
@@ -240,9 +240,9 @@ discard block |
||
240 | 240 | { |
241 | 241 | Capabilities::verifyAtLeastPartialAccessTo($model, EEM_Base::caps_delete, 'delete'); |
242 | 242 | $default_cap_to_check_for = EE_Restriction_Generator_Base::get_default_restrictions_cap(); |
243 | - if (! current_user_can($default_cap_to_check_for)) { |
|
243 | + if ( ! current_user_can($default_cap_to_check_for)) { |
|
244 | 244 | throw new RestException( |
245 | - 'rest_cannot_delete_' . EEH_Inflector::pluralize_and_lower(($model->get_this_model_name())), |
|
245 | + 'rest_cannot_delete_'.EEH_Inflector::pluralize_and_lower(($model->get_this_model_name())), |
|
246 | 246 | sprintf( |
247 | 247 | esc_html__( |
248 | 248 | // @codingStandardsIgnoreStart |
@@ -258,7 +258,7 @@ discard block |
||
258 | 258 | $obj_id = $request->get_param('id'); |
259 | 259 | // this is where we would apply more fine-grained caps |
260 | 260 | $model_obj = $model->get_one_by_ID($obj_id); |
261 | - if (! $model_obj instanceof EE_Base_Class) { |
|
261 | + if ( ! $model_obj instanceof EE_Base_Class) { |
|
262 | 262 | $lowercase_model_name = strtolower($model->get_this_model_name()); |
263 | 263 | throw new RestException( |
264 | 264 | sprintf('rest_%s_invalid_id', $lowercase_model_name), |
@@ -316,7 +316,7 @@ discard block |
||
316 | 316 | } else { |
317 | 317 | $raw_value = $model_obj->get_raw($field_name); |
318 | 318 | } |
319 | - $simulated_db_row[ $field_obj->get_qualified_column() ] = $field_obj->prepare_for_use_in_db($raw_value); |
|
319 | + $simulated_db_row[$field_obj->get_qualified_column()] = $field_obj->prepare_for_use_in_db($raw_value); |
|
320 | 320 | } |
321 | 321 | $read_controller = LoaderFactory::getLoader()->getNew('EventEspresso\core\libraries\rest_api\controllers\model\Read'); |
322 | 322 | $read_controller->setRequestedVersion($this->getRequestedVersion()); |
@@ -447,7 +447,7 @@ discard block |
||
447 | 447 | ) |
448 | 448 | ) |
449 | 449 | ); |
450 | - $response['join'][ strtolower($relation->get_join_model()->get_this_model_name()) ] = $this->returnModelObjAsJsonResponse($join_model_obj, $request); |
|
450 | + $response['join'][strtolower($relation->get_join_model()->get_this_model_name())] = $this->returnModelObjAsJsonResponse($join_model_obj, $request); |
|
451 | 451 | } |
452 | 452 | return $response; |
453 | 453 | } |
@@ -495,7 +495,7 @@ discard block |
||
495 | 495 | { |
496 | 496 | // This endpoint doesn't accept body parameters (it's understandable to think it might, so let developers know |
497 | 497 | // up-front that it doesn't.) |
498 | - if (!empty($request->get_body_params())) { |
|
498 | + if ( ! empty($request->get_body_params())) { |
|
499 | 499 | $body_params = $request->get_body_params(); |
500 | 500 | throw new RestException( |
501 | 501 | 'invalid_field', |
@@ -537,9 +537,9 @@ discard block |
||
537 | 537 | ) |
538 | 538 | ); |
539 | 539 | if ($join_model_obj instanceof EE_Base_Class) { |
540 | - $response['join'][ strtolower($relation->get_join_model()->get_this_model_name()) ] = $this->returnModelObjAsJsonResponse($join_model_obj, $request); |
|
540 | + $response['join'][strtolower($relation->get_join_model()->get_this_model_name())] = $this->returnModelObjAsJsonResponse($join_model_obj, $request); |
|
541 | 541 | } else { |
542 | - $response['join'][ strtolower($relation->get_join_model()->get_this_model_name()) ] = null; |
|
542 | + $response['join'][strtolower($relation->get_join_model()->get_this_model_name())] = null; |
|
543 | 543 | } |
544 | 544 | } |
545 | 545 | return $response; |
@@ -564,9 +564,9 @@ discard block |
||
564 | 564 | // Check generic caps. For now, we're only allowing access to this endpoint to full admins. |
565 | 565 | Capabilities::verifyAtLeastPartialAccessTo($model, EEM_Base::caps_edit, 'edit'); |
566 | 566 | $default_cap_to_check_for = EE_Restriction_Generator_Base::get_default_restrictions_cap(); |
567 | - if (! current_user_can($default_cap_to_check_for)) { |
|
567 | + if ( ! current_user_can($default_cap_to_check_for)) { |
|
568 | 568 | throw new RestException( |
569 | - 'rest_cannot_edit_' . EEH_Inflector::pluralize_and_lower(($model->get_this_model_name())), |
|
569 | + 'rest_cannot_edit_'.EEH_Inflector::pluralize_and_lower(($model->get_this_model_name())), |
|
570 | 570 | sprintf( |
571 | 571 | esc_html__( |
572 | 572 | // @codingStandardsIgnoreStart |
@@ -583,7 +583,7 @@ discard block |
||
583 | 583 | $model_obj = $this->getOneOrThrowException($model, $request->get_param('id')); |
584 | 584 | // For now, we require the other model object to exist too. This might be relaxed later. |
585 | 585 | $other_obj = $this->getOneOrThrowException($relation->get_other_model(), $request->get_param('related_id')); |
586 | - return array($model_obj,$other_obj); |
|
586 | + return array($model_obj, $other_obj); |
|
587 | 587 | } |
588 | 588 | |
589 | 589 | /** |
@@ -37,573 +37,573 @@ |
||
37 | 37 | */ |
38 | 38 | class Write extends Base |
39 | 39 | { |
40 | - public function __construct() |
|
41 | - { |
|
42 | - parent::__construct(); |
|
43 | - EE_Registry::instance()->load_helper('Inflector'); |
|
44 | - } |
|
40 | + public function __construct() |
|
41 | + { |
|
42 | + parent::__construct(); |
|
43 | + EE_Registry::instance()->load_helper('Inflector'); |
|
44 | + } |
|
45 | 45 | |
46 | 46 | |
47 | - /** |
|
48 | - * Handles requests to get all (or a filtered subset) of entities for a particular model |
|
49 | - * |
|
50 | - * @param WP_REST_Request $request |
|
51 | - * @param string $version |
|
52 | - * @param string $model_name |
|
53 | - * @return WP_REST_Response|\WP_Error |
|
54 | - */ |
|
55 | - public static function handleRequestInsert(WP_REST_Request $request, $version, $model_name) |
|
56 | - { |
|
57 | - $controller = new Write(); |
|
58 | - try { |
|
59 | - $controller->setRequestedVersion($version); |
|
60 | - return $controller->sendResponse( |
|
61 | - $controller->insert( |
|
62 | - $controller->getModelVersionInfo()->loadModel($model_name), |
|
63 | - $request |
|
64 | - ) |
|
65 | - ); |
|
66 | - } catch (Exception $e) { |
|
67 | - return $controller->sendResponse($e); |
|
68 | - } |
|
69 | - } |
|
47 | + /** |
|
48 | + * Handles requests to get all (or a filtered subset) of entities for a particular model |
|
49 | + * |
|
50 | + * @param WP_REST_Request $request |
|
51 | + * @param string $version |
|
52 | + * @param string $model_name |
|
53 | + * @return WP_REST_Response|\WP_Error |
|
54 | + */ |
|
55 | + public static function handleRequestInsert(WP_REST_Request $request, $version, $model_name) |
|
56 | + { |
|
57 | + $controller = new Write(); |
|
58 | + try { |
|
59 | + $controller->setRequestedVersion($version); |
|
60 | + return $controller->sendResponse( |
|
61 | + $controller->insert( |
|
62 | + $controller->getModelVersionInfo()->loadModel($model_name), |
|
63 | + $request |
|
64 | + ) |
|
65 | + ); |
|
66 | + } catch (Exception $e) { |
|
67 | + return $controller->sendResponse($e); |
|
68 | + } |
|
69 | + } |
|
70 | 70 | |
71 | 71 | |
72 | - /** |
|
73 | - * Handles a request from \WP_REST_Server to update an EE model |
|
74 | - * |
|
75 | - * @param WP_REST_Request $request |
|
76 | - * @param string $version |
|
77 | - * @param string $model_name |
|
78 | - * @return WP_REST_Response|\WP_Error |
|
79 | - */ |
|
80 | - public static function handleRequestUpdate(WP_REST_Request $request, $version, $model_name) |
|
81 | - { |
|
82 | - $controller = new Write(); |
|
83 | - try { |
|
84 | - $controller->setRequestedVersion($version); |
|
85 | - return $controller->sendResponse( |
|
86 | - $controller->update( |
|
87 | - $controller->getModelVersionInfo()->loadModel($model_name), |
|
88 | - $request |
|
89 | - ) |
|
90 | - ); |
|
91 | - } catch (Exception $e) { |
|
92 | - return $controller->sendResponse($e); |
|
93 | - } |
|
94 | - } |
|
72 | + /** |
|
73 | + * Handles a request from \WP_REST_Server to update an EE model |
|
74 | + * |
|
75 | + * @param WP_REST_Request $request |
|
76 | + * @param string $version |
|
77 | + * @param string $model_name |
|
78 | + * @return WP_REST_Response|\WP_Error |
|
79 | + */ |
|
80 | + public static function handleRequestUpdate(WP_REST_Request $request, $version, $model_name) |
|
81 | + { |
|
82 | + $controller = new Write(); |
|
83 | + try { |
|
84 | + $controller->setRequestedVersion($version); |
|
85 | + return $controller->sendResponse( |
|
86 | + $controller->update( |
|
87 | + $controller->getModelVersionInfo()->loadModel($model_name), |
|
88 | + $request |
|
89 | + ) |
|
90 | + ); |
|
91 | + } catch (Exception $e) { |
|
92 | + return $controller->sendResponse($e); |
|
93 | + } |
|
94 | + } |
|
95 | 95 | |
96 | 96 | |
97 | - /** |
|
98 | - * Deletes a single model object and returns it. Unless |
|
99 | - * |
|
100 | - * @param WP_REST_Request $request |
|
101 | - * @param string $version |
|
102 | - * @param string $model_name |
|
103 | - * @return WP_REST_Response|\WP_Error |
|
104 | - */ |
|
105 | - public static function handleRequestDelete(WP_REST_Request $request, $version, $model_name) |
|
106 | - { |
|
107 | - $controller = new Write(); |
|
108 | - try { |
|
109 | - $controller->setRequestedVersion($version); |
|
110 | - return $controller->sendResponse( |
|
111 | - $controller->delete( |
|
112 | - $controller->getModelVersionInfo()->loadModel($model_name), |
|
113 | - $request |
|
114 | - ) |
|
115 | - ); |
|
116 | - } catch (Exception $e) { |
|
117 | - return $controller->sendResponse($e); |
|
118 | - } |
|
119 | - } |
|
97 | + /** |
|
98 | + * Deletes a single model object and returns it. Unless |
|
99 | + * |
|
100 | + * @param WP_REST_Request $request |
|
101 | + * @param string $version |
|
102 | + * @param string $model_name |
|
103 | + * @return WP_REST_Response|\WP_Error |
|
104 | + */ |
|
105 | + public static function handleRequestDelete(WP_REST_Request $request, $version, $model_name) |
|
106 | + { |
|
107 | + $controller = new Write(); |
|
108 | + try { |
|
109 | + $controller->setRequestedVersion($version); |
|
110 | + return $controller->sendResponse( |
|
111 | + $controller->delete( |
|
112 | + $controller->getModelVersionInfo()->loadModel($model_name), |
|
113 | + $request |
|
114 | + ) |
|
115 | + ); |
|
116 | + } catch (Exception $e) { |
|
117 | + return $controller->sendResponse($e); |
|
118 | + } |
|
119 | + } |
|
120 | 120 | |
121 | 121 | |
122 | - /** |
|
123 | - * Inserts a new model object according to the $request |
|
124 | - * |
|
125 | - * @param EEM_Base $model |
|
126 | - * @param WP_REST_Request $request |
|
127 | - * @return array |
|
128 | - * @throws EE_Error |
|
129 | - * @throws RestException |
|
130 | - */ |
|
131 | - public function insert(EEM_Base $model, WP_REST_Request $request) |
|
132 | - { |
|
133 | - Capabilities::verifyAtLeastPartialAccessTo($model, EEM_Base::caps_edit, 'create'); |
|
134 | - $default_cap_to_check_for = EE_Restriction_Generator_Base::get_default_restrictions_cap(); |
|
135 | - if (! current_user_can($default_cap_to_check_for)) { |
|
136 | - throw new RestException( |
|
137 | - 'rest_cannot_create_' . EEH_Inflector::pluralize_and_lower(($model->get_this_model_name())), |
|
138 | - sprintf( |
|
139 | - esc_html__( |
|
140 | - // @codingStandardsIgnoreStart |
|
141 | - 'For now, only those with the admin capability to "%1$s" are allowed to use the REST API to insert data into Event Espresso.', |
|
142 | - // @codingStandardsIgnoreEnd |
|
143 | - 'event_espresso' |
|
144 | - ), |
|
145 | - $default_cap_to_check_for |
|
146 | - ), |
|
147 | - array('status' => 403) |
|
148 | - ); |
|
149 | - } |
|
150 | - $submitted_json_data = array_merge((array) $request->get_body_params(), (array) $request->get_json_params()); |
|
151 | - $model_data = ModelDataTranslator::prepareConditionsQueryParamsForModels( |
|
152 | - $submitted_json_data, |
|
153 | - $model, |
|
154 | - $this->getModelVersionInfo()->requestedVersion(), |
|
155 | - true |
|
156 | - ); |
|
157 | - $model_obj = EE_Registry::instance()->load_class( |
|
158 | - $model->get_this_model_name(), |
|
159 | - array($model_data, $model->get_timezone()), |
|
160 | - false, |
|
161 | - false |
|
162 | - ); |
|
163 | - $model_obj->save(); |
|
164 | - $new_id = $model_obj->ID(); |
|
165 | - if (! $new_id) { |
|
166 | - throw new RestException( |
|
167 | - 'rest_insertion_failed', |
|
168 | - sprintf(esc_html__('Could not insert new %1$s', 'event_espresso'), $model->get_this_model_name()) |
|
169 | - ); |
|
170 | - } |
|
171 | - return $this->returnModelObjAsJsonResponse($model_obj, $request); |
|
172 | - } |
|
122 | + /** |
|
123 | + * Inserts a new model object according to the $request |
|
124 | + * |
|
125 | + * @param EEM_Base $model |
|
126 | + * @param WP_REST_Request $request |
|
127 | + * @return array |
|
128 | + * @throws EE_Error |
|
129 | + * @throws RestException |
|
130 | + */ |
|
131 | + public function insert(EEM_Base $model, WP_REST_Request $request) |
|
132 | + { |
|
133 | + Capabilities::verifyAtLeastPartialAccessTo($model, EEM_Base::caps_edit, 'create'); |
|
134 | + $default_cap_to_check_for = EE_Restriction_Generator_Base::get_default_restrictions_cap(); |
|
135 | + if (! current_user_can($default_cap_to_check_for)) { |
|
136 | + throw new RestException( |
|
137 | + 'rest_cannot_create_' . EEH_Inflector::pluralize_and_lower(($model->get_this_model_name())), |
|
138 | + sprintf( |
|
139 | + esc_html__( |
|
140 | + // @codingStandardsIgnoreStart |
|
141 | + 'For now, only those with the admin capability to "%1$s" are allowed to use the REST API to insert data into Event Espresso.', |
|
142 | + // @codingStandardsIgnoreEnd |
|
143 | + 'event_espresso' |
|
144 | + ), |
|
145 | + $default_cap_to_check_for |
|
146 | + ), |
|
147 | + array('status' => 403) |
|
148 | + ); |
|
149 | + } |
|
150 | + $submitted_json_data = array_merge((array) $request->get_body_params(), (array) $request->get_json_params()); |
|
151 | + $model_data = ModelDataTranslator::prepareConditionsQueryParamsForModels( |
|
152 | + $submitted_json_data, |
|
153 | + $model, |
|
154 | + $this->getModelVersionInfo()->requestedVersion(), |
|
155 | + true |
|
156 | + ); |
|
157 | + $model_obj = EE_Registry::instance()->load_class( |
|
158 | + $model->get_this_model_name(), |
|
159 | + array($model_data, $model->get_timezone()), |
|
160 | + false, |
|
161 | + false |
|
162 | + ); |
|
163 | + $model_obj->save(); |
|
164 | + $new_id = $model_obj->ID(); |
|
165 | + if (! $new_id) { |
|
166 | + throw new RestException( |
|
167 | + 'rest_insertion_failed', |
|
168 | + sprintf(esc_html__('Could not insert new %1$s', 'event_espresso'), $model->get_this_model_name()) |
|
169 | + ); |
|
170 | + } |
|
171 | + return $this->returnModelObjAsJsonResponse($model_obj, $request); |
|
172 | + } |
|
173 | 173 | |
174 | 174 | |
175 | - /** |
|
176 | - * Updates an existing model object according to the $request |
|
177 | - * |
|
178 | - * @param EEM_Base $model |
|
179 | - * @param WP_REST_Request $request |
|
180 | - * @return array |
|
181 | - * @throws EE_Error |
|
182 | - */ |
|
183 | - public function update(EEM_Base $model, WP_REST_Request $request) |
|
184 | - { |
|
185 | - Capabilities::verifyAtLeastPartialAccessTo($model, EEM_Base::caps_edit, 'edit'); |
|
186 | - $default_cap_to_check_for = EE_Restriction_Generator_Base::get_default_restrictions_cap(); |
|
187 | - if (! current_user_can($default_cap_to_check_for)) { |
|
188 | - throw new RestException( |
|
189 | - 'rest_cannot_edit_' . EEH_Inflector::pluralize_and_lower(($model->get_this_model_name())), |
|
190 | - sprintf( |
|
191 | - esc_html__( |
|
192 | - // @codingStandardsIgnoreStart |
|
193 | - 'For now, only those with the admin capability to "%1$s" are allowed to use the REST API to update data into Event Espresso.', |
|
194 | - // @codingStandardsIgnoreEnd |
|
195 | - 'event_espresso' |
|
196 | - ), |
|
197 | - $default_cap_to_check_for |
|
198 | - ), |
|
199 | - array('status' => 403) |
|
200 | - ); |
|
201 | - } |
|
202 | - $obj_id = $request->get_param('id'); |
|
203 | - if (! $obj_id) { |
|
204 | - throw new RestException( |
|
205 | - 'rest_edit_failed', |
|
206 | - sprintf(esc_html__('Could not edit %1$s', 'event_espresso'), $model->get_this_model_name()) |
|
207 | - ); |
|
208 | - } |
|
209 | - $model_data = ModelDataTranslator::prepareConditionsQueryParamsForModels( |
|
210 | - $this->getBodyParams($request), |
|
211 | - $model, |
|
212 | - $this->getModelVersionInfo()->requestedVersion(), |
|
213 | - true |
|
214 | - ); |
|
215 | - $model_obj = $model->get_one_by_ID($obj_id); |
|
216 | - if (! $model_obj instanceof EE_Base_Class) { |
|
217 | - $lowercase_model_name = strtolower($model->get_this_model_name()); |
|
218 | - throw new RestException( |
|
219 | - sprintf('rest_%s_invalid_id', $lowercase_model_name), |
|
220 | - sprintf(esc_html__('Invalid %s ID.', 'event_espresso'), $lowercase_model_name), |
|
221 | - array('status' => 404) |
|
222 | - ); |
|
223 | - } |
|
224 | - $model_obj->save($model_data); |
|
225 | - return $this->returnModelObjAsJsonResponse($model_obj, $request); |
|
226 | - } |
|
175 | + /** |
|
176 | + * Updates an existing model object according to the $request |
|
177 | + * |
|
178 | + * @param EEM_Base $model |
|
179 | + * @param WP_REST_Request $request |
|
180 | + * @return array |
|
181 | + * @throws EE_Error |
|
182 | + */ |
|
183 | + public function update(EEM_Base $model, WP_REST_Request $request) |
|
184 | + { |
|
185 | + Capabilities::verifyAtLeastPartialAccessTo($model, EEM_Base::caps_edit, 'edit'); |
|
186 | + $default_cap_to_check_for = EE_Restriction_Generator_Base::get_default_restrictions_cap(); |
|
187 | + if (! current_user_can($default_cap_to_check_for)) { |
|
188 | + throw new RestException( |
|
189 | + 'rest_cannot_edit_' . EEH_Inflector::pluralize_and_lower(($model->get_this_model_name())), |
|
190 | + sprintf( |
|
191 | + esc_html__( |
|
192 | + // @codingStandardsIgnoreStart |
|
193 | + 'For now, only those with the admin capability to "%1$s" are allowed to use the REST API to update data into Event Espresso.', |
|
194 | + // @codingStandardsIgnoreEnd |
|
195 | + 'event_espresso' |
|
196 | + ), |
|
197 | + $default_cap_to_check_for |
|
198 | + ), |
|
199 | + array('status' => 403) |
|
200 | + ); |
|
201 | + } |
|
202 | + $obj_id = $request->get_param('id'); |
|
203 | + if (! $obj_id) { |
|
204 | + throw new RestException( |
|
205 | + 'rest_edit_failed', |
|
206 | + sprintf(esc_html__('Could not edit %1$s', 'event_espresso'), $model->get_this_model_name()) |
|
207 | + ); |
|
208 | + } |
|
209 | + $model_data = ModelDataTranslator::prepareConditionsQueryParamsForModels( |
|
210 | + $this->getBodyParams($request), |
|
211 | + $model, |
|
212 | + $this->getModelVersionInfo()->requestedVersion(), |
|
213 | + true |
|
214 | + ); |
|
215 | + $model_obj = $model->get_one_by_ID($obj_id); |
|
216 | + if (! $model_obj instanceof EE_Base_Class) { |
|
217 | + $lowercase_model_name = strtolower($model->get_this_model_name()); |
|
218 | + throw new RestException( |
|
219 | + sprintf('rest_%s_invalid_id', $lowercase_model_name), |
|
220 | + sprintf(esc_html__('Invalid %s ID.', 'event_espresso'), $lowercase_model_name), |
|
221 | + array('status' => 404) |
|
222 | + ); |
|
223 | + } |
|
224 | + $model_obj->save($model_data); |
|
225 | + return $this->returnModelObjAsJsonResponse($model_obj, $request); |
|
226 | + } |
|
227 | 227 | |
228 | 228 | |
229 | - /** |
|
230 | - * Updates an existing model object according to the $request |
|
231 | - * |
|
232 | - * @param EEM_Base $model |
|
233 | - * @param WP_REST_Request $request |
|
234 | - * @return array of either the soft-deleted item, or |
|
235 | - * @throws EE_Error |
|
236 | - */ |
|
237 | - public function delete(EEM_Base $model, WP_REST_Request $request) |
|
238 | - { |
|
239 | - Capabilities::verifyAtLeastPartialAccessTo($model, EEM_Base::caps_delete, 'delete'); |
|
240 | - $default_cap_to_check_for = EE_Restriction_Generator_Base::get_default_restrictions_cap(); |
|
241 | - if (! current_user_can($default_cap_to_check_for)) { |
|
242 | - throw new RestException( |
|
243 | - 'rest_cannot_delete_' . EEH_Inflector::pluralize_and_lower(($model->get_this_model_name())), |
|
244 | - sprintf( |
|
245 | - esc_html__( |
|
246 | - // @codingStandardsIgnoreStart |
|
247 | - 'For now, only those with the admin capability to "%1$s" are allowed to use the REST API to delete data into Event Espresso.', |
|
248 | - // @codingStandardsIgnoreEnd |
|
249 | - 'event_espresso' |
|
250 | - ), |
|
251 | - $default_cap_to_check_for |
|
252 | - ), |
|
253 | - array('status' => 403) |
|
254 | - ); |
|
255 | - } |
|
256 | - $obj_id = $request->get_param('id'); |
|
257 | - // this is where we would apply more fine-grained caps |
|
258 | - $model_obj = $model->get_one_by_ID($obj_id); |
|
259 | - if (! $model_obj instanceof EE_Base_Class) { |
|
260 | - $lowercase_model_name = strtolower($model->get_this_model_name()); |
|
261 | - throw new RestException( |
|
262 | - sprintf('rest_%s_invalid_id', $lowercase_model_name), |
|
263 | - sprintf(esc_html__('Invalid %s ID.', 'event_espresso'), $lowercase_model_name), |
|
264 | - array('status' => 404) |
|
265 | - ); |
|
266 | - } |
|
267 | - $requested_permanent_delete = filter_var($request->get_param('force'), FILTER_VALIDATE_BOOLEAN); |
|
268 | - $requested_allow_blocking = filter_var($request->get_param('allow_blocking'), FILTER_VALIDATE_BOOLEAN); |
|
269 | - if ($requested_permanent_delete) { |
|
270 | - $previous = $this->returnModelObjAsJsonResponse($model_obj, $request); |
|
271 | - $deleted = (bool) $model->delete_permanently_by_ID($obj_id, $requested_allow_blocking); |
|
272 | - return array( |
|
273 | - 'deleted' => $deleted, |
|
274 | - 'previous' => $previous, |
|
275 | - ); |
|
276 | - } else { |
|
277 | - if ($model instanceof EEM_Soft_Delete_Base) { |
|
278 | - $model->delete_by_ID($obj_id, $requested_allow_blocking); |
|
279 | - return $this->returnModelObjAsJsonResponse($model_obj, $request); |
|
280 | - } else { |
|
281 | - throw new RestException( |
|
282 | - 'rest_trash_not_supported', |
|
283 | - 501, |
|
284 | - sprintf( |
|
285 | - esc_html__('%1$s do not support trashing. Set force=1 to delete.', 'event_espresso'), |
|
286 | - EEH_Inflector::pluralize($model->get_this_model_name()) |
|
287 | - ) |
|
288 | - ); |
|
289 | - } |
|
290 | - } |
|
291 | - } |
|
229 | + /** |
|
230 | + * Updates an existing model object according to the $request |
|
231 | + * |
|
232 | + * @param EEM_Base $model |
|
233 | + * @param WP_REST_Request $request |
|
234 | + * @return array of either the soft-deleted item, or |
|
235 | + * @throws EE_Error |
|
236 | + */ |
|
237 | + public function delete(EEM_Base $model, WP_REST_Request $request) |
|
238 | + { |
|
239 | + Capabilities::verifyAtLeastPartialAccessTo($model, EEM_Base::caps_delete, 'delete'); |
|
240 | + $default_cap_to_check_for = EE_Restriction_Generator_Base::get_default_restrictions_cap(); |
|
241 | + if (! current_user_can($default_cap_to_check_for)) { |
|
242 | + throw new RestException( |
|
243 | + 'rest_cannot_delete_' . EEH_Inflector::pluralize_and_lower(($model->get_this_model_name())), |
|
244 | + sprintf( |
|
245 | + esc_html__( |
|
246 | + // @codingStandardsIgnoreStart |
|
247 | + 'For now, only those with the admin capability to "%1$s" are allowed to use the REST API to delete data into Event Espresso.', |
|
248 | + // @codingStandardsIgnoreEnd |
|
249 | + 'event_espresso' |
|
250 | + ), |
|
251 | + $default_cap_to_check_for |
|
252 | + ), |
|
253 | + array('status' => 403) |
|
254 | + ); |
|
255 | + } |
|
256 | + $obj_id = $request->get_param('id'); |
|
257 | + // this is where we would apply more fine-grained caps |
|
258 | + $model_obj = $model->get_one_by_ID($obj_id); |
|
259 | + if (! $model_obj instanceof EE_Base_Class) { |
|
260 | + $lowercase_model_name = strtolower($model->get_this_model_name()); |
|
261 | + throw new RestException( |
|
262 | + sprintf('rest_%s_invalid_id', $lowercase_model_name), |
|
263 | + sprintf(esc_html__('Invalid %s ID.', 'event_espresso'), $lowercase_model_name), |
|
264 | + array('status' => 404) |
|
265 | + ); |
|
266 | + } |
|
267 | + $requested_permanent_delete = filter_var($request->get_param('force'), FILTER_VALIDATE_BOOLEAN); |
|
268 | + $requested_allow_blocking = filter_var($request->get_param('allow_blocking'), FILTER_VALIDATE_BOOLEAN); |
|
269 | + if ($requested_permanent_delete) { |
|
270 | + $previous = $this->returnModelObjAsJsonResponse($model_obj, $request); |
|
271 | + $deleted = (bool) $model->delete_permanently_by_ID($obj_id, $requested_allow_blocking); |
|
272 | + return array( |
|
273 | + 'deleted' => $deleted, |
|
274 | + 'previous' => $previous, |
|
275 | + ); |
|
276 | + } else { |
|
277 | + if ($model instanceof EEM_Soft_Delete_Base) { |
|
278 | + $model->delete_by_ID($obj_id, $requested_allow_blocking); |
|
279 | + return $this->returnModelObjAsJsonResponse($model_obj, $request); |
|
280 | + } else { |
|
281 | + throw new RestException( |
|
282 | + 'rest_trash_not_supported', |
|
283 | + 501, |
|
284 | + sprintf( |
|
285 | + esc_html__('%1$s do not support trashing. Set force=1 to delete.', 'event_espresso'), |
|
286 | + EEH_Inflector::pluralize($model->get_this_model_name()) |
|
287 | + ) |
|
288 | + ); |
|
289 | + } |
|
290 | + } |
|
291 | + } |
|
292 | 292 | |
293 | 293 | |
294 | - /** |
|
295 | - * Returns an array ready to be converted into a JSON response, based solely on the model object |
|
296 | - * |
|
297 | - * @param EE_Base_Class $model_obj |
|
298 | - * @param WP_REST_Request $request |
|
299 | - * @return array ready for a response |
|
300 | - */ |
|
301 | - protected function returnModelObjAsJsonResponse(EE_Base_Class $model_obj, WP_REST_Request $request) |
|
302 | - { |
|
303 | - $model = $model_obj->get_model(); |
|
304 | - // create an array exactly like the wpdb results row, |
|
305 | - // so we can pass it to controllers/model/Read::create_entity_from_wpdb_result() |
|
306 | - $simulated_db_row = array(); |
|
307 | - foreach ($model->field_settings(true) as $field_name => $field_obj) { |
|
308 | - // we need to reconstruct the normal wpdb results, including the db-only fields |
|
309 | - // like a secondary table's primary key. The models expect those (but don't care what value they have) |
|
310 | - if ($field_obj instanceof EE_DB_Only_Field_Base) { |
|
311 | - $raw_value = true; |
|
312 | - } elseif ($field_obj instanceof EE_Datetime_Field) { |
|
313 | - $raw_value = $model_obj->get_DateTime_object($field_name); |
|
314 | - } else { |
|
315 | - $raw_value = $model_obj->get_raw($field_name); |
|
316 | - } |
|
317 | - $simulated_db_row[ $field_obj->get_qualified_column() ] = $field_obj->prepare_for_use_in_db($raw_value); |
|
318 | - } |
|
319 | - $read_controller = LoaderFactory::getLoader()->getNew('EventEspresso\core\libraries\rest_api\controllers\model\Read'); |
|
320 | - $read_controller->setRequestedVersion($this->getRequestedVersion()); |
|
321 | - // the simulates request really doesn't need any info downstream |
|
322 | - $simulated_request = new WP_REST_Request('GET'); |
|
323 | - // set the caps context on the simulated according to the original request. |
|
324 | - switch ($request->get_method()) { |
|
325 | - case 'POST': |
|
326 | - case 'PUT': |
|
327 | - $caps_context = EEM_Base::caps_edit; |
|
328 | - break; |
|
329 | - case 'DELETE': |
|
330 | - $caps_context = EEM_Base::caps_delete; |
|
331 | - break; |
|
332 | - default: |
|
333 | - $caps_context = EEM_Base::caps_read_admin; |
|
334 | - } |
|
335 | - $simulated_request->set_param('caps', $caps_context); |
|
336 | - return $read_controller->createEntityFromWpdbResult( |
|
337 | - $model_obj->get_model(), |
|
338 | - $simulated_db_row, |
|
339 | - $simulated_request |
|
340 | - ); |
|
341 | - } |
|
294 | + /** |
|
295 | + * Returns an array ready to be converted into a JSON response, based solely on the model object |
|
296 | + * |
|
297 | + * @param EE_Base_Class $model_obj |
|
298 | + * @param WP_REST_Request $request |
|
299 | + * @return array ready for a response |
|
300 | + */ |
|
301 | + protected function returnModelObjAsJsonResponse(EE_Base_Class $model_obj, WP_REST_Request $request) |
|
302 | + { |
|
303 | + $model = $model_obj->get_model(); |
|
304 | + // create an array exactly like the wpdb results row, |
|
305 | + // so we can pass it to controllers/model/Read::create_entity_from_wpdb_result() |
|
306 | + $simulated_db_row = array(); |
|
307 | + foreach ($model->field_settings(true) as $field_name => $field_obj) { |
|
308 | + // we need to reconstruct the normal wpdb results, including the db-only fields |
|
309 | + // like a secondary table's primary key. The models expect those (but don't care what value they have) |
|
310 | + if ($field_obj instanceof EE_DB_Only_Field_Base) { |
|
311 | + $raw_value = true; |
|
312 | + } elseif ($field_obj instanceof EE_Datetime_Field) { |
|
313 | + $raw_value = $model_obj->get_DateTime_object($field_name); |
|
314 | + } else { |
|
315 | + $raw_value = $model_obj->get_raw($field_name); |
|
316 | + } |
|
317 | + $simulated_db_row[ $field_obj->get_qualified_column() ] = $field_obj->prepare_for_use_in_db($raw_value); |
|
318 | + } |
|
319 | + $read_controller = LoaderFactory::getLoader()->getNew('EventEspresso\core\libraries\rest_api\controllers\model\Read'); |
|
320 | + $read_controller->setRequestedVersion($this->getRequestedVersion()); |
|
321 | + // the simulates request really doesn't need any info downstream |
|
322 | + $simulated_request = new WP_REST_Request('GET'); |
|
323 | + // set the caps context on the simulated according to the original request. |
|
324 | + switch ($request->get_method()) { |
|
325 | + case 'POST': |
|
326 | + case 'PUT': |
|
327 | + $caps_context = EEM_Base::caps_edit; |
|
328 | + break; |
|
329 | + case 'DELETE': |
|
330 | + $caps_context = EEM_Base::caps_delete; |
|
331 | + break; |
|
332 | + default: |
|
333 | + $caps_context = EEM_Base::caps_read_admin; |
|
334 | + } |
|
335 | + $simulated_request->set_param('caps', $caps_context); |
|
336 | + return $read_controller->createEntityFromWpdbResult( |
|
337 | + $model_obj->get_model(), |
|
338 | + $simulated_db_row, |
|
339 | + $simulated_request |
|
340 | + ); |
|
341 | + } |
|
342 | 342 | |
343 | 343 | |
344 | - /** |
|
345 | - * Gets the item affected by this request |
|
346 | - * |
|
347 | - * @param EEM_Base $model |
|
348 | - * @param WP_REST_Request $request |
|
349 | - * @param int|string $obj_id |
|
350 | - * @return \WP_Error|array |
|
351 | - */ |
|
352 | - protected function getOneBasedOnRequest(EEM_Base $model, WP_REST_Request $request, $obj_id) |
|
353 | - { |
|
354 | - $requested_version = $this->getRequestedVersion($request->get_route()); |
|
355 | - $get_request = new WP_REST_Request( |
|
356 | - 'GET', |
|
357 | - EED_Core_Rest_Api::ee_api_namespace |
|
358 | - . $requested_version |
|
359 | - . '/' |
|
360 | - . EEH_Inflector::pluralize_and_lower($model->get_this_model_name()) |
|
361 | - . '/' |
|
362 | - . $obj_id |
|
363 | - ); |
|
364 | - $get_request->set_url_params( |
|
365 | - array( |
|
366 | - 'id' => $obj_id, |
|
367 | - 'include' => $request->get_param('include'), |
|
368 | - ) |
|
369 | - ); |
|
370 | - $read_controller = new Read(); |
|
371 | - $read_controller->setRequestedVersion($this->getRequestedVersion()); |
|
372 | - return $read_controller->getEntityFromModel($model, $get_request); |
|
373 | - } |
|
344 | + /** |
|
345 | + * Gets the item affected by this request |
|
346 | + * |
|
347 | + * @param EEM_Base $model |
|
348 | + * @param WP_REST_Request $request |
|
349 | + * @param int|string $obj_id |
|
350 | + * @return \WP_Error|array |
|
351 | + */ |
|
352 | + protected function getOneBasedOnRequest(EEM_Base $model, WP_REST_Request $request, $obj_id) |
|
353 | + { |
|
354 | + $requested_version = $this->getRequestedVersion($request->get_route()); |
|
355 | + $get_request = new WP_REST_Request( |
|
356 | + 'GET', |
|
357 | + EED_Core_Rest_Api::ee_api_namespace |
|
358 | + . $requested_version |
|
359 | + . '/' |
|
360 | + . EEH_Inflector::pluralize_and_lower($model->get_this_model_name()) |
|
361 | + . '/' |
|
362 | + . $obj_id |
|
363 | + ); |
|
364 | + $get_request->set_url_params( |
|
365 | + array( |
|
366 | + 'id' => $obj_id, |
|
367 | + 'include' => $request->get_param('include'), |
|
368 | + ) |
|
369 | + ); |
|
370 | + $read_controller = new Read(); |
|
371 | + $read_controller->setRequestedVersion($this->getRequestedVersion()); |
|
372 | + return $read_controller->getEntityFromModel($model, $get_request); |
|
373 | + } |
|
374 | 374 | |
375 | - /** |
|
376 | - * Adds a relation between the specified models (if it doesn't already exist.) |
|
377 | - * @since 4.9.76.p |
|
378 | - * @param WP_REST_Request $request |
|
379 | - * @return WP_REST_Response |
|
380 | - */ |
|
381 | - public static function handleRequestAddRelation(WP_REST_Request $request, $version, $model_name, $related_model_name) |
|
382 | - { |
|
383 | - $controller = new Write(); |
|
384 | - try { |
|
385 | - $controller->setRequestedVersion($version); |
|
386 | - $main_model = $controller->validateModel($model_name); |
|
387 | - $controller->validateModel($related_model_name); |
|
388 | - return $controller->sendResponse( |
|
389 | - $controller->addRelation( |
|
390 | - $main_model, |
|
391 | - $main_model->related_settings_for($related_model_name), |
|
392 | - $request |
|
393 | - ) |
|
394 | - ); |
|
395 | - } catch (Exception $e) { |
|
396 | - return $controller->sendResponse($e); |
|
397 | - } |
|
398 | - } |
|
375 | + /** |
|
376 | + * Adds a relation between the specified models (if it doesn't already exist.) |
|
377 | + * @since 4.9.76.p |
|
378 | + * @param WP_REST_Request $request |
|
379 | + * @return WP_REST_Response |
|
380 | + */ |
|
381 | + public static function handleRequestAddRelation(WP_REST_Request $request, $version, $model_name, $related_model_name) |
|
382 | + { |
|
383 | + $controller = new Write(); |
|
384 | + try { |
|
385 | + $controller->setRequestedVersion($version); |
|
386 | + $main_model = $controller->validateModel($model_name); |
|
387 | + $controller->validateModel($related_model_name); |
|
388 | + return $controller->sendResponse( |
|
389 | + $controller->addRelation( |
|
390 | + $main_model, |
|
391 | + $main_model->related_settings_for($related_model_name), |
|
392 | + $request |
|
393 | + ) |
|
394 | + ); |
|
395 | + } catch (Exception $e) { |
|
396 | + return $controller->sendResponse($e); |
|
397 | + } |
|
398 | + } |
|
399 | 399 | |
400 | - /** |
|
401 | - * Adds a relation between the two model specified model objects. |
|
402 | - * @since 4.9.76.p |
|
403 | - * @param EEM_Base $model |
|
404 | - * @param EE_Model_Relation_Base $relation |
|
405 | - * @param WP_REST_Request $request |
|
406 | - * @return array |
|
407 | - * @throws EE_Error |
|
408 | - * @throws InvalidArgumentException |
|
409 | - * @throws InvalidDataTypeException |
|
410 | - * @throws InvalidInterfaceException |
|
411 | - * @throws RestException |
|
412 | - * @throws DomainException |
|
413 | - */ |
|
414 | - public function addRelation(EEM_Base $model, EE_Model_Relation_Base $relation, WP_REST_Request $request) |
|
415 | - { |
|
416 | - list($model_obj, $other_obj) = $this->getBothModelObjects($model, $relation, $request); |
|
417 | - $extra_params = array(); |
|
418 | - if ($relation instanceof EE_HABTM_Relation) { |
|
419 | - $extra_params = array_intersect_key( |
|
420 | - ModelDataTranslator::prepareConditionsQueryParamsForModels( |
|
421 | - $request->get_body_params(), |
|
422 | - $relation->get_join_model(), |
|
423 | - $this->getModelVersionInfo()->requestedVersion(), |
|
424 | - true |
|
425 | - ), |
|
426 | - $relation->getNonKeyFields() |
|
427 | - ); |
|
428 | - } |
|
429 | - // Add a relation. |
|
430 | - $related_obj = $model_obj->_add_relation_to( |
|
431 | - $other_obj, |
|
432 | - $relation->get_other_model()->get_this_model_name(), |
|
433 | - $extra_params |
|
434 | - ); |
|
435 | - $response = array( |
|
436 | - strtolower($model->get_this_model_name()) => $this->returnModelObjAsJsonResponse($model_obj, $request), |
|
437 | - strtolower($relation->get_other_model()->get_this_model_name()) => $this->returnModelObjAsJsonResponse($related_obj, $request), |
|
438 | - ); |
|
439 | - if ($relation instanceof EE_HABTM_Relation) { |
|
440 | - $join_model_obj = $relation->get_join_model()->get_one( |
|
441 | - array( |
|
442 | - array( |
|
443 | - $relation->get_join_model()->get_foreign_key_to($model->get_this_model_name())->get_name() => $model_obj->ID(), |
|
444 | - $relation->get_join_model()->get_foreign_key_to($relation->get_other_model()->get_this_model_name())->get_name() => $related_obj->ID() |
|
445 | - ) |
|
446 | - ) |
|
447 | - ); |
|
448 | - $response['join'][ strtolower($relation->get_join_model()->get_this_model_name()) ] = $this->returnModelObjAsJsonResponse($join_model_obj, $request); |
|
449 | - } |
|
450 | - return $response; |
|
451 | - } |
|
400 | + /** |
|
401 | + * Adds a relation between the two model specified model objects. |
|
402 | + * @since 4.9.76.p |
|
403 | + * @param EEM_Base $model |
|
404 | + * @param EE_Model_Relation_Base $relation |
|
405 | + * @param WP_REST_Request $request |
|
406 | + * @return array |
|
407 | + * @throws EE_Error |
|
408 | + * @throws InvalidArgumentException |
|
409 | + * @throws InvalidDataTypeException |
|
410 | + * @throws InvalidInterfaceException |
|
411 | + * @throws RestException |
|
412 | + * @throws DomainException |
|
413 | + */ |
|
414 | + public function addRelation(EEM_Base $model, EE_Model_Relation_Base $relation, WP_REST_Request $request) |
|
415 | + { |
|
416 | + list($model_obj, $other_obj) = $this->getBothModelObjects($model, $relation, $request); |
|
417 | + $extra_params = array(); |
|
418 | + if ($relation instanceof EE_HABTM_Relation) { |
|
419 | + $extra_params = array_intersect_key( |
|
420 | + ModelDataTranslator::prepareConditionsQueryParamsForModels( |
|
421 | + $request->get_body_params(), |
|
422 | + $relation->get_join_model(), |
|
423 | + $this->getModelVersionInfo()->requestedVersion(), |
|
424 | + true |
|
425 | + ), |
|
426 | + $relation->getNonKeyFields() |
|
427 | + ); |
|
428 | + } |
|
429 | + // Add a relation. |
|
430 | + $related_obj = $model_obj->_add_relation_to( |
|
431 | + $other_obj, |
|
432 | + $relation->get_other_model()->get_this_model_name(), |
|
433 | + $extra_params |
|
434 | + ); |
|
435 | + $response = array( |
|
436 | + strtolower($model->get_this_model_name()) => $this->returnModelObjAsJsonResponse($model_obj, $request), |
|
437 | + strtolower($relation->get_other_model()->get_this_model_name()) => $this->returnModelObjAsJsonResponse($related_obj, $request), |
|
438 | + ); |
|
439 | + if ($relation instanceof EE_HABTM_Relation) { |
|
440 | + $join_model_obj = $relation->get_join_model()->get_one( |
|
441 | + array( |
|
442 | + array( |
|
443 | + $relation->get_join_model()->get_foreign_key_to($model->get_this_model_name())->get_name() => $model_obj->ID(), |
|
444 | + $relation->get_join_model()->get_foreign_key_to($relation->get_other_model()->get_this_model_name())->get_name() => $related_obj->ID() |
|
445 | + ) |
|
446 | + ) |
|
447 | + ); |
|
448 | + $response['join'][ strtolower($relation->get_join_model()->get_this_model_name()) ] = $this->returnModelObjAsJsonResponse($join_model_obj, $request); |
|
449 | + } |
|
450 | + return $response; |
|
451 | + } |
|
452 | 452 | |
453 | 453 | |
454 | - /** |
|
455 | - * Removes the relation between the specified models (if it exists). |
|
456 | - * @since 4.9.76.p |
|
457 | - * @param WP_REST_Request $request |
|
458 | - * @return WP_REST_Response |
|
459 | - */ |
|
460 | - public static function handleRequestRemoveRelation(WP_REST_Request $request, $version, $model_name, $related_model_name) |
|
461 | - { |
|
462 | - $controller = new Write(); |
|
463 | - try { |
|
464 | - $controller->setRequestedVersion($version); |
|
465 | - $main_model = $controller->getModelVersionInfo()->loadModel($model_name); |
|
466 | - return $controller->sendResponse( |
|
467 | - $controller->removeRelation( |
|
468 | - $main_model, |
|
469 | - $main_model->related_settings_for($related_model_name), |
|
470 | - $request |
|
471 | - ) |
|
472 | - ); |
|
473 | - } catch (Exception $e) { |
|
474 | - return $controller->sendResponse($e); |
|
475 | - } |
|
476 | - } |
|
454 | + /** |
|
455 | + * Removes the relation between the specified models (if it exists). |
|
456 | + * @since 4.9.76.p |
|
457 | + * @param WP_REST_Request $request |
|
458 | + * @return WP_REST_Response |
|
459 | + */ |
|
460 | + public static function handleRequestRemoveRelation(WP_REST_Request $request, $version, $model_name, $related_model_name) |
|
461 | + { |
|
462 | + $controller = new Write(); |
|
463 | + try { |
|
464 | + $controller->setRequestedVersion($version); |
|
465 | + $main_model = $controller->getModelVersionInfo()->loadModel($model_name); |
|
466 | + return $controller->sendResponse( |
|
467 | + $controller->removeRelation( |
|
468 | + $main_model, |
|
469 | + $main_model->related_settings_for($related_model_name), |
|
470 | + $request |
|
471 | + ) |
|
472 | + ); |
|
473 | + } catch (Exception $e) { |
|
474 | + return $controller->sendResponse($e); |
|
475 | + } |
|
476 | + } |
|
477 | 477 | |
478 | - /** |
|
479 | - * Adds a relation between the two model specified model objects. |
|
480 | - * @since 4.9.76.p |
|
481 | - * @param EEM_Base $model |
|
482 | - * @param EE_Model_Relation_Base $relation |
|
483 | - * @param WP_REST_Request $request |
|
484 | - * @return array |
|
485 | - * @throws DomainException |
|
486 | - * @throws EE_Error |
|
487 | - * @throws InvalidArgumentException |
|
488 | - * @throws InvalidDataTypeException |
|
489 | - * @throws InvalidInterfaceException |
|
490 | - * @throws RestException |
|
491 | - */ |
|
492 | - public function removeRelation(EEM_Base $model, EE_Model_Relation_Base $relation, WP_REST_Request $request) |
|
493 | - { |
|
494 | - // This endpoint doesn't accept body parameters (it's understandable to think it might, so let developers know |
|
495 | - // up-front that it doesn't.) |
|
496 | - if (!empty($request->get_body_params())) { |
|
497 | - $body_params = $request->get_body_params(); |
|
498 | - throw new RestException( |
|
499 | - 'invalid_field', |
|
500 | - sprintf( |
|
501 | - esc_html__('This endpoint doesn\'t accept post body arguments, you sent in %1$s', 'event_espresso'), |
|
502 | - implode(array_keys($body_params)) |
|
503 | - ) |
|
504 | - ); |
|
505 | - } |
|
506 | - list($model_obj, $other_obj) = $this->getBothModelObjects($model, $relation, $request); |
|
507 | - // Remember the old relation, if it used a join entry. |
|
508 | - $join_model_obj = null; |
|
509 | - if ($relation instanceof EE_HABTM_Relation) { |
|
510 | - $join_model_obj = $relation->get_join_model()->get_one( |
|
511 | - array( |
|
512 | - array( |
|
513 | - $model->primary_key_name() => $model_obj->ID(), |
|
514 | - $relation->get_other_model()->primary_key_name() => $other_obj->ID() |
|
515 | - ) |
|
516 | - ) |
|
517 | - ); |
|
518 | - } |
|
519 | - // Remove the relation. |
|
520 | - $related_obj = $model_obj->_remove_relation_to( |
|
521 | - $other_obj, |
|
522 | - $relation->get_other_model()->get_this_model_name() |
|
523 | - ); |
|
524 | - $response = array( |
|
525 | - strtolower($model->get_this_model_name()) => $this->returnModelObjAsJsonResponse($model_obj, $request), |
|
526 | - strtolower($relation->get_other_model()->get_this_model_name()) => $this->returnModelObjAsJsonResponse($related_obj, $request), |
|
527 | - ); |
|
528 | - if ($relation instanceof EE_HABTM_Relation) { |
|
529 | - $join_model_obj_after_removal = $relation->get_join_model()->get_one( |
|
530 | - array( |
|
531 | - array( |
|
532 | - $model->primary_key_name() => $model_obj->ID(), |
|
533 | - $relation->get_other_model()->primary_key_name() => $other_obj->ID() |
|
534 | - ) |
|
535 | - ) |
|
536 | - ); |
|
537 | - if ($join_model_obj instanceof EE_Base_Class) { |
|
538 | - $response['join'][ strtolower($relation->get_join_model()->get_this_model_name()) ] = $this->returnModelObjAsJsonResponse($join_model_obj, $request); |
|
539 | - } else { |
|
540 | - $response['join'][ strtolower($relation->get_join_model()->get_this_model_name()) ] = null; |
|
541 | - } |
|
542 | - } |
|
543 | - return $response; |
|
544 | - } |
|
478 | + /** |
|
479 | + * Adds a relation between the two model specified model objects. |
|
480 | + * @since 4.9.76.p |
|
481 | + * @param EEM_Base $model |
|
482 | + * @param EE_Model_Relation_Base $relation |
|
483 | + * @param WP_REST_Request $request |
|
484 | + * @return array |
|
485 | + * @throws DomainException |
|
486 | + * @throws EE_Error |
|
487 | + * @throws InvalidArgumentException |
|
488 | + * @throws InvalidDataTypeException |
|
489 | + * @throws InvalidInterfaceException |
|
490 | + * @throws RestException |
|
491 | + */ |
|
492 | + public function removeRelation(EEM_Base $model, EE_Model_Relation_Base $relation, WP_REST_Request $request) |
|
493 | + { |
|
494 | + // This endpoint doesn't accept body parameters (it's understandable to think it might, so let developers know |
|
495 | + // up-front that it doesn't.) |
|
496 | + if (!empty($request->get_body_params())) { |
|
497 | + $body_params = $request->get_body_params(); |
|
498 | + throw new RestException( |
|
499 | + 'invalid_field', |
|
500 | + sprintf( |
|
501 | + esc_html__('This endpoint doesn\'t accept post body arguments, you sent in %1$s', 'event_espresso'), |
|
502 | + implode(array_keys($body_params)) |
|
503 | + ) |
|
504 | + ); |
|
505 | + } |
|
506 | + list($model_obj, $other_obj) = $this->getBothModelObjects($model, $relation, $request); |
|
507 | + // Remember the old relation, if it used a join entry. |
|
508 | + $join_model_obj = null; |
|
509 | + if ($relation instanceof EE_HABTM_Relation) { |
|
510 | + $join_model_obj = $relation->get_join_model()->get_one( |
|
511 | + array( |
|
512 | + array( |
|
513 | + $model->primary_key_name() => $model_obj->ID(), |
|
514 | + $relation->get_other_model()->primary_key_name() => $other_obj->ID() |
|
515 | + ) |
|
516 | + ) |
|
517 | + ); |
|
518 | + } |
|
519 | + // Remove the relation. |
|
520 | + $related_obj = $model_obj->_remove_relation_to( |
|
521 | + $other_obj, |
|
522 | + $relation->get_other_model()->get_this_model_name() |
|
523 | + ); |
|
524 | + $response = array( |
|
525 | + strtolower($model->get_this_model_name()) => $this->returnModelObjAsJsonResponse($model_obj, $request), |
|
526 | + strtolower($relation->get_other_model()->get_this_model_name()) => $this->returnModelObjAsJsonResponse($related_obj, $request), |
|
527 | + ); |
|
528 | + if ($relation instanceof EE_HABTM_Relation) { |
|
529 | + $join_model_obj_after_removal = $relation->get_join_model()->get_one( |
|
530 | + array( |
|
531 | + array( |
|
532 | + $model->primary_key_name() => $model_obj->ID(), |
|
533 | + $relation->get_other_model()->primary_key_name() => $other_obj->ID() |
|
534 | + ) |
|
535 | + ) |
|
536 | + ); |
|
537 | + if ($join_model_obj instanceof EE_Base_Class) { |
|
538 | + $response['join'][ strtolower($relation->get_join_model()->get_this_model_name()) ] = $this->returnModelObjAsJsonResponse($join_model_obj, $request); |
|
539 | + } else { |
|
540 | + $response['join'][ strtolower($relation->get_join_model()->get_this_model_name()) ] = null; |
|
541 | + } |
|
542 | + } |
|
543 | + return $response; |
|
544 | + } |
|
545 | 545 | |
546 | - /** |
|
547 | - * Gets the model objects indicated by the model, relation object, and request. |
|
548 | - * Throws an exception if the first object doesn't exist, and currently if the related object also doesn't exist. |
|
549 | - * However, this behaviour may change, as we may add support for simultaneously creating and relating data. |
|
550 | - * @since 4.9.76.p |
|
551 | - * @param EEM_Base $model |
|
552 | - * @param EE_Model_Relation_Base $relation |
|
553 | - * @param WP_REST_Request $request |
|
554 | - * @return array { |
|
555 | - * @type EE_Base_Class $model_obj |
|
556 | - * @type EE_Base_Class|null $other_model_obj |
|
557 | - * } |
|
558 | - * @throws RestException |
|
559 | - */ |
|
560 | - protected function getBothModelObjects(EEM_Base $model, EE_Model_Relation_Base $relation, WP_REST_Request $request) |
|
561 | - { |
|
562 | - // Check generic caps. For now, we're only allowing access to this endpoint to full admins. |
|
563 | - Capabilities::verifyAtLeastPartialAccessTo($model, EEM_Base::caps_edit, 'edit'); |
|
564 | - $default_cap_to_check_for = EE_Restriction_Generator_Base::get_default_restrictions_cap(); |
|
565 | - if (! current_user_can($default_cap_to_check_for)) { |
|
566 | - throw new RestException( |
|
567 | - 'rest_cannot_edit_' . EEH_Inflector::pluralize_and_lower(($model->get_this_model_name())), |
|
568 | - sprintf( |
|
569 | - esc_html__( |
|
570 | - // @codingStandardsIgnoreStart |
|
571 | - 'For now, only those with the admin capability to "%1$s" are allowed to use the REST API to add relations in Event Espresso.', |
|
572 | - // @codingStandardsIgnoreEnd |
|
573 | - 'event_espresso' |
|
574 | - ), |
|
575 | - $default_cap_to_check_for |
|
576 | - ), |
|
577 | - array('status' => 403) |
|
578 | - ); |
|
579 | - } |
|
580 | - // Get the main model object. |
|
581 | - $model_obj = $this->getOneOrThrowException($model, $request->get_param('id')); |
|
582 | - // For now, we require the other model object to exist too. This might be relaxed later. |
|
583 | - $other_obj = $this->getOneOrThrowException($relation->get_other_model(), $request->get_param('related_id')); |
|
584 | - return array($model_obj,$other_obj); |
|
585 | - } |
|
546 | + /** |
|
547 | + * Gets the model objects indicated by the model, relation object, and request. |
|
548 | + * Throws an exception if the first object doesn't exist, and currently if the related object also doesn't exist. |
|
549 | + * However, this behaviour may change, as we may add support for simultaneously creating and relating data. |
|
550 | + * @since 4.9.76.p |
|
551 | + * @param EEM_Base $model |
|
552 | + * @param EE_Model_Relation_Base $relation |
|
553 | + * @param WP_REST_Request $request |
|
554 | + * @return array { |
|
555 | + * @type EE_Base_Class $model_obj |
|
556 | + * @type EE_Base_Class|null $other_model_obj |
|
557 | + * } |
|
558 | + * @throws RestException |
|
559 | + */ |
|
560 | + protected function getBothModelObjects(EEM_Base $model, EE_Model_Relation_Base $relation, WP_REST_Request $request) |
|
561 | + { |
|
562 | + // Check generic caps. For now, we're only allowing access to this endpoint to full admins. |
|
563 | + Capabilities::verifyAtLeastPartialAccessTo($model, EEM_Base::caps_edit, 'edit'); |
|
564 | + $default_cap_to_check_for = EE_Restriction_Generator_Base::get_default_restrictions_cap(); |
|
565 | + if (! current_user_can($default_cap_to_check_for)) { |
|
566 | + throw new RestException( |
|
567 | + 'rest_cannot_edit_' . EEH_Inflector::pluralize_and_lower(($model->get_this_model_name())), |
|
568 | + sprintf( |
|
569 | + esc_html__( |
|
570 | + // @codingStandardsIgnoreStart |
|
571 | + 'For now, only those with the admin capability to "%1$s" are allowed to use the REST API to add relations in Event Espresso.', |
|
572 | + // @codingStandardsIgnoreEnd |
|
573 | + 'event_espresso' |
|
574 | + ), |
|
575 | + $default_cap_to_check_for |
|
576 | + ), |
|
577 | + array('status' => 403) |
|
578 | + ); |
|
579 | + } |
|
580 | + // Get the main model object. |
|
581 | + $model_obj = $this->getOneOrThrowException($model, $request->get_param('id')); |
|
582 | + // For now, we require the other model object to exist too. This might be relaxed later. |
|
583 | + $other_obj = $this->getOneOrThrowException($relation->get_other_model(), $request->get_param('related_id')); |
|
584 | + return array($model_obj,$other_obj); |
|
585 | + } |
|
586 | 586 | |
587 | - /** |
|
588 | - * Gets the model with that ID or throws a REST exception. |
|
589 | - * @since 4.9.76.p |
|
590 | - * @param EEM_Base $model |
|
591 | - * @param $id |
|
592 | - * @return EE_Base_Class |
|
593 | - * @throws RestException |
|
594 | - */ |
|
595 | - protected function getOneOrThrowException(EEM_Base $model, $id) |
|
596 | - { |
|
597 | - $model_obj = $model->get_one_by_ID($id); |
|
598 | - // @todo: check they can permission for it. For now unnecessary because only full admins can use this endpoint. |
|
599 | - if ($model_obj instanceof EE_Base_Class) { |
|
600 | - return $model_obj; |
|
601 | - } |
|
602 | - $lowercase_model_name = strtolower($model->get_this_model_name()); |
|
603 | - throw new RestException( |
|
604 | - sprintf('rest_%s_invalid_id', $lowercase_model_name), |
|
605 | - sprintf(esc_html__('Invalid %s ID.', 'event_espresso'), $lowercase_model_name), |
|
606 | - array('status' => 404) |
|
607 | - ); |
|
608 | - } |
|
587 | + /** |
|
588 | + * Gets the model with that ID or throws a REST exception. |
|
589 | + * @since 4.9.76.p |
|
590 | + * @param EEM_Base $model |
|
591 | + * @param $id |
|
592 | + * @return EE_Base_Class |
|
593 | + * @throws RestException |
|
594 | + */ |
|
595 | + protected function getOneOrThrowException(EEM_Base $model, $id) |
|
596 | + { |
|
597 | + $model_obj = $model->get_one_by_ID($id); |
|
598 | + // @todo: check they can permission for it. For now unnecessary because only full admins can use this endpoint. |
|
599 | + if ($model_obj instanceof EE_Base_Class) { |
|
600 | + return $model_obj; |
|
601 | + } |
|
602 | + $lowercase_model_name = strtolower($model->get_this_model_name()); |
|
603 | + throw new RestException( |
|
604 | + sprintf('rest_%s_invalid_id', $lowercase_model_name), |
|
605 | + sprintf(esc_html__('Invalid %s ID.', 'event_espresso'), $lowercase_model_name), |
|
606 | + array('status' => 404) |
|
607 | + ); |
|
608 | + } |
|
609 | 609 | } |
@@ -95,7 +95,7 @@ discard block |
||
95 | 95 | */ |
96 | 96 | protected function setDebugInfo($key, $info) |
97 | 97 | { |
98 | - $this->debug_info[ $key ] = $info; |
|
98 | + $this->debug_info[$key] = $info; |
|
99 | 99 | } |
100 | 100 | |
101 | 101 | |
@@ -112,11 +112,11 @@ discard block |
||
112 | 112 | { |
113 | 113 | if (is_array($value)) { |
114 | 114 | foreach ($value as $value_key => $value_value) { |
115 | - $this->setResponseHeader($header_key . '[' . $value_key . ']', $value_value); |
|
115 | + $this->setResponseHeader($header_key.'['.$value_key.']', $value_value); |
|
116 | 116 | } |
117 | 117 | } else { |
118 | 118 | $prefix = $use_ee_prefix ? Base::HEADER_PREFIX_FOR_EE : Base::HEADER_PREFIX_FOR_WP; |
119 | - $this->response_headers[ $prefix . $header_key ] = $value; |
|
119 | + $this->response_headers[$prefix.$header_key] = $value; |
|
120 | 120 | } |
121 | 121 | } |
122 | 122 | |
@@ -146,7 +146,7 @@ discard block |
||
146 | 146 | protected function addEeErrorsToResponse(WP_Error $wp_error_response) |
147 | 147 | { |
148 | 148 | $notices_during_checkin = EE_Error::get_raw_notices(); |
149 | - if (! empty($notices_during_checkin['errors'])) { |
|
149 | + if ( ! empty($notices_during_checkin['errors'])) { |
|
150 | 150 | foreach ($notices_during_checkin['errors'] as $error_code => $error_message) { |
151 | 151 | $wp_error_response->add( |
152 | 152 | sanitize_key($error_code), |
@@ -189,7 +189,7 @@ discard block |
||
189 | 189 | if (is_array($debug_info)) { |
190 | 190 | $debug_info = wp_json_encode($debug_info); |
191 | 191 | } |
192 | - $headers[ 'X-EE4-Debug-' . ucwords($debug_key) ] = $debug_info; |
|
192 | + $headers['X-EE4-Debug-'.ucwords($debug_key)] = $debug_info; |
|
193 | 193 | } |
194 | 194 | } |
195 | 195 | $headers = array_merge( |
@@ -248,15 +248,15 @@ discard block |
||
248 | 248 | $headers = array(); |
249 | 249 | $notices = EE_Error::get_raw_notices(); |
250 | 250 | foreach ($notices as $notice_type => $sub_notices) { |
251 | - if (! is_array($sub_notices)) { |
|
251 | + if ( ! is_array($sub_notices)) { |
|
252 | 252 | continue; |
253 | 253 | } |
254 | 254 | foreach ($sub_notices as $notice_code => $sub_notice) { |
255 | - $headers[ 'X-EE4-Notices-' |
|
255 | + $headers['X-EE4-Notices-' |
|
256 | 256 | . EEH_Inflector::humanize($notice_type) |
257 | 257 | . '[' |
258 | 258 | . $notice_code |
259 | - . ']' ] = strip_tags($sub_notice); |
|
259 | + . ']'] = strip_tags($sub_notice); |
|
260 | 260 | } |
261 | 261 | } |
262 | 262 | return apply_filters( |
@@ -285,7 +285,7 @@ discard block |
||
285 | 285 | } |
286 | 286 | $matches = $this->parseRoute( |
287 | 287 | $route, |
288 | - '~' . EED_Core_Rest_Api::ee_api_namespace_for_regex . '~', |
|
288 | + '~'.EED_Core_Rest_Api::ee_api_namespace_for_regex.'~', |
|
289 | 289 | array('version') |
290 | 290 | ); |
291 | 291 | if (isset($matches['version'])) { |
@@ -319,14 +319,14 @@ discard block |
||
319 | 319 | if (is_array($matches)) { |
320 | 320 | // skip the overall regex match. Who cares |
321 | 321 | for ($i = 1; $i <= count($match_keys); $i++) { |
322 | - if (! isset($matches[ $i ])) { |
|
322 | + if ( ! isset($matches[$i])) { |
|
323 | 323 | $success = false; |
324 | 324 | } else { |
325 | - $indexed_matches[ $match_keys[ $i - 1 ] ] = $matches[ $i ]; |
|
325 | + $indexed_matches[$match_keys[$i - 1]] = $matches[$i]; |
|
326 | 326 | } |
327 | 327 | } |
328 | 328 | } |
329 | - if (! $success) { |
|
329 | + if ( ! $success) { |
|
330 | 330 | throw new EE_Error( |
331 | 331 | esc_html__('We could not parse the URL. Please contact Event Espresso Support', 'event_espresso'), |
332 | 332 | 'endpoint_parsing_error' |
@@ -20,338 +20,338 @@ |
||
20 | 20 | */ |
21 | 21 | class Base |
22 | 22 | { |
23 | - /** |
|
24 | - * @deprecated use all-caps version |
|
25 | - */ |
|
26 | - // @codingStandardsIgnoreStart |
|
27 | - const header_prefix_for_ee = 'X-EE-'; |
|
28 | - // @codingStandardsIgnoreEnd |
|
29 | - |
|
30 | - const HEADER_PREFIX_FOR_EE = 'X-EE-'; |
|
31 | - |
|
32 | - /** |
|
33 | - * @deprecated use all-caps version instead |
|
34 | - */ |
|
35 | - // @codingStandardsIgnoreStart |
|
36 | - const header_prefix_for_wp = 'X-WP-'; |
|
37 | - // @codingStandardsIgnoreEnd |
|
38 | - |
|
39 | - const HEADER_PREFIX_FOR_WP = 'X-WP-'; |
|
40 | - |
|
41 | - /** |
|
42 | - * Contains debug info we'll send back in the response headers |
|
43 | - * |
|
44 | - * @var array |
|
45 | - */ |
|
46 | - protected $debug_info = array(); |
|
47 | - |
|
48 | - /** |
|
49 | - * Indicates whether or not the API is in debug mode |
|
50 | - * |
|
51 | - * @var boolean |
|
52 | - */ |
|
53 | - protected $debug_mode = false; |
|
54 | - |
|
55 | - /** |
|
56 | - * Indicates the version that was requested |
|
57 | - * |
|
58 | - * @var string |
|
59 | - */ |
|
60 | - protected $requested_version; |
|
61 | - |
|
62 | - /** |
|
63 | - * flat array of headers to send in the response |
|
64 | - * |
|
65 | - * @var array |
|
66 | - */ |
|
67 | - protected $response_headers = array(); |
|
68 | - |
|
69 | - |
|
70 | - public function __construct() |
|
71 | - { |
|
72 | - $this->debug_mode = EED_Core_Rest_Api::debugMode(); |
|
73 | - // we are handling a REST request. Don't show a fancy HTML error message is any error comes up |
|
74 | - add_filter('FHEE__EE_Error__get_error__show_normal_exceptions', '__return_true'); |
|
75 | - } |
|
76 | - |
|
77 | - |
|
78 | - /** |
|
79 | - * Sets the version the user requested |
|
80 | - * |
|
81 | - * @param string $version eg '4.8' |
|
82 | - */ |
|
83 | - public function setRequestedVersion($version) |
|
84 | - { |
|
85 | - $this->requested_version = $version; |
|
86 | - } |
|
87 | - |
|
88 | - |
|
89 | - /** |
|
90 | - * Sets some debug info that we'll send back in headers |
|
91 | - * |
|
92 | - * @param string $key |
|
93 | - * @param string|array $info |
|
94 | - */ |
|
95 | - protected function setDebugInfo($key, $info) |
|
96 | - { |
|
97 | - $this->debug_info[ $key ] = $info; |
|
98 | - } |
|
99 | - |
|
100 | - |
|
101 | - /** |
|
102 | - * Sets headers for the response |
|
103 | - * |
|
104 | - * @param string $header_key , excluding the "X-EE-" part |
|
105 | - * @param array|string $value if an array, multiple headers will be added, one |
|
106 | - * for each key in the array |
|
107 | - * @param boolean $use_ee_prefix whether to use the EE prefix on the header, or fallback to |
|
108 | - * the standard WP one |
|
109 | - */ |
|
110 | - protected function setResponseHeader($header_key, $value, $use_ee_prefix = true) |
|
111 | - { |
|
112 | - if (is_array($value)) { |
|
113 | - foreach ($value as $value_key => $value_value) { |
|
114 | - $this->setResponseHeader($header_key . '[' . $value_key . ']', $value_value); |
|
115 | - } |
|
116 | - } else { |
|
117 | - $prefix = $use_ee_prefix ? Base::HEADER_PREFIX_FOR_EE : Base::HEADER_PREFIX_FOR_WP; |
|
118 | - $this->response_headers[ $prefix . $header_key ] = $value; |
|
119 | - } |
|
120 | - } |
|
121 | - |
|
122 | - |
|
123 | - /** |
|
124 | - * Returns a flat array of headers to be added to the response |
|
125 | - * |
|
126 | - * @return array |
|
127 | - */ |
|
128 | - protected function getResponseHeaders() |
|
129 | - { |
|
130 | - return apply_filters( |
|
131 | - 'FHEE__EventEspresso\core\libraries\rest_api\controllers\Base___get_response_headers', |
|
132 | - $this->response_headers, |
|
133 | - $this, |
|
134 | - $this->requested_version |
|
135 | - ); |
|
136 | - } |
|
137 | - |
|
138 | - |
|
139 | - /** |
|
140 | - * Adds error notices from EE_Error onto the provided \WP_Error |
|
141 | - * |
|
142 | - * @param WP_Error $wp_error_response |
|
143 | - * @return WP_Error |
|
144 | - */ |
|
145 | - protected function addEeErrorsToResponse(WP_Error $wp_error_response) |
|
146 | - { |
|
147 | - $notices_during_checkin = EE_Error::get_raw_notices(); |
|
148 | - if (! empty($notices_during_checkin['errors'])) { |
|
149 | - foreach ($notices_during_checkin['errors'] as $error_code => $error_message) { |
|
150 | - $wp_error_response->add( |
|
151 | - sanitize_key($error_code), |
|
152 | - strip_tags($error_message) |
|
153 | - ); |
|
154 | - } |
|
155 | - } |
|
156 | - return $wp_error_response; |
|
157 | - } |
|
158 | - |
|
159 | - |
|
160 | - /** |
|
161 | - * Sends a response, but also makes sure to attach headers that |
|
162 | - * are handy for debugging. |
|
163 | - * Specifically, we assume folks will want to know what exactly was the DB query that got run, |
|
164 | - * what exactly was the Models query that got run, what capabilities came into play, what fields were omitted from |
|
165 | - * the response, others? |
|
166 | - * |
|
167 | - * @param array|WP_Error|Exception|RestException $response |
|
168 | - * @return WP_REST_Response |
|
169 | - */ |
|
170 | - public function sendResponse($response): WP_REST_Response |
|
171 | - { |
|
172 | - if ($response instanceof RestException) { |
|
173 | - $response = new WP_Error($response->getStringCode(), $response->getMessage(), $response->getData()); |
|
174 | - } |
|
175 | - if ($response instanceof Exception) { |
|
176 | - $code = $response->getCode() ? $response->getCode() : 'error_occurred'; |
|
177 | - $response = new WP_Error($code, $response->getMessage()); |
|
178 | - } |
|
179 | - if ($response instanceof WP_Error) { |
|
180 | - $response = $this->addEeErrorsToResponse($response); |
|
181 | - $rest_response = $this->createRestResponseFromWpError($response); |
|
182 | - } else { |
|
183 | - $rest_response = new WP_REST_Response($response, 200); |
|
184 | - } |
|
185 | - $headers = array(); |
|
186 | - if ($this->debug_mode && is_array($this->debug_info)) { |
|
187 | - foreach ($this->debug_info as $debug_key => $debug_info) { |
|
188 | - if (is_array($debug_info)) { |
|
189 | - $debug_info = wp_json_encode($debug_info); |
|
190 | - } |
|
191 | - $headers[ 'X-EE4-Debug-' . ucwords($debug_key) ] = $debug_info; |
|
192 | - } |
|
193 | - } |
|
194 | - $headers = array_merge( |
|
195 | - $headers, |
|
196 | - $this->getResponseHeaders(), |
|
197 | - $this->getHeadersFromEeNotices() |
|
198 | - ); |
|
199 | - $rest_response->set_headers($headers); |
|
200 | - return $rest_response; |
|
201 | - } |
|
202 | - |
|
203 | - |
|
204 | - /** |
|
205 | - * Converts the \WP_Error into `WP_REST_Response. |
|
206 | - * Mostly this is just a copy-and-paste from \WP_REST_Server::error_to_response |
|
207 | - * (which is protected) |
|
208 | - * |
|
209 | - * @param WP_Error $wp_error |
|
210 | - * @return WP_REST_Response |
|
211 | - */ |
|
212 | - protected function createRestResponseFromWpError(WP_Error $wp_error) |
|
213 | - { |
|
214 | - $error_data = $wp_error->get_error_data(); |
|
215 | - if (is_array($error_data) && isset($error_data['status'])) { |
|
216 | - $status = $error_data['status']; |
|
217 | - } else { |
|
218 | - $status = 500; |
|
219 | - } |
|
220 | - $errors = array(); |
|
221 | - foreach ((array) $wp_error->errors as $code => $messages) { |
|
222 | - foreach ((array) $messages as $message) { |
|
223 | - $errors[] = array( |
|
224 | - 'code' => $code, |
|
225 | - 'message' => $message, |
|
226 | - 'data' => $wp_error->get_error_data($code), |
|
227 | - ); |
|
228 | - } |
|
229 | - } |
|
230 | - $data = isset($errors[0]) ? $errors[0] : array(); |
|
231 | - if (count($errors) > 1) { |
|
232 | - // Remove the primary error. |
|
233 | - array_shift($errors); |
|
234 | - $data['additional_errors'] = $errors; |
|
235 | - } |
|
236 | - return new WP_REST_Response($data, $status); |
|
237 | - } |
|
238 | - |
|
239 | - |
|
240 | - /** |
|
241 | - * Array of headers derived from EE success, attention, and error messages |
|
242 | - * |
|
243 | - * @return array |
|
244 | - */ |
|
245 | - protected function getHeadersFromEeNotices() |
|
246 | - { |
|
247 | - $headers = array(); |
|
248 | - $notices = EE_Error::get_raw_notices(); |
|
249 | - foreach ($notices as $notice_type => $sub_notices) { |
|
250 | - if (! is_array($sub_notices)) { |
|
251 | - continue; |
|
252 | - } |
|
253 | - foreach ($sub_notices as $notice_code => $sub_notice) { |
|
254 | - $headers[ 'X-EE4-Notices-' |
|
255 | - . EEH_Inflector::humanize($notice_type) |
|
256 | - . '[' |
|
257 | - . $notice_code |
|
258 | - . ']' ] = strip_tags($sub_notice); |
|
259 | - } |
|
260 | - } |
|
261 | - return apply_filters( |
|
262 | - 'FHEE__EventEspresso\core\libraries\rest_api\controllers\Base___get_headers_from_ee_notices__return', |
|
263 | - $headers, |
|
264 | - $this->requested_version, |
|
265 | - $notices |
|
266 | - ); |
|
267 | - } |
|
268 | - |
|
269 | - |
|
270 | - /** |
|
271 | - * Finds which version of the API was requested given the route, and returns it. |
|
272 | - * eg in a request to "mysite.com/wp-json/ee/v4.8.29/events/123" this would return |
|
273 | - * "4.8.29". |
|
274 | - * We should know hte requested version in this model though, so if no route is |
|
275 | - * provided just use what we set earlier |
|
276 | - * |
|
277 | - * @param string $route |
|
278 | - * @return string |
|
279 | - */ |
|
280 | - public function getRequestedVersion($route = null) |
|
281 | - { |
|
282 | - if ($route === null) { |
|
283 | - return $this->requested_version; |
|
284 | - } |
|
285 | - $matches = $this->parseRoute( |
|
286 | - $route, |
|
287 | - '~' . EED_Core_Rest_Api::ee_api_namespace_for_regex . '~', |
|
288 | - array('version') |
|
289 | - ); |
|
290 | - if (isset($matches['version'])) { |
|
291 | - return $matches['version']; |
|
292 | - } else { |
|
293 | - return EED_Core_Rest_Api::latest_rest_api_version(); |
|
294 | - } |
|
295 | - } |
|
296 | - |
|
297 | - |
|
298 | - /** |
|
299 | - * Applies the regex to the route, then creates an array using the values of |
|
300 | - * $match_keys as keys (but ignores the full pattern match). Returns the array of matches. |
|
301 | - * For example, if you call |
|
302 | - * parse_route( '/ee/v4.8/events', '~\/ee\/v([^/]*)\/(.*)~', array( 'version', 'model' ) ) |
|
303 | - * it will return array( 'version' => '4.8', 'model' => 'events' ) |
|
304 | - * |
|
305 | - * @param string $route |
|
306 | - * @param string $regex |
|
307 | - * @param array $match_keys EXCLUDING matching the entire regex |
|
308 | - * @return array where $match_keys are the keys (the first value of $match_keys |
|
309 | - * becomes the first key of the return value, etc. Eg passing in $match_keys of |
|
310 | - * array( 'model', 'id' ), will, if the regex is successful, will return |
|
311 | - * array( 'model' => 'foo', 'id' => 'bar' ) |
|
312 | - * @throws EE_Error if it couldn't be parsed |
|
313 | - */ |
|
314 | - public function parseRoute($route, $regex, $match_keys) |
|
315 | - { |
|
316 | - $indexed_matches = array(); |
|
317 | - $success = preg_match($regex, $route, $matches); |
|
318 | - if (is_array($matches)) { |
|
319 | - // skip the overall regex match. Who cares |
|
320 | - for ($i = 1; $i <= count($match_keys); $i++) { |
|
321 | - if (! isset($matches[ $i ])) { |
|
322 | - $success = false; |
|
323 | - } else { |
|
324 | - $indexed_matches[ $match_keys[ $i - 1 ] ] = $matches[ $i ]; |
|
325 | - } |
|
326 | - } |
|
327 | - } |
|
328 | - if (! $success) { |
|
329 | - throw new EE_Error( |
|
330 | - esc_html__('We could not parse the URL. Please contact Event Espresso Support', 'event_espresso'), |
|
331 | - 'endpoint_parsing_error' |
|
332 | - ); |
|
333 | - } |
|
334 | - return $indexed_matches; |
|
335 | - } |
|
336 | - |
|
337 | - |
|
338 | - /** |
|
339 | - * Gets the body's params (either from JSON or parsed body), which EXCLUDES the GET params and URL params |
|
340 | - * |
|
341 | - * @param \WP_REST_Request $request |
|
342 | - * @return array |
|
343 | - */ |
|
344 | - protected function getBodyParams(\WP_REST_Request $request) |
|
345 | - { |
|
346 | - // $request->get_params(); |
|
347 | - return array_merge( |
|
348 | - (array) $request->get_body_params(), |
|
349 | - (array) $request->get_json_params() |
|
350 | - ); |
|
351 | - // return array_diff_key( |
|
352 | - // $request->get_params(), |
|
353 | - // $request->get_url_params(), |
|
354 | - // $request->get_query_params() |
|
355 | - // ); |
|
356 | - } |
|
23 | + /** |
|
24 | + * @deprecated use all-caps version |
|
25 | + */ |
|
26 | + // @codingStandardsIgnoreStart |
|
27 | + const header_prefix_for_ee = 'X-EE-'; |
|
28 | + // @codingStandardsIgnoreEnd |
|
29 | + |
|
30 | + const HEADER_PREFIX_FOR_EE = 'X-EE-'; |
|
31 | + |
|
32 | + /** |
|
33 | + * @deprecated use all-caps version instead |
|
34 | + */ |
|
35 | + // @codingStandardsIgnoreStart |
|
36 | + const header_prefix_for_wp = 'X-WP-'; |
|
37 | + // @codingStandardsIgnoreEnd |
|
38 | + |
|
39 | + const HEADER_PREFIX_FOR_WP = 'X-WP-'; |
|
40 | + |
|
41 | + /** |
|
42 | + * Contains debug info we'll send back in the response headers |
|
43 | + * |
|
44 | + * @var array |
|
45 | + */ |
|
46 | + protected $debug_info = array(); |
|
47 | + |
|
48 | + /** |
|
49 | + * Indicates whether or not the API is in debug mode |
|
50 | + * |
|
51 | + * @var boolean |
|
52 | + */ |
|
53 | + protected $debug_mode = false; |
|
54 | + |
|
55 | + /** |
|
56 | + * Indicates the version that was requested |
|
57 | + * |
|
58 | + * @var string |
|
59 | + */ |
|
60 | + protected $requested_version; |
|
61 | + |
|
62 | + /** |
|
63 | + * flat array of headers to send in the response |
|
64 | + * |
|
65 | + * @var array |
|
66 | + */ |
|
67 | + protected $response_headers = array(); |
|
68 | + |
|
69 | + |
|
70 | + public function __construct() |
|
71 | + { |
|
72 | + $this->debug_mode = EED_Core_Rest_Api::debugMode(); |
|
73 | + // we are handling a REST request. Don't show a fancy HTML error message is any error comes up |
|
74 | + add_filter('FHEE__EE_Error__get_error__show_normal_exceptions', '__return_true'); |
|
75 | + } |
|
76 | + |
|
77 | + |
|
78 | + /** |
|
79 | + * Sets the version the user requested |
|
80 | + * |
|
81 | + * @param string $version eg '4.8' |
|
82 | + */ |
|
83 | + public function setRequestedVersion($version) |
|
84 | + { |
|
85 | + $this->requested_version = $version; |
|
86 | + } |
|
87 | + |
|
88 | + |
|
89 | + /** |
|
90 | + * Sets some debug info that we'll send back in headers |
|
91 | + * |
|
92 | + * @param string $key |
|
93 | + * @param string|array $info |
|
94 | + */ |
|
95 | + protected function setDebugInfo($key, $info) |
|
96 | + { |
|
97 | + $this->debug_info[ $key ] = $info; |
|
98 | + } |
|
99 | + |
|
100 | + |
|
101 | + /** |
|
102 | + * Sets headers for the response |
|
103 | + * |
|
104 | + * @param string $header_key , excluding the "X-EE-" part |
|
105 | + * @param array|string $value if an array, multiple headers will be added, one |
|
106 | + * for each key in the array |
|
107 | + * @param boolean $use_ee_prefix whether to use the EE prefix on the header, or fallback to |
|
108 | + * the standard WP one |
|
109 | + */ |
|
110 | + protected function setResponseHeader($header_key, $value, $use_ee_prefix = true) |
|
111 | + { |
|
112 | + if (is_array($value)) { |
|
113 | + foreach ($value as $value_key => $value_value) { |
|
114 | + $this->setResponseHeader($header_key . '[' . $value_key . ']', $value_value); |
|
115 | + } |
|
116 | + } else { |
|
117 | + $prefix = $use_ee_prefix ? Base::HEADER_PREFIX_FOR_EE : Base::HEADER_PREFIX_FOR_WP; |
|
118 | + $this->response_headers[ $prefix . $header_key ] = $value; |
|
119 | + } |
|
120 | + } |
|
121 | + |
|
122 | + |
|
123 | + /** |
|
124 | + * Returns a flat array of headers to be added to the response |
|
125 | + * |
|
126 | + * @return array |
|
127 | + */ |
|
128 | + protected function getResponseHeaders() |
|
129 | + { |
|
130 | + return apply_filters( |
|
131 | + 'FHEE__EventEspresso\core\libraries\rest_api\controllers\Base___get_response_headers', |
|
132 | + $this->response_headers, |
|
133 | + $this, |
|
134 | + $this->requested_version |
|
135 | + ); |
|
136 | + } |
|
137 | + |
|
138 | + |
|
139 | + /** |
|
140 | + * Adds error notices from EE_Error onto the provided \WP_Error |
|
141 | + * |
|
142 | + * @param WP_Error $wp_error_response |
|
143 | + * @return WP_Error |
|
144 | + */ |
|
145 | + protected function addEeErrorsToResponse(WP_Error $wp_error_response) |
|
146 | + { |
|
147 | + $notices_during_checkin = EE_Error::get_raw_notices(); |
|
148 | + if (! empty($notices_during_checkin['errors'])) { |
|
149 | + foreach ($notices_during_checkin['errors'] as $error_code => $error_message) { |
|
150 | + $wp_error_response->add( |
|
151 | + sanitize_key($error_code), |
|
152 | + strip_tags($error_message) |
|
153 | + ); |
|
154 | + } |
|
155 | + } |
|
156 | + return $wp_error_response; |
|
157 | + } |
|
158 | + |
|
159 | + |
|
160 | + /** |
|
161 | + * Sends a response, but also makes sure to attach headers that |
|
162 | + * are handy for debugging. |
|
163 | + * Specifically, we assume folks will want to know what exactly was the DB query that got run, |
|
164 | + * what exactly was the Models query that got run, what capabilities came into play, what fields were omitted from |
|
165 | + * the response, others? |
|
166 | + * |
|
167 | + * @param array|WP_Error|Exception|RestException $response |
|
168 | + * @return WP_REST_Response |
|
169 | + */ |
|
170 | + public function sendResponse($response): WP_REST_Response |
|
171 | + { |
|
172 | + if ($response instanceof RestException) { |
|
173 | + $response = new WP_Error($response->getStringCode(), $response->getMessage(), $response->getData()); |
|
174 | + } |
|
175 | + if ($response instanceof Exception) { |
|
176 | + $code = $response->getCode() ? $response->getCode() : 'error_occurred'; |
|
177 | + $response = new WP_Error($code, $response->getMessage()); |
|
178 | + } |
|
179 | + if ($response instanceof WP_Error) { |
|
180 | + $response = $this->addEeErrorsToResponse($response); |
|
181 | + $rest_response = $this->createRestResponseFromWpError($response); |
|
182 | + } else { |
|
183 | + $rest_response = new WP_REST_Response($response, 200); |
|
184 | + } |
|
185 | + $headers = array(); |
|
186 | + if ($this->debug_mode && is_array($this->debug_info)) { |
|
187 | + foreach ($this->debug_info as $debug_key => $debug_info) { |
|
188 | + if (is_array($debug_info)) { |
|
189 | + $debug_info = wp_json_encode($debug_info); |
|
190 | + } |
|
191 | + $headers[ 'X-EE4-Debug-' . ucwords($debug_key) ] = $debug_info; |
|
192 | + } |
|
193 | + } |
|
194 | + $headers = array_merge( |
|
195 | + $headers, |
|
196 | + $this->getResponseHeaders(), |
|
197 | + $this->getHeadersFromEeNotices() |
|
198 | + ); |
|
199 | + $rest_response->set_headers($headers); |
|
200 | + return $rest_response; |
|
201 | + } |
|
202 | + |
|
203 | + |
|
204 | + /** |
|
205 | + * Converts the \WP_Error into `WP_REST_Response. |
|
206 | + * Mostly this is just a copy-and-paste from \WP_REST_Server::error_to_response |
|
207 | + * (which is protected) |
|
208 | + * |
|
209 | + * @param WP_Error $wp_error |
|
210 | + * @return WP_REST_Response |
|
211 | + */ |
|
212 | + protected function createRestResponseFromWpError(WP_Error $wp_error) |
|
213 | + { |
|
214 | + $error_data = $wp_error->get_error_data(); |
|
215 | + if (is_array($error_data) && isset($error_data['status'])) { |
|
216 | + $status = $error_data['status']; |
|
217 | + } else { |
|
218 | + $status = 500; |
|
219 | + } |
|
220 | + $errors = array(); |
|
221 | + foreach ((array) $wp_error->errors as $code => $messages) { |
|
222 | + foreach ((array) $messages as $message) { |
|
223 | + $errors[] = array( |
|
224 | + 'code' => $code, |
|
225 | + 'message' => $message, |
|
226 | + 'data' => $wp_error->get_error_data($code), |
|
227 | + ); |
|
228 | + } |
|
229 | + } |
|
230 | + $data = isset($errors[0]) ? $errors[0] : array(); |
|
231 | + if (count($errors) > 1) { |
|
232 | + // Remove the primary error. |
|
233 | + array_shift($errors); |
|
234 | + $data['additional_errors'] = $errors; |
|
235 | + } |
|
236 | + return new WP_REST_Response($data, $status); |
|
237 | + } |
|
238 | + |
|
239 | + |
|
240 | + /** |
|
241 | + * Array of headers derived from EE success, attention, and error messages |
|
242 | + * |
|
243 | + * @return array |
|
244 | + */ |
|
245 | + protected function getHeadersFromEeNotices() |
|
246 | + { |
|
247 | + $headers = array(); |
|
248 | + $notices = EE_Error::get_raw_notices(); |
|
249 | + foreach ($notices as $notice_type => $sub_notices) { |
|
250 | + if (! is_array($sub_notices)) { |
|
251 | + continue; |
|
252 | + } |
|
253 | + foreach ($sub_notices as $notice_code => $sub_notice) { |
|
254 | + $headers[ 'X-EE4-Notices-' |
|
255 | + . EEH_Inflector::humanize($notice_type) |
|
256 | + . '[' |
|
257 | + . $notice_code |
|
258 | + . ']' ] = strip_tags($sub_notice); |
|
259 | + } |
|
260 | + } |
|
261 | + return apply_filters( |
|
262 | + 'FHEE__EventEspresso\core\libraries\rest_api\controllers\Base___get_headers_from_ee_notices__return', |
|
263 | + $headers, |
|
264 | + $this->requested_version, |
|
265 | + $notices |
|
266 | + ); |
|
267 | + } |
|
268 | + |
|
269 | + |
|
270 | + /** |
|
271 | + * Finds which version of the API was requested given the route, and returns it. |
|
272 | + * eg in a request to "mysite.com/wp-json/ee/v4.8.29/events/123" this would return |
|
273 | + * "4.8.29". |
|
274 | + * We should know hte requested version in this model though, so if no route is |
|
275 | + * provided just use what we set earlier |
|
276 | + * |
|
277 | + * @param string $route |
|
278 | + * @return string |
|
279 | + */ |
|
280 | + public function getRequestedVersion($route = null) |
|
281 | + { |
|
282 | + if ($route === null) { |
|
283 | + return $this->requested_version; |
|
284 | + } |
|
285 | + $matches = $this->parseRoute( |
|
286 | + $route, |
|
287 | + '~' . EED_Core_Rest_Api::ee_api_namespace_for_regex . '~', |
|
288 | + array('version') |
|
289 | + ); |
|
290 | + if (isset($matches['version'])) { |
|
291 | + return $matches['version']; |
|
292 | + } else { |
|
293 | + return EED_Core_Rest_Api::latest_rest_api_version(); |
|
294 | + } |
|
295 | + } |
|
296 | + |
|
297 | + |
|
298 | + /** |
|
299 | + * Applies the regex to the route, then creates an array using the values of |
|
300 | + * $match_keys as keys (but ignores the full pattern match). Returns the array of matches. |
|
301 | + * For example, if you call |
|
302 | + * parse_route( '/ee/v4.8/events', '~\/ee\/v([^/]*)\/(.*)~', array( 'version', 'model' ) ) |
|
303 | + * it will return array( 'version' => '4.8', 'model' => 'events' ) |
|
304 | + * |
|
305 | + * @param string $route |
|
306 | + * @param string $regex |
|
307 | + * @param array $match_keys EXCLUDING matching the entire regex |
|
308 | + * @return array where $match_keys are the keys (the first value of $match_keys |
|
309 | + * becomes the first key of the return value, etc. Eg passing in $match_keys of |
|
310 | + * array( 'model', 'id' ), will, if the regex is successful, will return |
|
311 | + * array( 'model' => 'foo', 'id' => 'bar' ) |
|
312 | + * @throws EE_Error if it couldn't be parsed |
|
313 | + */ |
|
314 | + public function parseRoute($route, $regex, $match_keys) |
|
315 | + { |
|
316 | + $indexed_matches = array(); |
|
317 | + $success = preg_match($regex, $route, $matches); |
|
318 | + if (is_array($matches)) { |
|
319 | + // skip the overall regex match. Who cares |
|
320 | + for ($i = 1; $i <= count($match_keys); $i++) { |
|
321 | + if (! isset($matches[ $i ])) { |
|
322 | + $success = false; |
|
323 | + } else { |
|
324 | + $indexed_matches[ $match_keys[ $i - 1 ] ] = $matches[ $i ]; |
|
325 | + } |
|
326 | + } |
|
327 | + } |
|
328 | + if (! $success) { |
|
329 | + throw new EE_Error( |
|
330 | + esc_html__('We could not parse the URL. Please contact Event Espresso Support', 'event_espresso'), |
|
331 | + 'endpoint_parsing_error' |
|
332 | + ); |
|
333 | + } |
|
334 | + return $indexed_matches; |
|
335 | + } |
|
336 | + |
|
337 | + |
|
338 | + /** |
|
339 | + * Gets the body's params (either from JSON or parsed body), which EXCLUDES the GET params and URL params |
|
340 | + * |
|
341 | + * @param \WP_REST_Request $request |
|
342 | + * @return array |
|
343 | + */ |
|
344 | + protected function getBodyParams(\WP_REST_Request $request) |
|
345 | + { |
|
346 | + // $request->get_params(); |
|
347 | + return array_merge( |
|
348 | + (array) $request->get_body_params(), |
|
349 | + (array) $request->get_json_params() |
|
350 | + ); |
|
351 | + // return array_diff_key( |
|
352 | + // $request->get_params(), |
|
353 | + // $request->get_url_params(), |
|
354 | + // $request->get_query_params() |
|
355 | + // ); |
|
356 | + } |
|
357 | 357 | } |
@@ -37,74 +37,74 @@ |
||
37 | 37 | abstract class ChangesInBase |
38 | 38 | { |
39 | 39 | |
40 | - /** |
|
41 | - * The version that these changes happened |
|
42 | - * |
|
43 | - * @var string |
|
44 | - */ |
|
45 | - protected $version = null; |
|
40 | + /** |
|
41 | + * The version that these changes happened |
|
42 | + * |
|
43 | + * @var string |
|
44 | + */ |
|
45 | + protected $version = null; |
|
46 | 46 | |
47 | 47 | |
48 | - /** |
|
49 | - * Called when an EE4 REST API request is made to an earlier version than |
|
50 | - * what is indicated in this class' name. |
|
51 | - * Uses WordPress' add_filter and add_action to modify the EE4 REST API's response |
|
52 | - * so that regardless of what version of EE4 core is running, API clients |
|
53 | - * will have a consistent response |
|
54 | - * |
|
55 | - * @return void |
|
56 | - */ |
|
57 | - abstract public function setHooks(); |
|
48 | + /** |
|
49 | + * Called when an EE4 REST API request is made to an earlier version than |
|
50 | + * what is indicated in this class' name. |
|
51 | + * Uses WordPress' add_filter and add_action to modify the EE4 REST API's response |
|
52 | + * so that regardless of what version of EE4 core is running, API clients |
|
53 | + * will have a consistent response |
|
54 | + * |
|
55 | + * @return void |
|
56 | + */ |
|
57 | + abstract public function setHooks(); |
|
58 | 58 | |
59 | 59 | |
60 | - /** |
|
61 | - * Returns whether or not this class' name indicates its hooks should |
|
62 | - * apply when a request comes in for $requested_version. A class can use |
|
63 | - * other conditions when determining whether to perform their callbacks or not, |
|
64 | - * but this will typically be enough |
|
65 | - * |
|
66 | - * @param string $requested_version eg "4.8.33" |
|
67 | - * @return boolean true: this class' name indicates its filters and actions |
|
68 | - * should take effect. False: this class' name indicates it shouldn't do anything |
|
69 | - */ |
|
70 | - public function appliesToVersion($requested_version) |
|
71 | - { |
|
72 | - if ($this->version() > $requested_version) { |
|
73 | - return true; |
|
74 | - } |
|
75 | - return false; |
|
76 | - } |
|
60 | + /** |
|
61 | + * Returns whether or not this class' name indicates its hooks should |
|
62 | + * apply when a request comes in for $requested_version. A class can use |
|
63 | + * other conditions when determining whether to perform their callbacks or not, |
|
64 | + * but this will typically be enough |
|
65 | + * |
|
66 | + * @param string $requested_version eg "4.8.33" |
|
67 | + * @return boolean true: this class' name indicates its filters and actions |
|
68 | + * should take effect. False: this class' name indicates it shouldn't do anything |
|
69 | + */ |
|
70 | + public function appliesToVersion($requested_version) |
|
71 | + { |
|
72 | + if ($this->version() > $requested_version) { |
|
73 | + return true; |
|
74 | + } |
|
75 | + return false; |
|
76 | + } |
|
77 | 77 | |
78 | 78 | |
79 | - /** |
|
80 | - * Gets the EE core version when this changes were made to the rest api. |
|
81 | - * Any requests to earlier versions should have modifications made to them |
|
82 | - * by the callbacks of this class. |
|
83 | - * |
|
84 | - * @return string eg "4.8.33" |
|
85 | - * @throws EE_Error |
|
86 | - */ |
|
87 | - public function version() |
|
88 | - { |
|
89 | - if ($this->version === null) { |
|
90 | - $matches = array(); |
|
91 | - $regex = '~ChangesIn(\d)(\d\d)(\d\d)$~'; |
|
92 | - $success = preg_match( |
|
93 | - $regex, |
|
94 | - get_class($this), |
|
95 | - $matches |
|
96 | - ); |
|
97 | - if (! $success) { |
|
98 | - throw new EE_Error( |
|
99 | - sprintf( |
|
100 | - esc_html__('The class %1$s was misnamed. It name should match the regex "%2$s"', 'event_espresso'), |
|
101 | - get_class($this), |
|
102 | - $regex |
|
103 | - ) |
|
104 | - ); |
|
105 | - } |
|
106 | - $this->version = (int) $matches[1] . '.' . (int) $matches[2] . '.' . (int) $matches[3]; |
|
107 | - } |
|
108 | - return $this->version; |
|
109 | - } |
|
79 | + /** |
|
80 | + * Gets the EE core version when this changes were made to the rest api. |
|
81 | + * Any requests to earlier versions should have modifications made to them |
|
82 | + * by the callbacks of this class. |
|
83 | + * |
|
84 | + * @return string eg "4.8.33" |
|
85 | + * @throws EE_Error |
|
86 | + */ |
|
87 | + public function version() |
|
88 | + { |
|
89 | + if ($this->version === null) { |
|
90 | + $matches = array(); |
|
91 | + $regex = '~ChangesIn(\d)(\d\d)(\d\d)$~'; |
|
92 | + $success = preg_match( |
|
93 | + $regex, |
|
94 | + get_class($this), |
|
95 | + $matches |
|
96 | + ); |
|
97 | + if (! $success) { |
|
98 | + throw new EE_Error( |
|
99 | + sprintf( |
|
100 | + esc_html__('The class %1$s was misnamed. It name should match the regex "%2$s"', 'event_espresso'), |
|
101 | + get_class($this), |
|
102 | + $regex |
|
103 | + ) |
|
104 | + ); |
|
105 | + } |
|
106 | + $this->version = (int) $matches[1] . '.' . (int) $matches[2] . '.' . (int) $matches[3]; |
|
107 | + } |
|
108 | + return $this->version; |
|
109 | + } |
|
110 | 110 | } |
@@ -94,7 +94,7 @@ discard block |
||
94 | 94 | get_class($this), |
95 | 95 | $matches |
96 | 96 | ); |
97 | - if (! $success) { |
|
97 | + if ( ! $success) { |
|
98 | 98 | throw new EE_Error( |
99 | 99 | sprintf( |
100 | 100 | esc_html__('The class %1$s was misnamed. It name should match the regex "%2$s"', 'event_espresso'), |
@@ -103,7 +103,7 @@ discard block |
||
103 | 103 | ) |
104 | 104 | ); |
105 | 105 | } |
106 | - $this->version = (int) $matches[1] . '.' . (int) $matches[2] . '.' . (int) $matches[3]; |
|
106 | + $this->version = (int) $matches[1].'.'.(int) $matches[2].'.'.(int) $matches[3]; |
|
107 | 107 | } |
108 | 108 | return $this->version; |
109 | 109 | } |
@@ -17,214 +17,214 @@ |
||
17 | 17 | |
18 | 18 | class Datetime extends DatetimeCalculationBase |
19 | 19 | { |
20 | - /** |
|
21 | - * @var EEM_Datetime |
|
22 | - */ |
|
23 | - protected $datetime_model; |
|
20 | + /** |
|
21 | + * @var EEM_Datetime |
|
22 | + */ |
|
23 | + protected $datetime_model; |
|
24 | 24 | |
25 | - /** |
|
26 | - * @var EEM_Registration |
|
27 | - */ |
|
28 | - protected $registration_model; |
|
29 | - public function __construct(EEM_Datetime $datetime_model, EEM_Registration $registration_model) |
|
30 | - { |
|
31 | - $this->datetime_model = $datetime_model; |
|
32 | - $this->registration_model = $registration_model; |
|
33 | - } |
|
25 | + /** |
|
26 | + * @var EEM_Registration |
|
27 | + */ |
|
28 | + protected $registration_model; |
|
29 | + public function __construct(EEM_Datetime $datetime_model, EEM_Registration $registration_model) |
|
30 | + { |
|
31 | + $this->datetime_model = $datetime_model; |
|
32 | + $this->registration_model = $registration_model; |
|
33 | + } |
|
34 | 34 | |
35 | - /** |
|
36 | - * Calculates the total spaces available on the datetime, taking into account |
|
37 | - * ticket limits too. |
|
38 | - * |
|
39 | - * @see EE_Datetime::spaces_remaining( true ) |
|
40 | - * @param array $wpdb_row |
|
41 | - * @param WP_REST_Request $request |
|
42 | - * @param DatetimeControllerBase $controller |
|
43 | - * @return int |
|
44 | - * @throws EE_Error |
|
45 | - * @throws InvalidDataTypeException |
|
46 | - * @throws InvalidInterfaceException |
|
47 | - * @throws InvalidArgumentException |
|
48 | - * @throws ReflectionException |
|
49 | - */ |
|
50 | - public function spacesRemainingConsideringTickets($wpdb_row, $request, $controller) |
|
51 | - { |
|
52 | - if (is_array($wpdb_row) && isset($wpdb_row['Datetime.DTT_ID'])) { |
|
53 | - $dtt_obj = $this->datetime_model->get_one_by_ID($wpdb_row['Datetime.DTT_ID']); |
|
54 | - } else { |
|
55 | - $dtt_obj = null; |
|
56 | - } |
|
57 | - if ($dtt_obj instanceof EE_Datetime) { |
|
58 | - return $dtt_obj->spaces_remaining(true); |
|
59 | - } |
|
60 | - throw new EE_Error( |
|
61 | - sprintf( |
|
62 | - esc_html__( |
|
63 | - // @codingStandardsIgnoreStart |
|
64 | - 'Cannot calculate spaces_remaining_considering_tickets because the datetime with ID %1$s (from database row %2$s) was not found', |
|
65 | - // @codingStandardsIgnoreEnd |
|
66 | - 'event_espresso' |
|
67 | - ), |
|
68 | - $wpdb_row['Datetime.DTT_ID'], |
|
69 | - print_r($wpdb_row, true) |
|
70 | - ) |
|
71 | - ); |
|
72 | - } |
|
35 | + /** |
|
36 | + * Calculates the total spaces available on the datetime, taking into account |
|
37 | + * ticket limits too. |
|
38 | + * |
|
39 | + * @see EE_Datetime::spaces_remaining( true ) |
|
40 | + * @param array $wpdb_row |
|
41 | + * @param WP_REST_Request $request |
|
42 | + * @param DatetimeControllerBase $controller |
|
43 | + * @return int |
|
44 | + * @throws EE_Error |
|
45 | + * @throws InvalidDataTypeException |
|
46 | + * @throws InvalidInterfaceException |
|
47 | + * @throws InvalidArgumentException |
|
48 | + * @throws ReflectionException |
|
49 | + */ |
|
50 | + public function spacesRemainingConsideringTickets($wpdb_row, $request, $controller) |
|
51 | + { |
|
52 | + if (is_array($wpdb_row) && isset($wpdb_row['Datetime.DTT_ID'])) { |
|
53 | + $dtt_obj = $this->datetime_model->get_one_by_ID($wpdb_row['Datetime.DTT_ID']); |
|
54 | + } else { |
|
55 | + $dtt_obj = null; |
|
56 | + } |
|
57 | + if ($dtt_obj instanceof EE_Datetime) { |
|
58 | + return $dtt_obj->spaces_remaining(true); |
|
59 | + } |
|
60 | + throw new EE_Error( |
|
61 | + sprintf( |
|
62 | + esc_html__( |
|
63 | + // @codingStandardsIgnoreStart |
|
64 | + 'Cannot calculate spaces_remaining_considering_tickets because the datetime with ID %1$s (from database row %2$s) was not found', |
|
65 | + // @codingStandardsIgnoreEnd |
|
66 | + 'event_espresso' |
|
67 | + ), |
|
68 | + $wpdb_row['Datetime.DTT_ID'], |
|
69 | + print_r($wpdb_row, true) |
|
70 | + ) |
|
71 | + ); |
|
72 | + } |
|
73 | 73 | |
74 | 74 | |
75 | - /** |
|
76 | - * Counts registrations who have checked into this datetime |
|
77 | - * |
|
78 | - * @param array $wpdb_row |
|
79 | - * @param WP_REST_Request $request |
|
80 | - * @param DatetimeControllerBase $controller |
|
81 | - * @return int |
|
82 | - * @throws EE_Error |
|
83 | - * @throws InvalidArgumentException |
|
84 | - * @throws InvalidDataTypeException |
|
85 | - * @throws InvalidInterfaceException |
|
86 | - * @throws RestException |
|
87 | - */ |
|
88 | - public function registrationsCheckedInCount($wpdb_row, $request, $controller) |
|
89 | - { |
|
90 | - if (! is_array($wpdb_row) || ! isset($wpdb_row['Datetime.DTT_ID'])) { |
|
91 | - throw new EE_Error( |
|
92 | - sprintf( |
|
93 | - esc_html__( |
|
94 | - // @codingStandardsIgnoreStart |
|
95 | - 'Cannot calculate registrations_checked_in_count because the database row %1$s does not have an entry for "Datetime.DTT_ID"', |
|
96 | - // @codingStandardsIgnoreEnd |
|
97 | - 'event_espresso' |
|
98 | - ), |
|
99 | - print_r($wpdb_row, true) |
|
100 | - ) |
|
101 | - ); |
|
102 | - } |
|
103 | - $this->verifyCurrentUserCan('ee_read_checkins', 'registrations_checked_in_count'); |
|
104 | - return $this->registration_model |
|
105 | - ->count_registrations_checked_into_datetime($wpdb_row['Datetime.DTT_ID'], true); |
|
106 | - } |
|
75 | + /** |
|
76 | + * Counts registrations who have checked into this datetime |
|
77 | + * |
|
78 | + * @param array $wpdb_row |
|
79 | + * @param WP_REST_Request $request |
|
80 | + * @param DatetimeControllerBase $controller |
|
81 | + * @return int |
|
82 | + * @throws EE_Error |
|
83 | + * @throws InvalidArgumentException |
|
84 | + * @throws InvalidDataTypeException |
|
85 | + * @throws InvalidInterfaceException |
|
86 | + * @throws RestException |
|
87 | + */ |
|
88 | + public function registrationsCheckedInCount($wpdb_row, $request, $controller) |
|
89 | + { |
|
90 | + if (! is_array($wpdb_row) || ! isset($wpdb_row['Datetime.DTT_ID'])) { |
|
91 | + throw new EE_Error( |
|
92 | + sprintf( |
|
93 | + esc_html__( |
|
94 | + // @codingStandardsIgnoreStart |
|
95 | + 'Cannot calculate registrations_checked_in_count because the database row %1$s does not have an entry for "Datetime.DTT_ID"', |
|
96 | + // @codingStandardsIgnoreEnd |
|
97 | + 'event_espresso' |
|
98 | + ), |
|
99 | + print_r($wpdb_row, true) |
|
100 | + ) |
|
101 | + ); |
|
102 | + } |
|
103 | + $this->verifyCurrentUserCan('ee_read_checkins', 'registrations_checked_in_count'); |
|
104 | + return $this->registration_model |
|
105 | + ->count_registrations_checked_into_datetime($wpdb_row['Datetime.DTT_ID'], true); |
|
106 | + } |
|
107 | 107 | |
108 | 108 | |
109 | - /** |
|
110 | - * Counts registrations who have checked out of this datetime |
|
111 | - * |
|
112 | - * @param array $wpdb_row |
|
113 | - * @param WP_REST_Request $request |
|
114 | - * @param DatetimeControllerBase $controller |
|
115 | - * @return int |
|
116 | - * @throws EE_Error |
|
117 | - * @throws InvalidArgumentException |
|
118 | - * @throws InvalidDataTypeException |
|
119 | - * @throws InvalidInterfaceException |
|
120 | - * @throws RestException |
|
121 | - */ |
|
122 | - public function registrationsCheckedOutCount($wpdb_row, $request, $controller) |
|
123 | - { |
|
124 | - if (! is_array($wpdb_row) || ! isset($wpdb_row['Datetime.DTT_ID'])) { |
|
125 | - throw new EE_Error( |
|
126 | - sprintf( |
|
127 | - esc_html__( |
|
128 | - // @codingStandardsIgnoreStart |
|
129 | - 'Cannot calculate registrations_checked_out_count because the database row %1$s does not have an entry for "Datetime.DTT_ID"', |
|
130 | - // @codingStandardsIgnoreEnd |
|
131 | - 'event_espresso' |
|
132 | - ), |
|
133 | - print_r($wpdb_row, true) |
|
134 | - ) |
|
135 | - ); |
|
136 | - } |
|
137 | - $this->verifyCurrentUserCan('ee_read_checkins', 'registrations_checked_out_count'); |
|
138 | - return $this->registration_model |
|
139 | - ->count_registrations_checked_into_datetime($wpdb_row['Datetime.DTT_ID'], false); |
|
140 | - } |
|
109 | + /** |
|
110 | + * Counts registrations who have checked out of this datetime |
|
111 | + * |
|
112 | + * @param array $wpdb_row |
|
113 | + * @param WP_REST_Request $request |
|
114 | + * @param DatetimeControllerBase $controller |
|
115 | + * @return int |
|
116 | + * @throws EE_Error |
|
117 | + * @throws InvalidArgumentException |
|
118 | + * @throws InvalidDataTypeException |
|
119 | + * @throws InvalidInterfaceException |
|
120 | + * @throws RestException |
|
121 | + */ |
|
122 | + public function registrationsCheckedOutCount($wpdb_row, $request, $controller) |
|
123 | + { |
|
124 | + if (! is_array($wpdb_row) || ! isset($wpdb_row['Datetime.DTT_ID'])) { |
|
125 | + throw new EE_Error( |
|
126 | + sprintf( |
|
127 | + esc_html__( |
|
128 | + // @codingStandardsIgnoreStart |
|
129 | + 'Cannot calculate registrations_checked_out_count because the database row %1$s does not have an entry for "Datetime.DTT_ID"', |
|
130 | + // @codingStandardsIgnoreEnd |
|
131 | + 'event_espresso' |
|
132 | + ), |
|
133 | + print_r($wpdb_row, true) |
|
134 | + ) |
|
135 | + ); |
|
136 | + } |
|
137 | + $this->verifyCurrentUserCan('ee_read_checkins', 'registrations_checked_out_count'); |
|
138 | + return $this->registration_model |
|
139 | + ->count_registrations_checked_into_datetime($wpdb_row['Datetime.DTT_ID'], false); |
|
140 | + } |
|
141 | 141 | |
142 | 142 | |
143 | - /** |
|
144 | - * Counts the number of pending-payment registrations for this event (regardless |
|
145 | - * of how many datetimes each registrations' ticket purchase is for) |
|
146 | - * |
|
147 | - * @param array $wpdb_row |
|
148 | - * @param WP_REST_Request $request |
|
149 | - * @param DatetimeControllerBase $controller |
|
150 | - * @return int |
|
151 | - * @throws EE_Error |
|
152 | - * @throws InvalidArgumentException |
|
153 | - * @throws InvalidDataTypeException |
|
154 | - * @throws InvalidInterfaceException |
|
155 | - * @throws RestException |
|
156 | - */ |
|
157 | - public function spotsTakenPendingPayment($wpdb_row, $request, $controller) |
|
158 | - { |
|
159 | - if (! is_array($wpdb_row) || ! isset($wpdb_row['Datetime.DTT_ID'])) { |
|
160 | - throw new EE_Error( |
|
161 | - sprintf( |
|
162 | - esc_html__( |
|
163 | - // @codingStandardsIgnoreStart |
|
164 | - 'Cannot calculate spots_taken_pending_payment because the database row %1$s does not have an entry for "Datetime.DTT_ID"', |
|
165 | - // @codingStandardsIgnoreEnd |
|
166 | - 'event_espresso' |
|
167 | - ), |
|
168 | - print_r($wpdb_row, true) |
|
169 | - ) |
|
170 | - ); |
|
171 | - } |
|
172 | - $this->verifyCurrentUserCan('ee_read_registrations', 'spots_taken_pending_payment'); |
|
173 | - return $this->registration_model->count( |
|
174 | - array( |
|
175 | - array( |
|
176 | - 'Ticket.Datetime.DTT_ID' => $wpdb_row['Datetime.DTT_ID'], |
|
177 | - 'STS_ID' => EEM_Registration::status_id_pending_payment, |
|
178 | - ), |
|
179 | - ), |
|
180 | - 'REG_ID', |
|
181 | - true |
|
182 | - ); |
|
183 | - } |
|
143 | + /** |
|
144 | + * Counts the number of pending-payment registrations for this event (regardless |
|
145 | + * of how many datetimes each registrations' ticket purchase is for) |
|
146 | + * |
|
147 | + * @param array $wpdb_row |
|
148 | + * @param WP_REST_Request $request |
|
149 | + * @param DatetimeControllerBase $controller |
|
150 | + * @return int |
|
151 | + * @throws EE_Error |
|
152 | + * @throws InvalidArgumentException |
|
153 | + * @throws InvalidDataTypeException |
|
154 | + * @throws InvalidInterfaceException |
|
155 | + * @throws RestException |
|
156 | + */ |
|
157 | + public function spotsTakenPendingPayment($wpdb_row, $request, $controller) |
|
158 | + { |
|
159 | + if (! is_array($wpdb_row) || ! isset($wpdb_row['Datetime.DTT_ID'])) { |
|
160 | + throw new EE_Error( |
|
161 | + sprintf( |
|
162 | + esc_html__( |
|
163 | + // @codingStandardsIgnoreStart |
|
164 | + 'Cannot calculate spots_taken_pending_payment because the database row %1$s does not have an entry for "Datetime.DTT_ID"', |
|
165 | + // @codingStandardsIgnoreEnd |
|
166 | + 'event_espresso' |
|
167 | + ), |
|
168 | + print_r($wpdb_row, true) |
|
169 | + ) |
|
170 | + ); |
|
171 | + } |
|
172 | + $this->verifyCurrentUserCan('ee_read_registrations', 'spots_taken_pending_payment'); |
|
173 | + return $this->registration_model->count( |
|
174 | + array( |
|
175 | + array( |
|
176 | + 'Ticket.Datetime.DTT_ID' => $wpdb_row['Datetime.DTT_ID'], |
|
177 | + 'STS_ID' => EEM_Registration::status_id_pending_payment, |
|
178 | + ), |
|
179 | + ), |
|
180 | + 'REG_ID', |
|
181 | + true |
|
182 | + ); |
|
183 | + } |
|
184 | 184 | |
185 | 185 | |
186 | - /** |
|
187 | - * Provides an array for all the calculations possible that outlines a json schema for those calculations. |
|
188 | - * Array is indexed by calculation (snake case) and value is the schema for that calculation. |
|
189 | - * |
|
190 | - * @since 4.9.68.p |
|
191 | - * @return array |
|
192 | - */ |
|
193 | - public function schemaForCalculations() |
|
194 | - { |
|
195 | - return array( |
|
196 | - 'spaces_remaining_considering_tickets' => array( |
|
197 | - 'description' => esc_html__( |
|
198 | - 'Calculates the total spaces available on the datetime, taking into account ticket limits too.', |
|
199 | - 'event_espresso' |
|
200 | - ), |
|
201 | - 'type' => 'number', |
|
202 | - 'protected' => true, |
|
203 | - ), |
|
204 | - 'registrations_checked_in_count' => array( |
|
205 | - 'description' => esc_html__( |
|
206 | - 'Counts registrations who have checked into this datetime.', |
|
207 | - 'event_espresso' |
|
208 | - ), |
|
209 | - 'type' => 'number', |
|
210 | - 'protected' => true, |
|
211 | - ), |
|
212 | - 'registrations_checked_out_count' => array( |
|
213 | - 'description' => esc_html__( |
|
214 | - 'Counts registrations who have checked out of this datetime.', |
|
215 | - 'event_espresso' |
|
216 | - ), |
|
217 | - 'type' => 'number', |
|
218 | - 'protected' => true, |
|
219 | - ), |
|
220 | - 'spots_taken_pending_payment' => array( |
|
221 | - 'description' => esc_html__( |
|
222 | - 'The count of pending-payment registrations for this event (regardless of how many datetimes each registration\'s ticket purchase is for', |
|
223 | - 'event_espresso' |
|
224 | - ), |
|
225 | - 'type' => 'number', |
|
226 | - 'protected' => true, |
|
227 | - ), |
|
228 | - ); |
|
229 | - } |
|
186 | + /** |
|
187 | + * Provides an array for all the calculations possible that outlines a json schema for those calculations. |
|
188 | + * Array is indexed by calculation (snake case) and value is the schema for that calculation. |
|
189 | + * |
|
190 | + * @since 4.9.68.p |
|
191 | + * @return array |
|
192 | + */ |
|
193 | + public function schemaForCalculations() |
|
194 | + { |
|
195 | + return array( |
|
196 | + 'spaces_remaining_considering_tickets' => array( |
|
197 | + 'description' => esc_html__( |
|
198 | + 'Calculates the total spaces available on the datetime, taking into account ticket limits too.', |
|
199 | + 'event_espresso' |
|
200 | + ), |
|
201 | + 'type' => 'number', |
|
202 | + 'protected' => true, |
|
203 | + ), |
|
204 | + 'registrations_checked_in_count' => array( |
|
205 | + 'description' => esc_html__( |
|
206 | + 'Counts registrations who have checked into this datetime.', |
|
207 | + 'event_espresso' |
|
208 | + ), |
|
209 | + 'type' => 'number', |
|
210 | + 'protected' => true, |
|
211 | + ), |
|
212 | + 'registrations_checked_out_count' => array( |
|
213 | + 'description' => esc_html__( |
|
214 | + 'Counts registrations who have checked out of this datetime.', |
|
215 | + 'event_espresso' |
|
216 | + ), |
|
217 | + 'type' => 'number', |
|
218 | + 'protected' => true, |
|
219 | + ), |
|
220 | + 'spots_taken_pending_payment' => array( |
|
221 | + 'description' => esc_html__( |
|
222 | + 'The count of pending-payment registrations for this event (regardless of how many datetimes each registration\'s ticket purchase is for', |
|
223 | + 'event_espresso' |
|
224 | + ), |
|
225 | + 'type' => 'number', |
|
226 | + 'protected' => true, |
|
227 | + ), |
|
228 | + ); |
|
229 | + } |
|
230 | 230 | } |
@@ -87,7 +87,7 @@ discard block |
||
87 | 87 | */ |
88 | 88 | public function registrationsCheckedInCount($wpdb_row, $request, $controller) |
89 | 89 | { |
90 | - if (! is_array($wpdb_row) || ! isset($wpdb_row['Datetime.DTT_ID'])) { |
|
90 | + if ( ! is_array($wpdb_row) || ! isset($wpdb_row['Datetime.DTT_ID'])) { |
|
91 | 91 | throw new EE_Error( |
92 | 92 | sprintf( |
93 | 93 | esc_html__( |
@@ -121,7 +121,7 @@ discard block |
||
121 | 121 | */ |
122 | 122 | public function registrationsCheckedOutCount($wpdb_row, $request, $controller) |
123 | 123 | { |
124 | - if (! is_array($wpdb_row) || ! isset($wpdb_row['Datetime.DTT_ID'])) { |
|
124 | + if ( ! is_array($wpdb_row) || ! isset($wpdb_row['Datetime.DTT_ID'])) { |
|
125 | 125 | throw new EE_Error( |
126 | 126 | sprintf( |
127 | 127 | esc_html__( |
@@ -156,7 +156,7 @@ discard block |
||
156 | 156 | */ |
157 | 157 | public function spotsTakenPendingPayment($wpdb_row, $request, $controller) |
158 | 158 | { |
159 | - if (! is_array($wpdb_row) || ! isset($wpdb_row['Datetime.DTT_ID'])) { |
|
159 | + if ( ! is_array($wpdb_row) || ! isset($wpdb_row['Datetime.DTT_ID'])) { |
|
160 | 160 | throw new EE_Error( |
161 | 161 | sprintf( |
162 | 162 | esc_html__( |
@@ -26,571 +26,571 @@ |
||
26 | 26 | */ |
27 | 27 | class Event extends EventCalculationBase |
28 | 28 | { |
29 | - /** |
|
30 | - * @var EEM_Event |
|
31 | - */ |
|
32 | - protected $event_model; |
|
33 | - |
|
34 | - /** |
|
35 | - * @var EEM_Registration |
|
36 | - */ |
|
37 | - protected $registration_model; |
|
38 | - public function __construct(EEM_Event $event_model, EEM_Registration $registration_model) |
|
39 | - { |
|
40 | - $this->event_model = $event_model; |
|
41 | - $this->registration_model = $registration_model; |
|
42 | - } |
|
43 | - |
|
44 | - /** |
|
45 | - * Calculates the total spaces on the event (not subtracting sales, but taking |
|
46 | - * sales into account; so this is the optimum sales that CAN still be achieved) |
|
47 | - * See EE_Event::total_available_spaces( true ); |
|
48 | - * |
|
49 | - * @param array $wpdb_row |
|
50 | - * @param WP_REST_Request $request |
|
51 | - * @param EventControllerBase $controller |
|
52 | - * @return int |
|
53 | - * @throws EE_Error |
|
54 | - * @throws DomainException |
|
55 | - * @throws InvalidDataTypeException |
|
56 | - * @throws InvalidInterfaceException |
|
57 | - * @throws UnexpectedEntityException |
|
58 | - * @throws InvalidArgumentException |
|
59 | - */ |
|
60 | - public function optimumSalesAtStart($wpdb_row, $request, $controller) |
|
61 | - { |
|
62 | - $event_obj = null; |
|
63 | - if (Event::wpdbRowHasEventId($wpdb_row)) { |
|
64 | - $event_obj = $this->event_model->get_one_by_ID($wpdb_row['Event_CPT.ID']); |
|
65 | - } |
|
66 | - if ($event_obj instanceof EE_Event) { |
|
67 | - return $event_obj->total_available_spaces(); |
|
68 | - } |
|
69 | - throw new EE_Error( |
|
70 | - sprintf( |
|
71 | - esc_html__( |
|
72 | - // @codingStandardsIgnoreStart |
|
73 | - 'Cannot calculate optimum_sales_at_start because the event with ID %1$s (from database row %2$s) was not found', |
|
74 | - // @codingStandardsIgnoreEnd |
|
75 | - 'event_espresso' |
|
76 | - ), |
|
77 | - $wpdb_row['Event_CPT.ID'], |
|
78 | - print_r($wpdb_row, true) |
|
79 | - ) |
|
80 | - ); |
|
81 | - } |
|
82 | - |
|
83 | - |
|
84 | - /** |
|
85 | - * Calculates the total spaces on the event (ignoring all sales; so this is the optimum |
|
86 | - * sales that COULD have been achieved) |
|
87 | - * See EE_Event::total_available_spaces( true ); |
|
88 | - * |
|
89 | - * @param array $wpdb_row |
|
90 | - * @param WP_REST_Request $request |
|
91 | - * @param EventControllerBase $controller |
|
92 | - * @return int |
|
93 | - * @throws DomainException |
|
94 | - * @throws EE_Error |
|
95 | - * @throws InvalidArgumentException |
|
96 | - * @throws InvalidDataTypeException |
|
97 | - * @throws InvalidInterfaceException |
|
98 | - * @throws UnexpectedEntityException |
|
99 | - */ |
|
100 | - public function optimumSalesNow($wpdb_row, $request, $controller) |
|
101 | - { |
|
102 | - $event_obj = null; |
|
103 | - if (Event::wpdbRowHasEventId($wpdb_row)) { |
|
104 | - $event_obj = $this->event_model->get_one_by_ID($wpdb_row['Event_CPT.ID']); |
|
105 | - } |
|
106 | - if ($event_obj instanceof EE_Event) { |
|
107 | - return $event_obj->total_available_spaces(true); |
|
108 | - } |
|
109 | - throw new EE_Error( |
|
110 | - sprintf( |
|
111 | - esc_html__( |
|
112 | - // @codingStandardsIgnoreStart |
|
113 | - 'Cannot calculate optimum_sales_now because the event with ID %1$s (from database row %2$s) was not found', |
|
114 | - // @codingStandardsIgnoreEnd |
|
115 | - 'event_espresso' |
|
116 | - ), |
|
117 | - $wpdb_row['Event_CPT.ID'], |
|
118 | - print_r($wpdb_row, true) |
|
119 | - ) |
|
120 | - ); |
|
121 | - } |
|
122 | - |
|
123 | - |
|
124 | - /** |
|
125 | - * Like optimum_sales_now, but minus total sales so far. |
|
126 | - * See EE_Event::spaces_remaining_for_sale( true ); |
|
127 | - * |
|
128 | - * @param array $wpdb_row |
|
129 | - * @param WP_REST_Request $request |
|
130 | - * @param EventControllerBase $controller |
|
131 | - * @return int |
|
132 | - * @throws DomainException |
|
133 | - * @throws EE_Error |
|
134 | - * @throws InvalidArgumentException |
|
135 | - * @throws InvalidDataTypeException |
|
136 | - * @throws InvalidInterfaceException |
|
137 | - * @throws UnexpectedEntityException |
|
138 | - */ |
|
139 | - public function spacesRemaining($wpdb_row, $request, $controller) |
|
140 | - { |
|
141 | - $event_obj = null; |
|
142 | - if (Event::wpdbRowHasEventId($wpdb_row)) { |
|
143 | - $event_obj = $this->event_model->get_one_by_ID($wpdb_row['Event_CPT.ID']); |
|
144 | - } |
|
145 | - if ($event_obj instanceof EE_Event) { |
|
146 | - return $event_obj->spaces_remaining_for_sale(); |
|
147 | - } |
|
148 | - throw new EE_Error( |
|
149 | - sprintf( |
|
150 | - esc_html__( |
|
151 | - // @codingStandardsIgnoreStart |
|
152 | - 'Cannot calculate spaces_remaining because the event with ID %1$s (from database row %2$s) was not found', |
|
153 | - // @codingStandardsIgnoreEnd |
|
154 | - 'event_espresso' |
|
155 | - ), |
|
156 | - $wpdb_row['Event_CPT.ID'], |
|
157 | - print_r($wpdb_row, true) |
|
158 | - ) |
|
159 | - ); |
|
160 | - } |
|
161 | - |
|
162 | - |
|
163 | - /** |
|
164 | - * Counts the number of approved registrations for this event (regardless |
|
165 | - * of how many datetimes each registrations' ticket purchase is for) |
|
166 | - * |
|
167 | - * @param array $wpdb_row |
|
168 | - * @param WP_REST_Request $request |
|
169 | - * @param EventControllerBase $controller |
|
170 | - * @return int |
|
171 | - * @throws EE_Error |
|
172 | - * @throws InvalidArgumentException |
|
173 | - * @throws InvalidDataTypeException |
|
174 | - * @throws InvalidInterfaceException |
|
175 | - */ |
|
176 | - public function spotsTaken($wpdb_row, $request, $controller) |
|
177 | - { |
|
178 | - if (! Event::wpdbRowHasEventId($wpdb_row)) { |
|
179 | - throw new EE_Error( |
|
180 | - sprintf( |
|
181 | - esc_html__( |
|
182 | - // @codingStandardsIgnoreStart |
|
183 | - 'Cannot calculate spots_taken because the database row %1$s does not have a valid entry for "Event_CPT.ID"', |
|
184 | - // @codingStandardsIgnoreEnd |
|
185 | - 'event_espresso' |
|
186 | - ), |
|
187 | - print_r($wpdb_row, true) |
|
188 | - ) |
|
189 | - ); |
|
190 | - } |
|
191 | - return $this->registration_model->count( |
|
192 | - array( |
|
193 | - array( |
|
194 | - 'EVT_ID' => $wpdb_row['Event_CPT.ID'], |
|
195 | - 'STS_ID' => EEM_Registration::status_id_approved, |
|
196 | - ), |
|
197 | - ), |
|
198 | - 'REG_ID', |
|
199 | - true |
|
200 | - ); |
|
201 | - } |
|
202 | - |
|
203 | - |
|
204 | - /** |
|
205 | - * Counts the number of pending-payment registrations for this event (regardless |
|
206 | - * of how many datetimes each registrations' ticket purchase is for) |
|
207 | - * |
|
208 | - * @param array $wpdb_row |
|
209 | - * @param WP_REST_Request $request |
|
210 | - * @param EventControllerBase $controller |
|
211 | - * @return int |
|
212 | - * @throws EE_Error |
|
213 | - * @throws InvalidArgumentException |
|
214 | - * @throws InvalidDataTypeException |
|
215 | - * @throws InvalidInterfaceException |
|
216 | - * @throws RestException |
|
217 | - */ |
|
218 | - public function spotsTakenPendingPayment($wpdb_row, $request, $controller) |
|
219 | - { |
|
220 | - if (! Event::wpdbRowHasEventId($wpdb_row)) { |
|
221 | - throw new EE_Error( |
|
222 | - sprintf( |
|
223 | - esc_html__( |
|
224 | - // @codingStandardsIgnoreStart |
|
225 | - 'Cannot calculate spots_taken_pending_payment because the database row %1$s does not have an entry for "Event_CPT.ID"', |
|
226 | - // @codingStandardsIgnoreEnd |
|
227 | - 'event_espresso' |
|
228 | - ), |
|
229 | - print_r($wpdb_row, true) |
|
230 | - ) |
|
231 | - ); |
|
232 | - } |
|
233 | - $this->verifyCurrentUserCan('ee_read_registrations', 'spots_taken_pending_payment'); |
|
234 | - return $this->registration_model->count( |
|
235 | - array( |
|
236 | - array( |
|
237 | - 'EVT_ID' => $wpdb_row['Event_CPT.ID'], |
|
238 | - 'STS_ID' => EEM_Registration::status_id_pending_payment, |
|
239 | - ), |
|
240 | - ), |
|
241 | - 'REG_ID', |
|
242 | - true |
|
243 | - ); |
|
244 | - } |
|
245 | - |
|
246 | - |
|
247 | - /** |
|
248 | - * Counts all the registrations who have checked into one of this events' datetimes |
|
249 | - * See EE_Event::total_available_spaces( false ); |
|
250 | - * |
|
251 | - * @param array $wpdb_row |
|
252 | - * @param WP_REST_Request $request |
|
253 | - * @param EventControllerBase $controller |
|
254 | - * @return int|null if permission denied |
|
255 | - * @throws EE_Error |
|
256 | - * @throws InvalidArgumentException |
|
257 | - * @throws InvalidDataTypeException |
|
258 | - * @throws InvalidInterfaceException |
|
259 | - * @throws RestException |
|
260 | - */ |
|
261 | - public function registrationsCheckedInCount($wpdb_row, $request, $controller) |
|
262 | - { |
|
263 | - if (! Event::wpdbRowHasEventId($wpdb_row)) { |
|
264 | - throw new EE_Error( |
|
265 | - sprintf( |
|
266 | - esc_html__( |
|
267 | - // @codingStandardsIgnoreStart |
|
268 | - 'Cannot calculate registrations_checked_in_count because the database row %1$s does not have an entry for "Event_CPT.ID"', |
|
269 | - // @codingStandardsIgnoreEnd |
|
270 | - 'event_espresso' |
|
271 | - ), |
|
272 | - print_r($wpdb_row, true) |
|
273 | - ) |
|
274 | - ); |
|
275 | - } |
|
276 | - $this->verifyCurrentUserCan('ee_read_checkins', 'registrations_checked_in_count'); |
|
277 | - return $this->registration_model->count_registrations_checked_into_event($wpdb_row['Event_CPT.ID'], true); |
|
278 | - } |
|
279 | - |
|
280 | - |
|
281 | - /** |
|
282 | - * Counts all the registrations who have checked out of one of this events' datetimes |
|
283 | - * See EE_Event::total_available_spaces( false ); |
|
284 | - * |
|
285 | - * @param array $wpdb_row |
|
286 | - * @param WP_REST_Request $request |
|
287 | - * @param EventControllerBase $controller |
|
288 | - * @return int |
|
289 | - * @throws EE_Error |
|
290 | - * @throws InvalidArgumentException |
|
291 | - * @throws InvalidDataTypeException |
|
292 | - * @throws InvalidInterfaceException |
|
293 | - * @throws RestException |
|
294 | - */ |
|
295 | - public function registrationsCheckedOutCount($wpdb_row, $request, $controller) |
|
296 | - { |
|
297 | - if (! Event::wpdbRowHasEventId($wpdb_row)) { |
|
298 | - throw new EE_Error( |
|
299 | - sprintf( |
|
300 | - esc_html__( |
|
301 | - // @codingStandardsIgnoreStart |
|
302 | - 'Cannot calculate registrations_checked_out_count because the database row %1$s does not have an entry for "Event_CPT.ID"', |
|
303 | - // @codingStandardsIgnoreEnd |
|
304 | - 'event_espresso' |
|
305 | - ), |
|
306 | - print_r($wpdb_row, true) |
|
307 | - ) |
|
308 | - ); |
|
309 | - } |
|
310 | - $this->verifyCurrentUserCan('ee_read_checkins', 'registrations_checked_out_count'); |
|
311 | - return $this->registration_model->count_registrations_checked_into_event($wpdb_row['Event_CPT.ID'], false); |
|
312 | - } |
|
313 | - |
|
314 | - |
|
315 | - /** |
|
316 | - * Gets the thumbnail image |
|
317 | - * |
|
318 | - * @param array $wpdb_row |
|
319 | - * @param WP_REST_Request $request |
|
320 | - * @param EventControllerBase $controller |
|
321 | - * @return array |
|
322 | - * @throws EE_Error |
|
323 | - */ |
|
324 | - public function imageThumbnail($wpdb_row, $request, $controller) |
|
325 | - { |
|
326 | - return self::calculateImageData($wpdb_row, 'thumbnail'); |
|
327 | - } |
|
328 | - |
|
329 | - |
|
330 | - /** |
|
331 | - * Gets the medium image |
|
332 | - * |
|
333 | - * @param array $wpdb_row |
|
334 | - * @param WP_REST_Request $request |
|
335 | - * @param EventControllerBase $controller |
|
336 | - * @return array |
|
337 | - * @throws EE_Error |
|
338 | - */ |
|
339 | - public function imageMedium($wpdb_row, $request, $controller) |
|
340 | - { |
|
341 | - return self::calculateImageData($wpdb_row, 'medium'); |
|
342 | - } |
|
343 | - |
|
344 | - |
|
345 | - /** |
|
346 | - * Gets the medium-large image |
|
347 | - * |
|
348 | - * @param array $wpdb_row |
|
349 | - * @param WP_REST_Request $request |
|
350 | - * @param EventControllerBase $controller |
|
351 | - * @return array |
|
352 | - * @throws EE_Error |
|
353 | - */ |
|
354 | - public function imageMediumLarge($wpdb_row, $request, $controller) |
|
355 | - { |
|
356 | - return self::calculateImageData($wpdb_row, 'medium_large'); |
|
357 | - } |
|
358 | - |
|
359 | - |
|
360 | - /** |
|
361 | - * Gets the large image |
|
362 | - * |
|
363 | - * @param array $wpdb_row |
|
364 | - * @param WP_REST_Request $request |
|
365 | - * @param EventControllerBase $controller |
|
366 | - * @return array |
|
367 | - * @throws EE_Error |
|
368 | - */ |
|
369 | - public function imageLarge($wpdb_row, $request, $controller) |
|
370 | - { |
|
371 | - return self::calculateImageData($wpdb_row, 'large'); |
|
372 | - } |
|
373 | - |
|
374 | - |
|
375 | - /** |
|
376 | - * Gets the post-thumbnail image |
|
377 | - * |
|
378 | - * @param array $wpdb_row |
|
379 | - * @param WP_REST_Request $request |
|
380 | - * @param EventControllerBase $controller |
|
381 | - * @return array |
|
382 | - * @throws EE_Error |
|
383 | - */ |
|
384 | - public function imagePostThumbnail($wpdb_row, $request, $controller) |
|
385 | - { |
|
386 | - return self::calculateImageData($wpdb_row, 'post-thumbnail'); |
|
387 | - } |
|
388 | - |
|
389 | - |
|
390 | - /** |
|
391 | - * Gets the full size image |
|
392 | - * |
|
393 | - * @param array $wpdb_row |
|
394 | - * @param WP_REST_Request $request |
|
395 | - * @param EventControllerBase $controller |
|
396 | - * @return array |
|
397 | - * @throws EE_Error |
|
398 | - */ |
|
399 | - public function imageFull($wpdb_row, $request, $controller) |
|
400 | - { |
|
401 | - return self::calculateImageData($wpdb_row, 'full'); |
|
402 | - } |
|
403 | - |
|
404 | - |
|
405 | - /** |
|
406 | - * Gets image specs and formats them for the display in the API, |
|
407 | - * according to the image size requested |
|
408 | - * |
|
409 | - * @param array $wpdb_row |
|
410 | - * @param string $image_size one of these: thumbnail, medium, medium_large, large, post-thumbnail, full |
|
411 | - * @return array|false if no such image exists. If array it will have keys 'url', 'width', 'height' and 'original' |
|
412 | - * @throws EE_Error |
|
413 | - */ |
|
414 | - protected function calculateImageData($wpdb_row, $image_size) |
|
415 | - { |
|
416 | - if (! Event::wpdbRowHasEventId($wpdb_row)) { |
|
417 | - throw new EE_Error( |
|
418 | - sprintf( |
|
419 | - esc_html__( |
|
420 | - // @codingStandardsIgnoreStart |
|
421 | - 'Cannot calculate image because the database row %1$s does not have an entry for "Event_CPT.ID"', |
|
422 | - // @codingStandardsIgnoreEnd |
|
423 | - 'event_espresso' |
|
424 | - ), |
|
425 | - print_r($wpdb_row, true) |
|
426 | - ) |
|
427 | - ); |
|
428 | - } |
|
429 | - $EVT_ID = $wpdb_row['Event_CPT.ID']; |
|
430 | - $attachment_id = get_post_thumbnail_id($EVT_ID); |
|
431 | - $data = wp_get_attachment_image_src($attachment_id, $image_size); |
|
432 | - if (! $data) { |
|
433 | - return null; |
|
434 | - } |
|
435 | - $generated = true; |
|
436 | - if (isset($data[3])) { |
|
437 | - $generated = $data[3]; |
|
438 | - } |
|
439 | - return array( |
|
440 | - 'url' => $data[0], |
|
441 | - 'width' => $data[1], |
|
442 | - 'height' => $data[2], |
|
443 | - 'generated' => $generated, |
|
444 | - ); |
|
445 | - } |
|
446 | - |
|
447 | - |
|
448 | - /** |
|
449 | - * Returns true if the array of data contains 'Event_CPT.ID'. False otherwise |
|
450 | - * |
|
451 | - * @param array $wpdb_row |
|
452 | - * @return bool |
|
453 | - */ |
|
454 | - protected function wpdbRowHasEventId($wpdb_row) |
|
455 | - { |
|
456 | - return (is_array($wpdb_row) && isset($wpdb_row['Event_CPT.ID']) && absint($wpdb_row['Event_CPT.ID'])); |
|
457 | - } |
|
458 | - |
|
459 | - |
|
460 | - /** |
|
461 | - * Provides an array for all the calculations possible that outlines a json schema for those calculations. |
|
462 | - * Array is indexed by calculation (snake case) and value is the schema for that calculation. |
|
463 | - * |
|
464 | - * @since 4.9.68.p |
|
465 | - * @return array |
|
466 | - */ |
|
467 | - public function schemaForCalculations() |
|
468 | - { |
|
469 | - $image_object_properties = array( |
|
470 | - 'url' => array( |
|
471 | - 'type' => 'string', |
|
472 | - ), |
|
473 | - 'width' => array( |
|
474 | - 'type' => 'number', |
|
475 | - ), |
|
476 | - 'height' => array( |
|
477 | - 'type' => 'number', |
|
478 | - ), |
|
479 | - 'generated' => array( |
|
480 | - 'type' => 'boolean', |
|
481 | - ), |
|
482 | - ); |
|
483 | - return array( |
|
484 | - 'optimum_sales_at_start' => array( |
|
485 | - 'description' => esc_html__( |
|
486 | - 'The total spaces on the event (not subtracting sales, but taking sales into account; so this is the optimum sales that CAN still be achieved.', |
|
487 | - 'event_espresso' |
|
488 | - ), |
|
489 | - 'type' => 'number', |
|
490 | - 'protected' => true, |
|
491 | - ), |
|
492 | - 'optimum_sales_now' => array( |
|
493 | - 'description' => esc_html__( |
|
494 | - 'The total spaces on the event (ignoring all sales; so this is the optimum sales that could have been achieved.', |
|
495 | - 'event_espresso' |
|
496 | - ), |
|
497 | - 'type' => 'number', |
|
498 | - 'protected' => true, |
|
499 | - ), |
|
500 | - 'spaces_remaining' => array( |
|
501 | - 'description' => esc_html__( |
|
502 | - 'The optimum_sales_number result, minus total sales so far.', |
|
503 | - 'event_espresso' |
|
504 | - ), |
|
505 | - 'type' => 'number', |
|
506 | - 'protected' => true, |
|
507 | - ), |
|
508 | - 'spots_taken' => array( |
|
509 | - 'description' => esc_html__( |
|
510 | - 'The number of approved registrations for this event (regardless of how many datetimes each registration\'s ticket purchase is for)', |
|
511 | - 'event_espresso' |
|
512 | - ), |
|
513 | - 'type' => 'number', |
|
514 | - 'protected' => true, |
|
515 | - ), |
|
516 | - 'spots_taken_pending_payment' => array( |
|
517 | - 'description' => esc_html__( |
|
518 | - 'The number of pending-payment registrations for this event (regardless of how many datetimes each registration\'s ticket purchase is for)', |
|
519 | - 'event_espresso' |
|
520 | - ), |
|
521 | - 'type' => 'number', |
|
522 | - 'protected' => true, |
|
523 | - ), |
|
524 | - 'registrations_checked_in_count' => array( |
|
525 | - 'description' => esc_html__( |
|
526 | - 'The count of all the registrations who have checked into one of this event\'s datetimes.', |
|
527 | - 'event_espresso' |
|
528 | - ), |
|
529 | - 'type' => 'number', |
|
530 | - 'protected' => true, |
|
531 | - ), |
|
532 | - 'registrations_checked_out_count' => array( |
|
533 | - 'description' => esc_html__( |
|
534 | - 'The count of all registrations who have checked out of one of this event\'s datetimes.', |
|
535 | - 'event_espresso' |
|
536 | - ), |
|
537 | - 'type' => 'number', |
|
538 | - 'protected' => true, |
|
539 | - ), |
|
540 | - 'image_thumbnail' => array( |
|
541 | - 'description' => esc_html__( |
|
542 | - 'The thumbnail image data.', |
|
543 | - 'event_espresso' |
|
544 | - ), |
|
545 | - 'type' => 'object', |
|
546 | - 'properties' => $image_object_properties, |
|
547 | - 'additionalProperties' => false, |
|
548 | - ), |
|
549 | - 'image_medium' => array( |
|
550 | - 'description' => esc_html__( |
|
551 | - 'The medium image data.', |
|
552 | - 'event_espresso' |
|
553 | - ), |
|
554 | - 'type' => 'object', |
|
555 | - 'properties' => $image_object_properties, |
|
556 | - 'additionalProperties' => false, |
|
557 | - ), |
|
558 | - 'image_medium_large' => array( |
|
559 | - 'description' => esc_html__( |
|
560 | - 'The medium-large image data.', |
|
561 | - 'event_espresso' |
|
562 | - ), |
|
563 | - 'type' => 'object', |
|
564 | - 'properties' => $image_object_properties, |
|
565 | - 'additionalProperties' => false, |
|
566 | - ), |
|
567 | - 'image_large' => array( |
|
568 | - 'description' => esc_html__( |
|
569 | - 'The large image data.', |
|
570 | - 'event_espresso' |
|
571 | - ), |
|
572 | - 'type' => 'object', |
|
573 | - 'properties' => $image_object_properties, |
|
574 | - 'additionalProperties' => false, |
|
575 | - ), |
|
576 | - 'image_post_thumbnail' => array( |
|
577 | - 'description' => esc_html__( |
|
578 | - 'The post-thumbnail image data.', |
|
579 | - 'event_espresso' |
|
580 | - ), |
|
581 | - 'type' => 'object', |
|
582 | - 'properties' => $image_object_properties, |
|
583 | - 'additionalProperties' => false, |
|
584 | - ), |
|
585 | - 'image_full' => array( |
|
586 | - 'description' => esc_html__( |
|
587 | - 'The full size image data', |
|
588 | - 'event_espresso' |
|
589 | - ), |
|
590 | - 'type' => 'object', |
|
591 | - 'properties' => $image_object_properties, |
|
592 | - 'additionalProperties' => false, |
|
593 | - ), |
|
594 | - ); |
|
595 | - } |
|
29 | + /** |
|
30 | + * @var EEM_Event |
|
31 | + */ |
|
32 | + protected $event_model; |
|
33 | + |
|
34 | + /** |
|
35 | + * @var EEM_Registration |
|
36 | + */ |
|
37 | + protected $registration_model; |
|
38 | + public function __construct(EEM_Event $event_model, EEM_Registration $registration_model) |
|
39 | + { |
|
40 | + $this->event_model = $event_model; |
|
41 | + $this->registration_model = $registration_model; |
|
42 | + } |
|
43 | + |
|
44 | + /** |
|
45 | + * Calculates the total spaces on the event (not subtracting sales, but taking |
|
46 | + * sales into account; so this is the optimum sales that CAN still be achieved) |
|
47 | + * See EE_Event::total_available_spaces( true ); |
|
48 | + * |
|
49 | + * @param array $wpdb_row |
|
50 | + * @param WP_REST_Request $request |
|
51 | + * @param EventControllerBase $controller |
|
52 | + * @return int |
|
53 | + * @throws EE_Error |
|
54 | + * @throws DomainException |
|
55 | + * @throws InvalidDataTypeException |
|
56 | + * @throws InvalidInterfaceException |
|
57 | + * @throws UnexpectedEntityException |
|
58 | + * @throws InvalidArgumentException |
|
59 | + */ |
|
60 | + public function optimumSalesAtStart($wpdb_row, $request, $controller) |
|
61 | + { |
|
62 | + $event_obj = null; |
|
63 | + if (Event::wpdbRowHasEventId($wpdb_row)) { |
|
64 | + $event_obj = $this->event_model->get_one_by_ID($wpdb_row['Event_CPT.ID']); |
|
65 | + } |
|
66 | + if ($event_obj instanceof EE_Event) { |
|
67 | + return $event_obj->total_available_spaces(); |
|
68 | + } |
|
69 | + throw new EE_Error( |
|
70 | + sprintf( |
|
71 | + esc_html__( |
|
72 | + // @codingStandardsIgnoreStart |
|
73 | + 'Cannot calculate optimum_sales_at_start because the event with ID %1$s (from database row %2$s) was not found', |
|
74 | + // @codingStandardsIgnoreEnd |
|
75 | + 'event_espresso' |
|
76 | + ), |
|
77 | + $wpdb_row['Event_CPT.ID'], |
|
78 | + print_r($wpdb_row, true) |
|
79 | + ) |
|
80 | + ); |
|
81 | + } |
|
82 | + |
|
83 | + |
|
84 | + /** |
|
85 | + * Calculates the total spaces on the event (ignoring all sales; so this is the optimum |
|
86 | + * sales that COULD have been achieved) |
|
87 | + * See EE_Event::total_available_spaces( true ); |
|
88 | + * |
|
89 | + * @param array $wpdb_row |
|
90 | + * @param WP_REST_Request $request |
|
91 | + * @param EventControllerBase $controller |
|
92 | + * @return int |
|
93 | + * @throws DomainException |
|
94 | + * @throws EE_Error |
|
95 | + * @throws InvalidArgumentException |
|
96 | + * @throws InvalidDataTypeException |
|
97 | + * @throws InvalidInterfaceException |
|
98 | + * @throws UnexpectedEntityException |
|
99 | + */ |
|
100 | + public function optimumSalesNow($wpdb_row, $request, $controller) |
|
101 | + { |
|
102 | + $event_obj = null; |
|
103 | + if (Event::wpdbRowHasEventId($wpdb_row)) { |
|
104 | + $event_obj = $this->event_model->get_one_by_ID($wpdb_row['Event_CPT.ID']); |
|
105 | + } |
|
106 | + if ($event_obj instanceof EE_Event) { |
|
107 | + return $event_obj->total_available_spaces(true); |
|
108 | + } |
|
109 | + throw new EE_Error( |
|
110 | + sprintf( |
|
111 | + esc_html__( |
|
112 | + // @codingStandardsIgnoreStart |
|
113 | + 'Cannot calculate optimum_sales_now because the event with ID %1$s (from database row %2$s) was not found', |
|
114 | + // @codingStandardsIgnoreEnd |
|
115 | + 'event_espresso' |
|
116 | + ), |
|
117 | + $wpdb_row['Event_CPT.ID'], |
|
118 | + print_r($wpdb_row, true) |
|
119 | + ) |
|
120 | + ); |
|
121 | + } |
|
122 | + |
|
123 | + |
|
124 | + /** |
|
125 | + * Like optimum_sales_now, but minus total sales so far. |
|
126 | + * See EE_Event::spaces_remaining_for_sale( true ); |
|
127 | + * |
|
128 | + * @param array $wpdb_row |
|
129 | + * @param WP_REST_Request $request |
|
130 | + * @param EventControllerBase $controller |
|
131 | + * @return int |
|
132 | + * @throws DomainException |
|
133 | + * @throws EE_Error |
|
134 | + * @throws InvalidArgumentException |
|
135 | + * @throws InvalidDataTypeException |
|
136 | + * @throws InvalidInterfaceException |
|
137 | + * @throws UnexpectedEntityException |
|
138 | + */ |
|
139 | + public function spacesRemaining($wpdb_row, $request, $controller) |
|
140 | + { |
|
141 | + $event_obj = null; |
|
142 | + if (Event::wpdbRowHasEventId($wpdb_row)) { |
|
143 | + $event_obj = $this->event_model->get_one_by_ID($wpdb_row['Event_CPT.ID']); |
|
144 | + } |
|
145 | + if ($event_obj instanceof EE_Event) { |
|
146 | + return $event_obj->spaces_remaining_for_sale(); |
|
147 | + } |
|
148 | + throw new EE_Error( |
|
149 | + sprintf( |
|
150 | + esc_html__( |
|
151 | + // @codingStandardsIgnoreStart |
|
152 | + 'Cannot calculate spaces_remaining because the event with ID %1$s (from database row %2$s) was not found', |
|
153 | + // @codingStandardsIgnoreEnd |
|
154 | + 'event_espresso' |
|
155 | + ), |
|
156 | + $wpdb_row['Event_CPT.ID'], |
|
157 | + print_r($wpdb_row, true) |
|
158 | + ) |
|
159 | + ); |
|
160 | + } |
|
161 | + |
|
162 | + |
|
163 | + /** |
|
164 | + * Counts the number of approved registrations for this event (regardless |
|
165 | + * of how many datetimes each registrations' ticket purchase is for) |
|
166 | + * |
|
167 | + * @param array $wpdb_row |
|
168 | + * @param WP_REST_Request $request |
|
169 | + * @param EventControllerBase $controller |
|
170 | + * @return int |
|
171 | + * @throws EE_Error |
|
172 | + * @throws InvalidArgumentException |
|
173 | + * @throws InvalidDataTypeException |
|
174 | + * @throws InvalidInterfaceException |
|
175 | + */ |
|
176 | + public function spotsTaken($wpdb_row, $request, $controller) |
|
177 | + { |
|
178 | + if (! Event::wpdbRowHasEventId($wpdb_row)) { |
|
179 | + throw new EE_Error( |
|
180 | + sprintf( |
|
181 | + esc_html__( |
|
182 | + // @codingStandardsIgnoreStart |
|
183 | + 'Cannot calculate spots_taken because the database row %1$s does not have a valid entry for "Event_CPT.ID"', |
|
184 | + // @codingStandardsIgnoreEnd |
|
185 | + 'event_espresso' |
|
186 | + ), |
|
187 | + print_r($wpdb_row, true) |
|
188 | + ) |
|
189 | + ); |
|
190 | + } |
|
191 | + return $this->registration_model->count( |
|
192 | + array( |
|
193 | + array( |
|
194 | + 'EVT_ID' => $wpdb_row['Event_CPT.ID'], |
|
195 | + 'STS_ID' => EEM_Registration::status_id_approved, |
|
196 | + ), |
|
197 | + ), |
|
198 | + 'REG_ID', |
|
199 | + true |
|
200 | + ); |
|
201 | + } |
|
202 | + |
|
203 | + |
|
204 | + /** |
|
205 | + * Counts the number of pending-payment registrations for this event (regardless |
|
206 | + * of how many datetimes each registrations' ticket purchase is for) |
|
207 | + * |
|
208 | + * @param array $wpdb_row |
|
209 | + * @param WP_REST_Request $request |
|
210 | + * @param EventControllerBase $controller |
|
211 | + * @return int |
|
212 | + * @throws EE_Error |
|
213 | + * @throws InvalidArgumentException |
|
214 | + * @throws InvalidDataTypeException |
|
215 | + * @throws InvalidInterfaceException |
|
216 | + * @throws RestException |
|
217 | + */ |
|
218 | + public function spotsTakenPendingPayment($wpdb_row, $request, $controller) |
|
219 | + { |
|
220 | + if (! Event::wpdbRowHasEventId($wpdb_row)) { |
|
221 | + throw new EE_Error( |
|
222 | + sprintf( |
|
223 | + esc_html__( |
|
224 | + // @codingStandardsIgnoreStart |
|
225 | + 'Cannot calculate spots_taken_pending_payment because the database row %1$s does not have an entry for "Event_CPT.ID"', |
|
226 | + // @codingStandardsIgnoreEnd |
|
227 | + 'event_espresso' |
|
228 | + ), |
|
229 | + print_r($wpdb_row, true) |
|
230 | + ) |
|
231 | + ); |
|
232 | + } |
|
233 | + $this->verifyCurrentUserCan('ee_read_registrations', 'spots_taken_pending_payment'); |
|
234 | + return $this->registration_model->count( |
|
235 | + array( |
|
236 | + array( |
|
237 | + 'EVT_ID' => $wpdb_row['Event_CPT.ID'], |
|
238 | + 'STS_ID' => EEM_Registration::status_id_pending_payment, |
|
239 | + ), |
|
240 | + ), |
|
241 | + 'REG_ID', |
|
242 | + true |
|
243 | + ); |
|
244 | + } |
|
245 | + |
|
246 | + |
|
247 | + /** |
|
248 | + * Counts all the registrations who have checked into one of this events' datetimes |
|
249 | + * See EE_Event::total_available_spaces( false ); |
|
250 | + * |
|
251 | + * @param array $wpdb_row |
|
252 | + * @param WP_REST_Request $request |
|
253 | + * @param EventControllerBase $controller |
|
254 | + * @return int|null if permission denied |
|
255 | + * @throws EE_Error |
|
256 | + * @throws InvalidArgumentException |
|
257 | + * @throws InvalidDataTypeException |
|
258 | + * @throws InvalidInterfaceException |
|
259 | + * @throws RestException |
|
260 | + */ |
|
261 | + public function registrationsCheckedInCount($wpdb_row, $request, $controller) |
|
262 | + { |
|
263 | + if (! Event::wpdbRowHasEventId($wpdb_row)) { |
|
264 | + throw new EE_Error( |
|
265 | + sprintf( |
|
266 | + esc_html__( |
|
267 | + // @codingStandardsIgnoreStart |
|
268 | + 'Cannot calculate registrations_checked_in_count because the database row %1$s does not have an entry for "Event_CPT.ID"', |
|
269 | + // @codingStandardsIgnoreEnd |
|
270 | + 'event_espresso' |
|
271 | + ), |
|
272 | + print_r($wpdb_row, true) |
|
273 | + ) |
|
274 | + ); |
|
275 | + } |
|
276 | + $this->verifyCurrentUserCan('ee_read_checkins', 'registrations_checked_in_count'); |
|
277 | + return $this->registration_model->count_registrations_checked_into_event($wpdb_row['Event_CPT.ID'], true); |
|
278 | + } |
|
279 | + |
|
280 | + |
|
281 | + /** |
|
282 | + * Counts all the registrations who have checked out of one of this events' datetimes |
|
283 | + * See EE_Event::total_available_spaces( false ); |
|
284 | + * |
|
285 | + * @param array $wpdb_row |
|
286 | + * @param WP_REST_Request $request |
|
287 | + * @param EventControllerBase $controller |
|
288 | + * @return int |
|
289 | + * @throws EE_Error |
|
290 | + * @throws InvalidArgumentException |
|
291 | + * @throws InvalidDataTypeException |
|
292 | + * @throws InvalidInterfaceException |
|
293 | + * @throws RestException |
|
294 | + */ |
|
295 | + public function registrationsCheckedOutCount($wpdb_row, $request, $controller) |
|
296 | + { |
|
297 | + if (! Event::wpdbRowHasEventId($wpdb_row)) { |
|
298 | + throw new EE_Error( |
|
299 | + sprintf( |
|
300 | + esc_html__( |
|
301 | + // @codingStandardsIgnoreStart |
|
302 | + 'Cannot calculate registrations_checked_out_count because the database row %1$s does not have an entry for "Event_CPT.ID"', |
|
303 | + // @codingStandardsIgnoreEnd |
|
304 | + 'event_espresso' |
|
305 | + ), |
|
306 | + print_r($wpdb_row, true) |
|
307 | + ) |
|
308 | + ); |
|
309 | + } |
|
310 | + $this->verifyCurrentUserCan('ee_read_checkins', 'registrations_checked_out_count'); |
|
311 | + return $this->registration_model->count_registrations_checked_into_event($wpdb_row['Event_CPT.ID'], false); |
|
312 | + } |
|
313 | + |
|
314 | + |
|
315 | + /** |
|
316 | + * Gets the thumbnail image |
|
317 | + * |
|
318 | + * @param array $wpdb_row |
|
319 | + * @param WP_REST_Request $request |
|
320 | + * @param EventControllerBase $controller |
|
321 | + * @return array |
|
322 | + * @throws EE_Error |
|
323 | + */ |
|
324 | + public function imageThumbnail($wpdb_row, $request, $controller) |
|
325 | + { |
|
326 | + return self::calculateImageData($wpdb_row, 'thumbnail'); |
|
327 | + } |
|
328 | + |
|
329 | + |
|
330 | + /** |
|
331 | + * Gets the medium image |
|
332 | + * |
|
333 | + * @param array $wpdb_row |
|
334 | + * @param WP_REST_Request $request |
|
335 | + * @param EventControllerBase $controller |
|
336 | + * @return array |
|
337 | + * @throws EE_Error |
|
338 | + */ |
|
339 | + public function imageMedium($wpdb_row, $request, $controller) |
|
340 | + { |
|
341 | + return self::calculateImageData($wpdb_row, 'medium'); |
|
342 | + } |
|
343 | + |
|
344 | + |
|
345 | + /** |
|
346 | + * Gets the medium-large image |
|
347 | + * |
|
348 | + * @param array $wpdb_row |
|
349 | + * @param WP_REST_Request $request |
|
350 | + * @param EventControllerBase $controller |
|
351 | + * @return array |
|
352 | + * @throws EE_Error |
|
353 | + */ |
|
354 | + public function imageMediumLarge($wpdb_row, $request, $controller) |
|
355 | + { |
|
356 | + return self::calculateImageData($wpdb_row, 'medium_large'); |
|
357 | + } |
|
358 | + |
|
359 | + |
|
360 | + /** |
|
361 | + * Gets the large image |
|
362 | + * |
|
363 | + * @param array $wpdb_row |
|
364 | + * @param WP_REST_Request $request |
|
365 | + * @param EventControllerBase $controller |
|
366 | + * @return array |
|
367 | + * @throws EE_Error |
|
368 | + */ |
|
369 | + public function imageLarge($wpdb_row, $request, $controller) |
|
370 | + { |
|
371 | + return self::calculateImageData($wpdb_row, 'large'); |
|
372 | + } |
|
373 | + |
|
374 | + |
|
375 | + /** |
|
376 | + * Gets the post-thumbnail image |
|
377 | + * |
|
378 | + * @param array $wpdb_row |
|
379 | + * @param WP_REST_Request $request |
|
380 | + * @param EventControllerBase $controller |
|
381 | + * @return array |
|
382 | + * @throws EE_Error |
|
383 | + */ |
|
384 | + public function imagePostThumbnail($wpdb_row, $request, $controller) |
|
385 | + { |
|
386 | + return self::calculateImageData($wpdb_row, 'post-thumbnail'); |
|
387 | + } |
|
388 | + |
|
389 | + |
|
390 | + /** |
|
391 | + * Gets the full size image |
|
392 | + * |
|
393 | + * @param array $wpdb_row |
|
394 | + * @param WP_REST_Request $request |
|
395 | + * @param EventControllerBase $controller |
|
396 | + * @return array |
|
397 | + * @throws EE_Error |
|
398 | + */ |
|
399 | + public function imageFull($wpdb_row, $request, $controller) |
|
400 | + { |
|
401 | + return self::calculateImageData($wpdb_row, 'full'); |
|
402 | + } |
|
403 | + |
|
404 | + |
|
405 | + /** |
|
406 | + * Gets image specs and formats them for the display in the API, |
|
407 | + * according to the image size requested |
|
408 | + * |
|
409 | + * @param array $wpdb_row |
|
410 | + * @param string $image_size one of these: thumbnail, medium, medium_large, large, post-thumbnail, full |
|
411 | + * @return array|false if no such image exists. If array it will have keys 'url', 'width', 'height' and 'original' |
|
412 | + * @throws EE_Error |
|
413 | + */ |
|
414 | + protected function calculateImageData($wpdb_row, $image_size) |
|
415 | + { |
|
416 | + if (! Event::wpdbRowHasEventId($wpdb_row)) { |
|
417 | + throw new EE_Error( |
|
418 | + sprintf( |
|
419 | + esc_html__( |
|
420 | + // @codingStandardsIgnoreStart |
|
421 | + 'Cannot calculate image because the database row %1$s does not have an entry for "Event_CPT.ID"', |
|
422 | + // @codingStandardsIgnoreEnd |
|
423 | + 'event_espresso' |
|
424 | + ), |
|
425 | + print_r($wpdb_row, true) |
|
426 | + ) |
|
427 | + ); |
|
428 | + } |
|
429 | + $EVT_ID = $wpdb_row['Event_CPT.ID']; |
|
430 | + $attachment_id = get_post_thumbnail_id($EVT_ID); |
|
431 | + $data = wp_get_attachment_image_src($attachment_id, $image_size); |
|
432 | + if (! $data) { |
|
433 | + return null; |
|
434 | + } |
|
435 | + $generated = true; |
|
436 | + if (isset($data[3])) { |
|
437 | + $generated = $data[3]; |
|
438 | + } |
|
439 | + return array( |
|
440 | + 'url' => $data[0], |
|
441 | + 'width' => $data[1], |
|
442 | + 'height' => $data[2], |
|
443 | + 'generated' => $generated, |
|
444 | + ); |
|
445 | + } |
|
446 | + |
|
447 | + |
|
448 | + /** |
|
449 | + * Returns true if the array of data contains 'Event_CPT.ID'. False otherwise |
|
450 | + * |
|
451 | + * @param array $wpdb_row |
|
452 | + * @return bool |
|
453 | + */ |
|
454 | + protected function wpdbRowHasEventId($wpdb_row) |
|
455 | + { |
|
456 | + return (is_array($wpdb_row) && isset($wpdb_row['Event_CPT.ID']) && absint($wpdb_row['Event_CPT.ID'])); |
|
457 | + } |
|
458 | + |
|
459 | + |
|
460 | + /** |
|
461 | + * Provides an array for all the calculations possible that outlines a json schema for those calculations. |
|
462 | + * Array is indexed by calculation (snake case) and value is the schema for that calculation. |
|
463 | + * |
|
464 | + * @since 4.9.68.p |
|
465 | + * @return array |
|
466 | + */ |
|
467 | + public function schemaForCalculations() |
|
468 | + { |
|
469 | + $image_object_properties = array( |
|
470 | + 'url' => array( |
|
471 | + 'type' => 'string', |
|
472 | + ), |
|
473 | + 'width' => array( |
|
474 | + 'type' => 'number', |
|
475 | + ), |
|
476 | + 'height' => array( |
|
477 | + 'type' => 'number', |
|
478 | + ), |
|
479 | + 'generated' => array( |
|
480 | + 'type' => 'boolean', |
|
481 | + ), |
|
482 | + ); |
|
483 | + return array( |
|
484 | + 'optimum_sales_at_start' => array( |
|
485 | + 'description' => esc_html__( |
|
486 | + 'The total spaces on the event (not subtracting sales, but taking sales into account; so this is the optimum sales that CAN still be achieved.', |
|
487 | + 'event_espresso' |
|
488 | + ), |
|
489 | + 'type' => 'number', |
|
490 | + 'protected' => true, |
|
491 | + ), |
|
492 | + 'optimum_sales_now' => array( |
|
493 | + 'description' => esc_html__( |
|
494 | + 'The total spaces on the event (ignoring all sales; so this is the optimum sales that could have been achieved.', |
|
495 | + 'event_espresso' |
|
496 | + ), |
|
497 | + 'type' => 'number', |
|
498 | + 'protected' => true, |
|
499 | + ), |
|
500 | + 'spaces_remaining' => array( |
|
501 | + 'description' => esc_html__( |
|
502 | + 'The optimum_sales_number result, minus total sales so far.', |
|
503 | + 'event_espresso' |
|
504 | + ), |
|
505 | + 'type' => 'number', |
|
506 | + 'protected' => true, |
|
507 | + ), |
|
508 | + 'spots_taken' => array( |
|
509 | + 'description' => esc_html__( |
|
510 | + 'The number of approved registrations for this event (regardless of how many datetimes each registration\'s ticket purchase is for)', |
|
511 | + 'event_espresso' |
|
512 | + ), |
|
513 | + 'type' => 'number', |
|
514 | + 'protected' => true, |
|
515 | + ), |
|
516 | + 'spots_taken_pending_payment' => array( |
|
517 | + 'description' => esc_html__( |
|
518 | + 'The number of pending-payment registrations for this event (regardless of how many datetimes each registration\'s ticket purchase is for)', |
|
519 | + 'event_espresso' |
|
520 | + ), |
|
521 | + 'type' => 'number', |
|
522 | + 'protected' => true, |
|
523 | + ), |
|
524 | + 'registrations_checked_in_count' => array( |
|
525 | + 'description' => esc_html__( |
|
526 | + 'The count of all the registrations who have checked into one of this event\'s datetimes.', |
|
527 | + 'event_espresso' |
|
528 | + ), |
|
529 | + 'type' => 'number', |
|
530 | + 'protected' => true, |
|
531 | + ), |
|
532 | + 'registrations_checked_out_count' => array( |
|
533 | + 'description' => esc_html__( |
|
534 | + 'The count of all registrations who have checked out of one of this event\'s datetimes.', |
|
535 | + 'event_espresso' |
|
536 | + ), |
|
537 | + 'type' => 'number', |
|
538 | + 'protected' => true, |
|
539 | + ), |
|
540 | + 'image_thumbnail' => array( |
|
541 | + 'description' => esc_html__( |
|
542 | + 'The thumbnail image data.', |
|
543 | + 'event_espresso' |
|
544 | + ), |
|
545 | + 'type' => 'object', |
|
546 | + 'properties' => $image_object_properties, |
|
547 | + 'additionalProperties' => false, |
|
548 | + ), |
|
549 | + 'image_medium' => array( |
|
550 | + 'description' => esc_html__( |
|
551 | + 'The medium image data.', |
|
552 | + 'event_espresso' |
|
553 | + ), |
|
554 | + 'type' => 'object', |
|
555 | + 'properties' => $image_object_properties, |
|
556 | + 'additionalProperties' => false, |
|
557 | + ), |
|
558 | + 'image_medium_large' => array( |
|
559 | + 'description' => esc_html__( |
|
560 | + 'The medium-large image data.', |
|
561 | + 'event_espresso' |
|
562 | + ), |
|
563 | + 'type' => 'object', |
|
564 | + 'properties' => $image_object_properties, |
|
565 | + 'additionalProperties' => false, |
|
566 | + ), |
|
567 | + 'image_large' => array( |
|
568 | + 'description' => esc_html__( |
|
569 | + 'The large image data.', |
|
570 | + 'event_espresso' |
|
571 | + ), |
|
572 | + 'type' => 'object', |
|
573 | + 'properties' => $image_object_properties, |
|
574 | + 'additionalProperties' => false, |
|
575 | + ), |
|
576 | + 'image_post_thumbnail' => array( |
|
577 | + 'description' => esc_html__( |
|
578 | + 'The post-thumbnail image data.', |
|
579 | + 'event_espresso' |
|
580 | + ), |
|
581 | + 'type' => 'object', |
|
582 | + 'properties' => $image_object_properties, |
|
583 | + 'additionalProperties' => false, |
|
584 | + ), |
|
585 | + 'image_full' => array( |
|
586 | + 'description' => esc_html__( |
|
587 | + 'The full size image data', |
|
588 | + 'event_espresso' |
|
589 | + ), |
|
590 | + 'type' => 'object', |
|
591 | + 'properties' => $image_object_properties, |
|
592 | + 'additionalProperties' => false, |
|
593 | + ), |
|
594 | + ); |
|
595 | + } |
|
596 | 596 | } |
@@ -175,7 +175,7 @@ discard block |
||
175 | 175 | */ |
176 | 176 | public function spotsTaken($wpdb_row, $request, $controller) |
177 | 177 | { |
178 | - if (! Event::wpdbRowHasEventId($wpdb_row)) { |
|
178 | + if ( ! Event::wpdbRowHasEventId($wpdb_row)) { |
|
179 | 179 | throw new EE_Error( |
180 | 180 | sprintf( |
181 | 181 | esc_html__( |
@@ -217,7 +217,7 @@ discard block |
||
217 | 217 | */ |
218 | 218 | public function spotsTakenPendingPayment($wpdb_row, $request, $controller) |
219 | 219 | { |
220 | - if (! Event::wpdbRowHasEventId($wpdb_row)) { |
|
220 | + if ( ! Event::wpdbRowHasEventId($wpdb_row)) { |
|
221 | 221 | throw new EE_Error( |
222 | 222 | sprintf( |
223 | 223 | esc_html__( |
@@ -260,7 +260,7 @@ discard block |
||
260 | 260 | */ |
261 | 261 | public function registrationsCheckedInCount($wpdb_row, $request, $controller) |
262 | 262 | { |
263 | - if (! Event::wpdbRowHasEventId($wpdb_row)) { |
|
263 | + if ( ! Event::wpdbRowHasEventId($wpdb_row)) { |
|
264 | 264 | throw new EE_Error( |
265 | 265 | sprintf( |
266 | 266 | esc_html__( |
@@ -294,7 +294,7 @@ discard block |
||
294 | 294 | */ |
295 | 295 | public function registrationsCheckedOutCount($wpdb_row, $request, $controller) |
296 | 296 | { |
297 | - if (! Event::wpdbRowHasEventId($wpdb_row)) { |
|
297 | + if ( ! Event::wpdbRowHasEventId($wpdb_row)) { |
|
298 | 298 | throw new EE_Error( |
299 | 299 | sprintf( |
300 | 300 | esc_html__( |
@@ -413,7 +413,7 @@ discard block |
||
413 | 413 | */ |
414 | 414 | protected function calculateImageData($wpdb_row, $image_size) |
415 | 415 | { |
416 | - if (! Event::wpdbRowHasEventId($wpdb_row)) { |
|
416 | + if ( ! Event::wpdbRowHasEventId($wpdb_row)) { |
|
417 | 417 | throw new EE_Error( |
418 | 418 | sprintf( |
419 | 419 | esc_html__( |
@@ -429,7 +429,7 @@ discard block |
||
429 | 429 | $EVT_ID = $wpdb_row['Event_CPT.ID']; |
430 | 430 | $attachment_id = get_post_thumbnail_id($EVT_ID); |
431 | 431 | $data = wp_get_attachment_image_src($attachment_id, $image_size); |
432 | - if (! $data) { |
|
432 | + if ( ! $data) { |
|
433 | 433 | return null; |
434 | 434 | } |
435 | 435 | $generated = true; |
@@ -15,88 +15,88 @@ |
||
15 | 15 | * ------------------------------------------------------------------------ |
16 | 16 | */ |
17 | 17 | /** |
18 | - * |
|
19 | - * Class EE_Receipt_Line_Item_Display_Strategy |
|
20 | - * |
|
21 | - * Description |
|
22 | - * |
|
23 | - * @package Event Espresso |
|
24 | - * @subpackage core |
|
25 | - * @author Brent Christensen |
|
26 | - * |
|
27 | - * |
|
28 | - */ |
|
18 | + * |
|
19 | + * Class EE_Receipt_Line_Item_Display_Strategy |
|
20 | + * |
|
21 | + * Description |
|
22 | + * |
|
23 | + * @package Event Espresso |
|
24 | + * @subpackage core |
|
25 | + * @author Brent Christensen |
|
26 | + * |
|
27 | + * |
|
28 | + */ |
|
29 | 29 | |
30 | 30 | class EE_Receipt_Line_Item_Display_Strategy implements EEI_Line_Item_Display |
31 | 31 | { |
32 | 32 | |
33 | - /** |
|
34 | - * @param EE_Line_Item $line_item |
|
35 | - * @param array $options |
|
36 | - * @return mixed |
|
37 | - */ |
|
38 | - public function display_line_item(EE_Line_Item $line_item, $options = array()) |
|
39 | - { |
|
33 | + /** |
|
34 | + * @param EE_Line_Item $line_item |
|
35 | + * @param array $options |
|
36 | + * @return mixed |
|
37 | + */ |
|
38 | + public function display_line_item(EE_Line_Item $line_item, $options = array()) |
|
39 | + { |
|
40 | 40 | |
41 | - $html = ''; |
|
42 | - // set some default options and merge with incoming |
|
43 | - $default_options = array( |
|
44 | - 'show_desc' => true, |
|
45 | - 'odd' => false |
|
46 | - ); |
|
47 | - $options = array_merge($default_options, (array) $options); |
|
48 | - switch ($line_item->type()) { |
|
49 | - case EEM_Line_Item::type_total: |
|
50 | - // loop thru children |
|
51 | - foreach ($line_item->children() as $child_line_item) { |
|
52 | - // recursively feed children back into this method |
|
41 | + $html = ''; |
|
42 | + // set some default options and merge with incoming |
|
43 | + $default_options = array( |
|
44 | + 'show_desc' => true, |
|
45 | + 'odd' => false |
|
46 | + ); |
|
47 | + $options = array_merge($default_options, (array) $options); |
|
48 | + switch ($line_item->type()) { |
|
49 | + case EEM_Line_Item::type_total: |
|
50 | + // loop thru children |
|
51 | + foreach ($line_item->children() as $child_line_item) { |
|
52 | + // recursively feed children back into this method |
|
53 | 53 | // $html .= $this->display_line_item( $child_line_item, $options ); |
54 | - } |
|
54 | + } |
|
55 | 55 | // $html .= $this->_separator_row( $options ); |
56 | 56 | // $html .= $this->_total_row( $line_item, esc_html__('Total', 'event_espresso'), $options ); |
57 | - break; |
|
57 | + break; |
|
58 | 58 | |
59 | 59 | |
60 | - case EEM_Line_Item::type_sub_total: |
|
61 | - // loop thru children |
|
62 | - foreach ($line_item->children() as $child_line_item) { |
|
63 | - // recursively feed children back into this method |
|
60 | + case EEM_Line_Item::type_sub_total: |
|
61 | + // loop thru children |
|
62 | + foreach ($line_item->children() as $child_line_item) { |
|
63 | + // recursively feed children back into this method |
|
64 | 64 | // $html .= $this->display_line_item( $child_line_item, $options ); |
65 | - } |
|
65 | + } |
|
66 | 66 | // $html .= $this->_total_row( $line_item, esc_html__('Sub-Total', 'event_espresso'), $options ); |
67 | - break; |
|
67 | + break; |
|
68 | 68 | |
69 | 69 | |
70 | - case EEM_Line_Item::type_tax_sub_total: |
|
71 | - // loop thru children |
|
72 | - foreach ($line_item->children() as $child_line_item) { |
|
73 | - // recursively feed children back into this method |
|
70 | + case EEM_Line_Item::type_tax_sub_total: |
|
71 | + // loop thru children |
|
72 | + foreach ($line_item->children() as $child_line_item) { |
|
73 | + // recursively feed children back into this method |
|
74 | 74 | // $html .= $this->display_line_item( $child_line_item, $options ); |
75 | - } |
|
75 | + } |
|
76 | 76 | // $html .= $this->_total_row( $line_item, esc_html__('Tax Total', 'event_espresso'), $options ); |
77 | - break; |
|
77 | + break; |
|
78 | 78 | |
79 | 79 | |
80 | - case EEM_Line_Item::type_line_item: |
|
81 | - // item row |
|
80 | + case EEM_Line_Item::type_line_item: |
|
81 | + // item row |
|
82 | 82 | // $html .= $this->_item_row( $line_item, $options ); |
83 | - // got any kids? |
|
84 | - foreach ($line_item->children() as $child_line_item) { |
|
83 | + // got any kids? |
|
84 | + foreach ($line_item->children() as $child_line_item) { |
|
85 | 85 | // $this->display_line_item( $child_line_item, $options ); |
86 | - } |
|
87 | - break; |
|
86 | + } |
|
87 | + break; |
|
88 | 88 | |
89 | 89 | |
90 | - case EEM_Line_Item::type_sub_line_item: |
|
90 | + case EEM_Line_Item::type_sub_line_item: |
|
91 | 91 | // $html .= $this->_sub_item_row( $line_item, $options ); |
92 | - break; |
|
92 | + break; |
|
93 | 93 | |
94 | 94 | |
95 | - case EEM_Line_Item::type_tax: |
|
95 | + case EEM_Line_Item::type_tax: |
|
96 | 96 | // $html .= $this->_tax_row( $line_item, $options ); |
97 | - break; |
|
98 | - } |
|
97 | + break; |
|
98 | + } |
|
99 | 99 | |
100 | - return $html; |
|
101 | - } |
|
100 | + return $html; |
|
101 | + } |
|
102 | 102 | } |
@@ -72,22 +72,22 @@ discard block |
||
72 | 72 | |
73 | 73 | $name_html = $line_item_related_object instanceof EEI_Line_Item_Object |
74 | 74 | ? $line_item_related_object->name() : $line_item->name(); |
75 | - $name_html = $name_link ? '<a href="' . $name_link . '">' . $name_html . '</a>' |
|
75 | + $name_html = $name_link ? '<a href="'.$name_link.'">'.$name_html.'</a>' |
|
76 | 76 | : $name_html; |
77 | 77 | $name_html .= $line_item->is_taxable() ? ' *' : ''; |
78 | 78 | // maybe preface with icon? |
79 | 79 | $name_html = $line_item_related_object instanceof EEI_Has_Icon |
80 | - ? $line_item_related_object->get_icon() . $name_html |
|
80 | + ? $line_item_related_object->get_icon().$name_html |
|
81 | 81 | : $name_html; |
82 | - $name_html = '<span class="ee-line-item-name linked">' . $name_html . '</span><br>'; |
|
83 | - $name_html .= sprintf( |
|
82 | + $name_html = '<span class="ee-line-item-name linked">'.$name_html.'</span><br>'; |
|
83 | + $name_html .= sprintf( |
|
84 | 84 | _x('%1$sfor the %2$s: %3$s%4$s', 'eg. "for the Event: My Cool Event"', 'event_espresso'), |
85 | 85 | '<span class="ee-line-item-related-parent-object">', |
86 | 86 | $line_item->parent() instanceof EE_Line_Item |
87 | 87 | ? $line_item->parent()->OBJ_type_i18n() |
88 | 88 | : esc_html__('Item:', 'event_espresso'), |
89 | 89 | $parent_related_object_link |
90 | - ? '<a href="' . $parent_related_object_link . '">' . $parent_related_object_name . '</a>' |
|
90 | + ? '<a href="'.$parent_related_object_link.'">'.$parent_related_object_name.'</a>' |
|
91 | 91 | : $parent_related_object_name, |
92 | 92 | '</span>' |
93 | 93 | ); |
@@ -105,7 +105,7 @@ discard block |
||
105 | 105 | $type_html .= $this->_get_cancellations($line_item); |
106 | 106 | $type_html .= $line_item->OBJ_type() ? '<br />' : ''; |
107 | 107 | $code = $line_item_related_object instanceof EEI_Has_Code ? $line_item_related_object->code() : ''; |
108 | - $type_html .= ! empty($code) ? '<span class="ee-line-item-id">' . sprintf(esc_html__('Code: %s', 'event_espresso'), $code) . '</span>' : ''; |
|
108 | + $type_html .= ! empty($code) ? '<span class="ee-line-item-id">'.sprintf(esc_html__('Code: %s', 'event_espresso'), $code).'</span>' : ''; |
|
109 | 109 | $html .= EEH_HTML::td($type_html, '', 'jst-left'); |
110 | 110 | |
111 | 111 | // Date column |
@@ -114,7 +114,7 @@ discard block |
||
114 | 114 | $datetimes = $line_item_related_object->datetimes(); |
115 | 115 | foreach ($datetimes as $datetime) { |
116 | 116 | if ($datetime instanceof EE_Datetime) { |
117 | - $datetime_content .= $datetime->get_dtt_display_name() . '<br>'; |
|
117 | + $datetime_content .= $datetime->get_dtt_display_name().'<br>'; |
|
118 | 118 | } |
119 | 119 | } |
120 | 120 | } |
@@ -122,7 +122,7 @@ discard block |
||
122 | 122 | |
123 | 123 | // Amount Column |
124 | 124 | if ($line_item->is_percent()) { |
125 | - $html .= EEH_HTML::td($line_item->percent() . '%', '', 'jst-rght'); |
|
125 | + $html .= EEH_HTML::td($line_item->percent().'%', '', 'jst-rght'); |
|
126 | 126 | } else { |
127 | 127 | $html .= EEH_HTML::td($line_item->unit_price_no_code(), '', 'jst-rght'); |
128 | 128 | } |
@@ -147,7 +147,7 @@ discard block |
||
147 | 147 | // start of row |
148 | 148 | $html = EEH_HTML::tr('', 'admin-primary-mbox-taxes-tr'); |
149 | 149 | // name th |
150 | - $html .= EEH_HTML::th($line_item->name() . '(' . $line_item->get_pretty('LIN_percent') . '%)', '', 'jst-rght', '', ' colspan="3"'); |
|
150 | + $html .= EEH_HTML::th($line_item->name().'('.$line_item->get_pretty('LIN_percent').'%)', '', 'jst-rght', '', ' colspan="3"'); |
|
151 | 151 | // total th |
152 | 152 | $html .= EEH_HTML::th(EEH_Template::format_currency($line_item->total(), false, false), '', 'jst-rght'); |
153 | 153 | // end of row |
@@ -180,9 +180,9 @@ discard block |
||
180 | 180 | $html = EEH_HTML::tr('', '', 'admin-primary-mbox-total-tr'); |
181 | 181 | // Total th label |
182 | 182 | if ($total_match) { |
183 | - $total_label = sprintf(esc_html__('This registration\'s total %s:', 'event_espresso'), '(' . EE_Registry::instance()->CFG->currency->code . ')'); |
|
183 | + $total_label = sprintf(esc_html__('This registration\'s total %s:', 'event_espresso'), '('.EE_Registry::instance()->CFG->currency->code.')'); |
|
184 | 184 | } else { |
185 | - $total_label = sprintf(esc_html__('This registration\'s approximate total %s', 'event_espresso'), '(' . EE_Registry::instance()->CFG->currency->code . ')'); |
|
185 | + $total_label = sprintf(esc_html__('This registration\'s approximate total %s', 'event_espresso'), '('.EE_Registry::instance()->CFG->currency->code.')'); |
|
186 | 186 | $total_label .= '<br>'; |
187 | 187 | $total_label .= '<p class="ee-footnote-text">' |
188 | 188 | . sprintf( |
@@ -14,190 +14,190 @@ |
||
14 | 14 | class EE_Admin_Table_Registration_Line_Item_Display_Strategy extends EE_Admin_Table_Line_Item_Display_Strategy |
15 | 15 | { |
16 | 16 | |
17 | - /** |
|
18 | - * Table header for display. |
|
19 | - * @since 4.8 |
|
20 | - * @param array $options |
|
21 | - * @return string |
|
22 | - */ |
|
23 | - protected function _table_header($options) |
|
24 | - { |
|
25 | - $html = EEH_HTML::table('', '', $options['table_css_class']); |
|
26 | - $html .= EEH_HTML::thead(); |
|
27 | - $html .= EEH_HTML::tr(); |
|
28 | - $html .= EEH_HTML::th(esc_html__('Name', 'event_espresso'), '', 'jst-left'); |
|
29 | - $html .= EEH_HTML::th(esc_html__('Type', 'event_espresso'), '', 'jst-left'); |
|
30 | - $html .= EEH_HTML::th(esc_html__('Date(s)', 'event_espresso'), '', 'jst-left'); |
|
31 | - $html .= EEH_HTML::th(esc_html__('Amount', 'event_espresso'), '', 'jst-cntr'); |
|
32 | - $html .= EEH_HTML::tbody(); |
|
33 | - return $html; |
|
34 | - } |
|
35 | - |
|
36 | - |
|
37 | - |
|
38 | - |
|
39 | - |
|
40 | - /** |
|
41 | - * _item_row |
|
42 | - * |
|
43 | - * @param EE_Line_Item $line_item |
|
44 | - * @param array $options |
|
45 | - * @return mixed |
|
46 | - */ |
|
47 | - protected function _item_row(EE_Line_Item $line_item, $options = array()) |
|
48 | - { |
|
49 | - $line_item_related_object = $line_item->get_object(); |
|
50 | - $parent_line_item_related_object = $line_item->parent() instanceof EE_Line_Item |
|
51 | - ? $line_item->parent()->get_object() |
|
52 | - : null; |
|
53 | - // start of row |
|
54 | - $html = EEH_HTML::tr('', '', 'item'); |
|
55 | - |
|
56 | - |
|
57 | - // Name Column |
|
58 | - $name_link = $line_item_related_object instanceof EEI_Admin_Links ? $line_item_related_object->get_admin_details_link() : ''; |
|
59 | - |
|
60 | - // related object scope. |
|
61 | - $parent_related_object_name = $parent_line_item_related_object instanceof EEI_Line_Item_Object |
|
62 | - ? $parent_line_item_related_object->name() |
|
63 | - : ''; |
|
64 | - $parent_related_object_name = empty($parent_related_object_name) && $line_item->parent() instanceof EE_Line_Item |
|
65 | - ? $line_item->parent()->name() |
|
66 | - : $parent_related_object_name; |
|
67 | - $parent_related_object_link = $parent_line_item_related_object instanceof EEI_Admin_Links |
|
68 | - ? $parent_line_item_related_object->get_admin_details_link() |
|
69 | - : ''; |
|
70 | - |
|
71 | - |
|
72 | - $name_html = $line_item_related_object instanceof EEI_Line_Item_Object |
|
73 | - ? $line_item_related_object->name() : $line_item->name(); |
|
74 | - $name_html = $name_link ? '<a href="' . $name_link . '">' . $name_html . '</a>' |
|
75 | - : $name_html; |
|
76 | - $name_html .= $line_item->is_taxable() ? ' *' : ''; |
|
77 | - // maybe preface with icon? |
|
78 | - $name_html = $line_item_related_object instanceof EEI_Has_Icon |
|
79 | - ? $line_item_related_object->get_icon() . $name_html |
|
80 | - : $name_html; |
|
81 | - $name_html = '<span class="ee-line-item-name linked">' . $name_html . '</span><br>'; |
|
82 | - $name_html .= sprintf( |
|
83 | - _x('%1$sfor the %2$s: %3$s%4$s', 'eg. "for the Event: My Cool Event"', 'event_espresso'), |
|
84 | - '<span class="ee-line-item-related-parent-object">', |
|
85 | - $line_item->parent() instanceof EE_Line_Item |
|
86 | - ? $line_item->parent()->OBJ_type_i18n() |
|
87 | - : esc_html__('Item:', 'event_espresso'), |
|
88 | - $parent_related_object_link |
|
89 | - ? '<a href="' . $parent_related_object_link . '">' . $parent_related_object_name . '</a>' |
|
90 | - : $parent_related_object_name, |
|
91 | - '</span>' |
|
92 | - ); |
|
93 | - |
|
94 | - $name_html = apply_filters( |
|
95 | - 'FHEE__EE_Admin_Table_Registration_Line_Item_Display_Strategy___item_row__name_html', |
|
96 | - $name_html, |
|
97 | - $line_item, |
|
98 | - $options |
|
99 | - ); |
|
100 | - |
|
101 | - $html .= EEH_HTML::td($name_html, '', 'jst-left'); |
|
102 | - // Type Column |
|
103 | - $type_html = $line_item->OBJ_type() ? $line_item->OBJ_type_i18n() : ''; |
|
104 | - $type_html .= $this->_get_cancellations($line_item); |
|
105 | - $type_html .= $line_item->OBJ_type() ? '<br />' : ''; |
|
106 | - $code = $line_item_related_object instanceof EEI_Has_Code ? $line_item_related_object->code() : ''; |
|
107 | - $type_html .= ! empty($code) ? '<span class="ee-line-item-id">' . sprintf(esc_html__('Code: %s', 'event_espresso'), $code) . '</span>' : ''; |
|
108 | - $html .= EEH_HTML::td($type_html, '', 'jst-left'); |
|
109 | - |
|
110 | - // Date column |
|
111 | - $datetime_content = ''; |
|
112 | - if ($line_item_related_object instanceof EE_Ticket) { |
|
113 | - $datetimes = $line_item_related_object->datetimes(); |
|
114 | - foreach ($datetimes as $datetime) { |
|
115 | - if ($datetime instanceof EE_Datetime) { |
|
116 | - $datetime_content .= $datetime->get_dtt_display_name() . '<br>'; |
|
117 | - } |
|
118 | - } |
|
119 | - } |
|
120 | - $html .= EEH_HTML::td($datetime_content, '', 'jst-left'); |
|
121 | - |
|
122 | - // Amount Column |
|
123 | - if ($line_item->is_percent()) { |
|
124 | - $html .= EEH_HTML::td($line_item->percent() . '%', '', 'jst-rght'); |
|
125 | - } else { |
|
126 | - $html .= EEH_HTML::td($line_item->unit_price_no_code(), '', 'jst-rght'); |
|
127 | - } |
|
128 | - |
|
129 | - |
|
130 | - // finish things off and return |
|
131 | - $html .= EEH_HTML::trx(); |
|
132 | - return $html; |
|
133 | - } |
|
134 | - |
|
135 | - |
|
136 | - |
|
137 | - /** |
|
138 | - * _tax_row |
|
139 | - * |
|
140 | - * @param EE_Line_Item $line_item |
|
141 | - * @param array $options |
|
142 | - * @return mixed |
|
143 | - */ |
|
144 | - protected function _tax_row(EE_Line_Item $line_item, $options = array()) |
|
145 | - { |
|
146 | - // start of row |
|
147 | - $html = EEH_HTML::tr('', 'admin-primary-mbox-taxes-tr'); |
|
148 | - // name th |
|
149 | - $html .= EEH_HTML::th($line_item->name() . '(' . $line_item->get_pretty('LIN_percent') . '%)', '', 'jst-rght', '', ' colspan="3"'); |
|
150 | - // total th |
|
151 | - $html .= EEH_HTML::th(EEH_Template::format_currency($line_item->total(), false, false), '', 'jst-rght'); |
|
152 | - // end of row |
|
153 | - $html .= EEH_HTML::trx(); |
|
154 | - return $html; |
|
155 | - } |
|
156 | - |
|
157 | - |
|
158 | - |
|
159 | - |
|
160 | - |
|
161 | - /** |
|
162 | - * _total_row |
|
163 | - * |
|
164 | - * @param EE_Line_Item $line_item |
|
165 | - * @param array $options |
|
166 | - * @return mixed |
|
167 | - */ |
|
168 | - protected function _total_row(EE_Line_Item $line_item, $options = array()) |
|
169 | - { |
|
170 | - |
|
171 | - $registration = isset($options['EE_Registration']) ? $options['EE_Registration'] : null; |
|
172 | - $registration_total = $registration instanceof EE_Registration ? $registration->pretty_final_price() : 0; |
|
173 | - // if no valid registration object then we're not going to show the approximate text. |
|
174 | - $total_match = $registration instanceof EE_Registration |
|
175 | - ? $registration->final_price() === $line_item->total() |
|
176 | - : true; |
|
177 | - |
|
178 | - // start of row |
|
179 | - $html = EEH_HTML::tr('', '', 'admin-primary-mbox-total-tr'); |
|
180 | - // Total th label |
|
181 | - if ($total_match) { |
|
182 | - $total_label = sprintf(esc_html__('This registration\'s total %s:', 'event_espresso'), '(' . EE_Registry::instance()->CFG->currency->code . ')'); |
|
183 | - } else { |
|
184 | - $total_label = sprintf(esc_html__('This registration\'s approximate total %s', 'event_espresso'), '(' . EE_Registry::instance()->CFG->currency->code . ')'); |
|
185 | - $total_label .= '<br>'; |
|
186 | - $total_label .= '<p class="ee-footnote-text">' |
|
187 | - . sprintf( |
|
188 | - esc_html__('The registrations\' share of the transaction total is approximate because it might not be possible to evenly divide the transaction total among each registration, and so some registrations may need to pay a penny more than others. This registration\'s final share is actually %1$s%2$s%3$s.', 'event_espresso'), |
|
189 | - '<strong>', |
|
190 | - $registration_total, |
|
191 | - '</strong>' |
|
192 | - ) |
|
193 | - . '</p>'; |
|
194 | - } |
|
195 | - $html .= EEH_HTML::th($total_label, '', 'jst-rght', '', ' colspan="3"'); |
|
196 | - // total th |
|
197 | - |
|
198 | - $html .= EEH_HTML::th(EEH_Template::format_currency($line_item->total(), false, false), '', 'jst-rght'); |
|
199 | - // end of row |
|
200 | - $html .= EEH_HTML::trx(); |
|
201 | - return $html; |
|
202 | - } |
|
17 | + /** |
|
18 | + * Table header for display. |
|
19 | + * @since 4.8 |
|
20 | + * @param array $options |
|
21 | + * @return string |
|
22 | + */ |
|
23 | + protected function _table_header($options) |
|
24 | + { |
|
25 | + $html = EEH_HTML::table('', '', $options['table_css_class']); |
|
26 | + $html .= EEH_HTML::thead(); |
|
27 | + $html .= EEH_HTML::tr(); |
|
28 | + $html .= EEH_HTML::th(esc_html__('Name', 'event_espresso'), '', 'jst-left'); |
|
29 | + $html .= EEH_HTML::th(esc_html__('Type', 'event_espresso'), '', 'jst-left'); |
|
30 | + $html .= EEH_HTML::th(esc_html__('Date(s)', 'event_espresso'), '', 'jst-left'); |
|
31 | + $html .= EEH_HTML::th(esc_html__('Amount', 'event_espresso'), '', 'jst-cntr'); |
|
32 | + $html .= EEH_HTML::tbody(); |
|
33 | + return $html; |
|
34 | + } |
|
35 | + |
|
36 | + |
|
37 | + |
|
38 | + |
|
39 | + |
|
40 | + /** |
|
41 | + * _item_row |
|
42 | + * |
|
43 | + * @param EE_Line_Item $line_item |
|
44 | + * @param array $options |
|
45 | + * @return mixed |
|
46 | + */ |
|
47 | + protected function _item_row(EE_Line_Item $line_item, $options = array()) |
|
48 | + { |
|
49 | + $line_item_related_object = $line_item->get_object(); |
|
50 | + $parent_line_item_related_object = $line_item->parent() instanceof EE_Line_Item |
|
51 | + ? $line_item->parent()->get_object() |
|
52 | + : null; |
|
53 | + // start of row |
|
54 | + $html = EEH_HTML::tr('', '', 'item'); |
|
55 | + |
|
56 | + |
|
57 | + // Name Column |
|
58 | + $name_link = $line_item_related_object instanceof EEI_Admin_Links ? $line_item_related_object->get_admin_details_link() : ''; |
|
59 | + |
|
60 | + // related object scope. |
|
61 | + $parent_related_object_name = $parent_line_item_related_object instanceof EEI_Line_Item_Object |
|
62 | + ? $parent_line_item_related_object->name() |
|
63 | + : ''; |
|
64 | + $parent_related_object_name = empty($parent_related_object_name) && $line_item->parent() instanceof EE_Line_Item |
|
65 | + ? $line_item->parent()->name() |
|
66 | + : $parent_related_object_name; |
|
67 | + $parent_related_object_link = $parent_line_item_related_object instanceof EEI_Admin_Links |
|
68 | + ? $parent_line_item_related_object->get_admin_details_link() |
|
69 | + : ''; |
|
70 | + |
|
71 | + |
|
72 | + $name_html = $line_item_related_object instanceof EEI_Line_Item_Object |
|
73 | + ? $line_item_related_object->name() : $line_item->name(); |
|
74 | + $name_html = $name_link ? '<a href="' . $name_link . '">' . $name_html . '</a>' |
|
75 | + : $name_html; |
|
76 | + $name_html .= $line_item->is_taxable() ? ' *' : ''; |
|
77 | + // maybe preface with icon? |
|
78 | + $name_html = $line_item_related_object instanceof EEI_Has_Icon |
|
79 | + ? $line_item_related_object->get_icon() . $name_html |
|
80 | + : $name_html; |
|
81 | + $name_html = '<span class="ee-line-item-name linked">' . $name_html . '</span><br>'; |
|
82 | + $name_html .= sprintf( |
|
83 | + _x('%1$sfor the %2$s: %3$s%4$s', 'eg. "for the Event: My Cool Event"', 'event_espresso'), |
|
84 | + '<span class="ee-line-item-related-parent-object">', |
|
85 | + $line_item->parent() instanceof EE_Line_Item |
|
86 | + ? $line_item->parent()->OBJ_type_i18n() |
|
87 | + : esc_html__('Item:', 'event_espresso'), |
|
88 | + $parent_related_object_link |
|
89 | + ? '<a href="' . $parent_related_object_link . '">' . $parent_related_object_name . '</a>' |
|
90 | + : $parent_related_object_name, |
|
91 | + '</span>' |
|
92 | + ); |
|
93 | + |
|
94 | + $name_html = apply_filters( |
|
95 | + 'FHEE__EE_Admin_Table_Registration_Line_Item_Display_Strategy___item_row__name_html', |
|
96 | + $name_html, |
|
97 | + $line_item, |
|
98 | + $options |
|
99 | + ); |
|
100 | + |
|
101 | + $html .= EEH_HTML::td($name_html, '', 'jst-left'); |
|
102 | + // Type Column |
|
103 | + $type_html = $line_item->OBJ_type() ? $line_item->OBJ_type_i18n() : ''; |
|
104 | + $type_html .= $this->_get_cancellations($line_item); |
|
105 | + $type_html .= $line_item->OBJ_type() ? '<br />' : ''; |
|
106 | + $code = $line_item_related_object instanceof EEI_Has_Code ? $line_item_related_object->code() : ''; |
|
107 | + $type_html .= ! empty($code) ? '<span class="ee-line-item-id">' . sprintf(esc_html__('Code: %s', 'event_espresso'), $code) . '</span>' : ''; |
|
108 | + $html .= EEH_HTML::td($type_html, '', 'jst-left'); |
|
109 | + |
|
110 | + // Date column |
|
111 | + $datetime_content = ''; |
|
112 | + if ($line_item_related_object instanceof EE_Ticket) { |
|
113 | + $datetimes = $line_item_related_object->datetimes(); |
|
114 | + foreach ($datetimes as $datetime) { |
|
115 | + if ($datetime instanceof EE_Datetime) { |
|
116 | + $datetime_content .= $datetime->get_dtt_display_name() . '<br>'; |
|
117 | + } |
|
118 | + } |
|
119 | + } |
|
120 | + $html .= EEH_HTML::td($datetime_content, '', 'jst-left'); |
|
121 | + |
|
122 | + // Amount Column |
|
123 | + if ($line_item->is_percent()) { |
|
124 | + $html .= EEH_HTML::td($line_item->percent() . '%', '', 'jst-rght'); |
|
125 | + } else { |
|
126 | + $html .= EEH_HTML::td($line_item->unit_price_no_code(), '', 'jst-rght'); |
|
127 | + } |
|
128 | + |
|
129 | + |
|
130 | + // finish things off and return |
|
131 | + $html .= EEH_HTML::trx(); |
|
132 | + return $html; |
|
133 | + } |
|
134 | + |
|
135 | + |
|
136 | + |
|
137 | + /** |
|
138 | + * _tax_row |
|
139 | + * |
|
140 | + * @param EE_Line_Item $line_item |
|
141 | + * @param array $options |
|
142 | + * @return mixed |
|
143 | + */ |
|
144 | + protected function _tax_row(EE_Line_Item $line_item, $options = array()) |
|
145 | + { |
|
146 | + // start of row |
|
147 | + $html = EEH_HTML::tr('', 'admin-primary-mbox-taxes-tr'); |
|
148 | + // name th |
|
149 | + $html .= EEH_HTML::th($line_item->name() . '(' . $line_item->get_pretty('LIN_percent') . '%)', '', 'jst-rght', '', ' colspan="3"'); |
|
150 | + // total th |
|
151 | + $html .= EEH_HTML::th(EEH_Template::format_currency($line_item->total(), false, false), '', 'jst-rght'); |
|
152 | + // end of row |
|
153 | + $html .= EEH_HTML::trx(); |
|
154 | + return $html; |
|
155 | + } |
|
156 | + |
|
157 | + |
|
158 | + |
|
159 | + |
|
160 | + |
|
161 | + /** |
|
162 | + * _total_row |
|
163 | + * |
|
164 | + * @param EE_Line_Item $line_item |
|
165 | + * @param array $options |
|
166 | + * @return mixed |
|
167 | + */ |
|
168 | + protected function _total_row(EE_Line_Item $line_item, $options = array()) |
|
169 | + { |
|
170 | + |
|
171 | + $registration = isset($options['EE_Registration']) ? $options['EE_Registration'] : null; |
|
172 | + $registration_total = $registration instanceof EE_Registration ? $registration->pretty_final_price() : 0; |
|
173 | + // if no valid registration object then we're not going to show the approximate text. |
|
174 | + $total_match = $registration instanceof EE_Registration |
|
175 | + ? $registration->final_price() === $line_item->total() |
|
176 | + : true; |
|
177 | + |
|
178 | + // start of row |
|
179 | + $html = EEH_HTML::tr('', '', 'admin-primary-mbox-total-tr'); |
|
180 | + // Total th label |
|
181 | + if ($total_match) { |
|
182 | + $total_label = sprintf(esc_html__('This registration\'s total %s:', 'event_espresso'), '(' . EE_Registry::instance()->CFG->currency->code . ')'); |
|
183 | + } else { |
|
184 | + $total_label = sprintf(esc_html__('This registration\'s approximate total %s', 'event_espresso'), '(' . EE_Registry::instance()->CFG->currency->code . ')'); |
|
185 | + $total_label .= '<br>'; |
|
186 | + $total_label .= '<p class="ee-footnote-text">' |
|
187 | + . sprintf( |
|
188 | + esc_html__('The registrations\' share of the transaction total is approximate because it might not be possible to evenly divide the transaction total among each registration, and so some registrations may need to pay a penny more than others. This registration\'s final share is actually %1$s%2$s%3$s.', 'event_espresso'), |
|
189 | + '<strong>', |
|
190 | + $registration_total, |
|
191 | + '</strong>' |
|
192 | + ) |
|
193 | + . '</p>'; |
|
194 | + } |
|
195 | + $html .= EEH_HTML::th($total_label, '', 'jst-rght', '', ' colspan="3"'); |
|
196 | + // total th |
|
197 | + |
|
198 | + $html .= EEH_HTML::th(EEH_Template::format_currency($line_item->total(), false, false), '', 'jst-rght'); |
|
199 | + // end of row |
|
200 | + $html .= EEH_HTML::trx(); |
|
201 | + return $html; |
|
202 | + } |
|
203 | 203 | } |
@@ -95,7 +95,7 @@ discard block |
||
95 | 95 | // used by display strategies |
96 | 96 | // assign incoming values to properties |
97 | 97 | foreach ($options_array as $key => $value) { |
98 | - $key = '_' . $key; |
|
98 | + $key = '_'.$key; |
|
99 | 99 | if (property_exists($this, $key) && empty($this->{$key})) { |
100 | 100 | $this->{$key} = $value; |
101 | 101 | if ($key === '_subsections' && ! is_array($value)) { |
@@ -135,7 +135,7 @@ discard block |
||
135 | 135 | */ |
136 | 136 | public function ensure_construct_finalized_called() |
137 | 137 | { |
138 | - if (! $this->_construction_finalized) { |
|
138 | + if ( ! $this->_construction_finalized) { |
|
139 | 139 | $this->_construct_finalize($this->_parent_section, $this->_name); |
140 | 140 | } |
141 | 141 | } |
@@ -198,7 +198,7 @@ discard block |
||
198 | 198 | */ |
199 | 199 | protected function _set_default_html_id_if_empty() |
200 | 200 | { |
201 | - if (! $this->_html_id) { |
|
201 | + if ( ! $this->_html_id) { |
|
202 | 202 | if ($this->_parent_section && $this->_parent_section instanceof EE_Form_Section_Proper) { |
203 | 203 | $this->_html_id = $this->_parent_section->html_id() |
204 | 204 | . '-' |
@@ -257,7 +257,7 @@ discard block |
||
257 | 257 | public function html_id($add_pound_sign = false) |
258 | 258 | { |
259 | 259 | $this->_set_default_html_id_if_empty(); |
260 | - return $add_pound_sign ? '#' . $this->_html_id : $this->_html_id; |
|
260 | + return $add_pound_sign ? '#'.$this->_html_id : $this->_html_id; |
|
261 | 261 | } |
262 | 262 | |
263 | 263 | |
@@ -327,7 +327,7 @@ discard block |
||
327 | 327 | */ |
328 | 328 | public function other_html_attributes() |
329 | 329 | { |
330 | - return ! empty($this->_other_html_attributes) ? ' ' . $this->_other_html_attributes : ''; |
|
330 | + return ! empty($this->_other_html_attributes) ? ' '.$this->_other_html_attributes : ''; |
|
331 | 331 | } |
332 | 332 | |
333 | 333 | |
@@ -340,7 +340,7 @@ discard block |
||
340 | 340 | */ |
341 | 341 | public function name() |
342 | 342 | { |
343 | - if (! $this->_construction_finalized) { |
|
343 | + if ( ! $this->_construction_finalized) { |
|
344 | 344 | throw new EE_Error(sprintf(esc_html__( |
345 | 345 | 'You cannot use the form section\s name until _construct_finalize has been called on it (when we set the name). It was called on a form section of type \'s\'', |
346 | 346 | 'event_espresso' |
@@ -373,18 +373,18 @@ discard block |
||
373 | 373 | */ |
374 | 374 | public function form_open($action = '', $method = '', $other_attributes = '') |
375 | 375 | { |
376 | - if (! empty($action)) { |
|
376 | + if ( ! empty($action)) { |
|
377 | 377 | $this->set_action($action); |
378 | 378 | } |
379 | - if (! empty($method)) { |
|
379 | + if ( ! empty($method)) { |
|
380 | 380 | $this->set_method($method); |
381 | 381 | } |
382 | - $html = EEH_HTML::nl(1, 'form') . '<form'; |
|
383 | - $html .= $this->html_id() !== '' ? ' id="' . $this->get_html_id_for_form($this->html_id()) . '"' : ''; |
|
384 | - $html .= ' action="' . $this->action() . '"'; |
|
385 | - $html .= ' method="' . $this->method() . '"'; |
|
386 | - $html .= ' name="' . $this->name() . '"'; |
|
387 | - $html .= $other_attributes . '>'; |
|
382 | + $html = EEH_HTML::nl(1, 'form').'<form'; |
|
383 | + $html .= $this->html_id() !== '' ? ' id="'.$this->get_html_id_for_form($this->html_id()).'"' : ''; |
|
384 | + $html .= ' action="'.$this->action().'"'; |
|
385 | + $html .= ' method="'.$this->method().'"'; |
|
386 | + $html .= ' name="'.$this->name().'"'; |
|
387 | + $html .= $other_attributes.'>'; |
|
388 | 388 | return $html; |
389 | 389 | } |
390 | 390 | |
@@ -402,7 +402,7 @@ discard block |
||
402 | 402 | $strlen = strlen($html_id); |
403 | 403 | $html_id = strpos($html_id, '-form') === $strlen - 5 || strpos($html_id, '-frm') === $strlen - 4 |
404 | 404 | ? $html_id |
405 | - : $html_id . '-frm'; |
|
405 | + : $html_id.'-frm'; |
|
406 | 406 | return $html_id; |
407 | 407 | } |
408 | 408 |
@@ -14,486 +14,486 @@ |
||
14 | 14 | */ |
15 | 15 | abstract class EE_Form_Section_Base |
16 | 16 | { |
17 | - /** |
|
18 | - * the URL the form is submitted to |
|
19 | - * |
|
20 | - * @var string |
|
21 | - */ |
|
22 | - protected $_action; |
|
23 | - |
|
24 | - /** |
|
25 | - * POST (default) or GET |
|
26 | - * |
|
27 | - * @var string |
|
28 | - */ |
|
29 | - protected $_method; |
|
30 | - |
|
31 | - /** |
|
32 | - * html_id and html_name are derived from this by default |
|
33 | - * |
|
34 | - * @var string |
|
35 | - */ |
|
36 | - protected $_name; |
|
37 | - |
|
38 | - /** |
|
39 | - * $_html_id |
|
40 | - * @var string |
|
41 | - */ |
|
42 | - protected $_html_id; |
|
43 | - |
|
44 | - /** |
|
45 | - * $_html_class |
|
46 | - * @var string |
|
47 | - */ |
|
48 | - protected $_html_class; |
|
49 | - |
|
50 | - /** |
|
51 | - * $_html_style |
|
52 | - * @var string |
|
53 | - */ |
|
54 | - protected $_html_style; |
|
55 | - |
|
56 | - /** |
|
57 | - * $_other_html_attributes |
|
58 | - * @var string |
|
59 | - */ |
|
60 | - protected $_other_html_attributes; |
|
61 | - |
|
62 | - /** |
|
63 | - * The form section of which this form section is a part |
|
64 | - * |
|
65 | - * @var EE_Form_Section_Proper |
|
66 | - */ |
|
67 | - protected $_parent_section; |
|
68 | - |
|
69 | - /** |
|
70 | - * flag indicating that _construct_finalize has been called. |
|
71 | - * If it has not been called and we try to use functions which require it, we call it |
|
72 | - * with no parameters. But normally, _construct_finalize should be called by the instantiating class |
|
73 | - * |
|
74 | - * @var boolean |
|
75 | - */ |
|
76 | - protected $_construction_finalized; |
|
77 | - |
|
78 | - /** |
|
79 | - * Strategy for parsing the form HTML upon display |
|
80 | - * |
|
81 | - * @var FormHtmlFilter |
|
82 | - */ |
|
83 | - protected $_form_html_filter; |
|
84 | - |
|
85 | - |
|
86 | - /** |
|
87 | - * @param array $options_array { |
|
88 | - * @type $name string the name for this form section, if you want to explicitly define it |
|
89 | - * } |
|
90 | - * @throws InvalidDataTypeException |
|
91 | - */ |
|
92 | - public function __construct($options_array = array()) |
|
93 | - { |
|
94 | - // used by display strategies |
|
95 | - // assign incoming values to properties |
|
96 | - foreach ($options_array as $key => $value) { |
|
97 | - $key = '_' . $key; |
|
98 | - if (property_exists($this, $key) && empty($this->{$key})) { |
|
99 | - $this->{$key} = $value; |
|
100 | - if ($key === '_subsections' && ! is_array($value)) { |
|
101 | - throw new InvalidDataTypeException($key, $value, 'array'); |
|
102 | - } |
|
103 | - } |
|
104 | - } |
|
105 | - // set parser which allows the form section's rendered HTML to be filtered |
|
106 | - if (isset($options_array['form_html_filter']) && $options_array['form_html_filter'] instanceof FormHtmlFilter) { |
|
107 | - $this->_form_html_filter = $options_array['form_html_filter']; |
|
108 | - } |
|
109 | - } |
|
110 | - |
|
111 | - |
|
112 | - |
|
113 | - /** |
|
114 | - * @param $parent_form_section |
|
115 | - * @param $name |
|
116 | - * @throws \EE_Error |
|
117 | - */ |
|
118 | - protected function _construct_finalize($parent_form_section, $name) |
|
119 | - { |
|
120 | - $this->_construction_finalized = true; |
|
121 | - $this->_parent_section = $parent_form_section; |
|
122 | - if ($name !== null) { |
|
123 | - $this->_name = $name; |
|
124 | - } |
|
125 | - } |
|
126 | - |
|
127 | - |
|
128 | - |
|
129 | - /** |
|
130 | - * make sure construction finalized was called, otherwise children might not be ready |
|
131 | - * |
|
132 | - * @return void |
|
133 | - * @throws \EE_Error |
|
134 | - */ |
|
135 | - public function ensure_construct_finalized_called() |
|
136 | - { |
|
137 | - if (! $this->_construction_finalized) { |
|
138 | - $this->_construct_finalize($this->_parent_section, $this->_name); |
|
139 | - } |
|
140 | - } |
|
141 | - |
|
142 | - |
|
143 | - |
|
144 | - /** |
|
145 | - * @return string |
|
146 | - */ |
|
147 | - public function action() |
|
148 | - { |
|
149 | - return $this->_action; |
|
150 | - } |
|
151 | - |
|
152 | - |
|
153 | - |
|
154 | - /** |
|
155 | - * @param string $action |
|
156 | - */ |
|
157 | - public function set_action($action) |
|
158 | - { |
|
159 | - $this->_action = $action; |
|
160 | - } |
|
161 | - |
|
162 | - |
|
163 | - |
|
164 | - /** |
|
165 | - * @return string |
|
166 | - */ |
|
167 | - public function method() |
|
168 | - { |
|
169 | - return ! empty($this->_method) ? $this->_method : 'POST'; |
|
170 | - } |
|
171 | - |
|
172 | - |
|
173 | - |
|
174 | - /** |
|
175 | - * @param string $method |
|
176 | - */ |
|
177 | - public function set_method($method) |
|
178 | - { |
|
179 | - switch ($method) { |
|
180 | - case 'get': |
|
181 | - case 'GET': |
|
182 | - $this->_method = 'GET'; |
|
183 | - break; |
|
184 | - default: |
|
185 | - $this->_method = 'POST'; |
|
186 | - } |
|
187 | - } |
|
188 | - |
|
189 | - |
|
190 | - |
|
191 | - /** |
|
192 | - * Sets the html_id to its default value, if none was specified in the constructor. |
|
193 | - * Calculation involves using the name and the parent's html id |
|
194 | - * return void |
|
195 | - * |
|
196 | - * @throws \EE_Error |
|
197 | - */ |
|
198 | - protected function _set_default_html_id_if_empty() |
|
199 | - { |
|
200 | - if (! $this->_html_id) { |
|
201 | - if ($this->_parent_section && $this->_parent_section instanceof EE_Form_Section_Proper) { |
|
202 | - $this->_html_id = $this->_parent_section->html_id() |
|
203 | - . '-' |
|
204 | - . $this->_prep_name_for_html_id($this->name()); |
|
205 | - } else { |
|
206 | - $this->_html_id = $this->_prep_name_for_html_id($this->name()); |
|
207 | - } |
|
208 | - } |
|
209 | - } |
|
210 | - |
|
211 | - |
|
212 | - |
|
213 | - /** |
|
214 | - * _prep_name_for_html_id |
|
215 | - * |
|
216 | - * @param $name |
|
217 | - * @return string |
|
218 | - */ |
|
219 | - private function _prep_name_for_html_id($name) |
|
220 | - { |
|
221 | - return sanitize_key(str_replace(array(' ', ' ', '_'), '-', $name)); |
|
222 | - } |
|
17 | + /** |
|
18 | + * the URL the form is submitted to |
|
19 | + * |
|
20 | + * @var string |
|
21 | + */ |
|
22 | + protected $_action; |
|
23 | + |
|
24 | + /** |
|
25 | + * POST (default) or GET |
|
26 | + * |
|
27 | + * @var string |
|
28 | + */ |
|
29 | + protected $_method; |
|
30 | + |
|
31 | + /** |
|
32 | + * html_id and html_name are derived from this by default |
|
33 | + * |
|
34 | + * @var string |
|
35 | + */ |
|
36 | + protected $_name; |
|
37 | + |
|
38 | + /** |
|
39 | + * $_html_id |
|
40 | + * @var string |
|
41 | + */ |
|
42 | + protected $_html_id; |
|
43 | + |
|
44 | + /** |
|
45 | + * $_html_class |
|
46 | + * @var string |
|
47 | + */ |
|
48 | + protected $_html_class; |
|
49 | + |
|
50 | + /** |
|
51 | + * $_html_style |
|
52 | + * @var string |
|
53 | + */ |
|
54 | + protected $_html_style; |
|
55 | + |
|
56 | + /** |
|
57 | + * $_other_html_attributes |
|
58 | + * @var string |
|
59 | + */ |
|
60 | + protected $_other_html_attributes; |
|
61 | + |
|
62 | + /** |
|
63 | + * The form section of which this form section is a part |
|
64 | + * |
|
65 | + * @var EE_Form_Section_Proper |
|
66 | + */ |
|
67 | + protected $_parent_section; |
|
68 | + |
|
69 | + /** |
|
70 | + * flag indicating that _construct_finalize has been called. |
|
71 | + * If it has not been called and we try to use functions which require it, we call it |
|
72 | + * with no parameters. But normally, _construct_finalize should be called by the instantiating class |
|
73 | + * |
|
74 | + * @var boolean |
|
75 | + */ |
|
76 | + protected $_construction_finalized; |
|
77 | + |
|
78 | + /** |
|
79 | + * Strategy for parsing the form HTML upon display |
|
80 | + * |
|
81 | + * @var FormHtmlFilter |
|
82 | + */ |
|
83 | + protected $_form_html_filter; |
|
84 | + |
|
85 | + |
|
86 | + /** |
|
87 | + * @param array $options_array { |
|
88 | + * @type $name string the name for this form section, if you want to explicitly define it |
|
89 | + * } |
|
90 | + * @throws InvalidDataTypeException |
|
91 | + */ |
|
92 | + public function __construct($options_array = array()) |
|
93 | + { |
|
94 | + // used by display strategies |
|
95 | + // assign incoming values to properties |
|
96 | + foreach ($options_array as $key => $value) { |
|
97 | + $key = '_' . $key; |
|
98 | + if (property_exists($this, $key) && empty($this->{$key})) { |
|
99 | + $this->{$key} = $value; |
|
100 | + if ($key === '_subsections' && ! is_array($value)) { |
|
101 | + throw new InvalidDataTypeException($key, $value, 'array'); |
|
102 | + } |
|
103 | + } |
|
104 | + } |
|
105 | + // set parser which allows the form section's rendered HTML to be filtered |
|
106 | + if (isset($options_array['form_html_filter']) && $options_array['form_html_filter'] instanceof FormHtmlFilter) { |
|
107 | + $this->_form_html_filter = $options_array['form_html_filter']; |
|
108 | + } |
|
109 | + } |
|
110 | + |
|
111 | + |
|
112 | + |
|
113 | + /** |
|
114 | + * @param $parent_form_section |
|
115 | + * @param $name |
|
116 | + * @throws \EE_Error |
|
117 | + */ |
|
118 | + protected function _construct_finalize($parent_form_section, $name) |
|
119 | + { |
|
120 | + $this->_construction_finalized = true; |
|
121 | + $this->_parent_section = $parent_form_section; |
|
122 | + if ($name !== null) { |
|
123 | + $this->_name = $name; |
|
124 | + } |
|
125 | + } |
|
126 | + |
|
127 | + |
|
128 | + |
|
129 | + /** |
|
130 | + * make sure construction finalized was called, otherwise children might not be ready |
|
131 | + * |
|
132 | + * @return void |
|
133 | + * @throws \EE_Error |
|
134 | + */ |
|
135 | + public function ensure_construct_finalized_called() |
|
136 | + { |
|
137 | + if (! $this->_construction_finalized) { |
|
138 | + $this->_construct_finalize($this->_parent_section, $this->_name); |
|
139 | + } |
|
140 | + } |
|
141 | + |
|
142 | + |
|
143 | + |
|
144 | + /** |
|
145 | + * @return string |
|
146 | + */ |
|
147 | + public function action() |
|
148 | + { |
|
149 | + return $this->_action; |
|
150 | + } |
|
151 | + |
|
152 | + |
|
153 | + |
|
154 | + /** |
|
155 | + * @param string $action |
|
156 | + */ |
|
157 | + public function set_action($action) |
|
158 | + { |
|
159 | + $this->_action = $action; |
|
160 | + } |
|
161 | + |
|
162 | + |
|
163 | + |
|
164 | + /** |
|
165 | + * @return string |
|
166 | + */ |
|
167 | + public function method() |
|
168 | + { |
|
169 | + return ! empty($this->_method) ? $this->_method : 'POST'; |
|
170 | + } |
|
171 | + |
|
172 | + |
|
173 | + |
|
174 | + /** |
|
175 | + * @param string $method |
|
176 | + */ |
|
177 | + public function set_method($method) |
|
178 | + { |
|
179 | + switch ($method) { |
|
180 | + case 'get': |
|
181 | + case 'GET': |
|
182 | + $this->_method = 'GET'; |
|
183 | + break; |
|
184 | + default: |
|
185 | + $this->_method = 'POST'; |
|
186 | + } |
|
187 | + } |
|
188 | + |
|
189 | + |
|
190 | + |
|
191 | + /** |
|
192 | + * Sets the html_id to its default value, if none was specified in the constructor. |
|
193 | + * Calculation involves using the name and the parent's html id |
|
194 | + * return void |
|
195 | + * |
|
196 | + * @throws \EE_Error |
|
197 | + */ |
|
198 | + protected function _set_default_html_id_if_empty() |
|
199 | + { |
|
200 | + if (! $this->_html_id) { |
|
201 | + if ($this->_parent_section && $this->_parent_section instanceof EE_Form_Section_Proper) { |
|
202 | + $this->_html_id = $this->_parent_section->html_id() |
|
203 | + . '-' |
|
204 | + . $this->_prep_name_for_html_id($this->name()); |
|
205 | + } else { |
|
206 | + $this->_html_id = $this->_prep_name_for_html_id($this->name()); |
|
207 | + } |
|
208 | + } |
|
209 | + } |
|
210 | + |
|
211 | + |
|
212 | + |
|
213 | + /** |
|
214 | + * _prep_name_for_html_id |
|
215 | + * |
|
216 | + * @param $name |
|
217 | + * @return string |
|
218 | + */ |
|
219 | + private function _prep_name_for_html_id($name) |
|
220 | + { |
|
221 | + return sanitize_key(str_replace(array(' ', ' ', '_'), '-', $name)); |
|
222 | + } |
|
223 | 223 | |
224 | 224 | |
225 | 225 | |
226 | - /** |
|
227 | - * Returns the HTML, JS, and CSS necessary to display this form section on a page. |
|
228 | - * Note however, it's recommended that you instead call enqueue_js on the "wp_enqueue_scripts" action, |
|
229 | - * and call get_html when you want to output the html. Calling get_html_and_js after |
|
230 | - * "wp_enqueue_scripts" has already fired seems to work for now, but is contrary |
|
231 | - * to the instructions on https://developer.wordpress.org/reference/functions/wp_enqueue_script/ |
|
232 | - * and so might stop working anytime. |
|
233 | - * |
|
234 | - * @return string |
|
235 | - */ |
|
236 | - public function get_html_and_js() |
|
237 | - { |
|
238 | - return $this->get_html(); |
|
239 | - } |
|
226 | + /** |
|
227 | + * Returns the HTML, JS, and CSS necessary to display this form section on a page. |
|
228 | + * Note however, it's recommended that you instead call enqueue_js on the "wp_enqueue_scripts" action, |
|
229 | + * and call get_html when you want to output the html. Calling get_html_and_js after |
|
230 | + * "wp_enqueue_scripts" has already fired seems to work for now, but is contrary |
|
231 | + * to the instructions on https://developer.wordpress.org/reference/functions/wp_enqueue_script/ |
|
232 | + * and so might stop working anytime. |
|
233 | + * |
|
234 | + * @return string |
|
235 | + */ |
|
236 | + public function get_html_and_js() |
|
237 | + { |
|
238 | + return $this->get_html(); |
|
239 | + } |
|
240 | 240 | |
241 | 241 | |
242 | - |
|
243 | - /** |
|
244 | - * Gets the HTML for displaying this form section |
|
245 | - * |
|
246 | - * @return string |
|
247 | - */ |
|
248 | - abstract public function get_html(); |
|
249 | - |
|
250 | - |
|
251 | - /** |
|
252 | - * @param bool $add_pound_sign |
|
253 | - * @return string |
|
254 | - * @throws EE_Error |
|
255 | - */ |
|
256 | - public function html_id($add_pound_sign = false) |
|
257 | - { |
|
258 | - $this->_set_default_html_id_if_empty(); |
|
259 | - return $add_pound_sign ? '#' . $this->_html_id : $this->_html_id; |
|
260 | - } |
|
261 | - |
|
262 | - |
|
263 | - |
|
264 | - /** |
|
265 | - * @return string |
|
266 | - */ |
|
267 | - public function html_class() |
|
268 | - { |
|
269 | - return $this->_html_class; |
|
270 | - } |
|
271 | - |
|
272 | - |
|
273 | - |
|
274 | - /** |
|
275 | - * @return string |
|
276 | - */ |
|
277 | - public function html_style() |
|
278 | - { |
|
279 | - return $this->_html_style; |
|
280 | - } |
|
281 | - |
|
282 | - |
|
283 | - |
|
284 | - /** |
|
285 | - * @param mixed $html_class |
|
286 | - */ |
|
287 | - public function set_html_class($html_class) |
|
288 | - { |
|
289 | - $this->_html_class = $html_class; |
|
290 | - } |
|
291 | - |
|
292 | - |
|
293 | - |
|
294 | - /** |
|
295 | - * @param mixed $html_id |
|
296 | - */ |
|
297 | - public function set_html_id($html_id) |
|
298 | - { |
|
299 | - $this->_html_id = $html_id; |
|
300 | - } |
|
301 | - |
|
302 | - |
|
303 | - |
|
304 | - /** |
|
305 | - * @param mixed $html_style |
|
306 | - */ |
|
307 | - public function set_html_style($html_style) |
|
308 | - { |
|
309 | - $this->_html_style = $html_style; |
|
310 | - } |
|
311 | - |
|
312 | - |
|
313 | - |
|
314 | - /** |
|
315 | - * @param string $other_html_attributes |
|
316 | - */ |
|
317 | - public function set_other_html_attributes($other_html_attributes) |
|
318 | - { |
|
319 | - $this->_other_html_attributes = $other_html_attributes; |
|
320 | - } |
|
321 | - |
|
322 | - |
|
323 | - |
|
324 | - /** |
|
325 | - * @return string |
|
326 | - */ |
|
327 | - public function other_html_attributes() |
|
328 | - { |
|
329 | - return ! empty($this->_other_html_attributes) ? ' ' . $this->_other_html_attributes : ''; |
|
330 | - } |
|
331 | - |
|
332 | - |
|
333 | - |
|
334 | - /** |
|
335 | - * Gets the name of the form section. This is not the same as the HTML name. |
|
336 | - * |
|
337 | - * @throws EE_Error |
|
338 | - * @return string |
|
339 | - */ |
|
340 | - public function name() |
|
341 | - { |
|
342 | - if (! $this->_construction_finalized) { |
|
343 | - throw new EE_Error(sprintf(esc_html__( |
|
344 | - 'You cannot use the form section\s name until _construct_finalize has been called on it (when we set the name). It was called on a form section of type \'s\'', |
|
345 | - 'event_espresso' |
|
346 | - ), get_class($this))); |
|
347 | - } |
|
348 | - return $this->_name; |
|
349 | - } |
|
350 | - |
|
351 | - |
|
352 | - |
|
353 | - /** |
|
354 | - * Gets the parent section |
|
355 | - * |
|
356 | - * @return EE_Form_Section_Proper |
|
357 | - */ |
|
358 | - public function parent_section() |
|
359 | - { |
|
360 | - return $this->_parent_section; |
|
361 | - } |
|
362 | - |
|
363 | - |
|
364 | - /** |
|
365 | - * returns HTML for generating the opening form HTML tag (<form>) |
|
366 | - * |
|
367 | - * @param string $action the URL the form is submitted to |
|
368 | - * @param string $method POST (default) or GET |
|
369 | - * @param string $other_attributes anything else added to the form open tag, MUST BE VALID HTML |
|
370 | - * @return string |
|
371 | - * @throws EE_Error |
|
372 | - */ |
|
373 | - public function form_open($action = '', $method = '', $other_attributes = '') |
|
374 | - { |
|
375 | - if (! empty($action)) { |
|
376 | - $this->set_action($action); |
|
377 | - } |
|
378 | - if (! empty($method)) { |
|
379 | - $this->set_method($method); |
|
380 | - } |
|
381 | - $html = EEH_HTML::nl(1, 'form') . '<form'; |
|
382 | - $html .= $this->html_id() !== '' ? ' id="' . $this->get_html_id_for_form($this->html_id()) . '"' : ''; |
|
383 | - $html .= ' action="' . $this->action() . '"'; |
|
384 | - $html .= ' method="' . $this->method() . '"'; |
|
385 | - $html .= ' name="' . $this->name() . '"'; |
|
386 | - $html .= $other_attributes . '>'; |
|
387 | - return $html; |
|
388 | - } |
|
389 | - |
|
390 | - |
|
391 | - |
|
392 | - /** |
|
393 | - * ensures that html id for form either ends in "-form" or "-frm" |
|
394 | - * so that id doesn't conflict/collide with other elements |
|
395 | - * |
|
396 | - * @param string $html_id |
|
397 | - * @return string |
|
398 | - */ |
|
399 | - protected function get_html_id_for_form($html_id) |
|
400 | - { |
|
401 | - $strlen = strlen($html_id); |
|
402 | - $html_id = strpos($html_id, '-form') === $strlen - 5 || strpos($html_id, '-frm') === $strlen - 4 |
|
403 | - ? $html_id |
|
404 | - : $html_id . '-frm'; |
|
405 | - return $html_id; |
|
406 | - } |
|
407 | - |
|
408 | - |
|
409 | - /** |
|
410 | - * returns HTML for generating the closing form HTML tag (</form>) |
|
411 | - * |
|
412 | - * @return string |
|
413 | - * @throws EE_Error |
|
414 | - */ |
|
415 | - public function form_close() |
|
416 | - { |
|
417 | - return EEH_HTML::nl(-1, 'form') |
|
418 | - . '</form>' |
|
419 | - . EEH_HTML::nl() |
|
420 | - . '<!-- end of ee-' |
|
421 | - . $this->html_id() |
|
422 | - . '-form -->' |
|
423 | - . EEH_HTML::nl(); |
|
424 | - } |
|
425 | - |
|
426 | - |
|
427 | - |
|
428 | - /** |
|
429 | - * enqueues JS (and CSS) for the form (ie immediately call wp_enqueue_script and |
|
430 | - * wp_enqueue_style; the scripts could have optionally been registered earlier) |
|
431 | - * Default does nothing, but child classes can override |
|
432 | - * |
|
433 | - * @return void |
|
434 | - */ |
|
435 | - public function enqueue_js() |
|
436 | - { |
|
437 | - // defaults to enqueue NO js or css |
|
438 | - } |
|
439 | - |
|
440 | - |
|
441 | - |
|
442 | - /** |
|
443 | - * Adds any extra data needed by js. Eventually we'll call wp_localize_script |
|
444 | - * with it, and it will be on each form section's 'other_data' property. |
|
445 | - * By default nothing is added, but child classes can extend this method to add something. |
|
446 | - * Eg, if you have an input that will cause a modal dialog to appear, |
|
447 | - * here you could add an entry like 'modal_dialog_inputs' to this array |
|
448 | - * to map between the input's html ID and the modal dialogue's ID, so that |
|
449 | - * your JS code will know where to find the modal dialog when the input is pressed. |
|
450 | - * Eg $form_other_js_data['modal_dialog_inputs']['some-input-id']='modal-dialog-id'; |
|
451 | - * |
|
452 | - * @param array $form_other_js_data |
|
453 | - * @return array |
|
454 | - */ |
|
455 | - public function get_other_js_data($form_other_js_data = array()) |
|
456 | - { |
|
457 | - return $form_other_js_data; |
|
458 | - } |
|
459 | - |
|
460 | - |
|
461 | - |
|
462 | - /** |
|
463 | - * This isn't just the name of an input, it's a path pointing to an input. The |
|
464 | - * path is similar to a folder path: slash (/) means to descend into a subsection, |
|
465 | - * dot-dot-slash (../) means to ascend into the parent section. |
|
466 | - * After a series of slashes and dot-dot-slashes, there should be the name of an input, |
|
467 | - * which will be returned. |
|
468 | - * Eg, if you want the related input to be conditional on a sibling input name 'foobar' |
|
469 | - * just use 'foobar'. If you want it to be conditional on an aunt/uncle input name |
|
470 | - * 'baz', use '../baz'. If you want it to be conditional on a cousin input, |
|
471 | - * the child of 'baz_section' named 'baz_child', use '../baz_section/baz_child'. |
|
472 | - * Etc |
|
473 | - * |
|
474 | - * @param string|false $form_section_path we accept false also because substr( '../', '../' ) = false |
|
475 | - * @return EE_Form_Section_Base |
|
476 | - */ |
|
477 | - public function find_section_from_path($form_section_path) |
|
478 | - { |
|
479 | - if (strpos($form_section_path, '/') === 0) { |
|
480 | - $form_section_path = substr($form_section_path, strlen('/')); |
|
481 | - } |
|
482 | - if (empty($form_section_path)) { |
|
483 | - return $this; |
|
484 | - } |
|
485 | - if (strpos($form_section_path, '../') === 0) { |
|
486 | - $parent = $this->parent_section(); |
|
487 | - $form_section_path = substr($form_section_path, strlen('../')); |
|
488 | - if ($parent instanceof EE_Form_Section_Base) { |
|
489 | - return $parent->find_section_from_path($form_section_path); |
|
490 | - } |
|
491 | - if (empty($form_section_path)) { |
|
492 | - return $this; |
|
493 | - } |
|
494 | - } |
|
495 | - // couldn't find it using simple parent following |
|
496 | - return null; |
|
497 | - } |
|
242 | + |
|
243 | + /** |
|
244 | + * Gets the HTML for displaying this form section |
|
245 | + * |
|
246 | + * @return string |
|
247 | + */ |
|
248 | + abstract public function get_html(); |
|
249 | + |
|
250 | + |
|
251 | + /** |
|
252 | + * @param bool $add_pound_sign |
|
253 | + * @return string |
|
254 | + * @throws EE_Error |
|
255 | + */ |
|
256 | + public function html_id($add_pound_sign = false) |
|
257 | + { |
|
258 | + $this->_set_default_html_id_if_empty(); |
|
259 | + return $add_pound_sign ? '#' . $this->_html_id : $this->_html_id; |
|
260 | + } |
|
261 | + |
|
262 | + |
|
263 | + |
|
264 | + /** |
|
265 | + * @return string |
|
266 | + */ |
|
267 | + public function html_class() |
|
268 | + { |
|
269 | + return $this->_html_class; |
|
270 | + } |
|
271 | + |
|
272 | + |
|
273 | + |
|
274 | + /** |
|
275 | + * @return string |
|
276 | + */ |
|
277 | + public function html_style() |
|
278 | + { |
|
279 | + return $this->_html_style; |
|
280 | + } |
|
281 | + |
|
282 | + |
|
283 | + |
|
284 | + /** |
|
285 | + * @param mixed $html_class |
|
286 | + */ |
|
287 | + public function set_html_class($html_class) |
|
288 | + { |
|
289 | + $this->_html_class = $html_class; |
|
290 | + } |
|
291 | + |
|
292 | + |
|
293 | + |
|
294 | + /** |
|
295 | + * @param mixed $html_id |
|
296 | + */ |
|
297 | + public function set_html_id($html_id) |
|
298 | + { |
|
299 | + $this->_html_id = $html_id; |
|
300 | + } |
|
301 | + |
|
302 | + |
|
303 | + |
|
304 | + /** |
|
305 | + * @param mixed $html_style |
|
306 | + */ |
|
307 | + public function set_html_style($html_style) |
|
308 | + { |
|
309 | + $this->_html_style = $html_style; |
|
310 | + } |
|
311 | + |
|
312 | + |
|
313 | + |
|
314 | + /** |
|
315 | + * @param string $other_html_attributes |
|
316 | + */ |
|
317 | + public function set_other_html_attributes($other_html_attributes) |
|
318 | + { |
|
319 | + $this->_other_html_attributes = $other_html_attributes; |
|
320 | + } |
|
321 | + |
|
322 | + |
|
323 | + |
|
324 | + /** |
|
325 | + * @return string |
|
326 | + */ |
|
327 | + public function other_html_attributes() |
|
328 | + { |
|
329 | + return ! empty($this->_other_html_attributes) ? ' ' . $this->_other_html_attributes : ''; |
|
330 | + } |
|
331 | + |
|
332 | + |
|
333 | + |
|
334 | + /** |
|
335 | + * Gets the name of the form section. This is not the same as the HTML name. |
|
336 | + * |
|
337 | + * @throws EE_Error |
|
338 | + * @return string |
|
339 | + */ |
|
340 | + public function name() |
|
341 | + { |
|
342 | + if (! $this->_construction_finalized) { |
|
343 | + throw new EE_Error(sprintf(esc_html__( |
|
344 | + 'You cannot use the form section\s name until _construct_finalize has been called on it (when we set the name). It was called on a form section of type \'s\'', |
|
345 | + 'event_espresso' |
|
346 | + ), get_class($this))); |
|
347 | + } |
|
348 | + return $this->_name; |
|
349 | + } |
|
350 | + |
|
351 | + |
|
352 | + |
|
353 | + /** |
|
354 | + * Gets the parent section |
|
355 | + * |
|
356 | + * @return EE_Form_Section_Proper |
|
357 | + */ |
|
358 | + public function parent_section() |
|
359 | + { |
|
360 | + return $this->_parent_section; |
|
361 | + } |
|
362 | + |
|
363 | + |
|
364 | + /** |
|
365 | + * returns HTML for generating the opening form HTML tag (<form>) |
|
366 | + * |
|
367 | + * @param string $action the URL the form is submitted to |
|
368 | + * @param string $method POST (default) or GET |
|
369 | + * @param string $other_attributes anything else added to the form open tag, MUST BE VALID HTML |
|
370 | + * @return string |
|
371 | + * @throws EE_Error |
|
372 | + */ |
|
373 | + public function form_open($action = '', $method = '', $other_attributes = '') |
|
374 | + { |
|
375 | + if (! empty($action)) { |
|
376 | + $this->set_action($action); |
|
377 | + } |
|
378 | + if (! empty($method)) { |
|
379 | + $this->set_method($method); |
|
380 | + } |
|
381 | + $html = EEH_HTML::nl(1, 'form') . '<form'; |
|
382 | + $html .= $this->html_id() !== '' ? ' id="' . $this->get_html_id_for_form($this->html_id()) . '"' : ''; |
|
383 | + $html .= ' action="' . $this->action() . '"'; |
|
384 | + $html .= ' method="' . $this->method() . '"'; |
|
385 | + $html .= ' name="' . $this->name() . '"'; |
|
386 | + $html .= $other_attributes . '>'; |
|
387 | + return $html; |
|
388 | + } |
|
389 | + |
|
390 | + |
|
391 | + |
|
392 | + /** |
|
393 | + * ensures that html id for form either ends in "-form" or "-frm" |
|
394 | + * so that id doesn't conflict/collide with other elements |
|
395 | + * |
|
396 | + * @param string $html_id |
|
397 | + * @return string |
|
398 | + */ |
|
399 | + protected function get_html_id_for_form($html_id) |
|
400 | + { |
|
401 | + $strlen = strlen($html_id); |
|
402 | + $html_id = strpos($html_id, '-form') === $strlen - 5 || strpos($html_id, '-frm') === $strlen - 4 |
|
403 | + ? $html_id |
|
404 | + : $html_id . '-frm'; |
|
405 | + return $html_id; |
|
406 | + } |
|
407 | + |
|
408 | + |
|
409 | + /** |
|
410 | + * returns HTML for generating the closing form HTML tag (</form>) |
|
411 | + * |
|
412 | + * @return string |
|
413 | + * @throws EE_Error |
|
414 | + */ |
|
415 | + public function form_close() |
|
416 | + { |
|
417 | + return EEH_HTML::nl(-1, 'form') |
|
418 | + . '</form>' |
|
419 | + . EEH_HTML::nl() |
|
420 | + . '<!-- end of ee-' |
|
421 | + . $this->html_id() |
|
422 | + . '-form -->' |
|
423 | + . EEH_HTML::nl(); |
|
424 | + } |
|
425 | + |
|
426 | + |
|
427 | + |
|
428 | + /** |
|
429 | + * enqueues JS (and CSS) for the form (ie immediately call wp_enqueue_script and |
|
430 | + * wp_enqueue_style; the scripts could have optionally been registered earlier) |
|
431 | + * Default does nothing, but child classes can override |
|
432 | + * |
|
433 | + * @return void |
|
434 | + */ |
|
435 | + public function enqueue_js() |
|
436 | + { |
|
437 | + // defaults to enqueue NO js or css |
|
438 | + } |
|
439 | + |
|
440 | + |
|
441 | + |
|
442 | + /** |
|
443 | + * Adds any extra data needed by js. Eventually we'll call wp_localize_script |
|
444 | + * with it, and it will be on each form section's 'other_data' property. |
|
445 | + * By default nothing is added, but child classes can extend this method to add something. |
|
446 | + * Eg, if you have an input that will cause a modal dialog to appear, |
|
447 | + * here you could add an entry like 'modal_dialog_inputs' to this array |
|
448 | + * to map between the input's html ID and the modal dialogue's ID, so that |
|
449 | + * your JS code will know where to find the modal dialog when the input is pressed. |
|
450 | + * Eg $form_other_js_data['modal_dialog_inputs']['some-input-id']='modal-dialog-id'; |
|
451 | + * |
|
452 | + * @param array $form_other_js_data |
|
453 | + * @return array |
|
454 | + */ |
|
455 | + public function get_other_js_data($form_other_js_data = array()) |
|
456 | + { |
|
457 | + return $form_other_js_data; |
|
458 | + } |
|
459 | + |
|
460 | + |
|
461 | + |
|
462 | + /** |
|
463 | + * This isn't just the name of an input, it's a path pointing to an input. The |
|
464 | + * path is similar to a folder path: slash (/) means to descend into a subsection, |
|
465 | + * dot-dot-slash (../) means to ascend into the parent section. |
|
466 | + * After a series of slashes and dot-dot-slashes, there should be the name of an input, |
|
467 | + * which will be returned. |
|
468 | + * Eg, if you want the related input to be conditional on a sibling input name 'foobar' |
|
469 | + * just use 'foobar'. If you want it to be conditional on an aunt/uncle input name |
|
470 | + * 'baz', use '../baz'. If you want it to be conditional on a cousin input, |
|
471 | + * the child of 'baz_section' named 'baz_child', use '../baz_section/baz_child'. |
|
472 | + * Etc |
|
473 | + * |
|
474 | + * @param string|false $form_section_path we accept false also because substr( '../', '../' ) = false |
|
475 | + * @return EE_Form_Section_Base |
|
476 | + */ |
|
477 | + public function find_section_from_path($form_section_path) |
|
478 | + { |
|
479 | + if (strpos($form_section_path, '/') === 0) { |
|
480 | + $form_section_path = substr($form_section_path, strlen('/')); |
|
481 | + } |
|
482 | + if (empty($form_section_path)) { |
|
483 | + return $this; |
|
484 | + } |
|
485 | + if (strpos($form_section_path, '../') === 0) { |
|
486 | + $parent = $this->parent_section(); |
|
487 | + $form_section_path = substr($form_section_path, strlen('../')); |
|
488 | + if ($parent instanceof EE_Form_Section_Base) { |
|
489 | + return $parent->find_section_from_path($form_section_path); |
|
490 | + } |
|
491 | + if (empty($form_section_path)) { |
|
492 | + return $this; |
|
493 | + } |
|
494 | + } |
|
495 | + // couldn't find it using simple parent following |
|
496 | + return null; |
|
497 | + } |
|
498 | 498 | } |
499 | 499 | // End of file EE_Form_Section_Base.form.php |