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 / RealtimeEndpoint.php

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

107 lines 3.1 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 Exception;
8 use Metricool\Http\Endpoints\Responses\RealtimeResponse;
9 use Metricool\Http\Metricool\MetricoolApi;
10 use Metricool\Interfaces\SingleEndpointInterface;
11 use Metricool\Services\RealtimeService;
12 use Metricool\Traits\HasAllowlistControl;
13 use Metricool\Traits\HasRestAccess;
14
15 class RealtimeEndpoint implements SingleEndpointInterface
16 {
17 use HasRestAccess;
18 use HasAllowlistControl;
19
20 public const ROUTE = 'realtime';
21
22 public RealtimeService $service;
23 public MetricoolApi $metricoolApi;
24
25 public function __construct(RealtimeService $service, MetricoolApi $metricoolApi)
26 {
27 $this->service = $service;
28 $this->metricoolApi = $metricoolApi;
29 }
30
31 /**
32 * @inheritDoc
33 */
34 public function registerRoute(): string
35 {
36 return self::ROUTE;
37 }
38
39 /**
40 * @inheritDoc
41 */
42 public function enabled(): bool
43 {
44 return $this->adminAccessAllowed();
45 }
46
47 /**
48 * @inheritDoc
49 */
50 public function registerArguments(): array
51 {
52 return [
53 'methods' => \WP_REST_Server::READABLE,
54 'callback' => [$this, 'callback'],
55 'middleware' => ['metricool:auth', 'metricool:blog_id'],
56 ];
57 }
58
59 /**
60 * Build the specific Realtime response for the endpoint. This is mainly
61 * used in the plugin Dashboard. Building it server side prevents client-side complexity.
62 */
63 public function callback(\WP_REST_Request $request): \WP_REST_Response
64 {
65 try {
66 $response = $this->buildResponse($request);
67 } catch (Exception $e) {
68 return $this->sendHttpErrorResponse(__('Failed to load Realtime data', 'metricool'), $e->getMessage(), $e->getCode());
69 }
70
71 return $this->sendHttpResponse($response);
72 }
73
74 /**
75 * Build the specific Analytics response for the endpoint. This is mainly
76 * used in the plugin Dashboard to reflect non-realtime statistics.
77 * Building it server side prevents client-side complexity.
78 * @throws Exception
79 */
80 private function buildResponse(\WP_REST_Request $request): array
81 {
82 $realtimeModule = $this->metricoolApi->realtime();
83
84 // Get our data
85 $sessions = $realtimeModule->sessions()->get();
86 if (!isset($sessions['timeline'])) {
87 throw new Exception('Session data is missing');
88 }
89
90 $values = $realtimeModule->current()->get();
91 if (!isset($values['activeVisits'])) {
92 throw new Exception('Visitors data is missing');
93 }
94
95 // Add the pageViews to the timeline and totals
96 $this->service->addMetric('pageViews', __('Page views', 'metricool'), $sessions['timeline']);
97 // Add visitors just to the totals
98 $this->service->addTotals('visitors', __('Visitors', 'metricool'), $values['activeVisits']);
99
100 $response = new RealtimeResponse();
101 $response->setTotals($this->service->getTotals());
102 $response->setTimelineData($this->service->getTimelineData());
103
104 return $response->body();
105 }
106 }
107