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