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

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