PluginProbe
Activity Logs, User Activity Tracking, Multisite Activity Log from Logtivity / 1.11.0
Activity Logs, User Activity Tracking, Multisite Activity Log from Logtivity v1.11.0
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 / Services / Logtivity_Api.php

Logtivity_Api.php in Activity Logs, User Activity Tracking, Multisite Activity Log from Logtivity 1.11.0, at Services/Logtivity_Api.php

135 lines 2.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 class Logtivity_Api
4 {
5 /**
6 * Option class to access the plugin settings
7 *
8 * @var object
9 */
10 protected $options;
11
12 /**
13 * Should we wait to return the response from the API?
14 *
15 * @var boolean
16 */
17 public $waitForResponse = true;
18
19 /**
20 * The API key for either the site or team
21 *
22 * @var string
23 */
24 public $api_key;
25
26 public function __construct()
27 {
28 $this->options = new Logtivity_Options;
29 }
30
31 /**
32 * Get the API URL for the Logtivity endpoint
33 *
34 * @return string
35 */
36 public function getEndpoint($endpoint)
37 {
38 return logtivity_get_api_url() . $endpoint;
39 }
40
41 public function post($url, $body)
42 {
43 return $this->makeRequest($url, $body, 'POST');
44 }
45
46 public function get($url, $body)
47 {
48 return $this->makeRequest($url, $body, 'GET');
49 }
50
51 public function setApiKey($api_key)
52 {
53 $this->api_key = $api_key;
54
55 return $this;
56 }
57
58 /**
59 * Make a request to the Logtivity API
60 *
61 * @param string $url
62 * @param array $body
63 * @param string $method
64 * @return mixed $response
65 */
66 public function makeRequest($url, $body, $method = 'POST')
67 {
68 if (!$this->api_key) {
69 $this->api_key = logtivity_get_api_key();
70 }
71
72 if (!$this->api_key) {
73 return;
74 }
75
76 if (!$this->options->urlHash()) {
77 $this->options->update(['logtivity_url_hash' => home_url()], false);
78 }
79
80 if (logtivity_has_site_url_changed()) {
81 return;
82 }
83
84 $shouldLogLatestResponse = $this->waitForResponse || $this->options->shouldLogLatestResponse();
85
86 $response = wp_remote_post($this->getEndpoint($url), [
87 'method' => $method,
88 'timeout' => ( $shouldLogLatestResponse ? 45 : 0.01),
89 'blocking' => ( $shouldLogLatestResponse ? true : false),
90 'redirection' => 5,
91 'httpversion' => '1.0',
92 'headers' => [
93 'Authorization' => 'Bearer '.$this->api_key
94 ],
95 'body' => $body,
96 'cookies' => array()
97 ]);
98
99 $response = wp_remote_retrieve_body($response);
100
101 if ($shouldLogLatestResponse && $this->notUpdatingWidgetInCustomizer() && $method === 'POST') {
102
103 $this->options->update([
104 'logtivity_latest_response' => [
105 'date' => date("Y-m-d H:i:s"),
106 'response' => print_r($response, true)
107 ]
108 ],
109 false
110 );
111
112 }
113
114 return $response;
115 }
116
117 /**
118 * You cannot call an extra update_option during a widget update so we make
119 * sure not to log the most recent log response in this case.
120 *
121 * @return bool
122 */
123 private function notUpdatingWidgetInCustomizer()
124 {
125 if (!isset($_POST['wp_customize'])) {
126 return true;
127 }
128
129 if (!isset($_POST['action'])) {
130 return true;
131 }
132
133 return ! ($_POST['action'] === 'update-widget' && $_POST['wp_customize'] === 'on');
134 }
135 }