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

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