PluginProbe
Metricool – Social media and site statistics / trunk
Metricool – Social media and site statistics vtrunk
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 trunk, at app/Http/Endpoints/RealtimeEndpoint.php

112 lines 3.3 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\DashboardService;
12 use Metricool\Services\RealtimeService;
13 use Metricool\Traits\HasAllowlistControl;
14 use Metricool\Traits\HasRestAccess;
15
16 class RealtimeEndpoint implements SingleEndpointInterface
17 {
18 use HasRestAccess;
19 use HasAllowlistControl;
20
21 public const ROUTE = 'realtime';
22
23 public RealtimeService $service;
24 public MetricoolApi $metricoolApi;
25 public DashboardService $dashboard;
26
27 public function __construct(RealtimeService $service, MetricoolApi $metricoolApi, DashboardService $dashboard)
28 {
29 $this->service = $service;
30 $this->metricoolApi = $metricoolApi;
31 $this->dashboard = $dashboard;
32 }
33
34 /**
35 * @inheritDoc
36 */
37 public function registerRoute(): string
38 {
39 return self::ROUTE;
40 }
41
42 /**
43 * Only enable this endpoint when onboarding is completed
44 */
45 public function enabled(): bool
46 {
47 return $this->dashboard->isOnboardingCompleted();
48 }
49
50 /**
51 * @inheritDoc
52 */
53 public function registerArguments(): array
54 {
55 return [
56 'methods' => \WP_REST_Server::READABLE,
57 'callback' => [$this, 'callback'],
58 'middleware' => ['metricool:auth', 'metricool:blog_id'],
59 ];
60 }
61
62 /**
63 * Build the specific Realtime response for the endpoint. This is mainly
64 * used in the plugin Dashboard. Building it server side prevents client-side complexity.
65 *
66 * GET /wp-json/metricool/v1/realtime
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 Realtime 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 * @throws Exception
84 */
85 private function buildResponse(\WP_REST_Request $request): array
86 {
87 $realtimeModule = $this->metricoolApi->realtime();
88
89 // Get our data
90 $sessions = $realtimeModule->sessions()->get();
91 if (!isset($sessions['timeline'])) {
92 throw new Exception('Session data is missing');
93 }
94
95 $values = $realtimeModule->current()->get();
96 if (!isset($values['activeVisits'])) {
97 throw new Exception('Visitors data is missing');
98 }
99
100 // Add the pageViews to the timeline and totals
101 $this->service->addMetric('pageViews', __('Page views', 'metricool'), $sessions['timeline']);
102 // Add visitors just to the totals
103 $this->service->addTotals('visitors', __('Visitors', 'metricool'), $values['activeVisits']);
104
105 $response = new RealtimeResponse();
106 $response->setTotals($this->service->getTotals());
107 $response->setTimelineData($this->service->getTimelineData());
108
109 return $response->body();
110 }
111 }
112