PluginProbe
Activity Logs, User Activity Tracking, Multisite Activity Log from Logtivity / 1.6.0
Activity Logs, User Activity Tracking, Multisite Activity Log from Logtivity v1.6.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_Logger.php

Logtivity_Logger.php in Activity Logs, User Activity Tracking, Multisite Activity Log from Logtivity 1.6.0, at Services/Logtivity_Logger.php

338 lines 5.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 class Logtivity_Logger extends Logtivity_Log_API
4 {
5 /**
6 * Can this instance log something
7 *
8 * @var bool
9 */
10 public $active = true;
11
12 /**
13 * Logtivity_Wp_User
14 *
15 * @var object
16 */
17 public $user;
18
19 /**
20 * The action for the given log
21 *
22 * @var string
23 */
24 public $action;
25
26 /**
27 * The context for the given log. Could be a post title, or plugin
28 * name, or anything to help give this log some more context.
29 *
30 * @var string
31 */
32 public $context;
33
34 /**
35 * Extra info to pass to the log
36 *
37 * @var array
38 */
39 public $meta = [];
40
41 /**
42 * Extra user meta to pass to the log
43 *
44 * @var array
45 */
46 public $userMeta = [];
47
48 /**
49 * Set the user and call the parent constructor
50 */
51 public function __construct($user_id = null)
52 {
53 $this->setUser($user_id);
54
55 parent::__construct();
56 }
57
58 /**
59 * Way into class.
60 *
61 * @param string $action
62 * @param string $meta
63 * @param string $user_id
64 * @return Logtivity_Logger::send()
65 */
66 public static function log($action = null, $meta = null, $user_id = null)
67 {
68 $Logtivity_logger = new Logtivity_Logger($user_id);
69
70 if(is_null($action)) {
71
72 return new $Logtivity_logger;
73
74 }
75
76 $Logtivity_logger->setAction($action);
77
78 if ($meta) {
79 $Logtivity_logger->addMeta($meta['key'], $meta['value']);
80 }
81
82 return $Logtivity_logger->send();
83 }
84
85 /**
86 * Set the user for the current log instance
87 *
88 * @param integer $user_id
89 */
90 public function setUser($user_id)
91 {
92 $this->user = new Logtivity_Wp_User($user_id);
93
94 return $this;
95 }
96
97 /**
98 * Set the action string before sending
99 *
100 * @param string
101 */
102 public function setAction($action)
103 {
104 $this->action = $action;
105
106 return $this;
107 }
108
109 /**
110 * Set the context string before sending.
111 *
112 * @param string
113 */
114 public function setContext($context)
115 {
116 $this->context = $context;
117
118 return $this;
119 }
120
121 /**
122 * Add to an array any additional information you would like to pass to this log.
123 *
124 * @param string $key
125 * @param mixed $value
126 * @return $this
127 */
128 public function addMeta($key, $value)
129 {
130 $this->meta[] = [
131 'key' => $key,
132 'value' => $value,
133 ];
134
135 return $this;
136 }
137
138 /**
139 * Add to an array of user meta you would like to pass to this log.
140 *
141 * @param string $key
142 * @param mixed $value
143 * @return $this
144 */
145 public function addUserMeta($key, $value)
146 {
147 $this->userMeta[$key] = $value;
148
149 return $this;
150 }
151
152 /**
153 * Should we wait and record the response from logtivity.
154 *
155 * @return $this
156 */
157 public function waitForResponse()
158 {
159 $this->waitForResponse = true;
160
161 return $this;
162 }
163
164 /**
165 * Stop this instance of Logtivity_Logger from logging
166 *
167 * @return $this
168 */
169 public function stop()
170 {
171 $this->active = false;
172
173 return $this;
174 }
175
176 /**
177 * Send the logged data to Logtivity
178 *
179 * @return void
180 */
181 public function send()
182 {
183 $this->maybeAddProfileLink();
184
185 do_action('wp_logtivity_instance', $this);
186
187 if (!$this->active) {
188 return;
189 }
190
191 return $this->makeRequest($this->getEndpoint('/logs/store'), $this->getData());
192 }
193
194 /**
195 * Build the data array for storing the log
196 *
197 * @return array
198 */
199 protected function getData()
200 {
201 return [
202 'action' => $this->action,
203 'context' => $this->context,
204 'meta' => $this->getMeta(),
205 'user_id' => $this->getUserID(),
206 'username' => $this->maybeGetUsersUsername(),
207 'user_meta' => $this->getUserMeta(),
208 'ip_address' => $this->maybeGetUsersIp(),
209 ];
210 }
211
212 /**
213 * Protected function to get the User ID if the user is logged in
214 *
215 * @return mixed string|integer
216 */
217 protected function getUserID()
218 {
219 if (!$this->options->shouldStoreUserId()) {
220 return;
221 }
222
223 if (!$this->user->isLoggedIn()) {
224 return;
225 }
226
227 return $this->user->id();
228 }
229
230 /**
231 * Maybe get the users IP address
232 *
233 * @return string|false
234 */
235 protected function maybeGetUsersIp()
236 {
237 if (!$this->options->shouldStoreIp()) {
238 return;
239 }
240
241 if ( ! empty( $_SERVER['HTTP_CLIENT_IP'] ) ) {
242
243 //check ip from share internet
244 return $_SERVER['HTTP_CLIENT_IP'];
245
246 } elseif ( ! empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) {
247
248 //to check ip is pass from proxy
249 return $_SERVER['HTTP_X_FORWARDED_FOR'];
250
251 } else {
252
253 return $_SERVER['REMOTE_ADDR'];
254
255 }
256 }
257
258 /**
259 * Build the user meta array
260 *
261 * @return array
262 */
263 public function getUserMeta()
264 {
265 return (array) apply_filters('wp_logtivity_get_user_meta', $this->userMeta);
266 }
267
268 /**
269 * Build the meta array
270 *
271 * @return array
272 */
273 public function getMeta()
274 {
275 try {
276 $this->addMeta(
277 'URL',
278 sanitize_text_field(
279 (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"
280 )
281 );
282
283 if (isset($_SERVER['REQUEST_METHOD'])) {
284 $this->addMeta(
285 'Request',
286 sanitize_text_field($_SERVER['REQUEST_METHOD'])
287 );
288 }
289 } catch (\Exception $e) {
290 }
291
292 return (array) apply_filters('wp_logtivity_get_meta', $this->meta);
293 }
294
295 /**
296 * Maybe get the users profile link
297 *
298 * @return string|false
299 */
300 protected function maybeAddProfileLink()
301 {
302 if (!$this->options->shouldStoreProfileLink()) {
303 return;
304 }
305
306 if (!$this->user->isLoggedIn()) {
307 return;
308 }
309
310 $profileLink = $this->user->profileLink();
311
312 if ($profileLink == '') {
313 return null;
314 }
315
316 return $this->addUserMeta('Profile Link', $profileLink);
317 }
318
319 /**
320 * Maybe get the users username
321 *
322 * @return string|false
323 */
324 protected function maybeGetUsersUsername()
325 {
326 if (!$this->options->shouldStoreUsername()) {
327 return null;
328 }
329
330 if (!$this->user->isLoggedIn()) {
331 return;
332 }
333
334 return $this->user->userLogin();
335 }
336
337 }
338