| @@ -1,0 +1,79 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +declare(strict_types=1); | |
| 4 | + | |
| 5 | +namespace Metricool\Traits; | |
| 6 | + | |
| 7 | +use Metricool\Support\Helpers\Storage; | |
| 8 | + | |
| 9 | +trait HasRestAccess | |
| 10 | +{ | |
| 11 | + /** | |
| 12 | + * Retrieve the parameters from the request. | |
| 13 | + * | |
| 14 | + * If the data is coming from an AJAX request, its data will be prioritized | |
| 15 | + * over the request's JSON parameters. | |
| 16 | + * | |
| 17 | + * @param string $param - The param to search all the parameters in the | |
| 18 | + * request. The key 'data' is often used as the main key. In that case set | |
| 19 | + * $param to 'data' to retrieve the parameters from that level. | |
| 20 | + */ | |
| 21 | + public function retrieveHttpParameters(\WP_REST_Request $request, array $ajaxData = [], string $param = ''): array | |
| 22 | + { | |
| 23 | + if (!empty($param)) { | |
| 24 | + return $ajaxData[$param] ?? $request->get_param($param); | |
| 25 | + } | |
| 26 | + | |
| 27 | + $httpParameters = $ajaxData ?: $request->get_json_params(); | |
| 28 | + return $httpParameters ?: []; | |
| 29 | + } | |
| 30 | + | |
| 31 | + /** | |
| 32 | + * Retrieve the parameters from the request and store them as Storage. | |
| 33 | + * @uses \Metricool\Helpers\Storage | |
| 34 | + * @uses HasRestAccess::retrieveHttpParameters | |
| 35 | + */ | |
| 36 | + public function retrieveHttpStorage(\WP_REST_Request $request, array $ajaxData = [], string $param = ''): Storage | |
| 37 | + { | |
| 38 | + return new Storage( | |
| 39 | + $this->retrieveHttpParameters($request, $ajaxData, $param) | |
| 40 | + ); | |
| 41 | + } | |
| 42 | + | |
| 43 | + /** | |
| 44 | + * Standardized response format | |
| 45 | + * | |
| 46 | + * @param mixed $data - Data to return | |
| 47 | + * @param bool $status - If this action has completed successfully | |
| 48 | + * @param string $message - Message to return | |
| 49 | + * @param int $code - HTTP status code | |
| 50 | + * @return \WP_REST_Response | |
| 51 | + */ | |
| 52 | + public function sendHttpResponse($data = null, bool $status = true, string $message = '', int $code = 200): \WP_REST_Response | |
| 53 | + { | |
| 54 | + if (ob_get_length()) { | |
| 55 | + ob_clean(); | |
| 56 | + } | |
| 57 | + | |
| 58 | + return new \WP_REST_Response([ | |
| 59 | + 'message' => $message, | |
| 60 | + 'status' => $status ? 'success' : 'error', | |
| 61 | + 'data' => $data, | |
| 62 | + 'request_success' => true, // can be used to check if the response in react actually contains this array. | |
| 63 | + ], $code ?: 500); | |
| 64 | + } | |
| 65 | + | |
| 66 | + /** | |
| 67 | + * Standardized response error format | |
| 68 | + * | |
| 69 | + * @param string $message - A user friendly error message | |
| 70 | + * @param mixed $data - The data of the error | |
| 71 | + * @param int $code - HTTP status code | |
| 72 | + * @return \WP_REST_Response | |
| 73 | + */ | |
| 74 | + public function sendHttpErrorResponse(string $message = 'An error occurred', $data = null, int $code = 500): \WP_REST_Response | |
| 75 | + { | |
| 76 | + $data = (defined('WP_DEBUG') && WP_DEBUG) ? $data : null; | |
| 77 | + return $this->sendHttpResponse($data, false, $message, $code); | |
| 78 | + } | |
| 79 | +} | |