PluginProbe
Metricool – Social media and site statistics / 2.0.2
Metricool – Social media and site statistics v2.0.2
2.1.0 2.0.2 2.0.1 2.0.0 1.27 trunk
metricool / app / Http / Endpoints / AnalyticsEndpoint.php

AnalyticsEndpoint.php in Metricool – Social media and site statistics 2.0.2, at app/Http/Endpoints/AnalyticsEndpoint.php

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