PluginProbe
Activity Logs, User Activity Tracking, Multisite Activity Log from Logtivity / 3.3.9
Activity Logs, User Activity Tracking, Multisite Activity Log from Logtivity v3.3.9
3.3.9 3.3.8 trunk 1.0 1.1.0 1.10.0 1.11.0 1.11.1 1.12.0 1.13.0 1.14.0 1.15.0 1.16.0 1.17.0 1.17.1 1.18.0 1.19.0 1.2.0 1.20.0 1.20.1 1.3.0 1.3.1 1.4.0 1.5.0 1.6.0 All 67 releases
logtivity / Core / Admin / Logtivity_Response.php

Logtivity_Response.php in Activity Logs, User Activity Tracking, Multisite Activity Log from Logtivity 3.3.9, at Core/Admin/Logtivity_Response.php

98 lines 2.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * @package Logtivity
5 * @copyright 2025-2026 Logtivity. All rights reserved
6 * @contact logtivity.io, [email protected]
7 *
8 * This file is part of Logtivity
9 */
10
11 class Logtivity_Response
12 {
13 /**
14 * @var ?int
15 */
16 public ?int $code = null;
17
18 /**
19 * @var ?string
20 */
21 public ?string $message = null;
22
23 /**
24 * @var ?string
25 */
26 public ?string $error = null;
27
28 /**
29 * @var ?array
30 */
31 public ?array $body = null;
32
33 /**
34 * @var array
35 */
36 public array $request = [
37 'method' => 'POST',
38 'timeout' => 0.01,
39 'blocking' => false,
40 'redirection' => 5,
41 'httpversion' => '1.0',
42 'body' => null,
43 'cookies' => [],
44 ];
45
46 /**
47 * @var null|array|WP_Error
48 */
49 public $response = null;
50
51 public function __construct(string $apikey, string $url, array $request)
52 {
53 $this->request = array_merge(
54 $this->request,
55 [
56 'headers' => [
57 'Authorization' => 'Bearer ' . $apikey,
58 ],
59 ],
60 $request
61 );
62
63 $api = new Logtivity_Api();
64
65 /**
66 * This request must not be cached. Like Logtivity_Api::makeRequest(), it writes
67 * to the API rather than reading a cacheable resource, and defaults to a
68 * non-blocking 0.01s timeout.
69 */
70 $this->response = wp_remote_request($api->getEndpoint($url), $this->request);
71
72 $this->processResponse();
73 }
74
75 protected function processResponse(): void
76 {
77 if ($this->response instanceof WP_Error) {
78 $this->code = 500;
79 $this->message = $this->response->get_error_code();
80 $this->error = $this->response->get_error_message() ?: $this->response->get_error_code();
81 $this->body = get_object_vars($this->response);
82
83 } else {
84 $this->code = wp_remote_retrieve_response_code($this->response);
85 $this->message = wp_remote_retrieve_response_message($this->response);
86 $this->body = json_decode(wp_remote_retrieve_body($this->response), true);
87
88 if ($this->code < 400) {
89 $this->error = $responseBody['error'] ?? null;
90
91 } else {
92 $this->error = $this->message;
93 $this->message = (($this->body['message'] ?? $this->message) ?: 'Unknown error');
94 }
95 }
96 }
97 }
98