| @@ -20,338 +20,338 @@ | ||
| 20 | 20 | */ | 
| 21 | 21 | class Base | 
| 22 | 22 |  { | 
| 23 | - /** | |
| 24 | - * @deprecated use all-caps version | |
| 25 | - */ | |
| 26 | - // @codingStandardsIgnoreStart | |
| 27 | - const header_prefix_for_ee = 'X-EE-'; | |
| 28 | - // @codingStandardsIgnoreEnd | |
| 29 | - | |
| 30 | - const HEADER_PREFIX_FOR_EE = 'X-EE-'; | |
| 31 | - | |
| 32 | - /** | |
| 33 | - * @deprecated use all-caps version instead | |
| 34 | - */ | |
| 35 | - // @codingStandardsIgnoreStart | |
| 36 | - const header_prefix_for_wp = 'X-WP-'; | |
| 37 | - // @codingStandardsIgnoreEnd | |
| 38 | - | |
| 39 | - const HEADER_PREFIX_FOR_WP = 'X-WP-'; | |
| 40 | - | |
| 41 | - /** | |
| 42 | - * Contains debug info we'll send back in the response headers | |
| 43 | - * | |
| 44 | - * @var array | |
| 45 | - */ | |
| 46 | - protected $debug_info = array(); | |
| 47 | - | |
| 48 | - /** | |
| 49 | - * Indicates whether or not the API is in debug mode | |
| 50 | - * | |
| 51 | - * @var boolean | |
| 52 | - */ | |
| 53 | - protected $debug_mode = false; | |
| 54 | - | |
| 55 | - /** | |
| 56 | - * Indicates the version that was requested | |
| 57 | - * | |
| 58 | - * @var string | |
| 59 | - */ | |
| 60 | - protected $requested_version; | |
| 61 | - | |
| 62 | - /** | |
| 63 | - * flat array of headers to send in the response | |
| 64 | - * | |
| 65 | - * @var array | |
| 66 | - */ | |
| 67 | - protected $response_headers = array(); | |
| 68 | - | |
| 69 | - | |
| 70 | - public function __construct() | |
| 71 | -    { | |
| 72 | - $this->debug_mode = EED_Core_Rest_Api::debugMode(); | |
| 73 | - // we are handling a REST request. Don't show a fancy HTML error message is any error comes up | |
| 74 | -        add_filter('FHEE__EE_Error__get_error__show_normal_exceptions', '__return_true'); | |
| 75 | - } | |
| 76 | - | |
| 77 | - | |
| 78 | - /** | |
| 79 | - * Sets the version the user requested | |
| 80 | - * | |
| 81 | - * @param string $version eg '4.8' | |
| 82 | - */ | |
| 83 | - public function setRequestedVersion($version) | |
| 84 | -    { | |
| 85 | - $this->requested_version = $version; | |
| 86 | - } | |
| 87 | - | |
| 88 | - | |
| 89 | - /** | |
| 90 | - * Sets some debug info that we'll send back in headers | |
| 91 | - * | |
| 92 | - * @param string $key | |
| 93 | - * @param string|array $info | |
| 94 | - */ | |
| 95 | - protected function setDebugInfo($key, $info) | |
| 96 | -    { | |
| 97 | - $this->debug_info[ $key ] = $info; | |
| 98 | - } | |
| 99 | - | |
| 100 | - | |
| 101 | - /** | |
| 102 | - * Sets headers for the response | |
| 103 | - * | |
| 104 | - * @param string $header_key , excluding the "X-EE-" part | |
| 105 | - * @param array|string $value if an array, multiple headers will be added, one | |
| 106 | - * for each key in the array | |
| 107 | - * @param boolean $use_ee_prefix whether to use the EE prefix on the header, or fallback to | |
| 108 | - * the standard WP one | |
| 109 | - */ | |
| 110 | - protected function setResponseHeader($header_key, $value, $use_ee_prefix = true) | |
| 111 | -    { | |
| 112 | -        if (is_array($value)) { | |
| 113 | -            foreach ($value as $value_key => $value_value) { | |
| 114 | - $this->setResponseHeader($header_key . '[' . $value_key . ']', $value_value); | |
| 115 | - } | |
| 116 | -        } else { | |
| 117 | - $prefix = $use_ee_prefix ? Base::HEADER_PREFIX_FOR_EE : Base::HEADER_PREFIX_FOR_WP; | |
| 118 | - $this->response_headers[ $prefix . $header_key ] = $value; | |
| 119 | - } | |
| 120 | - } | |
| 121 | - | |
| 122 | - | |
| 123 | - /** | |
| 124 | - * Returns a flat array of headers to be added to the response | |
| 125 | - * | |
| 126 | - * @return array | |
| 127 | - */ | |
| 128 | - protected function getResponseHeaders() | |
| 129 | -    { | |
| 130 | - return apply_filters( | |
| 131 | - 'FHEE__EventEspresso\core\libraries\rest_api\controllers\Base___get_response_headers', | |
| 132 | - $this->response_headers, | |
| 133 | - $this, | |
| 134 | - $this->requested_version | |
| 135 | - ); | |
| 136 | - } | |
| 137 | - | |
| 138 | - | |
| 139 | - /** | |
| 140 | - * Adds error notices from EE_Error onto the provided \WP_Error | |
| 141 | - * | |
| 142 | - * @param WP_Error $wp_error_response | |
| 143 | - * @return WP_Error | |
| 144 | - */ | |
| 145 | - protected function addEeErrorsToResponse(WP_Error $wp_error_response) | |
| 146 | -    { | |
| 147 | - $notices_during_checkin = EE_Error::get_raw_notices(); | |
| 148 | -        if (! empty($notices_during_checkin['errors'])) { | |
| 149 | -            foreach ($notices_during_checkin['errors'] as $error_code => $error_message) { | |
| 150 | - $wp_error_response->add( | |
| 151 | - sanitize_key($error_code), | |
| 152 | - strip_tags($error_message) | |
| 153 | - ); | |
| 154 | - } | |
| 155 | - } | |
| 156 | - return $wp_error_response; | |
| 157 | - } | |
| 158 | - | |
| 159 | - | |
| 160 | - /** | |
| 161 | - * Sends a response, but also makes sure to attach headers that | |
| 162 | - * are handy for debugging. | |
| 163 | - * Specifically, we assume folks will want to know what exactly was the DB query that got run, | |
| 164 | - * what exactly was the Models query that got run, what capabilities came into play, what fields were omitted from | |
| 165 | - * the response, others? | |
| 166 | - * | |
| 167 | - * @param array|WP_Error|Exception|RestException $response | |
| 168 | - * @return WP_REST_Response | |
| 169 | - */ | |
| 170 | - public function sendResponse($response) | |
| 171 | -    { | |
| 172 | -        if ($response instanceof RestException) { | |
| 173 | - $response = new WP_Error($response->getStringCode(), $response->getMessage(), $response->getData()); | |
| 174 | - } | |
| 175 | -        if ($response instanceof Exception) { | |
| 176 | - $code = $response->getCode() ? $response->getCode() : 'error_occurred'; | |
| 177 | - $response = new WP_Error($code, $response->getMessage()); | |
| 178 | - } | |
| 179 | -        if ($response instanceof WP_Error) { | |
| 180 | - $response = $this->addEeErrorsToResponse($response); | |
| 181 | - $rest_response = $this->createRestResponseFromWpError($response); | |
| 182 | -        } else { | |
| 183 | - $rest_response = new WP_REST_Response($response, 200); | |
| 184 | - } | |
| 185 | - $headers = array(); | |
| 186 | -        if ($this->debug_mode && is_array($this->debug_info)) { | |
| 187 | -            foreach ($this->debug_info as $debug_key => $debug_info) { | |
| 188 | -                if (is_array($debug_info)) { | |
| 189 | - $debug_info = wp_json_encode($debug_info); | |
| 190 | - } | |
| 191 | -                $headers[ 'X-EE4-Debug-' . str_replace(" ", "-", ucwords($debug_key)) ] = $debug_info; | |
| 192 | - } | |
| 193 | - } | |
| 194 | - $headers = array_merge( | |
| 195 | - $headers, | |
| 196 | - $this->getResponseHeaders(), | |
| 197 | - $this->getHeadersFromEeNotices() | |
| 198 | - ); | |
| 199 | - $rest_response->set_headers($headers); | |
| 200 | - return $rest_response; | |
| 201 | - } | |
| 202 | - | |
| 203 | - | |
| 204 | - /** | |
| 205 | - * Converts the \WP_Error into `WP_REST_Response. | |
| 206 | - * Mostly this is just a copy-and-paste from \WP_REST_Server::error_to_response | |
| 207 | - * (which is protected) | |
| 208 | - * | |
| 209 | - * @param WP_Error $wp_error | |
| 210 | - * @return WP_REST_Response | |
| 211 | - */ | |
| 212 | - protected function createRestResponseFromWpError(WP_Error $wp_error) | |
| 213 | -    { | |
| 214 | - $error_data = $wp_error->get_error_data(); | |
| 215 | -        if (is_array($error_data) && isset($error_data['status'])) { | |
| 216 | - $status = $error_data['status']; | |
| 217 | -        } else { | |
| 218 | - $status = 500; | |
| 219 | - } | |
| 220 | - $errors = array(); | |
| 221 | -        foreach ((array) $wp_error->errors as $code => $messages) { | |
| 222 | -            foreach ((array) $messages as $message) { | |
| 223 | - $errors[] = array( | |
| 224 | - 'code' => $code, | |
| 225 | - 'message' => $message, | |
| 226 | - 'data' => $wp_error->get_error_data($code), | |
| 227 | - ); | |
| 228 | - } | |
| 229 | - } | |
| 230 | - $data = isset($errors[0]) ? $errors[0] : array(); | |
| 231 | -        if (count($errors) > 1) { | |
| 232 | - // Remove the primary error. | |
| 233 | - array_shift($errors); | |
| 234 | - $data['additional_errors'] = $errors; | |
| 235 | - } | |
| 236 | - return new WP_REST_Response($data, $status); | |
| 237 | - } | |
| 238 | - | |
| 239 | - | |
| 240 | - /** | |
| 241 | - * Array of headers derived from EE success, attention, and error messages | |
| 242 | - * | |
| 243 | - * @return array | |
| 244 | - */ | |
| 245 | - protected function getHeadersFromEeNotices() | |
| 246 | -    { | |
| 247 | - $headers = array(); | |
| 248 | - $notices = EE_Error::get_raw_notices(); | |
| 249 | -        foreach ($notices as $notice_type => $sub_notices) { | |
| 250 | -            if (! is_array($sub_notices)) { | |
| 251 | - continue; | |
| 252 | - } | |
| 253 | -            foreach ($sub_notices as $notice_code => $sub_notice) { | |
| 254 | - $headers[ 'X-EE4-Notices-' | |
| 255 | - . EEH_Inflector::humanize($notice_type) | |
| 256 | - . '[' | |
| 257 | - . $notice_code | |
| 258 | - . ']' ] = strip_tags((string) $sub_notice); | |
| 259 | - } | |
| 260 | - } | |
| 261 | - return apply_filters( | |
| 262 | - 'FHEE__EventEspresso\core\libraries\rest_api\controllers\Base___get_headers_from_ee_notices__return', | |
| 263 | - $headers, | |
| 264 | - $this->requested_version, | |
| 265 | - $notices | |
| 266 | - ); | |
| 267 | - } | |
| 268 | - | |
| 269 | - | |
| 270 | - /** | |
| 271 | - * Finds which version of the API was requested given the route, and returns it. | |
| 272 | - * eg in a request to "mysite.com/wp-json/ee/v4.8.29/events/123" this would return | |
| 273 | - * "4.8.29". | |
| 274 | - * We should know hte requested version in this model though, so if no route is | |
| 275 | - * provided just use what we set earlier | |
| 276 | - * | |
| 277 | - * @param string $route | |
| 278 | - * @return string | |
| 279 | - */ | |
| 280 | - public function getRequestedVersion($route = null) | |
| 281 | -    { | |
| 282 | -        if ($route === null) { | |
| 283 | - return $this->requested_version; | |
| 284 | - } | |
| 285 | - $matches = $this->parseRoute( | |
| 286 | - $route, | |
| 287 | - '~' . EED_Core_Rest_Api::ee_api_namespace_for_regex . '~', | |
| 288 | -            array('version') | |
| 289 | - ); | |
| 290 | -        if (isset($matches['version'])) { | |
| 291 | - return $matches['version']; | |
| 292 | -        } else { | |
| 293 | - return EED_Core_Rest_Api::latest_rest_api_version(); | |
| 294 | - } | |
| 295 | - } | |
| 296 | - | |
| 297 | - | |
| 298 | - /** | |
| 299 | - * Applies the regex to the route, then creates an array using the values of | |
| 300 | - * $match_keys as keys (but ignores the full pattern match). Returns the array of matches. | |
| 301 | - * For example, if you call | |
| 302 | - * parse_route( '/ee/v4.8/events', '~\/ee\/v([^/]*)\/(.*)~', array( 'version', 'model' ) ) | |
| 303 | - * it will return array( 'version' => '4.8', 'model' => 'events' ) | |
| 304 | - * | |
| 305 | - * @param string $route | |
| 306 | - * @param string $regex | |
| 307 | - * @param array $match_keys EXCLUDING matching the entire regex | |
| 308 | - * @return array where $match_keys are the keys (the first value of $match_keys | |
| 309 | - * becomes the first key of the return value, etc. Eg passing in $match_keys of | |
| 310 | - * array( 'model', 'id' ), will, if the regex is successful, will return | |
| 311 | - * array( 'model' => 'foo', 'id' => 'bar' ) | |
| 312 | - * @throws EE_Error if it couldn't be parsed | |
| 313 | - */ | |
| 314 | - public function parseRoute($route, $regex, $match_keys) | |
| 315 | -    { | |
| 316 | - $indexed_matches = array(); | |
| 317 | - $success = preg_match($regex, $route, $matches); | |
| 318 | -        if (is_array($matches)) { | |
| 319 | - // skip the overall regex match. Who cares | |
| 320 | -            for ($i = 1; $i <= count($match_keys); $i++) { | |
| 321 | -                if (! isset($matches[ $i ])) { | |
| 322 | - $success = false; | |
| 323 | -                } else { | |
| 324 | - $indexed_matches[ $match_keys[ $i - 1 ] ] = $matches[ $i ]; | |
| 325 | - } | |
| 326 | - } | |
| 327 | - } | |
| 328 | -        if (! $success) { | |
| 329 | - throw new EE_Error( | |
| 330 | -                esc_html__('We could not parse the URL. Please contact Event Espresso Support', 'event_espresso'), | |
| 331 | - 'endpoint_parsing_error' | |
| 332 | - ); | |
| 333 | - } | |
| 334 | - return $indexed_matches; | |
| 335 | - } | |
| 336 | - | |
| 337 | - | |
| 338 | - /** | |
| 339 | - * Gets the body's params (either from JSON or parsed body), which EXCLUDES the GET params and URL params | |
| 340 | - * | |
| 341 | - * @param \WP_REST_Request $request | |
| 342 | - * @return array | |
| 343 | - */ | |
| 344 | - protected function getBodyParams(\WP_REST_Request $request) | |
| 345 | -    { | |
| 346 | - // $request->get_params(); | |
| 347 | - return array_merge( | |
| 348 | - (array) $request->get_body_params(), | |
| 349 | - (array) $request->get_json_params() | |
| 350 | - ); | |
| 351 | - // return array_diff_key( | |
| 352 | - // $request->get_params(), | |
| 353 | - // $request->get_url_params(), | |
| 354 | - // $request->get_query_params() | |
| 355 | - // ); | |
| 356 | - } | |
| 23 | + /** | |
| 24 | + * @deprecated use all-caps version | |
| 25 | + */ | |
| 26 | + // @codingStandardsIgnoreStart | |
| 27 | + const header_prefix_for_ee = 'X-EE-'; | |
| 28 | + // @codingStandardsIgnoreEnd | |
| 29 | + | |
| 30 | + const HEADER_PREFIX_FOR_EE = 'X-EE-'; | |
| 31 | + | |
| 32 | + /** | |
| 33 | + * @deprecated use all-caps version instead | |
| 34 | + */ | |
| 35 | + // @codingStandardsIgnoreStart | |
| 36 | + const header_prefix_for_wp = 'X-WP-'; | |
| 37 | + // @codingStandardsIgnoreEnd | |
| 38 | + | |
| 39 | + const HEADER_PREFIX_FOR_WP = 'X-WP-'; | |
| 40 | + | |
| 41 | + /** | |
| 42 | + * Contains debug info we'll send back in the response headers | |
| 43 | + * | |
| 44 | + * @var array | |
| 45 | + */ | |
| 46 | + protected $debug_info = array(); | |
| 47 | + | |
| 48 | + /** | |
| 49 | + * Indicates whether or not the API is in debug mode | |
| 50 | + * | |
| 51 | + * @var boolean | |
| 52 | + */ | |
| 53 | + protected $debug_mode = false; | |
| 54 | + | |
| 55 | + /** | |
| 56 | + * Indicates the version that was requested | |
| 57 | + * | |
| 58 | + * @var string | |
| 59 | + */ | |
| 60 | + protected $requested_version; | |
| 61 | + | |
| 62 | + /** | |
| 63 | + * flat array of headers to send in the response | |
| 64 | + * | |
| 65 | + * @var array | |
| 66 | + */ | |
| 67 | + protected $response_headers = array(); | |
| 68 | + | |
| 69 | + | |
| 70 | + public function __construct() | |
| 71 | +	{ | |
| 72 | + $this->debug_mode = EED_Core_Rest_Api::debugMode(); | |
| 73 | + // we are handling a REST request. Don't show a fancy HTML error message is any error comes up | |
| 74 | +		add_filter('FHEE__EE_Error__get_error__show_normal_exceptions', '__return_true'); | |
| 75 | + } | |
| 76 | + | |
| 77 | + | |
| 78 | + /** | |
| 79 | + * Sets the version the user requested | |
| 80 | + * | |
| 81 | + * @param string $version eg '4.8' | |
| 82 | + */ | |
| 83 | + public function setRequestedVersion($version) | |
| 84 | +	{ | |
| 85 | + $this->requested_version = $version; | |
| 86 | + } | |
| 87 | + | |
| 88 | + | |
| 89 | + /** | |
| 90 | + * Sets some debug info that we'll send back in headers | |
| 91 | + * | |
| 92 | + * @param string $key | |
| 93 | + * @param string|array $info | |
| 94 | + */ | |
| 95 | + protected function setDebugInfo($key, $info) | |
| 96 | +	{ | |
| 97 | + $this->debug_info[ $key ] = $info; | |
| 98 | + } | |
| 99 | + | |
| 100 | + | |
| 101 | + /** | |
| 102 | + * Sets headers for the response | |
| 103 | + * | |
| 104 | + * @param string $header_key , excluding the "X-EE-" part | |
| 105 | + * @param array|string $value if an array, multiple headers will be added, one | |
| 106 | + * for each key in the array | |
| 107 | + * @param boolean $use_ee_prefix whether to use the EE prefix on the header, or fallback to | |
| 108 | + * the standard WP one | |
| 109 | + */ | |
| 110 | + protected function setResponseHeader($header_key, $value, $use_ee_prefix = true) | |
| 111 | +	{ | |
| 112 | +		if (is_array($value)) { | |
| 113 | +			foreach ($value as $value_key => $value_value) { | |
| 114 | + $this->setResponseHeader($header_key . '[' . $value_key . ']', $value_value); | |
| 115 | + } | |
| 116 | +		} else { | |
| 117 | + $prefix = $use_ee_prefix ? Base::HEADER_PREFIX_FOR_EE : Base::HEADER_PREFIX_FOR_WP; | |
| 118 | + $this->response_headers[ $prefix . $header_key ] = $value; | |
| 119 | + } | |
| 120 | + } | |
| 121 | + | |
| 122 | + | |
| 123 | + /** | |
| 124 | + * Returns a flat array of headers to be added to the response | |
| 125 | + * | |
| 126 | + * @return array | |
| 127 | + */ | |
| 128 | + protected function getResponseHeaders() | |
| 129 | +	{ | |
| 130 | + return apply_filters( | |
| 131 | + 'FHEE__EventEspresso\core\libraries\rest_api\controllers\Base___get_response_headers', | |
| 132 | + $this->response_headers, | |
| 133 | + $this, | |
| 134 | + $this->requested_version | |
| 135 | + ); | |
| 136 | + } | |
| 137 | + | |
| 138 | + | |
| 139 | + /** | |
| 140 | + * Adds error notices from EE_Error onto the provided \WP_Error | |
| 141 | + * | |
| 142 | + * @param WP_Error $wp_error_response | |
| 143 | + * @return WP_Error | |
| 144 | + */ | |
| 145 | + protected function addEeErrorsToResponse(WP_Error $wp_error_response) | |
| 146 | +	{ | |
| 147 | + $notices_during_checkin = EE_Error::get_raw_notices(); | |
| 148 | +		if (! empty($notices_during_checkin['errors'])) { | |
| 149 | +			foreach ($notices_during_checkin['errors'] as $error_code => $error_message) { | |
| 150 | + $wp_error_response->add( | |
| 151 | + sanitize_key($error_code), | |
| 152 | + strip_tags($error_message) | |
| 153 | + ); | |
| 154 | + } | |
| 155 | + } | |
| 156 | + return $wp_error_response; | |
| 157 | + } | |
| 158 | + | |
| 159 | + | |
| 160 | + /** | |
| 161 | + * Sends a response, but also makes sure to attach headers that | |
| 162 | + * are handy for debugging. | |
| 163 | + * Specifically, we assume folks will want to know what exactly was the DB query that got run, | |
| 164 | + * what exactly was the Models query that got run, what capabilities came into play, what fields were omitted from | |
| 165 | + * the response, others? | |
| 166 | + * | |
| 167 | + * @param array|WP_Error|Exception|RestException $response | |
| 168 | + * @return WP_REST_Response | |
| 169 | + */ | |
| 170 | + public function sendResponse($response) | |
| 171 | +	{ | |
| 172 | +		if ($response instanceof RestException) { | |
| 173 | + $response = new WP_Error($response->getStringCode(), $response->getMessage(), $response->getData()); | |
| 174 | + } | |
| 175 | +		if ($response instanceof Exception) { | |
| 176 | + $code = $response->getCode() ? $response->getCode() : 'error_occurred'; | |
| 177 | + $response = new WP_Error($code, $response->getMessage()); | |
| 178 | + } | |
| 179 | +		if ($response instanceof WP_Error) { | |
| 180 | + $response = $this->addEeErrorsToResponse($response); | |
| 181 | + $rest_response = $this->createRestResponseFromWpError($response); | |
| 182 | +		} else { | |
| 183 | + $rest_response = new WP_REST_Response($response, 200); | |
| 184 | + } | |
| 185 | + $headers = array(); | |
| 186 | +		if ($this->debug_mode && is_array($this->debug_info)) { | |
| 187 | +			foreach ($this->debug_info as $debug_key => $debug_info) { | |
| 188 | +				if (is_array($debug_info)) { | |
| 189 | + $debug_info = wp_json_encode($debug_info); | |
| 190 | + } | |
| 191 | +				$headers[ 'X-EE4-Debug-' . str_replace(" ", "-", ucwords($debug_key)) ] = $debug_info; | |
| 192 | + } | |
| 193 | + } | |
| 194 | + $headers = array_merge( | |
| 195 | + $headers, | |
| 196 | + $this->getResponseHeaders(), | |
| 197 | + $this->getHeadersFromEeNotices() | |
| 198 | + ); | |
| 199 | + $rest_response->set_headers($headers); | |
| 200 | + return $rest_response; | |
| 201 | + } | |
| 202 | + | |
| 203 | + | |
| 204 | + /** | |
| 205 | + * Converts the \WP_Error into `WP_REST_Response. | |
| 206 | + * Mostly this is just a copy-and-paste from \WP_REST_Server::error_to_response | |
| 207 | + * (which is protected) | |
| 208 | + * | |
| 209 | + * @param WP_Error $wp_error | |
| 210 | + * @return WP_REST_Response | |
| 211 | + */ | |
| 212 | + protected function createRestResponseFromWpError(WP_Error $wp_error) | |
| 213 | +	{ | |
| 214 | + $error_data = $wp_error->get_error_data(); | |
| 215 | +		if (is_array($error_data) && isset($error_data['status'])) { | |
| 216 | + $status = $error_data['status']; | |
| 217 | +		} else { | |
| 218 | + $status = 500; | |
| 219 | + } | |
| 220 | + $errors = array(); | |
| 221 | +		foreach ((array) $wp_error->errors as $code => $messages) { | |
| 222 | +			foreach ((array) $messages as $message) { | |
| 223 | + $errors[] = array( | |
| 224 | + 'code' => $code, | |
| 225 | + 'message' => $message, | |
| 226 | + 'data' => $wp_error->get_error_data($code), | |
| 227 | + ); | |
| 228 | + } | |
| 229 | + } | |
| 230 | + $data = isset($errors[0]) ? $errors[0] : array(); | |
| 231 | +		if (count($errors) > 1) { | |
| 232 | + // Remove the primary error. | |
| 233 | + array_shift($errors); | |
| 234 | + $data['additional_errors'] = $errors; | |
| 235 | + } | |
| 236 | + return new WP_REST_Response($data, $status); | |
| 237 | + } | |
| 238 | + | |
| 239 | + | |
| 240 | + /** | |
| 241 | + * Array of headers derived from EE success, attention, and error messages | |
| 242 | + * | |
| 243 | + * @return array | |
| 244 | + */ | |
| 245 | + protected function getHeadersFromEeNotices() | |
| 246 | +	{ | |
| 247 | + $headers = array(); | |
| 248 | + $notices = EE_Error::get_raw_notices(); | |
| 249 | +		foreach ($notices as $notice_type => $sub_notices) { | |
| 250 | +			if (! is_array($sub_notices)) { | |
| 251 | + continue; | |
| 252 | + } | |
| 253 | +			foreach ($sub_notices as $notice_code => $sub_notice) { | |
| 254 | + $headers[ 'X-EE4-Notices-' | |
| 255 | + . EEH_Inflector::humanize($notice_type) | |
| 256 | + . '[' | |
| 257 | + . $notice_code | |
| 258 | + . ']' ] = strip_tags((string) $sub_notice); | |
| 259 | + } | |
| 260 | + } | |
| 261 | + return apply_filters( | |
| 262 | + 'FHEE__EventEspresso\core\libraries\rest_api\controllers\Base___get_headers_from_ee_notices__return', | |
| 263 | + $headers, | |
| 264 | + $this->requested_version, | |
| 265 | + $notices | |
| 266 | + ); | |
| 267 | + } | |
| 268 | + | |
| 269 | + | |
| 270 | + /** | |
| 271 | + * Finds which version of the API was requested given the route, and returns it. | |
| 272 | + * eg in a request to "mysite.com/wp-json/ee/v4.8.29/events/123" this would return | |
| 273 | + * "4.8.29". | |
| 274 | + * We should know hte requested version in this model though, so if no route is | |
| 275 | + * provided just use what we set earlier | |
| 276 | + * | |
| 277 | + * @param string $route | |
| 278 | + * @return string | |
| 279 | + */ | |
| 280 | + public function getRequestedVersion($route = null) | |
| 281 | +	{ | |
| 282 | +		if ($route === null) { | |
| 283 | + return $this->requested_version; | |
| 284 | + } | |
| 285 | + $matches = $this->parseRoute( | |
| 286 | + $route, | |
| 287 | + '~' . EED_Core_Rest_Api::ee_api_namespace_for_regex . '~', | |
| 288 | +			array('version') | |
| 289 | + ); | |
| 290 | +		if (isset($matches['version'])) { | |
| 291 | + return $matches['version']; | |
| 292 | +		} else { | |
| 293 | + return EED_Core_Rest_Api::latest_rest_api_version(); | |
| 294 | + } | |
| 295 | + } | |
| 296 | + | |
| 297 | + | |
| 298 | + /** | |
| 299 | + * Applies the regex to the route, then creates an array using the values of | |
| 300 | + * $match_keys as keys (but ignores the full pattern match). Returns the array of matches. | |
| 301 | + * For example, if you call | |
| 302 | + * parse_route( '/ee/v4.8/events', '~\/ee\/v([^/]*)\/(.*)~', array( 'version', 'model' ) ) | |
| 303 | + * it will return array( 'version' => '4.8', 'model' => 'events' ) | |
| 304 | + * | |
| 305 | + * @param string $route | |
| 306 | + * @param string $regex | |
| 307 | + * @param array $match_keys EXCLUDING matching the entire regex | |
| 308 | + * @return array where $match_keys are the keys (the first value of $match_keys | |
| 309 | + * becomes the first key of the return value, etc. Eg passing in $match_keys of | |
| 310 | + * array( 'model', 'id' ), will, if the regex is successful, will return | |
| 311 | + * array( 'model' => 'foo', 'id' => 'bar' ) | |
| 312 | + * @throws EE_Error if it couldn't be parsed | |
| 313 | + */ | |
| 314 | + public function parseRoute($route, $regex, $match_keys) | |
| 315 | +	{ | |
| 316 | + $indexed_matches = array(); | |
| 317 | + $success = preg_match($regex, $route, $matches); | |
| 318 | +		if (is_array($matches)) { | |
| 319 | + // skip the overall regex match. Who cares | |
| 320 | +			for ($i = 1; $i <= count($match_keys); $i++) { | |
| 321 | +				if (! isset($matches[ $i ])) { | |
| 322 | + $success = false; | |
| 323 | +				} else { | |
| 324 | + $indexed_matches[ $match_keys[ $i - 1 ] ] = $matches[ $i ]; | |
| 325 | + } | |
| 326 | + } | |
| 327 | + } | |
| 328 | +		if (! $success) { | |
| 329 | + throw new EE_Error( | |
| 330 | +				esc_html__('We could not parse the URL. Please contact Event Espresso Support', 'event_espresso'), | |
| 331 | + 'endpoint_parsing_error' | |
| 332 | + ); | |
| 333 | + } | |
| 334 | + return $indexed_matches; | |
| 335 | + } | |
| 336 | + | |
| 337 | + | |
| 338 | + /** | |
| 339 | + * Gets the body's params (either from JSON or parsed body), which EXCLUDES the GET params and URL params | |
| 340 | + * | |
| 341 | + * @param \WP_REST_Request $request | |
| 342 | + * @return array | |
| 343 | + */ | |
| 344 | + protected function getBodyParams(\WP_REST_Request $request) | |
| 345 | +	{ | |
| 346 | + // $request->get_params(); | |
| 347 | + return array_merge( | |
| 348 | + (array) $request->get_body_params(), | |
| 349 | + (array) $request->get_json_params() | |
| 350 | + ); | |
| 351 | + // return array_diff_key( | |
| 352 | + // $request->get_params(), | |
| 353 | + // $request->get_url_params(), | |
| 354 | + // $request->get_query_params() | |
| 355 | + // ); | |
| 356 | + } | |
| 357 | 357 | } | 
| @@ -94,7 +94,7 @@ discard block | ||
| 94 | 94 | */ | 
| 95 | 95 | protected function setDebugInfo($key, $info) | 
| 96 | 96 |      { | 
| 97 | - $this->debug_info[ $key ] = $info; | |
| 97 | + $this->debug_info[$key] = $info; | |
| 98 | 98 | } | 
| 99 | 99 | |
| 100 | 100 | |
| @@ -111,11 +111,11 @@ discard block | ||
| 111 | 111 |      { | 
| 112 | 112 |          if (is_array($value)) { | 
| 113 | 113 |              foreach ($value as $value_key => $value_value) { | 
| 114 | - $this->setResponseHeader($header_key . '[' . $value_key . ']', $value_value); | |
| 114 | + $this->setResponseHeader($header_key.'['.$value_key.']', $value_value); | |
| 115 | 115 | } | 
| 116 | 116 |          } else { | 
| 117 | 117 | $prefix = $use_ee_prefix ? Base::HEADER_PREFIX_FOR_EE : Base::HEADER_PREFIX_FOR_WP; | 
| 118 | - $this->response_headers[ $prefix . $header_key ] = $value; | |
| 118 | + $this->response_headers[$prefix.$header_key] = $value; | |
| 119 | 119 | } | 
| 120 | 120 | } | 
| 121 | 121 | |
| @@ -145,7 +145,7 @@ discard block | ||
| 145 | 145 | protected function addEeErrorsToResponse(WP_Error $wp_error_response) | 
| 146 | 146 |      { | 
| 147 | 147 | $notices_during_checkin = EE_Error::get_raw_notices(); | 
| 148 | -        if (! empty($notices_during_checkin['errors'])) { | |
| 148 | +        if ( ! empty($notices_during_checkin['errors'])) { | |
| 149 | 149 |              foreach ($notices_during_checkin['errors'] as $error_code => $error_message) { | 
| 150 | 150 | $wp_error_response->add( | 
| 151 | 151 | sanitize_key($error_code), | 
| @@ -188,7 +188,7 @@ discard block | ||
| 188 | 188 |                  if (is_array($debug_info)) { | 
| 189 | 189 | $debug_info = wp_json_encode($debug_info); | 
| 190 | 190 | } | 
| 191 | -                $headers[ 'X-EE4-Debug-' . str_replace(" ", "-", ucwords($debug_key)) ] = $debug_info; | |
| 191 | +                $headers['X-EE4-Debug-'.str_replace(" ", "-", ucwords($debug_key))] = $debug_info; | |
| 192 | 192 | } | 
| 193 | 193 | } | 
| 194 | 194 | $headers = array_merge( | 
| @@ -247,15 +247,15 @@ discard block | ||
| 247 | 247 | $headers = array(); | 
| 248 | 248 | $notices = EE_Error::get_raw_notices(); | 
| 249 | 249 |          foreach ($notices as $notice_type => $sub_notices) { | 
| 250 | -            if (! is_array($sub_notices)) { | |
| 250 | +            if ( ! is_array($sub_notices)) { | |
| 251 | 251 | continue; | 
| 252 | 252 | } | 
| 253 | 253 |              foreach ($sub_notices as $notice_code => $sub_notice) { | 
| 254 | - $headers[ 'X-EE4-Notices-' | |
| 254 | + $headers['X-EE4-Notices-' | |
| 255 | 255 | . EEH_Inflector::humanize($notice_type) | 
| 256 | 256 | . '[' | 
| 257 | 257 | . $notice_code | 
| 258 | - . ']' ] = strip_tags((string) $sub_notice); | |
| 258 | + . ']'] = strip_tags((string) $sub_notice); | |
| 259 | 259 | } | 
| 260 | 260 | } | 
| 261 | 261 | return apply_filters( | 
| @@ -284,7 +284,7 @@ discard block | ||
| 284 | 284 | } | 
| 285 | 285 | $matches = $this->parseRoute( | 
| 286 | 286 | $route, | 
| 287 | - '~' . EED_Core_Rest_Api::ee_api_namespace_for_regex . '~', | |
| 287 | + '~'.EED_Core_Rest_Api::ee_api_namespace_for_regex.'~', | |
| 288 | 288 |              array('version') | 
| 289 | 289 | ); | 
| 290 | 290 |          if (isset($matches['version'])) { | 
| @@ -318,14 +318,14 @@ discard block | ||
| 318 | 318 |          if (is_array($matches)) { | 
| 319 | 319 | // skip the overall regex match. Who cares | 
| 320 | 320 |              for ($i = 1; $i <= count($match_keys); $i++) { | 
| 321 | -                if (! isset($matches[ $i ])) { | |
| 321 | +                if ( ! isset($matches[$i])) { | |
| 322 | 322 | $success = false; | 
| 323 | 323 |                  } else { | 
| 324 | - $indexed_matches[ $match_keys[ $i - 1 ] ] = $matches[ $i ]; | |
| 324 | + $indexed_matches[$match_keys[$i - 1]] = $matches[$i]; | |
| 325 | 325 | } | 
| 326 | 326 | } | 
| 327 | 327 | } | 
| 328 | -        if (! $success) { | |
| 328 | +        if ( ! $success) { | |
| 329 | 329 | throw new EE_Error( | 
| 330 | 330 |                  esc_html__('We could not parse the URL. Please contact Event Espresso Support', 'event_espresso'), | 
| 331 | 331 | 'endpoint_parsing_error' |