| @@ -1,0 +1,118 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +declare(strict_types=1); | |
| 4 | + | |
| 5 | +namespace Metricool\Http\Endpoints; | |
| 6 | + | |
| 7 | +use Metricool\Http\Endpoints\Responses\AnalyticsResponse; | |
| 8 | +use Metricool\Http\Metricool\MetricoolApi; | |
| 9 | +use Metricool\Interfaces\SingleEndpointInterface; | |
| 10 | +use Metricool\Services\AnalyticsService; | |
| 11 | +use Metricool\Services\DashboardService; | |
| 12 | +use Metricool\Traits\HasAllowlistControl; | |
| 13 | +use Metricool\Traits\HasRestAccess; | |
| 14 | + | |
| 15 | +class AnalyticsEndpoint implements SingleEndpointInterface | |
| 16 | +{ | |
| 17 | + use HasRestAccess; | |
| 18 | + use HasAllowlistControl; | |
| 19 | + | |
| 20 | + public const ROUTE = 'analytics'; | |
| 21 | + | |
| 22 | + public AnalyticsService $service; | |
| 23 | + public MetricoolApi $metricoolApi; | |
| 24 | + public DashboardService $dashboard; | |
| 25 | + | |
| 26 | + public function __construct(AnalyticsService $service, MetricoolApi $metricoolApi, DashboardService $dashboard) | |
| 27 | + { | |
| 28 | + $this->service = $service; | |
| 29 | + $this->metricoolApi = $metricoolApi; | |
| 30 | + $this->dashboard = $dashboard; | |
| 31 | + } | |
| 32 | + | |
| 33 | + /** | |
| 34 | + * @inheritDoc | |
| 35 | + */ | |
| 36 | + public function registerRoute(): string | |
| 37 | + { | |
| 38 | + return self::ROUTE; | |
| 39 | + } | |
| 40 | + | |
| 41 | + /** | |
| 42 | + * Only enable this endpoint when onboarding is completed | |
| 43 | + */ | |
| 44 | + public function enabled(): bool | |
| 45 | + { | |
| 46 | + return $this->dashboard->isOnboardingCompleted(); | |
| 47 | + } | |
| 48 | + | |
| 49 | + /** | |
| 50 | + * @inheritDoc | |
| 51 | + */ | |
| 52 | + public function registerArguments(): array | |
| 53 | + { | |
| 54 | + return [ | |
| 55 | + 'methods' => \WP_REST_Server::READABLE, | |
| 56 | + 'callback' => [$this, 'callback'], | |
| 57 | + 'middleware' => ['metricool:auth', 'metricool:blog_id'], | |
| 58 | + ]; | |
| 59 | + } | |
| 60 | + | |
| 61 | + /** | |
| 62 | + * Method will dynamically request the requested statistic. If the metric | |
| 63 | + * is filterable and filters are provided, it will apply them before | |
| 64 | + * retrieving the data. | |
| 65 | + * | |
| 66 | + * GET /wp-json/metricool/v1/analytics?filters[start]=20250618&filters[end]=20250718&metrics[]=pageViews&metrics[]=visits | |
| 67 | + */ | |
| 68 | + public function callback(\WP_REST_Request $request): \WP_REST_Response | |
| 69 | + { | |
| 70 | + try { | |
| 71 | + $response = $this->buildResponse($request); | |
| 72 | + } catch (\Exception $e) { | |
| 73 | + return $this->sendHttpErrorResponse(__('Failed to load Analytics data', 'metricool'), $e->getMessage(), $e->getCode()); | |
| 74 | + } | |
| 75 | + | |
| 76 | + return $this->sendHttpResponse($response); | |
| 77 | + } | |
| 78 | + | |
| 79 | + /** | |
| 80 | + * Build the specific Analytics response for the endpoint. This is mainly | |
| 81 | + * used in the plugin Dashboard to reflect non-realtime statistics. | |
| 82 | + * Building it server side prevents client-side complexity. | |
| 83 | + */ | |
| 84 | + private function buildResponse(\WP_REST_Request $request): array | |
| 85 | + { | |
| 86 | + $statisticsModule = $this->metricoolApi->statistics(); | |
| 87 | + $requestFilters = ($request->get_param('filters') ?: []); | |
| 88 | + $requestMetrics = ($request->get_param('metrics') ?: ['pageViews', 'visits', 'visitors', 'posts', 'comments']); | |
| 89 | + | |
| 90 | + $this->service->setRequestFilters($requestFilters); | |
| 91 | + | |
| 92 | + if (in_array('pageViews', $requestMetrics)) { | |
| 93 | + $this->service->loadMetric('pageViews', __('Page views', 'metricool'), $statisticsModule->pageViews()); | |
| 94 | + } | |
| 95 | + | |
| 96 | + if (in_array('visits', $requestMetrics)) { | |
| 97 | + $this->service->loadMetric('visits', __('Visits', 'metricool'), $statisticsModule->visits()); | |
| 98 | + } | |
| 99 | + | |
| 100 | + if (in_array('visitors', $requestMetrics)) { | |
| 101 | + $this->service->loadMetric('visitors', __('Visitors', 'metricool'), $statisticsModule->visitors()); | |
| 102 | + } | |
| 103 | + | |
| 104 | + if (in_array('posts', $requestMetrics)) { | |
| 105 | + $this->service->loadMetric('posts', __('Posts', 'metricool'), $statisticsModule->posts()); | |
| 106 | + } | |
| 107 | + | |
| 108 | + if (in_array('comments', $requestMetrics)) { | |
| 109 | + $this->service->loadMetric('comments', __('Comments', 'metricool'), $statisticsModule->comments()); | |
| 110 | + } | |
| 111 | + | |
| 112 | + $response = new AnalyticsResponse(); | |
| 113 | + $response->setTotals($this->service->getTotals()); | |
| 114 | + $response->setTimelineData($this->service->getTimelineData()); | |
| 115 | + | |
| 116 | + return $response->body(); | |
| 117 | + } | |
| 118 | +} | |