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

315 lines 5.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_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 * Allow the overriding of async posting on a per log basis. Gives us the ability
50 * to turn it off for certain logs that don't support async. eg. logout
51 *
52 * @var boolean
53 */
54 public $canAsync = true;
55
56 /**
57 * Set the user and call the parent constructor
58 */
59 public function __construct($user_id = null)
60 {
61 $this->user = new Logtivity_Wp_User($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 action string before sending
95 *
96 * @param string
97 */
98 public function setAction($action)
99 {
100 $this->action = $action;
101
102 return $this;
103 }
104
105 /**
106 * Set the context string before sending.
107 *
108 * @param string
109 */
110 public function setContext($context)
111 {
112 $this->context = $context;
113
114 return $this;
115 }
116
117 /**
118 * Add to an array any additional information you would like to pass to this log.
119 *
120 * @param string $key
121 * @param mixed $value
122 * @return $this
123 */
124 public function addMeta($key, $value)
125 {
126 $this->meta[$key] = $value;
127
128 return $this;
129 }
130
131 /**
132 * Add to an array of user meta you would like to pass to this log.
133 *
134 * @param string $key
135 * @param mixed $value
136 * @return $this
137 */
138 public function addUserMeta($key, $value)
139 {
140 $this->userMeta[$key] = $value;
141
142 return $this;
143 }
144
145 /**
146 * Set whether this log is going to be posted asynchronously or not
147 *
148 * @param boolean $value
149 * @return $this
150 */
151 public function async($value = true)
152 {
153 $this->canAsync = $value;
154
155 return $this;
156 }
157
158 /**
159 * Stop this instance of Logtivity_Logger from logging
160 *
161 * @return $this
162 */
163 public function stop()
164 {
165 $this->active = false;
166
167 return $this;
168 }
169
170 /**
171 * Send the logged data to Logtivity
172 *
173 * @return void
174 */
175 public function send()
176 {
177 $this->maybeAddProfileLink();
178
179 do_action('wp_logtivity_instance', $this);
180
181 if (!$this->active) {
182 return;
183 }
184
185 return $this->makeRequest($this->getStoreUrl(), $this->getData());
186 }
187
188 /**
189 * Build the data array for storing the log
190 *
191 * @return array
192 */
193 protected function getData()
194 {
195 return [
196 'action' => $this->action,
197 'context' => $this->context,
198 'meta' => $this->getMeta(),
199 'user_id' => $this->getUserID(),
200 'username' => $this->maybeGetUsersUsername(),
201 'user_meta' => $this->getUserMeta(),
202 'ip_address' => $this->maybeGetUsersIp(),
203 ];
204 }
205
206 /**
207 * Protected function to get the User ID if the user is logged in
208 *
209 * @return mixed string|integer
210 */
211 protected function getUserID()
212 {
213 if (!$this->options->shouldStoreUserId()) {
214 return;
215 }
216
217 if (!$this->user->isLoggedIn()) {
218 return;
219 }
220
221 return $this->user->id();
222 }
223
224 /**
225 * Maybe get the users IP address
226 *
227 * @return string|false
228 */
229 protected function maybeGetUsersIp()
230 {
231 if (!$this->options->shouldStoreIp()) {
232 return;
233 }
234
235 if ( ! empty( $_SERVER['HTTP_CLIENT_IP'] ) ) {
236
237 //check ip from share internet
238 return $_SERVER['HTTP_CLIENT_IP'];
239
240 } elseif ( ! empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) {
241
242 //to check ip is pass from proxy
243 return $_SERVER['HTTP_X_FORWARDED_FOR'];
244
245 } else {
246
247 return $_SERVER['REMOTE_ADDR'];
248
249 }
250 }
251
252 /**
253 * Build the user meta array
254 *
255 * @return array
256 */
257 public function getUserMeta()
258 {
259 return (array) apply_filters('wp_logtivity_get_user_meta', $this->userMeta);
260 }
261
262 /**
263 * Build the meta array
264 *
265 * @return array
266 */
267 public function getMeta()
268 {
269 return (array) apply_filters('wp_logtivity_get_meta', $this->meta);
270 }
271
272 /**
273 * Maybe get the users profile link
274 *
275 * @return string|false
276 */
277 protected function maybeAddProfileLink()
278 {
279 if (!$this->options->shouldStoreProfileLink()) {
280 return;
281 }
282
283 if (!$this->user->isLoggedIn()) {
284 return;
285 }
286
287 $profileLink = $this->user->profileLink();
288
289 if ($profileLink == '') {
290 return null;
291 }
292
293 return $this->addUserMeta('Profile Link', $profileLink);
294 }
295
296 /**
297 * Maybe get the users username
298 *
299 * @return string|false
300 */
301 protected function maybeGetUsersUsername()
302 {
303 if (!$this->options->shouldStoreUsername()) {
304 return null;
305 }
306
307 if (!$this->user->isLoggedIn()) {
308 return;
309 }
310
311 return $this->user->userLogin();
312 }
313
314 }
315