@@ -5,7 +5,6 @@ |
||
5 | 5 | use WP_Error; |
6 | 6 | use WP_REST_Response; |
7 | 7 | use EventEspresso\core\libraries\rest_api\RestException; |
8 | - |
|
9 | 8 | use EE_Error; |
10 | 9 | use EED_Core_Rest_Api; |
11 | 10 | use EEH_Inflector; |
@@ -10,7 +10,7 @@ discard block |
||
10 | 10 | use EED_Core_Rest_Api; |
11 | 11 | use EEH_Inflector; |
12 | 12 | |
13 | -if (! defined('EVENT_ESPRESSO_VERSION')) { |
|
13 | +if ( ! defined('EVENT_ESPRESSO_VERSION')) { |
|
14 | 14 | exit('No direct script access allowed'); |
15 | 15 | } |
16 | 16 | |
@@ -120,11 +120,11 @@ discard block |
||
120 | 120 | { |
121 | 121 | if (is_array($value)) { |
122 | 122 | foreach ($value as $value_key => $value_value) { |
123 | - $this->setResponseHeader($header_key . '[' . $value_key . ']', $value_value); |
|
123 | + $this->setResponseHeader($header_key.'['.$value_key.']', $value_value); |
|
124 | 124 | } |
125 | 125 | } else { |
126 | 126 | $prefix = $use_ee_prefix ? Base::HEADER_PREFIX_FOR_EE : Base::HEADER_PREFIX_FOR_WP; |
127 | - $this->response_headers[$prefix . $header_key] = $value; |
|
127 | + $this->response_headers[$prefix.$header_key] = $value; |
|
128 | 128 | } |
129 | 129 | } |
130 | 130 | |
@@ -156,7 +156,7 @@ discard block |
||
156 | 156 | protected function addEeErrorsToResponse(WP_Error $wp_error_response) |
157 | 157 | { |
158 | 158 | $notices_during_checkin = EE_Error::get_raw_notices(); |
159 | - if (! empty($notices_during_checkin['errors'])) { |
|
159 | + if ( ! empty($notices_during_checkin['errors'])) { |
|
160 | 160 | foreach ($notices_during_checkin['errors'] as $error_code => $error_message) { |
161 | 161 | $wp_error_response->add( |
162 | 162 | sanitize_key($error_code), |
@@ -200,7 +200,7 @@ discard block |
||
200 | 200 | if (is_array($debug_info)) { |
201 | 201 | $debug_info = wp_json_encode($debug_info); |
202 | 202 | } |
203 | - $headers['X-EE4-Debug-' . ucwords($debug_key)] = $debug_info; |
|
203 | + $headers['X-EE4-Debug-'.ucwords($debug_key)] = $debug_info; |
|
204 | 204 | } |
205 | 205 | } |
206 | 206 | $headers = array_merge( |
@@ -231,8 +231,8 @@ discard block |
||
231 | 231 | $status = 500; |
232 | 232 | } |
233 | 233 | $errors = array(); |
234 | - foreach ((array)$wp_error->errors as $code => $messages) { |
|
235 | - foreach ((array)$messages as $message) { |
|
234 | + foreach ((array) $wp_error->errors as $code => $messages) { |
|
235 | + foreach ((array) $messages as $message) { |
|
236 | 236 | $errors[] = array( |
237 | 237 | 'code' => $code, |
238 | 238 | 'message' => $message, |
@@ -261,7 +261,7 @@ discard block |
||
261 | 261 | $headers = array(); |
262 | 262 | $notices = EE_Error::get_raw_notices(); |
263 | 263 | foreach ($notices as $notice_type => $sub_notices) { |
264 | - if (! is_array($sub_notices)) { |
|
264 | + if ( ! is_array($sub_notices)) { |
|
265 | 265 | continue; |
266 | 266 | } |
267 | 267 | foreach ($sub_notices as $notice_code => $sub_notice) { |
@@ -299,7 +299,7 @@ discard block |
||
299 | 299 | } |
300 | 300 | $matches = $this->parseRoute( |
301 | 301 | $route, |
302 | - '~' . EED_Core_Rest_Api::ee_api_namespace_for_regex . '~', |
|
302 | + '~'.EED_Core_Rest_Api::ee_api_namespace_for_regex.'~', |
|
303 | 303 | array('version') |
304 | 304 | ); |
305 | 305 | if (isset($matches['version'])) { |
@@ -334,14 +334,14 @@ discard block |
||
334 | 334 | if (is_array($matches)) { |
335 | 335 | //skip the overall regex match. Who cares |
336 | 336 | for ($i = 1; $i <= count($match_keys); $i++) { |
337 | - if (! isset($matches[$i])) { |
|
337 | + if ( ! isset($matches[$i])) { |
|
338 | 338 | $success = false; |
339 | 339 | } else { |
340 | 340 | $indexed_matches[$match_keys[$i - 1]] = $matches[$i]; |
341 | 341 | } |
342 | 342 | } |
343 | 343 | } |
344 | - if (! $success) { |
|
344 | + if ( ! $success) { |
|
345 | 345 | throw new EE_Error( |
346 | 346 | __('We could not parse the URL. Please contact Event Espresso Support', 'event_espresso'), |
347 | 347 | 'endpoint_parsing_error' |
@@ -362,8 +362,8 @@ discard block |
||
362 | 362 | { |
363 | 363 | //$request->get_params(); |
364 | 364 | return array_merge( |
365 | - (array)$request->get_body_params(), |
|
366 | - (array)$request->get_json_params() |
|
365 | + (array) $request->get_body_params(), |
|
366 | + (array) $request->get_json_params() |
|
367 | 367 | ); |
368 | 368 | // return array_diff_key( |
369 | 369 | // $request->get_params(), |
@@ -11,7 +11,7 @@ discard block |
||
11 | 11 | use EEH_Inflector; |
12 | 12 | |
13 | 13 | if (! defined('EVENT_ESPRESSO_VERSION')) { |
14 | - exit('No direct script access allowed'); |
|
14 | + exit('No direct script access allowed'); |
|
15 | 15 | } |
16 | 16 | |
17 | 17 | |
@@ -27,352 +27,352 @@ discard block |
||
27 | 27 | class Base |
28 | 28 | { |
29 | 29 | |
30 | - /** |
|
31 | - * @deprecated use all-caps version |
|
32 | - */ |
|
33 | - // @codingStandardsIgnoreStart |
|
34 | - const header_prefix_for_ee = 'X-EE-'; |
|
35 | - // @codingStandardsIgnoreEnd |
|
36 | - |
|
37 | - const HEADER_PREFIX_FOR_EE = 'X-EE-'; |
|
38 | - |
|
39 | - /** |
|
40 | - * @deprecated use all-caps version instead |
|
41 | - */ |
|
42 | - // @codingStandardsIgnoreStart |
|
43 | - const header_prefix_for_wp = 'X-WP-'; |
|
44 | - // @codingStandardsIgnoreEnd |
|
45 | - |
|
46 | - const HEADER_PREFIX_FOR_WP = 'X-WP-'; |
|
47 | - |
|
48 | - /** |
|
49 | - * Contains debug info we'll send back in the response headers |
|
50 | - * |
|
51 | - * @var array |
|
52 | - */ |
|
53 | - protected $debug_info = array(); |
|
54 | - |
|
55 | - /** |
|
56 | - * Indicates whether or not the API is in debug mode |
|
57 | - * |
|
58 | - * @var boolean |
|
59 | - */ |
|
60 | - protected $debug_mode = false; |
|
61 | - |
|
62 | - /** |
|
63 | - * Indicates the version that was requested |
|
64 | - * |
|
65 | - * @var string |
|
66 | - */ |
|
67 | - protected $requested_version; |
|
68 | - |
|
69 | - /** |
|
70 | - * flat array of headers to send in the response |
|
71 | - * |
|
72 | - * @var array |
|
73 | - */ |
|
74 | - protected $response_headers = array(); |
|
75 | - |
|
76 | - |
|
77 | - |
|
78 | - public function __construct() |
|
79 | - { |
|
80 | - $this->debug_mode = defined('EE_REST_API_DEBUG_MODE') ? EE_REST_API_DEBUG_MODE : false; |
|
81 | - //we are handling a REST request. Don't show a fancy HTML error message is any error comes up |
|
82 | - add_filter('FHEE__EE_Error__get_error__show_normal_exceptions', '__return_true'); |
|
83 | - } |
|
84 | - |
|
85 | - |
|
86 | - |
|
87 | - /** |
|
88 | - * Sets the version the user requested |
|
89 | - * |
|
90 | - * @param string $version eg '4.8' |
|
91 | - */ |
|
92 | - public function setRequestedVersion($version) |
|
93 | - { |
|
94 | - $this->requested_version = $version; |
|
95 | - } |
|
96 | - |
|
97 | - |
|
98 | - |
|
99 | - /** |
|
100 | - * Sets some debug info that we'll send back in headers |
|
101 | - * |
|
102 | - * @param string $key |
|
103 | - * @param string|array $info |
|
104 | - */ |
|
105 | - protected function setDebugInfo($key, $info) |
|
106 | - { |
|
107 | - $this->debug_info[$key] = $info; |
|
108 | - } |
|
109 | - |
|
110 | - |
|
111 | - |
|
112 | - /** |
|
113 | - * Sets headers for the response |
|
114 | - * |
|
115 | - * @param string $header_key , excluding the "X-EE-" part |
|
116 | - * @param array|string $value if an array, multiple headers will be added, one |
|
117 | - * for each key in the array |
|
118 | - * @param boolean $use_ee_prefix whether to use the EE prefix on the header, or fallback to |
|
119 | - * the standard WP one |
|
120 | - */ |
|
121 | - protected function setResponseHeader($header_key, $value, $use_ee_prefix = true) |
|
122 | - { |
|
123 | - if (is_array($value)) { |
|
124 | - foreach ($value as $value_key => $value_value) { |
|
125 | - $this->setResponseHeader($header_key . '[' . $value_key . ']', $value_value); |
|
126 | - } |
|
127 | - } else { |
|
128 | - $prefix = $use_ee_prefix ? Base::HEADER_PREFIX_FOR_EE : Base::HEADER_PREFIX_FOR_WP; |
|
129 | - $this->response_headers[$prefix . $header_key] = $value; |
|
130 | - } |
|
131 | - } |
|
132 | - |
|
133 | - |
|
134 | - |
|
135 | - /** |
|
136 | - * Returns a flat array of headers to be added to the response |
|
137 | - * |
|
138 | - * @return array |
|
139 | - */ |
|
140 | - protected function getResponseHeaders() |
|
141 | - { |
|
142 | - return apply_filters( |
|
143 | - 'FHEE__EventEspresso\core\libraries\rest_api\controllers\Base___get_response_headers', |
|
144 | - $this->response_headers, |
|
145 | - $this, |
|
146 | - $this->requested_version |
|
147 | - ); |
|
148 | - } |
|
149 | - |
|
150 | - |
|
151 | - |
|
152 | - /** |
|
153 | - * Adds error notices from EE_Error onto the provided \WP_Error |
|
154 | - * |
|
155 | - * @param WP_Error $wp_error_response |
|
156 | - * @return WP_Error |
|
157 | - */ |
|
158 | - protected function addEeErrorsToResponse(WP_Error $wp_error_response) |
|
159 | - { |
|
160 | - $notices_during_checkin = EE_Error::get_raw_notices(); |
|
161 | - if (! empty($notices_during_checkin['errors'])) { |
|
162 | - foreach ($notices_during_checkin['errors'] as $error_code => $error_message) { |
|
163 | - $wp_error_response->add( |
|
164 | - sanitize_key($error_code), |
|
165 | - strip_tags($error_message) |
|
166 | - ); |
|
167 | - } |
|
168 | - } |
|
169 | - return $wp_error_response; |
|
170 | - } |
|
171 | - |
|
172 | - |
|
173 | - |
|
174 | - /** |
|
175 | - * Sends a response, but also makes sure to attach headers that |
|
176 | - * are handy for debugging. |
|
177 | - * Specifically, we assume folks will want to know what exactly was the DB query that got run, |
|
178 | - * what exactly was the Models query that got run, what capabilities came into play, what fields were omitted from |
|
179 | - * the response, others? |
|
180 | - * |
|
181 | - * @param array|WP_Error|Exception|RestException $response |
|
182 | - * @return WP_REST_Response |
|
183 | - */ |
|
184 | - public function sendResponse($response) |
|
185 | - { |
|
186 | - if ($response instanceof RestException) { |
|
187 | - $response = new WP_Error($response->getStringCode(), $response->getMessage(), $response->getData()); |
|
188 | - } |
|
189 | - if ($response instanceof Exception) { |
|
190 | - $code = $response->getCode() ? $response->getCode() : 'error_occurred'; |
|
191 | - $response = new WP_Error($code, $response->getMessage()); |
|
192 | - } |
|
193 | - if ($response instanceof WP_Error) { |
|
194 | - $response = $this->addEeErrorsToResponse($response); |
|
195 | - $rest_response = $this->createRestResponseFromWpError($response); |
|
196 | - } else { |
|
197 | - $rest_response = new WP_REST_Response($response, 200); |
|
198 | - } |
|
199 | - $headers = array(); |
|
200 | - if ($this->debug_mode && is_array($this->debug_info)) { |
|
201 | - foreach ($this->debug_info as $debug_key => $debug_info) { |
|
202 | - if (is_array($debug_info)) { |
|
203 | - $debug_info = wp_json_encode($debug_info); |
|
204 | - } |
|
205 | - $headers['X-EE4-Debug-' . ucwords($debug_key)] = $debug_info; |
|
206 | - } |
|
207 | - } |
|
208 | - $headers = array_merge( |
|
209 | - $headers, |
|
210 | - $this->getResponseHeaders(), |
|
211 | - $this->getHeadersFromEeNotices() |
|
212 | - ); |
|
213 | - $rest_response->set_headers($headers); |
|
214 | - return $rest_response; |
|
215 | - } |
|
216 | - |
|
217 | - |
|
218 | - |
|
219 | - /** |
|
220 | - * Converts the \WP_Error into `WP_REST_Response. |
|
221 | - * Mostly this is just a copy-and-paste from \WP_REST_Server::error_to_response |
|
222 | - * (which is protected) |
|
223 | - * |
|
224 | - * @param WP_Error $wp_error |
|
225 | - * @return WP_REST_Response |
|
226 | - */ |
|
227 | - protected function createRestResponseFromWpError(WP_Error $wp_error) |
|
228 | - { |
|
229 | - $error_data = $wp_error->get_error_data(); |
|
230 | - if (is_array($error_data) && isset($error_data['status'])) { |
|
231 | - $status = $error_data['status']; |
|
232 | - } else { |
|
233 | - $status = 500; |
|
234 | - } |
|
235 | - $errors = array(); |
|
236 | - foreach ((array)$wp_error->errors as $code => $messages) { |
|
237 | - foreach ((array)$messages as $message) { |
|
238 | - $errors[] = array( |
|
239 | - 'code' => $code, |
|
240 | - 'message' => $message, |
|
241 | - 'data' => $wp_error->get_error_data($code), |
|
242 | - ); |
|
243 | - } |
|
244 | - } |
|
245 | - $data = isset($errors[0]) ? $errors[0] : array(); |
|
246 | - if (count($errors) > 1) { |
|
247 | - // Remove the primary error. |
|
248 | - array_shift($errors); |
|
249 | - $data['additional_errors'] = $errors; |
|
250 | - } |
|
251 | - return new WP_REST_Response($data, $status); |
|
252 | - } |
|
253 | - |
|
254 | - |
|
255 | - |
|
256 | - /** |
|
257 | - * Array of headers derived from EE success, attention, and error messages |
|
258 | - * |
|
259 | - * @return array |
|
260 | - */ |
|
261 | - protected function getHeadersFromEeNotices() |
|
262 | - { |
|
263 | - $headers = array(); |
|
264 | - $notices = EE_Error::get_raw_notices(); |
|
265 | - foreach ($notices as $notice_type => $sub_notices) { |
|
266 | - if (! is_array($sub_notices)) { |
|
267 | - continue; |
|
268 | - } |
|
269 | - foreach ($sub_notices as $notice_code => $sub_notice) { |
|
270 | - $headers['X-EE4-Notices-' |
|
271 | - . EEH_Inflector::humanize($notice_type) |
|
272 | - . '[' |
|
273 | - . $notice_code |
|
274 | - . ']'] = strip_tags($sub_notice); |
|
275 | - } |
|
276 | - } |
|
277 | - return apply_filters( |
|
278 | - 'FHEE__EventEspresso\core\libraries\rest_api\controllers\Base___get_headers_from_ee_notices__return', |
|
279 | - $headers, |
|
280 | - $this->requested_version, |
|
281 | - $notices |
|
282 | - ); |
|
283 | - } |
|
284 | - |
|
285 | - |
|
286 | - |
|
287 | - /** |
|
288 | - * Finds which version of the API was requested given the route, and returns it. |
|
289 | - * eg in a request to "mysite.com/wp-json/ee/v4.8.29/events/123" this would return |
|
290 | - * "4.8.29". |
|
291 | - * We should know hte requested version in this model though, so if no route is |
|
292 | - * provided just use what we set earlier |
|
293 | - * |
|
294 | - * @param string $route |
|
295 | - * @return string |
|
296 | - */ |
|
297 | - public function getRequestedVersion($route = null) |
|
298 | - { |
|
299 | - if ($route === null) { |
|
300 | - return $this->requested_version; |
|
301 | - } |
|
302 | - $matches = $this->parseRoute( |
|
303 | - $route, |
|
304 | - '~' . EED_Core_Rest_Api::ee_api_namespace_for_regex . '~', |
|
305 | - array('version') |
|
306 | - ); |
|
307 | - if (isset($matches['version'])) { |
|
308 | - return $matches['version']; |
|
309 | - } else { |
|
310 | - return EED_Core_Rest_Api::latest_rest_api_version(); |
|
311 | - } |
|
312 | - } |
|
313 | - |
|
314 | - |
|
315 | - |
|
316 | - /** |
|
317 | - * Applies the regex to the route, then creates an array using the values of |
|
318 | - * $match_keys as keys (but ignores the full pattern match). Returns the array of matches. |
|
319 | - * For example, if you call |
|
320 | - * parse_route( '/ee/v4.8/events', '~\/ee\/v([^/]*)\/(.*)~', array( 'version', 'model' ) ) |
|
321 | - * it will return array( 'version' => '4.8', 'model' => 'events' ) |
|
322 | - * |
|
323 | - * @param string $route |
|
324 | - * @param string $regex |
|
325 | - * @param array $match_keys EXCLUDING matching the entire regex |
|
326 | - * @return array where $match_keys are the keys (the first value of $match_keys |
|
327 | - * becomes the first key of the return value, etc. Eg passing in $match_keys of |
|
328 | - * array( 'model', 'id' ), will, if the regex is successful, will return |
|
329 | - * array( 'model' => 'foo', 'id' => 'bar' ) |
|
330 | - * @throws EE_Error if it couldn't be parsed |
|
331 | - */ |
|
332 | - public function parseRoute($route, $regex, $match_keys) |
|
333 | - { |
|
334 | - $indexed_matches = array(); |
|
335 | - $success = preg_match($regex, $route, $matches); |
|
336 | - if (is_array($matches)) { |
|
337 | - //skip the overall regex match. Who cares |
|
338 | - for ($i = 1; $i <= count($match_keys); $i++) { |
|
339 | - if (! isset($matches[$i])) { |
|
340 | - $success = false; |
|
341 | - } else { |
|
342 | - $indexed_matches[$match_keys[$i - 1]] = $matches[$i]; |
|
343 | - } |
|
344 | - } |
|
345 | - } |
|
346 | - if (! $success) { |
|
347 | - throw new EE_Error( |
|
348 | - __('We could not parse the URL. Please contact Event Espresso Support', 'event_espresso'), |
|
349 | - 'endpoint_parsing_error' |
|
350 | - ); |
|
351 | - } |
|
352 | - return $indexed_matches; |
|
353 | - } |
|
354 | - |
|
355 | - |
|
356 | - |
|
357 | - /** |
|
358 | - * Gets the body's params (either from JSON or parsed body), which EXCLUDES the GET params and URL params |
|
359 | - * |
|
360 | - * @param \WP_REST_Request $request |
|
361 | - * @return array |
|
362 | - */ |
|
363 | - protected function getBodyParams(\WP_REST_Request $request) |
|
364 | - { |
|
365 | - //$request->get_params(); |
|
366 | - return array_merge( |
|
367 | - (array)$request->get_body_params(), |
|
368 | - (array)$request->get_json_params() |
|
369 | - ); |
|
370 | - // return array_diff_key( |
|
371 | - // $request->get_params(), |
|
372 | - // $request->get_url_params(), |
|
373 | - // $request->get_query_params() |
|
374 | - // ); |
|
375 | - } |
|
30 | + /** |
|
31 | + * @deprecated use all-caps version |
|
32 | + */ |
|
33 | + // @codingStandardsIgnoreStart |
|
34 | + const header_prefix_for_ee = 'X-EE-'; |
|
35 | + // @codingStandardsIgnoreEnd |
|
36 | + |
|
37 | + const HEADER_PREFIX_FOR_EE = 'X-EE-'; |
|
38 | + |
|
39 | + /** |
|
40 | + * @deprecated use all-caps version instead |
|
41 | + */ |
|
42 | + // @codingStandardsIgnoreStart |
|
43 | + const header_prefix_for_wp = 'X-WP-'; |
|
44 | + // @codingStandardsIgnoreEnd |
|
45 | + |
|
46 | + const HEADER_PREFIX_FOR_WP = 'X-WP-'; |
|
47 | + |
|
48 | + /** |
|
49 | + * Contains debug info we'll send back in the response headers |
|
50 | + * |
|
51 | + * @var array |
|
52 | + */ |
|
53 | + protected $debug_info = array(); |
|
54 | + |
|
55 | + /** |
|
56 | + * Indicates whether or not the API is in debug mode |
|
57 | + * |
|
58 | + * @var boolean |
|
59 | + */ |
|
60 | + protected $debug_mode = false; |
|
61 | + |
|
62 | + /** |
|
63 | + * Indicates the version that was requested |
|
64 | + * |
|
65 | + * @var string |
|
66 | + */ |
|
67 | + protected $requested_version; |
|
68 | + |
|
69 | + /** |
|
70 | + * flat array of headers to send in the response |
|
71 | + * |
|
72 | + * @var array |
|
73 | + */ |
|
74 | + protected $response_headers = array(); |
|
75 | + |
|
76 | + |
|
77 | + |
|
78 | + public function __construct() |
|
79 | + { |
|
80 | + $this->debug_mode = defined('EE_REST_API_DEBUG_MODE') ? EE_REST_API_DEBUG_MODE : false; |
|
81 | + //we are handling a REST request. Don't show a fancy HTML error message is any error comes up |
|
82 | + add_filter('FHEE__EE_Error__get_error__show_normal_exceptions', '__return_true'); |
|
83 | + } |
|
84 | + |
|
85 | + |
|
86 | + |
|
87 | + /** |
|
88 | + * Sets the version the user requested |
|
89 | + * |
|
90 | + * @param string $version eg '4.8' |
|
91 | + */ |
|
92 | + public function setRequestedVersion($version) |
|
93 | + { |
|
94 | + $this->requested_version = $version; |
|
95 | + } |
|
96 | + |
|
97 | + |
|
98 | + |
|
99 | + /** |
|
100 | + * Sets some debug info that we'll send back in headers |
|
101 | + * |
|
102 | + * @param string $key |
|
103 | + * @param string|array $info |
|
104 | + */ |
|
105 | + protected function setDebugInfo($key, $info) |
|
106 | + { |
|
107 | + $this->debug_info[$key] = $info; |
|
108 | + } |
|
109 | + |
|
110 | + |
|
111 | + |
|
112 | + /** |
|
113 | + * Sets headers for the response |
|
114 | + * |
|
115 | + * @param string $header_key , excluding the "X-EE-" part |
|
116 | + * @param array|string $value if an array, multiple headers will be added, one |
|
117 | + * for each key in the array |
|
118 | + * @param boolean $use_ee_prefix whether to use the EE prefix on the header, or fallback to |
|
119 | + * the standard WP one |
|
120 | + */ |
|
121 | + protected function setResponseHeader($header_key, $value, $use_ee_prefix = true) |
|
122 | + { |
|
123 | + if (is_array($value)) { |
|
124 | + foreach ($value as $value_key => $value_value) { |
|
125 | + $this->setResponseHeader($header_key . '[' . $value_key . ']', $value_value); |
|
126 | + } |
|
127 | + } else { |
|
128 | + $prefix = $use_ee_prefix ? Base::HEADER_PREFIX_FOR_EE : Base::HEADER_PREFIX_FOR_WP; |
|
129 | + $this->response_headers[$prefix . $header_key] = $value; |
|
130 | + } |
|
131 | + } |
|
132 | + |
|
133 | + |
|
134 | + |
|
135 | + /** |
|
136 | + * Returns a flat array of headers to be added to the response |
|
137 | + * |
|
138 | + * @return array |
|
139 | + */ |
|
140 | + protected function getResponseHeaders() |
|
141 | + { |
|
142 | + return apply_filters( |
|
143 | + 'FHEE__EventEspresso\core\libraries\rest_api\controllers\Base___get_response_headers', |
|
144 | + $this->response_headers, |
|
145 | + $this, |
|
146 | + $this->requested_version |
|
147 | + ); |
|
148 | + } |
|
149 | + |
|
150 | + |
|
151 | + |
|
152 | + /** |
|
153 | + * Adds error notices from EE_Error onto the provided \WP_Error |
|
154 | + * |
|
155 | + * @param WP_Error $wp_error_response |
|
156 | + * @return WP_Error |
|
157 | + */ |
|
158 | + protected function addEeErrorsToResponse(WP_Error $wp_error_response) |
|
159 | + { |
|
160 | + $notices_during_checkin = EE_Error::get_raw_notices(); |
|
161 | + if (! empty($notices_during_checkin['errors'])) { |
|
162 | + foreach ($notices_during_checkin['errors'] as $error_code => $error_message) { |
|
163 | + $wp_error_response->add( |
|
164 | + sanitize_key($error_code), |
|
165 | + strip_tags($error_message) |
|
166 | + ); |
|
167 | + } |
|
168 | + } |
|
169 | + return $wp_error_response; |
|
170 | + } |
|
171 | + |
|
172 | + |
|
173 | + |
|
174 | + /** |
|
175 | + * Sends a response, but also makes sure to attach headers that |
|
176 | + * are handy for debugging. |
|
177 | + * Specifically, we assume folks will want to know what exactly was the DB query that got run, |
|
178 | + * what exactly was the Models query that got run, what capabilities came into play, what fields were omitted from |
|
179 | + * the response, others? |
|
180 | + * |
|
181 | + * @param array|WP_Error|Exception|RestException $response |
|
182 | + * @return WP_REST_Response |
|
183 | + */ |
|
184 | + public function sendResponse($response) |
|
185 | + { |
|
186 | + if ($response instanceof RestException) { |
|
187 | + $response = new WP_Error($response->getStringCode(), $response->getMessage(), $response->getData()); |
|
188 | + } |
|
189 | + if ($response instanceof Exception) { |
|
190 | + $code = $response->getCode() ? $response->getCode() : 'error_occurred'; |
|
191 | + $response = new WP_Error($code, $response->getMessage()); |
|
192 | + } |
|
193 | + if ($response instanceof WP_Error) { |
|
194 | + $response = $this->addEeErrorsToResponse($response); |
|
195 | + $rest_response = $this->createRestResponseFromWpError($response); |
|
196 | + } else { |
|
197 | + $rest_response = new WP_REST_Response($response, 200); |
|
198 | + } |
|
199 | + $headers = array(); |
|
200 | + if ($this->debug_mode && is_array($this->debug_info)) { |
|
201 | + foreach ($this->debug_info as $debug_key => $debug_info) { |
|
202 | + if (is_array($debug_info)) { |
|
203 | + $debug_info = wp_json_encode($debug_info); |
|
204 | + } |
|
205 | + $headers['X-EE4-Debug-' . ucwords($debug_key)] = $debug_info; |
|
206 | + } |
|
207 | + } |
|
208 | + $headers = array_merge( |
|
209 | + $headers, |
|
210 | + $this->getResponseHeaders(), |
|
211 | + $this->getHeadersFromEeNotices() |
|
212 | + ); |
|
213 | + $rest_response->set_headers($headers); |
|
214 | + return $rest_response; |
|
215 | + } |
|
216 | + |
|
217 | + |
|
218 | + |
|
219 | + /** |
|
220 | + * Converts the \WP_Error into `WP_REST_Response. |
|
221 | + * Mostly this is just a copy-and-paste from \WP_REST_Server::error_to_response |
|
222 | + * (which is protected) |
|
223 | + * |
|
224 | + * @param WP_Error $wp_error |
|
225 | + * @return WP_REST_Response |
|
226 | + */ |
|
227 | + protected function createRestResponseFromWpError(WP_Error $wp_error) |
|
228 | + { |
|
229 | + $error_data = $wp_error->get_error_data(); |
|
230 | + if (is_array($error_data) && isset($error_data['status'])) { |
|
231 | + $status = $error_data['status']; |
|
232 | + } else { |
|
233 | + $status = 500; |
|
234 | + } |
|
235 | + $errors = array(); |
|
236 | + foreach ((array)$wp_error->errors as $code => $messages) { |
|
237 | + foreach ((array)$messages as $message) { |
|
238 | + $errors[] = array( |
|
239 | + 'code' => $code, |
|
240 | + 'message' => $message, |
|
241 | + 'data' => $wp_error->get_error_data($code), |
|
242 | + ); |
|
243 | + } |
|
244 | + } |
|
245 | + $data = isset($errors[0]) ? $errors[0] : array(); |
|
246 | + if (count($errors) > 1) { |
|
247 | + // Remove the primary error. |
|
248 | + array_shift($errors); |
|
249 | + $data['additional_errors'] = $errors; |
|
250 | + } |
|
251 | + return new WP_REST_Response($data, $status); |
|
252 | + } |
|
253 | + |
|
254 | + |
|
255 | + |
|
256 | + /** |
|
257 | + * Array of headers derived from EE success, attention, and error messages |
|
258 | + * |
|
259 | + * @return array |
|
260 | + */ |
|
261 | + protected function getHeadersFromEeNotices() |
|
262 | + { |
|
263 | + $headers = array(); |
|
264 | + $notices = EE_Error::get_raw_notices(); |
|
265 | + foreach ($notices as $notice_type => $sub_notices) { |
|
266 | + if (! is_array($sub_notices)) { |
|
267 | + continue; |
|
268 | + } |
|
269 | + foreach ($sub_notices as $notice_code => $sub_notice) { |
|
270 | + $headers['X-EE4-Notices-' |
|
271 | + . EEH_Inflector::humanize($notice_type) |
|
272 | + . '[' |
|
273 | + . $notice_code |
|
274 | + . ']'] = strip_tags($sub_notice); |
|
275 | + } |
|
276 | + } |
|
277 | + return apply_filters( |
|
278 | + 'FHEE__EventEspresso\core\libraries\rest_api\controllers\Base___get_headers_from_ee_notices__return', |
|
279 | + $headers, |
|
280 | + $this->requested_version, |
|
281 | + $notices |
|
282 | + ); |
|
283 | + } |
|
284 | + |
|
285 | + |
|
286 | + |
|
287 | + /** |
|
288 | + * Finds which version of the API was requested given the route, and returns it. |
|
289 | + * eg in a request to "mysite.com/wp-json/ee/v4.8.29/events/123" this would return |
|
290 | + * "4.8.29". |
|
291 | + * We should know hte requested version in this model though, so if no route is |
|
292 | + * provided just use what we set earlier |
|
293 | + * |
|
294 | + * @param string $route |
|
295 | + * @return string |
|
296 | + */ |
|
297 | + public function getRequestedVersion($route = null) |
|
298 | + { |
|
299 | + if ($route === null) { |
|
300 | + return $this->requested_version; |
|
301 | + } |
|
302 | + $matches = $this->parseRoute( |
|
303 | + $route, |
|
304 | + '~' . EED_Core_Rest_Api::ee_api_namespace_for_regex . '~', |
|
305 | + array('version') |
|
306 | + ); |
|
307 | + if (isset($matches['version'])) { |
|
308 | + return $matches['version']; |
|
309 | + } else { |
|
310 | + return EED_Core_Rest_Api::latest_rest_api_version(); |
|
311 | + } |
|
312 | + } |
|
313 | + |
|
314 | + |
|
315 | + |
|
316 | + /** |
|
317 | + * Applies the regex to the route, then creates an array using the values of |
|
318 | + * $match_keys as keys (but ignores the full pattern match). Returns the array of matches. |
|
319 | + * For example, if you call |
|
320 | + * parse_route( '/ee/v4.8/events', '~\/ee\/v([^/]*)\/(.*)~', array( 'version', 'model' ) ) |
|
321 | + * it will return array( 'version' => '4.8', 'model' => 'events' ) |
|
322 | + * |
|
323 | + * @param string $route |
|
324 | + * @param string $regex |
|
325 | + * @param array $match_keys EXCLUDING matching the entire regex |
|
326 | + * @return array where $match_keys are the keys (the first value of $match_keys |
|
327 | + * becomes the first key of the return value, etc. Eg passing in $match_keys of |
|
328 | + * array( 'model', 'id' ), will, if the regex is successful, will return |
|
329 | + * array( 'model' => 'foo', 'id' => 'bar' ) |
|
330 | + * @throws EE_Error if it couldn't be parsed |
|
331 | + */ |
|
332 | + public function parseRoute($route, $regex, $match_keys) |
|
333 | + { |
|
334 | + $indexed_matches = array(); |
|
335 | + $success = preg_match($regex, $route, $matches); |
|
336 | + if (is_array($matches)) { |
|
337 | + //skip the overall regex match. Who cares |
|
338 | + for ($i = 1; $i <= count($match_keys); $i++) { |
|
339 | + if (! isset($matches[$i])) { |
|
340 | + $success = false; |
|
341 | + } else { |
|
342 | + $indexed_matches[$match_keys[$i - 1]] = $matches[$i]; |
|
343 | + } |
|
344 | + } |
|
345 | + } |
|
346 | + if (! $success) { |
|
347 | + throw new EE_Error( |
|
348 | + __('We could not parse the URL. Please contact Event Espresso Support', 'event_espresso'), |
|
349 | + 'endpoint_parsing_error' |
|
350 | + ); |
|
351 | + } |
|
352 | + return $indexed_matches; |
|
353 | + } |
|
354 | + |
|
355 | + |
|
356 | + |
|
357 | + /** |
|
358 | + * Gets the body's params (either from JSON or parsed body), which EXCLUDES the GET params and URL params |
|
359 | + * |
|
360 | + * @param \WP_REST_Request $request |
|
361 | + * @return array |
|
362 | + */ |
|
363 | + protected function getBodyParams(\WP_REST_Request $request) |
|
364 | + { |
|
365 | + //$request->get_params(); |
|
366 | + return array_merge( |
|
367 | + (array)$request->get_body_params(), |
|
368 | + (array)$request->get_json_params() |
|
369 | + ); |
|
370 | + // return array_diff_key( |
|
371 | + // $request->get_params(), |
|
372 | + // $request->get_url_params(), |
|
373 | + // $request->get_query_params() |
|
374 | + // ); |
|
375 | + } |
|
376 | 376 | } |
377 | 377 | |
378 | 378 | // End of file Base.php |
@@ -11,7 +11,7 @@ discard block |
||
11 | 11 | * @subpackage |
12 | 12 | * @author Mike Nelson |
13 | 13 | */ |
14 | -class EE_Select_Ajax_Model_Rest_Input extends EE_Form_Input_With_Options_Base{ |
|
14 | +class EE_Select_Ajax_Model_Rest_Input extends EE_Form_Input_With_Options_Base { |
|
15 | 15 | |
16 | 16 | /** |
17 | 17 | * @var string $_model_name |
@@ -50,7 +50,7 @@ discard block |
||
50 | 50 | * And the arguments accepted by EE_Form_Input_With_Options_Base |
51 | 51 | * @throws \EE_Error |
52 | 52 | */ |
53 | - public function __construct( $input_settings = array() ) { |
|
53 | + public function __construct($input_settings = array()) { |
|
54 | 54 | //needed input settings: |
55 | 55 | //select2_args |
56 | 56 | $this->_model_name = EEH_Array::is_set( |
@@ -66,7 +66,7 @@ discard block |
||
66 | 66 | ); |
67 | 67 | // make sure limit and caps are always set |
68 | 68 | $query_params = array_merge( |
69 | - array( 'limit' => 10, 'caps' => EEM_Base::caps_read_admin ), |
|
69 | + array('limit' => 10, 'caps' => EEM_Base::caps_read_admin), |
|
70 | 70 | $query_params |
71 | 71 | ); |
72 | 72 | $this->_value_field_name = EEH_Array::is_set( |
@@ -77,7 +77,7 @@ discard block |
||
77 | 77 | $this->_display_field_name = EEH_Array::is_set( |
78 | 78 | $input_settings, |
79 | 79 | 'display_field_name', |
80 | - $model->get_a_field_of_type( 'EE_Text_Field_Base' )->get_name() |
|
80 | + $model->get_a_field_of_type('EE_Text_Field_Base')->get_name() |
|
81 | 81 | ); |
82 | 82 | $this->_extra_select_columns = EEH_Array::is_set( |
83 | 83 | $input_settings, |
@@ -94,24 +94,24 @@ discard block |
||
94 | 94 | ); |
95 | 95 | //get resource endpoint |
96 | 96 | $rest_controller = new EventEspresso\core\libraries\rest_api\controllers\model\Read(); |
97 | - $rest_controller->setRequestedVersion( EED_Core_Rest_Api::latest_rest_api_version() ); |
|
97 | + $rest_controller->setRequestedVersion(EED_Core_Rest_Api::latest_rest_api_version()); |
|
98 | 98 | $default_select2_args = array( |
99 | 99 | 'ajax' => array( |
100 | 100 | 'url' => $rest_controller->getVersionedLinkTo( |
101 | - EEH_Inflector::pluralize_and_lower( $this->_model_name ) |
|
101 | + EEH_Inflector::pluralize_and_lower($this->_model_name) |
|
102 | 102 | ), |
103 | 103 | 'dataType' => 'json', |
104 | 104 | 'delay' => '250', |
105 | 105 | 'data_interface' => 'EE_Select2_REST_API_Interface', |
106 | 106 | 'data_interface_args' => array( |
107 | - 'default_query_params' => (object)ModelDataTranslator::prepareQueryParamsForRestApi( |
|
107 | + 'default_query_params' => (object) ModelDataTranslator::prepareQueryParamsForRestApi( |
|
108 | 108 | $query_params, |
109 | 109 | $model |
110 | 110 | ), |
111 | 111 | 'display_field' => $this->_display_field_name, |
112 | 112 | 'value_field' => $this->_value_field_name, |
113 | - 'nonce' => wp_create_nonce( 'wp_rest' ), |
|
114 | - 'locale' => str_replace( '_', '-', strtolower( get_locale() ) ) |
|
113 | + 'nonce' => wp_create_nonce('wp_rest'), |
|
114 | + 'locale' => str_replace('_', '-', strtolower(get_locale())) |
|
115 | 115 | ), |
116 | 116 | ), |
117 | 117 | 'cache' => true, |
@@ -119,10 +119,10 @@ discard block |
||
119 | 119 | ); |
120 | 120 | $select2_args = array_replace_recursive( |
121 | 121 | $default_select2_args, |
122 | - (array)EEH_Array::is_set( $input_settings, 'select2_args', array() ) |
|
122 | + (array) EEH_Array::is_set($input_settings, 'select2_args', array()) |
|
123 | 123 | ); |
124 | - $this->set_display_strategy( new EE_Select2_Display_Strategy( $select2_args ) ); |
|
125 | - parent::__construct( array(), $input_settings ); |
|
124 | + $this->set_display_strategy(new EE_Select2_Display_Strategy($select2_args)); |
|
125 | + parent::__construct(array(), $input_settings); |
|
126 | 126 | } |
127 | 127 | |
128 | 128 | |
@@ -140,34 +140,34 @@ discard block |
||
140 | 140 | * @return void |
141 | 141 | * @throws \EE_Error |
142 | 142 | */ |
143 | - public function _set_raw_value( $value ) { |
|
144 | - $values_for_options = (array)$value; |
|
145 | - $value_field = $this->_get_model()->field_settings_for( $this->_value_field_name ); |
|
146 | - $display_field = $this->_get_model()->field_settings_for( $this->_display_field_name ); |
|
147 | - $this->_extra_select_columns[] = $value_field->get_qualified_column() . ' AS ' . $this->_value_field_name; |
|
148 | - $this->_extra_select_columns[] = $display_field->get_qualified_column() . ' AS ' . $this->_display_field_name; |
|
143 | + public function _set_raw_value($value) { |
|
144 | + $values_for_options = (array) $value; |
|
145 | + $value_field = $this->_get_model()->field_settings_for($this->_value_field_name); |
|
146 | + $display_field = $this->_get_model()->field_settings_for($this->_display_field_name); |
|
147 | + $this->_extra_select_columns[] = $value_field->get_qualified_column().' AS '.$this->_value_field_name; |
|
148 | + $this->_extra_select_columns[] = $display_field->get_qualified_column().' AS '.$this->_display_field_name; |
|
149 | 149 | $display_values = $this->_get_model()->get_all_wpdb_results( |
150 | 150 | array( |
151 | 151 | array( |
152 | - $this->_value_field_name => array( 'IN', $values_for_options ) |
|
152 | + $this->_value_field_name => array('IN', $values_for_options) |
|
153 | 153 | ) |
154 | 154 | ), |
155 | 155 | ARRAY_A, |
156 | - implode( ',', $this->_extra_select_columns ) |
|
156 | + implode(',', $this->_extra_select_columns) |
|
157 | 157 | ); |
158 | 158 | $select_options = array(); |
159 | - if( is_array( $select_options ) ) { |
|
160 | - foreach( $display_values as $db_rows ) { |
|
161 | - $db_rows = (array)$db_rows; |
|
162 | - $select_options[ $db_rows[ $this->_value_field_name ] ] = apply_filters( |
|
159 | + if (is_array($select_options)) { |
|
160 | + foreach ($display_values as $db_rows) { |
|
161 | + $db_rows = (array) $db_rows; |
|
162 | + $select_options[$db_rows[$this->_value_field_name]] = apply_filters( |
|
163 | 163 | 'FHEE__EE_Select_Ajax_Model_Rest_Input___set_raw_value__select_option_value', |
164 | - $db_rows[ $this->_display_field_name ], |
|
164 | + $db_rows[$this->_display_field_name], |
|
165 | 165 | $db_rows |
166 | 166 | ); |
167 | 167 | } |
168 | 168 | } |
169 | - $this->set_select_options( $select_options ); |
|
170 | - parent::_set_raw_value( $value ); |
|
169 | + $this->set_select_options($select_options); |
|
170 | + parent::_set_raw_value($value); |
|
171 | 171 | } |
172 | 172 | |
173 | 173 | /** |
@@ -176,7 +176,7 @@ discard block |
||
176 | 176 | * @throws EE_Error |
177 | 177 | */ |
178 | 178 | protected function _get_model() { |
179 | - if( ! EE_Registry::instance()->is_model_name( $this->_model_name ) ) { |
|
179 | + if ( ! EE_Registry::instance()->is_model_name($this->_model_name)) { |
|
180 | 180 | throw new EE_Error( |
181 | 181 | sprintf( |
182 | 182 | __( |
@@ -187,7 +187,7 @@ discard block |
||
187 | 187 | ) |
188 | 188 | ); |
189 | 189 | } else { |
190 | - return EE_Registry::instance()->load_model( $this->_model_name ); |
|
190 | + return EE_Registry::instance()->load_model($this->_model_name); |
|
191 | 191 | } |
192 | 192 | } |
193 | 193 |
@@ -16,7 +16,7 @@ discard block |
||
16 | 16 | * @author Mike Nelson |
17 | 17 | * @since 4.8.35.rc.001 |
18 | 18 | */ |
19 | -if (! defined('EVENT_ESPRESSO_VERSION')) { |
|
19 | +if ( ! defined('EVENT_ESPRESSO_VERSION')) { |
|
20 | 20 | exit('No direct script access allowed'); |
21 | 21 | } |
22 | 22 | |
@@ -44,7 +44,7 @@ discard block |
||
44 | 44 | */ |
45 | 45 | public function mapping($refresh = false) |
46 | 46 | { |
47 | - if (! $this->mapping || $refresh) { |
|
47 | + if ( ! $this->mapping || $refresh) { |
|
48 | 48 | $this->mapping = $this->generateNewMapping(); |
49 | 49 | } |
50 | 50 | return $this->mapping; |
@@ -60,9 +60,9 @@ discard block |
||
60 | 60 | protected function generateNewMapping() |
61 | 61 | { |
62 | 62 | $rest_api_calculations_namespace = 'EventEspresso\core\libraries\rest_api\calculations\\'; |
63 | - $event_calculations_class = $rest_api_calculations_namespace . 'Event'; |
|
64 | - $datetime_calculations_class = $rest_api_calculations_namespace . 'Datetime'; |
|
65 | - $registration_class = $rest_api_calculations_namespace . 'Registration'; |
|
63 | + $event_calculations_class = $rest_api_calculations_namespace.'Event'; |
|
64 | + $datetime_calculations_class = $rest_api_calculations_namespace.'Datetime'; |
|
65 | + $registration_class = $rest_api_calculations_namespace.'Registration'; |
|
66 | 66 | return apply_filters( |
67 | 67 | 'FHEE__EventEspresso\core\libraries\rest_api\Calculated_Model_Fields__mapping', |
68 | 68 | array( |
@@ -16,7 +16,7 @@ discard block |
||
16 | 16 | * @since 4.8.35.rc.001 |
17 | 17 | */ |
18 | 18 | if (! defined('EVENT_ESPRESSO_VERSION')) { |
19 | - exit('No direct script access allowed'); |
|
19 | + exit('No direct script access allowed'); |
|
20 | 20 | } |
21 | 21 | |
22 | 22 | |
@@ -24,128 +24,128 @@ discard block |
||
24 | 24 | class CalculatedModelFields |
25 | 25 | { |
26 | 26 | |
27 | - /** |
|
28 | - * @var array |
|
29 | - */ |
|
30 | - protected $mapping; |
|
31 | - |
|
32 | - |
|
33 | - |
|
34 | - /** |
|
35 | - * @param bool $refresh |
|
36 | - * @return array top-level-keys are model names (eg "Event") |
|
37 | - * next-level are the calculated field names AND method names on classes |
|
38 | - * which perform calculations, values are the fully qualified classnames which do the calculations |
|
39 | - * These callbacks should accept as arguments: |
|
40 | - * the wpdb row results, |
|
41 | - * the WP_Request object, |
|
42 | - * the controller object |
|
43 | - */ |
|
44 | - public function mapping($refresh = false) |
|
45 | - { |
|
46 | - if (! $this->mapping || $refresh) { |
|
47 | - $this->mapping = $this->generateNewMapping(); |
|
48 | - } |
|
49 | - return $this->mapping; |
|
50 | - } |
|
51 | - |
|
52 | - |
|
53 | - |
|
54 | - /** |
|
55 | - * Generates anew mapping between model calculated fields and their callbacks |
|
56 | - * |
|
57 | - * @return array |
|
58 | - */ |
|
59 | - protected function generateNewMapping() |
|
60 | - { |
|
61 | - $rest_api_calculations_namespace = 'EventEspresso\core\libraries\rest_api\calculations\\'; |
|
62 | - $event_calculations_class = $rest_api_calculations_namespace . 'Event'; |
|
63 | - $datetime_calculations_class = $rest_api_calculations_namespace . 'Datetime'; |
|
64 | - $registration_class = $rest_api_calculations_namespace . 'Registration'; |
|
65 | - return apply_filters( |
|
66 | - 'FHEE__EventEspresso\core\libraries\rest_api\Calculated_Model_Fields__mapping', |
|
67 | - array( |
|
68 | - 'Event' => array( |
|
69 | - 'optimum_sales_at_start' => $event_calculations_class, |
|
70 | - 'optimum_sales_now' => $event_calculations_class, |
|
71 | - 'spots_taken' => $event_calculations_class, |
|
72 | - 'spots_taken_pending_payment' => $event_calculations_class, |
|
73 | - 'spaces_remaining' => $event_calculations_class, |
|
74 | - 'registrations_checked_in_count' => $event_calculations_class, |
|
75 | - 'registrations_checked_out_count' => $event_calculations_class, |
|
76 | - 'image_thumbnail' => $event_calculations_class, |
|
77 | - 'image_medium' => $event_calculations_class, |
|
78 | - 'image_medium_large' => $event_calculations_class, |
|
79 | - 'image_large' => $event_calculations_class, |
|
80 | - 'image_post_thumbnail' => $event_calculations_class, |
|
81 | - 'image_full' => $event_calculations_class, |
|
82 | - ), |
|
83 | - 'Datetime' => array( |
|
84 | - 'spaces_remaining_considering_tickets' => $datetime_calculations_class, |
|
85 | - 'registrations_checked_in_count' => $datetime_calculations_class, |
|
86 | - 'registrations_checked_out_count' => $datetime_calculations_class, |
|
87 | - 'spots_taken_pending_payment' => $datetime_calculations_class, |
|
88 | - ), |
|
89 | - 'Registration' => array( |
|
90 | - 'datetime_checkin_stati' => $registration_class, |
|
91 | - ), |
|
92 | - ) |
|
93 | - ); |
|
94 | - } |
|
95 | - |
|
96 | - |
|
97 | - |
|
98 | - /** |
|
99 | - * Gets the known calculated fields for model |
|
100 | - * |
|
101 | - * @param EEM_Base $model |
|
102 | - * @return array allowable values for this field |
|
103 | - */ |
|
104 | - public function retrieveCalculatedFieldsForModel(EEM_Base $model) |
|
105 | - { |
|
106 | - $mapping = $this->mapping(); |
|
107 | - if (isset($mapping[$model->get_this_model_name()])) { |
|
108 | - return array_keys($mapping[$model->get_this_model_name()]); |
|
109 | - } else { |
|
110 | - return array(); |
|
111 | - } |
|
112 | - } |
|
113 | - |
|
114 | - |
|
115 | - |
|
116 | - /** |
|
117 | - * Retrieves the value for this calculation |
|
118 | - * |
|
119 | - * @param EEM_Base $model |
|
120 | - * @param string $field_name |
|
121 | - * @param array $wpdb_row |
|
122 | - * @param \WP_REST_Request |
|
123 | - * @param \EventEspresso\core\libraries\rest_api\controllers\Base $controller |
|
124 | - * @return mixed|null |
|
125 | - * @throws \EE_Error |
|
126 | - */ |
|
127 | - public function retrieveCalculatedFieldValue( |
|
128 | - EEM_Base $model, |
|
129 | - $field_name, |
|
130 | - $wpdb_row, |
|
131 | - $rest_request, |
|
132 | - Base $controller |
|
133 | - ) { |
|
134 | - $mapping = $this->mapping(); |
|
135 | - if (isset($mapping[$model->get_this_model_name()]) |
|
136 | - && isset($mapping[$model->get_this_model_name()][$field_name]) |
|
137 | - ) { |
|
138 | - $classname = $mapping[$model->get_this_model_name()][$field_name]; |
|
139 | - $class_method_name = EEH_Inflector::camelize_all_but_first($field_name); |
|
140 | - return call_user_func(array($classname, $class_method_name), $wpdb_row, $rest_request, $controller); |
|
141 | - } |
|
142 | - throw new RestException( |
|
143 | - 'calculated_field_does_not_exist', |
|
144 | - sprintf( |
|
145 | - __('There is no calculated field %1$s on resource %2$s', 'event_espresso'), |
|
146 | - $field_name, |
|
147 | - $model->get_this_model_name() |
|
148 | - ) |
|
149 | - ); |
|
150 | - } |
|
27 | + /** |
|
28 | + * @var array |
|
29 | + */ |
|
30 | + protected $mapping; |
|
31 | + |
|
32 | + |
|
33 | + |
|
34 | + /** |
|
35 | + * @param bool $refresh |
|
36 | + * @return array top-level-keys are model names (eg "Event") |
|
37 | + * next-level are the calculated field names AND method names on classes |
|
38 | + * which perform calculations, values are the fully qualified classnames which do the calculations |
|
39 | + * These callbacks should accept as arguments: |
|
40 | + * the wpdb row results, |
|
41 | + * the WP_Request object, |
|
42 | + * the controller object |
|
43 | + */ |
|
44 | + public function mapping($refresh = false) |
|
45 | + { |
|
46 | + if (! $this->mapping || $refresh) { |
|
47 | + $this->mapping = $this->generateNewMapping(); |
|
48 | + } |
|
49 | + return $this->mapping; |
|
50 | + } |
|
51 | + |
|
52 | + |
|
53 | + |
|
54 | + /** |
|
55 | + * Generates anew mapping between model calculated fields and their callbacks |
|
56 | + * |
|
57 | + * @return array |
|
58 | + */ |
|
59 | + protected function generateNewMapping() |
|
60 | + { |
|
61 | + $rest_api_calculations_namespace = 'EventEspresso\core\libraries\rest_api\calculations\\'; |
|
62 | + $event_calculations_class = $rest_api_calculations_namespace . 'Event'; |
|
63 | + $datetime_calculations_class = $rest_api_calculations_namespace . 'Datetime'; |
|
64 | + $registration_class = $rest_api_calculations_namespace . 'Registration'; |
|
65 | + return apply_filters( |
|
66 | + 'FHEE__EventEspresso\core\libraries\rest_api\Calculated_Model_Fields__mapping', |
|
67 | + array( |
|
68 | + 'Event' => array( |
|
69 | + 'optimum_sales_at_start' => $event_calculations_class, |
|
70 | + 'optimum_sales_now' => $event_calculations_class, |
|
71 | + 'spots_taken' => $event_calculations_class, |
|
72 | + 'spots_taken_pending_payment' => $event_calculations_class, |
|
73 | + 'spaces_remaining' => $event_calculations_class, |
|
74 | + 'registrations_checked_in_count' => $event_calculations_class, |
|
75 | + 'registrations_checked_out_count' => $event_calculations_class, |
|
76 | + 'image_thumbnail' => $event_calculations_class, |
|
77 | + 'image_medium' => $event_calculations_class, |
|
78 | + 'image_medium_large' => $event_calculations_class, |
|
79 | + 'image_large' => $event_calculations_class, |
|
80 | + 'image_post_thumbnail' => $event_calculations_class, |
|
81 | + 'image_full' => $event_calculations_class, |
|
82 | + ), |
|
83 | + 'Datetime' => array( |
|
84 | + 'spaces_remaining_considering_tickets' => $datetime_calculations_class, |
|
85 | + 'registrations_checked_in_count' => $datetime_calculations_class, |
|
86 | + 'registrations_checked_out_count' => $datetime_calculations_class, |
|
87 | + 'spots_taken_pending_payment' => $datetime_calculations_class, |
|
88 | + ), |
|
89 | + 'Registration' => array( |
|
90 | + 'datetime_checkin_stati' => $registration_class, |
|
91 | + ), |
|
92 | + ) |
|
93 | + ); |
|
94 | + } |
|
95 | + |
|
96 | + |
|
97 | + |
|
98 | + /** |
|
99 | + * Gets the known calculated fields for model |
|
100 | + * |
|
101 | + * @param EEM_Base $model |
|
102 | + * @return array allowable values for this field |
|
103 | + */ |
|
104 | + public function retrieveCalculatedFieldsForModel(EEM_Base $model) |
|
105 | + { |
|
106 | + $mapping = $this->mapping(); |
|
107 | + if (isset($mapping[$model->get_this_model_name()])) { |
|
108 | + return array_keys($mapping[$model->get_this_model_name()]); |
|
109 | + } else { |
|
110 | + return array(); |
|
111 | + } |
|
112 | + } |
|
113 | + |
|
114 | + |
|
115 | + |
|
116 | + /** |
|
117 | + * Retrieves the value for this calculation |
|
118 | + * |
|
119 | + * @param EEM_Base $model |
|
120 | + * @param string $field_name |
|
121 | + * @param array $wpdb_row |
|
122 | + * @param \WP_REST_Request |
|
123 | + * @param \EventEspresso\core\libraries\rest_api\controllers\Base $controller |
|
124 | + * @return mixed|null |
|
125 | + * @throws \EE_Error |
|
126 | + */ |
|
127 | + public function retrieveCalculatedFieldValue( |
|
128 | + EEM_Base $model, |
|
129 | + $field_name, |
|
130 | + $wpdb_row, |
|
131 | + $rest_request, |
|
132 | + Base $controller |
|
133 | + ) { |
|
134 | + $mapping = $this->mapping(); |
|
135 | + if (isset($mapping[$model->get_this_model_name()]) |
|
136 | + && isset($mapping[$model->get_this_model_name()][$field_name]) |
|
137 | + ) { |
|
138 | + $classname = $mapping[$model->get_this_model_name()][$field_name]; |
|
139 | + $class_method_name = EEH_Inflector::camelize_all_but_first($field_name); |
|
140 | + return call_user_func(array($classname, $class_method_name), $wpdb_row, $rest_request, $controller); |
|
141 | + } |
|
142 | + throw new RestException( |
|
143 | + 'calculated_field_does_not_exist', |
|
144 | + sprintf( |
|
145 | + __('There is no calculated field %1$s on resource %2$s', 'event_espresso'), |
|
146 | + $field_name, |
|
147 | + $model->get_this_model_name() |
|
148 | + ) |
|
149 | + ); |
|
150 | + } |
|
151 | 151 | } |
@@ -12,7 +12,7 @@ discard block |
||
12 | 12 | * @author Mike Nelson |
13 | 13 | * @since $VID:$ |
14 | 14 | */ |
15 | -if (! defined('EVENT_ESPRESSO_VERSION')) { |
|
15 | +if ( ! defined('EVENT_ESPRESSO_VERSION')) { |
|
16 | 16 | exit('No direct script access allowed'); |
17 | 17 | } |
18 | 18 | |
@@ -28,7 +28,7 @@ discard block |
||
28 | 28 | */ |
29 | 29 | protected static function verifyCurrentUserCan($required_permission, $attempted_calculation) |
30 | 30 | { |
31 | - if (! current_user_can($required_permission)) { |
|
31 | + if ( ! current_user_can($required_permission)) { |
|
32 | 32 | throw new RestException( |
33 | 33 | 'permission_denied', |
34 | 34 | sprintf( |
@@ -14,7 +14,7 @@ discard block |
||
14 | 14 | * @since $VID:$ |
15 | 15 | */ |
16 | 16 | if (! defined('EVENT_ESPRESSO_VERSION')) { |
17 | - exit('No direct script access allowed'); |
|
17 | + exit('No direct script access allowed'); |
|
18 | 18 | } |
19 | 19 | |
20 | 20 | |
@@ -22,41 +22,41 @@ discard block |
||
22 | 22 | class Base |
23 | 23 | { |
24 | 24 | |
25 | - /** |
|
26 | - * @param $required_permission |
|
27 | - * @param $attempted_calculation |
|
28 | - * @throws \EventEspresso\core\libraries\rest_api\RestException |
|
29 | - */ |
|
30 | - protected static function verifyCurrentUserCan($required_permission, $attempted_calculation) |
|
31 | - { |
|
32 | - if (! current_user_can($required_permission)) { |
|
33 | - throw new RestException( |
|
34 | - 'permission_denied', |
|
35 | - sprintf( |
|
36 | - __( |
|
37 | - // @codingStandardsIgnoreStart |
|
38 | - 'Permission denied, you cannot calculate %1$s on %2$s because you do not have the capability "%3$s"', |
|
39 | - // @codingStandardsIgnoreEnd |
|
40 | - 'event_espresso' |
|
41 | - ), |
|
42 | - $attempted_calculation, |
|
43 | - EEH_Inflector::pluralize_and_lower(self::getResourceName()), |
|
44 | - $required_permission |
|
45 | - ) |
|
46 | - ); |
|
47 | - } |
|
48 | - } |
|
49 | - |
|
50 | - |
|
51 | - |
|
52 | - /** |
|
53 | - * Gets the name of the resource of the called class |
|
54 | - * |
|
55 | - * @return string |
|
56 | - */ |
|
57 | - public static function getResourceName() |
|
58 | - { |
|
59 | - $classname = get_called_class(); |
|
60 | - return substr($classname, strrpos($classname, '\\') + 1); |
|
61 | - } |
|
25 | + /** |
|
26 | + * @param $required_permission |
|
27 | + * @param $attempted_calculation |
|
28 | + * @throws \EventEspresso\core\libraries\rest_api\RestException |
|
29 | + */ |
|
30 | + protected static function verifyCurrentUserCan($required_permission, $attempted_calculation) |
|
31 | + { |
|
32 | + if (! current_user_can($required_permission)) { |
|
33 | + throw new RestException( |
|
34 | + 'permission_denied', |
|
35 | + sprintf( |
|
36 | + __( |
|
37 | + // @codingStandardsIgnoreStart |
|
38 | + 'Permission denied, you cannot calculate %1$s on %2$s because you do not have the capability "%3$s"', |
|
39 | + // @codingStandardsIgnoreEnd |
|
40 | + 'event_espresso' |
|
41 | + ), |
|
42 | + $attempted_calculation, |
|
43 | + EEH_Inflector::pluralize_and_lower(self::getResourceName()), |
|
44 | + $required_permission |
|
45 | + ) |
|
46 | + ); |
|
47 | + } |
|
48 | + } |
|
49 | + |
|
50 | + |
|
51 | + |
|
52 | + /** |
|
53 | + * Gets the name of the resource of the called class |
|
54 | + * |
|
55 | + * @return string |
|
56 | + */ |
|
57 | + public static function getResourceName() |
|
58 | + { |
|
59 | + $classname = get_called_class(); |
|
60 | + return substr($classname, strrpos($classname, '\\') + 1); |
|
61 | + } |
|
62 | 62 | } |
@@ -15,7 +15,7 @@ discard block |
||
15 | 15 | * @author Mike Nelson |
16 | 16 | * @since $VID:$ |
17 | 17 | */ |
18 | -if (! defined('EVENT_ESPRESSO_VERSION')) { |
|
18 | +if ( ! defined('EVENT_ESPRESSO_VERSION')) { |
|
19 | 19 | exit('No direct script access allowed'); |
20 | 20 | } |
21 | 21 | |
@@ -40,7 +40,7 @@ discard block |
||
40 | 40 | } else { |
41 | 41 | $reg = null; |
42 | 42 | } |
43 | - if (! $reg instanceof EE_Registration |
|
43 | + if ( ! $reg instanceof EE_Registration |
|
44 | 44 | ) { |
45 | 45 | throw new \EE_Error( |
46 | 46 | sprintf( |
@@ -17,7 +17,7 @@ discard block |
||
17 | 17 | * @since $VID:$ |
18 | 18 | */ |
19 | 19 | if (! defined('EVENT_ESPRESSO_VERSION')) { |
20 | - exit('No direct script access allowed'); |
|
20 | + exit('No direct script access allowed'); |
|
21 | 21 | } |
22 | 22 | |
23 | 23 | |
@@ -25,62 +25,62 @@ discard block |
||
25 | 25 | class Registration extends Calculations_Base |
26 | 26 | { |
27 | 27 | |
28 | - /** |
|
29 | - * Calculates the checkin status for each datetime this registration has access to |
|
30 | - * |
|
31 | - * @param array $wpdb_row |
|
32 | - * @param \WP_REST_Request $request |
|
33 | - * @param Base $controller |
|
34 | - * @return array |
|
35 | - * @throws \EE_Error |
|
36 | - */ |
|
37 | - public static function datetimeCheckinStati($wpdb_row, $request, $controller) |
|
38 | - { |
|
39 | - if (is_array($wpdb_row) && isset($wpdb_row['Registration.REG_ID'])) { |
|
40 | - $reg = EEM_Registration::instance()->get_one_by_ID($wpdb_row['Registration.REG_ID']); |
|
41 | - } else { |
|
42 | - $reg = null; |
|
43 | - } |
|
44 | - if (! $reg instanceof EE_Registration |
|
45 | - ) { |
|
46 | - throw new \EE_Error( |
|
47 | - sprintf( |
|
48 | - __( |
|
49 | - // @codingStandardsIgnoreStart |
|
50 | - 'Cannot calculate datetime_checkin_stati because the registration with ID %1$s (from database row %2$s) was not found', |
|
51 | - // @codingStandardsIgnoreEnd |
|
52 | - 'event_espresso' |
|
53 | - ), |
|
54 | - $wpdb_row['Registration.REG_ID'], |
|
55 | - print_r($wpdb_row, true) |
|
56 | - ) |
|
57 | - ); |
|
58 | - } |
|
59 | - $datetime_ids = EEM_Datetime::instance()->get_col( |
|
60 | - array( |
|
61 | - array( |
|
62 | - 'Ticket.TKT_ID' => $reg->ticket_ID(), |
|
63 | - ), |
|
64 | - 'default_where_conditions' => \EEM_Base::default_where_conditions_minimum_all |
|
65 | - ) |
|
66 | - ); |
|
67 | - $checkin_stati = array(); |
|
68 | - foreach ($datetime_ids as $datetime_id) { |
|
69 | - $status = $reg->check_in_status_for_datetime($datetime_id); |
|
70 | - switch ($status) { |
|
71 | - case EE_Checkin::status_checked_out: |
|
72 | - $status_pretty = 'OUT'; |
|
73 | - break; |
|
74 | - case EE_Checkin::status_checked_in: |
|
75 | - $status_pretty = 'IN'; |
|
76 | - break; |
|
77 | - case EE_Checkin::status_checked_never: |
|
78 | - default: |
|
79 | - $status_pretty = 'NEVER'; |
|
80 | - break; |
|
81 | - } |
|
82 | - $checkin_stati[$datetime_id] = $status_pretty; |
|
83 | - } |
|
84 | - return $checkin_stati; |
|
85 | - } |
|
28 | + /** |
|
29 | + * Calculates the checkin status for each datetime this registration has access to |
|
30 | + * |
|
31 | + * @param array $wpdb_row |
|
32 | + * @param \WP_REST_Request $request |
|
33 | + * @param Base $controller |
|
34 | + * @return array |
|
35 | + * @throws \EE_Error |
|
36 | + */ |
|
37 | + public static function datetimeCheckinStati($wpdb_row, $request, $controller) |
|
38 | + { |
|
39 | + if (is_array($wpdb_row) && isset($wpdb_row['Registration.REG_ID'])) { |
|
40 | + $reg = EEM_Registration::instance()->get_one_by_ID($wpdb_row['Registration.REG_ID']); |
|
41 | + } else { |
|
42 | + $reg = null; |
|
43 | + } |
|
44 | + if (! $reg instanceof EE_Registration |
|
45 | + ) { |
|
46 | + throw new \EE_Error( |
|
47 | + sprintf( |
|
48 | + __( |
|
49 | + // @codingStandardsIgnoreStart |
|
50 | + 'Cannot calculate datetime_checkin_stati because the registration with ID %1$s (from database row %2$s) was not found', |
|
51 | + // @codingStandardsIgnoreEnd |
|
52 | + 'event_espresso' |
|
53 | + ), |
|
54 | + $wpdb_row['Registration.REG_ID'], |
|
55 | + print_r($wpdb_row, true) |
|
56 | + ) |
|
57 | + ); |
|
58 | + } |
|
59 | + $datetime_ids = EEM_Datetime::instance()->get_col( |
|
60 | + array( |
|
61 | + array( |
|
62 | + 'Ticket.TKT_ID' => $reg->ticket_ID(), |
|
63 | + ), |
|
64 | + 'default_where_conditions' => \EEM_Base::default_where_conditions_minimum_all |
|
65 | + ) |
|
66 | + ); |
|
67 | + $checkin_stati = array(); |
|
68 | + foreach ($datetime_ids as $datetime_id) { |
|
69 | + $status = $reg->check_in_status_for_datetime($datetime_id); |
|
70 | + switch ($status) { |
|
71 | + case EE_Checkin::status_checked_out: |
|
72 | + $status_pretty = 'OUT'; |
|
73 | + break; |
|
74 | + case EE_Checkin::status_checked_in: |
|
75 | + $status_pretty = 'IN'; |
|
76 | + break; |
|
77 | + case EE_Checkin::status_checked_never: |
|
78 | + default: |
|
79 | + $status_pretty = 'NEVER'; |
|
80 | + break; |
|
81 | + } |
|
82 | + $checkin_stati[$datetime_id] = $status_pretty; |
|
83 | + } |
|
84 | + return $checkin_stati; |
|
85 | + } |
|
86 | 86 | } |
@@ -9,7 +9,7 @@ discard block |
||
9 | 9 | use EE_Restriction_Generator_Base; |
10 | 10 | use EEH_DTT_Helper; |
11 | 11 | |
12 | -if (! defined('EVENT_ESPRESSO_VERSION')) { |
|
12 | +if ( ! defined('EVENT_ESPRESSO_VERSION')) { |
|
13 | 13 | exit('No direct script access allowed'); |
14 | 14 | } |
15 | 15 | |
@@ -90,8 +90,8 @@ discard block |
||
90 | 90 | ), |
91 | 91 | 'default_currency' => EE_Config::instance()->currency, |
92 | 92 | 'authentication' => array( |
93 | - 'received_basic_auth_data' => (bool)$wp_json_basic_auth_received_data, |
|
94 | - 'insecure_usage_of_basic_auth' => (bool)$insecure_usage_of_basic_auth, |
|
93 | + 'received_basic_auth_data' => (bool) $wp_json_basic_auth_received_data, |
|
94 | + 'insecure_usage_of_basic_auth' => (bool) $insecure_usage_of_basic_auth, |
|
95 | 95 | 'warning' => $warning |
96 | 96 | ) |
97 | 97 | ); |
@@ -9,7 +9,7 @@ discard block |
||
9 | 9 | use EEH_DTT_Helper; |
10 | 10 | |
11 | 11 | if (! defined('EVENT_ESPRESSO_VERSION')) { |
12 | - exit('No direct script access allowed'); |
|
12 | + exit('No direct script access allowed'); |
|
13 | 13 | } |
14 | 14 | |
15 | 15 | |
@@ -25,82 +25,82 @@ discard block |
||
25 | 25 | class Read |
26 | 26 | { |
27 | 27 | |
28 | - /** |
|
29 | - * @param WP_REST_Request $request |
|
30 | - * @param string $version |
|
31 | - * @return EE_Config|WP_Error |
|
32 | - */ |
|
33 | - public static function handleRequest(WP_REST_Request $request, $version) |
|
34 | - { |
|
35 | - $cap = EE_Restriction_Generator_Base::get_default_restrictions_cap(); |
|
36 | - if (EE_Capabilities::instance()->current_user_can($cap, 'read_over_api')) { |
|
37 | - return EE_Config::instance(); |
|
38 | - } else { |
|
39 | - return new WP_Error( |
|
40 | - 'cannot_read_config', |
|
41 | - sprintf( |
|
42 | - __( |
|
43 | - 'You do not have the necessary capabilities (%s) to read Event Espresso Configuration data', |
|
44 | - 'event_espresso' |
|
45 | - ), |
|
46 | - $cap |
|
47 | - ), |
|
48 | - array('status' => 403) |
|
49 | - ); |
|
50 | - } |
|
51 | - } |
|
28 | + /** |
|
29 | + * @param WP_REST_Request $request |
|
30 | + * @param string $version |
|
31 | + * @return EE_Config|WP_Error |
|
32 | + */ |
|
33 | + public static function handleRequest(WP_REST_Request $request, $version) |
|
34 | + { |
|
35 | + $cap = EE_Restriction_Generator_Base::get_default_restrictions_cap(); |
|
36 | + if (EE_Capabilities::instance()->current_user_can($cap, 'read_over_api')) { |
|
37 | + return EE_Config::instance(); |
|
38 | + } else { |
|
39 | + return new WP_Error( |
|
40 | + 'cannot_read_config', |
|
41 | + sprintf( |
|
42 | + __( |
|
43 | + 'You do not have the necessary capabilities (%s) to read Event Espresso Configuration data', |
|
44 | + 'event_espresso' |
|
45 | + ), |
|
46 | + $cap |
|
47 | + ), |
|
48 | + array('status' => 403) |
|
49 | + ); |
|
50 | + } |
|
51 | + } |
|
52 | 52 | |
53 | 53 | |
54 | 54 | |
55 | - /** |
|
56 | - * Handles the request for public site info |
|
57 | - * |
|
58 | - * @global $wp_json_basic_auth_success boolean set by the basic auth plugin, indicates if the |
|
59 | - * current user could be authenticated using basic auth data |
|
60 | - * @global $wp_json_basic_auth_received_data boolean set by the basic auth plugin, indicates if |
|
61 | - * basic auth data was somehow received |
|
62 | - * @param WP_REST_Request $request |
|
63 | - * @param string $version |
|
64 | - * @return array|WP_Error |
|
65 | - */ |
|
66 | - public static function handleRequestSiteInfo(WP_REST_Request $request, $version) |
|
67 | - { |
|
68 | - global $wp_json_basic_auth_success, $wp_json_basic_auth_received_data; |
|
69 | - $insecure_usage_of_basic_auth = apply_filters( |
|
70 | - // @codingStandardsIgnoreStart |
|
71 | - 'EventEspresso__core__libraries__rest_api__controllers__config__handle_request_site_info__insecure_usage_of_basic_auth', |
|
72 | - // @codingStandardsIgnoreEnd |
|
73 | - $wp_json_basic_auth_success && ! is_ssl(), |
|
74 | - $request |
|
75 | - ); |
|
76 | - if ($insecure_usage_of_basic_auth) { |
|
77 | - $warning = sprintf( |
|
78 | - esc_html__( |
|
79 | - // @codingStandardsIgnoreStart |
|
80 | - 'Notice: We strongly recommend installing an SSL Certificate on your website to keep your data secure. %1$sPlease see our recommendations.%2$s', |
|
81 | - // @codingStandardsIgnoreEnd |
|
82 | - 'event_espresso' |
|
83 | - ), |
|
84 | - '<a href="https://eventespresso.com/wiki/rest-api-security-recommendations/">', |
|
85 | - '</a>' |
|
86 | - ); |
|
87 | - } else { |
|
88 | - $warning = ''; |
|
89 | - } |
|
90 | - return array( |
|
91 | - 'default_timezone' => array( |
|
92 | - 'pretty' => EEH_DTT_Helper::get_timezone_string_for_display(), |
|
93 | - 'string' => get_option('timezone_string'), |
|
94 | - 'offset' => EEH_DTT_Helper::get_site_timezone_gmt_offset(), |
|
95 | - ), |
|
96 | - 'default_currency' => EE_Config::instance()->currency, |
|
97 | - 'authentication' => array( |
|
98 | - 'received_basic_auth_data' => (bool)$wp_json_basic_auth_received_data, |
|
99 | - 'insecure_usage_of_basic_auth' => (bool)$insecure_usage_of_basic_auth, |
|
100 | - 'warning' => $warning |
|
101 | - ) |
|
102 | - ); |
|
103 | - } |
|
55 | + /** |
|
56 | + * Handles the request for public site info |
|
57 | + * |
|
58 | + * @global $wp_json_basic_auth_success boolean set by the basic auth plugin, indicates if the |
|
59 | + * current user could be authenticated using basic auth data |
|
60 | + * @global $wp_json_basic_auth_received_data boolean set by the basic auth plugin, indicates if |
|
61 | + * basic auth data was somehow received |
|
62 | + * @param WP_REST_Request $request |
|
63 | + * @param string $version |
|
64 | + * @return array|WP_Error |
|
65 | + */ |
|
66 | + public static function handleRequestSiteInfo(WP_REST_Request $request, $version) |
|
67 | + { |
|
68 | + global $wp_json_basic_auth_success, $wp_json_basic_auth_received_data; |
|
69 | + $insecure_usage_of_basic_auth = apply_filters( |
|
70 | + // @codingStandardsIgnoreStart |
|
71 | + 'EventEspresso__core__libraries__rest_api__controllers__config__handle_request_site_info__insecure_usage_of_basic_auth', |
|
72 | + // @codingStandardsIgnoreEnd |
|
73 | + $wp_json_basic_auth_success && ! is_ssl(), |
|
74 | + $request |
|
75 | + ); |
|
76 | + if ($insecure_usage_of_basic_auth) { |
|
77 | + $warning = sprintf( |
|
78 | + esc_html__( |
|
79 | + // @codingStandardsIgnoreStart |
|
80 | + 'Notice: We strongly recommend installing an SSL Certificate on your website to keep your data secure. %1$sPlease see our recommendations.%2$s', |
|
81 | + // @codingStandardsIgnoreEnd |
|
82 | + 'event_espresso' |
|
83 | + ), |
|
84 | + '<a href="https://eventespresso.com/wiki/rest-api-security-recommendations/">', |
|
85 | + '</a>' |
|
86 | + ); |
|
87 | + } else { |
|
88 | + $warning = ''; |
|
89 | + } |
|
90 | + return array( |
|
91 | + 'default_timezone' => array( |
|
92 | + 'pretty' => EEH_DTT_Helper::get_timezone_string_for_display(), |
|
93 | + 'string' => get_option('timezone_string'), |
|
94 | + 'offset' => EEH_DTT_Helper::get_site_timezone_gmt_offset(), |
|
95 | + ), |
|
96 | + 'default_currency' => EE_Config::instance()->currency, |
|
97 | + 'authentication' => array( |
|
98 | + 'received_basic_auth_data' => (bool)$wp_json_basic_auth_received_data, |
|
99 | + 'insecure_usage_of_basic_auth' => (bool)$insecure_usage_of_basic_auth, |
|
100 | + 'warning' => $warning |
|
101 | + ) |
|
102 | + ); |
|
103 | + } |
|
104 | 104 | } |
105 | 105 | |
106 | 106 | // End of file Read.php |
@@ -4,7 +4,7 @@ discard block |
||
4 | 4 | use EE_Registry; |
5 | 5 | use EED_Core_Rest_Api; |
6 | 6 | |
7 | -if (! defined('EVENT_ESPRESSO_VERSION')) { |
|
7 | +if ( ! defined('EVENT_ESPRESSO_VERSION')) { |
|
8 | 8 | exit('No direct script access allowed'); |
9 | 9 | } |
10 | 10 | |
@@ -97,7 +97,7 @@ discard block |
||
97 | 97 | */ |
98 | 98 | public function __construct($requested_version) |
99 | 99 | { |
100 | - $this->requested_version = (string)$requested_version; |
|
100 | + $this->requested_version = (string) $requested_version; |
|
101 | 101 | $this->model_changes = array( |
102 | 102 | '4.8.29' => array( |
103 | 103 | //first version where the REST API is in EE core, so no need |
@@ -122,7 +122,7 @@ discard block |
||
122 | 122 | foreach ($extra_fields as $fieldname => $field_data) { |
123 | 123 | $this->resource_changes[$model_classname][$fieldname]['name'] = $fieldname; |
124 | 124 | foreach ($defaults as $attribute => $default_value) { |
125 | - if (! isset($this->resource_changes[$model_classname][$fieldname][$attribute])) { |
|
125 | + if ( ! isset($this->resource_changes[$model_classname][$fieldname][$attribute])) { |
|
126 | 126 | $this->resource_changes[$model_classname][$fieldname][$attribute] = $default_value; |
127 | 127 | } |
128 | 128 | } |
@@ -294,7 +294,7 @@ discard block |
||
294 | 294 | */ |
295 | 295 | public function fieldsOnModelInThisVersion($model) |
296 | 296 | { |
297 | - if (! isset($this->cached_fields_on_models[$model->get_this_model_name()])) { |
|
297 | + if ( ! isset($this->cached_fields_on_models[$model->get_this_model_name()])) { |
|
298 | 298 | //get all model changes between the requested version and current core version |
299 | 299 | $changes = $this->modelChangesBetweenRequestedVersionAndCurrent(); |
300 | 300 | //fetch all fields currently on this model |
@@ -5,7 +5,7 @@ discard block |
||
5 | 5 | use EED_Core_Rest_Api; |
6 | 6 | |
7 | 7 | if (! defined('EVENT_ESPRESSO_VERSION')) { |
8 | - exit('No direct script access allowed'); |
|
8 | + exit('No direct script access allowed'); |
|
9 | 9 | } |
10 | 10 | |
11 | 11 | |
@@ -26,461 +26,461 @@ discard block |
||
26 | 26 | class ModelVersionInfo |
27 | 27 | { |
28 | 28 | |
29 | - /** |
|
30 | - * Constant used in the $_model_changes array to indicate that a model |
|
31 | - * was completely new in this version |
|
32 | - */ |
|
33 | - const MODEL_ADDED = 'model_added_in_this_version'; |
|
34 | - |
|
35 | - /** |
|
36 | - * Top-level keys are versions (major and minor version numbers, eg "4.6") |
|
37 | - * next-level keys are model names (eg "Event") that underwent some change in that version |
|
38 | - * and the value is either Model_Version_Info::model_added (indicating the model is completely NEW in this version), |
|
39 | - * or it's an array where the values are model field names, |
|
40 | - * or API resource properties (ie, non-model fields that appear in REST API results) |
|
41 | - * If a version is missing then we don't know anything about what changes it introduced from the previous version |
|
42 | - * |
|
43 | - * @var array |
|
44 | - */ |
|
45 | - protected $model_changes = array(); |
|
46 | - |
|
47 | - /** |
|
48 | - * top-level keys are version numbers, |
|
49 | - * next-level keys are model CLASSNAMES (even parent classnames), |
|
50 | - * and next-level keys are extra resource properties to attach to those models' resources, |
|
51 | - * and next-level key-value pairs, where the keys are: |
|
52 | - * 'raw', 'type', 'nullable', 'table_alias', 'table_column', 'always_available' |
|
53 | - * |
|
54 | - * @var array |
|
55 | - */ |
|
56 | - protected $resource_changes = array(); |
|
57 | - |
|
58 | - /** |
|
59 | - * @var string indicating what version of the API was requested |
|
60 | - * (eg although core might be at version 4.8.11, they may have sent a request |
|
61 | - * for 4.6) |
|
62 | - */ |
|
63 | - protected $requested_version = null; |
|
64 | - |
|
65 | - /** |
|
66 | - * Keys are model names, values are their classnames. |
|
67 | - * We cache this so we only need to calculate this once per request |
|
68 | - * |
|
69 | - * @var array |
|
70 | - */ |
|
71 | - protected $cached_models_for_requested_version = null; |
|
72 | - |
|
73 | - /** |
|
74 | - * @var array |
|
75 | - */ |
|
76 | - protected $cached_model_changes_between_requested_version_and_current = null; |
|
77 | - |
|
78 | - /** |
|
79 | - * @var array |
|
80 | - */ |
|
81 | - protected $cached_resource_changes_between_requested_version_and_current = null; |
|
82 | - |
|
83 | - /** |
|
84 | - * 2d array where top-level keys are model names, 2nd-level keys are field names |
|
85 | - * and values are the actual field objects |
|
86 | - * |
|
87 | - * @var array |
|
88 | - */ |
|
89 | - protected $cached_fields_on_models = array(); |
|
90 | - |
|
91 | - |
|
92 | - |
|
93 | - /** |
|
94 | - * Model_Version_Info constructor. |
|
95 | - * |
|
96 | - * @param string $requested_version |
|
97 | - */ |
|
98 | - public function __construct($requested_version) |
|
99 | - { |
|
100 | - $this->requested_version = (string)$requested_version; |
|
101 | - $this->model_changes = array( |
|
102 | - '4.8.29' => array( |
|
103 | - //first version where the REST API is in EE core, so no need |
|
104 | - //to specify how its different from the previous |
|
105 | - ), |
|
106 | - ); |
|
107 | - //setup data for "extra" fields added onto resources which don't actually exist on models |
|
108 | - $this->resource_changes = apply_filters( |
|
109 | - 'FHEE__Model_Version_Info___construct__extra_resource_properties_for_models', |
|
110 | - array() |
|
111 | - ); |
|
112 | - $defaults = array( |
|
113 | - 'raw' => false, |
|
114 | - 'type' => 'N/A', |
|
115 | - 'nullable' => true, |
|
116 | - 'table_alias' => 'N/A', |
|
117 | - 'table_column' => 'N/A', |
|
118 | - 'always_available' => true, |
|
119 | - ); |
|
120 | - foreach ($this->resource_changes as $version => $model_classnames) { |
|
121 | - foreach ($model_classnames as $model_classname => $extra_fields) { |
|
122 | - foreach ($extra_fields as $fieldname => $field_data) { |
|
123 | - $this->resource_changes[$model_classname][$fieldname]['name'] = $fieldname; |
|
124 | - foreach ($defaults as $attribute => $default_value) { |
|
125 | - if (! isset($this->resource_changes[$model_classname][$fieldname][$attribute])) { |
|
126 | - $this->resource_changes[$model_classname][$fieldname][$attribute] = $default_value; |
|
127 | - } |
|
128 | - } |
|
129 | - } |
|
130 | - } |
|
131 | - } |
|
132 | - } |
|
133 | - |
|
134 | - |
|
135 | - |
|
136 | - /** |
|
137 | - * Returns a slice of Model_Version_Info::model_changes()'s array |
|
138 | - * indicating exactly what changes happened between the current core version, |
|
139 | - * and the version requested |
|
140 | - * |
|
141 | - * @return array |
|
142 | - */ |
|
143 | - public function modelChangesBetweenRequestedVersionAndCurrent() |
|
144 | - { |
|
145 | - if ($this->cached_model_changes_between_requested_version_and_current === null) { |
|
146 | - $model_changes = array(); |
|
147 | - foreach ($this->modelChanges() as $version => $models_changed_in_version) { |
|
148 | - if ($version <= EED_Core_Rest_Api::core_version() && $version > $this->requestedVersion()) { |
|
149 | - $model_changes[$version] = $models_changed_in_version; |
|
150 | - } |
|
151 | - } |
|
152 | - $this->cached_model_changes_between_requested_version_and_current = $model_changes; |
|
153 | - } |
|
154 | - return $this->cached_model_changes_between_requested_version_and_current; |
|
155 | - } |
|
156 | - |
|
157 | - |
|
158 | - |
|
159 | - /** |
|
160 | - * Returns a slice of Model_Version_Info::model_changes()'s array |
|
161 | - * indicating exactly what changes happened between the current core version, |
|
162 | - * and the version requested |
|
163 | - * |
|
164 | - * @return array |
|
165 | - */ |
|
166 | - public function resourceChangesBetweenRequestedVersionAndCurrent() |
|
167 | - { |
|
168 | - if ($this->cached_resource_changes_between_requested_version_and_current === null) { |
|
169 | - $resource_changes = array(); |
|
170 | - foreach ($this->resourceChanges() as $version => $model_classnames) { |
|
171 | - if ($version <= EED_Core_Rest_Api::core_version() && $version > $this->requestedVersion()) { |
|
172 | - $resource_changes[$version] = $model_classnames; |
|
173 | - } |
|
174 | - } |
|
175 | - $this->cached_resource_changes_between_requested_version_and_current = $resource_changes; |
|
176 | - } |
|
177 | - return $this->cached_resource_changes_between_requested_version_and_current; |
|
178 | - } |
|
179 | - |
|
180 | - |
|
181 | - |
|
182 | - /** |
|
183 | - * If a request was sent to 'wp-json/ee/v4.7/events' this would be '4.7' |
|
184 | - * |
|
185 | - * @return string like '4.6' |
|
186 | - */ |
|
187 | - public function requestedVersion() |
|
188 | - { |
|
189 | - return $this->requested_version; |
|
190 | - } |
|
191 | - |
|
192 | - |
|
193 | - |
|
194 | - /** |
|
195 | - * Returns an array describing how the models have changed in each version of core |
|
196 | - * that supports the API (starting at 4.6) |
|
197 | - * Top-level keys are versions (major and minor version numbers, eg "4.6") |
|
198 | - * next-level keys are model names (eg "Event") that underwent some change in that version |
|
199 | - * and the value is either NULL (indicating the model is completely NEW in this version), |
|
200 | - * or it's an array where fields are value names. |
|
201 | - * If a version is missing then we don't know anything about what changes it introduced from the previous version |
|
202 | - * |
|
203 | - * @return array |
|
204 | - */ |
|
205 | - public function modelChanges() |
|
206 | - { |
|
207 | - return $this->model_changes; |
|
208 | - } |
|
209 | - |
|
210 | - |
|
211 | - |
|
212 | - /** |
|
213 | - * Takes into account the requested version, and the current version, and |
|
214 | - * what changed between the two, and tries to return. |
|
215 | - * Analogous to EE_Registry::instance()->non_abstract_db_models |
|
216 | - * |
|
217 | - * @return array keys are model names, values are their classname |
|
218 | - */ |
|
219 | - public function modelsForRequestedVersion() |
|
220 | - { |
|
221 | - if ($this->cached_models_for_requested_version === null) { |
|
222 | - $all_models_in_current_version = EE_Registry::instance()->non_abstract_db_models; |
|
223 | - foreach ($this->modelChangesBetweenRequestedVersionAndCurrent() as $version => $models_changed) { |
|
224 | - foreach ($models_changed as $model_name => $new_indicator_or_fields_added) { |
|
225 | - if ($new_indicator_or_fields_added === ModelVersionInfo::MODEL_ADDED) { |
|
226 | - unset($all_models_in_current_version[$model_name]); |
|
227 | - } |
|
228 | - } |
|
229 | - } |
|
230 | - $this->cached_models_for_requested_version = apply_filters( |
|
231 | - 'FHEE__EventEspresso_core_libraries_rest_api__models_for_requested_version', |
|
232 | - $all_models_in_current_version, |
|
233 | - $this |
|
234 | - ); |
|
235 | - } |
|
236 | - return $this->cached_models_for_requested_version; |
|
237 | - } |
|
238 | - |
|
239 | - |
|
240 | - |
|
241 | - /** |
|
242 | - * Determines if this is a valid model name in the requested version. |
|
243 | - * Similar to EE_Registry::instance()->is_model_name(), but takes the requested |
|
244 | - * version's models into account |
|
245 | - * |
|
246 | - * @param string $model_name eg 'Event' |
|
247 | - * @return boolean |
|
248 | - */ |
|
249 | - public function isModelNameInThisVersion($model_name) |
|
250 | - { |
|
251 | - $model_names = $this->modelsForRequestedVersion(); |
|
252 | - if (isset($model_names[$model_name])) { |
|
253 | - return true; |
|
254 | - } else { |
|
255 | - return false; |
|
256 | - } |
|
257 | - } |
|
258 | - |
|
259 | - |
|
260 | - |
|
261 | - /** |
|
262 | - * Wrapper for EE_Registry::instance()->load_model(), but takes the requested |
|
263 | - * version's models into account |
|
264 | - * |
|
265 | - * @param string $model_name |
|
266 | - * @return \EEM_Base |
|
267 | - * @throws \EE_Error |
|
268 | - */ |
|
269 | - public function loadModel($model_name) |
|
270 | - { |
|
271 | - if ($this->isModelNameInThisVersion($model_name)) { |
|
272 | - return EE_Registry::instance()->load_model($model_name); |
|
273 | - } else { |
|
274 | - throw new \EE_Error( |
|
275 | - sprintf( |
|
276 | - __( |
|
277 | - 'Cannot load model "%1$s" because it does not exist in version %2$s of Event Espresso', |
|
278 | - 'event_espresso' |
|
279 | - ), |
|
280 | - $model_name, |
|
281 | - $this->requestedVersion() |
|
282 | - ) |
|
283 | - ); |
|
284 | - } |
|
285 | - } |
|
286 | - |
|
287 | - |
|
288 | - |
|
289 | - /** |
|
290 | - * Gets all the fields that should exist on this model right now |
|
291 | - * |
|
292 | - * @param \EEM_Base $model |
|
293 | - * @return array|\EE_Model_Field_Base[] |
|
294 | - */ |
|
295 | - public function fieldsOnModelInThisVersion($model) |
|
296 | - { |
|
297 | - if (! isset($this->cached_fields_on_models[$model->get_this_model_name()])) { |
|
298 | - //get all model changes between the requested version and current core version |
|
299 | - $changes = $this->modelChangesBetweenRequestedVersionAndCurrent(); |
|
300 | - //fetch all fields currently on this model |
|
301 | - $current_fields = $model->field_settings(); |
|
302 | - //remove all fields that have been added since |
|
303 | - foreach ($changes as $version => $changes_in_version) { |
|
304 | - if (isset($changes_in_version[$model->get_this_model_name()]) |
|
305 | - && $changes_in_version[$model->get_this_model_name()] !== ModelVersionInfo::MODEL_ADDED |
|
306 | - ) { |
|
307 | - $current_fields = array_diff_key( |
|
308 | - $current_fields, |
|
309 | - array_flip($changes_in_version[$model->get_this_model_name()]) |
|
310 | - ); |
|
311 | - } |
|
312 | - } |
|
313 | - $this->cached_fields_on_models = $current_fields; |
|
314 | - } |
|
315 | - return $this->cached_fields_on_models; |
|
316 | - } |
|
317 | - |
|
318 | - |
|
319 | - |
|
320 | - /** |
|
321 | - * Determines if $object is of one of the classes of $classes. Similar to |
|
322 | - * in_array(), except this checks if $object is a subclass of the classnames provided |
|
323 | - * in $classnames |
|
324 | - * |
|
325 | - * @param object $object |
|
326 | - * @param array $classnames |
|
327 | - * @return boolean |
|
328 | - */ |
|
329 | - public function isSubclassOfOne($object, $classnames) |
|
330 | - { |
|
331 | - foreach ($classnames as $classname) { |
|
332 | - if (is_a($object, $classname)) { |
|
333 | - return true; |
|
334 | - } |
|
335 | - } |
|
336 | - return false; |
|
337 | - } |
|
338 | - |
|
339 | - |
|
340 | - |
|
341 | - /** |
|
342 | - * Returns the list of model field classes that that the API basically ignores |
|
343 | - * |
|
344 | - * @return array |
|
345 | - */ |
|
346 | - public function fieldsIgnored() |
|
347 | - { |
|
348 | - return apply_filters( |
|
349 | - 'FHEE__Controller_Model_Read_fields_ignored', |
|
350 | - array('EE_Foreign_Key_Field_Base', 'EE_Any_Foreign_Model_Name_Field') |
|
351 | - ); |
|
352 | - } |
|
353 | - |
|
354 | - |
|
355 | - |
|
356 | - /** |
|
357 | - * If this field one that should be ignored by the API? |
|
358 | - * |
|
359 | - * @param EE_Model_Field_Base |
|
360 | - * @return boolean |
|
361 | - */ |
|
362 | - public function fieldIsIgnored($field_obj) |
|
363 | - { |
|
364 | - return $this->isSubclassOfOne($field_obj, $this->fieldsIgnored()); |
|
365 | - } |
|
366 | - |
|
367 | - |
|
368 | - |
|
369 | - /** |
|
370 | - * Returns the list of model field classes that have a "raw" and non-raw formats. |
|
371 | - * Normally the "raw" versions are only accessible to those who can edit them. |
|
372 | - * |
|
373 | - * @return array an array of EE_Model_Field_Base child classnames |
|
374 | - */ |
|
375 | - public function fieldsThatHaveRenderedFormat() |
|
376 | - { |
|
377 | - return apply_filters( |
|
378 | - 'FHEE__Controller_Model_Read__fields_raw', |
|
379 | - array('EE_Post_Content_Field', 'EE_Full_HTML_Field') |
|
380 | - ); |
|
381 | - } |
|
382 | - |
|
383 | - |
|
384 | - |
|
385 | - /** |
|
386 | - * If this field one that has a raw format |
|
387 | - * |
|
388 | - * @param EE_Model_Field_Base |
|
389 | - * @return boolean |
|
390 | - */ |
|
391 | - public function fieldHasRenderedFormat($field_obj) |
|
392 | - { |
|
393 | - return $this->isSubclassOfOne($field_obj, $this->fieldsThatHaveRenderedFormat()); |
|
394 | - } |
|
395 | - |
|
396 | - |
|
397 | - |
|
398 | - /** |
|
399 | - * Returns the list of model field classes that have a "_pretty" and non-pretty versions. |
|
400 | - * The pretty version of the field is NOT query-able or editable, but requires no extra permissions |
|
401 | - * to view |
|
402 | - * |
|
403 | - * @return array an array of EE_Model_Field_Base child classnames |
|
404 | - */ |
|
405 | - public function fieldsThatHavePrettyFormat() |
|
406 | - { |
|
407 | - return apply_filters( |
|
408 | - 'FHEE__Controller_Model_Read__fields_pretty', |
|
409 | - array('EE_Enum_Integer_Field', 'EE_Enum_Text_Field', 'EE_Money_Field') |
|
410 | - ); |
|
411 | - } |
|
412 | - |
|
413 | - |
|
414 | - |
|
415 | - /** |
|
416 | - * If this field one that has a pretty equivalent |
|
417 | - * |
|
418 | - * @param EE_Model_Field_Base |
|
419 | - * @return boolean |
|
420 | - */ |
|
421 | - public function fieldHasPrettyFormat($field_obj) |
|
422 | - { |
|
423 | - return $this->isSubclassOfOne($field_obj, $this->fieldsThatHavePrettyFormat()); |
|
424 | - } |
|
425 | - |
|
426 | - |
|
427 | - |
|
428 | - /** |
|
429 | - * Returns an array describing what extra API resource properties have been added through the versions |
|
430 | - * |
|
431 | - * @return array @see $this->_extra_resource_properties_for_models |
|
432 | - */ |
|
433 | - public function resourceChanges() |
|
434 | - { |
|
435 | - return $this->resource_changes; |
|
436 | - } |
|
437 | - |
|
438 | - |
|
439 | - |
|
440 | - /** |
|
441 | - * Returns an array where keys are extra resource properties in this version of the API, |
|
442 | - * and values are key-value pairs describing the new properties. @see Model_Version::_resource_changes |
|
443 | - * |
|
444 | - * @param \EEM_Base $model |
|
445 | - * @return array |
|
446 | - */ |
|
447 | - public function extraResourcePropertiesForModel($model) |
|
448 | - { |
|
449 | - $extra_properties = array(); |
|
450 | - foreach ($this->resourceChangesBetweenRequestedVersionAndCurrent() as $version => $model_classnames) { |
|
451 | - foreach ($model_classnames as $model_classname => $properties_added_in_this_version) { |
|
452 | - if (is_subclass_of($model, $model_classname)) { |
|
453 | - $extra_properties = array_merge($extra_properties, $properties_added_in_this_version); |
|
454 | - } |
|
455 | - } |
|
456 | - } |
|
457 | - return $extra_properties; |
|
458 | - } |
|
459 | - |
|
460 | - |
|
461 | - |
|
462 | - /** |
|
463 | - * Gets all the related models for the specified model. It's good to use this |
|
464 | - * in case this model didn't exist for this version or something |
|
465 | - * |
|
466 | - * @param \EEM_Base $model |
|
467 | - * @return \EE_Model_Relation_Base[] |
|
468 | - */ |
|
469 | - public function relationSettings(\EEM_Base $model) |
|
470 | - { |
|
471 | - $relations = array(); |
|
472 | - foreach ($model->relation_settings() as $relation_name => $relation_obj) { |
|
473 | - if ($this->isModelNameInThisVersion($relation_name)) { |
|
474 | - $relations[$relation_name] = $relation_obj; |
|
475 | - } |
|
476 | - } |
|
477 | - //filter the results, but use the old filter name |
|
478 | - return apply_filters( |
|
479 | - 'FHEE__Read__create_entity_from_wpdb_result__related_models_to_include', |
|
480 | - $relations, |
|
481 | - $model |
|
482 | - ); |
|
483 | - } |
|
29 | + /** |
|
30 | + * Constant used in the $_model_changes array to indicate that a model |
|
31 | + * was completely new in this version |
|
32 | + */ |
|
33 | + const MODEL_ADDED = 'model_added_in_this_version'; |
|
34 | + |
|
35 | + /** |
|
36 | + * Top-level keys are versions (major and minor version numbers, eg "4.6") |
|
37 | + * next-level keys are model names (eg "Event") that underwent some change in that version |
|
38 | + * and the value is either Model_Version_Info::model_added (indicating the model is completely NEW in this version), |
|
39 | + * or it's an array where the values are model field names, |
|
40 | + * or API resource properties (ie, non-model fields that appear in REST API results) |
|
41 | + * If a version is missing then we don't know anything about what changes it introduced from the previous version |
|
42 | + * |
|
43 | + * @var array |
|
44 | + */ |
|
45 | + protected $model_changes = array(); |
|
46 | + |
|
47 | + /** |
|
48 | + * top-level keys are version numbers, |
|
49 | + * next-level keys are model CLASSNAMES (even parent classnames), |
|
50 | + * and next-level keys are extra resource properties to attach to those models' resources, |
|
51 | + * and next-level key-value pairs, where the keys are: |
|
52 | + * 'raw', 'type', 'nullable', 'table_alias', 'table_column', 'always_available' |
|
53 | + * |
|
54 | + * @var array |
|
55 | + */ |
|
56 | + protected $resource_changes = array(); |
|
57 | + |
|
58 | + /** |
|
59 | + * @var string indicating what version of the API was requested |
|
60 | + * (eg although core might be at version 4.8.11, they may have sent a request |
|
61 | + * for 4.6) |
|
62 | + */ |
|
63 | + protected $requested_version = null; |
|
64 | + |
|
65 | + /** |
|
66 | + * Keys are model names, values are their classnames. |
|
67 | + * We cache this so we only need to calculate this once per request |
|
68 | + * |
|
69 | + * @var array |
|
70 | + */ |
|
71 | + protected $cached_models_for_requested_version = null; |
|
72 | + |
|
73 | + /** |
|
74 | + * @var array |
|
75 | + */ |
|
76 | + protected $cached_model_changes_between_requested_version_and_current = null; |
|
77 | + |
|
78 | + /** |
|
79 | + * @var array |
|
80 | + */ |
|
81 | + protected $cached_resource_changes_between_requested_version_and_current = null; |
|
82 | + |
|
83 | + /** |
|
84 | + * 2d array where top-level keys are model names, 2nd-level keys are field names |
|
85 | + * and values are the actual field objects |
|
86 | + * |
|
87 | + * @var array |
|
88 | + */ |
|
89 | + protected $cached_fields_on_models = array(); |
|
90 | + |
|
91 | + |
|
92 | + |
|
93 | + /** |
|
94 | + * Model_Version_Info constructor. |
|
95 | + * |
|
96 | + * @param string $requested_version |
|
97 | + */ |
|
98 | + public function __construct($requested_version) |
|
99 | + { |
|
100 | + $this->requested_version = (string)$requested_version; |
|
101 | + $this->model_changes = array( |
|
102 | + '4.8.29' => array( |
|
103 | + //first version where the REST API is in EE core, so no need |
|
104 | + //to specify how its different from the previous |
|
105 | + ), |
|
106 | + ); |
|
107 | + //setup data for "extra" fields added onto resources which don't actually exist on models |
|
108 | + $this->resource_changes = apply_filters( |
|
109 | + 'FHEE__Model_Version_Info___construct__extra_resource_properties_for_models', |
|
110 | + array() |
|
111 | + ); |
|
112 | + $defaults = array( |
|
113 | + 'raw' => false, |
|
114 | + 'type' => 'N/A', |
|
115 | + 'nullable' => true, |
|
116 | + 'table_alias' => 'N/A', |
|
117 | + 'table_column' => 'N/A', |
|
118 | + 'always_available' => true, |
|
119 | + ); |
|
120 | + foreach ($this->resource_changes as $version => $model_classnames) { |
|
121 | + foreach ($model_classnames as $model_classname => $extra_fields) { |
|
122 | + foreach ($extra_fields as $fieldname => $field_data) { |
|
123 | + $this->resource_changes[$model_classname][$fieldname]['name'] = $fieldname; |
|
124 | + foreach ($defaults as $attribute => $default_value) { |
|
125 | + if (! isset($this->resource_changes[$model_classname][$fieldname][$attribute])) { |
|
126 | + $this->resource_changes[$model_classname][$fieldname][$attribute] = $default_value; |
|
127 | + } |
|
128 | + } |
|
129 | + } |
|
130 | + } |
|
131 | + } |
|
132 | + } |
|
133 | + |
|
134 | + |
|
135 | + |
|
136 | + /** |
|
137 | + * Returns a slice of Model_Version_Info::model_changes()'s array |
|
138 | + * indicating exactly what changes happened between the current core version, |
|
139 | + * and the version requested |
|
140 | + * |
|
141 | + * @return array |
|
142 | + */ |
|
143 | + public function modelChangesBetweenRequestedVersionAndCurrent() |
|
144 | + { |
|
145 | + if ($this->cached_model_changes_between_requested_version_and_current === null) { |
|
146 | + $model_changes = array(); |
|
147 | + foreach ($this->modelChanges() as $version => $models_changed_in_version) { |
|
148 | + if ($version <= EED_Core_Rest_Api::core_version() && $version > $this->requestedVersion()) { |
|
149 | + $model_changes[$version] = $models_changed_in_version; |
|
150 | + } |
|
151 | + } |
|
152 | + $this->cached_model_changes_between_requested_version_and_current = $model_changes; |
|
153 | + } |
|
154 | + return $this->cached_model_changes_between_requested_version_and_current; |
|
155 | + } |
|
156 | + |
|
157 | + |
|
158 | + |
|
159 | + /** |
|
160 | + * Returns a slice of Model_Version_Info::model_changes()'s array |
|
161 | + * indicating exactly what changes happened between the current core version, |
|
162 | + * and the version requested |
|
163 | + * |
|
164 | + * @return array |
|
165 | + */ |
|
166 | + public function resourceChangesBetweenRequestedVersionAndCurrent() |
|
167 | + { |
|
168 | + if ($this->cached_resource_changes_between_requested_version_and_current === null) { |
|
169 | + $resource_changes = array(); |
|
170 | + foreach ($this->resourceChanges() as $version => $model_classnames) { |
|
171 | + if ($version <= EED_Core_Rest_Api::core_version() && $version > $this->requestedVersion()) { |
|
172 | + $resource_changes[$version] = $model_classnames; |
|
173 | + } |
|
174 | + } |
|
175 | + $this->cached_resource_changes_between_requested_version_and_current = $resource_changes; |
|
176 | + } |
|
177 | + return $this->cached_resource_changes_between_requested_version_and_current; |
|
178 | + } |
|
179 | + |
|
180 | + |
|
181 | + |
|
182 | + /** |
|
183 | + * If a request was sent to 'wp-json/ee/v4.7/events' this would be '4.7' |
|
184 | + * |
|
185 | + * @return string like '4.6' |
|
186 | + */ |
|
187 | + public function requestedVersion() |
|
188 | + { |
|
189 | + return $this->requested_version; |
|
190 | + } |
|
191 | + |
|
192 | + |
|
193 | + |
|
194 | + /** |
|
195 | + * Returns an array describing how the models have changed in each version of core |
|
196 | + * that supports the API (starting at 4.6) |
|
197 | + * Top-level keys are versions (major and minor version numbers, eg "4.6") |
|
198 | + * next-level keys are model names (eg "Event") that underwent some change in that version |
|
199 | + * and the value is either NULL (indicating the model is completely NEW in this version), |
|
200 | + * or it's an array where fields are value names. |
|
201 | + * If a version is missing then we don't know anything about what changes it introduced from the previous version |
|
202 | + * |
|
203 | + * @return array |
|
204 | + */ |
|
205 | + public function modelChanges() |
|
206 | + { |
|
207 | + return $this->model_changes; |
|
208 | + } |
|
209 | + |
|
210 | + |
|
211 | + |
|
212 | + /** |
|
213 | + * Takes into account the requested version, and the current version, and |
|
214 | + * what changed between the two, and tries to return. |
|
215 | + * Analogous to EE_Registry::instance()->non_abstract_db_models |
|
216 | + * |
|
217 | + * @return array keys are model names, values are their classname |
|
218 | + */ |
|
219 | + public function modelsForRequestedVersion() |
|
220 | + { |
|
221 | + if ($this->cached_models_for_requested_version === null) { |
|
222 | + $all_models_in_current_version = EE_Registry::instance()->non_abstract_db_models; |
|
223 | + foreach ($this->modelChangesBetweenRequestedVersionAndCurrent() as $version => $models_changed) { |
|
224 | + foreach ($models_changed as $model_name => $new_indicator_or_fields_added) { |
|
225 | + if ($new_indicator_or_fields_added === ModelVersionInfo::MODEL_ADDED) { |
|
226 | + unset($all_models_in_current_version[$model_name]); |
|
227 | + } |
|
228 | + } |
|
229 | + } |
|
230 | + $this->cached_models_for_requested_version = apply_filters( |
|
231 | + 'FHEE__EventEspresso_core_libraries_rest_api__models_for_requested_version', |
|
232 | + $all_models_in_current_version, |
|
233 | + $this |
|
234 | + ); |
|
235 | + } |
|
236 | + return $this->cached_models_for_requested_version; |
|
237 | + } |
|
238 | + |
|
239 | + |
|
240 | + |
|
241 | + /** |
|
242 | + * Determines if this is a valid model name in the requested version. |
|
243 | + * Similar to EE_Registry::instance()->is_model_name(), but takes the requested |
|
244 | + * version's models into account |
|
245 | + * |
|
246 | + * @param string $model_name eg 'Event' |
|
247 | + * @return boolean |
|
248 | + */ |
|
249 | + public function isModelNameInThisVersion($model_name) |
|
250 | + { |
|
251 | + $model_names = $this->modelsForRequestedVersion(); |
|
252 | + if (isset($model_names[$model_name])) { |
|
253 | + return true; |
|
254 | + } else { |
|
255 | + return false; |
|
256 | + } |
|
257 | + } |
|
258 | + |
|
259 | + |
|
260 | + |
|
261 | + /** |
|
262 | + * Wrapper for EE_Registry::instance()->load_model(), but takes the requested |
|
263 | + * version's models into account |
|
264 | + * |
|
265 | + * @param string $model_name |
|
266 | + * @return \EEM_Base |
|
267 | + * @throws \EE_Error |
|
268 | + */ |
|
269 | + public function loadModel($model_name) |
|
270 | + { |
|
271 | + if ($this->isModelNameInThisVersion($model_name)) { |
|
272 | + return EE_Registry::instance()->load_model($model_name); |
|
273 | + } else { |
|
274 | + throw new \EE_Error( |
|
275 | + sprintf( |
|
276 | + __( |
|
277 | + 'Cannot load model "%1$s" because it does not exist in version %2$s of Event Espresso', |
|
278 | + 'event_espresso' |
|
279 | + ), |
|
280 | + $model_name, |
|
281 | + $this->requestedVersion() |
|
282 | + ) |
|
283 | + ); |
|
284 | + } |
|
285 | + } |
|
286 | + |
|
287 | + |
|
288 | + |
|
289 | + /** |
|
290 | + * Gets all the fields that should exist on this model right now |
|
291 | + * |
|
292 | + * @param \EEM_Base $model |
|
293 | + * @return array|\EE_Model_Field_Base[] |
|
294 | + */ |
|
295 | + public function fieldsOnModelInThisVersion($model) |
|
296 | + { |
|
297 | + if (! isset($this->cached_fields_on_models[$model->get_this_model_name()])) { |
|
298 | + //get all model changes between the requested version and current core version |
|
299 | + $changes = $this->modelChangesBetweenRequestedVersionAndCurrent(); |
|
300 | + //fetch all fields currently on this model |
|
301 | + $current_fields = $model->field_settings(); |
|
302 | + //remove all fields that have been added since |
|
303 | + foreach ($changes as $version => $changes_in_version) { |
|
304 | + if (isset($changes_in_version[$model->get_this_model_name()]) |
|
305 | + && $changes_in_version[$model->get_this_model_name()] !== ModelVersionInfo::MODEL_ADDED |
|
306 | + ) { |
|
307 | + $current_fields = array_diff_key( |
|
308 | + $current_fields, |
|
309 | + array_flip($changes_in_version[$model->get_this_model_name()]) |
|
310 | + ); |
|
311 | + } |
|
312 | + } |
|
313 | + $this->cached_fields_on_models = $current_fields; |
|
314 | + } |
|
315 | + return $this->cached_fields_on_models; |
|
316 | + } |
|
317 | + |
|
318 | + |
|
319 | + |
|
320 | + /** |
|
321 | + * Determines if $object is of one of the classes of $classes. Similar to |
|
322 | + * in_array(), except this checks if $object is a subclass of the classnames provided |
|
323 | + * in $classnames |
|
324 | + * |
|
325 | + * @param object $object |
|
326 | + * @param array $classnames |
|
327 | + * @return boolean |
|
328 | + */ |
|
329 | + public function isSubclassOfOne($object, $classnames) |
|
330 | + { |
|
331 | + foreach ($classnames as $classname) { |
|
332 | + if (is_a($object, $classname)) { |
|
333 | + return true; |
|
334 | + } |
|
335 | + } |
|
336 | + return false; |
|
337 | + } |
|
338 | + |
|
339 | + |
|
340 | + |
|
341 | + /** |
|
342 | + * Returns the list of model field classes that that the API basically ignores |
|
343 | + * |
|
344 | + * @return array |
|
345 | + */ |
|
346 | + public function fieldsIgnored() |
|
347 | + { |
|
348 | + return apply_filters( |
|
349 | + 'FHEE__Controller_Model_Read_fields_ignored', |
|
350 | + array('EE_Foreign_Key_Field_Base', 'EE_Any_Foreign_Model_Name_Field') |
|
351 | + ); |
|
352 | + } |
|
353 | + |
|
354 | + |
|
355 | + |
|
356 | + /** |
|
357 | + * If this field one that should be ignored by the API? |
|
358 | + * |
|
359 | + * @param EE_Model_Field_Base |
|
360 | + * @return boolean |
|
361 | + */ |
|
362 | + public function fieldIsIgnored($field_obj) |
|
363 | + { |
|
364 | + return $this->isSubclassOfOne($field_obj, $this->fieldsIgnored()); |
|
365 | + } |
|
366 | + |
|
367 | + |
|
368 | + |
|
369 | + /** |
|
370 | + * Returns the list of model field classes that have a "raw" and non-raw formats. |
|
371 | + * Normally the "raw" versions are only accessible to those who can edit them. |
|
372 | + * |
|
373 | + * @return array an array of EE_Model_Field_Base child classnames |
|
374 | + */ |
|
375 | + public function fieldsThatHaveRenderedFormat() |
|
376 | + { |
|
377 | + return apply_filters( |
|
378 | + 'FHEE__Controller_Model_Read__fields_raw', |
|
379 | + array('EE_Post_Content_Field', 'EE_Full_HTML_Field') |
|
380 | + ); |
|
381 | + } |
|
382 | + |
|
383 | + |
|
384 | + |
|
385 | + /** |
|
386 | + * If this field one that has a raw format |
|
387 | + * |
|
388 | + * @param EE_Model_Field_Base |
|
389 | + * @return boolean |
|
390 | + */ |
|
391 | + public function fieldHasRenderedFormat($field_obj) |
|
392 | + { |
|
393 | + return $this->isSubclassOfOne($field_obj, $this->fieldsThatHaveRenderedFormat()); |
|
394 | + } |
|
395 | + |
|
396 | + |
|
397 | + |
|
398 | + /** |
|
399 | + * Returns the list of model field classes that have a "_pretty" and non-pretty versions. |
|
400 | + * The pretty version of the field is NOT query-able or editable, but requires no extra permissions |
|
401 | + * to view |
|
402 | + * |
|
403 | + * @return array an array of EE_Model_Field_Base child classnames |
|
404 | + */ |
|
405 | + public function fieldsThatHavePrettyFormat() |
|
406 | + { |
|
407 | + return apply_filters( |
|
408 | + 'FHEE__Controller_Model_Read__fields_pretty', |
|
409 | + array('EE_Enum_Integer_Field', 'EE_Enum_Text_Field', 'EE_Money_Field') |
|
410 | + ); |
|
411 | + } |
|
412 | + |
|
413 | + |
|
414 | + |
|
415 | + /** |
|
416 | + * If this field one that has a pretty equivalent |
|
417 | + * |
|
418 | + * @param EE_Model_Field_Base |
|
419 | + * @return boolean |
|
420 | + */ |
|
421 | + public function fieldHasPrettyFormat($field_obj) |
|
422 | + { |
|
423 | + return $this->isSubclassOfOne($field_obj, $this->fieldsThatHavePrettyFormat()); |
|
424 | + } |
|
425 | + |
|
426 | + |
|
427 | + |
|
428 | + /** |
|
429 | + * Returns an array describing what extra API resource properties have been added through the versions |
|
430 | + * |
|
431 | + * @return array @see $this->_extra_resource_properties_for_models |
|
432 | + */ |
|
433 | + public function resourceChanges() |
|
434 | + { |
|
435 | + return $this->resource_changes; |
|
436 | + } |
|
437 | + |
|
438 | + |
|
439 | + |
|
440 | + /** |
|
441 | + * Returns an array where keys are extra resource properties in this version of the API, |
|
442 | + * and values are key-value pairs describing the new properties. @see Model_Version::_resource_changes |
|
443 | + * |
|
444 | + * @param \EEM_Base $model |
|
445 | + * @return array |
|
446 | + */ |
|
447 | + public function extraResourcePropertiesForModel($model) |
|
448 | + { |
|
449 | + $extra_properties = array(); |
|
450 | + foreach ($this->resourceChangesBetweenRequestedVersionAndCurrent() as $version => $model_classnames) { |
|
451 | + foreach ($model_classnames as $model_classname => $properties_added_in_this_version) { |
|
452 | + if (is_subclass_of($model, $model_classname)) { |
|
453 | + $extra_properties = array_merge($extra_properties, $properties_added_in_this_version); |
|
454 | + } |
|
455 | + } |
|
456 | + } |
|
457 | + return $extra_properties; |
|
458 | + } |
|
459 | + |
|
460 | + |
|
461 | + |
|
462 | + /** |
|
463 | + * Gets all the related models for the specified model. It's good to use this |
|
464 | + * in case this model didn't exist for this version or something |
|
465 | + * |
|
466 | + * @param \EEM_Base $model |
|
467 | + * @return \EE_Model_Relation_Base[] |
|
468 | + */ |
|
469 | + public function relationSettings(\EEM_Base $model) |
|
470 | + { |
|
471 | + $relations = array(); |
|
472 | + foreach ($model->relation_settings() as $relation_name => $relation_obj) { |
|
473 | + if ($this->isModelNameInThisVersion($relation_name)) { |
|
474 | + $relations[$relation_name] = $relation_obj; |
|
475 | + } |
|
476 | + } |
|
477 | + //filter the results, but use the old filter name |
|
478 | + return apply_filters( |
|
479 | + 'FHEE__Read__create_entity_from_wpdb_result__related_models_to_include', |
|
480 | + $relations, |
|
481 | + $model |
|
482 | + ); |
|
483 | + } |
|
484 | 484 | } |
485 | 485 | |
486 | 486 | // End of file Model_Version_Info.php |
@@ -13,7 +13,7 @@ discard block |
||
13 | 13 | use EventEspresso\core\services\commands\CommandRequiresCapCheckInterface; |
14 | 14 | |
15 | 15 | if ( ! defined('EVENT_ESPRESSO_VERSION')) { |
16 | - exit('No direct script access allowed'); |
|
16 | + exit('No direct script access allowed'); |
|
17 | 17 | } |
18 | 18 | |
19 | 19 | |
@@ -29,162 +29,162 @@ discard block |
||
29 | 29 | class CreateRegistrationCommand extends Command implements CommandRequiresCapCheckInterface |
30 | 30 | { |
31 | 31 | |
32 | - /** |
|
33 | - * @var EE_Transaction $transaction |
|
34 | - */ |
|
35 | - private $transaction; |
|
36 | - |
|
37 | - /** |
|
38 | - * @var EE_Ticket $ticket |
|
39 | - */ |
|
40 | - private $ticket; |
|
41 | - |
|
42 | - /** |
|
43 | - * @var EE_Line_Item $ticket_line_item |
|
44 | - */ |
|
45 | - private $ticket_line_item; |
|
46 | - |
|
47 | - /** |
|
48 | - * @var int $reg_count |
|
49 | - */ |
|
50 | - private $reg_count; |
|
51 | - |
|
52 | - /** |
|
53 | - * @var int $reg_group_size |
|
54 | - */ |
|
55 | - private $reg_group_size; |
|
56 | - |
|
57 | - /** |
|
58 | - * @var string $reg_status |
|
59 | - */ |
|
60 | - private $reg_status; |
|
61 | - |
|
62 | - /** |
|
63 | - * @var EE_Registration $registration |
|
64 | - */ |
|
65 | - protected $registration; |
|
66 | - |
|
67 | - |
|
68 | - |
|
69 | - /** |
|
70 | - * CreateRegistrationCommand constructor. |
|
71 | - * |
|
72 | - * @param EE_Transaction $transaction |
|
73 | - * @param EE_Line_Item $ticket_line_item |
|
74 | - * @param int $reg_count |
|
75 | - * @param int $reg_group_size |
|
76 | - * @param string $reg_status |
|
77 | - * @throws InvalidEntityException |
|
78 | - */ |
|
79 | - public function __construct( |
|
80 | - EE_Transaction $transaction, |
|
81 | - EE_Line_Item $ticket_line_item, |
|
82 | - $reg_count = 1, |
|
83 | - $reg_group_size = 0, |
|
84 | - $reg_status = EEM_Registration::status_id_incomplete |
|
85 | - ) { |
|
86 | - // grab the related ticket object for this line_item |
|
87 | - $this->ticket = $ticket_line_item->ticket(); |
|
88 | - if ( ! $this->ticket instanceof EE_Ticket) { |
|
89 | - throw new InvalidEntityException( |
|
90 | - is_object($this->ticket) ? get_class($this->ticket) : gettype($this->ticket), |
|
91 | - 'EE_Ticket', |
|
92 | - sprintf( |
|
93 | - __('Line item %s did not contain a valid ticket', 'event_espresso'), |
|
94 | - $ticket_line_item->ID() |
|
95 | - ) |
|
96 | - ); |
|
97 | - } |
|
98 | - $this->transaction = $transaction; |
|
99 | - $this->ticket_line_item = $ticket_line_item; |
|
100 | - $this->reg_count = absint($reg_count); |
|
101 | - $this->reg_group_size = absint($reg_group_size); |
|
102 | - $this->reg_status = $reg_status; |
|
103 | - } |
|
104 | - |
|
105 | - |
|
106 | - |
|
107 | - /** |
|
108 | - * @return \EventEspresso\core\domain\services\capabilities\CapCheckInterface |
|
109 | - * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
110 | - */ |
|
111 | - public function getCapCheck() |
|
112 | - { |
|
113 | - if ( ! $this->cap_check instanceof CapCheckInterface) { |
|
114 | - return new CapCheck('ee_edit_registrations', 'create_new_registration'); |
|
115 | - } |
|
116 | - return $this->cap_check; |
|
117 | - } |
|
118 | - |
|
119 | - |
|
120 | - |
|
121 | - /** |
|
122 | - * @return EE_Transaction |
|
123 | - */ |
|
124 | - public function transaction() |
|
125 | - { |
|
126 | - return $this->transaction; |
|
127 | - } |
|
128 | - |
|
129 | - |
|
130 | - |
|
131 | - /** |
|
132 | - * @return EE_Ticket |
|
133 | - */ |
|
134 | - public function ticket() |
|
135 | - { |
|
136 | - return $this->ticket; |
|
137 | - } |
|
138 | - |
|
139 | - |
|
140 | - |
|
141 | - /** |
|
142 | - * @return EE_Line_Item |
|
143 | - */ |
|
144 | - public function ticketLineItem() |
|
145 | - { |
|
146 | - return $this->ticket_line_item; |
|
147 | - } |
|
148 | - |
|
149 | - |
|
150 | - |
|
151 | - /** |
|
152 | - * @return int |
|
153 | - */ |
|
154 | - public function regCount() |
|
155 | - { |
|
156 | - return $this->reg_count; |
|
157 | - } |
|
158 | - |
|
159 | - |
|
160 | - |
|
161 | - /** |
|
162 | - * @return int |
|
163 | - */ |
|
164 | - public function regGroupSize() |
|
165 | - { |
|
166 | - return $this->reg_group_size; |
|
167 | - } |
|
168 | - |
|
169 | - |
|
170 | - |
|
171 | - /** |
|
172 | - * @return string |
|
173 | - */ |
|
174 | - public function regStatus() |
|
175 | - { |
|
176 | - return $this->reg_status; |
|
177 | - } |
|
178 | - |
|
179 | - |
|
180 | - |
|
181 | - /** |
|
182 | - * @return EE_Registration |
|
183 | - */ |
|
184 | - public function registration() |
|
185 | - { |
|
186 | - return $this->registration; |
|
187 | - } |
|
32 | + /** |
|
33 | + * @var EE_Transaction $transaction |
|
34 | + */ |
|
35 | + private $transaction; |
|
36 | + |
|
37 | + /** |
|
38 | + * @var EE_Ticket $ticket |
|
39 | + */ |
|
40 | + private $ticket; |
|
41 | + |
|
42 | + /** |
|
43 | + * @var EE_Line_Item $ticket_line_item |
|
44 | + */ |
|
45 | + private $ticket_line_item; |
|
46 | + |
|
47 | + /** |
|
48 | + * @var int $reg_count |
|
49 | + */ |
|
50 | + private $reg_count; |
|
51 | + |
|
52 | + /** |
|
53 | + * @var int $reg_group_size |
|
54 | + */ |
|
55 | + private $reg_group_size; |
|
56 | + |
|
57 | + /** |
|
58 | + * @var string $reg_status |
|
59 | + */ |
|
60 | + private $reg_status; |
|
61 | + |
|
62 | + /** |
|
63 | + * @var EE_Registration $registration |
|
64 | + */ |
|
65 | + protected $registration; |
|
66 | + |
|
67 | + |
|
68 | + |
|
69 | + /** |
|
70 | + * CreateRegistrationCommand constructor. |
|
71 | + * |
|
72 | + * @param EE_Transaction $transaction |
|
73 | + * @param EE_Line_Item $ticket_line_item |
|
74 | + * @param int $reg_count |
|
75 | + * @param int $reg_group_size |
|
76 | + * @param string $reg_status |
|
77 | + * @throws InvalidEntityException |
|
78 | + */ |
|
79 | + public function __construct( |
|
80 | + EE_Transaction $transaction, |
|
81 | + EE_Line_Item $ticket_line_item, |
|
82 | + $reg_count = 1, |
|
83 | + $reg_group_size = 0, |
|
84 | + $reg_status = EEM_Registration::status_id_incomplete |
|
85 | + ) { |
|
86 | + // grab the related ticket object for this line_item |
|
87 | + $this->ticket = $ticket_line_item->ticket(); |
|
88 | + if ( ! $this->ticket instanceof EE_Ticket) { |
|
89 | + throw new InvalidEntityException( |
|
90 | + is_object($this->ticket) ? get_class($this->ticket) : gettype($this->ticket), |
|
91 | + 'EE_Ticket', |
|
92 | + sprintf( |
|
93 | + __('Line item %s did not contain a valid ticket', 'event_espresso'), |
|
94 | + $ticket_line_item->ID() |
|
95 | + ) |
|
96 | + ); |
|
97 | + } |
|
98 | + $this->transaction = $transaction; |
|
99 | + $this->ticket_line_item = $ticket_line_item; |
|
100 | + $this->reg_count = absint($reg_count); |
|
101 | + $this->reg_group_size = absint($reg_group_size); |
|
102 | + $this->reg_status = $reg_status; |
|
103 | + } |
|
104 | + |
|
105 | + |
|
106 | + |
|
107 | + /** |
|
108 | + * @return \EventEspresso\core\domain\services\capabilities\CapCheckInterface |
|
109 | + * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
110 | + */ |
|
111 | + public function getCapCheck() |
|
112 | + { |
|
113 | + if ( ! $this->cap_check instanceof CapCheckInterface) { |
|
114 | + return new CapCheck('ee_edit_registrations', 'create_new_registration'); |
|
115 | + } |
|
116 | + return $this->cap_check; |
|
117 | + } |
|
118 | + |
|
119 | + |
|
120 | + |
|
121 | + /** |
|
122 | + * @return EE_Transaction |
|
123 | + */ |
|
124 | + public function transaction() |
|
125 | + { |
|
126 | + return $this->transaction; |
|
127 | + } |
|
128 | + |
|
129 | + |
|
130 | + |
|
131 | + /** |
|
132 | + * @return EE_Ticket |
|
133 | + */ |
|
134 | + public function ticket() |
|
135 | + { |
|
136 | + return $this->ticket; |
|
137 | + } |
|
138 | + |
|
139 | + |
|
140 | + |
|
141 | + /** |
|
142 | + * @return EE_Line_Item |
|
143 | + */ |
|
144 | + public function ticketLineItem() |
|
145 | + { |
|
146 | + return $this->ticket_line_item; |
|
147 | + } |
|
148 | + |
|
149 | + |
|
150 | + |
|
151 | + /** |
|
152 | + * @return int |
|
153 | + */ |
|
154 | + public function regCount() |
|
155 | + { |
|
156 | + return $this->reg_count; |
|
157 | + } |
|
158 | + |
|
159 | + |
|
160 | + |
|
161 | + /** |
|
162 | + * @return int |
|
163 | + */ |
|
164 | + public function regGroupSize() |
|
165 | + { |
|
166 | + return $this->reg_group_size; |
|
167 | + } |
|
168 | + |
|
169 | + |
|
170 | + |
|
171 | + /** |
|
172 | + * @return string |
|
173 | + */ |
|
174 | + public function regStatus() |
|
175 | + { |
|
176 | + return $this->reg_status; |
|
177 | + } |
|
178 | + |
|
179 | + |
|
180 | + |
|
181 | + /** |
|
182 | + * @return EE_Registration |
|
183 | + */ |
|
184 | + public function registration() |
|
185 | + { |
|
186 | + return $this->registration; |
|
187 | + } |
|
188 | 188 | |
189 | 189 | |
190 | 190 |
@@ -7,7 +7,7 @@ discard block |
||
7 | 7 | use InvalidArgumentException; |
8 | 8 | |
9 | 9 | if ( ! defined('EVENT_ESPRESSO_VERSION')) { |
10 | - exit('No direct script access allowed'); |
|
10 | + exit('No direct script access allowed'); |
|
11 | 11 | } |
12 | 12 | |
13 | 13 | |
@@ -23,103 +23,103 @@ discard block |
||
23 | 23 | class RegUrlLink |
24 | 24 | { |
25 | 25 | |
26 | - /* |
|
26 | + /* |
|
27 | 27 | * @var string $reg_url_link |
28 | 28 | */ |
29 | - private $reg_url_link; |
|
30 | - |
|
31 | - |
|
32 | - |
|
33 | - /** |
|
34 | - * @param string $reg_url_link |
|
35 | - * @return RegUrlLink |
|
36 | - * @throws InvalidArgumentException |
|
37 | - */ |
|
38 | - public static function fromRegUrlLinkString($reg_url_link) |
|
39 | - { |
|
40 | - if (empty($reg_url_link) || ! is_string($reg_url_link)) { |
|
41 | - throw new InvalidArgumentException( |
|
42 | - __( |
|
43 | - 'You must supply a valid non-empty string to generate a reg_url_link.', |
|
44 | - 'event_espresso' |
|
45 | - ) |
|
46 | - ); |
|
47 | - } |
|
48 | - return new RegUrlLink(1, '', $reg_url_link); |
|
49 | - } |
|
50 | - |
|
51 | - |
|
52 | - |
|
53 | - /** |
|
54 | - * @param EE_Registration $registration |
|
55 | - * @return RegUrlLink |
|
56 | - * @throws EntityNotFoundException |
|
57 | - * @throws EE_Error |
|
58 | - * @throws InvalidArgumentException |
|
59 | - */ |
|
60 | - public static function fromRegistration(EE_Registration $registration) |
|
61 | - { |
|
62 | - return new RegUrlLink( |
|
63 | - $registration->count(), |
|
64 | - $registration->ticket_line_item() |
|
65 | - ); |
|
66 | - } |
|
67 | - |
|
68 | - |
|
69 | - |
|
70 | - /** |
|
71 | - * CreateRegUrlLinkCommand constructor. |
|
72 | - * |
|
73 | - * @param int $reg_count |
|
74 | - * @param mixed $base_code |
|
75 | - * @param string $reg_url_link |
|
76 | - * @throws InvalidArgumentException |
|
77 | - */ |
|
78 | - public function __construct( |
|
79 | - $reg_count = 1, |
|
80 | - $base_code = '', |
|
81 | - $reg_url_link = '' |
|
82 | - ) { |
|
83 | - if ( ! empty($reg_url_link) && is_string($reg_url_link)) { |
|
84 | - $this->reg_url_link = apply_filters( |
|
85 | - 'FHEE__\EventEspresso\core\domain\entities\RegUrlLink__construct__reg_url_link', |
|
86 | - $reg_url_link, |
|
87 | - $reg_count, |
|
88 | - $base_code, |
|
89 | - $reg_url_link |
|
90 | - ); |
|
91 | - return; |
|
92 | - } |
|
93 | - $reg_count = max(1, absint($reg_count)); |
|
94 | - $base_code = $base_code instanceof \EE_Line_Item ? $base_code->code() : $base_code; |
|
95 | - if (empty($base_code) || ! is_string($base_code)) { |
|
96 | - throw new InvalidArgumentException( |
|
97 | - __( |
|
98 | - 'You must supply a valid EE_Line_Item or a non-empty string to generate a reg_url_link.', |
|
99 | - 'event_espresso' |
|
100 | - ) |
|
101 | - ); |
|
102 | - } |
|
103 | - $this->reg_url_link = (string) apply_filters( |
|
104 | - 'FHEE__\EventEspresso\core\domain\entities\RegUrlLink__construct__reg_url_link', |
|
105 | - $reg_count . '-' . md5($base_code . microtime()), |
|
106 | - $reg_count, |
|
107 | - $base_code, |
|
108 | - $reg_url_link |
|
109 | - ); |
|
110 | - } |
|
111 | - |
|
112 | - |
|
113 | - |
|
114 | - /** |
|
115 | - * Return the object as a string |
|
116 | - * |
|
117 | - * @return string |
|
118 | - */ |
|
119 | - public function __toString() |
|
120 | - { |
|
121 | - return $this->reg_url_link; |
|
122 | - } |
|
29 | + private $reg_url_link; |
|
30 | + |
|
31 | + |
|
32 | + |
|
33 | + /** |
|
34 | + * @param string $reg_url_link |
|
35 | + * @return RegUrlLink |
|
36 | + * @throws InvalidArgumentException |
|
37 | + */ |
|
38 | + public static function fromRegUrlLinkString($reg_url_link) |
|
39 | + { |
|
40 | + if (empty($reg_url_link) || ! is_string($reg_url_link)) { |
|
41 | + throw new InvalidArgumentException( |
|
42 | + __( |
|
43 | + 'You must supply a valid non-empty string to generate a reg_url_link.', |
|
44 | + 'event_espresso' |
|
45 | + ) |
|
46 | + ); |
|
47 | + } |
|
48 | + return new RegUrlLink(1, '', $reg_url_link); |
|
49 | + } |
|
50 | + |
|
51 | + |
|
52 | + |
|
53 | + /** |
|
54 | + * @param EE_Registration $registration |
|
55 | + * @return RegUrlLink |
|
56 | + * @throws EntityNotFoundException |
|
57 | + * @throws EE_Error |
|
58 | + * @throws InvalidArgumentException |
|
59 | + */ |
|
60 | + public static function fromRegistration(EE_Registration $registration) |
|
61 | + { |
|
62 | + return new RegUrlLink( |
|
63 | + $registration->count(), |
|
64 | + $registration->ticket_line_item() |
|
65 | + ); |
|
66 | + } |
|
67 | + |
|
68 | + |
|
69 | + |
|
70 | + /** |
|
71 | + * CreateRegUrlLinkCommand constructor. |
|
72 | + * |
|
73 | + * @param int $reg_count |
|
74 | + * @param mixed $base_code |
|
75 | + * @param string $reg_url_link |
|
76 | + * @throws InvalidArgumentException |
|
77 | + */ |
|
78 | + public function __construct( |
|
79 | + $reg_count = 1, |
|
80 | + $base_code = '', |
|
81 | + $reg_url_link = '' |
|
82 | + ) { |
|
83 | + if ( ! empty($reg_url_link) && is_string($reg_url_link)) { |
|
84 | + $this->reg_url_link = apply_filters( |
|
85 | + 'FHEE__\EventEspresso\core\domain\entities\RegUrlLink__construct__reg_url_link', |
|
86 | + $reg_url_link, |
|
87 | + $reg_count, |
|
88 | + $base_code, |
|
89 | + $reg_url_link |
|
90 | + ); |
|
91 | + return; |
|
92 | + } |
|
93 | + $reg_count = max(1, absint($reg_count)); |
|
94 | + $base_code = $base_code instanceof \EE_Line_Item ? $base_code->code() : $base_code; |
|
95 | + if (empty($base_code) || ! is_string($base_code)) { |
|
96 | + throw new InvalidArgumentException( |
|
97 | + __( |
|
98 | + 'You must supply a valid EE_Line_Item or a non-empty string to generate a reg_url_link.', |
|
99 | + 'event_espresso' |
|
100 | + ) |
|
101 | + ); |
|
102 | + } |
|
103 | + $this->reg_url_link = (string) apply_filters( |
|
104 | + 'FHEE__\EventEspresso\core\domain\entities\RegUrlLink__construct__reg_url_link', |
|
105 | + $reg_count . '-' . md5($base_code . microtime()), |
|
106 | + $reg_count, |
|
107 | + $base_code, |
|
108 | + $reg_url_link |
|
109 | + ); |
|
110 | + } |
|
111 | + |
|
112 | + |
|
113 | + |
|
114 | + /** |
|
115 | + * Return the object as a string |
|
116 | + * |
|
117 | + * @return string |
|
118 | + */ |
|
119 | + public function __toString() |
|
120 | + { |
|
121 | + return $this->reg_url_link; |
|
122 | + } |
|
123 | 123 | } |
124 | 124 | // End of file RegUrlLink.php |
125 | 125 | // Location: /RegUrlLink.php |
126 | 126 | \ No newline at end of file |
@@ -102,7 +102,7 @@ |
||
102 | 102 | } |
103 | 103 | $this->reg_url_link = (string) apply_filters( |
104 | 104 | 'FHEE__\EventEspresso\core\domain\entities\RegUrlLink__construct__reg_url_link', |
105 | - $reg_count . '-' . md5($base_code . microtime()), |
|
105 | + $reg_count.'-'.md5($base_code.microtime()), |
|
106 | 106 | $reg_count, |
107 | 107 | $base_code, |
108 | 108 | $reg_url_link |