@@ -31,325 +31,325 @@ |
||
31 | 31 | { |
32 | 32 | |
33 | 33 | |
34 | - public function __construct() |
|
35 | - { |
|
36 | - parent::__construct(); |
|
37 | - EE_Registry::instance()->load_helper('Inflector'); |
|
38 | - } |
|
34 | + public function __construct() |
|
35 | + { |
|
36 | + parent::__construct(); |
|
37 | + EE_Registry::instance()->load_helper('Inflector'); |
|
38 | + } |
|
39 | 39 | |
40 | 40 | |
41 | - /** |
|
42 | - * Handles requests to get all (or a filtered subset) of entities for a particular model |
|
43 | - * |
|
44 | - * @param WP_REST_Request $request |
|
45 | - * @param string $version |
|
46 | - * @param string $model_name |
|
47 | - * @return WP_REST_Response|\WP_Error |
|
48 | - */ |
|
49 | - public static function handleRequestInsert(WP_REST_Request $request, $version, $model_name) |
|
50 | - { |
|
51 | - $controller = new Write(); |
|
52 | - try { |
|
53 | - $controller->setRequestedVersion($version); |
|
54 | - return $controller->sendResponse( |
|
55 | - $controller->insert( |
|
56 | - $controller->getModelVersionInfo()->loadModel($model_name), |
|
57 | - $request |
|
58 | - ) |
|
59 | - ); |
|
60 | - } catch (\Exception $e) { |
|
61 | - return $controller->sendResponse($e); |
|
62 | - } |
|
63 | - } |
|
41 | + /** |
|
42 | + * Handles requests to get all (or a filtered subset) of entities for a particular model |
|
43 | + * |
|
44 | + * @param WP_REST_Request $request |
|
45 | + * @param string $version |
|
46 | + * @param string $model_name |
|
47 | + * @return WP_REST_Response|\WP_Error |
|
48 | + */ |
|
49 | + public static function handleRequestInsert(WP_REST_Request $request, $version, $model_name) |
|
50 | + { |
|
51 | + $controller = new Write(); |
|
52 | + try { |
|
53 | + $controller->setRequestedVersion($version); |
|
54 | + return $controller->sendResponse( |
|
55 | + $controller->insert( |
|
56 | + $controller->getModelVersionInfo()->loadModel($model_name), |
|
57 | + $request |
|
58 | + ) |
|
59 | + ); |
|
60 | + } catch (\Exception $e) { |
|
61 | + return $controller->sendResponse($e); |
|
62 | + } |
|
63 | + } |
|
64 | 64 | |
65 | 65 | |
66 | - /** |
|
67 | - * Handles a request from \WP_REST_Server to update an EE model |
|
68 | - * |
|
69 | - * @param WP_REST_Request $request |
|
70 | - * @param string $version |
|
71 | - * @param string $model_name |
|
72 | - * @return WP_REST_Response|\WP_Error |
|
73 | - */ |
|
74 | - public static function handleRequestUpdate(WP_REST_Request $request, $version, $model_name) |
|
75 | - { |
|
76 | - $controller = new Write(); |
|
77 | - try { |
|
78 | - $controller->setRequestedVersion($version); |
|
79 | - return $controller->sendResponse( |
|
80 | - $controller->update( |
|
81 | - $controller->getModelVersionInfo()->loadModel($model_name), |
|
82 | - $request |
|
83 | - ) |
|
84 | - ); |
|
85 | - } catch (\Exception $e) { |
|
86 | - return $controller->sendResponse($e); |
|
87 | - } |
|
88 | - } |
|
66 | + /** |
|
67 | + * Handles a request from \WP_REST_Server to update an EE model |
|
68 | + * |
|
69 | + * @param WP_REST_Request $request |
|
70 | + * @param string $version |
|
71 | + * @param string $model_name |
|
72 | + * @return WP_REST_Response|\WP_Error |
|
73 | + */ |
|
74 | + public static function handleRequestUpdate(WP_REST_Request $request, $version, $model_name) |
|
75 | + { |
|
76 | + $controller = new Write(); |
|
77 | + try { |
|
78 | + $controller->setRequestedVersion($version); |
|
79 | + return $controller->sendResponse( |
|
80 | + $controller->update( |
|
81 | + $controller->getModelVersionInfo()->loadModel($model_name), |
|
82 | + $request |
|
83 | + ) |
|
84 | + ); |
|
85 | + } catch (\Exception $e) { |
|
86 | + return $controller->sendResponse($e); |
|
87 | + } |
|
88 | + } |
|
89 | 89 | |
90 | 90 | |
91 | - /** |
|
92 | - * Deletes a single model object and returns it. Unless |
|
93 | - * |
|
94 | - * @param WP_REST_Request $request |
|
95 | - * @param string $version |
|
96 | - * @param string $model_name |
|
97 | - * @return WP_REST_Response|\WP_Error |
|
98 | - */ |
|
99 | - public static function handleRequestDelete(WP_REST_Request $request, $version, $model_name) |
|
100 | - { |
|
101 | - $controller = new Write(); |
|
102 | - try { |
|
103 | - $controller->setRequestedVersion($version); |
|
104 | - return $controller->sendResponse( |
|
105 | - $controller->delete( |
|
106 | - $controller->getModelVersionInfo()->loadModel($model_name), |
|
107 | - $request |
|
108 | - ) |
|
109 | - ); |
|
110 | - } catch (\Exception $e) { |
|
111 | - return $controller->sendResponse($e); |
|
112 | - } |
|
113 | - } |
|
91 | + /** |
|
92 | + * Deletes a single model object and returns it. Unless |
|
93 | + * |
|
94 | + * @param WP_REST_Request $request |
|
95 | + * @param string $version |
|
96 | + * @param string $model_name |
|
97 | + * @return WP_REST_Response|\WP_Error |
|
98 | + */ |
|
99 | + public static function handleRequestDelete(WP_REST_Request $request, $version, $model_name) |
|
100 | + { |
|
101 | + $controller = new Write(); |
|
102 | + try { |
|
103 | + $controller->setRequestedVersion($version); |
|
104 | + return $controller->sendResponse( |
|
105 | + $controller->delete( |
|
106 | + $controller->getModelVersionInfo()->loadModel($model_name), |
|
107 | + $request |
|
108 | + ) |
|
109 | + ); |
|
110 | + } catch (\Exception $e) { |
|
111 | + return $controller->sendResponse($e); |
|
112 | + } |
|
113 | + } |
|
114 | 114 | |
115 | 115 | |
116 | - /** |
|
117 | - * Inserts a new model object according to the $request |
|
118 | - * |
|
119 | - * @param EEM_Base $model |
|
120 | - * @param WP_REST_Request $request |
|
121 | - * @return array |
|
122 | - * @throws EE_Error |
|
123 | - * @throws RestException |
|
124 | - */ |
|
125 | - public function insert(EEM_Base $model, WP_REST_Request $request) |
|
126 | - { |
|
127 | - Capabilities::verifyAtLeastPartialAccessTo($model, EEM_Base::caps_edit, 'create'); |
|
128 | - $default_cap_to_check_for = EE_Restriction_Generator_Base::get_default_restrictions_cap(); |
|
129 | - if (! current_user_can($default_cap_to_check_for)) { |
|
130 | - throw new RestException( |
|
131 | - 'rest_cannot_create_' . EEH_Inflector::pluralize_and_lower(($model->get_this_model_name())), |
|
132 | - sprintf( |
|
133 | - esc_html__( |
|
134 | - // @codingStandardsIgnoreStart |
|
135 | - 'For now, only those with the admin capability to "%1$s" are allowed to use the REST API to insert data into Event Espresso.', |
|
136 | - // @codingStandardsIgnoreEnd |
|
137 | - 'event_espresso' |
|
138 | - ), |
|
139 | - $default_cap_to_check_for |
|
140 | - ), |
|
141 | - array('status' => 403) |
|
142 | - ); |
|
143 | - } |
|
144 | - $submitted_json_data = array_merge((array) $request->get_body_params(), (array) $request->get_json_params()); |
|
145 | - $model_data = ModelDataTranslator::prepareConditionsQueryParamsForModels( |
|
146 | - $submitted_json_data, |
|
147 | - $model, |
|
148 | - $this->getModelVersionInfo()->requestedVersion(), |
|
149 | - true |
|
150 | - ); |
|
151 | - $model_obj = EE_Registry::instance()->load_class( |
|
152 | - $model->get_this_model_name(), |
|
153 | - array($model_data, $model->get_timezone()), |
|
154 | - false, |
|
155 | - false |
|
156 | - ); |
|
157 | - $model_obj->save(); |
|
158 | - $new_id = $model_obj->ID(); |
|
159 | - if (! $new_id) { |
|
160 | - throw new RestException( |
|
161 | - 'rest_insertion_failed', |
|
162 | - sprintf(__('Could not insert new %1$s', 'event_espresso'), $model->get_this_model_name()) |
|
163 | - ); |
|
164 | - } |
|
165 | - return $this->returnModelObjAsJsonResponse($model_obj, $request); |
|
166 | - } |
|
116 | + /** |
|
117 | + * Inserts a new model object according to the $request |
|
118 | + * |
|
119 | + * @param EEM_Base $model |
|
120 | + * @param WP_REST_Request $request |
|
121 | + * @return array |
|
122 | + * @throws EE_Error |
|
123 | + * @throws RestException |
|
124 | + */ |
|
125 | + public function insert(EEM_Base $model, WP_REST_Request $request) |
|
126 | + { |
|
127 | + Capabilities::verifyAtLeastPartialAccessTo($model, EEM_Base::caps_edit, 'create'); |
|
128 | + $default_cap_to_check_for = EE_Restriction_Generator_Base::get_default_restrictions_cap(); |
|
129 | + if (! current_user_can($default_cap_to_check_for)) { |
|
130 | + throw new RestException( |
|
131 | + 'rest_cannot_create_' . EEH_Inflector::pluralize_and_lower(($model->get_this_model_name())), |
|
132 | + sprintf( |
|
133 | + esc_html__( |
|
134 | + // @codingStandardsIgnoreStart |
|
135 | + 'For now, only those with the admin capability to "%1$s" are allowed to use the REST API to insert data into Event Espresso.', |
|
136 | + // @codingStandardsIgnoreEnd |
|
137 | + 'event_espresso' |
|
138 | + ), |
|
139 | + $default_cap_to_check_for |
|
140 | + ), |
|
141 | + array('status' => 403) |
|
142 | + ); |
|
143 | + } |
|
144 | + $submitted_json_data = array_merge((array) $request->get_body_params(), (array) $request->get_json_params()); |
|
145 | + $model_data = ModelDataTranslator::prepareConditionsQueryParamsForModels( |
|
146 | + $submitted_json_data, |
|
147 | + $model, |
|
148 | + $this->getModelVersionInfo()->requestedVersion(), |
|
149 | + true |
|
150 | + ); |
|
151 | + $model_obj = EE_Registry::instance()->load_class( |
|
152 | + $model->get_this_model_name(), |
|
153 | + array($model_data, $model->get_timezone()), |
|
154 | + false, |
|
155 | + false |
|
156 | + ); |
|
157 | + $model_obj->save(); |
|
158 | + $new_id = $model_obj->ID(); |
|
159 | + if (! $new_id) { |
|
160 | + throw new RestException( |
|
161 | + 'rest_insertion_failed', |
|
162 | + sprintf(__('Could not insert new %1$s', 'event_espresso'), $model->get_this_model_name()) |
|
163 | + ); |
|
164 | + } |
|
165 | + return $this->returnModelObjAsJsonResponse($model_obj, $request); |
|
166 | + } |
|
167 | 167 | |
168 | 168 | |
169 | - /** |
|
170 | - * Updates an existing model object according to the $request |
|
171 | - * |
|
172 | - * @param EEM_Base $model |
|
173 | - * @param WP_REST_Request $request |
|
174 | - * @return array |
|
175 | - * @throws EE_Error |
|
176 | - */ |
|
177 | - public function update(EEM_Base $model, WP_REST_Request $request) |
|
178 | - { |
|
179 | - Capabilities::verifyAtLeastPartialAccessTo($model, EEM_Base::caps_edit, 'edit'); |
|
180 | - $default_cap_to_check_for = EE_Restriction_Generator_Base::get_default_restrictions_cap(); |
|
181 | - if (! current_user_can($default_cap_to_check_for)) { |
|
182 | - throw new RestException( |
|
183 | - 'rest_cannot_edit_' . EEH_Inflector::pluralize_and_lower(($model->get_this_model_name())), |
|
184 | - sprintf( |
|
185 | - esc_html__( |
|
186 | - // @codingStandardsIgnoreStart |
|
187 | - 'For now, only those with the admin capability to "%1$s" are allowed to use the REST API to update data into Event Espresso.', |
|
188 | - // @codingStandardsIgnoreEnd |
|
189 | - 'event_espresso' |
|
190 | - ), |
|
191 | - $default_cap_to_check_for |
|
192 | - ), |
|
193 | - array('status' => 403) |
|
194 | - ); |
|
195 | - } |
|
196 | - $obj_id = $request->get_param('id'); |
|
197 | - if (! $obj_id) { |
|
198 | - throw new RestException( |
|
199 | - 'rest_edit_failed', |
|
200 | - sprintf(__('Could not edit %1$s', 'event_espresso'), $model->get_this_model_name()) |
|
201 | - ); |
|
202 | - } |
|
203 | - $model_data = ModelDataTranslator::prepareConditionsQueryParamsForModels( |
|
204 | - $this->getBodyParams($request), |
|
205 | - $model, |
|
206 | - $this->getModelVersionInfo()->requestedVersion(), |
|
207 | - true |
|
208 | - ); |
|
209 | - $model_obj = $model->get_one_by_ID($obj_id); |
|
210 | - if (! $model_obj instanceof EE_Base_Class) { |
|
211 | - $lowercase_model_name = strtolower($model->get_this_model_name()); |
|
212 | - throw new RestException( |
|
213 | - sprintf('rest_%s_invalid_id', $lowercase_model_name), |
|
214 | - sprintf(__('Invalid %s ID.', 'event_espresso'), $lowercase_model_name), |
|
215 | - array('status' => 404) |
|
216 | - ); |
|
217 | - } |
|
218 | - $model_obj->save($model_data); |
|
219 | - return $this->returnModelObjAsJsonResponse($model_obj, $request); |
|
220 | - } |
|
169 | + /** |
|
170 | + * Updates an existing model object according to the $request |
|
171 | + * |
|
172 | + * @param EEM_Base $model |
|
173 | + * @param WP_REST_Request $request |
|
174 | + * @return array |
|
175 | + * @throws EE_Error |
|
176 | + */ |
|
177 | + public function update(EEM_Base $model, WP_REST_Request $request) |
|
178 | + { |
|
179 | + Capabilities::verifyAtLeastPartialAccessTo($model, EEM_Base::caps_edit, 'edit'); |
|
180 | + $default_cap_to_check_for = EE_Restriction_Generator_Base::get_default_restrictions_cap(); |
|
181 | + if (! current_user_can($default_cap_to_check_for)) { |
|
182 | + throw new RestException( |
|
183 | + 'rest_cannot_edit_' . EEH_Inflector::pluralize_and_lower(($model->get_this_model_name())), |
|
184 | + sprintf( |
|
185 | + esc_html__( |
|
186 | + // @codingStandardsIgnoreStart |
|
187 | + 'For now, only those with the admin capability to "%1$s" are allowed to use the REST API to update data into Event Espresso.', |
|
188 | + // @codingStandardsIgnoreEnd |
|
189 | + 'event_espresso' |
|
190 | + ), |
|
191 | + $default_cap_to_check_for |
|
192 | + ), |
|
193 | + array('status' => 403) |
|
194 | + ); |
|
195 | + } |
|
196 | + $obj_id = $request->get_param('id'); |
|
197 | + if (! $obj_id) { |
|
198 | + throw new RestException( |
|
199 | + 'rest_edit_failed', |
|
200 | + sprintf(__('Could not edit %1$s', 'event_espresso'), $model->get_this_model_name()) |
|
201 | + ); |
|
202 | + } |
|
203 | + $model_data = ModelDataTranslator::prepareConditionsQueryParamsForModels( |
|
204 | + $this->getBodyParams($request), |
|
205 | + $model, |
|
206 | + $this->getModelVersionInfo()->requestedVersion(), |
|
207 | + true |
|
208 | + ); |
|
209 | + $model_obj = $model->get_one_by_ID($obj_id); |
|
210 | + if (! $model_obj instanceof EE_Base_Class) { |
|
211 | + $lowercase_model_name = strtolower($model->get_this_model_name()); |
|
212 | + throw new RestException( |
|
213 | + sprintf('rest_%s_invalid_id', $lowercase_model_name), |
|
214 | + sprintf(__('Invalid %s ID.', 'event_espresso'), $lowercase_model_name), |
|
215 | + array('status' => 404) |
|
216 | + ); |
|
217 | + } |
|
218 | + $model_obj->save($model_data); |
|
219 | + return $this->returnModelObjAsJsonResponse($model_obj, $request); |
|
220 | + } |
|
221 | 221 | |
222 | 222 | |
223 | - /** |
|
224 | - * Updates an existing model object according to the $request |
|
225 | - * |
|
226 | - * @param EEM_Base $model |
|
227 | - * @param WP_REST_Request $request |
|
228 | - * @return array of either the soft-deleted item, or |
|
229 | - * @throws EE_Error |
|
230 | - */ |
|
231 | - public function delete(EEM_Base $model, WP_REST_Request $request) |
|
232 | - { |
|
233 | - Capabilities::verifyAtLeastPartialAccessTo($model, EEM_Base::caps_delete, 'delete'); |
|
234 | - $default_cap_to_check_for = EE_Restriction_Generator_Base::get_default_restrictions_cap(); |
|
235 | - if (! current_user_can($default_cap_to_check_for)) { |
|
236 | - throw new RestException( |
|
237 | - 'rest_cannot_delete_' . EEH_Inflector::pluralize_and_lower(($model->get_this_model_name())), |
|
238 | - sprintf( |
|
239 | - esc_html__( |
|
240 | - // @codingStandardsIgnoreStart |
|
241 | - 'For now, only those with the admin capability to "%1$s" are allowed to use the REST API to delete data into Event Espresso.', |
|
242 | - // @codingStandardsIgnoreEnd |
|
243 | - 'event_espresso' |
|
244 | - ), |
|
245 | - $default_cap_to_check_for |
|
246 | - ), |
|
247 | - array('status' => 403) |
|
248 | - ); |
|
249 | - } |
|
250 | - $obj_id = $request->get_param('id'); |
|
251 | - // this is where we would apply more fine-grained caps |
|
252 | - $model_obj = $model->get_one_by_ID($obj_id); |
|
253 | - if (! $model_obj instanceof EE_Base_Class) { |
|
254 | - $lowercase_model_name = strtolower($model->get_this_model_name()); |
|
255 | - throw new RestException( |
|
256 | - sprintf('rest_%s_invalid_id', $lowercase_model_name), |
|
257 | - sprintf(__('Invalid %s ID.', 'event_espresso'), $lowercase_model_name), |
|
258 | - array('status' => 404) |
|
259 | - ); |
|
260 | - } |
|
261 | - $requested_permanent_delete = filter_var($request->get_param('force'), FILTER_VALIDATE_BOOLEAN); |
|
262 | - $requested_allow_blocking = filter_var($request->get_param('allow_blocking'), FILTER_VALIDATE_BOOLEAN); |
|
263 | - if ($requested_permanent_delete) { |
|
264 | - $previous = $this->returnModelObjAsJsonResponse($model_obj, $request); |
|
265 | - $deleted = (bool) $model->delete_permanently_by_ID($obj_id, $requested_allow_blocking); |
|
266 | - return array( |
|
267 | - 'deleted' => $deleted, |
|
268 | - 'previous' => $previous, |
|
269 | - ); |
|
270 | - } else { |
|
271 | - if ($model instanceof EEM_Soft_Delete_Base) { |
|
272 | - $model->delete_by_ID($obj_id, $requested_allow_blocking); |
|
273 | - return $this->returnModelObjAsJsonResponse($model_obj, $request); |
|
274 | - } else { |
|
275 | - throw new RestException( |
|
276 | - 'rest_trash_not_supported', |
|
277 | - 501, |
|
278 | - sprintf( |
|
279 | - esc_html__('%1$s do not support trashing. Set force=1 to delete.', 'event_espresso'), |
|
280 | - EEH_Inflector::pluralize($model->get_this_model_name()) |
|
281 | - ) |
|
282 | - ); |
|
283 | - } |
|
284 | - } |
|
285 | - } |
|
223 | + /** |
|
224 | + * Updates an existing model object according to the $request |
|
225 | + * |
|
226 | + * @param EEM_Base $model |
|
227 | + * @param WP_REST_Request $request |
|
228 | + * @return array of either the soft-deleted item, or |
|
229 | + * @throws EE_Error |
|
230 | + */ |
|
231 | + public function delete(EEM_Base $model, WP_REST_Request $request) |
|
232 | + { |
|
233 | + Capabilities::verifyAtLeastPartialAccessTo($model, EEM_Base::caps_delete, 'delete'); |
|
234 | + $default_cap_to_check_for = EE_Restriction_Generator_Base::get_default_restrictions_cap(); |
|
235 | + if (! current_user_can($default_cap_to_check_for)) { |
|
236 | + throw new RestException( |
|
237 | + 'rest_cannot_delete_' . EEH_Inflector::pluralize_and_lower(($model->get_this_model_name())), |
|
238 | + sprintf( |
|
239 | + esc_html__( |
|
240 | + // @codingStandardsIgnoreStart |
|
241 | + 'For now, only those with the admin capability to "%1$s" are allowed to use the REST API to delete data into Event Espresso.', |
|
242 | + // @codingStandardsIgnoreEnd |
|
243 | + 'event_espresso' |
|
244 | + ), |
|
245 | + $default_cap_to_check_for |
|
246 | + ), |
|
247 | + array('status' => 403) |
|
248 | + ); |
|
249 | + } |
|
250 | + $obj_id = $request->get_param('id'); |
|
251 | + // this is where we would apply more fine-grained caps |
|
252 | + $model_obj = $model->get_one_by_ID($obj_id); |
|
253 | + if (! $model_obj instanceof EE_Base_Class) { |
|
254 | + $lowercase_model_name = strtolower($model->get_this_model_name()); |
|
255 | + throw new RestException( |
|
256 | + sprintf('rest_%s_invalid_id', $lowercase_model_name), |
|
257 | + sprintf(__('Invalid %s ID.', 'event_espresso'), $lowercase_model_name), |
|
258 | + array('status' => 404) |
|
259 | + ); |
|
260 | + } |
|
261 | + $requested_permanent_delete = filter_var($request->get_param('force'), FILTER_VALIDATE_BOOLEAN); |
|
262 | + $requested_allow_blocking = filter_var($request->get_param('allow_blocking'), FILTER_VALIDATE_BOOLEAN); |
|
263 | + if ($requested_permanent_delete) { |
|
264 | + $previous = $this->returnModelObjAsJsonResponse($model_obj, $request); |
|
265 | + $deleted = (bool) $model->delete_permanently_by_ID($obj_id, $requested_allow_blocking); |
|
266 | + return array( |
|
267 | + 'deleted' => $deleted, |
|
268 | + 'previous' => $previous, |
|
269 | + ); |
|
270 | + } else { |
|
271 | + if ($model instanceof EEM_Soft_Delete_Base) { |
|
272 | + $model->delete_by_ID($obj_id, $requested_allow_blocking); |
|
273 | + return $this->returnModelObjAsJsonResponse($model_obj, $request); |
|
274 | + } else { |
|
275 | + throw new RestException( |
|
276 | + 'rest_trash_not_supported', |
|
277 | + 501, |
|
278 | + sprintf( |
|
279 | + esc_html__('%1$s do not support trashing. Set force=1 to delete.', 'event_espresso'), |
|
280 | + EEH_Inflector::pluralize($model->get_this_model_name()) |
|
281 | + ) |
|
282 | + ); |
|
283 | + } |
|
284 | + } |
|
285 | + } |
|
286 | 286 | |
287 | 287 | |
288 | - /** |
|
289 | - * Returns an array ready to be converted into a JSON response, based solely on the model object |
|
290 | - * |
|
291 | - * @param EE_Base_Class $model_obj |
|
292 | - * @param WP_REST_Request $request |
|
293 | - * @return array ready for a response |
|
294 | - */ |
|
295 | - protected function returnModelObjAsJsonResponse(EE_Base_Class $model_obj, WP_REST_Request $request) |
|
296 | - { |
|
297 | - $model = $model_obj->get_model(); |
|
298 | - // create an array exactly like the wpdb results row, |
|
299 | - // so we can pass it to controllers/model/Read::create_entity_from_wpdb_result() |
|
300 | - $simulated_db_row = array(); |
|
301 | - foreach ($model->field_settings(true) as $field_name => $field_obj) { |
|
302 | - // we need to reconstruct the normal wpdb results, including the db-only fields |
|
303 | - // like a secondary table's primary key. The models expect those (but don't care what value they have) |
|
304 | - if ($field_obj instanceof EE_DB_Only_Field_Base) { |
|
305 | - $raw_value = true; |
|
306 | - } elseif ($field_obj instanceof EE_Datetime_Field) { |
|
307 | - $raw_value = $model_obj->get_DateTime_object($field_name); |
|
308 | - } else { |
|
309 | - $raw_value = $model_obj->get_raw($field_name); |
|
310 | - } |
|
311 | - $simulated_db_row[ $field_obj->get_qualified_column() ] = $field_obj->prepare_for_use_in_db($raw_value); |
|
312 | - } |
|
313 | - $read_controller = LoaderFactory::getLoader()->getNew('EventEspresso\core\libraries\rest_api\controllers\model\Read'); |
|
314 | - $read_controller->setRequestedVersion($this->getRequestedVersion()); |
|
315 | - // the simulates request really doesn't need any info downstream |
|
316 | - $simulated_request = new WP_REST_Request('GET'); |
|
317 | - return $read_controller->createEntityFromWpdbResult( |
|
318 | - $model_obj->get_model(), |
|
319 | - $simulated_db_row, |
|
320 | - $simulated_request |
|
321 | - ); |
|
322 | - } |
|
288 | + /** |
|
289 | + * Returns an array ready to be converted into a JSON response, based solely on the model object |
|
290 | + * |
|
291 | + * @param EE_Base_Class $model_obj |
|
292 | + * @param WP_REST_Request $request |
|
293 | + * @return array ready for a response |
|
294 | + */ |
|
295 | + protected function returnModelObjAsJsonResponse(EE_Base_Class $model_obj, WP_REST_Request $request) |
|
296 | + { |
|
297 | + $model = $model_obj->get_model(); |
|
298 | + // create an array exactly like the wpdb results row, |
|
299 | + // so we can pass it to controllers/model/Read::create_entity_from_wpdb_result() |
|
300 | + $simulated_db_row = array(); |
|
301 | + foreach ($model->field_settings(true) as $field_name => $field_obj) { |
|
302 | + // we need to reconstruct the normal wpdb results, including the db-only fields |
|
303 | + // like a secondary table's primary key. The models expect those (but don't care what value they have) |
|
304 | + if ($field_obj instanceof EE_DB_Only_Field_Base) { |
|
305 | + $raw_value = true; |
|
306 | + } elseif ($field_obj instanceof EE_Datetime_Field) { |
|
307 | + $raw_value = $model_obj->get_DateTime_object($field_name); |
|
308 | + } else { |
|
309 | + $raw_value = $model_obj->get_raw($field_name); |
|
310 | + } |
|
311 | + $simulated_db_row[ $field_obj->get_qualified_column() ] = $field_obj->prepare_for_use_in_db($raw_value); |
|
312 | + } |
|
313 | + $read_controller = LoaderFactory::getLoader()->getNew('EventEspresso\core\libraries\rest_api\controllers\model\Read'); |
|
314 | + $read_controller->setRequestedVersion($this->getRequestedVersion()); |
|
315 | + // the simulates request really doesn't need any info downstream |
|
316 | + $simulated_request = new WP_REST_Request('GET'); |
|
317 | + return $read_controller->createEntityFromWpdbResult( |
|
318 | + $model_obj->get_model(), |
|
319 | + $simulated_db_row, |
|
320 | + $simulated_request |
|
321 | + ); |
|
322 | + } |
|
323 | 323 | |
324 | 324 | |
325 | - /** |
|
326 | - * Gets the item affected by this request |
|
327 | - * |
|
328 | - * @param EEM_Base $model |
|
329 | - * @param WP_REST_Request $request |
|
330 | - * @param int|string $obj_id |
|
331 | - * @return \WP_Error|array |
|
332 | - */ |
|
333 | - protected function getOneBasedOnRequest(EEM_Base $model, WP_REST_Request $request, $obj_id) |
|
334 | - { |
|
335 | - $requested_version = $this->getRequestedVersion($request->get_route()); |
|
336 | - $get_request = new WP_REST_Request( |
|
337 | - 'GET', |
|
338 | - EED_Core_Rest_Api::ee_api_namespace |
|
339 | - . $requested_version |
|
340 | - . '/' |
|
341 | - . EEH_Inflector::pluralize_and_lower($model->get_this_model_name()) |
|
342 | - . '/' |
|
343 | - . $obj_id |
|
344 | - ); |
|
345 | - $get_request->set_url_params( |
|
346 | - array( |
|
347 | - 'id' => $obj_id, |
|
348 | - 'include' => $request->get_param('include'), |
|
349 | - ) |
|
350 | - ); |
|
351 | - $read_controller = new Read(); |
|
352 | - $read_controller->setRequestedVersion($this->getRequestedVersion()); |
|
353 | - return $read_controller->getEntityFromModel($model, $get_request); |
|
354 | - } |
|
325 | + /** |
|
326 | + * Gets the item affected by this request |
|
327 | + * |
|
328 | + * @param EEM_Base $model |
|
329 | + * @param WP_REST_Request $request |
|
330 | + * @param int|string $obj_id |
|
331 | + * @return \WP_Error|array |
|
332 | + */ |
|
333 | + protected function getOneBasedOnRequest(EEM_Base $model, WP_REST_Request $request, $obj_id) |
|
334 | + { |
|
335 | + $requested_version = $this->getRequestedVersion($request->get_route()); |
|
336 | + $get_request = new WP_REST_Request( |
|
337 | + 'GET', |
|
338 | + EED_Core_Rest_Api::ee_api_namespace |
|
339 | + . $requested_version |
|
340 | + . '/' |
|
341 | + . EEH_Inflector::pluralize_and_lower($model->get_this_model_name()) |
|
342 | + . '/' |
|
343 | + . $obj_id |
|
344 | + ); |
|
345 | + $get_request->set_url_params( |
|
346 | + array( |
|
347 | + 'id' => $obj_id, |
|
348 | + 'include' => $request->get_param('include'), |
|
349 | + ) |
|
350 | + ); |
|
351 | + $read_controller = new Read(); |
|
352 | + $read_controller->setRequestedVersion($this->getRequestedVersion()); |
|
353 | + return $read_controller->getEntityFromModel($model, $get_request); |
|
354 | + } |
|
355 | 355 | } |
@@ -126,9 +126,9 @@ discard block |
||
126 | 126 | { |
127 | 127 | Capabilities::verifyAtLeastPartialAccessTo($model, EEM_Base::caps_edit, 'create'); |
128 | 128 | $default_cap_to_check_for = EE_Restriction_Generator_Base::get_default_restrictions_cap(); |
129 | - if (! current_user_can($default_cap_to_check_for)) { |
|
129 | + if ( ! current_user_can($default_cap_to_check_for)) { |
|
130 | 130 | throw new RestException( |
131 | - 'rest_cannot_create_' . EEH_Inflector::pluralize_and_lower(($model->get_this_model_name())), |
|
131 | + 'rest_cannot_create_'.EEH_Inflector::pluralize_and_lower(($model->get_this_model_name())), |
|
132 | 132 | sprintf( |
133 | 133 | esc_html__( |
134 | 134 | // @codingStandardsIgnoreStart |
@@ -156,7 +156,7 @@ discard block |
||
156 | 156 | ); |
157 | 157 | $model_obj->save(); |
158 | 158 | $new_id = $model_obj->ID(); |
159 | - if (! $new_id) { |
|
159 | + if ( ! $new_id) { |
|
160 | 160 | throw new RestException( |
161 | 161 | 'rest_insertion_failed', |
162 | 162 | sprintf(__('Could not insert new %1$s', 'event_espresso'), $model->get_this_model_name()) |
@@ -178,9 +178,9 @@ discard block |
||
178 | 178 | { |
179 | 179 | Capabilities::verifyAtLeastPartialAccessTo($model, EEM_Base::caps_edit, 'edit'); |
180 | 180 | $default_cap_to_check_for = EE_Restriction_Generator_Base::get_default_restrictions_cap(); |
181 | - if (! current_user_can($default_cap_to_check_for)) { |
|
181 | + if ( ! current_user_can($default_cap_to_check_for)) { |
|
182 | 182 | throw new RestException( |
183 | - 'rest_cannot_edit_' . EEH_Inflector::pluralize_and_lower(($model->get_this_model_name())), |
|
183 | + 'rest_cannot_edit_'.EEH_Inflector::pluralize_and_lower(($model->get_this_model_name())), |
|
184 | 184 | sprintf( |
185 | 185 | esc_html__( |
186 | 186 | // @codingStandardsIgnoreStart |
@@ -194,7 +194,7 @@ discard block |
||
194 | 194 | ); |
195 | 195 | } |
196 | 196 | $obj_id = $request->get_param('id'); |
197 | - if (! $obj_id) { |
|
197 | + if ( ! $obj_id) { |
|
198 | 198 | throw new RestException( |
199 | 199 | 'rest_edit_failed', |
200 | 200 | sprintf(__('Could not edit %1$s', 'event_espresso'), $model->get_this_model_name()) |
@@ -207,7 +207,7 @@ discard block |
||
207 | 207 | true |
208 | 208 | ); |
209 | 209 | $model_obj = $model->get_one_by_ID($obj_id); |
210 | - if (! $model_obj instanceof EE_Base_Class) { |
|
210 | + if ( ! $model_obj instanceof EE_Base_Class) { |
|
211 | 211 | $lowercase_model_name = strtolower($model->get_this_model_name()); |
212 | 212 | throw new RestException( |
213 | 213 | sprintf('rest_%s_invalid_id', $lowercase_model_name), |
@@ -232,9 +232,9 @@ discard block |
||
232 | 232 | { |
233 | 233 | Capabilities::verifyAtLeastPartialAccessTo($model, EEM_Base::caps_delete, 'delete'); |
234 | 234 | $default_cap_to_check_for = EE_Restriction_Generator_Base::get_default_restrictions_cap(); |
235 | - if (! current_user_can($default_cap_to_check_for)) { |
|
235 | + if ( ! current_user_can($default_cap_to_check_for)) { |
|
236 | 236 | throw new RestException( |
237 | - 'rest_cannot_delete_' . EEH_Inflector::pluralize_and_lower(($model->get_this_model_name())), |
|
237 | + 'rest_cannot_delete_'.EEH_Inflector::pluralize_and_lower(($model->get_this_model_name())), |
|
238 | 238 | sprintf( |
239 | 239 | esc_html__( |
240 | 240 | // @codingStandardsIgnoreStart |
@@ -250,7 +250,7 @@ discard block |
||
250 | 250 | $obj_id = $request->get_param('id'); |
251 | 251 | // this is where we would apply more fine-grained caps |
252 | 252 | $model_obj = $model->get_one_by_ID($obj_id); |
253 | - if (! $model_obj instanceof EE_Base_Class) { |
|
253 | + if ( ! $model_obj instanceof EE_Base_Class) { |
|
254 | 254 | $lowercase_model_name = strtolower($model->get_this_model_name()); |
255 | 255 | throw new RestException( |
256 | 256 | sprintf('rest_%s_invalid_id', $lowercase_model_name), |
@@ -308,7 +308,7 @@ discard block |
||
308 | 308 | } else { |
309 | 309 | $raw_value = $model_obj->get_raw($field_name); |
310 | 310 | } |
311 | - $simulated_db_row[ $field_obj->get_qualified_column() ] = $field_obj->prepare_for_use_in_db($raw_value); |
|
311 | + $simulated_db_row[$field_obj->get_qualified_column()] = $field_obj->prepare_for_use_in_db($raw_value); |
|
312 | 312 | } |
313 | 313 | $read_controller = LoaderFactory::getLoader()->getNew('EventEspresso\core\libraries\rest_api\controllers\model\Read'); |
314 | 314 | $read_controller->setRequestedVersion($this->getRequestedVersion()); |
@@ -40,1350 +40,1350 @@ |
||
40 | 40 | { |
41 | 41 | |
42 | 42 | |
43 | - /** |
|
44 | - * @var CalculatedModelFields |
|
45 | - */ |
|
46 | - protected $fields_calculator; |
|
47 | - |
|
48 | - |
|
49 | - /** |
|
50 | - * Read constructor. |
|
51 | - * @param CalculatedModelFields $fields_calculator |
|
52 | - */ |
|
53 | - public function __construct(CalculatedModelFields $fields_calculator) |
|
54 | - { |
|
55 | - parent::__construct(); |
|
56 | - $this->fields_calculator = $fields_calculator; |
|
57 | - } |
|
58 | - |
|
59 | - |
|
60 | - /** |
|
61 | - * Handles requests to get all (or a filtered subset) of entities for a particular model |
|
62 | - * |
|
63 | - * @param WP_REST_Request $request |
|
64 | - * @param string $version |
|
65 | - * @param string $model_name |
|
66 | - * @return WP_REST_Response|WP_Error |
|
67 | - * @throws InvalidArgumentException |
|
68 | - * @throws InvalidDataTypeException |
|
69 | - * @throws InvalidInterfaceException |
|
70 | - */ |
|
71 | - public static function handleRequestGetAll(WP_REST_Request $request, $version, $model_name) |
|
72 | - { |
|
73 | - $controller = LoaderFactory::getLoader()->getNew('EventEspresso\core\libraries\rest_api\controllers\model\Read'); |
|
74 | - try { |
|
75 | - $controller->setRequestedVersion($version); |
|
76 | - if (! $controller->getModelVersionInfo()->isModelNameInThisVersion($model_name)) { |
|
77 | - return $controller->sendResponse( |
|
78 | - new WP_Error( |
|
79 | - 'endpoint_parsing_error', |
|
80 | - sprintf( |
|
81 | - __( |
|
82 | - 'There is no model for endpoint %s. Please contact event espresso support', |
|
83 | - 'event_espresso' |
|
84 | - ), |
|
85 | - $model_name |
|
86 | - ) |
|
87 | - ) |
|
88 | - ); |
|
89 | - } |
|
90 | - return $controller->sendResponse( |
|
91 | - $controller->getEntitiesFromModel( |
|
92 | - $controller->getModelVersionInfo()->loadModel($model_name), |
|
93 | - $request |
|
94 | - ) |
|
95 | - ); |
|
96 | - } catch (Exception $e) { |
|
97 | - return $controller->sendResponse($e); |
|
98 | - } |
|
99 | - } |
|
100 | - |
|
101 | - |
|
102 | - /** |
|
103 | - * Prepares and returns schema for any OPTIONS request. |
|
104 | - * |
|
105 | - * @param string $version The API endpoint version being used. |
|
106 | - * @param string $model_name Something like `Event` or `Registration` |
|
107 | - * @return array |
|
108 | - * @throws InvalidArgumentException |
|
109 | - * @throws InvalidDataTypeException |
|
110 | - * @throws InvalidInterfaceException |
|
111 | - */ |
|
112 | - public static function handleSchemaRequest($version, $model_name) |
|
113 | - { |
|
114 | - $controller = LoaderFactory::getLoader()->getNew('EventEspresso\core\libraries\rest_api\controllers\model\Read'); |
|
115 | - try { |
|
116 | - $controller->setRequestedVersion($version); |
|
117 | - if (! $controller->getModelVersionInfo()->isModelNameInThisVersion($model_name)) { |
|
118 | - return array(); |
|
119 | - } |
|
120 | - // get the model for this version |
|
121 | - $model = $controller->getModelVersionInfo()->loadModel($model_name); |
|
122 | - $model_schema = new JsonModelSchema($model, LoaderFactory::getLoader()->getShared('EventEspresso\core\libraries\rest_api\CalculatedModelFields')); |
|
123 | - return $model_schema->getModelSchemaForRelations( |
|
124 | - $controller->getModelVersionInfo()->relationSettings($model), |
|
125 | - $controller->customizeSchemaForRestResponse( |
|
126 | - $model, |
|
127 | - $model_schema->getModelSchemaForFields( |
|
128 | - $controller->getModelVersionInfo()->fieldsOnModelInThisVersion($model), |
|
129 | - $model_schema->getInitialSchemaStructure() |
|
130 | - ) |
|
131 | - ) |
|
132 | - ); |
|
133 | - } catch (Exception $e) { |
|
134 | - return array(); |
|
135 | - } |
|
136 | - } |
|
137 | - |
|
138 | - |
|
139 | - /** |
|
140 | - * This loops through each field in the given schema for the model and does the following: |
|
141 | - * - add any extra fields that are REST API specific and related to existing fields. |
|
142 | - * - transform default values into the correct format for a REST API response. |
|
143 | - * |
|
144 | - * @param EEM_Base $model |
|
145 | - * @param array $schema |
|
146 | - * @return array The final schema. |
|
147 | - */ |
|
148 | - protected function customizeSchemaForRestResponse(EEM_Base $model, array $schema) |
|
149 | - { |
|
150 | - foreach ($this->getModelVersionInfo()->fieldsOnModelInThisVersion($model) as $field_name => $field) { |
|
151 | - $schema = $this->translateDefaultsForRestResponse( |
|
152 | - $field_name, |
|
153 | - $field, |
|
154 | - $this->maybeAddExtraFieldsToSchema($field_name, $field, $schema) |
|
155 | - ); |
|
156 | - } |
|
157 | - return $schema; |
|
158 | - } |
|
159 | - |
|
160 | - |
|
161 | - /** |
|
162 | - * This is used to ensure that the 'default' value set in the schema response is formatted correctly for the REST |
|
163 | - * response. |
|
164 | - * |
|
165 | - * @param $field_name |
|
166 | - * @param EE_Model_Field_Base $field |
|
167 | - * @param array $schema |
|
168 | - * @return array |
|
169 | - * @throws ObjectDetectedException if a default value has a PHP object, which should never do (and if we |
|
170 | - * did, let's know about it ASAP, so let the exception bubble up) |
|
171 | - */ |
|
172 | - protected function translateDefaultsForRestResponse($field_name, EE_Model_Field_Base $field, array $schema) |
|
173 | - { |
|
174 | - if (isset($schema['properties'][ $field_name ]['default'])) { |
|
175 | - if (is_array($schema['properties'][ $field_name ]['default'])) { |
|
176 | - foreach ($schema['properties'][ $field_name ]['default'] as $default_key => $default_value) { |
|
177 | - if ($default_key === 'raw') { |
|
178 | - $schema['properties'][ $field_name ]['default'][ $default_key ] = |
|
179 | - ModelDataTranslator::prepareFieldValueForJson( |
|
180 | - $field, |
|
181 | - $default_value, |
|
182 | - $this->getModelVersionInfo()->requestedVersion() |
|
183 | - ); |
|
184 | - } |
|
185 | - } |
|
186 | - } else { |
|
187 | - $schema['properties'][ $field_name ]['default'] = ModelDataTranslator::prepareFieldValueForJson( |
|
188 | - $field, |
|
189 | - $schema['properties'][ $field_name ]['default'], |
|
190 | - $this->getModelVersionInfo()->requestedVersion() |
|
191 | - ); |
|
192 | - } |
|
193 | - } |
|
194 | - return $schema; |
|
195 | - } |
|
196 | - |
|
197 | - |
|
198 | - /** |
|
199 | - * Adds additional fields to the schema |
|
200 | - * The REST API returns a GMT value field for each datetime field in the resource. Thus the description about this |
|
201 | - * needs to be added to the schema. |
|
202 | - * |
|
203 | - * @param $field_name |
|
204 | - * @param EE_Model_Field_Base $field |
|
205 | - * @param array $schema |
|
206 | - * @return array |
|
207 | - */ |
|
208 | - protected function maybeAddExtraFieldsToSchema($field_name, EE_Model_Field_Base $field, array $schema) |
|
209 | - { |
|
210 | - if ($field instanceof EE_Datetime_Field) { |
|
211 | - $schema['properties'][ $field_name . '_gmt' ] = $field->getSchema(); |
|
212 | - // modify the description |
|
213 | - $schema['properties'][ $field_name . '_gmt' ]['description'] = sprintf( |
|
214 | - esc_html__('%s - the value for this field is in GMT.', 'event_espresso'), |
|
215 | - wp_specialchars_decode($field->get_nicename(), ENT_QUOTES) |
|
216 | - ); |
|
217 | - } |
|
218 | - return $schema; |
|
219 | - } |
|
220 | - |
|
221 | - |
|
222 | - /** |
|
223 | - * Used to figure out the route from the request when a `WP_REST_Request` object is not available |
|
224 | - * |
|
225 | - * @return string |
|
226 | - */ |
|
227 | - protected function getRouteFromRequest() |
|
228 | - { |
|
229 | - if (isset($GLOBALS['wp']) |
|
230 | - && $GLOBALS['wp'] instanceof \WP |
|
231 | - && isset($GLOBALS['wp']->query_vars['rest_route']) |
|
232 | - ) { |
|
233 | - return $GLOBALS['wp']->query_vars['rest_route']; |
|
234 | - } else { |
|
235 | - return isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : '/'; |
|
236 | - } |
|
237 | - } |
|
238 | - |
|
239 | - |
|
240 | - /** |
|
241 | - * Gets a single entity related to the model indicated in the path and its id |
|
242 | - * |
|
243 | - * @param WP_REST_Request $request |
|
244 | - * @param string $version |
|
245 | - * @param string $model_name |
|
246 | - * @return WP_REST_Response|WP_Error |
|
247 | - * @throws InvalidDataTypeException |
|
248 | - * @throws InvalidInterfaceException |
|
249 | - * @throws InvalidArgumentException |
|
250 | - */ |
|
251 | - public static function handleRequestGetOne(WP_REST_Request $request, $version, $model_name) |
|
252 | - { |
|
253 | - $controller = LoaderFactory::getLoader()->getNew('EventEspresso\core\libraries\rest_api\controllers\model\Read'); |
|
254 | - try { |
|
255 | - $controller->setRequestedVersion($version); |
|
256 | - if (! $controller->getModelVersionInfo()->isModelNameInThisVersion($model_name)) { |
|
257 | - return $controller->sendResponse( |
|
258 | - new WP_Error( |
|
259 | - 'endpoint_parsing_error', |
|
260 | - sprintf( |
|
261 | - __( |
|
262 | - 'There is no model for endpoint %s. Please contact event espresso support', |
|
263 | - 'event_espresso' |
|
264 | - ), |
|
265 | - $model_name |
|
266 | - ) |
|
267 | - ) |
|
268 | - ); |
|
269 | - } |
|
270 | - return $controller->sendResponse( |
|
271 | - $controller->getEntityFromModel( |
|
272 | - $controller->getModelVersionInfo()->loadModel($model_name), |
|
273 | - $request |
|
274 | - ) |
|
275 | - ); |
|
276 | - } catch (Exception $e) { |
|
277 | - return $controller->sendResponse($e); |
|
278 | - } |
|
279 | - } |
|
280 | - |
|
281 | - |
|
282 | - /** |
|
283 | - * Gets all the related entities (or if its a belongs-to relation just the one) |
|
284 | - * to the item with the given id |
|
285 | - * |
|
286 | - * @param WP_REST_Request $request |
|
287 | - * @param string $version |
|
288 | - * @param string $model_name |
|
289 | - * @param string $related_model_name |
|
290 | - * @return WP_REST_Response|WP_Error |
|
291 | - * @throws InvalidDataTypeException |
|
292 | - * @throws InvalidInterfaceException |
|
293 | - * @throws InvalidArgumentException |
|
294 | - */ |
|
295 | - public static function handleRequestGetRelated( |
|
296 | - WP_REST_Request $request, |
|
297 | - $version, |
|
298 | - $model_name, |
|
299 | - $related_model_name |
|
300 | - ) { |
|
301 | - $controller = LoaderFactory::getLoader()->getNew('EventEspresso\core\libraries\rest_api\controllers\model\Read'); |
|
302 | - try { |
|
303 | - $controller->setRequestedVersion($version); |
|
304 | - if (! $controller->getModelVersionInfo()->isModelNameInThisVersion($model_name)) { |
|
305 | - return $controller->sendResponse( |
|
306 | - new WP_Error( |
|
307 | - 'endpoint_parsing_error', |
|
308 | - sprintf( |
|
309 | - __( |
|
310 | - 'There is no model for endpoint %s. Please contact event espresso support', |
|
311 | - 'event_espresso' |
|
312 | - ), |
|
313 | - $model_name |
|
314 | - ) |
|
315 | - ) |
|
316 | - ); |
|
317 | - } |
|
318 | - $main_model = $controller->getModelVersionInfo()->loadModel($model_name); |
|
319 | - if (! $controller->getModelVersionInfo()->isModelNameInThisVersion($related_model_name)) { |
|
320 | - return $controller->sendResponse( |
|
321 | - new WP_Error( |
|
322 | - 'endpoint_parsing_error', |
|
323 | - sprintf( |
|
324 | - __( |
|
325 | - 'There is no model for endpoint %s. Please contact event espresso support', |
|
326 | - 'event_espresso' |
|
327 | - ), |
|
328 | - $related_model_name |
|
329 | - ) |
|
330 | - ) |
|
331 | - ); |
|
332 | - } |
|
333 | - return $controller->sendResponse( |
|
334 | - $controller->getEntitiesFromRelation( |
|
335 | - $request->get_param('id'), |
|
336 | - $main_model->related_settings_for($related_model_name), |
|
337 | - $request |
|
338 | - ) |
|
339 | - ); |
|
340 | - } catch (Exception $e) { |
|
341 | - return $controller->sendResponse($e); |
|
342 | - } |
|
343 | - } |
|
344 | - |
|
345 | - |
|
346 | - /** |
|
347 | - * Gets a collection for the given model and filters |
|
348 | - * |
|
349 | - * @param EEM_Base $model |
|
350 | - * @param WP_REST_Request $request |
|
351 | - * @return array|WP_Error |
|
352 | - */ |
|
353 | - public function getEntitiesFromModel($model, $request) |
|
354 | - { |
|
355 | - $query_params = $this->createModelQueryParams($model, $request->get_params()); |
|
356 | - if (! Capabilities::currentUserHasPartialAccessTo($model, $query_params['caps'])) { |
|
357 | - $model_name_plural = EEH_Inflector::pluralize_and_lower($model->get_this_model_name()); |
|
358 | - return new WP_Error( |
|
359 | - sprintf('rest_%s_cannot_list', $model_name_plural), |
|
360 | - sprintf( |
|
361 | - __('Sorry, you are not allowed to list %1$s. Missing permissions: %2$s', 'event_espresso'), |
|
362 | - $model_name_plural, |
|
363 | - Capabilities::getMissingPermissionsString($model, $query_params['caps']) |
|
364 | - ), |
|
365 | - array('status' => 403) |
|
366 | - ); |
|
367 | - } |
|
368 | - if (! $request->get_header('no_rest_headers')) { |
|
369 | - $this->setHeadersFromQueryParams($model, $query_params); |
|
370 | - } |
|
371 | - /** @type array $results */ |
|
372 | - $results = $model->get_all_wpdb_results($query_params); |
|
373 | - $nice_results = array(); |
|
374 | - foreach ($results as $result) { |
|
375 | - $nice_results[] = $this->createEntityFromWpdbResult( |
|
376 | - $model, |
|
377 | - $result, |
|
378 | - $request |
|
379 | - ); |
|
380 | - } |
|
381 | - return $nice_results; |
|
382 | - } |
|
383 | - |
|
384 | - |
|
385 | - /** |
|
386 | - * Gets the collection for given relation object |
|
387 | - * The same as Read::get_entities_from_model(), except if the relation |
|
388 | - * is a HABTM relation, in which case it merges any non-foreign-key fields from |
|
389 | - * the join-model-object into the results |
|
390 | - * |
|
391 | - * @param array $primary_model_query_params query params for finding the item from which |
|
392 | - * relations will be based |
|
393 | - * @param \EE_Model_Relation_Base $relation |
|
394 | - * @param WP_REST_Request $request |
|
395 | - * @return WP_Error|array |
|
396 | - * @throws RestException |
|
397 | - */ |
|
398 | - protected function getEntitiesFromRelationUsingModelQueryParams($primary_model_query_params, $relation, $request) |
|
399 | - { |
|
400 | - $context = $this->validateContext($request->get_param('caps')); |
|
401 | - $model = $relation->get_this_model(); |
|
402 | - $related_model = $relation->get_other_model(); |
|
403 | - if (! isset($primary_model_query_params[0])) { |
|
404 | - $primary_model_query_params[0] = array(); |
|
405 | - } |
|
406 | - // check if they can access the 1st model object |
|
407 | - $primary_model_query_params = array( |
|
408 | - 0 => $primary_model_query_params[0], |
|
409 | - 'limit' => 1, |
|
410 | - ); |
|
411 | - if ($model instanceof \EEM_Soft_Delete_Base) { |
|
412 | - $primary_model_query_params = $model->alter_query_params_so_deleted_and_undeleted_items_included( |
|
413 | - $primary_model_query_params |
|
414 | - ); |
|
415 | - } |
|
416 | - $restricted_query_params = $primary_model_query_params; |
|
417 | - $restricted_query_params['caps'] = $context; |
|
418 | - $this->setDebugInfo('main model query params', $restricted_query_params); |
|
419 | - $this->setDebugInfo('missing caps', Capabilities::getMissingPermissionsString($related_model, $context)); |
|
420 | - if (! ( |
|
421 | - Capabilities::currentUserHasPartialAccessTo($related_model, $context) |
|
422 | - && $model->exists($restricted_query_params) |
|
423 | - ) |
|
424 | - ) { |
|
425 | - if ($relation instanceof EE_Belongs_To_Relation) { |
|
426 | - $related_model_name_maybe_plural = strtolower($related_model->get_this_model_name()); |
|
427 | - } else { |
|
428 | - $related_model_name_maybe_plural = EEH_Inflector::pluralize_and_lower( |
|
429 | - $related_model->get_this_model_name() |
|
430 | - ); |
|
431 | - } |
|
432 | - return new WP_Error( |
|
433 | - sprintf('rest_%s_cannot_list', $related_model_name_maybe_plural), |
|
434 | - sprintf( |
|
435 | - __( |
|
436 | - 'Sorry, you are not allowed to list %1$s related to %2$s. Missing permissions: %3$s', |
|
437 | - 'event_espresso' |
|
438 | - ), |
|
439 | - $related_model_name_maybe_plural, |
|
440 | - $relation->get_this_model()->get_this_model_name(), |
|
441 | - implode( |
|
442 | - ',', |
|
443 | - array_keys( |
|
444 | - Capabilities::getMissingPermissions($related_model, $context) |
|
445 | - ) |
|
446 | - ) |
|
447 | - ), |
|
448 | - array('status' => 403) |
|
449 | - ); |
|
450 | - } |
|
451 | - $query_params = $this->createModelQueryParams($relation->get_other_model(), $request->get_params()); |
|
452 | - foreach ($primary_model_query_params[0] as $where_condition_key => $where_condition_value) { |
|
453 | - $query_params[0][ $relation->get_this_model()->get_this_model_name() |
|
454 | - . '.' |
|
455 | - . $where_condition_key ] = $where_condition_value; |
|
456 | - } |
|
457 | - $query_params['default_where_conditions'] = 'none'; |
|
458 | - $query_params['caps'] = $context; |
|
459 | - if (! $request->get_header('no_rest_headers')) { |
|
460 | - $this->setHeadersFromQueryParams($relation->get_other_model(), $query_params); |
|
461 | - } |
|
462 | - /** @type array $results */ |
|
463 | - $results = $relation->get_other_model()->get_all_wpdb_results($query_params); |
|
464 | - $nice_results = array(); |
|
465 | - foreach ($results as $result) { |
|
466 | - $nice_result = $this->createEntityFromWpdbResult( |
|
467 | - $relation->get_other_model(), |
|
468 | - $result, |
|
469 | - $request |
|
470 | - ); |
|
471 | - if ($relation instanceof \EE_HABTM_Relation) { |
|
472 | - // put the unusual stuff (properties from the HABTM relation) first, and make sure |
|
473 | - // if there are conflicts we prefer the properties from the main model |
|
474 | - $join_model_result = $this->createEntityFromWpdbResult( |
|
475 | - $relation->get_join_model(), |
|
476 | - $result, |
|
477 | - $request |
|
478 | - ); |
|
479 | - $joined_result = array_merge($nice_result, $join_model_result); |
|
480 | - // but keep the meta stuff from the main model |
|
481 | - if (isset($nice_result['meta'])) { |
|
482 | - $joined_result['meta'] = $nice_result['meta']; |
|
483 | - } |
|
484 | - $nice_result = $joined_result; |
|
485 | - } |
|
486 | - $nice_results[] = $nice_result; |
|
487 | - } |
|
488 | - if ($relation instanceof EE_Belongs_To_Relation) { |
|
489 | - return array_shift($nice_results); |
|
490 | - } else { |
|
491 | - return $nice_results; |
|
492 | - } |
|
493 | - } |
|
494 | - |
|
495 | - |
|
496 | - /** |
|
497 | - * Gets the collection for given relation object |
|
498 | - * The same as Read::get_entities_from_model(), except if the relation |
|
499 | - * is a HABTM relation, in which case it merges any non-foreign-key fields from |
|
500 | - * the join-model-object into the results |
|
501 | - * |
|
502 | - * @param string $id the ID of the thing we are fetching related stuff from |
|
503 | - * @param \EE_Model_Relation_Base $relation |
|
504 | - * @param WP_REST_Request $request |
|
505 | - * @return array|WP_Error |
|
506 | - * @throws EE_Error |
|
507 | - */ |
|
508 | - public function getEntitiesFromRelation($id, $relation, $request) |
|
509 | - { |
|
510 | - if (! $relation->get_this_model()->has_primary_key_field()) { |
|
511 | - throw new EE_Error( |
|
512 | - sprintf( |
|
513 | - __( |
|
514 | - // @codingStandardsIgnoreStart |
|
515 | - 'Read::get_entities_from_relation should only be called from a model with a primary key, it was called from %1$s', |
|
516 | - // @codingStandardsIgnoreEnd |
|
517 | - 'event_espresso' |
|
518 | - ), |
|
519 | - $relation->get_this_model()->get_this_model_name() |
|
520 | - ) |
|
521 | - ); |
|
522 | - } |
|
523 | - return $this->getEntitiesFromRelationUsingModelQueryParams( |
|
524 | - array( |
|
525 | - array( |
|
526 | - $relation->get_this_model()->primary_key_name() => $id, |
|
527 | - ), |
|
528 | - ), |
|
529 | - $relation, |
|
530 | - $request |
|
531 | - ); |
|
532 | - } |
|
533 | - |
|
534 | - |
|
535 | - /** |
|
536 | - * Sets the headers that are based on the model and query params, |
|
537 | - * like the total records. This should only be called on the original request |
|
538 | - * from the client, not on subsequent internal |
|
539 | - * |
|
540 | - * @param EEM_Base $model |
|
541 | - * @param array $query_params |
|
542 | - * @return void |
|
543 | - */ |
|
544 | - protected function setHeadersFromQueryParams($model, $query_params) |
|
545 | - { |
|
546 | - $this->setDebugInfo('model query params', $query_params); |
|
547 | - $this->setDebugInfo( |
|
548 | - 'missing caps', |
|
549 | - Capabilities::getMissingPermissionsString($model, $query_params['caps']) |
|
550 | - ); |
|
551 | - // normally the limit to a 2-part array, where the 2nd item is the limit |
|
552 | - if (! isset($query_params['limit'])) { |
|
553 | - $query_params['limit'] = EED_Core_Rest_Api::get_default_query_limit(); |
|
554 | - } |
|
555 | - if (is_array($query_params['limit'])) { |
|
556 | - $limit_parts = $query_params['limit']; |
|
557 | - } else { |
|
558 | - $limit_parts = explode(',', $query_params['limit']); |
|
559 | - if (count($limit_parts) == 1) { |
|
560 | - $limit_parts = array(0, $limit_parts[0]); |
|
561 | - } |
|
562 | - } |
|
563 | - // remove the group by and having parts of the query, as those will |
|
564 | - // make the sql query return an array of values, instead of just a single value |
|
565 | - unset($query_params['group_by'], $query_params['having'], $query_params['limit']); |
|
566 | - $count = $model->count($query_params, null, true); |
|
567 | - $pages = $count / $limit_parts[1]; |
|
568 | - $this->setResponseHeader('Total', $count, false); |
|
569 | - $this->setResponseHeader('PageSize', $limit_parts[1], false); |
|
570 | - $this->setResponseHeader('TotalPages', ceil($pages), false); |
|
571 | - } |
|
572 | - |
|
573 | - |
|
574 | - /** |
|
575 | - * Changes database results into REST API entities |
|
576 | - * |
|
577 | - * @param EEM_Base $model |
|
578 | - * @param array $db_row like results from $wpdb->get_results() |
|
579 | - * @param WP_REST_Request $rest_request |
|
580 | - * @param string $deprecated no longer used |
|
581 | - * @return array ready for being converted into json for sending to client |
|
582 | - */ |
|
583 | - public function createEntityFromWpdbResult($model, $db_row, $rest_request, $deprecated = null) |
|
584 | - { |
|
585 | - if (! $rest_request instanceof WP_REST_Request) { |
|
586 | - // ok so this was called in the old style, where the 3rd arg was |
|
587 | - // $include, and the 4th arg was $context |
|
588 | - // now setup the request just to avoid fatal errors, although we won't be able |
|
589 | - // to truly make use of it because it's kinda devoid of info |
|
590 | - $rest_request = new WP_REST_Request(); |
|
591 | - $rest_request->set_param('include', $rest_request); |
|
592 | - $rest_request->set_param('caps', $deprecated); |
|
593 | - } |
|
594 | - if ($rest_request->get_param('caps') == null) { |
|
595 | - $rest_request->set_param('caps', EEM_Base::caps_read); |
|
596 | - } |
|
597 | - $entity_array = $this->createBareEntityFromWpdbResults($model, $db_row); |
|
598 | - $entity_array = $this->addExtraFields($model, $db_row, $entity_array); |
|
599 | - $entity_array['_links'] = $this->getEntityLinks($model, $db_row, $entity_array); |
|
600 | - $entity_array['_calculated_fields'] = $this->getEntityCalculations($model, $db_row, $rest_request); |
|
601 | - $entity_array = apply_filters( |
|
602 | - 'FHEE__Read__create_entity_from_wpdb_results__entity_before_including_requested_models', |
|
603 | - $entity_array, |
|
604 | - $model, |
|
605 | - $rest_request->get_param('caps'), |
|
606 | - $rest_request, |
|
607 | - $this |
|
608 | - ); |
|
609 | - $entity_array = $this->includeRequestedModels($model, $rest_request, $entity_array, $db_row); |
|
610 | - $entity_array = apply_filters( |
|
611 | - 'FHEE__Read__create_entity_from_wpdb_results__entity_before_inaccessible_field_removal', |
|
612 | - $entity_array, |
|
613 | - $model, |
|
614 | - $rest_request->get_param('caps'), |
|
615 | - $rest_request, |
|
616 | - $this |
|
617 | - ); |
|
618 | - $result_without_inaccessible_fields = Capabilities::filterOutInaccessibleEntityFields( |
|
619 | - $entity_array, |
|
620 | - $model, |
|
621 | - $rest_request->get_param('caps'), |
|
622 | - $this->getModelVersionInfo(), |
|
623 | - $model->get_index_primary_key_string( |
|
624 | - $model->deduce_fields_n_values_from_cols_n_values($db_row) |
|
625 | - ) |
|
626 | - ); |
|
627 | - $this->setDebugInfo( |
|
628 | - 'inaccessible fields', |
|
629 | - array_keys(array_diff_key($entity_array, $result_without_inaccessible_fields)) |
|
630 | - ); |
|
631 | - return apply_filters( |
|
632 | - 'FHEE__Read__create_entity_from_wpdb_results__entity_return', |
|
633 | - $result_without_inaccessible_fields, |
|
634 | - $model, |
|
635 | - $rest_request->get_param('caps') |
|
636 | - ); |
|
637 | - } |
|
638 | - |
|
639 | - |
|
640 | - /** |
|
641 | - * Creates a REST entity array (JSON object we're going to return in the response, but |
|
642 | - * for now still a PHP array, but soon enough we'll call json_encode on it, don't worry), |
|
643 | - * from $wpdb->get_row( $sql, ARRAY_A) |
|
644 | - * |
|
645 | - * @param EEM_Base $model |
|
646 | - * @param array $db_row |
|
647 | - * @return array entity mostly ready for converting to JSON and sending in the response |
|
648 | - */ |
|
649 | - protected function createBareEntityFromWpdbResults(EEM_Base $model, $db_row) |
|
650 | - { |
|
651 | - $result = $model->deduce_fields_n_values_from_cols_n_values($db_row); |
|
652 | - $result = array_intersect_key( |
|
653 | - $result, |
|
654 | - $this->getModelVersionInfo()->fieldsOnModelInThisVersion($model) |
|
655 | - ); |
|
656 | - // if this is a CPT, we need to set the global $post to it, |
|
657 | - // otherwise shortcodes etc won't work properly while rendering it |
|
658 | - if ($model instanceof \EEM_CPT_Base) { |
|
659 | - $do_chevy_shuffle = true; |
|
660 | - } else { |
|
661 | - $do_chevy_shuffle = false; |
|
662 | - } |
|
663 | - if ($do_chevy_shuffle) { |
|
664 | - global $post; |
|
665 | - $old_post = $post; |
|
666 | - $post = get_post($result[ $model->primary_key_name() ]); |
|
667 | - if (! $post instanceof \WP_Post) { |
|
668 | - // well that's weird, because $result is what we JUST fetched from the database |
|
669 | - throw new RestException( |
|
670 | - 'error_fetching_post_from_database_results', |
|
671 | - esc_html__( |
|
672 | - 'An item was retrieved from the database but it\'s not a WP_Post like it should be.', |
|
673 | - 'event_espresso' |
|
674 | - ) |
|
675 | - ); |
|
676 | - } |
|
677 | - $model_object_classname = 'EE_' . $model->get_this_model_name(); |
|
678 | - $post->{$model_object_classname} = \EE_Registry::instance()->load_class( |
|
679 | - $model_object_classname, |
|
680 | - $result, |
|
681 | - false, |
|
682 | - false |
|
683 | - ); |
|
684 | - } |
|
685 | - foreach ($result as $field_name => $field_value) { |
|
686 | - $field_obj = $model->field_settings_for($field_name); |
|
687 | - if ($this->isSubclassOfOne($field_obj, $this->getModelVersionInfo()->fieldsIgnored())) { |
|
688 | - unset($result[ $field_name ]); |
|
689 | - } elseif ($this->isSubclassOfOne( |
|
690 | - $field_obj, |
|
691 | - $this->getModelVersionInfo()->fieldsThatHaveRenderedFormat() |
|
692 | - ) |
|
693 | - ) { |
|
694 | - $result[ $field_name ] = array( |
|
695 | - 'raw' => $this->prepareFieldObjValueForJson($field_obj, $field_value), |
|
696 | - 'rendered' => $this->prepareFieldObjValueForJson($field_obj, $field_value, 'pretty'), |
|
697 | - ); |
|
698 | - } elseif ($this->isSubclassOfOne( |
|
699 | - $field_obj, |
|
700 | - $this->getModelVersionInfo()->fieldsThatHavePrettyFormat() |
|
701 | - ) |
|
702 | - ) { |
|
703 | - $result[ $field_name ] = array( |
|
704 | - 'raw' => $this->prepareFieldObjValueForJson($field_obj, $field_value), |
|
705 | - 'pretty' => $this->prepareFieldObjValueForJson($field_obj, $field_value, 'pretty'), |
|
706 | - ); |
|
707 | - } elseif ($field_obj instanceof \EE_Datetime_Field) { |
|
708 | - $field_value = $field_obj->prepare_for_set_from_db($field_value); |
|
709 | - // if the value is null, but we're not supposed to permit null, then set to the field's default |
|
710 | - if (is_null($field_value)) { |
|
711 | - $field_value = $field_obj->getDefaultDateTimeObj(); |
|
712 | - } |
|
713 | - if (is_null($field_value)) { |
|
714 | - $gmt_date = $local_date = ModelDataTranslator::prepareFieldValuesForJson( |
|
715 | - $field_obj, |
|
716 | - $field_value, |
|
717 | - $this->getModelVersionInfo()->requestedVersion() |
|
718 | - ); |
|
719 | - } else { |
|
720 | - $timezone = $field_value->getTimezone(); |
|
721 | - EEH_DTT_Helper::setTimezone($field_value, new DateTimeZone('UTC')); |
|
722 | - $gmt_date = ModelDataTranslator::prepareFieldValuesForJson( |
|
723 | - $field_obj, |
|
724 | - $field_value, |
|
725 | - $this->getModelVersionInfo()->requestedVersion() |
|
726 | - ); |
|
727 | - EEH_DTT_Helper::setTimezone($field_value, $timezone); |
|
728 | - $local_date = ModelDataTranslator::prepareFieldValuesForJson( |
|
729 | - $field_obj, |
|
730 | - $field_value, |
|
731 | - $this->getModelVersionInfo()->requestedVersion() |
|
732 | - ); |
|
733 | - } |
|
734 | - $result[ $field_name . '_gmt' ] = $gmt_date; |
|
735 | - $result[ $field_name ] = $local_date; |
|
736 | - } else { |
|
737 | - $result[ $field_name ] = $this->prepareFieldObjValueForJson($field_obj, $field_value); |
|
738 | - } |
|
739 | - } |
|
740 | - if ($do_chevy_shuffle) { |
|
741 | - $post = $old_post; |
|
742 | - } |
|
743 | - return $result; |
|
744 | - } |
|
745 | - |
|
746 | - |
|
747 | - /** |
|
748 | - * Takes a value all the way from the DB representation, to the model object's representation, to the |
|
749 | - * user-facing PHP representation, to the REST API representation. (Assumes you've already taken from the DB |
|
750 | - * representation using $field_obj->prepare_for_set_from_db()) |
|
751 | - * |
|
752 | - * @param EE_Model_Field_Base $field_obj |
|
753 | - * @param mixed $value as it's stored on a model object |
|
754 | - * @param string $format valid values are 'normal' (default), 'pretty', 'datetime_obj' |
|
755 | - * @return mixed |
|
756 | - * @throws ObjectDetectedException if $value contains a PHP object |
|
757 | - */ |
|
758 | - protected function prepareFieldObjValueForJson(EE_Model_Field_Base $field_obj, $value, $format = 'normal') |
|
759 | - { |
|
760 | - $value = $field_obj->prepare_for_set_from_db($value); |
|
761 | - switch ($format) { |
|
762 | - case 'pretty': |
|
763 | - $value = $field_obj->prepare_for_pretty_echoing($value); |
|
764 | - break; |
|
765 | - case 'normal': |
|
766 | - default: |
|
767 | - $value = $field_obj->prepare_for_get($value); |
|
768 | - break; |
|
769 | - } |
|
770 | - return ModelDataTranslator::prepareFieldValuesForJson( |
|
771 | - $field_obj, |
|
772 | - $value, |
|
773 | - $this->getModelVersionInfo()->requestedVersion() |
|
774 | - ); |
|
775 | - } |
|
776 | - |
|
777 | - |
|
778 | - /** |
|
779 | - * Adds a few extra fields to the entity response |
|
780 | - * |
|
781 | - * @param EEM_Base $model |
|
782 | - * @param array $db_row |
|
783 | - * @param array $entity_array |
|
784 | - * @return array modified entity |
|
785 | - */ |
|
786 | - protected function addExtraFields(EEM_Base $model, $db_row, $entity_array) |
|
787 | - { |
|
788 | - if ($model instanceof EEM_CPT_Base) { |
|
789 | - $entity_array['link'] = get_permalink($db_row[ $model->get_primary_key_field()->get_qualified_column() ]); |
|
790 | - } |
|
791 | - return $entity_array; |
|
792 | - } |
|
793 | - |
|
794 | - |
|
795 | - /** |
|
796 | - * Gets links we want to add to the response |
|
797 | - * |
|
798 | - * @global \WP_REST_Server $wp_rest_server |
|
799 | - * @param EEM_Base $model |
|
800 | - * @param array $db_row |
|
801 | - * @param array $entity_array |
|
802 | - * @return array the _links item in the entity |
|
803 | - */ |
|
804 | - protected function getEntityLinks($model, $db_row, $entity_array) |
|
805 | - { |
|
806 | - // add basic links |
|
807 | - $links = array(); |
|
808 | - if ($model->has_primary_key_field()) { |
|
809 | - $links['self'] = array( |
|
810 | - array( |
|
811 | - 'href' => $this->getVersionedLinkTo( |
|
812 | - EEH_Inflector::pluralize_and_lower($model->get_this_model_name()) |
|
813 | - . '/' |
|
814 | - . $entity_array[ $model->primary_key_name() ] |
|
815 | - ), |
|
816 | - ), |
|
817 | - ); |
|
818 | - } |
|
819 | - $links['collection'] = array( |
|
820 | - array( |
|
821 | - 'href' => $this->getVersionedLinkTo( |
|
822 | - EEH_Inflector::pluralize_and_lower($model->get_this_model_name()) |
|
823 | - ), |
|
824 | - ), |
|
825 | - ); |
|
826 | - // add links to related models |
|
827 | - if ($model->has_primary_key_field()) { |
|
828 | - foreach ($this->getModelVersionInfo()->relationSettings($model) as $relation_name => $relation_obj) { |
|
829 | - $related_model_part = Read::getRelatedEntityName($relation_name, $relation_obj); |
|
830 | - $links[ EED_Core_Rest_Api::ee_api_link_namespace . $related_model_part ] = array( |
|
831 | - array( |
|
832 | - 'href' => $this->getVersionedLinkTo( |
|
833 | - EEH_Inflector::pluralize_and_lower($model->get_this_model_name()) |
|
834 | - . '/' |
|
835 | - . $entity_array[ $model->primary_key_name() ] |
|
836 | - . '/' |
|
837 | - . $related_model_part |
|
838 | - ), |
|
839 | - 'single' => $relation_obj instanceof EE_Belongs_To_Relation ? true : false, |
|
840 | - ), |
|
841 | - ); |
|
842 | - } |
|
843 | - } |
|
844 | - return $links; |
|
845 | - } |
|
846 | - |
|
847 | - |
|
848 | - /** |
|
849 | - * Adds the included models indicated in the request to the entity provided |
|
850 | - * |
|
851 | - * @param EEM_Base $model |
|
852 | - * @param WP_REST_Request $rest_request |
|
853 | - * @param array $entity_array |
|
854 | - * @param array $db_row |
|
855 | - * @return array the modified entity |
|
856 | - */ |
|
857 | - protected function includeRequestedModels( |
|
858 | - EEM_Base $model, |
|
859 | - WP_REST_Request $rest_request, |
|
860 | - $entity_array, |
|
861 | - $db_row = array() |
|
862 | - ) { |
|
863 | - // if $db_row not included, hope the entity array has what we need |
|
864 | - if (! $db_row) { |
|
865 | - $db_row = $entity_array; |
|
866 | - } |
|
867 | - $includes_for_this_model = $this->explodeAndGetItemsPrefixedWith($rest_request->get_param('include'), ''); |
|
868 | - $includes_for_this_model = $this->removeModelNamesFromArray($includes_for_this_model); |
|
869 | - // if they passed in * or didn't specify any includes, return everything |
|
870 | - if (! in_array('*', $includes_for_this_model) |
|
871 | - && ! empty($includes_for_this_model) |
|
872 | - ) { |
|
873 | - if ($model->has_primary_key_field()) { |
|
874 | - // always include the primary key. ya just gotta know that at least |
|
875 | - $includes_for_this_model[] = $model->primary_key_name(); |
|
876 | - } |
|
877 | - if ($this->explodeAndGetItemsPrefixedWith($rest_request->get_param('calculate'), '')) { |
|
878 | - $includes_for_this_model[] = '_calculated_fields'; |
|
879 | - } |
|
880 | - $entity_array = array_intersect_key($entity_array, array_flip($includes_for_this_model)); |
|
881 | - } |
|
882 | - $relation_settings = $this->getModelVersionInfo()->relationSettings($model); |
|
883 | - foreach ($relation_settings as $relation_name => $relation_obj) { |
|
884 | - $related_fields_to_include = $this->explodeAndGetItemsPrefixedWith( |
|
885 | - $rest_request->get_param('include'), |
|
886 | - $relation_name |
|
887 | - ); |
|
888 | - $related_fields_to_calculate = $this->explodeAndGetItemsPrefixedWith( |
|
889 | - $rest_request->get_param('calculate'), |
|
890 | - $relation_name |
|
891 | - ); |
|
892 | - // did they specify they wanted to include a related model, or |
|
893 | - // specific fields from a related model? |
|
894 | - // or did they specify to calculate a field from a related model? |
|
895 | - if ($related_fields_to_include || $related_fields_to_calculate) { |
|
896 | - // if so, we should include at least some part of the related model |
|
897 | - $pretend_related_request = new WP_REST_Request(); |
|
898 | - $pretend_related_request->set_query_params( |
|
899 | - array( |
|
900 | - 'caps' => $rest_request->get_param('caps'), |
|
901 | - 'include' => $related_fields_to_include, |
|
902 | - 'calculate' => $related_fields_to_calculate, |
|
903 | - ) |
|
904 | - ); |
|
905 | - $pretend_related_request->add_header('no_rest_headers', true); |
|
906 | - $primary_model_query_params = $model->alter_query_params_to_restrict_by_ID( |
|
907 | - $model->get_index_primary_key_string( |
|
908 | - $model->deduce_fields_n_values_from_cols_n_values($db_row) |
|
909 | - ) |
|
910 | - ); |
|
911 | - $related_results = $this->getEntitiesFromRelationUsingModelQueryParams( |
|
912 | - $primary_model_query_params, |
|
913 | - $relation_obj, |
|
914 | - $pretend_related_request |
|
915 | - ); |
|
916 | - $entity_array[ Read::getRelatedEntityName($relation_name, $relation_obj) ] = $related_results |
|
917 | - instanceof |
|
918 | - WP_Error |
|
919 | - ? null |
|
920 | - : $related_results; |
|
921 | - } |
|
922 | - } |
|
923 | - return $entity_array; |
|
924 | - } |
|
925 | - |
|
926 | - |
|
927 | - /** |
|
928 | - * Returns a new array with all the names of models removed. Eg |
|
929 | - * array( 'Event', 'Datetime.*', 'foobar' ) would become array( 'Datetime.*', 'foobar' ) |
|
930 | - * |
|
931 | - * @param array $arr |
|
932 | - * @return array |
|
933 | - */ |
|
934 | - private function removeModelNamesFromArray($arr) |
|
935 | - { |
|
936 | - return array_diff($arr, array_keys(EE_Registry::instance()->non_abstract_db_models)); |
|
937 | - } |
|
938 | - |
|
939 | - |
|
940 | - /** |
|
941 | - * Gets the calculated fields for the response |
|
942 | - * |
|
943 | - * @param EEM_Base $model |
|
944 | - * @param array $wpdb_row |
|
945 | - * @param WP_REST_Request $rest_request |
|
946 | - * @return \stdClass the _calculations item in the entity |
|
947 | - * @throws ObjectDetectedException if a default value has a PHP object, which should never do (and if we |
|
948 | - * did, let's know about it ASAP, so let the exception bubble up) |
|
949 | - */ |
|
950 | - protected function getEntityCalculations($model, $wpdb_row, $rest_request) |
|
951 | - { |
|
952 | - $calculated_fields = $this->explodeAndGetItemsPrefixedWith( |
|
953 | - $rest_request->get_param('calculate'), |
|
954 | - '' |
|
955 | - ); |
|
956 | - // note: setting calculate=* doesn't do anything |
|
957 | - $calculated_fields_to_return = new \stdClass(); |
|
958 | - foreach ($calculated_fields as $field_to_calculate) { |
|
959 | - try { |
|
960 | - $calculated_fields_to_return->$field_to_calculate = ModelDataTranslator::prepareFieldValueForJson( |
|
961 | - null, |
|
962 | - $this->fields_calculator->retrieveCalculatedFieldValue( |
|
963 | - $model, |
|
964 | - $field_to_calculate, |
|
965 | - $wpdb_row, |
|
966 | - $rest_request, |
|
967 | - $this |
|
968 | - ), |
|
969 | - $this->getModelVersionInfo()->requestedVersion() |
|
970 | - ); |
|
971 | - } catch (RestException $e) { |
|
972 | - // if we don't have permission to read it, just leave it out. but let devs know about the problem |
|
973 | - $this->setResponseHeader( |
|
974 | - 'Notices-Field-Calculation-Errors[' |
|
975 | - . $e->getStringCode() |
|
976 | - . '][' |
|
977 | - . $model->get_this_model_name() |
|
978 | - . '][' |
|
979 | - . $field_to_calculate |
|
980 | - . ']', |
|
981 | - $e->getMessage(), |
|
982 | - true |
|
983 | - ); |
|
984 | - } |
|
985 | - } |
|
986 | - return $calculated_fields_to_return; |
|
987 | - } |
|
988 | - |
|
989 | - |
|
990 | - /** |
|
991 | - * Gets the full URL to the resource, taking the requested version into account |
|
992 | - * |
|
993 | - * @param string $link_part_after_version_and_slash eg "events/10/datetimes" |
|
994 | - * @return string url eg "http://mysite.com/wp-json/ee/v4.6/events/10/datetimes" |
|
995 | - */ |
|
996 | - public function getVersionedLinkTo($link_part_after_version_and_slash) |
|
997 | - { |
|
998 | - return rest_url( |
|
999 | - EED_Core_Rest_Api::get_versioned_route_to( |
|
1000 | - $link_part_after_version_and_slash, |
|
1001 | - $this->getModelVersionInfo()->requestedVersion() |
|
1002 | - ) |
|
1003 | - ); |
|
1004 | - } |
|
1005 | - |
|
1006 | - |
|
1007 | - /** |
|
1008 | - * Gets the correct lowercase name for the relation in the API according |
|
1009 | - * to the relation's type |
|
1010 | - * |
|
1011 | - * @param string $relation_name |
|
1012 | - * @param \EE_Model_Relation_Base $relation_obj |
|
1013 | - * @return string |
|
1014 | - */ |
|
1015 | - public static function getRelatedEntityName($relation_name, $relation_obj) |
|
1016 | - { |
|
1017 | - if ($relation_obj instanceof EE_Belongs_To_Relation) { |
|
1018 | - return strtolower($relation_name); |
|
1019 | - } else { |
|
1020 | - return EEH_Inflector::pluralize_and_lower($relation_name); |
|
1021 | - } |
|
1022 | - } |
|
1023 | - |
|
1024 | - |
|
1025 | - /** |
|
1026 | - * Gets the one model object with the specified id for the specified model |
|
1027 | - * |
|
1028 | - * @param EEM_Base $model |
|
1029 | - * @param WP_REST_Request $request |
|
1030 | - * @return array|WP_Error |
|
1031 | - */ |
|
1032 | - public function getEntityFromModel($model, $request) |
|
1033 | - { |
|
1034 | - $context = $this->validateContext($request->get_param('caps')); |
|
1035 | - return $this->getOneOrReportPermissionError($model, $request, $context); |
|
1036 | - } |
|
1037 | - |
|
1038 | - |
|
1039 | - /** |
|
1040 | - * If a context is provided which isn't valid, maybe it was added in a future |
|
1041 | - * version so just treat it as a default read |
|
1042 | - * |
|
1043 | - * @param string $context |
|
1044 | - * @return string array key of EEM_Base::cap_contexts_to_cap_action_map() |
|
1045 | - */ |
|
1046 | - public function validateContext($context) |
|
1047 | - { |
|
1048 | - if (! $context) { |
|
1049 | - $context = EEM_Base::caps_read; |
|
1050 | - } |
|
1051 | - $valid_contexts = EEM_Base::valid_cap_contexts(); |
|
1052 | - if (in_array($context, $valid_contexts)) { |
|
1053 | - return $context; |
|
1054 | - } else { |
|
1055 | - return EEM_Base::caps_read; |
|
1056 | - } |
|
1057 | - } |
|
1058 | - |
|
1059 | - |
|
1060 | - /** |
|
1061 | - * Verifies the passed in value is an allowable default where conditions value. |
|
1062 | - * |
|
1063 | - * @param $default_query_params |
|
1064 | - * @return string |
|
1065 | - */ |
|
1066 | - public function validateDefaultQueryParams($default_query_params) |
|
1067 | - { |
|
1068 | - $valid_default_where_conditions_for_api_calls = array( |
|
1069 | - EEM_Base::default_where_conditions_all, |
|
1070 | - EEM_Base::default_where_conditions_minimum_all, |
|
1071 | - EEM_Base::default_where_conditions_minimum_others, |
|
1072 | - ); |
|
1073 | - if (! $default_query_params) { |
|
1074 | - $default_query_params = EEM_Base::default_where_conditions_all; |
|
1075 | - } |
|
1076 | - if (in_array( |
|
1077 | - $default_query_params, |
|
1078 | - $valid_default_where_conditions_for_api_calls, |
|
1079 | - true |
|
1080 | - )) { |
|
1081 | - return $default_query_params; |
|
1082 | - } else { |
|
1083 | - return EEM_Base::default_where_conditions_all; |
|
1084 | - } |
|
1085 | - } |
|
1086 | - |
|
1087 | - |
|
1088 | - /** |
|
1089 | - * Translates API filter get parameter into $query_params array used by EEM_Base::get_all(). |
|
1090 | - * Note: right now the query parameter keys for fields (and related fields) |
|
1091 | - * can be left as-is, but it's quite possible this will change someday. |
|
1092 | - * Also, this method's contents might be candidate for moving to Model_Data_Translator |
|
1093 | - * |
|
1094 | - * @param EEM_Base $model |
|
1095 | - * @param array $query_parameters from $_GET parameter @see Read:handle_request_get_all |
|
1096 | - * @return array like what EEM_Base::get_all() expects or FALSE to indicate |
|
1097 | - * that absolutely no results should be returned |
|
1098 | - * @throws EE_Error |
|
1099 | - * @throws RestException |
|
1100 | - */ |
|
1101 | - public function createModelQueryParams($model, $query_parameters) |
|
1102 | - { |
|
1103 | - $model_query_params = array(); |
|
1104 | - if (isset($query_parameters['where'])) { |
|
1105 | - $model_query_params[0] = ModelDataTranslator::prepareConditionsQueryParamsForModels( |
|
1106 | - $query_parameters['where'], |
|
1107 | - $model, |
|
1108 | - $this->getModelVersionInfo()->requestedVersion() |
|
1109 | - ); |
|
1110 | - } |
|
1111 | - if (isset($query_parameters['order_by'])) { |
|
1112 | - $order_by = $query_parameters['order_by']; |
|
1113 | - } elseif (isset($query_parameters['orderby'])) { |
|
1114 | - $order_by = $query_parameters['orderby']; |
|
1115 | - } else { |
|
1116 | - $order_by = null; |
|
1117 | - } |
|
1118 | - if ($order_by !== null) { |
|
1119 | - if (is_array($order_by)) { |
|
1120 | - $order_by = ModelDataTranslator::prepareFieldNamesInArrayKeysFromJson($order_by); |
|
1121 | - } else { |
|
1122 | - // it's a single item |
|
1123 | - $order_by = ModelDataTranslator::prepareFieldNameFromJson($order_by); |
|
1124 | - } |
|
1125 | - $model_query_params['order_by'] = $order_by; |
|
1126 | - } |
|
1127 | - if (isset($query_parameters['group_by'])) { |
|
1128 | - $group_by = $query_parameters['group_by']; |
|
1129 | - } elseif (isset($query_parameters['groupby'])) { |
|
1130 | - $group_by = $query_parameters['groupby']; |
|
1131 | - } else { |
|
1132 | - $group_by = array_keys($model->get_combined_primary_key_fields()); |
|
1133 | - } |
|
1134 | - // make sure they're all real names |
|
1135 | - if (is_array($group_by)) { |
|
1136 | - $group_by = ModelDataTranslator::prepareFieldNamesFromJson($group_by); |
|
1137 | - } |
|
1138 | - if ($group_by !== null) { |
|
1139 | - $model_query_params['group_by'] = $group_by; |
|
1140 | - } |
|
1141 | - if (isset($query_parameters['having'])) { |
|
1142 | - $model_query_params['having'] = ModelDataTranslator::prepareConditionsQueryParamsForModels( |
|
1143 | - $query_parameters['having'], |
|
1144 | - $model, |
|
1145 | - $this->getModelVersionInfo()->requestedVersion() |
|
1146 | - ); |
|
1147 | - } |
|
1148 | - if (isset($query_parameters['order'])) { |
|
1149 | - $model_query_params['order'] = $query_parameters['order']; |
|
1150 | - } |
|
1151 | - if (isset($query_parameters['mine'])) { |
|
1152 | - $model_query_params = $model->alter_query_params_to_only_include_mine($model_query_params); |
|
1153 | - } |
|
1154 | - if (isset($query_parameters['limit'])) { |
|
1155 | - // limit should be either a string like '23' or '23,43', or an array with two items in it |
|
1156 | - if (! is_array($query_parameters['limit'])) { |
|
1157 | - $limit_array = explode(',', (string) $query_parameters['limit']); |
|
1158 | - } else { |
|
1159 | - $limit_array = $query_parameters['limit']; |
|
1160 | - } |
|
1161 | - $sanitized_limit = array(); |
|
1162 | - foreach ($limit_array as $key => $limit_part) { |
|
1163 | - if ($this->debug_mode && (! is_numeric($limit_part) || count($sanitized_limit) > 2)) { |
|
1164 | - throw new EE_Error( |
|
1165 | - sprintf( |
|
1166 | - __( |
|
1167 | - // @codingStandardsIgnoreStart |
|
1168 | - 'An invalid limit filter was provided. It was: %s. If the EE4 JSON REST API weren\'t in debug mode, this message would not appear.', |
|
1169 | - // @codingStandardsIgnoreEnd |
|
1170 | - 'event_espresso' |
|
1171 | - ), |
|
1172 | - wp_json_encode($query_parameters['limit']) |
|
1173 | - ) |
|
1174 | - ); |
|
1175 | - } |
|
1176 | - $sanitized_limit[] = (int) $limit_part; |
|
1177 | - } |
|
1178 | - $model_query_params['limit'] = implode(',', $sanitized_limit); |
|
1179 | - } else { |
|
1180 | - $model_query_params['limit'] = EED_Core_Rest_Api::get_default_query_limit(); |
|
1181 | - } |
|
1182 | - if (isset($query_parameters['caps'])) { |
|
1183 | - $model_query_params['caps'] = $this->validateContext($query_parameters['caps']); |
|
1184 | - } else { |
|
1185 | - $model_query_params['caps'] = EEM_Base::caps_read; |
|
1186 | - } |
|
1187 | - if (isset($query_parameters['default_where_conditions'])) { |
|
1188 | - $model_query_params['default_where_conditions'] = $this->validateDefaultQueryParams( |
|
1189 | - $query_parameters['default_where_conditions'] |
|
1190 | - ); |
|
1191 | - } |
|
1192 | - return apply_filters('FHEE__Read__create_model_query_params', $model_query_params, $query_parameters, $model); |
|
1193 | - } |
|
1194 | - |
|
1195 | - |
|
1196 | - /** |
|
1197 | - * Changes the REST-style query params for use in the models |
|
1198 | - * |
|
1199 | - * @deprecated |
|
1200 | - * @param EEM_Base $model |
|
1201 | - * @param array $query_params sub-array from @see EEM_Base::get_all() |
|
1202 | - * @return array |
|
1203 | - */ |
|
1204 | - public function prepareRestQueryParamsKeyForModels($model, $query_params) |
|
1205 | - { |
|
1206 | - $model_ready_query_params = array(); |
|
1207 | - foreach ($query_params as $key => $value) { |
|
1208 | - if (is_array($value)) { |
|
1209 | - $model_ready_query_params[ $key ] = $this->prepareRestQueryParamsKeyForModels($model, $value); |
|
1210 | - } else { |
|
1211 | - $model_ready_query_params[ $key ] = $value; |
|
1212 | - } |
|
1213 | - } |
|
1214 | - return $model_ready_query_params; |
|
1215 | - } |
|
1216 | - |
|
1217 | - |
|
1218 | - /** |
|
1219 | - * @deprecated instead use ModelDataTranslator::prepareFieldValuesFromJson() |
|
1220 | - * @param $model |
|
1221 | - * @param $query_params |
|
1222 | - * @return array |
|
1223 | - */ |
|
1224 | - public function prepareRestQueryParamsValuesForModels($model, $query_params) |
|
1225 | - { |
|
1226 | - $model_ready_query_params = array(); |
|
1227 | - foreach ($query_params as $key => $value) { |
|
1228 | - if (is_array($value)) { |
|
1229 | - $model_ready_query_params[ $key ] = $this->prepareRestQueryParamsValuesForModels($model, $value); |
|
1230 | - } else { |
|
1231 | - $model_ready_query_params[ $key ] = $value; |
|
1232 | - } |
|
1233 | - } |
|
1234 | - return $model_ready_query_params; |
|
1235 | - } |
|
1236 | - |
|
1237 | - |
|
1238 | - /** |
|
1239 | - * Explodes the string on commas, and only returns items with $prefix followed by a period. |
|
1240 | - * If no prefix is specified, returns items with no period. |
|
1241 | - * |
|
1242 | - * @param string|array $string_to_explode eg "jibba,jabba, blah, blah, blah" or array('jibba', 'jabba' ) |
|
1243 | - * @param string $prefix "Event" or "foobar" |
|
1244 | - * @return array $string_to_exploded exploded on COMMAS, and if a prefix was specified |
|
1245 | - * we only return strings starting with that and a period; if no prefix was |
|
1246 | - * specified we return all items containing NO periods |
|
1247 | - */ |
|
1248 | - public function explodeAndGetItemsPrefixedWith($string_to_explode, $prefix) |
|
1249 | - { |
|
1250 | - if (is_string($string_to_explode)) { |
|
1251 | - $exploded_contents = explode(',', $string_to_explode); |
|
1252 | - } elseif (is_array($string_to_explode)) { |
|
1253 | - $exploded_contents = $string_to_explode; |
|
1254 | - } else { |
|
1255 | - $exploded_contents = array(); |
|
1256 | - } |
|
1257 | - // if the string was empty, we want an empty array |
|
1258 | - $exploded_contents = array_filter($exploded_contents); |
|
1259 | - $contents_with_prefix = array(); |
|
1260 | - foreach ($exploded_contents as $item) { |
|
1261 | - $item = trim($item); |
|
1262 | - // if no prefix was provided, so we look for items with no "." in them |
|
1263 | - if (! $prefix) { |
|
1264 | - // does this item have a period? |
|
1265 | - if (strpos($item, '.') === false) { |
|
1266 | - // if not, then its what we're looking for |
|
1267 | - $contents_with_prefix[] = $item; |
|
1268 | - } |
|
1269 | - } elseif (strpos($item, $prefix . '.') === 0) { |
|
1270 | - // this item has the prefix and a period, grab it |
|
1271 | - $contents_with_prefix[] = substr( |
|
1272 | - $item, |
|
1273 | - strpos($item, $prefix . '.') + strlen($prefix . '.') |
|
1274 | - ); |
|
1275 | - } elseif ($item === $prefix) { |
|
1276 | - // this item is JUST the prefix |
|
1277 | - // so let's grab everything after, which is a blank string |
|
1278 | - $contents_with_prefix[] = ''; |
|
1279 | - } |
|
1280 | - } |
|
1281 | - return $contents_with_prefix; |
|
1282 | - } |
|
1283 | - |
|
1284 | - |
|
1285 | - /** |
|
1286 | - * @deprecated since 4.8.36.rc.001 You should instead use Read::explode_and_get_items_prefixed_with. |
|
1287 | - * Deprecated because its return values were really quite confusing- sometimes it returned |
|
1288 | - * an empty array (when the include string was blank or '*') or sometimes it returned |
|
1289 | - * array('*') (when you provided a model and a model of that kind was found). |
|
1290 | - * Parses the $include_string so we fetch all the field names relating to THIS model |
|
1291 | - * (ie have NO period in them), or for the provided model (ie start with the model |
|
1292 | - * name and then a period). |
|
1293 | - * @param string $include_string @see Read:handle_request_get_all |
|
1294 | - * @param string $model_name |
|
1295 | - * @return array of fields for this model. If $model_name is provided, then |
|
1296 | - * the fields for that model, with the model's name removed from each. |
|
1297 | - * If $include_string was blank or '*' returns an empty array |
|
1298 | - */ |
|
1299 | - public function extractIncludesForThisModel($include_string, $model_name = null) |
|
1300 | - { |
|
1301 | - if (is_array($include_string)) { |
|
1302 | - $include_string = implode(',', $include_string); |
|
1303 | - } |
|
1304 | - if ($include_string === '*' || $include_string === '') { |
|
1305 | - return array(); |
|
1306 | - } |
|
1307 | - $includes = explode(',', $include_string); |
|
1308 | - $extracted_fields_to_include = array(); |
|
1309 | - if ($model_name) { |
|
1310 | - foreach ($includes as $field_to_include) { |
|
1311 | - $field_to_include = trim($field_to_include); |
|
1312 | - if (strpos($field_to_include, $model_name . '.') === 0) { |
|
1313 | - // found the model name at the exact start |
|
1314 | - $field_sans_model_name = str_replace($model_name . '.', '', $field_to_include); |
|
1315 | - $extracted_fields_to_include[] = $field_sans_model_name; |
|
1316 | - } elseif ($field_to_include == $model_name) { |
|
1317 | - $extracted_fields_to_include[] = '*'; |
|
1318 | - } |
|
1319 | - } |
|
1320 | - } else { |
|
1321 | - // look for ones with no period |
|
1322 | - foreach ($includes as $field_to_include) { |
|
1323 | - $field_to_include = trim($field_to_include); |
|
1324 | - if (strpos($field_to_include, '.') === false |
|
1325 | - && ! $this->getModelVersionInfo()->isModelNameInThisVersion($field_to_include) |
|
1326 | - ) { |
|
1327 | - $extracted_fields_to_include[] = $field_to_include; |
|
1328 | - } |
|
1329 | - } |
|
1330 | - } |
|
1331 | - return $extracted_fields_to_include; |
|
1332 | - } |
|
1333 | - |
|
1334 | - |
|
1335 | - /** |
|
1336 | - * Gets the single item using the model according to the request in the context given, otherwise |
|
1337 | - * returns that it's inaccessible to the current user |
|
1338 | - * |
|
1339 | - * @param EEM_Base $model |
|
1340 | - * @param WP_REST_Request $request |
|
1341 | - * @param null $context |
|
1342 | - * @return array|WP_Error |
|
1343 | - */ |
|
1344 | - public function getOneOrReportPermissionError(EEM_Base $model, WP_REST_Request $request, $context = null) |
|
1345 | - { |
|
1346 | - $query_params = array(array($model->primary_key_name() => $request->get_param('id')), 'limit' => 1); |
|
1347 | - if ($model instanceof \EEM_Soft_Delete_Base) { |
|
1348 | - $query_params = $model->alter_query_params_so_deleted_and_undeleted_items_included($query_params); |
|
1349 | - } |
|
1350 | - $restricted_query_params = $query_params; |
|
1351 | - $restricted_query_params['caps'] = $context; |
|
1352 | - $this->setDebugInfo('model query params', $restricted_query_params); |
|
1353 | - $model_rows = $model->get_all_wpdb_results($restricted_query_params); |
|
1354 | - if (! empty($model_rows)) { |
|
1355 | - return $this->createEntityFromWpdbResult( |
|
1356 | - $model, |
|
1357 | - array_shift($model_rows), |
|
1358 | - $request |
|
1359 | - ); |
|
1360 | - } else { |
|
1361 | - // ok let's test to see if we WOULD have found it, had we not had restrictions from missing capabilities |
|
1362 | - $lowercase_model_name = strtolower($model->get_this_model_name()); |
|
1363 | - $model_rows_found_sans_restrictions = $model->get_all_wpdb_results($query_params); |
|
1364 | - if (! empty($model_rows_found_sans_restrictions)) { |
|
1365 | - // you got shafted- it existed but we didn't want to tell you! |
|
1366 | - return new WP_Error( |
|
1367 | - 'rest_user_cannot_' . $context, |
|
1368 | - sprintf( |
|
1369 | - __('Sorry, you cannot %1$s this %2$s. Missing permissions are: %3$s', 'event_espresso'), |
|
1370 | - $context, |
|
1371 | - strtolower($model->get_this_model_name()), |
|
1372 | - Capabilities::getMissingPermissionsString( |
|
1373 | - $model, |
|
1374 | - $context |
|
1375 | - ) |
|
1376 | - ), |
|
1377 | - array('status' => 403) |
|
1378 | - ); |
|
1379 | - } else { |
|
1380 | - // it's not you. It just doesn't exist |
|
1381 | - return new WP_Error( |
|
1382 | - sprintf('rest_%s_invalid_id', $lowercase_model_name), |
|
1383 | - sprintf(__('Invalid %s ID.', 'event_espresso'), $lowercase_model_name), |
|
1384 | - array('status' => 404) |
|
1385 | - ); |
|
1386 | - } |
|
1387 | - } |
|
1388 | - } |
|
43 | + /** |
|
44 | + * @var CalculatedModelFields |
|
45 | + */ |
|
46 | + protected $fields_calculator; |
|
47 | + |
|
48 | + |
|
49 | + /** |
|
50 | + * Read constructor. |
|
51 | + * @param CalculatedModelFields $fields_calculator |
|
52 | + */ |
|
53 | + public function __construct(CalculatedModelFields $fields_calculator) |
|
54 | + { |
|
55 | + parent::__construct(); |
|
56 | + $this->fields_calculator = $fields_calculator; |
|
57 | + } |
|
58 | + |
|
59 | + |
|
60 | + /** |
|
61 | + * Handles requests to get all (or a filtered subset) of entities for a particular model |
|
62 | + * |
|
63 | + * @param WP_REST_Request $request |
|
64 | + * @param string $version |
|
65 | + * @param string $model_name |
|
66 | + * @return WP_REST_Response|WP_Error |
|
67 | + * @throws InvalidArgumentException |
|
68 | + * @throws InvalidDataTypeException |
|
69 | + * @throws InvalidInterfaceException |
|
70 | + */ |
|
71 | + public static function handleRequestGetAll(WP_REST_Request $request, $version, $model_name) |
|
72 | + { |
|
73 | + $controller = LoaderFactory::getLoader()->getNew('EventEspresso\core\libraries\rest_api\controllers\model\Read'); |
|
74 | + try { |
|
75 | + $controller->setRequestedVersion($version); |
|
76 | + if (! $controller->getModelVersionInfo()->isModelNameInThisVersion($model_name)) { |
|
77 | + return $controller->sendResponse( |
|
78 | + new WP_Error( |
|
79 | + 'endpoint_parsing_error', |
|
80 | + sprintf( |
|
81 | + __( |
|
82 | + 'There is no model for endpoint %s. Please contact event espresso support', |
|
83 | + 'event_espresso' |
|
84 | + ), |
|
85 | + $model_name |
|
86 | + ) |
|
87 | + ) |
|
88 | + ); |
|
89 | + } |
|
90 | + return $controller->sendResponse( |
|
91 | + $controller->getEntitiesFromModel( |
|
92 | + $controller->getModelVersionInfo()->loadModel($model_name), |
|
93 | + $request |
|
94 | + ) |
|
95 | + ); |
|
96 | + } catch (Exception $e) { |
|
97 | + return $controller->sendResponse($e); |
|
98 | + } |
|
99 | + } |
|
100 | + |
|
101 | + |
|
102 | + /** |
|
103 | + * Prepares and returns schema for any OPTIONS request. |
|
104 | + * |
|
105 | + * @param string $version The API endpoint version being used. |
|
106 | + * @param string $model_name Something like `Event` or `Registration` |
|
107 | + * @return array |
|
108 | + * @throws InvalidArgumentException |
|
109 | + * @throws InvalidDataTypeException |
|
110 | + * @throws InvalidInterfaceException |
|
111 | + */ |
|
112 | + public static function handleSchemaRequest($version, $model_name) |
|
113 | + { |
|
114 | + $controller = LoaderFactory::getLoader()->getNew('EventEspresso\core\libraries\rest_api\controllers\model\Read'); |
|
115 | + try { |
|
116 | + $controller->setRequestedVersion($version); |
|
117 | + if (! $controller->getModelVersionInfo()->isModelNameInThisVersion($model_name)) { |
|
118 | + return array(); |
|
119 | + } |
|
120 | + // get the model for this version |
|
121 | + $model = $controller->getModelVersionInfo()->loadModel($model_name); |
|
122 | + $model_schema = new JsonModelSchema($model, LoaderFactory::getLoader()->getShared('EventEspresso\core\libraries\rest_api\CalculatedModelFields')); |
|
123 | + return $model_schema->getModelSchemaForRelations( |
|
124 | + $controller->getModelVersionInfo()->relationSettings($model), |
|
125 | + $controller->customizeSchemaForRestResponse( |
|
126 | + $model, |
|
127 | + $model_schema->getModelSchemaForFields( |
|
128 | + $controller->getModelVersionInfo()->fieldsOnModelInThisVersion($model), |
|
129 | + $model_schema->getInitialSchemaStructure() |
|
130 | + ) |
|
131 | + ) |
|
132 | + ); |
|
133 | + } catch (Exception $e) { |
|
134 | + return array(); |
|
135 | + } |
|
136 | + } |
|
137 | + |
|
138 | + |
|
139 | + /** |
|
140 | + * This loops through each field in the given schema for the model and does the following: |
|
141 | + * - add any extra fields that are REST API specific and related to existing fields. |
|
142 | + * - transform default values into the correct format for a REST API response. |
|
143 | + * |
|
144 | + * @param EEM_Base $model |
|
145 | + * @param array $schema |
|
146 | + * @return array The final schema. |
|
147 | + */ |
|
148 | + protected function customizeSchemaForRestResponse(EEM_Base $model, array $schema) |
|
149 | + { |
|
150 | + foreach ($this->getModelVersionInfo()->fieldsOnModelInThisVersion($model) as $field_name => $field) { |
|
151 | + $schema = $this->translateDefaultsForRestResponse( |
|
152 | + $field_name, |
|
153 | + $field, |
|
154 | + $this->maybeAddExtraFieldsToSchema($field_name, $field, $schema) |
|
155 | + ); |
|
156 | + } |
|
157 | + return $schema; |
|
158 | + } |
|
159 | + |
|
160 | + |
|
161 | + /** |
|
162 | + * This is used to ensure that the 'default' value set in the schema response is formatted correctly for the REST |
|
163 | + * response. |
|
164 | + * |
|
165 | + * @param $field_name |
|
166 | + * @param EE_Model_Field_Base $field |
|
167 | + * @param array $schema |
|
168 | + * @return array |
|
169 | + * @throws ObjectDetectedException if a default value has a PHP object, which should never do (and if we |
|
170 | + * did, let's know about it ASAP, so let the exception bubble up) |
|
171 | + */ |
|
172 | + protected function translateDefaultsForRestResponse($field_name, EE_Model_Field_Base $field, array $schema) |
|
173 | + { |
|
174 | + if (isset($schema['properties'][ $field_name ]['default'])) { |
|
175 | + if (is_array($schema['properties'][ $field_name ]['default'])) { |
|
176 | + foreach ($schema['properties'][ $field_name ]['default'] as $default_key => $default_value) { |
|
177 | + if ($default_key === 'raw') { |
|
178 | + $schema['properties'][ $field_name ]['default'][ $default_key ] = |
|
179 | + ModelDataTranslator::prepareFieldValueForJson( |
|
180 | + $field, |
|
181 | + $default_value, |
|
182 | + $this->getModelVersionInfo()->requestedVersion() |
|
183 | + ); |
|
184 | + } |
|
185 | + } |
|
186 | + } else { |
|
187 | + $schema['properties'][ $field_name ]['default'] = ModelDataTranslator::prepareFieldValueForJson( |
|
188 | + $field, |
|
189 | + $schema['properties'][ $field_name ]['default'], |
|
190 | + $this->getModelVersionInfo()->requestedVersion() |
|
191 | + ); |
|
192 | + } |
|
193 | + } |
|
194 | + return $schema; |
|
195 | + } |
|
196 | + |
|
197 | + |
|
198 | + /** |
|
199 | + * Adds additional fields to the schema |
|
200 | + * The REST API returns a GMT value field for each datetime field in the resource. Thus the description about this |
|
201 | + * needs to be added to the schema. |
|
202 | + * |
|
203 | + * @param $field_name |
|
204 | + * @param EE_Model_Field_Base $field |
|
205 | + * @param array $schema |
|
206 | + * @return array |
|
207 | + */ |
|
208 | + protected function maybeAddExtraFieldsToSchema($field_name, EE_Model_Field_Base $field, array $schema) |
|
209 | + { |
|
210 | + if ($field instanceof EE_Datetime_Field) { |
|
211 | + $schema['properties'][ $field_name . '_gmt' ] = $field->getSchema(); |
|
212 | + // modify the description |
|
213 | + $schema['properties'][ $field_name . '_gmt' ]['description'] = sprintf( |
|
214 | + esc_html__('%s - the value for this field is in GMT.', 'event_espresso'), |
|
215 | + wp_specialchars_decode($field->get_nicename(), ENT_QUOTES) |
|
216 | + ); |
|
217 | + } |
|
218 | + return $schema; |
|
219 | + } |
|
220 | + |
|
221 | + |
|
222 | + /** |
|
223 | + * Used to figure out the route from the request when a `WP_REST_Request` object is not available |
|
224 | + * |
|
225 | + * @return string |
|
226 | + */ |
|
227 | + protected function getRouteFromRequest() |
|
228 | + { |
|
229 | + if (isset($GLOBALS['wp']) |
|
230 | + && $GLOBALS['wp'] instanceof \WP |
|
231 | + && isset($GLOBALS['wp']->query_vars['rest_route']) |
|
232 | + ) { |
|
233 | + return $GLOBALS['wp']->query_vars['rest_route']; |
|
234 | + } else { |
|
235 | + return isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : '/'; |
|
236 | + } |
|
237 | + } |
|
238 | + |
|
239 | + |
|
240 | + /** |
|
241 | + * Gets a single entity related to the model indicated in the path and its id |
|
242 | + * |
|
243 | + * @param WP_REST_Request $request |
|
244 | + * @param string $version |
|
245 | + * @param string $model_name |
|
246 | + * @return WP_REST_Response|WP_Error |
|
247 | + * @throws InvalidDataTypeException |
|
248 | + * @throws InvalidInterfaceException |
|
249 | + * @throws InvalidArgumentException |
|
250 | + */ |
|
251 | + public static function handleRequestGetOne(WP_REST_Request $request, $version, $model_name) |
|
252 | + { |
|
253 | + $controller = LoaderFactory::getLoader()->getNew('EventEspresso\core\libraries\rest_api\controllers\model\Read'); |
|
254 | + try { |
|
255 | + $controller->setRequestedVersion($version); |
|
256 | + if (! $controller->getModelVersionInfo()->isModelNameInThisVersion($model_name)) { |
|
257 | + return $controller->sendResponse( |
|
258 | + new WP_Error( |
|
259 | + 'endpoint_parsing_error', |
|
260 | + sprintf( |
|
261 | + __( |
|
262 | + 'There is no model for endpoint %s. Please contact event espresso support', |
|
263 | + 'event_espresso' |
|
264 | + ), |
|
265 | + $model_name |
|
266 | + ) |
|
267 | + ) |
|
268 | + ); |
|
269 | + } |
|
270 | + return $controller->sendResponse( |
|
271 | + $controller->getEntityFromModel( |
|
272 | + $controller->getModelVersionInfo()->loadModel($model_name), |
|
273 | + $request |
|
274 | + ) |
|
275 | + ); |
|
276 | + } catch (Exception $e) { |
|
277 | + return $controller->sendResponse($e); |
|
278 | + } |
|
279 | + } |
|
280 | + |
|
281 | + |
|
282 | + /** |
|
283 | + * Gets all the related entities (or if its a belongs-to relation just the one) |
|
284 | + * to the item with the given id |
|
285 | + * |
|
286 | + * @param WP_REST_Request $request |
|
287 | + * @param string $version |
|
288 | + * @param string $model_name |
|
289 | + * @param string $related_model_name |
|
290 | + * @return WP_REST_Response|WP_Error |
|
291 | + * @throws InvalidDataTypeException |
|
292 | + * @throws InvalidInterfaceException |
|
293 | + * @throws InvalidArgumentException |
|
294 | + */ |
|
295 | + public static function handleRequestGetRelated( |
|
296 | + WP_REST_Request $request, |
|
297 | + $version, |
|
298 | + $model_name, |
|
299 | + $related_model_name |
|
300 | + ) { |
|
301 | + $controller = LoaderFactory::getLoader()->getNew('EventEspresso\core\libraries\rest_api\controllers\model\Read'); |
|
302 | + try { |
|
303 | + $controller->setRequestedVersion($version); |
|
304 | + if (! $controller->getModelVersionInfo()->isModelNameInThisVersion($model_name)) { |
|
305 | + return $controller->sendResponse( |
|
306 | + new WP_Error( |
|
307 | + 'endpoint_parsing_error', |
|
308 | + sprintf( |
|
309 | + __( |
|
310 | + 'There is no model for endpoint %s. Please contact event espresso support', |
|
311 | + 'event_espresso' |
|
312 | + ), |
|
313 | + $model_name |
|
314 | + ) |
|
315 | + ) |
|
316 | + ); |
|
317 | + } |
|
318 | + $main_model = $controller->getModelVersionInfo()->loadModel($model_name); |
|
319 | + if (! $controller->getModelVersionInfo()->isModelNameInThisVersion($related_model_name)) { |
|
320 | + return $controller->sendResponse( |
|
321 | + new WP_Error( |
|
322 | + 'endpoint_parsing_error', |
|
323 | + sprintf( |
|
324 | + __( |
|
325 | + 'There is no model for endpoint %s. Please contact event espresso support', |
|
326 | + 'event_espresso' |
|
327 | + ), |
|
328 | + $related_model_name |
|
329 | + ) |
|
330 | + ) |
|
331 | + ); |
|
332 | + } |
|
333 | + return $controller->sendResponse( |
|
334 | + $controller->getEntitiesFromRelation( |
|
335 | + $request->get_param('id'), |
|
336 | + $main_model->related_settings_for($related_model_name), |
|
337 | + $request |
|
338 | + ) |
|
339 | + ); |
|
340 | + } catch (Exception $e) { |
|
341 | + return $controller->sendResponse($e); |
|
342 | + } |
|
343 | + } |
|
344 | + |
|
345 | + |
|
346 | + /** |
|
347 | + * Gets a collection for the given model and filters |
|
348 | + * |
|
349 | + * @param EEM_Base $model |
|
350 | + * @param WP_REST_Request $request |
|
351 | + * @return array|WP_Error |
|
352 | + */ |
|
353 | + public function getEntitiesFromModel($model, $request) |
|
354 | + { |
|
355 | + $query_params = $this->createModelQueryParams($model, $request->get_params()); |
|
356 | + if (! Capabilities::currentUserHasPartialAccessTo($model, $query_params['caps'])) { |
|
357 | + $model_name_plural = EEH_Inflector::pluralize_and_lower($model->get_this_model_name()); |
|
358 | + return new WP_Error( |
|
359 | + sprintf('rest_%s_cannot_list', $model_name_plural), |
|
360 | + sprintf( |
|
361 | + __('Sorry, you are not allowed to list %1$s. Missing permissions: %2$s', 'event_espresso'), |
|
362 | + $model_name_plural, |
|
363 | + Capabilities::getMissingPermissionsString($model, $query_params['caps']) |
|
364 | + ), |
|
365 | + array('status' => 403) |
|
366 | + ); |
|
367 | + } |
|
368 | + if (! $request->get_header('no_rest_headers')) { |
|
369 | + $this->setHeadersFromQueryParams($model, $query_params); |
|
370 | + } |
|
371 | + /** @type array $results */ |
|
372 | + $results = $model->get_all_wpdb_results($query_params); |
|
373 | + $nice_results = array(); |
|
374 | + foreach ($results as $result) { |
|
375 | + $nice_results[] = $this->createEntityFromWpdbResult( |
|
376 | + $model, |
|
377 | + $result, |
|
378 | + $request |
|
379 | + ); |
|
380 | + } |
|
381 | + return $nice_results; |
|
382 | + } |
|
383 | + |
|
384 | + |
|
385 | + /** |
|
386 | + * Gets the collection for given relation object |
|
387 | + * The same as Read::get_entities_from_model(), except if the relation |
|
388 | + * is a HABTM relation, in which case it merges any non-foreign-key fields from |
|
389 | + * the join-model-object into the results |
|
390 | + * |
|
391 | + * @param array $primary_model_query_params query params for finding the item from which |
|
392 | + * relations will be based |
|
393 | + * @param \EE_Model_Relation_Base $relation |
|
394 | + * @param WP_REST_Request $request |
|
395 | + * @return WP_Error|array |
|
396 | + * @throws RestException |
|
397 | + */ |
|
398 | + protected function getEntitiesFromRelationUsingModelQueryParams($primary_model_query_params, $relation, $request) |
|
399 | + { |
|
400 | + $context = $this->validateContext($request->get_param('caps')); |
|
401 | + $model = $relation->get_this_model(); |
|
402 | + $related_model = $relation->get_other_model(); |
|
403 | + if (! isset($primary_model_query_params[0])) { |
|
404 | + $primary_model_query_params[0] = array(); |
|
405 | + } |
|
406 | + // check if they can access the 1st model object |
|
407 | + $primary_model_query_params = array( |
|
408 | + 0 => $primary_model_query_params[0], |
|
409 | + 'limit' => 1, |
|
410 | + ); |
|
411 | + if ($model instanceof \EEM_Soft_Delete_Base) { |
|
412 | + $primary_model_query_params = $model->alter_query_params_so_deleted_and_undeleted_items_included( |
|
413 | + $primary_model_query_params |
|
414 | + ); |
|
415 | + } |
|
416 | + $restricted_query_params = $primary_model_query_params; |
|
417 | + $restricted_query_params['caps'] = $context; |
|
418 | + $this->setDebugInfo('main model query params', $restricted_query_params); |
|
419 | + $this->setDebugInfo('missing caps', Capabilities::getMissingPermissionsString($related_model, $context)); |
|
420 | + if (! ( |
|
421 | + Capabilities::currentUserHasPartialAccessTo($related_model, $context) |
|
422 | + && $model->exists($restricted_query_params) |
|
423 | + ) |
|
424 | + ) { |
|
425 | + if ($relation instanceof EE_Belongs_To_Relation) { |
|
426 | + $related_model_name_maybe_plural = strtolower($related_model->get_this_model_name()); |
|
427 | + } else { |
|
428 | + $related_model_name_maybe_plural = EEH_Inflector::pluralize_and_lower( |
|
429 | + $related_model->get_this_model_name() |
|
430 | + ); |
|
431 | + } |
|
432 | + return new WP_Error( |
|
433 | + sprintf('rest_%s_cannot_list', $related_model_name_maybe_plural), |
|
434 | + sprintf( |
|
435 | + __( |
|
436 | + 'Sorry, you are not allowed to list %1$s related to %2$s. Missing permissions: %3$s', |
|
437 | + 'event_espresso' |
|
438 | + ), |
|
439 | + $related_model_name_maybe_plural, |
|
440 | + $relation->get_this_model()->get_this_model_name(), |
|
441 | + implode( |
|
442 | + ',', |
|
443 | + array_keys( |
|
444 | + Capabilities::getMissingPermissions($related_model, $context) |
|
445 | + ) |
|
446 | + ) |
|
447 | + ), |
|
448 | + array('status' => 403) |
|
449 | + ); |
|
450 | + } |
|
451 | + $query_params = $this->createModelQueryParams($relation->get_other_model(), $request->get_params()); |
|
452 | + foreach ($primary_model_query_params[0] as $where_condition_key => $where_condition_value) { |
|
453 | + $query_params[0][ $relation->get_this_model()->get_this_model_name() |
|
454 | + . '.' |
|
455 | + . $where_condition_key ] = $where_condition_value; |
|
456 | + } |
|
457 | + $query_params['default_where_conditions'] = 'none'; |
|
458 | + $query_params['caps'] = $context; |
|
459 | + if (! $request->get_header('no_rest_headers')) { |
|
460 | + $this->setHeadersFromQueryParams($relation->get_other_model(), $query_params); |
|
461 | + } |
|
462 | + /** @type array $results */ |
|
463 | + $results = $relation->get_other_model()->get_all_wpdb_results($query_params); |
|
464 | + $nice_results = array(); |
|
465 | + foreach ($results as $result) { |
|
466 | + $nice_result = $this->createEntityFromWpdbResult( |
|
467 | + $relation->get_other_model(), |
|
468 | + $result, |
|
469 | + $request |
|
470 | + ); |
|
471 | + if ($relation instanceof \EE_HABTM_Relation) { |
|
472 | + // put the unusual stuff (properties from the HABTM relation) first, and make sure |
|
473 | + // if there are conflicts we prefer the properties from the main model |
|
474 | + $join_model_result = $this->createEntityFromWpdbResult( |
|
475 | + $relation->get_join_model(), |
|
476 | + $result, |
|
477 | + $request |
|
478 | + ); |
|
479 | + $joined_result = array_merge($nice_result, $join_model_result); |
|
480 | + // but keep the meta stuff from the main model |
|
481 | + if (isset($nice_result['meta'])) { |
|
482 | + $joined_result['meta'] = $nice_result['meta']; |
|
483 | + } |
|
484 | + $nice_result = $joined_result; |
|
485 | + } |
|
486 | + $nice_results[] = $nice_result; |
|
487 | + } |
|
488 | + if ($relation instanceof EE_Belongs_To_Relation) { |
|
489 | + return array_shift($nice_results); |
|
490 | + } else { |
|
491 | + return $nice_results; |
|
492 | + } |
|
493 | + } |
|
494 | + |
|
495 | + |
|
496 | + /** |
|
497 | + * Gets the collection for given relation object |
|
498 | + * The same as Read::get_entities_from_model(), except if the relation |
|
499 | + * is a HABTM relation, in which case it merges any non-foreign-key fields from |
|
500 | + * the join-model-object into the results |
|
501 | + * |
|
502 | + * @param string $id the ID of the thing we are fetching related stuff from |
|
503 | + * @param \EE_Model_Relation_Base $relation |
|
504 | + * @param WP_REST_Request $request |
|
505 | + * @return array|WP_Error |
|
506 | + * @throws EE_Error |
|
507 | + */ |
|
508 | + public function getEntitiesFromRelation($id, $relation, $request) |
|
509 | + { |
|
510 | + if (! $relation->get_this_model()->has_primary_key_field()) { |
|
511 | + throw new EE_Error( |
|
512 | + sprintf( |
|
513 | + __( |
|
514 | + // @codingStandardsIgnoreStart |
|
515 | + 'Read::get_entities_from_relation should only be called from a model with a primary key, it was called from %1$s', |
|
516 | + // @codingStandardsIgnoreEnd |
|
517 | + 'event_espresso' |
|
518 | + ), |
|
519 | + $relation->get_this_model()->get_this_model_name() |
|
520 | + ) |
|
521 | + ); |
|
522 | + } |
|
523 | + return $this->getEntitiesFromRelationUsingModelQueryParams( |
|
524 | + array( |
|
525 | + array( |
|
526 | + $relation->get_this_model()->primary_key_name() => $id, |
|
527 | + ), |
|
528 | + ), |
|
529 | + $relation, |
|
530 | + $request |
|
531 | + ); |
|
532 | + } |
|
533 | + |
|
534 | + |
|
535 | + /** |
|
536 | + * Sets the headers that are based on the model and query params, |
|
537 | + * like the total records. This should only be called on the original request |
|
538 | + * from the client, not on subsequent internal |
|
539 | + * |
|
540 | + * @param EEM_Base $model |
|
541 | + * @param array $query_params |
|
542 | + * @return void |
|
543 | + */ |
|
544 | + protected function setHeadersFromQueryParams($model, $query_params) |
|
545 | + { |
|
546 | + $this->setDebugInfo('model query params', $query_params); |
|
547 | + $this->setDebugInfo( |
|
548 | + 'missing caps', |
|
549 | + Capabilities::getMissingPermissionsString($model, $query_params['caps']) |
|
550 | + ); |
|
551 | + // normally the limit to a 2-part array, where the 2nd item is the limit |
|
552 | + if (! isset($query_params['limit'])) { |
|
553 | + $query_params['limit'] = EED_Core_Rest_Api::get_default_query_limit(); |
|
554 | + } |
|
555 | + if (is_array($query_params['limit'])) { |
|
556 | + $limit_parts = $query_params['limit']; |
|
557 | + } else { |
|
558 | + $limit_parts = explode(',', $query_params['limit']); |
|
559 | + if (count($limit_parts) == 1) { |
|
560 | + $limit_parts = array(0, $limit_parts[0]); |
|
561 | + } |
|
562 | + } |
|
563 | + // remove the group by and having parts of the query, as those will |
|
564 | + // make the sql query return an array of values, instead of just a single value |
|
565 | + unset($query_params['group_by'], $query_params['having'], $query_params['limit']); |
|
566 | + $count = $model->count($query_params, null, true); |
|
567 | + $pages = $count / $limit_parts[1]; |
|
568 | + $this->setResponseHeader('Total', $count, false); |
|
569 | + $this->setResponseHeader('PageSize', $limit_parts[1], false); |
|
570 | + $this->setResponseHeader('TotalPages', ceil($pages), false); |
|
571 | + } |
|
572 | + |
|
573 | + |
|
574 | + /** |
|
575 | + * Changes database results into REST API entities |
|
576 | + * |
|
577 | + * @param EEM_Base $model |
|
578 | + * @param array $db_row like results from $wpdb->get_results() |
|
579 | + * @param WP_REST_Request $rest_request |
|
580 | + * @param string $deprecated no longer used |
|
581 | + * @return array ready for being converted into json for sending to client |
|
582 | + */ |
|
583 | + public function createEntityFromWpdbResult($model, $db_row, $rest_request, $deprecated = null) |
|
584 | + { |
|
585 | + if (! $rest_request instanceof WP_REST_Request) { |
|
586 | + // ok so this was called in the old style, where the 3rd arg was |
|
587 | + // $include, and the 4th arg was $context |
|
588 | + // now setup the request just to avoid fatal errors, although we won't be able |
|
589 | + // to truly make use of it because it's kinda devoid of info |
|
590 | + $rest_request = new WP_REST_Request(); |
|
591 | + $rest_request->set_param('include', $rest_request); |
|
592 | + $rest_request->set_param('caps', $deprecated); |
|
593 | + } |
|
594 | + if ($rest_request->get_param('caps') == null) { |
|
595 | + $rest_request->set_param('caps', EEM_Base::caps_read); |
|
596 | + } |
|
597 | + $entity_array = $this->createBareEntityFromWpdbResults($model, $db_row); |
|
598 | + $entity_array = $this->addExtraFields($model, $db_row, $entity_array); |
|
599 | + $entity_array['_links'] = $this->getEntityLinks($model, $db_row, $entity_array); |
|
600 | + $entity_array['_calculated_fields'] = $this->getEntityCalculations($model, $db_row, $rest_request); |
|
601 | + $entity_array = apply_filters( |
|
602 | + 'FHEE__Read__create_entity_from_wpdb_results__entity_before_including_requested_models', |
|
603 | + $entity_array, |
|
604 | + $model, |
|
605 | + $rest_request->get_param('caps'), |
|
606 | + $rest_request, |
|
607 | + $this |
|
608 | + ); |
|
609 | + $entity_array = $this->includeRequestedModels($model, $rest_request, $entity_array, $db_row); |
|
610 | + $entity_array = apply_filters( |
|
611 | + 'FHEE__Read__create_entity_from_wpdb_results__entity_before_inaccessible_field_removal', |
|
612 | + $entity_array, |
|
613 | + $model, |
|
614 | + $rest_request->get_param('caps'), |
|
615 | + $rest_request, |
|
616 | + $this |
|
617 | + ); |
|
618 | + $result_without_inaccessible_fields = Capabilities::filterOutInaccessibleEntityFields( |
|
619 | + $entity_array, |
|
620 | + $model, |
|
621 | + $rest_request->get_param('caps'), |
|
622 | + $this->getModelVersionInfo(), |
|
623 | + $model->get_index_primary_key_string( |
|
624 | + $model->deduce_fields_n_values_from_cols_n_values($db_row) |
|
625 | + ) |
|
626 | + ); |
|
627 | + $this->setDebugInfo( |
|
628 | + 'inaccessible fields', |
|
629 | + array_keys(array_diff_key($entity_array, $result_without_inaccessible_fields)) |
|
630 | + ); |
|
631 | + return apply_filters( |
|
632 | + 'FHEE__Read__create_entity_from_wpdb_results__entity_return', |
|
633 | + $result_without_inaccessible_fields, |
|
634 | + $model, |
|
635 | + $rest_request->get_param('caps') |
|
636 | + ); |
|
637 | + } |
|
638 | + |
|
639 | + |
|
640 | + /** |
|
641 | + * Creates a REST entity array (JSON object we're going to return in the response, but |
|
642 | + * for now still a PHP array, but soon enough we'll call json_encode on it, don't worry), |
|
643 | + * from $wpdb->get_row( $sql, ARRAY_A) |
|
644 | + * |
|
645 | + * @param EEM_Base $model |
|
646 | + * @param array $db_row |
|
647 | + * @return array entity mostly ready for converting to JSON and sending in the response |
|
648 | + */ |
|
649 | + protected function createBareEntityFromWpdbResults(EEM_Base $model, $db_row) |
|
650 | + { |
|
651 | + $result = $model->deduce_fields_n_values_from_cols_n_values($db_row); |
|
652 | + $result = array_intersect_key( |
|
653 | + $result, |
|
654 | + $this->getModelVersionInfo()->fieldsOnModelInThisVersion($model) |
|
655 | + ); |
|
656 | + // if this is a CPT, we need to set the global $post to it, |
|
657 | + // otherwise shortcodes etc won't work properly while rendering it |
|
658 | + if ($model instanceof \EEM_CPT_Base) { |
|
659 | + $do_chevy_shuffle = true; |
|
660 | + } else { |
|
661 | + $do_chevy_shuffle = false; |
|
662 | + } |
|
663 | + if ($do_chevy_shuffle) { |
|
664 | + global $post; |
|
665 | + $old_post = $post; |
|
666 | + $post = get_post($result[ $model->primary_key_name() ]); |
|
667 | + if (! $post instanceof \WP_Post) { |
|
668 | + // well that's weird, because $result is what we JUST fetched from the database |
|
669 | + throw new RestException( |
|
670 | + 'error_fetching_post_from_database_results', |
|
671 | + esc_html__( |
|
672 | + 'An item was retrieved from the database but it\'s not a WP_Post like it should be.', |
|
673 | + 'event_espresso' |
|
674 | + ) |
|
675 | + ); |
|
676 | + } |
|
677 | + $model_object_classname = 'EE_' . $model->get_this_model_name(); |
|
678 | + $post->{$model_object_classname} = \EE_Registry::instance()->load_class( |
|
679 | + $model_object_classname, |
|
680 | + $result, |
|
681 | + false, |
|
682 | + false |
|
683 | + ); |
|
684 | + } |
|
685 | + foreach ($result as $field_name => $field_value) { |
|
686 | + $field_obj = $model->field_settings_for($field_name); |
|
687 | + if ($this->isSubclassOfOne($field_obj, $this->getModelVersionInfo()->fieldsIgnored())) { |
|
688 | + unset($result[ $field_name ]); |
|
689 | + } elseif ($this->isSubclassOfOne( |
|
690 | + $field_obj, |
|
691 | + $this->getModelVersionInfo()->fieldsThatHaveRenderedFormat() |
|
692 | + ) |
|
693 | + ) { |
|
694 | + $result[ $field_name ] = array( |
|
695 | + 'raw' => $this->prepareFieldObjValueForJson($field_obj, $field_value), |
|
696 | + 'rendered' => $this->prepareFieldObjValueForJson($field_obj, $field_value, 'pretty'), |
|
697 | + ); |
|
698 | + } elseif ($this->isSubclassOfOne( |
|
699 | + $field_obj, |
|
700 | + $this->getModelVersionInfo()->fieldsThatHavePrettyFormat() |
|
701 | + ) |
|
702 | + ) { |
|
703 | + $result[ $field_name ] = array( |
|
704 | + 'raw' => $this->prepareFieldObjValueForJson($field_obj, $field_value), |
|
705 | + 'pretty' => $this->prepareFieldObjValueForJson($field_obj, $field_value, 'pretty'), |
|
706 | + ); |
|
707 | + } elseif ($field_obj instanceof \EE_Datetime_Field) { |
|
708 | + $field_value = $field_obj->prepare_for_set_from_db($field_value); |
|
709 | + // if the value is null, but we're not supposed to permit null, then set to the field's default |
|
710 | + if (is_null($field_value)) { |
|
711 | + $field_value = $field_obj->getDefaultDateTimeObj(); |
|
712 | + } |
|
713 | + if (is_null($field_value)) { |
|
714 | + $gmt_date = $local_date = ModelDataTranslator::prepareFieldValuesForJson( |
|
715 | + $field_obj, |
|
716 | + $field_value, |
|
717 | + $this->getModelVersionInfo()->requestedVersion() |
|
718 | + ); |
|
719 | + } else { |
|
720 | + $timezone = $field_value->getTimezone(); |
|
721 | + EEH_DTT_Helper::setTimezone($field_value, new DateTimeZone('UTC')); |
|
722 | + $gmt_date = ModelDataTranslator::prepareFieldValuesForJson( |
|
723 | + $field_obj, |
|
724 | + $field_value, |
|
725 | + $this->getModelVersionInfo()->requestedVersion() |
|
726 | + ); |
|
727 | + EEH_DTT_Helper::setTimezone($field_value, $timezone); |
|
728 | + $local_date = ModelDataTranslator::prepareFieldValuesForJson( |
|
729 | + $field_obj, |
|
730 | + $field_value, |
|
731 | + $this->getModelVersionInfo()->requestedVersion() |
|
732 | + ); |
|
733 | + } |
|
734 | + $result[ $field_name . '_gmt' ] = $gmt_date; |
|
735 | + $result[ $field_name ] = $local_date; |
|
736 | + } else { |
|
737 | + $result[ $field_name ] = $this->prepareFieldObjValueForJson($field_obj, $field_value); |
|
738 | + } |
|
739 | + } |
|
740 | + if ($do_chevy_shuffle) { |
|
741 | + $post = $old_post; |
|
742 | + } |
|
743 | + return $result; |
|
744 | + } |
|
745 | + |
|
746 | + |
|
747 | + /** |
|
748 | + * Takes a value all the way from the DB representation, to the model object's representation, to the |
|
749 | + * user-facing PHP representation, to the REST API representation. (Assumes you've already taken from the DB |
|
750 | + * representation using $field_obj->prepare_for_set_from_db()) |
|
751 | + * |
|
752 | + * @param EE_Model_Field_Base $field_obj |
|
753 | + * @param mixed $value as it's stored on a model object |
|
754 | + * @param string $format valid values are 'normal' (default), 'pretty', 'datetime_obj' |
|
755 | + * @return mixed |
|
756 | + * @throws ObjectDetectedException if $value contains a PHP object |
|
757 | + */ |
|
758 | + protected function prepareFieldObjValueForJson(EE_Model_Field_Base $field_obj, $value, $format = 'normal') |
|
759 | + { |
|
760 | + $value = $field_obj->prepare_for_set_from_db($value); |
|
761 | + switch ($format) { |
|
762 | + case 'pretty': |
|
763 | + $value = $field_obj->prepare_for_pretty_echoing($value); |
|
764 | + break; |
|
765 | + case 'normal': |
|
766 | + default: |
|
767 | + $value = $field_obj->prepare_for_get($value); |
|
768 | + break; |
|
769 | + } |
|
770 | + return ModelDataTranslator::prepareFieldValuesForJson( |
|
771 | + $field_obj, |
|
772 | + $value, |
|
773 | + $this->getModelVersionInfo()->requestedVersion() |
|
774 | + ); |
|
775 | + } |
|
776 | + |
|
777 | + |
|
778 | + /** |
|
779 | + * Adds a few extra fields to the entity response |
|
780 | + * |
|
781 | + * @param EEM_Base $model |
|
782 | + * @param array $db_row |
|
783 | + * @param array $entity_array |
|
784 | + * @return array modified entity |
|
785 | + */ |
|
786 | + protected function addExtraFields(EEM_Base $model, $db_row, $entity_array) |
|
787 | + { |
|
788 | + if ($model instanceof EEM_CPT_Base) { |
|
789 | + $entity_array['link'] = get_permalink($db_row[ $model->get_primary_key_field()->get_qualified_column() ]); |
|
790 | + } |
|
791 | + return $entity_array; |
|
792 | + } |
|
793 | + |
|
794 | + |
|
795 | + /** |
|
796 | + * Gets links we want to add to the response |
|
797 | + * |
|
798 | + * @global \WP_REST_Server $wp_rest_server |
|
799 | + * @param EEM_Base $model |
|
800 | + * @param array $db_row |
|
801 | + * @param array $entity_array |
|
802 | + * @return array the _links item in the entity |
|
803 | + */ |
|
804 | + protected function getEntityLinks($model, $db_row, $entity_array) |
|
805 | + { |
|
806 | + // add basic links |
|
807 | + $links = array(); |
|
808 | + if ($model->has_primary_key_field()) { |
|
809 | + $links['self'] = array( |
|
810 | + array( |
|
811 | + 'href' => $this->getVersionedLinkTo( |
|
812 | + EEH_Inflector::pluralize_and_lower($model->get_this_model_name()) |
|
813 | + . '/' |
|
814 | + . $entity_array[ $model->primary_key_name() ] |
|
815 | + ), |
|
816 | + ), |
|
817 | + ); |
|
818 | + } |
|
819 | + $links['collection'] = array( |
|
820 | + array( |
|
821 | + 'href' => $this->getVersionedLinkTo( |
|
822 | + EEH_Inflector::pluralize_and_lower($model->get_this_model_name()) |
|
823 | + ), |
|
824 | + ), |
|
825 | + ); |
|
826 | + // add links to related models |
|
827 | + if ($model->has_primary_key_field()) { |
|
828 | + foreach ($this->getModelVersionInfo()->relationSettings($model) as $relation_name => $relation_obj) { |
|
829 | + $related_model_part = Read::getRelatedEntityName($relation_name, $relation_obj); |
|
830 | + $links[ EED_Core_Rest_Api::ee_api_link_namespace . $related_model_part ] = array( |
|
831 | + array( |
|
832 | + 'href' => $this->getVersionedLinkTo( |
|
833 | + EEH_Inflector::pluralize_and_lower($model->get_this_model_name()) |
|
834 | + . '/' |
|
835 | + . $entity_array[ $model->primary_key_name() ] |
|
836 | + . '/' |
|
837 | + . $related_model_part |
|
838 | + ), |
|
839 | + 'single' => $relation_obj instanceof EE_Belongs_To_Relation ? true : false, |
|
840 | + ), |
|
841 | + ); |
|
842 | + } |
|
843 | + } |
|
844 | + return $links; |
|
845 | + } |
|
846 | + |
|
847 | + |
|
848 | + /** |
|
849 | + * Adds the included models indicated in the request to the entity provided |
|
850 | + * |
|
851 | + * @param EEM_Base $model |
|
852 | + * @param WP_REST_Request $rest_request |
|
853 | + * @param array $entity_array |
|
854 | + * @param array $db_row |
|
855 | + * @return array the modified entity |
|
856 | + */ |
|
857 | + protected function includeRequestedModels( |
|
858 | + EEM_Base $model, |
|
859 | + WP_REST_Request $rest_request, |
|
860 | + $entity_array, |
|
861 | + $db_row = array() |
|
862 | + ) { |
|
863 | + // if $db_row not included, hope the entity array has what we need |
|
864 | + if (! $db_row) { |
|
865 | + $db_row = $entity_array; |
|
866 | + } |
|
867 | + $includes_for_this_model = $this->explodeAndGetItemsPrefixedWith($rest_request->get_param('include'), ''); |
|
868 | + $includes_for_this_model = $this->removeModelNamesFromArray($includes_for_this_model); |
|
869 | + // if they passed in * or didn't specify any includes, return everything |
|
870 | + if (! in_array('*', $includes_for_this_model) |
|
871 | + && ! empty($includes_for_this_model) |
|
872 | + ) { |
|
873 | + if ($model->has_primary_key_field()) { |
|
874 | + // always include the primary key. ya just gotta know that at least |
|
875 | + $includes_for_this_model[] = $model->primary_key_name(); |
|
876 | + } |
|
877 | + if ($this->explodeAndGetItemsPrefixedWith($rest_request->get_param('calculate'), '')) { |
|
878 | + $includes_for_this_model[] = '_calculated_fields'; |
|
879 | + } |
|
880 | + $entity_array = array_intersect_key($entity_array, array_flip($includes_for_this_model)); |
|
881 | + } |
|
882 | + $relation_settings = $this->getModelVersionInfo()->relationSettings($model); |
|
883 | + foreach ($relation_settings as $relation_name => $relation_obj) { |
|
884 | + $related_fields_to_include = $this->explodeAndGetItemsPrefixedWith( |
|
885 | + $rest_request->get_param('include'), |
|
886 | + $relation_name |
|
887 | + ); |
|
888 | + $related_fields_to_calculate = $this->explodeAndGetItemsPrefixedWith( |
|
889 | + $rest_request->get_param('calculate'), |
|
890 | + $relation_name |
|
891 | + ); |
|
892 | + // did they specify they wanted to include a related model, or |
|
893 | + // specific fields from a related model? |
|
894 | + // or did they specify to calculate a field from a related model? |
|
895 | + if ($related_fields_to_include || $related_fields_to_calculate) { |
|
896 | + // if so, we should include at least some part of the related model |
|
897 | + $pretend_related_request = new WP_REST_Request(); |
|
898 | + $pretend_related_request->set_query_params( |
|
899 | + array( |
|
900 | + 'caps' => $rest_request->get_param('caps'), |
|
901 | + 'include' => $related_fields_to_include, |
|
902 | + 'calculate' => $related_fields_to_calculate, |
|
903 | + ) |
|
904 | + ); |
|
905 | + $pretend_related_request->add_header('no_rest_headers', true); |
|
906 | + $primary_model_query_params = $model->alter_query_params_to_restrict_by_ID( |
|
907 | + $model->get_index_primary_key_string( |
|
908 | + $model->deduce_fields_n_values_from_cols_n_values($db_row) |
|
909 | + ) |
|
910 | + ); |
|
911 | + $related_results = $this->getEntitiesFromRelationUsingModelQueryParams( |
|
912 | + $primary_model_query_params, |
|
913 | + $relation_obj, |
|
914 | + $pretend_related_request |
|
915 | + ); |
|
916 | + $entity_array[ Read::getRelatedEntityName($relation_name, $relation_obj) ] = $related_results |
|
917 | + instanceof |
|
918 | + WP_Error |
|
919 | + ? null |
|
920 | + : $related_results; |
|
921 | + } |
|
922 | + } |
|
923 | + return $entity_array; |
|
924 | + } |
|
925 | + |
|
926 | + |
|
927 | + /** |
|
928 | + * Returns a new array with all the names of models removed. Eg |
|
929 | + * array( 'Event', 'Datetime.*', 'foobar' ) would become array( 'Datetime.*', 'foobar' ) |
|
930 | + * |
|
931 | + * @param array $arr |
|
932 | + * @return array |
|
933 | + */ |
|
934 | + private function removeModelNamesFromArray($arr) |
|
935 | + { |
|
936 | + return array_diff($arr, array_keys(EE_Registry::instance()->non_abstract_db_models)); |
|
937 | + } |
|
938 | + |
|
939 | + |
|
940 | + /** |
|
941 | + * Gets the calculated fields for the response |
|
942 | + * |
|
943 | + * @param EEM_Base $model |
|
944 | + * @param array $wpdb_row |
|
945 | + * @param WP_REST_Request $rest_request |
|
946 | + * @return \stdClass the _calculations item in the entity |
|
947 | + * @throws ObjectDetectedException if a default value has a PHP object, which should never do (and if we |
|
948 | + * did, let's know about it ASAP, so let the exception bubble up) |
|
949 | + */ |
|
950 | + protected function getEntityCalculations($model, $wpdb_row, $rest_request) |
|
951 | + { |
|
952 | + $calculated_fields = $this->explodeAndGetItemsPrefixedWith( |
|
953 | + $rest_request->get_param('calculate'), |
|
954 | + '' |
|
955 | + ); |
|
956 | + // note: setting calculate=* doesn't do anything |
|
957 | + $calculated_fields_to_return = new \stdClass(); |
|
958 | + foreach ($calculated_fields as $field_to_calculate) { |
|
959 | + try { |
|
960 | + $calculated_fields_to_return->$field_to_calculate = ModelDataTranslator::prepareFieldValueForJson( |
|
961 | + null, |
|
962 | + $this->fields_calculator->retrieveCalculatedFieldValue( |
|
963 | + $model, |
|
964 | + $field_to_calculate, |
|
965 | + $wpdb_row, |
|
966 | + $rest_request, |
|
967 | + $this |
|
968 | + ), |
|
969 | + $this->getModelVersionInfo()->requestedVersion() |
|
970 | + ); |
|
971 | + } catch (RestException $e) { |
|
972 | + // if we don't have permission to read it, just leave it out. but let devs know about the problem |
|
973 | + $this->setResponseHeader( |
|
974 | + 'Notices-Field-Calculation-Errors[' |
|
975 | + . $e->getStringCode() |
|
976 | + . '][' |
|
977 | + . $model->get_this_model_name() |
|
978 | + . '][' |
|
979 | + . $field_to_calculate |
|
980 | + . ']', |
|
981 | + $e->getMessage(), |
|
982 | + true |
|
983 | + ); |
|
984 | + } |
|
985 | + } |
|
986 | + return $calculated_fields_to_return; |
|
987 | + } |
|
988 | + |
|
989 | + |
|
990 | + /** |
|
991 | + * Gets the full URL to the resource, taking the requested version into account |
|
992 | + * |
|
993 | + * @param string $link_part_after_version_and_slash eg "events/10/datetimes" |
|
994 | + * @return string url eg "http://mysite.com/wp-json/ee/v4.6/events/10/datetimes" |
|
995 | + */ |
|
996 | + public function getVersionedLinkTo($link_part_after_version_and_slash) |
|
997 | + { |
|
998 | + return rest_url( |
|
999 | + EED_Core_Rest_Api::get_versioned_route_to( |
|
1000 | + $link_part_after_version_and_slash, |
|
1001 | + $this->getModelVersionInfo()->requestedVersion() |
|
1002 | + ) |
|
1003 | + ); |
|
1004 | + } |
|
1005 | + |
|
1006 | + |
|
1007 | + /** |
|
1008 | + * Gets the correct lowercase name for the relation in the API according |
|
1009 | + * to the relation's type |
|
1010 | + * |
|
1011 | + * @param string $relation_name |
|
1012 | + * @param \EE_Model_Relation_Base $relation_obj |
|
1013 | + * @return string |
|
1014 | + */ |
|
1015 | + public static function getRelatedEntityName($relation_name, $relation_obj) |
|
1016 | + { |
|
1017 | + if ($relation_obj instanceof EE_Belongs_To_Relation) { |
|
1018 | + return strtolower($relation_name); |
|
1019 | + } else { |
|
1020 | + return EEH_Inflector::pluralize_and_lower($relation_name); |
|
1021 | + } |
|
1022 | + } |
|
1023 | + |
|
1024 | + |
|
1025 | + /** |
|
1026 | + * Gets the one model object with the specified id for the specified model |
|
1027 | + * |
|
1028 | + * @param EEM_Base $model |
|
1029 | + * @param WP_REST_Request $request |
|
1030 | + * @return array|WP_Error |
|
1031 | + */ |
|
1032 | + public function getEntityFromModel($model, $request) |
|
1033 | + { |
|
1034 | + $context = $this->validateContext($request->get_param('caps')); |
|
1035 | + return $this->getOneOrReportPermissionError($model, $request, $context); |
|
1036 | + } |
|
1037 | + |
|
1038 | + |
|
1039 | + /** |
|
1040 | + * If a context is provided which isn't valid, maybe it was added in a future |
|
1041 | + * version so just treat it as a default read |
|
1042 | + * |
|
1043 | + * @param string $context |
|
1044 | + * @return string array key of EEM_Base::cap_contexts_to_cap_action_map() |
|
1045 | + */ |
|
1046 | + public function validateContext($context) |
|
1047 | + { |
|
1048 | + if (! $context) { |
|
1049 | + $context = EEM_Base::caps_read; |
|
1050 | + } |
|
1051 | + $valid_contexts = EEM_Base::valid_cap_contexts(); |
|
1052 | + if (in_array($context, $valid_contexts)) { |
|
1053 | + return $context; |
|
1054 | + } else { |
|
1055 | + return EEM_Base::caps_read; |
|
1056 | + } |
|
1057 | + } |
|
1058 | + |
|
1059 | + |
|
1060 | + /** |
|
1061 | + * Verifies the passed in value is an allowable default where conditions value. |
|
1062 | + * |
|
1063 | + * @param $default_query_params |
|
1064 | + * @return string |
|
1065 | + */ |
|
1066 | + public function validateDefaultQueryParams($default_query_params) |
|
1067 | + { |
|
1068 | + $valid_default_where_conditions_for_api_calls = array( |
|
1069 | + EEM_Base::default_where_conditions_all, |
|
1070 | + EEM_Base::default_where_conditions_minimum_all, |
|
1071 | + EEM_Base::default_where_conditions_minimum_others, |
|
1072 | + ); |
|
1073 | + if (! $default_query_params) { |
|
1074 | + $default_query_params = EEM_Base::default_where_conditions_all; |
|
1075 | + } |
|
1076 | + if (in_array( |
|
1077 | + $default_query_params, |
|
1078 | + $valid_default_where_conditions_for_api_calls, |
|
1079 | + true |
|
1080 | + )) { |
|
1081 | + return $default_query_params; |
|
1082 | + } else { |
|
1083 | + return EEM_Base::default_where_conditions_all; |
|
1084 | + } |
|
1085 | + } |
|
1086 | + |
|
1087 | + |
|
1088 | + /** |
|
1089 | + * Translates API filter get parameter into $query_params array used by EEM_Base::get_all(). |
|
1090 | + * Note: right now the query parameter keys for fields (and related fields) |
|
1091 | + * can be left as-is, but it's quite possible this will change someday. |
|
1092 | + * Also, this method's contents might be candidate for moving to Model_Data_Translator |
|
1093 | + * |
|
1094 | + * @param EEM_Base $model |
|
1095 | + * @param array $query_parameters from $_GET parameter @see Read:handle_request_get_all |
|
1096 | + * @return array like what EEM_Base::get_all() expects or FALSE to indicate |
|
1097 | + * that absolutely no results should be returned |
|
1098 | + * @throws EE_Error |
|
1099 | + * @throws RestException |
|
1100 | + */ |
|
1101 | + public function createModelQueryParams($model, $query_parameters) |
|
1102 | + { |
|
1103 | + $model_query_params = array(); |
|
1104 | + if (isset($query_parameters['where'])) { |
|
1105 | + $model_query_params[0] = ModelDataTranslator::prepareConditionsQueryParamsForModels( |
|
1106 | + $query_parameters['where'], |
|
1107 | + $model, |
|
1108 | + $this->getModelVersionInfo()->requestedVersion() |
|
1109 | + ); |
|
1110 | + } |
|
1111 | + if (isset($query_parameters['order_by'])) { |
|
1112 | + $order_by = $query_parameters['order_by']; |
|
1113 | + } elseif (isset($query_parameters['orderby'])) { |
|
1114 | + $order_by = $query_parameters['orderby']; |
|
1115 | + } else { |
|
1116 | + $order_by = null; |
|
1117 | + } |
|
1118 | + if ($order_by !== null) { |
|
1119 | + if (is_array($order_by)) { |
|
1120 | + $order_by = ModelDataTranslator::prepareFieldNamesInArrayKeysFromJson($order_by); |
|
1121 | + } else { |
|
1122 | + // it's a single item |
|
1123 | + $order_by = ModelDataTranslator::prepareFieldNameFromJson($order_by); |
|
1124 | + } |
|
1125 | + $model_query_params['order_by'] = $order_by; |
|
1126 | + } |
|
1127 | + if (isset($query_parameters['group_by'])) { |
|
1128 | + $group_by = $query_parameters['group_by']; |
|
1129 | + } elseif (isset($query_parameters['groupby'])) { |
|
1130 | + $group_by = $query_parameters['groupby']; |
|
1131 | + } else { |
|
1132 | + $group_by = array_keys($model->get_combined_primary_key_fields()); |
|
1133 | + } |
|
1134 | + // make sure they're all real names |
|
1135 | + if (is_array($group_by)) { |
|
1136 | + $group_by = ModelDataTranslator::prepareFieldNamesFromJson($group_by); |
|
1137 | + } |
|
1138 | + if ($group_by !== null) { |
|
1139 | + $model_query_params['group_by'] = $group_by; |
|
1140 | + } |
|
1141 | + if (isset($query_parameters['having'])) { |
|
1142 | + $model_query_params['having'] = ModelDataTranslator::prepareConditionsQueryParamsForModels( |
|
1143 | + $query_parameters['having'], |
|
1144 | + $model, |
|
1145 | + $this->getModelVersionInfo()->requestedVersion() |
|
1146 | + ); |
|
1147 | + } |
|
1148 | + if (isset($query_parameters['order'])) { |
|
1149 | + $model_query_params['order'] = $query_parameters['order']; |
|
1150 | + } |
|
1151 | + if (isset($query_parameters['mine'])) { |
|
1152 | + $model_query_params = $model->alter_query_params_to_only_include_mine($model_query_params); |
|
1153 | + } |
|
1154 | + if (isset($query_parameters['limit'])) { |
|
1155 | + // limit should be either a string like '23' or '23,43', or an array with two items in it |
|
1156 | + if (! is_array($query_parameters['limit'])) { |
|
1157 | + $limit_array = explode(',', (string) $query_parameters['limit']); |
|
1158 | + } else { |
|
1159 | + $limit_array = $query_parameters['limit']; |
|
1160 | + } |
|
1161 | + $sanitized_limit = array(); |
|
1162 | + foreach ($limit_array as $key => $limit_part) { |
|
1163 | + if ($this->debug_mode && (! is_numeric($limit_part) || count($sanitized_limit) > 2)) { |
|
1164 | + throw new EE_Error( |
|
1165 | + sprintf( |
|
1166 | + __( |
|
1167 | + // @codingStandardsIgnoreStart |
|
1168 | + 'An invalid limit filter was provided. It was: %s. If the EE4 JSON REST API weren\'t in debug mode, this message would not appear.', |
|
1169 | + // @codingStandardsIgnoreEnd |
|
1170 | + 'event_espresso' |
|
1171 | + ), |
|
1172 | + wp_json_encode($query_parameters['limit']) |
|
1173 | + ) |
|
1174 | + ); |
|
1175 | + } |
|
1176 | + $sanitized_limit[] = (int) $limit_part; |
|
1177 | + } |
|
1178 | + $model_query_params['limit'] = implode(',', $sanitized_limit); |
|
1179 | + } else { |
|
1180 | + $model_query_params['limit'] = EED_Core_Rest_Api::get_default_query_limit(); |
|
1181 | + } |
|
1182 | + if (isset($query_parameters['caps'])) { |
|
1183 | + $model_query_params['caps'] = $this->validateContext($query_parameters['caps']); |
|
1184 | + } else { |
|
1185 | + $model_query_params['caps'] = EEM_Base::caps_read; |
|
1186 | + } |
|
1187 | + if (isset($query_parameters['default_where_conditions'])) { |
|
1188 | + $model_query_params['default_where_conditions'] = $this->validateDefaultQueryParams( |
|
1189 | + $query_parameters['default_where_conditions'] |
|
1190 | + ); |
|
1191 | + } |
|
1192 | + return apply_filters('FHEE__Read__create_model_query_params', $model_query_params, $query_parameters, $model); |
|
1193 | + } |
|
1194 | + |
|
1195 | + |
|
1196 | + /** |
|
1197 | + * Changes the REST-style query params for use in the models |
|
1198 | + * |
|
1199 | + * @deprecated |
|
1200 | + * @param EEM_Base $model |
|
1201 | + * @param array $query_params sub-array from @see EEM_Base::get_all() |
|
1202 | + * @return array |
|
1203 | + */ |
|
1204 | + public function prepareRestQueryParamsKeyForModels($model, $query_params) |
|
1205 | + { |
|
1206 | + $model_ready_query_params = array(); |
|
1207 | + foreach ($query_params as $key => $value) { |
|
1208 | + if (is_array($value)) { |
|
1209 | + $model_ready_query_params[ $key ] = $this->prepareRestQueryParamsKeyForModels($model, $value); |
|
1210 | + } else { |
|
1211 | + $model_ready_query_params[ $key ] = $value; |
|
1212 | + } |
|
1213 | + } |
|
1214 | + return $model_ready_query_params; |
|
1215 | + } |
|
1216 | + |
|
1217 | + |
|
1218 | + /** |
|
1219 | + * @deprecated instead use ModelDataTranslator::prepareFieldValuesFromJson() |
|
1220 | + * @param $model |
|
1221 | + * @param $query_params |
|
1222 | + * @return array |
|
1223 | + */ |
|
1224 | + public function prepareRestQueryParamsValuesForModels($model, $query_params) |
|
1225 | + { |
|
1226 | + $model_ready_query_params = array(); |
|
1227 | + foreach ($query_params as $key => $value) { |
|
1228 | + if (is_array($value)) { |
|
1229 | + $model_ready_query_params[ $key ] = $this->prepareRestQueryParamsValuesForModels($model, $value); |
|
1230 | + } else { |
|
1231 | + $model_ready_query_params[ $key ] = $value; |
|
1232 | + } |
|
1233 | + } |
|
1234 | + return $model_ready_query_params; |
|
1235 | + } |
|
1236 | + |
|
1237 | + |
|
1238 | + /** |
|
1239 | + * Explodes the string on commas, and only returns items with $prefix followed by a period. |
|
1240 | + * If no prefix is specified, returns items with no period. |
|
1241 | + * |
|
1242 | + * @param string|array $string_to_explode eg "jibba,jabba, blah, blah, blah" or array('jibba', 'jabba' ) |
|
1243 | + * @param string $prefix "Event" or "foobar" |
|
1244 | + * @return array $string_to_exploded exploded on COMMAS, and if a prefix was specified |
|
1245 | + * we only return strings starting with that and a period; if no prefix was |
|
1246 | + * specified we return all items containing NO periods |
|
1247 | + */ |
|
1248 | + public function explodeAndGetItemsPrefixedWith($string_to_explode, $prefix) |
|
1249 | + { |
|
1250 | + if (is_string($string_to_explode)) { |
|
1251 | + $exploded_contents = explode(',', $string_to_explode); |
|
1252 | + } elseif (is_array($string_to_explode)) { |
|
1253 | + $exploded_contents = $string_to_explode; |
|
1254 | + } else { |
|
1255 | + $exploded_contents = array(); |
|
1256 | + } |
|
1257 | + // if the string was empty, we want an empty array |
|
1258 | + $exploded_contents = array_filter($exploded_contents); |
|
1259 | + $contents_with_prefix = array(); |
|
1260 | + foreach ($exploded_contents as $item) { |
|
1261 | + $item = trim($item); |
|
1262 | + // if no prefix was provided, so we look for items with no "." in them |
|
1263 | + if (! $prefix) { |
|
1264 | + // does this item have a period? |
|
1265 | + if (strpos($item, '.') === false) { |
|
1266 | + // if not, then its what we're looking for |
|
1267 | + $contents_with_prefix[] = $item; |
|
1268 | + } |
|
1269 | + } elseif (strpos($item, $prefix . '.') === 0) { |
|
1270 | + // this item has the prefix and a period, grab it |
|
1271 | + $contents_with_prefix[] = substr( |
|
1272 | + $item, |
|
1273 | + strpos($item, $prefix . '.') + strlen($prefix . '.') |
|
1274 | + ); |
|
1275 | + } elseif ($item === $prefix) { |
|
1276 | + // this item is JUST the prefix |
|
1277 | + // so let's grab everything after, which is a blank string |
|
1278 | + $contents_with_prefix[] = ''; |
|
1279 | + } |
|
1280 | + } |
|
1281 | + return $contents_with_prefix; |
|
1282 | + } |
|
1283 | + |
|
1284 | + |
|
1285 | + /** |
|
1286 | + * @deprecated since 4.8.36.rc.001 You should instead use Read::explode_and_get_items_prefixed_with. |
|
1287 | + * Deprecated because its return values were really quite confusing- sometimes it returned |
|
1288 | + * an empty array (when the include string was blank or '*') or sometimes it returned |
|
1289 | + * array('*') (when you provided a model and a model of that kind was found). |
|
1290 | + * Parses the $include_string so we fetch all the field names relating to THIS model |
|
1291 | + * (ie have NO period in them), or for the provided model (ie start with the model |
|
1292 | + * name and then a period). |
|
1293 | + * @param string $include_string @see Read:handle_request_get_all |
|
1294 | + * @param string $model_name |
|
1295 | + * @return array of fields for this model. If $model_name is provided, then |
|
1296 | + * the fields for that model, with the model's name removed from each. |
|
1297 | + * If $include_string was blank or '*' returns an empty array |
|
1298 | + */ |
|
1299 | + public function extractIncludesForThisModel($include_string, $model_name = null) |
|
1300 | + { |
|
1301 | + if (is_array($include_string)) { |
|
1302 | + $include_string = implode(',', $include_string); |
|
1303 | + } |
|
1304 | + if ($include_string === '*' || $include_string === '') { |
|
1305 | + return array(); |
|
1306 | + } |
|
1307 | + $includes = explode(',', $include_string); |
|
1308 | + $extracted_fields_to_include = array(); |
|
1309 | + if ($model_name) { |
|
1310 | + foreach ($includes as $field_to_include) { |
|
1311 | + $field_to_include = trim($field_to_include); |
|
1312 | + if (strpos($field_to_include, $model_name . '.') === 0) { |
|
1313 | + // found the model name at the exact start |
|
1314 | + $field_sans_model_name = str_replace($model_name . '.', '', $field_to_include); |
|
1315 | + $extracted_fields_to_include[] = $field_sans_model_name; |
|
1316 | + } elseif ($field_to_include == $model_name) { |
|
1317 | + $extracted_fields_to_include[] = '*'; |
|
1318 | + } |
|
1319 | + } |
|
1320 | + } else { |
|
1321 | + // look for ones with no period |
|
1322 | + foreach ($includes as $field_to_include) { |
|
1323 | + $field_to_include = trim($field_to_include); |
|
1324 | + if (strpos($field_to_include, '.') === false |
|
1325 | + && ! $this->getModelVersionInfo()->isModelNameInThisVersion($field_to_include) |
|
1326 | + ) { |
|
1327 | + $extracted_fields_to_include[] = $field_to_include; |
|
1328 | + } |
|
1329 | + } |
|
1330 | + } |
|
1331 | + return $extracted_fields_to_include; |
|
1332 | + } |
|
1333 | + |
|
1334 | + |
|
1335 | + /** |
|
1336 | + * Gets the single item using the model according to the request in the context given, otherwise |
|
1337 | + * returns that it's inaccessible to the current user |
|
1338 | + * |
|
1339 | + * @param EEM_Base $model |
|
1340 | + * @param WP_REST_Request $request |
|
1341 | + * @param null $context |
|
1342 | + * @return array|WP_Error |
|
1343 | + */ |
|
1344 | + public function getOneOrReportPermissionError(EEM_Base $model, WP_REST_Request $request, $context = null) |
|
1345 | + { |
|
1346 | + $query_params = array(array($model->primary_key_name() => $request->get_param('id')), 'limit' => 1); |
|
1347 | + if ($model instanceof \EEM_Soft_Delete_Base) { |
|
1348 | + $query_params = $model->alter_query_params_so_deleted_and_undeleted_items_included($query_params); |
|
1349 | + } |
|
1350 | + $restricted_query_params = $query_params; |
|
1351 | + $restricted_query_params['caps'] = $context; |
|
1352 | + $this->setDebugInfo('model query params', $restricted_query_params); |
|
1353 | + $model_rows = $model->get_all_wpdb_results($restricted_query_params); |
|
1354 | + if (! empty($model_rows)) { |
|
1355 | + return $this->createEntityFromWpdbResult( |
|
1356 | + $model, |
|
1357 | + array_shift($model_rows), |
|
1358 | + $request |
|
1359 | + ); |
|
1360 | + } else { |
|
1361 | + // ok let's test to see if we WOULD have found it, had we not had restrictions from missing capabilities |
|
1362 | + $lowercase_model_name = strtolower($model->get_this_model_name()); |
|
1363 | + $model_rows_found_sans_restrictions = $model->get_all_wpdb_results($query_params); |
|
1364 | + if (! empty($model_rows_found_sans_restrictions)) { |
|
1365 | + // you got shafted- it existed but we didn't want to tell you! |
|
1366 | + return new WP_Error( |
|
1367 | + 'rest_user_cannot_' . $context, |
|
1368 | + sprintf( |
|
1369 | + __('Sorry, you cannot %1$s this %2$s. Missing permissions are: %3$s', 'event_espresso'), |
|
1370 | + $context, |
|
1371 | + strtolower($model->get_this_model_name()), |
|
1372 | + Capabilities::getMissingPermissionsString( |
|
1373 | + $model, |
|
1374 | + $context |
|
1375 | + ) |
|
1376 | + ), |
|
1377 | + array('status' => 403) |
|
1378 | + ); |
|
1379 | + } else { |
|
1380 | + // it's not you. It just doesn't exist |
|
1381 | + return new WP_Error( |
|
1382 | + sprintf('rest_%s_invalid_id', $lowercase_model_name), |
|
1383 | + sprintf(__('Invalid %s ID.', 'event_espresso'), $lowercase_model_name), |
|
1384 | + array('status' => 404) |
|
1385 | + ); |
|
1386 | + } |
|
1387 | + } |
|
1388 | + } |
|
1389 | 1389 | } |
@@ -73,7 +73,7 @@ discard block |
||
73 | 73 | $controller = LoaderFactory::getLoader()->getNew('EventEspresso\core\libraries\rest_api\controllers\model\Read'); |
74 | 74 | try { |
75 | 75 | $controller->setRequestedVersion($version); |
76 | - if (! $controller->getModelVersionInfo()->isModelNameInThisVersion($model_name)) { |
|
76 | + if ( ! $controller->getModelVersionInfo()->isModelNameInThisVersion($model_name)) { |
|
77 | 77 | return $controller->sendResponse( |
78 | 78 | new WP_Error( |
79 | 79 | 'endpoint_parsing_error', |
@@ -114,7 +114,7 @@ discard block |
||
114 | 114 | $controller = LoaderFactory::getLoader()->getNew('EventEspresso\core\libraries\rest_api\controllers\model\Read'); |
115 | 115 | try { |
116 | 116 | $controller->setRequestedVersion($version); |
117 | - if (! $controller->getModelVersionInfo()->isModelNameInThisVersion($model_name)) { |
|
117 | + if ( ! $controller->getModelVersionInfo()->isModelNameInThisVersion($model_name)) { |
|
118 | 118 | return array(); |
119 | 119 | } |
120 | 120 | // get the model for this version |
@@ -171,11 +171,11 @@ discard block |
||
171 | 171 | */ |
172 | 172 | protected function translateDefaultsForRestResponse($field_name, EE_Model_Field_Base $field, array $schema) |
173 | 173 | { |
174 | - if (isset($schema['properties'][ $field_name ]['default'])) { |
|
175 | - if (is_array($schema['properties'][ $field_name ]['default'])) { |
|
176 | - foreach ($schema['properties'][ $field_name ]['default'] as $default_key => $default_value) { |
|
174 | + if (isset($schema['properties'][$field_name]['default'])) { |
|
175 | + if (is_array($schema['properties'][$field_name]['default'])) { |
|
176 | + foreach ($schema['properties'][$field_name]['default'] as $default_key => $default_value) { |
|
177 | 177 | if ($default_key === 'raw') { |
178 | - $schema['properties'][ $field_name ]['default'][ $default_key ] = |
|
178 | + $schema['properties'][$field_name]['default'][$default_key] = |
|
179 | 179 | ModelDataTranslator::prepareFieldValueForJson( |
180 | 180 | $field, |
181 | 181 | $default_value, |
@@ -184,9 +184,9 @@ discard block |
||
184 | 184 | } |
185 | 185 | } |
186 | 186 | } else { |
187 | - $schema['properties'][ $field_name ]['default'] = ModelDataTranslator::prepareFieldValueForJson( |
|
187 | + $schema['properties'][$field_name]['default'] = ModelDataTranslator::prepareFieldValueForJson( |
|
188 | 188 | $field, |
189 | - $schema['properties'][ $field_name ]['default'], |
|
189 | + $schema['properties'][$field_name]['default'], |
|
190 | 190 | $this->getModelVersionInfo()->requestedVersion() |
191 | 191 | ); |
192 | 192 | } |
@@ -208,9 +208,9 @@ discard block |
||
208 | 208 | protected function maybeAddExtraFieldsToSchema($field_name, EE_Model_Field_Base $field, array $schema) |
209 | 209 | { |
210 | 210 | if ($field instanceof EE_Datetime_Field) { |
211 | - $schema['properties'][ $field_name . '_gmt' ] = $field->getSchema(); |
|
211 | + $schema['properties'][$field_name.'_gmt'] = $field->getSchema(); |
|
212 | 212 | // modify the description |
213 | - $schema['properties'][ $field_name . '_gmt' ]['description'] = sprintf( |
|
213 | + $schema['properties'][$field_name.'_gmt']['description'] = sprintf( |
|
214 | 214 | esc_html__('%s - the value for this field is in GMT.', 'event_espresso'), |
215 | 215 | wp_specialchars_decode($field->get_nicename(), ENT_QUOTES) |
216 | 216 | ); |
@@ -253,7 +253,7 @@ discard block |
||
253 | 253 | $controller = LoaderFactory::getLoader()->getNew('EventEspresso\core\libraries\rest_api\controllers\model\Read'); |
254 | 254 | try { |
255 | 255 | $controller->setRequestedVersion($version); |
256 | - if (! $controller->getModelVersionInfo()->isModelNameInThisVersion($model_name)) { |
|
256 | + if ( ! $controller->getModelVersionInfo()->isModelNameInThisVersion($model_name)) { |
|
257 | 257 | return $controller->sendResponse( |
258 | 258 | new WP_Error( |
259 | 259 | 'endpoint_parsing_error', |
@@ -301,7 +301,7 @@ discard block |
||
301 | 301 | $controller = LoaderFactory::getLoader()->getNew('EventEspresso\core\libraries\rest_api\controllers\model\Read'); |
302 | 302 | try { |
303 | 303 | $controller->setRequestedVersion($version); |
304 | - if (! $controller->getModelVersionInfo()->isModelNameInThisVersion($model_name)) { |
|
304 | + if ( ! $controller->getModelVersionInfo()->isModelNameInThisVersion($model_name)) { |
|
305 | 305 | return $controller->sendResponse( |
306 | 306 | new WP_Error( |
307 | 307 | 'endpoint_parsing_error', |
@@ -316,7 +316,7 @@ discard block |
||
316 | 316 | ); |
317 | 317 | } |
318 | 318 | $main_model = $controller->getModelVersionInfo()->loadModel($model_name); |
319 | - if (! $controller->getModelVersionInfo()->isModelNameInThisVersion($related_model_name)) { |
|
319 | + if ( ! $controller->getModelVersionInfo()->isModelNameInThisVersion($related_model_name)) { |
|
320 | 320 | return $controller->sendResponse( |
321 | 321 | new WP_Error( |
322 | 322 | 'endpoint_parsing_error', |
@@ -353,7 +353,7 @@ discard block |
||
353 | 353 | public function getEntitiesFromModel($model, $request) |
354 | 354 | { |
355 | 355 | $query_params = $this->createModelQueryParams($model, $request->get_params()); |
356 | - if (! Capabilities::currentUserHasPartialAccessTo($model, $query_params['caps'])) { |
|
356 | + if ( ! Capabilities::currentUserHasPartialAccessTo($model, $query_params['caps'])) { |
|
357 | 357 | $model_name_plural = EEH_Inflector::pluralize_and_lower($model->get_this_model_name()); |
358 | 358 | return new WP_Error( |
359 | 359 | sprintf('rest_%s_cannot_list', $model_name_plural), |
@@ -365,7 +365,7 @@ discard block |
||
365 | 365 | array('status' => 403) |
366 | 366 | ); |
367 | 367 | } |
368 | - if (! $request->get_header('no_rest_headers')) { |
|
368 | + if ( ! $request->get_header('no_rest_headers')) { |
|
369 | 369 | $this->setHeadersFromQueryParams($model, $query_params); |
370 | 370 | } |
371 | 371 | /** @type array $results */ |
@@ -400,7 +400,7 @@ discard block |
||
400 | 400 | $context = $this->validateContext($request->get_param('caps')); |
401 | 401 | $model = $relation->get_this_model(); |
402 | 402 | $related_model = $relation->get_other_model(); |
403 | - if (! isset($primary_model_query_params[0])) { |
|
403 | + if ( ! isset($primary_model_query_params[0])) { |
|
404 | 404 | $primary_model_query_params[0] = array(); |
405 | 405 | } |
406 | 406 | // check if they can access the 1st model object |
@@ -417,7 +417,7 @@ discard block |
||
417 | 417 | $restricted_query_params['caps'] = $context; |
418 | 418 | $this->setDebugInfo('main model query params', $restricted_query_params); |
419 | 419 | $this->setDebugInfo('missing caps', Capabilities::getMissingPermissionsString($related_model, $context)); |
420 | - if (! ( |
|
420 | + if ( ! ( |
|
421 | 421 | Capabilities::currentUserHasPartialAccessTo($related_model, $context) |
422 | 422 | && $model->exists($restricted_query_params) |
423 | 423 | ) |
@@ -450,13 +450,13 @@ discard block |
||
450 | 450 | } |
451 | 451 | $query_params = $this->createModelQueryParams($relation->get_other_model(), $request->get_params()); |
452 | 452 | foreach ($primary_model_query_params[0] as $where_condition_key => $where_condition_value) { |
453 | - $query_params[0][ $relation->get_this_model()->get_this_model_name() |
|
453 | + $query_params[0][$relation->get_this_model()->get_this_model_name() |
|
454 | 454 | . '.' |
455 | - . $where_condition_key ] = $where_condition_value; |
|
455 | + . $where_condition_key] = $where_condition_value; |
|
456 | 456 | } |
457 | 457 | $query_params['default_where_conditions'] = 'none'; |
458 | 458 | $query_params['caps'] = $context; |
459 | - if (! $request->get_header('no_rest_headers')) { |
|
459 | + if ( ! $request->get_header('no_rest_headers')) { |
|
460 | 460 | $this->setHeadersFromQueryParams($relation->get_other_model(), $query_params); |
461 | 461 | } |
462 | 462 | /** @type array $results */ |
@@ -507,7 +507,7 @@ discard block |
||
507 | 507 | */ |
508 | 508 | public function getEntitiesFromRelation($id, $relation, $request) |
509 | 509 | { |
510 | - if (! $relation->get_this_model()->has_primary_key_field()) { |
|
510 | + if ( ! $relation->get_this_model()->has_primary_key_field()) { |
|
511 | 511 | throw new EE_Error( |
512 | 512 | sprintf( |
513 | 513 | __( |
@@ -549,7 +549,7 @@ discard block |
||
549 | 549 | Capabilities::getMissingPermissionsString($model, $query_params['caps']) |
550 | 550 | ); |
551 | 551 | // normally the limit to a 2-part array, where the 2nd item is the limit |
552 | - if (! isset($query_params['limit'])) { |
|
552 | + if ( ! isset($query_params['limit'])) { |
|
553 | 553 | $query_params['limit'] = EED_Core_Rest_Api::get_default_query_limit(); |
554 | 554 | } |
555 | 555 | if (is_array($query_params['limit'])) { |
@@ -582,7 +582,7 @@ discard block |
||
582 | 582 | */ |
583 | 583 | public function createEntityFromWpdbResult($model, $db_row, $rest_request, $deprecated = null) |
584 | 584 | { |
585 | - if (! $rest_request instanceof WP_REST_Request) { |
|
585 | + if ( ! $rest_request instanceof WP_REST_Request) { |
|
586 | 586 | // ok so this was called in the old style, where the 3rd arg was |
587 | 587 | // $include, and the 4th arg was $context |
588 | 588 | // now setup the request just to avoid fatal errors, although we won't be able |
@@ -663,8 +663,8 @@ discard block |
||
663 | 663 | if ($do_chevy_shuffle) { |
664 | 664 | global $post; |
665 | 665 | $old_post = $post; |
666 | - $post = get_post($result[ $model->primary_key_name() ]); |
|
667 | - if (! $post instanceof \WP_Post) { |
|
666 | + $post = get_post($result[$model->primary_key_name()]); |
|
667 | + if ( ! $post instanceof \WP_Post) { |
|
668 | 668 | // well that's weird, because $result is what we JUST fetched from the database |
669 | 669 | throw new RestException( |
670 | 670 | 'error_fetching_post_from_database_results', |
@@ -674,7 +674,7 @@ discard block |
||
674 | 674 | ) |
675 | 675 | ); |
676 | 676 | } |
677 | - $model_object_classname = 'EE_' . $model->get_this_model_name(); |
|
677 | + $model_object_classname = 'EE_'.$model->get_this_model_name(); |
|
678 | 678 | $post->{$model_object_classname} = \EE_Registry::instance()->load_class( |
679 | 679 | $model_object_classname, |
680 | 680 | $result, |
@@ -685,13 +685,13 @@ discard block |
||
685 | 685 | foreach ($result as $field_name => $field_value) { |
686 | 686 | $field_obj = $model->field_settings_for($field_name); |
687 | 687 | if ($this->isSubclassOfOne($field_obj, $this->getModelVersionInfo()->fieldsIgnored())) { |
688 | - unset($result[ $field_name ]); |
|
688 | + unset($result[$field_name]); |
|
689 | 689 | } elseif ($this->isSubclassOfOne( |
690 | 690 | $field_obj, |
691 | 691 | $this->getModelVersionInfo()->fieldsThatHaveRenderedFormat() |
692 | 692 | ) |
693 | 693 | ) { |
694 | - $result[ $field_name ] = array( |
|
694 | + $result[$field_name] = array( |
|
695 | 695 | 'raw' => $this->prepareFieldObjValueForJson($field_obj, $field_value), |
696 | 696 | 'rendered' => $this->prepareFieldObjValueForJson($field_obj, $field_value, 'pretty'), |
697 | 697 | ); |
@@ -700,7 +700,7 @@ discard block |
||
700 | 700 | $this->getModelVersionInfo()->fieldsThatHavePrettyFormat() |
701 | 701 | ) |
702 | 702 | ) { |
703 | - $result[ $field_name ] = array( |
|
703 | + $result[$field_name] = array( |
|
704 | 704 | 'raw' => $this->prepareFieldObjValueForJson($field_obj, $field_value), |
705 | 705 | 'pretty' => $this->prepareFieldObjValueForJson($field_obj, $field_value, 'pretty'), |
706 | 706 | ); |
@@ -731,10 +731,10 @@ discard block |
||
731 | 731 | $this->getModelVersionInfo()->requestedVersion() |
732 | 732 | ); |
733 | 733 | } |
734 | - $result[ $field_name . '_gmt' ] = $gmt_date; |
|
735 | - $result[ $field_name ] = $local_date; |
|
734 | + $result[$field_name.'_gmt'] = $gmt_date; |
|
735 | + $result[$field_name] = $local_date; |
|
736 | 736 | } else { |
737 | - $result[ $field_name ] = $this->prepareFieldObjValueForJson($field_obj, $field_value); |
|
737 | + $result[$field_name] = $this->prepareFieldObjValueForJson($field_obj, $field_value); |
|
738 | 738 | } |
739 | 739 | } |
740 | 740 | if ($do_chevy_shuffle) { |
@@ -786,7 +786,7 @@ discard block |
||
786 | 786 | protected function addExtraFields(EEM_Base $model, $db_row, $entity_array) |
787 | 787 | { |
788 | 788 | if ($model instanceof EEM_CPT_Base) { |
789 | - $entity_array['link'] = get_permalink($db_row[ $model->get_primary_key_field()->get_qualified_column() ]); |
|
789 | + $entity_array['link'] = get_permalink($db_row[$model->get_primary_key_field()->get_qualified_column()]); |
|
790 | 790 | } |
791 | 791 | return $entity_array; |
792 | 792 | } |
@@ -811,7 +811,7 @@ discard block |
||
811 | 811 | 'href' => $this->getVersionedLinkTo( |
812 | 812 | EEH_Inflector::pluralize_and_lower($model->get_this_model_name()) |
813 | 813 | . '/' |
814 | - . $entity_array[ $model->primary_key_name() ] |
|
814 | + . $entity_array[$model->primary_key_name()] |
|
815 | 815 | ), |
816 | 816 | ), |
817 | 817 | ); |
@@ -827,12 +827,12 @@ discard block |
||
827 | 827 | if ($model->has_primary_key_field()) { |
828 | 828 | foreach ($this->getModelVersionInfo()->relationSettings($model) as $relation_name => $relation_obj) { |
829 | 829 | $related_model_part = Read::getRelatedEntityName($relation_name, $relation_obj); |
830 | - $links[ EED_Core_Rest_Api::ee_api_link_namespace . $related_model_part ] = array( |
|
830 | + $links[EED_Core_Rest_Api::ee_api_link_namespace.$related_model_part] = array( |
|
831 | 831 | array( |
832 | 832 | 'href' => $this->getVersionedLinkTo( |
833 | 833 | EEH_Inflector::pluralize_and_lower($model->get_this_model_name()) |
834 | 834 | . '/' |
835 | - . $entity_array[ $model->primary_key_name() ] |
|
835 | + . $entity_array[$model->primary_key_name()] |
|
836 | 836 | . '/' |
837 | 837 | . $related_model_part |
838 | 838 | ), |
@@ -861,13 +861,13 @@ discard block |
||
861 | 861 | $db_row = array() |
862 | 862 | ) { |
863 | 863 | // if $db_row not included, hope the entity array has what we need |
864 | - if (! $db_row) { |
|
864 | + if ( ! $db_row) { |
|
865 | 865 | $db_row = $entity_array; |
866 | 866 | } |
867 | 867 | $includes_for_this_model = $this->explodeAndGetItemsPrefixedWith($rest_request->get_param('include'), ''); |
868 | 868 | $includes_for_this_model = $this->removeModelNamesFromArray($includes_for_this_model); |
869 | 869 | // if they passed in * or didn't specify any includes, return everything |
870 | - if (! in_array('*', $includes_for_this_model) |
|
870 | + if ( ! in_array('*', $includes_for_this_model) |
|
871 | 871 | && ! empty($includes_for_this_model) |
872 | 872 | ) { |
873 | 873 | if ($model->has_primary_key_field()) { |
@@ -913,7 +913,7 @@ discard block |
||
913 | 913 | $relation_obj, |
914 | 914 | $pretend_related_request |
915 | 915 | ); |
916 | - $entity_array[ Read::getRelatedEntityName($relation_name, $relation_obj) ] = $related_results |
|
916 | + $entity_array[Read::getRelatedEntityName($relation_name, $relation_obj)] = $related_results |
|
917 | 917 | instanceof |
918 | 918 | WP_Error |
919 | 919 | ? null |
@@ -1045,7 +1045,7 @@ discard block |
||
1045 | 1045 | */ |
1046 | 1046 | public function validateContext($context) |
1047 | 1047 | { |
1048 | - if (! $context) { |
|
1048 | + if ( ! $context) { |
|
1049 | 1049 | $context = EEM_Base::caps_read; |
1050 | 1050 | } |
1051 | 1051 | $valid_contexts = EEM_Base::valid_cap_contexts(); |
@@ -1070,7 +1070,7 @@ discard block |
||
1070 | 1070 | EEM_Base::default_where_conditions_minimum_all, |
1071 | 1071 | EEM_Base::default_where_conditions_minimum_others, |
1072 | 1072 | ); |
1073 | - if (! $default_query_params) { |
|
1073 | + if ( ! $default_query_params) { |
|
1074 | 1074 | $default_query_params = EEM_Base::default_where_conditions_all; |
1075 | 1075 | } |
1076 | 1076 | if (in_array( |
@@ -1153,14 +1153,14 @@ discard block |
||
1153 | 1153 | } |
1154 | 1154 | if (isset($query_parameters['limit'])) { |
1155 | 1155 | // limit should be either a string like '23' or '23,43', or an array with two items in it |
1156 | - if (! is_array($query_parameters['limit'])) { |
|
1156 | + if ( ! is_array($query_parameters['limit'])) { |
|
1157 | 1157 | $limit_array = explode(',', (string) $query_parameters['limit']); |
1158 | 1158 | } else { |
1159 | 1159 | $limit_array = $query_parameters['limit']; |
1160 | 1160 | } |
1161 | 1161 | $sanitized_limit = array(); |
1162 | 1162 | foreach ($limit_array as $key => $limit_part) { |
1163 | - if ($this->debug_mode && (! is_numeric($limit_part) || count($sanitized_limit) > 2)) { |
|
1163 | + if ($this->debug_mode && ( ! is_numeric($limit_part) || count($sanitized_limit) > 2)) { |
|
1164 | 1164 | throw new EE_Error( |
1165 | 1165 | sprintf( |
1166 | 1166 | __( |
@@ -1206,9 +1206,9 @@ discard block |
||
1206 | 1206 | $model_ready_query_params = array(); |
1207 | 1207 | foreach ($query_params as $key => $value) { |
1208 | 1208 | if (is_array($value)) { |
1209 | - $model_ready_query_params[ $key ] = $this->prepareRestQueryParamsKeyForModels($model, $value); |
|
1209 | + $model_ready_query_params[$key] = $this->prepareRestQueryParamsKeyForModels($model, $value); |
|
1210 | 1210 | } else { |
1211 | - $model_ready_query_params[ $key ] = $value; |
|
1211 | + $model_ready_query_params[$key] = $value; |
|
1212 | 1212 | } |
1213 | 1213 | } |
1214 | 1214 | return $model_ready_query_params; |
@@ -1226,9 +1226,9 @@ discard block |
||
1226 | 1226 | $model_ready_query_params = array(); |
1227 | 1227 | foreach ($query_params as $key => $value) { |
1228 | 1228 | if (is_array($value)) { |
1229 | - $model_ready_query_params[ $key ] = $this->prepareRestQueryParamsValuesForModels($model, $value); |
|
1229 | + $model_ready_query_params[$key] = $this->prepareRestQueryParamsValuesForModels($model, $value); |
|
1230 | 1230 | } else { |
1231 | - $model_ready_query_params[ $key ] = $value; |
|
1231 | + $model_ready_query_params[$key] = $value; |
|
1232 | 1232 | } |
1233 | 1233 | } |
1234 | 1234 | return $model_ready_query_params; |
@@ -1260,17 +1260,17 @@ discard block |
||
1260 | 1260 | foreach ($exploded_contents as $item) { |
1261 | 1261 | $item = trim($item); |
1262 | 1262 | // if no prefix was provided, so we look for items with no "." in them |
1263 | - if (! $prefix) { |
|
1263 | + if ( ! $prefix) { |
|
1264 | 1264 | // does this item have a period? |
1265 | 1265 | if (strpos($item, '.') === false) { |
1266 | 1266 | // if not, then its what we're looking for |
1267 | 1267 | $contents_with_prefix[] = $item; |
1268 | 1268 | } |
1269 | - } elseif (strpos($item, $prefix . '.') === 0) { |
|
1269 | + } elseif (strpos($item, $prefix.'.') === 0) { |
|
1270 | 1270 | // this item has the prefix and a period, grab it |
1271 | 1271 | $contents_with_prefix[] = substr( |
1272 | 1272 | $item, |
1273 | - strpos($item, $prefix . '.') + strlen($prefix . '.') |
|
1273 | + strpos($item, $prefix.'.') + strlen($prefix.'.') |
|
1274 | 1274 | ); |
1275 | 1275 | } elseif ($item === $prefix) { |
1276 | 1276 | // this item is JUST the prefix |
@@ -1309,9 +1309,9 @@ discard block |
||
1309 | 1309 | if ($model_name) { |
1310 | 1310 | foreach ($includes as $field_to_include) { |
1311 | 1311 | $field_to_include = trim($field_to_include); |
1312 | - if (strpos($field_to_include, $model_name . '.') === 0) { |
|
1312 | + if (strpos($field_to_include, $model_name.'.') === 0) { |
|
1313 | 1313 | // found the model name at the exact start |
1314 | - $field_sans_model_name = str_replace($model_name . '.', '', $field_to_include); |
|
1314 | + $field_sans_model_name = str_replace($model_name.'.', '', $field_to_include); |
|
1315 | 1315 | $extracted_fields_to_include[] = $field_sans_model_name; |
1316 | 1316 | } elseif ($field_to_include == $model_name) { |
1317 | 1317 | $extracted_fields_to_include[] = '*'; |
@@ -1351,7 +1351,7 @@ discard block |
||
1351 | 1351 | $restricted_query_params['caps'] = $context; |
1352 | 1352 | $this->setDebugInfo('model query params', $restricted_query_params); |
1353 | 1353 | $model_rows = $model->get_all_wpdb_results($restricted_query_params); |
1354 | - if (! empty($model_rows)) { |
|
1354 | + if ( ! empty($model_rows)) { |
|
1355 | 1355 | return $this->createEntityFromWpdbResult( |
1356 | 1356 | $model, |
1357 | 1357 | array_shift($model_rows), |
@@ -1361,10 +1361,10 @@ discard block |
||
1361 | 1361 | // ok let's test to see if we WOULD have found it, had we not had restrictions from missing capabilities |
1362 | 1362 | $lowercase_model_name = strtolower($model->get_this_model_name()); |
1363 | 1363 | $model_rows_found_sans_restrictions = $model->get_all_wpdb_results($query_params); |
1364 | - if (! empty($model_rows_found_sans_restrictions)) { |
|
1364 | + if ( ! empty($model_rows_found_sans_restrictions)) { |
|
1365 | 1365 | // you got shafted- it existed but we didn't want to tell you! |
1366 | 1366 | return new WP_Error( |
1367 | - 'rest_user_cannot_' . $context, |
|
1367 | + 'rest_user_cannot_'.$context, |
|
1368 | 1368 | sprintf( |
1369 | 1369 | __('Sorry, you cannot %1$s this %2$s. Missing permissions are: %3$s', 'event_espresso'), |
1370 | 1370 | $context, |
@@ -38,103 +38,103 @@ |
||
38 | 38 | * @since 4.0 |
39 | 39 | */ |
40 | 40 | if (function_exists('espresso_version')) { |
41 | - if (! function_exists('espresso_duplicate_plugin_error')) { |
|
42 | - /** |
|
43 | - * espresso_duplicate_plugin_error |
|
44 | - * displays if more than one version of EE is activated at the same time |
|
45 | - */ |
|
46 | - function espresso_duplicate_plugin_error() |
|
47 | - { |
|
48 | - ?> |
|
41 | + if (! function_exists('espresso_duplicate_plugin_error')) { |
|
42 | + /** |
|
43 | + * espresso_duplicate_plugin_error |
|
44 | + * displays if more than one version of EE is activated at the same time |
|
45 | + */ |
|
46 | + function espresso_duplicate_plugin_error() |
|
47 | + { |
|
48 | + ?> |
|
49 | 49 | <div class="error"> |
50 | 50 | <p> |
51 | 51 | <?php |
52 | - echo esc_html__( |
|
53 | - 'Can not run multiple versions of Event Espresso! One version has been automatically deactivated. Please verify that you have the correct version you want still active.', |
|
54 | - 'event_espresso' |
|
55 | - ); ?> |
|
52 | + echo esc_html__( |
|
53 | + 'Can not run multiple versions of Event Espresso! One version has been automatically deactivated. Please verify that you have the correct version you want still active.', |
|
54 | + 'event_espresso' |
|
55 | + ); ?> |
|
56 | 56 | </p> |
57 | 57 | </div> |
58 | 58 | <?php |
59 | - espresso_deactivate_plugin(plugin_basename(__FILE__)); |
|
60 | - } |
|
61 | - } |
|
62 | - add_action('admin_notices', 'espresso_duplicate_plugin_error', 1); |
|
59 | + espresso_deactivate_plugin(plugin_basename(__FILE__)); |
|
60 | + } |
|
61 | + } |
|
62 | + add_action('admin_notices', 'espresso_duplicate_plugin_error', 1); |
|
63 | 63 | } else { |
64 | - define('EE_MIN_PHP_VER_REQUIRED', '5.4.0'); |
|
65 | - if (! version_compare(PHP_VERSION, EE_MIN_PHP_VER_REQUIRED, '>=')) { |
|
66 | - /** |
|
67 | - * espresso_minimum_php_version_error |
|
68 | - * |
|
69 | - * @return void |
|
70 | - */ |
|
71 | - function espresso_minimum_php_version_error() |
|
72 | - { |
|
73 | - ?> |
|
64 | + define('EE_MIN_PHP_VER_REQUIRED', '5.4.0'); |
|
65 | + if (! version_compare(PHP_VERSION, EE_MIN_PHP_VER_REQUIRED, '>=')) { |
|
66 | + /** |
|
67 | + * espresso_minimum_php_version_error |
|
68 | + * |
|
69 | + * @return void |
|
70 | + */ |
|
71 | + function espresso_minimum_php_version_error() |
|
72 | + { |
|
73 | + ?> |
|
74 | 74 | <div class="error"> |
75 | 75 | <p> |
76 | 76 | <?php |
77 | - printf( |
|
78 | - esc_html__( |
|
79 | - 'We\'re sorry, but Event Espresso requires PHP version %1$s or greater in order to operate. You are currently running version %2$s.%3$sIn order to update your version of PHP, you will need to contact your current hosting provider.%3$sFor information on stable PHP versions, please go to %4$s.', |
|
80 | - 'event_espresso' |
|
81 | - ), |
|
82 | - EE_MIN_PHP_VER_REQUIRED, |
|
83 | - PHP_VERSION, |
|
84 | - '<br/>', |
|
85 | - '<a href="http://php.net/downloads.php">http://php.net/downloads.php</a>' |
|
86 | - ); |
|
87 | - ?> |
|
77 | + printf( |
|
78 | + esc_html__( |
|
79 | + 'We\'re sorry, but Event Espresso requires PHP version %1$s or greater in order to operate. You are currently running version %2$s.%3$sIn order to update your version of PHP, you will need to contact your current hosting provider.%3$sFor information on stable PHP versions, please go to %4$s.', |
|
80 | + 'event_espresso' |
|
81 | + ), |
|
82 | + EE_MIN_PHP_VER_REQUIRED, |
|
83 | + PHP_VERSION, |
|
84 | + '<br/>', |
|
85 | + '<a href="http://php.net/downloads.php">http://php.net/downloads.php</a>' |
|
86 | + ); |
|
87 | + ?> |
|
88 | 88 | </p> |
89 | 89 | </div> |
90 | 90 | <?php |
91 | - espresso_deactivate_plugin(plugin_basename(__FILE__)); |
|
92 | - } |
|
91 | + espresso_deactivate_plugin(plugin_basename(__FILE__)); |
|
92 | + } |
|
93 | 93 | |
94 | - add_action('admin_notices', 'espresso_minimum_php_version_error', 1); |
|
95 | - } else { |
|
96 | - define('EVENT_ESPRESSO_MAIN_FILE', __FILE__); |
|
97 | - /** |
|
98 | - * espresso_version |
|
99 | - * Returns the plugin version |
|
100 | - * |
|
101 | - * @return string |
|
102 | - */ |
|
103 | - function espresso_version() |
|
104 | - { |
|
105 | - return apply_filters('FHEE__espresso__espresso_version', '4.9.68.rc.010'); |
|
106 | - } |
|
94 | + add_action('admin_notices', 'espresso_minimum_php_version_error', 1); |
|
95 | + } else { |
|
96 | + define('EVENT_ESPRESSO_MAIN_FILE', __FILE__); |
|
97 | + /** |
|
98 | + * espresso_version |
|
99 | + * Returns the plugin version |
|
100 | + * |
|
101 | + * @return string |
|
102 | + */ |
|
103 | + function espresso_version() |
|
104 | + { |
|
105 | + return apply_filters('FHEE__espresso__espresso_version', '4.9.68.rc.010'); |
|
106 | + } |
|
107 | 107 | |
108 | - /** |
|
109 | - * espresso_plugin_activation |
|
110 | - * adds a wp-option to indicate that EE has been activated via the WP admin plugins page |
|
111 | - */ |
|
112 | - function espresso_plugin_activation() |
|
113 | - { |
|
114 | - update_option('ee_espresso_activation', true); |
|
115 | - } |
|
108 | + /** |
|
109 | + * espresso_plugin_activation |
|
110 | + * adds a wp-option to indicate that EE has been activated via the WP admin plugins page |
|
111 | + */ |
|
112 | + function espresso_plugin_activation() |
|
113 | + { |
|
114 | + update_option('ee_espresso_activation', true); |
|
115 | + } |
|
116 | 116 | |
117 | - register_activation_hook(EVENT_ESPRESSO_MAIN_FILE, 'espresso_plugin_activation'); |
|
117 | + register_activation_hook(EVENT_ESPRESSO_MAIN_FILE, 'espresso_plugin_activation'); |
|
118 | 118 | |
119 | - require_once __DIR__ . '/core/bootstrap_espresso.php'; |
|
120 | - bootstrap_espresso(); |
|
121 | - } |
|
119 | + require_once __DIR__ . '/core/bootstrap_espresso.php'; |
|
120 | + bootstrap_espresso(); |
|
121 | + } |
|
122 | 122 | } |
123 | 123 | if (! function_exists('espresso_deactivate_plugin')) { |
124 | - /** |
|
125 | - * deactivate_plugin |
|
126 | - * usage: espresso_deactivate_plugin( plugin_basename( __FILE__ )); |
|
127 | - * |
|
128 | - * @access public |
|
129 | - * @param string $plugin_basename - the results of plugin_basename( __FILE__ ) for the plugin's main file |
|
130 | - * @return void |
|
131 | - */ |
|
132 | - function espresso_deactivate_plugin($plugin_basename = '') |
|
133 | - { |
|
134 | - if (! function_exists('deactivate_plugins')) { |
|
135 | - require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
|
136 | - } |
|
137 | - unset($_GET['activate'], $_REQUEST['activate']); |
|
138 | - deactivate_plugins($plugin_basename); |
|
139 | - } |
|
124 | + /** |
|
125 | + * deactivate_plugin |
|
126 | + * usage: espresso_deactivate_plugin( plugin_basename( __FILE__ )); |
|
127 | + * |
|
128 | + * @access public |
|
129 | + * @param string $plugin_basename - the results of plugin_basename( __FILE__ ) for the plugin's main file |
|
130 | + * @return void |
|
131 | + */ |
|
132 | + function espresso_deactivate_plugin($plugin_basename = '') |
|
133 | + { |
|
134 | + if (! function_exists('deactivate_plugins')) { |
|
135 | + require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
|
136 | + } |
|
137 | + unset($_GET['activate'], $_REQUEST['activate']); |
|
138 | + deactivate_plugins($plugin_basename); |
|
139 | + } |
|
140 | 140 | } |
@@ -24,1048 +24,1048 @@ |
||
24 | 24 | { |
25 | 25 | |
26 | 26 | |
27 | - /** |
|
28 | - * @var array |
|
29 | - */ |
|
30 | - private $_status; |
|
27 | + /** |
|
28 | + * @var array |
|
29 | + */ |
|
30 | + private $_status; |
|
31 | 31 | |
32 | 32 | |
33 | - /** |
|
34 | - * An array of transaction details for the related transaction to the registration being processed. |
|
35 | - * This is set via the _set_related_details method. |
|
36 | - * |
|
37 | - * @var array |
|
38 | - */ |
|
39 | - protected $_transaction_details = array(); |
|
33 | + /** |
|
34 | + * An array of transaction details for the related transaction to the registration being processed. |
|
35 | + * This is set via the _set_related_details method. |
|
36 | + * |
|
37 | + * @var array |
|
38 | + */ |
|
39 | + protected $_transaction_details = array(); |
|
40 | 40 | |
41 | 41 | |
42 | - /** |
|
43 | - * An array of event details for the related event to the registration being processed. |
|
44 | - * This is set via the _set_related_details method. |
|
45 | - * |
|
46 | - * @var array |
|
47 | - */ |
|
48 | - protected $_event_details = array(); |
|
42 | + /** |
|
43 | + * An array of event details for the related event to the registration being processed. |
|
44 | + * This is set via the _set_related_details method. |
|
45 | + * |
|
46 | + * @var array |
|
47 | + */ |
|
48 | + protected $_event_details = array(); |
|
49 | 49 | |
50 | 50 | |
51 | - /** |
|
52 | - * @param \Registrations_Admin_Page $admin_page |
|
53 | - */ |
|
54 | - public function __construct(Registrations_Admin_Page $admin_page) |
|
55 | - { |
|
56 | - if (! empty($_GET['event_id'])) { |
|
57 | - $extra_query_args = array(); |
|
58 | - foreach ($admin_page->get_views() as $key => $view_details) { |
|
59 | - $extra_query_args[ $view_details['slug'] ] = array('event_id' => $_GET['event_id']); |
|
60 | - } |
|
61 | - $this->_views = $admin_page->get_list_table_view_RLs($extra_query_args); |
|
62 | - } |
|
63 | - parent::__construct($admin_page); |
|
64 | - $this->_status = $this->_admin_page->get_registration_status_array(); |
|
65 | - } |
|
51 | + /** |
|
52 | + * @param \Registrations_Admin_Page $admin_page |
|
53 | + */ |
|
54 | + public function __construct(Registrations_Admin_Page $admin_page) |
|
55 | + { |
|
56 | + if (! empty($_GET['event_id'])) { |
|
57 | + $extra_query_args = array(); |
|
58 | + foreach ($admin_page->get_views() as $key => $view_details) { |
|
59 | + $extra_query_args[ $view_details['slug'] ] = array('event_id' => $_GET['event_id']); |
|
60 | + } |
|
61 | + $this->_views = $admin_page->get_list_table_view_RLs($extra_query_args); |
|
62 | + } |
|
63 | + parent::__construct($admin_page); |
|
64 | + $this->_status = $this->_admin_page->get_registration_status_array(); |
|
65 | + } |
|
66 | 66 | |
67 | 67 | |
68 | - /** |
|
69 | - * _setup_data |
|
70 | - * |
|
71 | - * @access protected |
|
72 | - * @return void |
|
73 | - */ |
|
74 | - protected function _setup_data() |
|
75 | - { |
|
76 | - $this->_data = $this->_admin_page->get_registrations($this->_per_page); |
|
77 | - $this->_all_data_count = $this->_admin_page->get_registrations($this->_per_page, true, false, false); |
|
78 | - } |
|
68 | + /** |
|
69 | + * _setup_data |
|
70 | + * |
|
71 | + * @access protected |
|
72 | + * @return void |
|
73 | + */ |
|
74 | + protected function _setup_data() |
|
75 | + { |
|
76 | + $this->_data = $this->_admin_page->get_registrations($this->_per_page); |
|
77 | + $this->_all_data_count = $this->_admin_page->get_registrations($this->_per_page, true, false, false); |
|
78 | + } |
|
79 | 79 | |
80 | 80 | |
81 | - /** |
|
82 | - * _set_properties |
|
83 | - * |
|
84 | - * @access protected |
|
85 | - * @return void |
|
86 | - */ |
|
87 | - protected function _set_properties() |
|
88 | - { |
|
89 | - $this->_wp_list_args = array( |
|
90 | - 'singular' => __('registration', 'event_espresso'), |
|
91 | - 'plural' => __('registrations', 'event_espresso'), |
|
92 | - 'ajax' => true, |
|
93 | - 'screen' => $this->_admin_page->get_current_screen()->id, |
|
94 | - ); |
|
95 | - $ID_column_name = __('ID', 'event_espresso'); |
|
96 | - $ID_column_name .= ' : <span class="show-on-mobile-view-only" style="float:none">'; |
|
97 | - $ID_column_name .= __('Registrant Name', 'event_espresso'); |
|
98 | - $ID_column_name .= '</span> '; |
|
99 | - if (isset($_GET['event_id'])) { |
|
100 | - $this->_columns = array( |
|
101 | - 'cb' => '<input type="checkbox" />', // Render a checkbox instead of text |
|
102 | - '_REG_ID' => $ID_column_name, |
|
103 | - 'ATT_fname' => __('Name', 'event_espresso'), |
|
104 | - 'ATT_email' => __('Email', 'event_espresso'), |
|
105 | - '_REG_date' => __('Reg Date', 'event_espresso'), |
|
106 | - 'PRC_amount' => __('TKT Price', 'event_espresso'), |
|
107 | - '_REG_final_price' => __('Final Price', 'event_espresso'), |
|
108 | - 'TXN_total' => __('Total Txn', 'event_espresso'), |
|
109 | - 'TXN_paid' => __('Paid', 'event_espresso'), |
|
110 | - 'actions' => __('Actions', 'event_espresso'), |
|
111 | - ); |
|
112 | - $this->_bottom_buttons = array( |
|
113 | - 'report' => array( |
|
114 | - 'route' => 'registrations_report', |
|
115 | - 'extra_request' => array( |
|
116 | - 'EVT_ID' => isset($this->_req_data['event_id']) ? $this->_req_data['event_id'] : null, |
|
117 | - 'return_url' => urlencode("//{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}"), |
|
118 | - ), |
|
119 | - ), |
|
120 | - ); |
|
121 | - } else { |
|
122 | - $this->_columns = array( |
|
123 | - 'cb' => '<input type="checkbox" />', // Render a checkbox instead of text |
|
124 | - '_REG_ID' => $ID_column_name, |
|
125 | - 'ATT_fname' => __('Name', 'event_espresso'), |
|
126 | - '_REG_date' => __('TXN Date', 'event_espresso'), |
|
127 | - 'event_name' => __('Event', 'event_espresso'), |
|
128 | - 'DTT_EVT_start' => __('Event Date', 'event_espresso'), |
|
129 | - '_REG_final_price' => __('Price', 'event_espresso'), |
|
130 | - '_REG_paid' => __('Paid', 'event_espresso'), |
|
131 | - 'actions' => __('Actions', 'event_espresso'), |
|
132 | - ); |
|
133 | - $this->_bottom_buttons = array( |
|
134 | - 'report_all' => array( |
|
135 | - 'route' => 'registrations_report', |
|
136 | - 'extra_request' => array( |
|
137 | - 'return_url' => urlencode("//{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}"), |
|
138 | - ), |
|
139 | - ), |
|
140 | - ); |
|
141 | - } |
|
142 | - $this->_bottom_buttons['report_filtered'] = array( |
|
143 | - 'route' => 'registrations_report', |
|
144 | - 'extra_request' => array( |
|
145 | - 'use_filters' => true, |
|
146 | - 'return_url' => urlencode("//{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}"), |
|
147 | - ), |
|
148 | - ); |
|
149 | - $filters = array_diff_key( |
|
150 | - $this->_req_data, |
|
151 | - array_flip( |
|
152 | - array( |
|
153 | - 'page', |
|
154 | - 'action', |
|
155 | - 'default_nonce', |
|
156 | - ) |
|
157 | - ) |
|
158 | - ); |
|
159 | - if (!empty($filters)) { |
|
160 | - $this->_bottom_buttons['report_filtered']['extra_request']['filers'] = $filters; |
|
161 | - } |
|
162 | - $this->_primary_column = '_REG_ID'; |
|
163 | - $this->_sortable_columns = array( |
|
164 | - '_REG_date' => array('_REG_date' => true), // true means its already sorted |
|
165 | - /** |
|
166 | - * Allows users to change the default sort if they wish. |
|
167 | - * Returning a falsey on this filter will result in the default sort to be by firstname rather than last |
|
168 | - * name. |
|
169 | - */ |
|
170 | - 'ATT_fname' => array( |
|
171 | - 'FHEE__EE_Registrations_List_Table___set_properties__default_sort_by_registration_last_name', |
|
172 | - true, |
|
173 | - $this, |
|
174 | - ) |
|
175 | - ? array('ATT_lname' => false) |
|
176 | - : array('ATT_fname' => false), |
|
177 | - 'event_name' => array('event_name' => false), |
|
178 | - 'DTT_EVT_start' => array('DTT_EVT_start' => false), |
|
179 | - '_REG_ID' => array('_REG_ID' => false), |
|
180 | - ); |
|
181 | - $this->_hidden_columns = array(); |
|
182 | - } |
|
81 | + /** |
|
82 | + * _set_properties |
|
83 | + * |
|
84 | + * @access protected |
|
85 | + * @return void |
|
86 | + */ |
|
87 | + protected function _set_properties() |
|
88 | + { |
|
89 | + $this->_wp_list_args = array( |
|
90 | + 'singular' => __('registration', 'event_espresso'), |
|
91 | + 'plural' => __('registrations', 'event_espresso'), |
|
92 | + 'ajax' => true, |
|
93 | + 'screen' => $this->_admin_page->get_current_screen()->id, |
|
94 | + ); |
|
95 | + $ID_column_name = __('ID', 'event_espresso'); |
|
96 | + $ID_column_name .= ' : <span class="show-on-mobile-view-only" style="float:none">'; |
|
97 | + $ID_column_name .= __('Registrant Name', 'event_espresso'); |
|
98 | + $ID_column_name .= '</span> '; |
|
99 | + if (isset($_GET['event_id'])) { |
|
100 | + $this->_columns = array( |
|
101 | + 'cb' => '<input type="checkbox" />', // Render a checkbox instead of text |
|
102 | + '_REG_ID' => $ID_column_name, |
|
103 | + 'ATT_fname' => __('Name', 'event_espresso'), |
|
104 | + 'ATT_email' => __('Email', 'event_espresso'), |
|
105 | + '_REG_date' => __('Reg Date', 'event_espresso'), |
|
106 | + 'PRC_amount' => __('TKT Price', 'event_espresso'), |
|
107 | + '_REG_final_price' => __('Final Price', 'event_espresso'), |
|
108 | + 'TXN_total' => __('Total Txn', 'event_espresso'), |
|
109 | + 'TXN_paid' => __('Paid', 'event_espresso'), |
|
110 | + 'actions' => __('Actions', 'event_espresso'), |
|
111 | + ); |
|
112 | + $this->_bottom_buttons = array( |
|
113 | + 'report' => array( |
|
114 | + 'route' => 'registrations_report', |
|
115 | + 'extra_request' => array( |
|
116 | + 'EVT_ID' => isset($this->_req_data['event_id']) ? $this->_req_data['event_id'] : null, |
|
117 | + 'return_url' => urlencode("//{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}"), |
|
118 | + ), |
|
119 | + ), |
|
120 | + ); |
|
121 | + } else { |
|
122 | + $this->_columns = array( |
|
123 | + 'cb' => '<input type="checkbox" />', // Render a checkbox instead of text |
|
124 | + '_REG_ID' => $ID_column_name, |
|
125 | + 'ATT_fname' => __('Name', 'event_espresso'), |
|
126 | + '_REG_date' => __('TXN Date', 'event_espresso'), |
|
127 | + 'event_name' => __('Event', 'event_espresso'), |
|
128 | + 'DTT_EVT_start' => __('Event Date', 'event_espresso'), |
|
129 | + '_REG_final_price' => __('Price', 'event_espresso'), |
|
130 | + '_REG_paid' => __('Paid', 'event_espresso'), |
|
131 | + 'actions' => __('Actions', 'event_espresso'), |
|
132 | + ); |
|
133 | + $this->_bottom_buttons = array( |
|
134 | + 'report_all' => array( |
|
135 | + 'route' => 'registrations_report', |
|
136 | + 'extra_request' => array( |
|
137 | + 'return_url' => urlencode("//{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}"), |
|
138 | + ), |
|
139 | + ), |
|
140 | + ); |
|
141 | + } |
|
142 | + $this->_bottom_buttons['report_filtered'] = array( |
|
143 | + 'route' => 'registrations_report', |
|
144 | + 'extra_request' => array( |
|
145 | + 'use_filters' => true, |
|
146 | + 'return_url' => urlencode("//{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}"), |
|
147 | + ), |
|
148 | + ); |
|
149 | + $filters = array_diff_key( |
|
150 | + $this->_req_data, |
|
151 | + array_flip( |
|
152 | + array( |
|
153 | + 'page', |
|
154 | + 'action', |
|
155 | + 'default_nonce', |
|
156 | + ) |
|
157 | + ) |
|
158 | + ); |
|
159 | + if (!empty($filters)) { |
|
160 | + $this->_bottom_buttons['report_filtered']['extra_request']['filers'] = $filters; |
|
161 | + } |
|
162 | + $this->_primary_column = '_REG_ID'; |
|
163 | + $this->_sortable_columns = array( |
|
164 | + '_REG_date' => array('_REG_date' => true), // true means its already sorted |
|
165 | + /** |
|
166 | + * Allows users to change the default sort if they wish. |
|
167 | + * Returning a falsey on this filter will result in the default sort to be by firstname rather than last |
|
168 | + * name. |
|
169 | + */ |
|
170 | + 'ATT_fname' => array( |
|
171 | + 'FHEE__EE_Registrations_List_Table___set_properties__default_sort_by_registration_last_name', |
|
172 | + true, |
|
173 | + $this, |
|
174 | + ) |
|
175 | + ? array('ATT_lname' => false) |
|
176 | + : array('ATT_fname' => false), |
|
177 | + 'event_name' => array('event_name' => false), |
|
178 | + 'DTT_EVT_start' => array('DTT_EVT_start' => false), |
|
179 | + '_REG_ID' => array('_REG_ID' => false), |
|
180 | + ); |
|
181 | + $this->_hidden_columns = array(); |
|
182 | + } |
|
183 | 183 | |
184 | 184 | |
185 | - /** |
|
186 | - * This simply sets up the row class for the table rows. |
|
187 | - * Allows for easier overriding of child methods for setting up sorting. |
|
188 | - * |
|
189 | - * @param EE_Registration $item the current item |
|
190 | - * @return string |
|
191 | - */ |
|
192 | - protected function _get_row_class($item) |
|
193 | - { |
|
194 | - $class = parent::_get_row_class($item); |
|
195 | - // add status class |
|
196 | - $class .= ' ee-status-strip reg-status-' . $item->status_ID(); |
|
197 | - if ($this->_has_checkbox_column) { |
|
198 | - $class .= ' has-checkbox-column'; |
|
199 | - } |
|
200 | - return $class; |
|
201 | - } |
|
185 | + /** |
|
186 | + * This simply sets up the row class for the table rows. |
|
187 | + * Allows for easier overriding of child methods for setting up sorting. |
|
188 | + * |
|
189 | + * @param EE_Registration $item the current item |
|
190 | + * @return string |
|
191 | + */ |
|
192 | + protected function _get_row_class($item) |
|
193 | + { |
|
194 | + $class = parent::_get_row_class($item); |
|
195 | + // add status class |
|
196 | + $class .= ' ee-status-strip reg-status-' . $item->status_ID(); |
|
197 | + if ($this->_has_checkbox_column) { |
|
198 | + $class .= ' has-checkbox-column'; |
|
199 | + } |
|
200 | + return $class; |
|
201 | + } |
|
202 | 202 | |
203 | 203 | |
204 | - /** |
|
205 | - * Set the $_transaction_details property if not set yet. |
|
206 | - * |
|
207 | - * @param EE_Registration $registration |
|
208 | - * @throws EE_Error |
|
209 | - * @throws InvalidArgumentException |
|
210 | - * @throws ReflectionException |
|
211 | - * @throws InvalidDataTypeException |
|
212 | - * @throws InvalidInterfaceException |
|
213 | - */ |
|
214 | - protected function _set_related_details(EE_Registration $registration) |
|
215 | - { |
|
216 | - $transaction = $registration->get_first_related('Transaction'); |
|
217 | - $status = $transaction instanceof EE_Transaction ? $transaction->status_ID() |
|
218 | - : EEM_Transaction::failed_status_code; |
|
219 | - $this->_transaction_details = array( |
|
220 | - 'transaction' => $transaction, |
|
221 | - 'status' => $status, |
|
222 | - 'id' => $transaction instanceof EE_Transaction ? $transaction->ID() : 0, |
|
223 | - 'title_attr' => sprintf( |
|
224 | - __('View Transaction Details (%s)', 'event_espresso'), |
|
225 | - EEH_Template::pretty_status($status, false, 'sentence') |
|
226 | - ), |
|
227 | - ); |
|
228 | - try { |
|
229 | - $event = $registration->event(); |
|
230 | - } catch (EntityNotFoundException $e) { |
|
231 | - $event = null; |
|
232 | - } |
|
233 | - $status = $event instanceof EE_Event ? $event->get_active_status() : EE_Datetime::inactive; |
|
234 | - $this->_event_details = array( |
|
235 | - 'event' => $event, |
|
236 | - 'status' => $status, |
|
237 | - 'id' => $event instanceof EE_Event ? $event->ID() : 0, |
|
238 | - 'title_attr' => sprintf( |
|
239 | - __('Edit Event (%s)', 'event_espresso'), |
|
240 | - EEH_Template::pretty_status($status, false, 'sentence') |
|
241 | - ), |
|
242 | - ); |
|
243 | - } |
|
204 | + /** |
|
205 | + * Set the $_transaction_details property if not set yet. |
|
206 | + * |
|
207 | + * @param EE_Registration $registration |
|
208 | + * @throws EE_Error |
|
209 | + * @throws InvalidArgumentException |
|
210 | + * @throws ReflectionException |
|
211 | + * @throws InvalidDataTypeException |
|
212 | + * @throws InvalidInterfaceException |
|
213 | + */ |
|
214 | + protected function _set_related_details(EE_Registration $registration) |
|
215 | + { |
|
216 | + $transaction = $registration->get_first_related('Transaction'); |
|
217 | + $status = $transaction instanceof EE_Transaction ? $transaction->status_ID() |
|
218 | + : EEM_Transaction::failed_status_code; |
|
219 | + $this->_transaction_details = array( |
|
220 | + 'transaction' => $transaction, |
|
221 | + 'status' => $status, |
|
222 | + 'id' => $transaction instanceof EE_Transaction ? $transaction->ID() : 0, |
|
223 | + 'title_attr' => sprintf( |
|
224 | + __('View Transaction Details (%s)', 'event_espresso'), |
|
225 | + EEH_Template::pretty_status($status, false, 'sentence') |
|
226 | + ), |
|
227 | + ); |
|
228 | + try { |
|
229 | + $event = $registration->event(); |
|
230 | + } catch (EntityNotFoundException $e) { |
|
231 | + $event = null; |
|
232 | + } |
|
233 | + $status = $event instanceof EE_Event ? $event->get_active_status() : EE_Datetime::inactive; |
|
234 | + $this->_event_details = array( |
|
235 | + 'event' => $event, |
|
236 | + 'status' => $status, |
|
237 | + 'id' => $event instanceof EE_Event ? $event->ID() : 0, |
|
238 | + 'title_attr' => sprintf( |
|
239 | + __('Edit Event (%s)', 'event_espresso'), |
|
240 | + EEH_Template::pretty_status($status, false, 'sentence') |
|
241 | + ), |
|
242 | + ); |
|
243 | + } |
|
244 | 244 | |
245 | 245 | |
246 | - /** |
|
247 | - * _get_table_filters |
|
248 | - * |
|
249 | - * @access protected |
|
250 | - * @return array |
|
251 | - */ |
|
252 | - protected function _get_table_filters() |
|
253 | - { |
|
254 | - $filters = array(); |
|
255 | - // todo we're currently using old functions here. We need to move things into the Events_Admin_Page() class as |
|
256 | - // methods. |
|
257 | - $cur_date = isset($this->_req_data['month_range']) ? $this->_req_data['month_range'] : ''; |
|
258 | - $cur_category = isset($this->_req_data['EVT_CAT']) ? $this->_req_data['EVT_CAT'] : -1; |
|
259 | - $reg_status = isset($this->_req_data['_reg_status']) ? $this->_req_data['_reg_status'] : ''; |
|
260 | - $filters[] = EEH_Form_Fields::generate_registration_months_dropdown($cur_date, $reg_status, $cur_category); |
|
261 | - $filters[] = EEH_Form_Fields::generate_event_category_dropdown($cur_category); |
|
262 | - $status = array(); |
|
263 | - $status[] = array('id' => 0, 'text' => __('Select Status', 'event_espresso')); |
|
264 | - foreach ($this->_status as $key => $value) { |
|
265 | - $status[] = array('id' => $key, 'text' => $value); |
|
266 | - } |
|
267 | - if ($this->_view !== 'incomplete') { |
|
268 | - $filters[] = EEH_Form_Fields::select_input( |
|
269 | - '_reg_status', |
|
270 | - $status, |
|
271 | - isset($this->_req_data['_reg_status']) ? strtoupper(sanitize_key($this->_req_data['_reg_status'])) |
|
272 | - : '' |
|
273 | - ); |
|
274 | - } |
|
275 | - if (isset($this->_req_data['event_id'])) { |
|
276 | - $filters[] = EEH_Form_Fields::hidden_input('event_id', $this->_req_data['event_id'], 'reg_event_id'); |
|
277 | - } |
|
278 | - return $filters; |
|
279 | - } |
|
246 | + /** |
|
247 | + * _get_table_filters |
|
248 | + * |
|
249 | + * @access protected |
|
250 | + * @return array |
|
251 | + */ |
|
252 | + protected function _get_table_filters() |
|
253 | + { |
|
254 | + $filters = array(); |
|
255 | + // todo we're currently using old functions here. We need to move things into the Events_Admin_Page() class as |
|
256 | + // methods. |
|
257 | + $cur_date = isset($this->_req_data['month_range']) ? $this->_req_data['month_range'] : ''; |
|
258 | + $cur_category = isset($this->_req_data['EVT_CAT']) ? $this->_req_data['EVT_CAT'] : -1; |
|
259 | + $reg_status = isset($this->_req_data['_reg_status']) ? $this->_req_data['_reg_status'] : ''; |
|
260 | + $filters[] = EEH_Form_Fields::generate_registration_months_dropdown($cur_date, $reg_status, $cur_category); |
|
261 | + $filters[] = EEH_Form_Fields::generate_event_category_dropdown($cur_category); |
|
262 | + $status = array(); |
|
263 | + $status[] = array('id' => 0, 'text' => __('Select Status', 'event_espresso')); |
|
264 | + foreach ($this->_status as $key => $value) { |
|
265 | + $status[] = array('id' => $key, 'text' => $value); |
|
266 | + } |
|
267 | + if ($this->_view !== 'incomplete') { |
|
268 | + $filters[] = EEH_Form_Fields::select_input( |
|
269 | + '_reg_status', |
|
270 | + $status, |
|
271 | + isset($this->_req_data['_reg_status']) ? strtoupper(sanitize_key($this->_req_data['_reg_status'])) |
|
272 | + : '' |
|
273 | + ); |
|
274 | + } |
|
275 | + if (isset($this->_req_data['event_id'])) { |
|
276 | + $filters[] = EEH_Form_Fields::hidden_input('event_id', $this->_req_data['event_id'], 'reg_event_id'); |
|
277 | + } |
|
278 | + return $filters; |
|
279 | + } |
|
280 | 280 | |
281 | 281 | |
282 | - /** |
|
283 | - * _add_view_counts |
|
284 | - * |
|
285 | - * @access protected |
|
286 | - * @return void |
|
287 | - * @throws EE_Error |
|
288 | - * @throws InvalidArgumentException |
|
289 | - * @throws InvalidDataTypeException |
|
290 | - * @throws InvalidInterfaceException |
|
291 | - */ |
|
292 | - protected function _add_view_counts() |
|
293 | - { |
|
294 | - $this->_views['all']['count'] = $this->_total_registrations(); |
|
295 | - $this->_views['month']['count'] = $this->_total_registrations_this_month(); |
|
296 | - $this->_views['today']['count'] = $this->_total_registrations_today(); |
|
297 | - if (EE_Registry::instance()->CAP->current_user_can( |
|
298 | - 'ee_delete_registrations', |
|
299 | - 'espresso_registrations_trash_registrations' |
|
300 | - )) { |
|
301 | - $this->_views['incomplete']['count'] = $this->_total_registrations('incomplete'); |
|
302 | - $this->_views['trash']['count'] = $this->_total_registrations('trash'); |
|
303 | - } |
|
304 | - } |
|
282 | + /** |
|
283 | + * _add_view_counts |
|
284 | + * |
|
285 | + * @access protected |
|
286 | + * @return void |
|
287 | + * @throws EE_Error |
|
288 | + * @throws InvalidArgumentException |
|
289 | + * @throws InvalidDataTypeException |
|
290 | + * @throws InvalidInterfaceException |
|
291 | + */ |
|
292 | + protected function _add_view_counts() |
|
293 | + { |
|
294 | + $this->_views['all']['count'] = $this->_total_registrations(); |
|
295 | + $this->_views['month']['count'] = $this->_total_registrations_this_month(); |
|
296 | + $this->_views['today']['count'] = $this->_total_registrations_today(); |
|
297 | + if (EE_Registry::instance()->CAP->current_user_can( |
|
298 | + 'ee_delete_registrations', |
|
299 | + 'espresso_registrations_trash_registrations' |
|
300 | + )) { |
|
301 | + $this->_views['incomplete']['count'] = $this->_total_registrations('incomplete'); |
|
302 | + $this->_views['trash']['count'] = $this->_total_registrations('trash'); |
|
303 | + } |
|
304 | + } |
|
305 | 305 | |
306 | 306 | |
307 | - /** |
|
308 | - * _total_registrations |
|
309 | - * |
|
310 | - * @access protected |
|
311 | - * @param string $view |
|
312 | - * @return int |
|
313 | - * @throws EE_Error |
|
314 | - * @throws InvalidArgumentException |
|
315 | - * @throws InvalidDataTypeException |
|
316 | - * @throws InvalidInterfaceException |
|
317 | - */ |
|
318 | - protected function _total_registrations($view = '') |
|
319 | - { |
|
320 | - $_where = array(); |
|
321 | - $EVT_ID = isset($this->_req_data['event_id']) ? absint($this->_req_data['event_id']) : false; |
|
322 | - if ($EVT_ID) { |
|
323 | - $_where['EVT_ID'] = $EVT_ID; |
|
324 | - } |
|
325 | - switch ($view) { |
|
326 | - case 'trash': |
|
327 | - return EEM_Registration::instance()->count_deleted(array($_where)); |
|
328 | - break; |
|
329 | - case 'incomplete': |
|
330 | - $_where['STS_ID'] = EEM_Registration::status_id_incomplete; |
|
331 | - break; |
|
332 | - default: |
|
333 | - $_where['STS_ID'] = array('!=', EEM_Registration::status_id_incomplete); |
|
334 | - } |
|
335 | - return EEM_Registration::instance()->count(array($_where)); |
|
336 | - } |
|
307 | + /** |
|
308 | + * _total_registrations |
|
309 | + * |
|
310 | + * @access protected |
|
311 | + * @param string $view |
|
312 | + * @return int |
|
313 | + * @throws EE_Error |
|
314 | + * @throws InvalidArgumentException |
|
315 | + * @throws InvalidDataTypeException |
|
316 | + * @throws InvalidInterfaceException |
|
317 | + */ |
|
318 | + protected function _total_registrations($view = '') |
|
319 | + { |
|
320 | + $_where = array(); |
|
321 | + $EVT_ID = isset($this->_req_data['event_id']) ? absint($this->_req_data['event_id']) : false; |
|
322 | + if ($EVT_ID) { |
|
323 | + $_where['EVT_ID'] = $EVT_ID; |
|
324 | + } |
|
325 | + switch ($view) { |
|
326 | + case 'trash': |
|
327 | + return EEM_Registration::instance()->count_deleted(array($_where)); |
|
328 | + break; |
|
329 | + case 'incomplete': |
|
330 | + $_where['STS_ID'] = EEM_Registration::status_id_incomplete; |
|
331 | + break; |
|
332 | + default: |
|
333 | + $_where['STS_ID'] = array('!=', EEM_Registration::status_id_incomplete); |
|
334 | + } |
|
335 | + return EEM_Registration::instance()->count(array($_where)); |
|
336 | + } |
|
337 | 337 | |
338 | 338 | |
339 | - /** |
|
340 | - * _total_registrations_this_month |
|
341 | - * |
|
342 | - * @access protected |
|
343 | - * @return int |
|
344 | - * @throws EE_Error |
|
345 | - * @throws InvalidArgumentException |
|
346 | - * @throws InvalidDataTypeException |
|
347 | - * @throws InvalidInterfaceException |
|
348 | - */ |
|
349 | - protected function _total_registrations_this_month() |
|
350 | - { |
|
351 | - $EVT_ID = isset($this->_req_data['event_id']) ? absint($this->_req_data['event_id']) : false; |
|
352 | - $_where = $EVT_ID ? array('EVT_ID' => $EVT_ID) : array(); |
|
353 | - $this_year_r = date('Y', current_time('timestamp')); |
|
354 | - $time_start = ' 00:00:00'; |
|
355 | - $time_end = ' 23:59:59'; |
|
356 | - $this_month_r = date('m', current_time('timestamp')); |
|
357 | - $days_this_month = date('t', current_time('timestamp')); |
|
358 | - // setup date query. |
|
359 | - $beginning_string = EEM_Registration::instance()->convert_datetime_for_query( |
|
360 | - 'REG_date', |
|
361 | - $this_year_r . '-' . $this_month_r . '-01' . ' ' . $time_start, |
|
362 | - 'Y-m-d H:i:s' |
|
363 | - ); |
|
364 | - $end_string = EEM_Registration::instance()->convert_datetime_for_query( |
|
365 | - 'REG_date', |
|
366 | - $this_year_r . '-' . $this_month_r . '-' . $days_this_month . ' ' . $time_end, |
|
367 | - 'Y-m-d H:i:s' |
|
368 | - ); |
|
369 | - $_where['REG_date'] = array( |
|
370 | - 'BETWEEN', |
|
371 | - array( |
|
372 | - $beginning_string, |
|
373 | - $end_string, |
|
374 | - ), |
|
375 | - ); |
|
376 | - $_where['STS_ID'] = array('!=', EEM_Registration::status_id_incomplete); |
|
377 | - return EEM_Registration::instance()->count(array($_where)); |
|
378 | - } |
|
339 | + /** |
|
340 | + * _total_registrations_this_month |
|
341 | + * |
|
342 | + * @access protected |
|
343 | + * @return int |
|
344 | + * @throws EE_Error |
|
345 | + * @throws InvalidArgumentException |
|
346 | + * @throws InvalidDataTypeException |
|
347 | + * @throws InvalidInterfaceException |
|
348 | + */ |
|
349 | + protected function _total_registrations_this_month() |
|
350 | + { |
|
351 | + $EVT_ID = isset($this->_req_data['event_id']) ? absint($this->_req_data['event_id']) : false; |
|
352 | + $_where = $EVT_ID ? array('EVT_ID' => $EVT_ID) : array(); |
|
353 | + $this_year_r = date('Y', current_time('timestamp')); |
|
354 | + $time_start = ' 00:00:00'; |
|
355 | + $time_end = ' 23:59:59'; |
|
356 | + $this_month_r = date('m', current_time('timestamp')); |
|
357 | + $days_this_month = date('t', current_time('timestamp')); |
|
358 | + // setup date query. |
|
359 | + $beginning_string = EEM_Registration::instance()->convert_datetime_for_query( |
|
360 | + 'REG_date', |
|
361 | + $this_year_r . '-' . $this_month_r . '-01' . ' ' . $time_start, |
|
362 | + 'Y-m-d H:i:s' |
|
363 | + ); |
|
364 | + $end_string = EEM_Registration::instance()->convert_datetime_for_query( |
|
365 | + 'REG_date', |
|
366 | + $this_year_r . '-' . $this_month_r . '-' . $days_this_month . ' ' . $time_end, |
|
367 | + 'Y-m-d H:i:s' |
|
368 | + ); |
|
369 | + $_where['REG_date'] = array( |
|
370 | + 'BETWEEN', |
|
371 | + array( |
|
372 | + $beginning_string, |
|
373 | + $end_string, |
|
374 | + ), |
|
375 | + ); |
|
376 | + $_where['STS_ID'] = array('!=', EEM_Registration::status_id_incomplete); |
|
377 | + return EEM_Registration::instance()->count(array($_where)); |
|
378 | + } |
|
379 | 379 | |
380 | 380 | |
381 | - /** |
|
382 | - * _total_registrations_today |
|
383 | - * |
|
384 | - * @access protected |
|
385 | - * @return int |
|
386 | - * @throws EE_Error |
|
387 | - * @throws InvalidArgumentException |
|
388 | - * @throws InvalidDataTypeException |
|
389 | - * @throws InvalidInterfaceException |
|
390 | - */ |
|
391 | - protected function _total_registrations_today() |
|
392 | - { |
|
393 | - $EVT_ID = isset($this->_req_data['event_id']) ? absint($this->_req_data['event_id']) : false; |
|
394 | - $_where = $EVT_ID ? array('EVT_ID' => $EVT_ID) : array(); |
|
395 | - $current_date = date('Y-m-d', current_time('timestamp')); |
|
396 | - $time_start = ' 00:00:00'; |
|
397 | - $time_end = ' 23:59:59'; |
|
398 | - $_where['REG_date'] = array( |
|
399 | - 'BETWEEN', |
|
400 | - array( |
|
401 | - EEM_Registration::instance()->convert_datetime_for_query( |
|
402 | - 'REG_date', |
|
403 | - $current_date . $time_start, |
|
404 | - 'Y-m-d H:i:s' |
|
405 | - ), |
|
406 | - EEM_Registration::instance()->convert_datetime_for_query( |
|
407 | - 'REG_date', |
|
408 | - $current_date . $time_end, |
|
409 | - 'Y-m-d H:i:s' |
|
410 | - ), |
|
411 | - ), |
|
412 | - ); |
|
413 | - $_where['STS_ID'] = array('!=', EEM_Registration::status_id_incomplete); |
|
414 | - return EEM_Registration::instance()->count(array($_where)); |
|
415 | - } |
|
381 | + /** |
|
382 | + * _total_registrations_today |
|
383 | + * |
|
384 | + * @access protected |
|
385 | + * @return int |
|
386 | + * @throws EE_Error |
|
387 | + * @throws InvalidArgumentException |
|
388 | + * @throws InvalidDataTypeException |
|
389 | + * @throws InvalidInterfaceException |
|
390 | + */ |
|
391 | + protected function _total_registrations_today() |
|
392 | + { |
|
393 | + $EVT_ID = isset($this->_req_data['event_id']) ? absint($this->_req_data['event_id']) : false; |
|
394 | + $_where = $EVT_ID ? array('EVT_ID' => $EVT_ID) : array(); |
|
395 | + $current_date = date('Y-m-d', current_time('timestamp')); |
|
396 | + $time_start = ' 00:00:00'; |
|
397 | + $time_end = ' 23:59:59'; |
|
398 | + $_where['REG_date'] = array( |
|
399 | + 'BETWEEN', |
|
400 | + array( |
|
401 | + EEM_Registration::instance()->convert_datetime_for_query( |
|
402 | + 'REG_date', |
|
403 | + $current_date . $time_start, |
|
404 | + 'Y-m-d H:i:s' |
|
405 | + ), |
|
406 | + EEM_Registration::instance()->convert_datetime_for_query( |
|
407 | + 'REG_date', |
|
408 | + $current_date . $time_end, |
|
409 | + 'Y-m-d H:i:s' |
|
410 | + ), |
|
411 | + ), |
|
412 | + ); |
|
413 | + $_where['STS_ID'] = array('!=', EEM_Registration::status_id_incomplete); |
|
414 | + return EEM_Registration::instance()->count(array($_where)); |
|
415 | + } |
|
416 | 416 | |
417 | 417 | |
418 | - /** |
|
419 | - * column_cb |
|
420 | - * |
|
421 | - * @access public |
|
422 | - * @param \EE_Registration $item |
|
423 | - * @return string |
|
424 | - * @throws EE_Error |
|
425 | - * @throws InvalidArgumentException |
|
426 | - * @throws InvalidDataTypeException |
|
427 | - * @throws InvalidInterfaceException |
|
428 | - * @throws ReflectionException |
|
429 | - */ |
|
430 | - public function column_cb($item) |
|
431 | - { |
|
432 | - /** checkbox/lock **/ |
|
433 | - $transaction = $item->get_first_related('Transaction'); |
|
434 | - $payment_count = $transaction instanceof EE_Transaction |
|
435 | - ? $transaction->count_related('Payment') |
|
436 | - : 0; |
|
437 | - return $payment_count > 0 |
|
438 | - || ! EE_Registry::instance()->CAP->current_user_can( |
|
439 | - 'ee_edit_registration', |
|
440 | - 'registration_list_table_checkbox_input', |
|
441 | - $item->ID() |
|
442 | - ) |
|
443 | - ? sprintf('<input type="checkbox" name="_REG_ID[]" value="%1$d" />', $item->ID()) |
|
444 | - . '<span class="ee-lock-icon"></span>' |
|
445 | - : sprintf('<input type="checkbox" name="_REG_ID[]" value="%1$d" />', $item->ID()); |
|
446 | - } |
|
418 | + /** |
|
419 | + * column_cb |
|
420 | + * |
|
421 | + * @access public |
|
422 | + * @param \EE_Registration $item |
|
423 | + * @return string |
|
424 | + * @throws EE_Error |
|
425 | + * @throws InvalidArgumentException |
|
426 | + * @throws InvalidDataTypeException |
|
427 | + * @throws InvalidInterfaceException |
|
428 | + * @throws ReflectionException |
|
429 | + */ |
|
430 | + public function column_cb($item) |
|
431 | + { |
|
432 | + /** checkbox/lock **/ |
|
433 | + $transaction = $item->get_first_related('Transaction'); |
|
434 | + $payment_count = $transaction instanceof EE_Transaction |
|
435 | + ? $transaction->count_related('Payment') |
|
436 | + : 0; |
|
437 | + return $payment_count > 0 |
|
438 | + || ! EE_Registry::instance()->CAP->current_user_can( |
|
439 | + 'ee_edit_registration', |
|
440 | + 'registration_list_table_checkbox_input', |
|
441 | + $item->ID() |
|
442 | + ) |
|
443 | + ? sprintf('<input type="checkbox" name="_REG_ID[]" value="%1$d" />', $item->ID()) |
|
444 | + . '<span class="ee-lock-icon"></span>' |
|
445 | + : sprintf('<input type="checkbox" name="_REG_ID[]" value="%1$d" />', $item->ID()); |
|
446 | + } |
|
447 | 447 | |
448 | 448 | |
449 | - /** |
|
450 | - * column__REG_ID |
|
451 | - * |
|
452 | - * @access public |
|
453 | - * @param \EE_Registration $item |
|
454 | - * @return string |
|
455 | - * @throws EE_Error |
|
456 | - * @throws InvalidArgumentException |
|
457 | - * @throws InvalidDataTypeException |
|
458 | - * @throws InvalidInterfaceException |
|
459 | - * @throws ReflectionException |
|
460 | - */ |
|
461 | - public function column__REG_ID(EE_Registration $item) |
|
462 | - { |
|
463 | - $attendee = $item->attendee(); |
|
464 | - $content = $item->ID(); |
|
465 | - $content .= '<div class="show-on-mobile-view-only">'; |
|
466 | - $content .= '<br>'; |
|
467 | - $content .= $attendee instanceof EE_Attendee ? $attendee->full_name() : ''; |
|
468 | - $content .= ' ' . sprintf(__('(%1$s / %2$s)', 'event_espresso'), $item->count(), $item->group_size()); |
|
469 | - $content .= '<br>' . sprintf(__('Reg Code: %s', 'event_espresso'), $item->get('REG_code')); |
|
470 | - $content .= '</div>'; |
|
471 | - return $content; |
|
472 | - } |
|
449 | + /** |
|
450 | + * column__REG_ID |
|
451 | + * |
|
452 | + * @access public |
|
453 | + * @param \EE_Registration $item |
|
454 | + * @return string |
|
455 | + * @throws EE_Error |
|
456 | + * @throws InvalidArgumentException |
|
457 | + * @throws InvalidDataTypeException |
|
458 | + * @throws InvalidInterfaceException |
|
459 | + * @throws ReflectionException |
|
460 | + */ |
|
461 | + public function column__REG_ID(EE_Registration $item) |
|
462 | + { |
|
463 | + $attendee = $item->attendee(); |
|
464 | + $content = $item->ID(); |
|
465 | + $content .= '<div class="show-on-mobile-view-only">'; |
|
466 | + $content .= '<br>'; |
|
467 | + $content .= $attendee instanceof EE_Attendee ? $attendee->full_name() : ''; |
|
468 | + $content .= ' ' . sprintf(__('(%1$s / %2$s)', 'event_espresso'), $item->count(), $item->group_size()); |
|
469 | + $content .= '<br>' . sprintf(__('Reg Code: %s', 'event_espresso'), $item->get('REG_code')); |
|
470 | + $content .= '</div>'; |
|
471 | + return $content; |
|
472 | + } |
|
473 | 473 | |
474 | 474 | |
475 | - /** |
|
476 | - * column__REG_date |
|
477 | - * |
|
478 | - * @access public |
|
479 | - * @param \EE_Registration $item |
|
480 | - * @return string |
|
481 | - * @throws EE_Error |
|
482 | - * @throws InvalidArgumentException |
|
483 | - * @throws InvalidDataTypeException |
|
484 | - * @throws InvalidInterfaceException |
|
485 | - * @throws ReflectionException |
|
486 | - */ |
|
487 | - public function column__REG_date(EE_Registration $item) |
|
488 | - { |
|
489 | - $this->_set_related_details($item); |
|
490 | - // Build row actions |
|
491 | - $view_lnk_url = EE_Admin_Page::add_query_args_and_nonce( |
|
492 | - array( |
|
493 | - 'action' => 'view_transaction', |
|
494 | - 'TXN_ID' => $this->_transaction_details['id'], |
|
495 | - ), |
|
496 | - TXN_ADMIN_URL |
|
497 | - ); |
|
498 | - $view_link = EE_Registry::instance()->CAP->current_user_can( |
|
499 | - 'ee_read_transaction', |
|
500 | - 'espresso_transactions_view_transaction' |
|
501 | - ) |
|
502 | - ? '<a class="ee-status-color-' |
|
503 | - . $this->_transaction_details['status'] |
|
504 | - . '" href="' |
|
505 | - . $view_lnk_url |
|
506 | - . '" title="' |
|
507 | - . esc_attr($this->_transaction_details['title_attr']) |
|
508 | - . '">' |
|
509 | - . $item->get_i18n_datetime('REG_date') |
|
510 | - . '</a>' : $item->get_i18n_datetime('REG_date'); |
|
511 | - $view_link .= '<br><span class="ee-status-text-small">' |
|
512 | - . EEH_Template::pretty_status($this->_transaction_details['status'], false, 'sentence') |
|
513 | - . '</span>'; |
|
514 | - return $view_link; |
|
515 | - } |
|
475 | + /** |
|
476 | + * column__REG_date |
|
477 | + * |
|
478 | + * @access public |
|
479 | + * @param \EE_Registration $item |
|
480 | + * @return string |
|
481 | + * @throws EE_Error |
|
482 | + * @throws InvalidArgumentException |
|
483 | + * @throws InvalidDataTypeException |
|
484 | + * @throws InvalidInterfaceException |
|
485 | + * @throws ReflectionException |
|
486 | + */ |
|
487 | + public function column__REG_date(EE_Registration $item) |
|
488 | + { |
|
489 | + $this->_set_related_details($item); |
|
490 | + // Build row actions |
|
491 | + $view_lnk_url = EE_Admin_Page::add_query_args_and_nonce( |
|
492 | + array( |
|
493 | + 'action' => 'view_transaction', |
|
494 | + 'TXN_ID' => $this->_transaction_details['id'], |
|
495 | + ), |
|
496 | + TXN_ADMIN_URL |
|
497 | + ); |
|
498 | + $view_link = EE_Registry::instance()->CAP->current_user_can( |
|
499 | + 'ee_read_transaction', |
|
500 | + 'espresso_transactions_view_transaction' |
|
501 | + ) |
|
502 | + ? '<a class="ee-status-color-' |
|
503 | + . $this->_transaction_details['status'] |
|
504 | + . '" href="' |
|
505 | + . $view_lnk_url |
|
506 | + . '" title="' |
|
507 | + . esc_attr($this->_transaction_details['title_attr']) |
|
508 | + . '">' |
|
509 | + . $item->get_i18n_datetime('REG_date') |
|
510 | + . '</a>' : $item->get_i18n_datetime('REG_date'); |
|
511 | + $view_link .= '<br><span class="ee-status-text-small">' |
|
512 | + . EEH_Template::pretty_status($this->_transaction_details['status'], false, 'sentence') |
|
513 | + . '</span>'; |
|
514 | + return $view_link; |
|
515 | + } |
|
516 | 516 | |
517 | 517 | |
518 | - /** |
|
519 | - * column_event_name |
|
520 | - * |
|
521 | - * @access public |
|
522 | - * @param \EE_Registration $item |
|
523 | - * @return string |
|
524 | - * @throws EE_Error |
|
525 | - * @throws InvalidArgumentException |
|
526 | - * @throws InvalidDataTypeException |
|
527 | - * @throws InvalidInterfaceException |
|
528 | - * @throws ReflectionException |
|
529 | - */ |
|
530 | - public function column_event_name(EE_Registration $item) |
|
531 | - { |
|
532 | - $this->_set_related_details($item); |
|
533 | - // page=espresso_events&action=edit_event&EVT_ID=2&edit_event_nonce=cf3a7e5b62 |
|
534 | - $EVT_ID = $item->event_ID(); |
|
535 | - $event_name = $item->event_name(); |
|
536 | - $event_name = $event_name ? $event_name : __("No Associated Event", 'event_espresso'); |
|
537 | - $event_name = wp_trim_words($event_name, 30, '...'); |
|
538 | - if ($EVT_ID) { |
|
539 | - $edit_event_url = EE_Admin_Page::add_query_args_and_nonce( |
|
540 | - array('action' => 'edit', 'post' => $EVT_ID), |
|
541 | - EVENTS_ADMIN_URL |
|
542 | - ); |
|
543 | - $edit_event = EE_Registry::instance()->CAP->current_user_can('ee_edit_event', 'edit_event', $EVT_ID) |
|
544 | - ? '<a class="ee-status-color-' |
|
545 | - . $this->_event_details['status'] |
|
546 | - . '" href="' |
|
547 | - . $edit_event_url |
|
548 | - . '" title="' |
|
549 | - . esc_attr($this->_event_details['title_attr']) |
|
550 | - . '">' |
|
551 | - . $event_name |
|
552 | - . '</a>' : $event_name; |
|
553 | - $edit_event_url = EE_Admin_Page::add_query_args_and_nonce(array('event_id' => $EVT_ID), REG_ADMIN_URL); |
|
554 | - $actions['event_filter'] = '<a href="' . $edit_event_url . '" title="'; |
|
555 | - $actions['event_filter'] .= sprintf( |
|
556 | - esc_attr__('Filter this list to only show registrations for %s', 'event_espresso'), |
|
557 | - $event_name |
|
558 | - ); |
|
559 | - $actions['event_filter'] .= '">' . __('View Registrations', 'event_espresso') . '</a>'; |
|
560 | - } else { |
|
561 | - $edit_event = $event_name; |
|
562 | - $actions['event_filter'] = ''; |
|
563 | - } |
|
564 | - return sprintf('%1$s %2$s', $edit_event, $this->row_actions($actions)); |
|
565 | - } |
|
518 | + /** |
|
519 | + * column_event_name |
|
520 | + * |
|
521 | + * @access public |
|
522 | + * @param \EE_Registration $item |
|
523 | + * @return string |
|
524 | + * @throws EE_Error |
|
525 | + * @throws InvalidArgumentException |
|
526 | + * @throws InvalidDataTypeException |
|
527 | + * @throws InvalidInterfaceException |
|
528 | + * @throws ReflectionException |
|
529 | + */ |
|
530 | + public function column_event_name(EE_Registration $item) |
|
531 | + { |
|
532 | + $this->_set_related_details($item); |
|
533 | + // page=espresso_events&action=edit_event&EVT_ID=2&edit_event_nonce=cf3a7e5b62 |
|
534 | + $EVT_ID = $item->event_ID(); |
|
535 | + $event_name = $item->event_name(); |
|
536 | + $event_name = $event_name ? $event_name : __("No Associated Event", 'event_espresso'); |
|
537 | + $event_name = wp_trim_words($event_name, 30, '...'); |
|
538 | + if ($EVT_ID) { |
|
539 | + $edit_event_url = EE_Admin_Page::add_query_args_and_nonce( |
|
540 | + array('action' => 'edit', 'post' => $EVT_ID), |
|
541 | + EVENTS_ADMIN_URL |
|
542 | + ); |
|
543 | + $edit_event = EE_Registry::instance()->CAP->current_user_can('ee_edit_event', 'edit_event', $EVT_ID) |
|
544 | + ? '<a class="ee-status-color-' |
|
545 | + . $this->_event_details['status'] |
|
546 | + . '" href="' |
|
547 | + . $edit_event_url |
|
548 | + . '" title="' |
|
549 | + . esc_attr($this->_event_details['title_attr']) |
|
550 | + . '">' |
|
551 | + . $event_name |
|
552 | + . '</a>' : $event_name; |
|
553 | + $edit_event_url = EE_Admin_Page::add_query_args_and_nonce(array('event_id' => $EVT_ID), REG_ADMIN_URL); |
|
554 | + $actions['event_filter'] = '<a href="' . $edit_event_url . '" title="'; |
|
555 | + $actions['event_filter'] .= sprintf( |
|
556 | + esc_attr__('Filter this list to only show registrations for %s', 'event_espresso'), |
|
557 | + $event_name |
|
558 | + ); |
|
559 | + $actions['event_filter'] .= '">' . __('View Registrations', 'event_espresso') . '</a>'; |
|
560 | + } else { |
|
561 | + $edit_event = $event_name; |
|
562 | + $actions['event_filter'] = ''; |
|
563 | + } |
|
564 | + return sprintf('%1$s %2$s', $edit_event, $this->row_actions($actions)); |
|
565 | + } |
|
566 | 566 | |
567 | 567 | |
568 | - /** |
|
569 | - * column_DTT_EVT_start |
|
570 | - * |
|
571 | - * @access public |
|
572 | - * @param \EE_Registration $item |
|
573 | - * @return string |
|
574 | - * @throws EE_Error |
|
575 | - * @throws InvalidArgumentException |
|
576 | - * @throws InvalidDataTypeException |
|
577 | - * @throws InvalidInterfaceException |
|
578 | - * @throws ReflectionException |
|
579 | - */ |
|
580 | - public function column_DTT_EVT_start(EE_Registration $item) |
|
581 | - { |
|
582 | - $datetime_strings = array(); |
|
583 | - $ticket = $item->ticket(true); |
|
584 | - if ($ticket instanceof EE_Ticket) { |
|
585 | - $remove_defaults = array('default_where_conditions' => 'none'); |
|
586 | - $datetimes = $ticket->datetimes($remove_defaults); |
|
587 | - foreach ($datetimes as $datetime) { |
|
588 | - $datetime_strings[] = $datetime->get_i18n_datetime('DTT_EVT_start'); |
|
589 | - } |
|
590 | - return $this->generateDisplayForDatetimes($datetime_strings); |
|
591 | - } |
|
592 | - return __('There is no ticket on this registration', 'event_espresso'); |
|
593 | - } |
|
568 | + /** |
|
569 | + * column_DTT_EVT_start |
|
570 | + * |
|
571 | + * @access public |
|
572 | + * @param \EE_Registration $item |
|
573 | + * @return string |
|
574 | + * @throws EE_Error |
|
575 | + * @throws InvalidArgumentException |
|
576 | + * @throws InvalidDataTypeException |
|
577 | + * @throws InvalidInterfaceException |
|
578 | + * @throws ReflectionException |
|
579 | + */ |
|
580 | + public function column_DTT_EVT_start(EE_Registration $item) |
|
581 | + { |
|
582 | + $datetime_strings = array(); |
|
583 | + $ticket = $item->ticket(true); |
|
584 | + if ($ticket instanceof EE_Ticket) { |
|
585 | + $remove_defaults = array('default_where_conditions' => 'none'); |
|
586 | + $datetimes = $ticket->datetimes($remove_defaults); |
|
587 | + foreach ($datetimes as $datetime) { |
|
588 | + $datetime_strings[] = $datetime->get_i18n_datetime('DTT_EVT_start'); |
|
589 | + } |
|
590 | + return $this->generateDisplayForDatetimes($datetime_strings); |
|
591 | + } |
|
592 | + return __('There is no ticket on this registration', 'event_espresso'); |
|
593 | + } |
|
594 | 594 | |
595 | 595 | |
596 | - /** |
|
597 | - * Receives an array of datetime strings to display and converts them to the html container for the column. |
|
598 | - * |
|
599 | - * @param array $datetime_strings |
|
600 | - * @return string |
|
601 | - */ |
|
602 | - public function generateDisplayForDateTimes(array $datetime_strings) |
|
603 | - { |
|
604 | - $content = '<div class="ee-registration-event-datetimes-container">'; |
|
605 | - $expand_toggle = count($datetime_strings) > 1 |
|
606 | - ? ' <span title="' . esc_attr__('Click to view all dates', 'event_espresso') |
|
607 | - . '" class="ee-js ee-more-datetimes-toggle dashicons dashicons-plus"></span>' |
|
608 | - : ''; |
|
609 | - // get first item for initial visibility |
|
610 | - $content .= '<div class="left">' . array_shift($datetime_strings) . '</div>'; |
|
611 | - $content .= $expand_toggle; |
|
612 | - if ($datetime_strings) { |
|
613 | - $content .= '<div style="clear:both"></div>'; |
|
614 | - $content .= '<div class="ee-registration-event-datetimes-container more-items hidden">'; |
|
615 | - $content .= implode("<br />", $datetime_strings); |
|
616 | - $content .= '</div>'; |
|
617 | - } |
|
618 | - $content .= '</div>'; |
|
619 | - return $content; |
|
620 | - } |
|
596 | + /** |
|
597 | + * Receives an array of datetime strings to display and converts them to the html container for the column. |
|
598 | + * |
|
599 | + * @param array $datetime_strings |
|
600 | + * @return string |
|
601 | + */ |
|
602 | + public function generateDisplayForDateTimes(array $datetime_strings) |
|
603 | + { |
|
604 | + $content = '<div class="ee-registration-event-datetimes-container">'; |
|
605 | + $expand_toggle = count($datetime_strings) > 1 |
|
606 | + ? ' <span title="' . esc_attr__('Click to view all dates', 'event_espresso') |
|
607 | + . '" class="ee-js ee-more-datetimes-toggle dashicons dashicons-plus"></span>' |
|
608 | + : ''; |
|
609 | + // get first item for initial visibility |
|
610 | + $content .= '<div class="left">' . array_shift($datetime_strings) . '</div>'; |
|
611 | + $content .= $expand_toggle; |
|
612 | + if ($datetime_strings) { |
|
613 | + $content .= '<div style="clear:both"></div>'; |
|
614 | + $content .= '<div class="ee-registration-event-datetimes-container more-items hidden">'; |
|
615 | + $content .= implode("<br />", $datetime_strings); |
|
616 | + $content .= '</div>'; |
|
617 | + } |
|
618 | + $content .= '</div>'; |
|
619 | + return $content; |
|
620 | + } |
|
621 | 621 | |
622 | 622 | |
623 | - /** |
|
624 | - * column_ATT_fname |
|
625 | - * |
|
626 | - * @access public |
|
627 | - * @param \EE_Registration $item |
|
628 | - * @return string |
|
629 | - * @throws EE_Error |
|
630 | - * @throws InvalidArgumentException |
|
631 | - * @throws InvalidDataTypeException |
|
632 | - * @throws InvalidInterfaceException |
|
633 | - * @throws ReflectionException |
|
634 | - */ |
|
635 | - public function column_ATT_fname(EE_Registration $item) |
|
636 | - { |
|
637 | - $attendee = $item->attendee(); |
|
638 | - $edit_lnk_url = EE_Admin_Page::add_query_args_and_nonce( |
|
639 | - array( |
|
640 | - 'action' => 'view_registration', |
|
641 | - '_REG_ID' => $item->ID(), |
|
642 | - ), |
|
643 | - REG_ADMIN_URL |
|
644 | - ); |
|
645 | - $attendee_name = $attendee instanceof EE_Attendee ? $attendee->full_name() : ''; |
|
646 | - $link = EE_Registry::instance()->CAP->current_user_can( |
|
647 | - 'ee_read_registration', |
|
648 | - 'espresso_registrations_view_registration', |
|
649 | - $item->ID() |
|
650 | - ) |
|
651 | - ? '<a href="' |
|
652 | - . $edit_lnk_url |
|
653 | - . '" title="' |
|
654 | - . esc_attr__('View Registration Details', 'event_espresso') |
|
655 | - . '">' |
|
656 | - . $attendee_name |
|
657 | - . '</a>' : $attendee_name; |
|
658 | - $link .= $item->count() === 1 |
|
659 | - ? ' <sup><div class="dashicons dashicons-star-filled lt-blue-icon ee-icon-size-8"></div></sup>' : ''; |
|
660 | - $t = $item->get_first_related('Transaction'); |
|
661 | - $payment_count = $t instanceof EE_Transaction ? $t->count_related('Payment') : 0; |
|
662 | - // append group count to name |
|
663 | - $link .= ' ' . sprintf(__('(%1$s / %2$s)', 'event_espresso'), $item->count(), $item->group_size()); |
|
664 | - // append reg_code |
|
665 | - $link .= '<br>' . sprintf(__('Reg Code: %s', 'event_espresso'), $item->get('REG_code')); |
|
666 | - // reg status text for accessibility |
|
667 | - $link .= '<br><span class="ee-status-text-small">' |
|
668 | - . EEH_Template::pretty_status($item->status_ID(), false, 'sentence') |
|
669 | - . '</span>'; |
|
670 | - // trash/restore/delete actions |
|
671 | - $actions = array(); |
|
672 | - if ($this->_view !== 'trash' |
|
673 | - && $payment_count === 0 |
|
674 | - && EE_Registry::instance()->CAP->current_user_can( |
|
675 | - 'ee_delete_registration', |
|
676 | - 'espresso_registrations_trash_registrations', |
|
677 | - $item->ID() |
|
678 | - )) { |
|
679 | - $trash_lnk_url = EE_Admin_Page::add_query_args_and_nonce( |
|
680 | - array( |
|
681 | - 'action' => 'trash_registrations', |
|
682 | - '_REG_ID' => $item->ID(), |
|
683 | - ), |
|
684 | - REG_ADMIN_URL |
|
685 | - ); |
|
686 | - $actions['trash'] = '<a href="' |
|
687 | - . $trash_lnk_url |
|
688 | - . '" title="' |
|
689 | - . esc_attr__('Trash Registration', 'event_espresso') |
|
690 | - . '">' . __('Trash', 'event_espresso') . '</a>'; |
|
691 | - } elseif ($this->_view === 'trash') { |
|
692 | - // restore registration link |
|
693 | - if (EE_Registry::instance()->CAP->current_user_can( |
|
694 | - 'ee_delete_registration', |
|
695 | - 'espresso_registrations_restore_registrations', |
|
696 | - $item->ID() |
|
697 | - )) { |
|
698 | - $restore_lnk_url = EE_Admin_Page::add_query_args_and_nonce( |
|
699 | - array( |
|
700 | - 'action' => 'restore_registrations', |
|
701 | - '_REG_ID' => $item->ID(), |
|
702 | - ), |
|
703 | - REG_ADMIN_URL |
|
704 | - ); |
|
705 | - $actions['restore'] = '<a href="' |
|
706 | - . $restore_lnk_url |
|
707 | - . '" title="' |
|
708 | - . esc_attr__('Restore Registration', 'event_espresso') . '">' |
|
709 | - . __('Restore', 'event_espresso') . '</a>'; |
|
710 | - } |
|
711 | - if (EE_Registry::instance()->CAP->current_user_can( |
|
712 | - 'ee_delete_registration', |
|
713 | - 'espresso_registrations_ee_delete_registrations', |
|
714 | - $item->ID() |
|
715 | - )) { |
|
716 | - $delete_lnk_url = EE_Admin_Page::add_query_args_and_nonce( |
|
717 | - array( |
|
718 | - 'action' => 'delete_registrations', |
|
719 | - '_REG_ID' => $item->ID(), |
|
720 | - ), |
|
721 | - REG_ADMIN_URL |
|
722 | - ); |
|
723 | - $actions['delete'] = '<a href="' |
|
724 | - . $delete_lnk_url |
|
725 | - . '" title="' |
|
726 | - . esc_attr__('Delete Registration Permanently', 'event_espresso') |
|
727 | - . '">' |
|
728 | - . __('Delete', 'event_espresso') |
|
729 | - . '</a>'; |
|
730 | - } |
|
731 | - } |
|
732 | - return sprintf('%1$s %2$s', $link, $this->row_actions($actions)); |
|
733 | - } |
|
623 | + /** |
|
624 | + * column_ATT_fname |
|
625 | + * |
|
626 | + * @access public |
|
627 | + * @param \EE_Registration $item |
|
628 | + * @return string |
|
629 | + * @throws EE_Error |
|
630 | + * @throws InvalidArgumentException |
|
631 | + * @throws InvalidDataTypeException |
|
632 | + * @throws InvalidInterfaceException |
|
633 | + * @throws ReflectionException |
|
634 | + */ |
|
635 | + public function column_ATT_fname(EE_Registration $item) |
|
636 | + { |
|
637 | + $attendee = $item->attendee(); |
|
638 | + $edit_lnk_url = EE_Admin_Page::add_query_args_and_nonce( |
|
639 | + array( |
|
640 | + 'action' => 'view_registration', |
|
641 | + '_REG_ID' => $item->ID(), |
|
642 | + ), |
|
643 | + REG_ADMIN_URL |
|
644 | + ); |
|
645 | + $attendee_name = $attendee instanceof EE_Attendee ? $attendee->full_name() : ''; |
|
646 | + $link = EE_Registry::instance()->CAP->current_user_can( |
|
647 | + 'ee_read_registration', |
|
648 | + 'espresso_registrations_view_registration', |
|
649 | + $item->ID() |
|
650 | + ) |
|
651 | + ? '<a href="' |
|
652 | + . $edit_lnk_url |
|
653 | + . '" title="' |
|
654 | + . esc_attr__('View Registration Details', 'event_espresso') |
|
655 | + . '">' |
|
656 | + . $attendee_name |
|
657 | + . '</a>' : $attendee_name; |
|
658 | + $link .= $item->count() === 1 |
|
659 | + ? ' <sup><div class="dashicons dashicons-star-filled lt-blue-icon ee-icon-size-8"></div></sup>' : ''; |
|
660 | + $t = $item->get_first_related('Transaction'); |
|
661 | + $payment_count = $t instanceof EE_Transaction ? $t->count_related('Payment') : 0; |
|
662 | + // append group count to name |
|
663 | + $link .= ' ' . sprintf(__('(%1$s / %2$s)', 'event_espresso'), $item->count(), $item->group_size()); |
|
664 | + // append reg_code |
|
665 | + $link .= '<br>' . sprintf(__('Reg Code: %s', 'event_espresso'), $item->get('REG_code')); |
|
666 | + // reg status text for accessibility |
|
667 | + $link .= '<br><span class="ee-status-text-small">' |
|
668 | + . EEH_Template::pretty_status($item->status_ID(), false, 'sentence') |
|
669 | + . '</span>'; |
|
670 | + // trash/restore/delete actions |
|
671 | + $actions = array(); |
|
672 | + if ($this->_view !== 'trash' |
|
673 | + && $payment_count === 0 |
|
674 | + && EE_Registry::instance()->CAP->current_user_can( |
|
675 | + 'ee_delete_registration', |
|
676 | + 'espresso_registrations_trash_registrations', |
|
677 | + $item->ID() |
|
678 | + )) { |
|
679 | + $trash_lnk_url = EE_Admin_Page::add_query_args_and_nonce( |
|
680 | + array( |
|
681 | + 'action' => 'trash_registrations', |
|
682 | + '_REG_ID' => $item->ID(), |
|
683 | + ), |
|
684 | + REG_ADMIN_URL |
|
685 | + ); |
|
686 | + $actions['trash'] = '<a href="' |
|
687 | + . $trash_lnk_url |
|
688 | + . '" title="' |
|
689 | + . esc_attr__('Trash Registration', 'event_espresso') |
|
690 | + . '">' . __('Trash', 'event_espresso') . '</a>'; |
|
691 | + } elseif ($this->_view === 'trash') { |
|
692 | + // restore registration link |
|
693 | + if (EE_Registry::instance()->CAP->current_user_can( |
|
694 | + 'ee_delete_registration', |
|
695 | + 'espresso_registrations_restore_registrations', |
|
696 | + $item->ID() |
|
697 | + )) { |
|
698 | + $restore_lnk_url = EE_Admin_Page::add_query_args_and_nonce( |
|
699 | + array( |
|
700 | + 'action' => 'restore_registrations', |
|
701 | + '_REG_ID' => $item->ID(), |
|
702 | + ), |
|
703 | + REG_ADMIN_URL |
|
704 | + ); |
|
705 | + $actions['restore'] = '<a href="' |
|
706 | + . $restore_lnk_url |
|
707 | + . '" title="' |
|
708 | + . esc_attr__('Restore Registration', 'event_espresso') . '">' |
|
709 | + . __('Restore', 'event_espresso') . '</a>'; |
|
710 | + } |
|
711 | + if (EE_Registry::instance()->CAP->current_user_can( |
|
712 | + 'ee_delete_registration', |
|
713 | + 'espresso_registrations_ee_delete_registrations', |
|
714 | + $item->ID() |
|
715 | + )) { |
|
716 | + $delete_lnk_url = EE_Admin_Page::add_query_args_and_nonce( |
|
717 | + array( |
|
718 | + 'action' => 'delete_registrations', |
|
719 | + '_REG_ID' => $item->ID(), |
|
720 | + ), |
|
721 | + REG_ADMIN_URL |
|
722 | + ); |
|
723 | + $actions['delete'] = '<a href="' |
|
724 | + . $delete_lnk_url |
|
725 | + . '" title="' |
|
726 | + . esc_attr__('Delete Registration Permanently', 'event_espresso') |
|
727 | + . '">' |
|
728 | + . __('Delete', 'event_espresso') |
|
729 | + . '</a>'; |
|
730 | + } |
|
731 | + } |
|
732 | + return sprintf('%1$s %2$s', $link, $this->row_actions($actions)); |
|
733 | + } |
|
734 | 734 | |
735 | 735 | |
736 | - /** |
|
737 | - * column_ATT_email |
|
738 | - * |
|
739 | - * @access public |
|
740 | - * @param \EE_Registration $item |
|
741 | - * @return string |
|
742 | - * @throws EE_Error |
|
743 | - * @throws InvalidArgumentException |
|
744 | - * @throws InvalidDataTypeException |
|
745 | - * @throws InvalidInterfaceException |
|
746 | - * @throws ReflectionException |
|
747 | - */ |
|
748 | - public function column_ATT_email(EE_Registration $item) |
|
749 | - { |
|
750 | - $attendee = $item->get_first_related('Attendee'); |
|
751 | - return ! $attendee instanceof EE_Attendee ? __('No attached contact record.', 'event_espresso') |
|
752 | - : $attendee->email(); |
|
753 | - } |
|
736 | + /** |
|
737 | + * column_ATT_email |
|
738 | + * |
|
739 | + * @access public |
|
740 | + * @param \EE_Registration $item |
|
741 | + * @return string |
|
742 | + * @throws EE_Error |
|
743 | + * @throws InvalidArgumentException |
|
744 | + * @throws InvalidDataTypeException |
|
745 | + * @throws InvalidInterfaceException |
|
746 | + * @throws ReflectionException |
|
747 | + */ |
|
748 | + public function column_ATT_email(EE_Registration $item) |
|
749 | + { |
|
750 | + $attendee = $item->get_first_related('Attendee'); |
|
751 | + return ! $attendee instanceof EE_Attendee ? __('No attached contact record.', 'event_espresso') |
|
752 | + : $attendee->email(); |
|
753 | + } |
|
754 | 754 | |
755 | 755 | |
756 | - /** |
|
757 | - * column__REG_count |
|
758 | - * |
|
759 | - * @access public |
|
760 | - * @param \EE_Registration $item |
|
761 | - * @return string |
|
762 | - */ |
|
763 | - public function column__REG_count(EE_Registration $item) |
|
764 | - { |
|
765 | - return sprintf(__('%1$s / %2$s', 'event_espresso'), $item->count(), $item->group_size()); |
|
766 | - } |
|
756 | + /** |
|
757 | + * column__REG_count |
|
758 | + * |
|
759 | + * @access public |
|
760 | + * @param \EE_Registration $item |
|
761 | + * @return string |
|
762 | + */ |
|
763 | + public function column__REG_count(EE_Registration $item) |
|
764 | + { |
|
765 | + return sprintf(__('%1$s / %2$s', 'event_espresso'), $item->count(), $item->group_size()); |
|
766 | + } |
|
767 | 767 | |
768 | 768 | |
769 | - /** |
|
770 | - * column_PRC_amount |
|
771 | - * |
|
772 | - * @access public |
|
773 | - * @param \EE_Registration $item |
|
774 | - * @return string |
|
775 | - * @throws EE_Error |
|
776 | - */ |
|
777 | - public function column_PRC_amount(EE_Registration $item) |
|
778 | - { |
|
779 | - $ticket = $item->ticket(); |
|
780 | - $content = isset($_GET['event_id']) && $ticket instanceof EE_Ticket ? '<span class="TKT_name">' |
|
781 | - . $ticket->name() |
|
782 | - . '</span><br />' : ''; |
|
783 | - if ($item->final_price() > 0) { |
|
784 | - $content .= '<span class="reg-pad-rght">' . $item->pretty_final_price() . '</span>'; |
|
785 | - } else { |
|
786 | - // free event |
|
787 | - $content .= '<span class="reg-overview-free-event-spn reg-pad-rght">' |
|
788 | - . __('free', 'event_espresso') |
|
789 | - . '</span>'; |
|
790 | - } |
|
791 | - return $content; |
|
792 | - } |
|
769 | + /** |
|
770 | + * column_PRC_amount |
|
771 | + * |
|
772 | + * @access public |
|
773 | + * @param \EE_Registration $item |
|
774 | + * @return string |
|
775 | + * @throws EE_Error |
|
776 | + */ |
|
777 | + public function column_PRC_amount(EE_Registration $item) |
|
778 | + { |
|
779 | + $ticket = $item->ticket(); |
|
780 | + $content = isset($_GET['event_id']) && $ticket instanceof EE_Ticket ? '<span class="TKT_name">' |
|
781 | + . $ticket->name() |
|
782 | + . '</span><br />' : ''; |
|
783 | + if ($item->final_price() > 0) { |
|
784 | + $content .= '<span class="reg-pad-rght">' . $item->pretty_final_price() . '</span>'; |
|
785 | + } else { |
|
786 | + // free event |
|
787 | + $content .= '<span class="reg-overview-free-event-spn reg-pad-rght">' |
|
788 | + . __('free', 'event_espresso') |
|
789 | + . '</span>'; |
|
790 | + } |
|
791 | + return $content; |
|
792 | + } |
|
793 | 793 | |
794 | 794 | |
795 | - /** |
|
796 | - * column__REG_final_price |
|
797 | - * |
|
798 | - * @access public |
|
799 | - * @param \EE_Registration $item |
|
800 | - * @return string |
|
801 | - * @throws EE_Error |
|
802 | - */ |
|
803 | - public function column__REG_final_price(EE_Registration $item) |
|
804 | - { |
|
805 | - $ticket = $item->ticket(); |
|
806 | - $content = isset($_GET['event_id']) || ! $ticket instanceof EE_Ticket |
|
807 | - ? '' |
|
808 | - : '<span class="TKT_name">' |
|
809 | - . $ticket->name() |
|
810 | - . '</span><br />'; |
|
811 | - $content .= '<span class="reg-pad-rght">' . $item->pretty_final_price() . '</span>'; |
|
812 | - return $content; |
|
813 | - } |
|
795 | + /** |
|
796 | + * column__REG_final_price |
|
797 | + * |
|
798 | + * @access public |
|
799 | + * @param \EE_Registration $item |
|
800 | + * @return string |
|
801 | + * @throws EE_Error |
|
802 | + */ |
|
803 | + public function column__REG_final_price(EE_Registration $item) |
|
804 | + { |
|
805 | + $ticket = $item->ticket(); |
|
806 | + $content = isset($_GET['event_id']) || ! $ticket instanceof EE_Ticket |
|
807 | + ? '' |
|
808 | + : '<span class="TKT_name">' |
|
809 | + . $ticket->name() |
|
810 | + . '</span><br />'; |
|
811 | + $content .= '<span class="reg-pad-rght">' . $item->pretty_final_price() . '</span>'; |
|
812 | + return $content; |
|
813 | + } |
|
814 | 814 | |
815 | 815 | |
816 | - /** |
|
817 | - * column__REG_paid |
|
818 | - * |
|
819 | - * @access public |
|
820 | - * @param \EE_Registration $item |
|
821 | - * @return string |
|
822 | - * @throws EE_Error |
|
823 | - */ |
|
824 | - public function column__REG_paid(EE_Registration $item) |
|
825 | - { |
|
826 | - $payment_method = $item->payment_method(); |
|
827 | - $payment_method_name = $payment_method instanceof EE_Payment_Method ? $payment_method->admin_name() |
|
828 | - : __('Unknown', 'event_espresso'); |
|
829 | - $content = '<span class="reg-pad-rght">' . $item->pretty_paid() . '</span>'; |
|
830 | - if ($item->paid() > 0) { |
|
831 | - $content .= '<br><span class="ee-status-text-small">' |
|
832 | - . sprintf( |
|
833 | - __('...via %s', 'event_espresso'), |
|
834 | - $payment_method_name |
|
835 | - ) |
|
836 | - . '</span>'; |
|
837 | - } |
|
838 | - return $content; |
|
839 | - } |
|
816 | + /** |
|
817 | + * column__REG_paid |
|
818 | + * |
|
819 | + * @access public |
|
820 | + * @param \EE_Registration $item |
|
821 | + * @return string |
|
822 | + * @throws EE_Error |
|
823 | + */ |
|
824 | + public function column__REG_paid(EE_Registration $item) |
|
825 | + { |
|
826 | + $payment_method = $item->payment_method(); |
|
827 | + $payment_method_name = $payment_method instanceof EE_Payment_Method ? $payment_method->admin_name() |
|
828 | + : __('Unknown', 'event_espresso'); |
|
829 | + $content = '<span class="reg-pad-rght">' . $item->pretty_paid() . '</span>'; |
|
830 | + if ($item->paid() > 0) { |
|
831 | + $content .= '<br><span class="ee-status-text-small">' |
|
832 | + . sprintf( |
|
833 | + __('...via %s', 'event_espresso'), |
|
834 | + $payment_method_name |
|
835 | + ) |
|
836 | + . '</span>'; |
|
837 | + } |
|
838 | + return $content; |
|
839 | + } |
|
840 | 840 | |
841 | 841 | |
842 | - /** |
|
843 | - * column_TXN_total |
|
844 | - * |
|
845 | - * @access public |
|
846 | - * @param \EE_Registration $item |
|
847 | - * @return string |
|
848 | - * @throws EE_Error |
|
849 | - * @throws EntityNotFoundException |
|
850 | - * @throws InvalidArgumentException |
|
851 | - * @throws InvalidDataTypeException |
|
852 | - * @throws InvalidInterfaceException |
|
853 | - */ |
|
854 | - public function column_TXN_total(EE_Registration $item) |
|
855 | - { |
|
856 | - if ($item->transaction()) { |
|
857 | - $view_txn_lnk_url = EE_Admin_Page::add_query_args_and_nonce( |
|
858 | - array( |
|
859 | - 'action' => 'view_transaction', |
|
860 | - 'TXN_ID' => $item->transaction_ID(), |
|
861 | - ), |
|
862 | - TXN_ADMIN_URL |
|
863 | - ); |
|
864 | - return EE_Registry::instance()->CAP->current_user_can( |
|
865 | - 'ee_read_transaction', |
|
866 | - 'espresso_transactions_view_transaction', |
|
867 | - $item->transaction_ID() |
|
868 | - ) |
|
869 | - ? '<span class="reg-pad-rght"><a class="status-' |
|
870 | - . $item->transaction()->status_ID() |
|
871 | - . '" href="' |
|
872 | - . $view_txn_lnk_url |
|
873 | - . '" title="' |
|
874 | - . esc_attr__('View Transaction', 'event_espresso') |
|
875 | - . '">' |
|
876 | - . $item->transaction()->pretty_total() |
|
877 | - . '</a></span>' : '<span class="reg-pad-rght">' . $item->transaction()->pretty_total() . '</span>'; |
|
878 | - } else { |
|
879 | - return __("None", "event_espresso"); |
|
880 | - } |
|
881 | - } |
|
842 | + /** |
|
843 | + * column_TXN_total |
|
844 | + * |
|
845 | + * @access public |
|
846 | + * @param \EE_Registration $item |
|
847 | + * @return string |
|
848 | + * @throws EE_Error |
|
849 | + * @throws EntityNotFoundException |
|
850 | + * @throws InvalidArgumentException |
|
851 | + * @throws InvalidDataTypeException |
|
852 | + * @throws InvalidInterfaceException |
|
853 | + */ |
|
854 | + public function column_TXN_total(EE_Registration $item) |
|
855 | + { |
|
856 | + if ($item->transaction()) { |
|
857 | + $view_txn_lnk_url = EE_Admin_Page::add_query_args_and_nonce( |
|
858 | + array( |
|
859 | + 'action' => 'view_transaction', |
|
860 | + 'TXN_ID' => $item->transaction_ID(), |
|
861 | + ), |
|
862 | + TXN_ADMIN_URL |
|
863 | + ); |
|
864 | + return EE_Registry::instance()->CAP->current_user_can( |
|
865 | + 'ee_read_transaction', |
|
866 | + 'espresso_transactions_view_transaction', |
|
867 | + $item->transaction_ID() |
|
868 | + ) |
|
869 | + ? '<span class="reg-pad-rght"><a class="status-' |
|
870 | + . $item->transaction()->status_ID() |
|
871 | + . '" href="' |
|
872 | + . $view_txn_lnk_url |
|
873 | + . '" title="' |
|
874 | + . esc_attr__('View Transaction', 'event_espresso') |
|
875 | + . '">' |
|
876 | + . $item->transaction()->pretty_total() |
|
877 | + . '</a></span>' : '<span class="reg-pad-rght">' . $item->transaction()->pretty_total() . '</span>'; |
|
878 | + } else { |
|
879 | + return __("None", "event_espresso"); |
|
880 | + } |
|
881 | + } |
|
882 | 882 | |
883 | 883 | |
884 | - /** |
|
885 | - * column_TXN_paid |
|
886 | - * |
|
887 | - * @access public |
|
888 | - * @param \EE_Registration $item |
|
889 | - * @return string |
|
890 | - * @throws EE_Error |
|
891 | - * @throws EntityNotFoundException |
|
892 | - * @throws InvalidArgumentException |
|
893 | - * @throws InvalidDataTypeException |
|
894 | - * @throws InvalidInterfaceException |
|
895 | - */ |
|
896 | - public function column_TXN_paid(EE_Registration $item) |
|
897 | - { |
|
898 | - if ($item->count() === 1) { |
|
899 | - $transaction = $item->transaction() ? $item->transaction() : EE_Transaction::new_instance(); |
|
900 | - if ($transaction->paid() >= $transaction->total()) { |
|
901 | - return '<span class="reg-pad-rght"><div class="dashicons dashicons-yes green-icon"></div></span>'; |
|
902 | - } else { |
|
903 | - $view_txn_lnk_url = EE_Admin_Page::add_query_args_and_nonce( |
|
904 | - array( |
|
905 | - 'action' => 'view_transaction', |
|
906 | - 'TXN_ID' => $item->transaction_ID(), |
|
907 | - ), |
|
908 | - TXN_ADMIN_URL |
|
909 | - ); |
|
910 | - return EE_Registry::instance()->CAP->current_user_can( |
|
911 | - 'ee_read_transaction', |
|
912 | - 'espresso_transactions_view_transaction', |
|
913 | - $item->transaction_ID() |
|
914 | - ) |
|
915 | - ? '<span class="reg-pad-rght"><a class="status-' |
|
916 | - . $transaction->status_ID() |
|
917 | - . '" href="' |
|
918 | - . $view_txn_lnk_url |
|
919 | - . '" title="' |
|
920 | - . esc_attr__('View Transaction', 'event_espresso') |
|
921 | - . '">' |
|
922 | - . $item->transaction()->pretty_paid() |
|
923 | - . '</a><span>' : '<span class="reg-pad-rght">' . $item->transaction()->pretty_paid() . '</span>'; |
|
924 | - } |
|
925 | - } |
|
926 | - return ' '; |
|
927 | - } |
|
884 | + /** |
|
885 | + * column_TXN_paid |
|
886 | + * |
|
887 | + * @access public |
|
888 | + * @param \EE_Registration $item |
|
889 | + * @return string |
|
890 | + * @throws EE_Error |
|
891 | + * @throws EntityNotFoundException |
|
892 | + * @throws InvalidArgumentException |
|
893 | + * @throws InvalidDataTypeException |
|
894 | + * @throws InvalidInterfaceException |
|
895 | + */ |
|
896 | + public function column_TXN_paid(EE_Registration $item) |
|
897 | + { |
|
898 | + if ($item->count() === 1) { |
|
899 | + $transaction = $item->transaction() ? $item->transaction() : EE_Transaction::new_instance(); |
|
900 | + if ($transaction->paid() >= $transaction->total()) { |
|
901 | + return '<span class="reg-pad-rght"><div class="dashicons dashicons-yes green-icon"></div></span>'; |
|
902 | + } else { |
|
903 | + $view_txn_lnk_url = EE_Admin_Page::add_query_args_and_nonce( |
|
904 | + array( |
|
905 | + 'action' => 'view_transaction', |
|
906 | + 'TXN_ID' => $item->transaction_ID(), |
|
907 | + ), |
|
908 | + TXN_ADMIN_URL |
|
909 | + ); |
|
910 | + return EE_Registry::instance()->CAP->current_user_can( |
|
911 | + 'ee_read_transaction', |
|
912 | + 'espresso_transactions_view_transaction', |
|
913 | + $item->transaction_ID() |
|
914 | + ) |
|
915 | + ? '<span class="reg-pad-rght"><a class="status-' |
|
916 | + . $transaction->status_ID() |
|
917 | + . '" href="' |
|
918 | + . $view_txn_lnk_url |
|
919 | + . '" title="' |
|
920 | + . esc_attr__('View Transaction', 'event_espresso') |
|
921 | + . '">' |
|
922 | + . $item->transaction()->pretty_paid() |
|
923 | + . '</a><span>' : '<span class="reg-pad-rght">' . $item->transaction()->pretty_paid() . '</span>'; |
|
924 | + } |
|
925 | + } |
|
926 | + return ' '; |
|
927 | + } |
|
928 | 928 | |
929 | 929 | |
930 | - /** |
|
931 | - * column_actions |
|
932 | - * |
|
933 | - * @access public |
|
934 | - * @param \EE_Registration $item |
|
935 | - * @return string |
|
936 | - * @throws EE_Error |
|
937 | - * @throws InvalidArgumentException |
|
938 | - * @throws InvalidDataTypeException |
|
939 | - * @throws InvalidInterfaceException |
|
940 | - * @throws ReflectionException |
|
941 | - */ |
|
942 | - public function column_actions(EE_Registration $item) |
|
943 | - { |
|
944 | - $actions = array(); |
|
945 | - $attendee = $item->attendee(); |
|
946 | - $this->_set_related_details($item); |
|
947 | - // Build row actions |
|
948 | - $view_lnk_url = EE_Admin_Page::add_query_args_and_nonce( |
|
949 | - array( |
|
950 | - 'action' => 'view_registration', |
|
951 | - '_REG_ID' => $item->ID(), |
|
952 | - ), |
|
953 | - REG_ADMIN_URL |
|
954 | - ); |
|
955 | - $edit_lnk_url = EE_Admin_Page::add_query_args_and_nonce( |
|
956 | - array( |
|
957 | - 'action' => 'edit_attendee', |
|
958 | - 'post' => $item->attendee_ID(), |
|
959 | - ), |
|
960 | - REG_ADMIN_URL |
|
961 | - ); |
|
962 | - // page=attendees&event_admin_reports=resend_email®istration_id=43653465634&event_id=2&form_action=resend_email |
|
963 | - // $resend_reg_lnk_url_params = array( 'action'=>'resend_registration', '_REG_ID'=>$item->REG_ID ); |
|
964 | - $resend_reg_lnk_url = EE_Admin_Page::add_query_args_and_nonce( |
|
965 | - array( |
|
966 | - 'action' => 'resend_registration', |
|
967 | - '_REG_ID' => $item->ID(), |
|
968 | - ), |
|
969 | - REG_ADMIN_URL, |
|
970 | - true |
|
971 | - ); |
|
972 | - // Build row actions |
|
973 | - $actions['view_lnk'] = EE_Registry::instance()->CAP->current_user_can( |
|
974 | - 'ee_read_registration', |
|
975 | - 'espresso_registrations_view_registration', |
|
976 | - $item->ID() |
|
977 | - ) ? '<li><a href="' |
|
978 | - . $view_lnk_url |
|
979 | - . '" title="' |
|
980 | - . esc_attr__('View Registration Details', 'event_espresso') |
|
981 | - . '" class="tiny-text"> |
|
930 | + /** |
|
931 | + * column_actions |
|
932 | + * |
|
933 | + * @access public |
|
934 | + * @param \EE_Registration $item |
|
935 | + * @return string |
|
936 | + * @throws EE_Error |
|
937 | + * @throws InvalidArgumentException |
|
938 | + * @throws InvalidDataTypeException |
|
939 | + * @throws InvalidInterfaceException |
|
940 | + * @throws ReflectionException |
|
941 | + */ |
|
942 | + public function column_actions(EE_Registration $item) |
|
943 | + { |
|
944 | + $actions = array(); |
|
945 | + $attendee = $item->attendee(); |
|
946 | + $this->_set_related_details($item); |
|
947 | + // Build row actions |
|
948 | + $view_lnk_url = EE_Admin_Page::add_query_args_and_nonce( |
|
949 | + array( |
|
950 | + 'action' => 'view_registration', |
|
951 | + '_REG_ID' => $item->ID(), |
|
952 | + ), |
|
953 | + REG_ADMIN_URL |
|
954 | + ); |
|
955 | + $edit_lnk_url = EE_Admin_Page::add_query_args_and_nonce( |
|
956 | + array( |
|
957 | + 'action' => 'edit_attendee', |
|
958 | + 'post' => $item->attendee_ID(), |
|
959 | + ), |
|
960 | + REG_ADMIN_URL |
|
961 | + ); |
|
962 | + // page=attendees&event_admin_reports=resend_email®istration_id=43653465634&event_id=2&form_action=resend_email |
|
963 | + // $resend_reg_lnk_url_params = array( 'action'=>'resend_registration', '_REG_ID'=>$item->REG_ID ); |
|
964 | + $resend_reg_lnk_url = EE_Admin_Page::add_query_args_and_nonce( |
|
965 | + array( |
|
966 | + 'action' => 'resend_registration', |
|
967 | + '_REG_ID' => $item->ID(), |
|
968 | + ), |
|
969 | + REG_ADMIN_URL, |
|
970 | + true |
|
971 | + ); |
|
972 | + // Build row actions |
|
973 | + $actions['view_lnk'] = EE_Registry::instance()->CAP->current_user_can( |
|
974 | + 'ee_read_registration', |
|
975 | + 'espresso_registrations_view_registration', |
|
976 | + $item->ID() |
|
977 | + ) ? '<li><a href="' |
|
978 | + . $view_lnk_url |
|
979 | + . '" title="' |
|
980 | + . esc_attr__('View Registration Details', 'event_espresso') |
|
981 | + . '" class="tiny-text"> |
|
982 | 982 | <div class="dashicons dashicons-clipboard"></div> |
983 | 983 | </a> |
984 | 984 | </li>' |
985 | - : ''; |
|
986 | - $actions['edit_lnk'] = EE_Registry::instance()->CAP->current_user_can( |
|
987 | - 'ee_edit_contacts', |
|
988 | - 'espresso_registrations_edit_attendee' |
|
989 | - ) |
|
990 | - && $attendee instanceof EE_Attendee |
|
991 | - ? ' |
|
985 | + : ''; |
|
986 | + $actions['edit_lnk'] = EE_Registry::instance()->CAP->current_user_can( |
|
987 | + 'ee_edit_contacts', |
|
988 | + 'espresso_registrations_edit_attendee' |
|
989 | + ) |
|
990 | + && $attendee instanceof EE_Attendee |
|
991 | + ? ' |
|
992 | 992 | <li> |
993 | 993 | <a href="' . $edit_lnk_url . '" title="' |
994 | - . esc_attr__('Edit Contact Details', 'event_espresso') . '" class="tiny-text"> |
|
994 | + . esc_attr__('Edit Contact Details', 'event_espresso') . '" class="tiny-text"> |
|
995 | 995 | <div class="ee-icon ee-icon-user-edit ee-icon-size-16"></div> |
996 | 996 | </a> |
997 | 997 | </li>' : ''; |
998 | - $actions['resend_reg_lnk'] = $attendee instanceof EE_Attendee |
|
999 | - && EE_Registry::instance()->CAP->current_user_can( |
|
1000 | - 'ee_send_message', |
|
1001 | - 'espresso_registrations_resend_registration', |
|
1002 | - $item->ID() |
|
1003 | - ) |
|
1004 | - ? ' |
|
998 | + $actions['resend_reg_lnk'] = $attendee instanceof EE_Attendee |
|
999 | + && EE_Registry::instance()->CAP->current_user_can( |
|
1000 | + 'ee_send_message', |
|
1001 | + 'espresso_registrations_resend_registration', |
|
1002 | + $item->ID() |
|
1003 | + ) |
|
1004 | + ? ' |
|
1005 | 1005 | <li> |
1006 | 1006 | <a href="' |
1007 | - . $resend_reg_lnk_url |
|
1008 | - . '" title="' |
|
1009 | - . esc_attr__('Resend Registration Details', 'event_espresso') |
|
1010 | - . '" class="tiny-text"> |
|
1007 | + . $resend_reg_lnk_url |
|
1008 | + . '" title="' |
|
1009 | + . esc_attr__('Resend Registration Details', 'event_espresso') |
|
1010 | + . '" class="tiny-text"> |
|
1011 | 1011 | <div class="dashicons dashicons-email-alt"></div> |
1012 | 1012 | </a> |
1013 | 1013 | </li>' : ''; |
1014 | - // page=transactions&action=view_transaction&txn=256&_wpnonce=6414da4dbb |
|
1015 | - $view_txn_lnk_url = EE_Admin_Page::add_query_args_and_nonce( |
|
1016 | - array( |
|
1017 | - 'action' => 'view_transaction', |
|
1018 | - 'TXN_ID' => $this->_transaction_details['id'], |
|
1019 | - ), |
|
1020 | - TXN_ADMIN_URL |
|
1021 | - ); |
|
1022 | - $actions['view_txn_lnk'] = EE_Registry::instance()->CAP->current_user_can( |
|
1023 | - 'ee_read_transaction', |
|
1024 | - 'espresso_transactions_view_transaction', |
|
1025 | - $this->_transaction_details['id'] |
|
1026 | - ) |
|
1027 | - ? ' |
|
1014 | + // page=transactions&action=view_transaction&txn=256&_wpnonce=6414da4dbb |
|
1015 | + $view_txn_lnk_url = EE_Admin_Page::add_query_args_and_nonce( |
|
1016 | + array( |
|
1017 | + 'action' => 'view_transaction', |
|
1018 | + 'TXN_ID' => $this->_transaction_details['id'], |
|
1019 | + ), |
|
1020 | + TXN_ADMIN_URL |
|
1021 | + ); |
|
1022 | + $actions['view_txn_lnk'] = EE_Registry::instance()->CAP->current_user_can( |
|
1023 | + 'ee_read_transaction', |
|
1024 | + 'espresso_transactions_view_transaction', |
|
1025 | + $this->_transaction_details['id'] |
|
1026 | + ) |
|
1027 | + ? ' |
|
1028 | 1028 | <li> |
1029 | 1029 | <a class="ee-status-color-' |
1030 | - . $this->_transaction_details['status'] |
|
1031 | - . ' tiny-text" href="' |
|
1032 | - . $view_txn_lnk_url |
|
1033 | - . '" title="' |
|
1034 | - . $this->_transaction_details['title_attr'] |
|
1035 | - . '"> |
|
1030 | + . $this->_transaction_details['status'] |
|
1031 | + . ' tiny-text" href="' |
|
1032 | + . $view_txn_lnk_url |
|
1033 | + . '" title="' |
|
1034 | + . $this->_transaction_details['title_attr'] |
|
1035 | + . '"> |
|
1036 | 1036 | <div class="dashicons dashicons-cart"></div> |
1037 | 1037 | </a> |
1038 | 1038 | </li>' : ''; |
1039 | - // invoice link |
|
1040 | - $actions['dl_invoice_lnk'] = ''; |
|
1041 | - $dl_invoice_lnk_url = $item->invoice_url(); |
|
1042 | - // only show invoice link if message type is active. |
|
1043 | - if ($attendee instanceof EE_Attendee |
|
1044 | - && $item->is_primary_registrant() |
|
1045 | - && EEH_MSG_Template::is_mt_active('invoice') |
|
1046 | - ) { |
|
1047 | - $actions['dl_invoice_lnk'] = ' |
|
1039 | + // invoice link |
|
1040 | + $actions['dl_invoice_lnk'] = ''; |
|
1041 | + $dl_invoice_lnk_url = $item->invoice_url(); |
|
1042 | + // only show invoice link if message type is active. |
|
1043 | + if ($attendee instanceof EE_Attendee |
|
1044 | + && $item->is_primary_registrant() |
|
1045 | + && EEH_MSG_Template::is_mt_active('invoice') |
|
1046 | + ) { |
|
1047 | + $actions['dl_invoice_lnk'] = ' |
|
1048 | 1048 | <li> |
1049 | 1049 | <a title="' |
1050 | - . esc_attr__('View Transaction Invoice', 'event_espresso') |
|
1051 | - . '" target="_blank" href="' |
|
1052 | - . $dl_invoice_lnk_url |
|
1053 | - . '" class="tiny-text"> |
|
1050 | + . esc_attr__('View Transaction Invoice', 'event_espresso') |
|
1051 | + . '" target="_blank" href="' |
|
1052 | + . $dl_invoice_lnk_url |
|
1053 | + . '" class="tiny-text"> |
|
1054 | 1054 | <span class="dashicons dashicons-media-spreadsheet ee-icon-size-18"></span> |
1055 | 1055 | </a> |
1056 | 1056 | </li>'; |
1057 | - } |
|
1058 | - $actions['filtered_messages_link'] = ''; |
|
1059 | - // message list table link (filtered by REG_ID |
|
1060 | - if (EE_Registry::instance()->CAP->current_user_can('ee_read_global_messages', 'view_filtered_messages')) { |
|
1061 | - $actions['filtered_messages_link'] = '<li>' |
|
1062 | - . EEH_MSG_Template::get_message_action_link( |
|
1063 | - 'see_notifications_for', |
|
1064 | - null, |
|
1065 | - array('_REG_ID' => $item->ID()) |
|
1066 | - ) . '</li>'; |
|
1067 | - } |
|
1068 | - $actions = apply_filters('FHEE__EE_Registrations_List_Table__column_actions__actions', $actions, $item, $this); |
|
1069 | - return $this->_action_string(implode('', $actions), $item, 'ul', 'reg-overview-actions-ul'); |
|
1070 | - } |
|
1057 | + } |
|
1058 | + $actions['filtered_messages_link'] = ''; |
|
1059 | + // message list table link (filtered by REG_ID |
|
1060 | + if (EE_Registry::instance()->CAP->current_user_can('ee_read_global_messages', 'view_filtered_messages')) { |
|
1061 | + $actions['filtered_messages_link'] = '<li>' |
|
1062 | + . EEH_MSG_Template::get_message_action_link( |
|
1063 | + 'see_notifications_for', |
|
1064 | + null, |
|
1065 | + array('_REG_ID' => $item->ID()) |
|
1066 | + ) . '</li>'; |
|
1067 | + } |
|
1068 | + $actions = apply_filters('FHEE__EE_Registrations_List_Table__column_actions__actions', $actions, $item, $this); |
|
1069 | + return $this->_action_string(implode('', $actions), $item, 'ul', 'reg-overview-actions-ul'); |
|
1070 | + } |
|
1071 | 1071 | } |
@@ -53,10 +53,10 @@ discard block |
||
53 | 53 | */ |
54 | 54 | public function __construct(Registrations_Admin_Page $admin_page) |
55 | 55 | { |
56 | - if (! empty($_GET['event_id'])) { |
|
56 | + if ( ! empty($_GET['event_id'])) { |
|
57 | 57 | $extra_query_args = array(); |
58 | 58 | foreach ($admin_page->get_views() as $key => $view_details) { |
59 | - $extra_query_args[ $view_details['slug'] ] = array('event_id' => $_GET['event_id']); |
|
59 | + $extra_query_args[$view_details['slug']] = array('event_id' => $_GET['event_id']); |
|
60 | 60 | } |
61 | 61 | $this->_views = $admin_page->get_list_table_view_RLs($extra_query_args); |
62 | 62 | } |
@@ -156,12 +156,12 @@ discard block |
||
156 | 156 | ) |
157 | 157 | ) |
158 | 158 | ); |
159 | - if (!empty($filters)) { |
|
159 | + if ( ! empty($filters)) { |
|
160 | 160 | $this->_bottom_buttons['report_filtered']['extra_request']['filers'] = $filters; |
161 | 161 | } |
162 | 162 | $this->_primary_column = '_REG_ID'; |
163 | 163 | $this->_sortable_columns = array( |
164 | - '_REG_date' => array('_REG_date' => true), // true means its already sorted |
|
164 | + '_REG_date' => array('_REG_date' => true), // true means its already sorted |
|
165 | 165 | /** |
166 | 166 | * Allows users to change the default sort if they wish. |
167 | 167 | * Returning a falsey on this filter will result in the default sort to be by firstname rather than last |
@@ -193,7 +193,7 @@ discard block |
||
193 | 193 | { |
194 | 194 | $class = parent::_get_row_class($item); |
195 | 195 | // add status class |
196 | - $class .= ' ee-status-strip reg-status-' . $item->status_ID(); |
|
196 | + $class .= ' ee-status-strip reg-status-'.$item->status_ID(); |
|
197 | 197 | if ($this->_has_checkbox_column) { |
198 | 198 | $class .= ' has-checkbox-column'; |
199 | 199 | } |
@@ -358,12 +358,12 @@ discard block |
||
358 | 358 | // setup date query. |
359 | 359 | $beginning_string = EEM_Registration::instance()->convert_datetime_for_query( |
360 | 360 | 'REG_date', |
361 | - $this_year_r . '-' . $this_month_r . '-01' . ' ' . $time_start, |
|
361 | + $this_year_r.'-'.$this_month_r.'-01'.' '.$time_start, |
|
362 | 362 | 'Y-m-d H:i:s' |
363 | 363 | ); |
364 | 364 | $end_string = EEM_Registration::instance()->convert_datetime_for_query( |
365 | 365 | 'REG_date', |
366 | - $this_year_r . '-' . $this_month_r . '-' . $days_this_month . ' ' . $time_end, |
|
366 | + $this_year_r.'-'.$this_month_r.'-'.$days_this_month.' '.$time_end, |
|
367 | 367 | 'Y-m-d H:i:s' |
368 | 368 | ); |
369 | 369 | $_where['REG_date'] = array( |
@@ -400,12 +400,12 @@ discard block |
||
400 | 400 | array( |
401 | 401 | EEM_Registration::instance()->convert_datetime_for_query( |
402 | 402 | 'REG_date', |
403 | - $current_date . $time_start, |
|
403 | + $current_date.$time_start, |
|
404 | 404 | 'Y-m-d H:i:s' |
405 | 405 | ), |
406 | 406 | EEM_Registration::instance()->convert_datetime_for_query( |
407 | 407 | 'REG_date', |
408 | - $current_date . $time_end, |
|
408 | + $current_date.$time_end, |
|
409 | 409 | 'Y-m-d H:i:s' |
410 | 410 | ), |
411 | 411 | ), |
@@ -465,8 +465,8 @@ discard block |
||
465 | 465 | $content .= '<div class="show-on-mobile-view-only">'; |
466 | 466 | $content .= '<br>'; |
467 | 467 | $content .= $attendee instanceof EE_Attendee ? $attendee->full_name() : ''; |
468 | - $content .= ' ' . sprintf(__('(%1$s / %2$s)', 'event_espresso'), $item->count(), $item->group_size()); |
|
469 | - $content .= '<br>' . sprintf(__('Reg Code: %s', 'event_espresso'), $item->get('REG_code')); |
|
468 | + $content .= ' '.sprintf(__('(%1$s / %2$s)', 'event_espresso'), $item->count(), $item->group_size()); |
|
469 | + $content .= '<br>'.sprintf(__('Reg Code: %s', 'event_espresso'), $item->get('REG_code')); |
|
470 | 470 | $content .= '</div>'; |
471 | 471 | return $content; |
472 | 472 | } |
@@ -551,12 +551,12 @@ discard block |
||
551 | 551 | . $event_name |
552 | 552 | . '</a>' : $event_name; |
553 | 553 | $edit_event_url = EE_Admin_Page::add_query_args_and_nonce(array('event_id' => $EVT_ID), REG_ADMIN_URL); |
554 | - $actions['event_filter'] = '<a href="' . $edit_event_url . '" title="'; |
|
554 | + $actions['event_filter'] = '<a href="'.$edit_event_url.'" title="'; |
|
555 | 555 | $actions['event_filter'] .= sprintf( |
556 | 556 | esc_attr__('Filter this list to only show registrations for %s', 'event_espresso'), |
557 | 557 | $event_name |
558 | 558 | ); |
559 | - $actions['event_filter'] .= '">' . __('View Registrations', 'event_espresso') . '</a>'; |
|
559 | + $actions['event_filter'] .= '">'.__('View Registrations', 'event_espresso').'</a>'; |
|
560 | 560 | } else { |
561 | 561 | $edit_event = $event_name; |
562 | 562 | $actions['event_filter'] = ''; |
@@ -603,11 +603,11 @@ discard block |
||
603 | 603 | { |
604 | 604 | $content = '<div class="ee-registration-event-datetimes-container">'; |
605 | 605 | $expand_toggle = count($datetime_strings) > 1 |
606 | - ? ' <span title="' . esc_attr__('Click to view all dates', 'event_espresso') |
|
606 | + ? ' <span title="'.esc_attr__('Click to view all dates', 'event_espresso') |
|
607 | 607 | . '" class="ee-js ee-more-datetimes-toggle dashicons dashicons-plus"></span>' |
608 | 608 | : ''; |
609 | 609 | // get first item for initial visibility |
610 | - $content .= '<div class="left">' . array_shift($datetime_strings) . '</div>'; |
|
610 | + $content .= '<div class="left">'.array_shift($datetime_strings).'</div>'; |
|
611 | 611 | $content .= $expand_toggle; |
612 | 612 | if ($datetime_strings) { |
613 | 613 | $content .= '<div style="clear:both"></div>'; |
@@ -660,9 +660,9 @@ discard block |
||
660 | 660 | $t = $item->get_first_related('Transaction'); |
661 | 661 | $payment_count = $t instanceof EE_Transaction ? $t->count_related('Payment') : 0; |
662 | 662 | // append group count to name |
663 | - $link .= ' ' . sprintf(__('(%1$s / %2$s)', 'event_espresso'), $item->count(), $item->group_size()); |
|
663 | + $link .= ' '.sprintf(__('(%1$s / %2$s)', 'event_espresso'), $item->count(), $item->group_size()); |
|
664 | 664 | // append reg_code |
665 | - $link .= '<br>' . sprintf(__('Reg Code: %s', 'event_espresso'), $item->get('REG_code')); |
|
665 | + $link .= '<br>'.sprintf(__('Reg Code: %s', 'event_espresso'), $item->get('REG_code')); |
|
666 | 666 | // reg status text for accessibility |
667 | 667 | $link .= '<br><span class="ee-status-text-small">' |
668 | 668 | . EEH_Template::pretty_status($item->status_ID(), false, 'sentence') |
@@ -687,7 +687,7 @@ discard block |
||
687 | 687 | . $trash_lnk_url |
688 | 688 | . '" title="' |
689 | 689 | . esc_attr__('Trash Registration', 'event_espresso') |
690 | - . '">' . __('Trash', 'event_espresso') . '</a>'; |
|
690 | + . '">'.__('Trash', 'event_espresso').'</a>'; |
|
691 | 691 | } elseif ($this->_view === 'trash') { |
692 | 692 | // restore registration link |
693 | 693 | if (EE_Registry::instance()->CAP->current_user_can( |
@@ -705,8 +705,8 @@ discard block |
||
705 | 705 | $actions['restore'] = '<a href="' |
706 | 706 | . $restore_lnk_url |
707 | 707 | . '" title="' |
708 | - . esc_attr__('Restore Registration', 'event_espresso') . '">' |
|
709 | - . __('Restore', 'event_espresso') . '</a>'; |
|
708 | + . esc_attr__('Restore Registration', 'event_espresso').'">' |
|
709 | + . __('Restore', 'event_espresso').'</a>'; |
|
710 | 710 | } |
711 | 711 | if (EE_Registry::instance()->CAP->current_user_can( |
712 | 712 | 'ee_delete_registration', |
@@ -781,7 +781,7 @@ discard block |
||
781 | 781 | . $ticket->name() |
782 | 782 | . '</span><br />' : ''; |
783 | 783 | if ($item->final_price() > 0) { |
784 | - $content .= '<span class="reg-pad-rght">' . $item->pretty_final_price() . '</span>'; |
|
784 | + $content .= '<span class="reg-pad-rght">'.$item->pretty_final_price().'</span>'; |
|
785 | 785 | } else { |
786 | 786 | // free event |
787 | 787 | $content .= '<span class="reg-overview-free-event-spn reg-pad-rght">' |
@@ -808,7 +808,7 @@ discard block |
||
808 | 808 | : '<span class="TKT_name">' |
809 | 809 | . $ticket->name() |
810 | 810 | . '</span><br />'; |
811 | - $content .= '<span class="reg-pad-rght">' . $item->pretty_final_price() . '</span>'; |
|
811 | + $content .= '<span class="reg-pad-rght">'.$item->pretty_final_price().'</span>'; |
|
812 | 812 | return $content; |
813 | 813 | } |
814 | 814 | |
@@ -826,7 +826,7 @@ discard block |
||
826 | 826 | $payment_method = $item->payment_method(); |
827 | 827 | $payment_method_name = $payment_method instanceof EE_Payment_Method ? $payment_method->admin_name() |
828 | 828 | : __('Unknown', 'event_espresso'); |
829 | - $content = '<span class="reg-pad-rght">' . $item->pretty_paid() . '</span>'; |
|
829 | + $content = '<span class="reg-pad-rght">'.$item->pretty_paid().'</span>'; |
|
830 | 830 | if ($item->paid() > 0) { |
831 | 831 | $content .= '<br><span class="ee-status-text-small">' |
832 | 832 | . sprintf( |
@@ -874,7 +874,7 @@ discard block |
||
874 | 874 | . esc_attr__('View Transaction', 'event_espresso') |
875 | 875 | . '">' |
876 | 876 | . $item->transaction()->pretty_total() |
877 | - . '</a></span>' : '<span class="reg-pad-rght">' . $item->transaction()->pretty_total() . '</span>'; |
|
877 | + . '</a></span>' : '<span class="reg-pad-rght">'.$item->transaction()->pretty_total().'</span>'; |
|
878 | 878 | } else { |
879 | 879 | return __("None", "event_espresso"); |
880 | 880 | } |
@@ -920,7 +920,7 @@ discard block |
||
920 | 920 | . esc_attr__('View Transaction', 'event_espresso') |
921 | 921 | . '">' |
922 | 922 | . $item->transaction()->pretty_paid() |
923 | - . '</a><span>' : '<span class="reg-pad-rght">' . $item->transaction()->pretty_paid() . '</span>'; |
|
923 | + . '</a><span>' : '<span class="reg-pad-rght">'.$item->transaction()->pretty_paid().'</span>'; |
|
924 | 924 | } |
925 | 925 | } |
926 | 926 | return ' '; |
@@ -990,8 +990,8 @@ discard block |
||
990 | 990 | && $attendee instanceof EE_Attendee |
991 | 991 | ? ' |
992 | 992 | <li> |
993 | - <a href="' . $edit_lnk_url . '" title="' |
|
994 | - . esc_attr__('Edit Contact Details', 'event_espresso') . '" class="tiny-text"> |
|
993 | + <a href="' . $edit_lnk_url.'" title="' |
|
994 | + . esc_attr__('Edit Contact Details', 'event_espresso').'" class="tiny-text"> |
|
995 | 995 | <div class="ee-icon ee-icon-user-edit ee-icon-size-16"></div> |
996 | 996 | </a> |
997 | 997 | </li>' : ''; |
@@ -1063,7 +1063,7 @@ discard block |
||
1063 | 1063 | 'see_notifications_for', |
1064 | 1064 | null, |
1065 | 1065 | array('_REG_ID' => $item->ID()) |
1066 | - ) . '</li>'; |
|
1066 | + ).'</li>'; |
|
1067 | 1067 | } |
1068 | 1068 | $actions = apply_filters('FHEE__EE_Registrations_List_Table__column_actions__actions', $actions, $item, $this); |
1069 | 1069 | return $this->_action_string(implode('', $actions), $item, 'ul', 'reg-overview-actions-ul'); |