PluginProbe
Activity Logs, User Activity Tracking, Multisite Activity Log from Logtivity / 1.14.0
Activity Logs, User Activity Tracking, Multisite Activity Log from Logtivity v1.14.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.14.0, at Services/Logtivity_Logger.php

385 lines 6.2 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_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 * The post type, if relevant for a given log
36 *
37 * @var string
38 */
39 public $post_type;
40
41 /**
42 * The post ID, if relevant for a given log
43 *
44 * @var integer
45 */
46 public $post_id;
47
48 /**
49 * Extra info to pass to the log
50 *
51 * @var array
52 */
53 public $meta = [];
54
55 /**
56 * Extra user meta to pass to the log
57 *
58 * @var array
59 */
60 public $userMeta = [];
61
62 /**
63 * When storing a log, generally we want to do this asynchronously
64 * and so we won't wait for a response from the API by default.
65 *
66 * @var boolean
67 */
68 public $waitForResponse = false;
69
70 /**
71 * Set the user and call the parent constructor
72 */
73 public function __construct($user_id = null)
74 {
75 $this->setUser($user_id);
76
77 parent::__construct();
78 }
79
80 /**
81 * Way into class.
82 *
83 * @param string $action
84 * @param string $meta
85 * @param string $user_id
86 * @return Logtivity_Logger::send()
87 */
88 public static function log($action = null, $meta = null, $user_id = null)
89 {
90 $Logtivity_logger = new Logtivity_Logger($user_id);
91
92 if(is_null($action)) {
93
94 return new $Logtivity_logger;
95
96 }
97
98 $Logtivity_logger->setAction($action);
99
100 if ($meta) {
101 $Logtivity_logger->addMeta($meta['key'], $meta['value']);
102 }
103
104 return $Logtivity_logger->send();
105 }
106
107 /**
108 * Set the user for the current log instance
109 *
110 * @param integer $user_id
111 */
112 public function setUser($user_id)
113 {
114 $this->user = new Logtivity_Wp_User($user_id);
115
116 return $this;
117 }
118
119 /**
120 * Set the action string before sending
121 *
122 * @param string
123 */
124 public function setAction($action)
125 {
126 $this->action = $action;
127
128 return $this;
129 }
130
131 /**
132 * Set the context string before sending.
133 *
134 * @param string
135 */
136 public function setContext($context)
137 {
138 $this->context = $context;
139
140 return $this;
141 }
142
143 /**
144 * Set the post_type string before sending.
145 *
146 * @param string
147 */
148 public function setPostType($post_type)
149 {
150 $this->post_type = $post_type;
151
152 return $this;
153 }
154
155 /**
156 * Set the post_id before sending.
157 *
158 * @param integer
159 */
160 public function setPostId($post_id)
161 {
162 $this->post_id = $post_id;
163
164 return $this;
165 }
166
167 /**
168 * Add to an array any additional information you would like to pass to this log.
169 *
170 * @param string $key
171 * @param mixed $value
172 * @return $this
173 */
174 public function addMeta($key, $value)
175 {
176 $this->meta[] = [
177 'key' => $key,
178 'value' => $value,
179 ];
180
181 return $this;
182 }
183
184 /**
185 * Add the meta if the first condition is true
186 *
187 * @param boolean $condition
188 * @param string $key
189 * @param mixed $value
190 */
191 public function addMetaIf($condition = false, $key, $value)
192 {
193 if ($condition) {
194 $this->addMeta($key, $value);
195 }
196
197 return $this;
198 }
199
200 /**
201 * Add to an array of user meta you would like to pass to this log.
202 *
203 * @param string $key
204 * @param mixed $value
205 * @return $this
206 */
207 public function addUserMeta($key, $value)
208 {
209 $this->userMeta[$key] = $value;
210
211 return $this;
212 }
213
214 /**
215 * Should we wait and record the response from logtivity.
216 *
217 * @return $this
218 */
219 public function waitForResponse()
220 {
221 $this->waitForResponse = true;
222
223 return $this;
224 }
225
226 /**
227 * Stop this instance of Logtivity_Logger from logging
228 *
229 * @return $this
230 */
231 public function stop()
232 {
233 $this->active = false;
234
235 return $this;
236 }
237
238 /**
239 * Send the logged data to Logtivity
240 *
241 * @return void
242 */
243 public function send()
244 {
245 $this->maybeAddProfileLink();
246
247 do_action('wp_logtivity_instance', $this);
248
249 if (!$this->active) {
250 return;
251 }
252
253 return $this->makeRequest('/logs/store', $this->getData());
254 }
255
256 /**
257 * Build the data array for storing the log
258 *
259 * @return array
260 */
261 protected function getData()
262 {
263 return [
264 'action' => $this->action,
265 'context' => $this->context,
266 'post_type' => $this->post_type,
267 'post_id' => $this->post_id,
268 'meta' => $this->getMeta(),
269 'user_id' => $this->getUserID(),
270 'username' => $this->maybeGetUsersUsername(),
271 'user_meta' => $this->getUserMeta(),
272 'ip_address' => $this->maybeGetUsersIp(),
273 ];
274 }
275
276 /**
277 * Protected function to get the User ID if the user is logged in
278 *
279 * @return mixed string|integer
280 */
281 protected function getUserID()
282 {
283 if (!$this->options->shouldStoreUserId()) {
284 return;
285 }
286
287 if (!$this->user->isLoggedIn()) {
288 return;
289 }
290
291 return $this->user->id();
292 }
293
294 /**
295 * Maybe get the users IP address
296 *
297 * @return string|false
298 */
299 protected function maybeGetUsersIp()
300 {
301 if (!$this->options->shouldStoreIp()) {
302 return;
303 }
304
305 if ( ! empty( $_SERVER['HTTP_CLIENT_IP'] ) ) {
306
307 //check ip from share internet
308 return $_SERVER['HTTP_CLIENT_IP'];
309
310 } elseif ( ! empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) {
311
312 //to check ip is pass from proxy
313 return $_SERVER['HTTP_X_FORWARDED_FOR'];
314
315 } else {
316
317 return $_SERVER['REMOTE_ADDR'];
318
319 }
320 }
321
322 /**
323 * Build the user meta array
324 *
325 * @return array
326 */
327 public function getUserMeta()
328 {
329 return (array) apply_filters('wp_logtivity_get_user_meta', $this->userMeta);
330 }
331
332 /**
333 * Build the meta array
334 *
335 * @return array
336 */
337 public function getMeta()
338 {
339 return (array) apply_filters('wp_logtivity_get_meta', $this->meta);
340 }
341
342 /**
343 * Maybe get the users profile link
344 *
345 * @return string|false
346 */
347 protected function maybeAddProfileLink()
348 {
349 if (!$this->options->shouldStoreProfileLink()) {
350 return;
351 }
352
353 if (!$this->user->isLoggedIn()) {
354 return;
355 }
356
357 $profileLink = $this->user->profileLink();
358
359 if ($profileLink == '') {
360 return null;
361 }
362
363 return $this->addUserMeta('Profile Link', $profileLink);
364 }
365
366 /**
367 * Maybe get the users username
368 *
369 * @return string|false
370 */
371 protected function maybeGetUsersUsername()
372 {
373 if (!$this->options->shouldStoreUsername()) {
374 return null;
375 }
376
377 if (!$this->user->isLoggedIn()) {
378 return;
379 }
380
381 return $this->user->userLogin();
382 }
383
384 }
385