| @@ -9,9 +9,11 @@ | ||
| 9 | 9 | use Metricool\Http\Endpoints\Responses\Statistics\RefererResponse; |
| 10 | 10 | use Metricool\Http\Metricool\DTOs\DistributionDTO; |
| 11 | 11 | use Metricool\Http\Metricool\MetricoolApi; |
| 12 | 12 | use Metricool\Interfaces\SingleEndpointInterface; |
| 13 | +use Metricool\Services\DashboardService; | |
| 13 | 14 | use Metricool\Support\Helpers\Collection; |
| 15 | +use Metricool\Support\Validation\Validator; | |
| 14 | 16 | use Metricool\Traits\HasAllowlistControl; |
| 15 | 17 | use Metricool\Traits\HasRestAccess; |
| 16 | 18 | |
| 17 | 19 | class DistributionEndpoint implements SingleEndpointInterface |
| @@ -26,12 +28,14 @@ | ||
| 26 | 28 | 'referers' => RefererResponse::class, |
| 27 | 29 | ]; |
| 28 | 30 | |
| 29 | 31 | public MetricoolApi $metricoolApi; |
| 32 | + public DashboardService $dashboard; | |
| 30 | 33 | |
| 31 | - public function __construct(MetricoolApi $metricoolApi) | |
| 34 | + public function __construct(MetricoolApi $metricoolApi, DashboardService $dashboard) | |
| 32 | 35 | { |
| 33 | 36 | $this->metricoolApi = $metricoolApi; |
| 37 | + $this->dashboard = $dashboard; | |
| 34 | 38 | } |
| 35 | 39 | |
| 36 | 40 | /** |
| 37 | 41 | * @inheritDoc |
| @@ -41,13 +45,13 @@ | ||
| 41 | 45 | return self::ROUTE . '/(?P<metric>[^/]+)'; |
| 42 | 46 | } |
| 43 | 47 | |
| 44 | 48 | /** |
| 45 | - * @inheritDoc | |
| 49 | + * Only enable this endpoint when onboarding is completed | |
| 46 | 50 | */ |
| 47 | 51 | public function enabled(): bool |
| 48 | 52 | { |
| 49 | - return $this->adminAccessAllowed(); | |
| 53 | + return $this->dashboard->isOnboardingCompleted(); | |
| 50 | 54 | } |
| 51 | 55 | |
| 52 | 56 | /** |
| 53 | 57 | * @inheritDoc |
| @@ -64,14 +68,20 @@ | ||
| 64 | 68 | /** |
| 65 | 69 | * Method will dynamically request the requested statistic. If the metric |
| 66 | 70 | * is filterable and filters are provided, it will apply them before |
| 67 | 71 | * retrieving the data. |
| 68 | - * @example /wp-json/metricool/v1/statistics/countries?filters[start]=20250618&filters[end]=20250718&filters[country]=nl | |
| 72 | + * | |
| 73 | + * GET /wp-json/metricool/v1/distribution/countries?filters[start]=20250618&filters[end]=20250718&filters[country]=nl | |
| 69 | 74 | */ |
| 70 | 75 | public function callback(\WP_REST_Request $request): \WP_REST_Response |
| 71 | 76 | { |
| 77 | + $validated = Validator::validate($request->get_params(), [ | |
| 78 | + 'metric' => 'required|string|in:countries,referers', | |
| 79 | + 'filters' => 'array', | |
| 80 | + ]); | |
| 81 | + | |
| 72 | 82 | try { |
| 73 | - $response = $this->buildResponse($request); | |
| 83 | + $response = $this->buildResponse($validated); | |
| 74 | 84 | } catch (\Exception $e) { |
| 75 | 85 | return $this->sendHttpErrorResponse(__('Failed to load Analytics data', 'metricool'), $e->getMessage(), $e->getCode()); |
| 76 | 86 | } |
| 77 | 87 | |
| @@ -84,12 +94,12 @@ | ||
| 84 | 94 | * Building it server side prevents client-side complexity. |
| 85 | 95 | * |
| 86 | 96 | * @throws \Exception |
| 87 | 97 | */ |
| 88 | - private function buildResponse(\WP_REST_Request $request): array | |
| 98 | + private function buildResponse(array $validated): array | |
| 89 | 99 | { |
| 90 | - $metric = ($request->get_param('metric') ?: ''); | |
| 91 | - $requestFilters = ($request->get_param('filters') ?: []); | |
| 100 | + $metric = $validated['metric']; | |
| 101 | + $requestFilters = $validated['filters'] ?? []; | |
| 92 | 102 | |
| 93 | 103 | // Load the statistics |
| 94 | 104 | $statistics = $this->getStatisticsForMetric($metric, $requestFilters); |
| 95 | 105 | |