user = new Logtivity_Wp_User($user_id); parent::__construct(); } /** * Way into class. * * @param string $action * @param string $meta * @param string $user_id * @return Logtivity_Logger::send() */ public static function log($action = null, $meta = null, $user_id = null) { $Logtivity_logger = new Logtivity_Logger($user_id); if(is_null($action)) { return new $Logtivity_logger; } $Logtivity_logger->setAction($action); if ($meta) { $Logtivity_logger->addMeta($meta['key'], $meta['value']); } return $Logtivity_logger->send(); } /** * Set the action string beore sending * * @param string */ public function setAction($action) { $this->action = $action; return $this; } /** * Add to an array any additional information you would like to pass to this log. * * @param string $key * @param mixed $value * @return $this */ public function addMeta($key, $value) { $this->meta[$key] = $value; return $this; } /** * Add to an array of user meta you would like to pass to this log. * * @param string $key * @param mixed $value * @return $this */ public function addUserMeta($key, $value) { $this->userMeta[$key] = $value; return $this; } /** * Set whether this log is going to be posted asynchronously or not * * @param boolean $value * @return $this */ public function async($value = true) { $this->canAsync = $value; return $this; } /** * Stop this instance of Logtivity_Logger from logging * * @return $this */ public function stop() { $this->active = false; return $this; } /** * Send the logged data to Logtivity * * @return void */ public function send() { $this->maybeAddProfileLink(); do_action('wp_logtivity_instance', $this); if (!$this->active) { return; } return $this->makeRequest($this->getStoreUrl(), $this->getData()); } /** * Build the data array for storing the log * * @return array */ protected function getData() { return [ 'action' => $this->action, 'meta' => $this->getMeta(), 'user_id' => $this->getUserID(), 'username' => $this->maybeGetUsersUsername(), 'user_meta' => $this->getUserMeta(), 'ip_address' => $this->maybeGetUsersIp(), ]; } /** * Protected function to get the User ID if the user is logged in * * @return mixed string|integer */ protected function getUserID() { if (!$this->options->shouldStoreUserId()) { return; } if ($this->user->isLoggedIn()) { return $this->user->id(); } return; } /** * Maybe get the users IP address * * @return string|false */ protected function maybeGetUsersIp() { if (!$this->options->shouldStoreIp()) { return; } if ( ! empty( $_SERVER['HTTP_CLIENT_IP'] ) ) { //check ip from share internet return $_SERVER['HTTP_CLIENT_IP']; } elseif ( ! empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) { //to check ip is pass from proxy return $_SERVER['HTTP_X_FORWARDED_FOR']; } else { return $_SERVER['REMOTE_ADDR']; } } /** * Build the user meta array * * @return array */ public function getUserMeta() { return (array) apply_filters('wp_logtivity_get_user_meta', $this->userMeta); } /** * Build the meta array * * @return array */ public function getMeta() { return (array) apply_filters('wp_logtivity_get_meta', $this->meta); } /** * Maybe get the users profile link * * @return string|false */ protected function maybeAddProfileLink() { if (!$this->options->shouldStoreProfileLink()) { return; } if (!$this->user->isLoggedIn()) { return; } $profileLink = $this->user->profileLink(); if ($profileLink == '') { return null; } return $this->addUserMeta('Profile Link', $profileLink); } /** * Maybe get the users username * * @return string|false */ protected function maybeGetUsersUsername() { if (!$this->options->shouldStoreUsername()) { return null; } return $this->user->userLogin(); } }