PluginProbe
Activity Logs, User Activity Tracking, Multisite Activity Log from Logtivity / 1.20.1
Activity Logs, User Activity Tracking, Multisite Activity Log from Logtivity v1.20.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 / Helpers / Logtivity_Wp_User.php

Logtivity_Wp_User.php in Activity Logs, User Activity Tracking, Multisite Activity Log from Logtivity 1.20.1, at Helpers/Logtivity_Wp_User.php

176 lines 2.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 class Logtivity_Wp_User
4 {
5 protected $user;
6
7 public function __construct($user = null, $field = 'ID')
8 {
9 if (is_null($user))
10 {
11 $this->user = wp_get_current_user();
12 }
13 elseif ($user instanceof WP_User)
14 {
15 $this->user = $user;
16 }
17 else
18 {
19 $this->user = get_user_by($field, $user);
20 }
21 }
22
23 public function findByUserMeta($field, $value)
24 {
25 $users = get_users([
26 'meta_key' => $field,
27 'meta_value' => $value
28 ]);
29
30 if (count($users))
31 {
32 $this->user = $users[0];
33 }
34 else
35 {
36 $this->user = false;
37 }
38
39 return $this;
40 }
41
42 public function id()
43 {
44 if ($this->user->ID == 0) {
45 return;
46 }
47
48 return $this->user->ID;
49 }
50
51 public function userLogin()
52 {
53 if ($this->user->ID == 0) {
54 return;
55 }
56
57 return $this->user->user_login;
58 }
59
60 public function email()
61 {
62 return $this->user->user_email;
63 }
64
65 public function name()
66 {
67 return $this->firstName() . ' ' . $this->lastName();
68 }
69
70 public function firstName()
71 {
72 return $this->meta('first_name');
73 }
74
75 public function lastName()
76 {
77 return $this->meta('last_name');
78 }
79
80 public function displayName()
81 {
82 return $this->user->display_name;
83 }
84
85 public function niceName()
86 {
87 return $this->user->user_nicename;
88 }
89
90 public function profileLink()
91 {
92 return add_query_arg( 'user_id', $this->id(), self_admin_url( 'user-edit.php' ) );
93 }
94
95 /**
96 * Get user meta
97 *
98 * @param string $meta_key
99 * @param boolean $returnString
100 * @return string/null
101 */
102 public function meta($meta_key, $returnString = true)
103 {
104 $meta = get_user_meta($this->user->ID, $meta_key, $returnString);
105
106 if ($meta != '') {
107 return $meta;
108 }
109
110 return null;
111 }
112
113 /**
114 * Does the fetched user exist
115 *
116 * @return boolean
117 */
118 public function exists()
119 {
120 return $this->user;
121 }
122
123 /**
124 * Is the user logged in
125 *
126 * @return boolean
127 */
128 public function isLoggedIn()
129 {
130 if ($this->user->ID == 0) {
131 return false;
132 }
133
134 return true;
135 }
136
137 /**
138 * Handles one user role. Useful for determining in more detail difference between employer and
139 * employer_premium for instance.
140 *
141 * @param string $role
142 * @return boolean
143 */
144 public function hasRole($role)
145 {
146 if ( in_array($role, $this->getRoles()) ) {
147 return true;
148 }
149
150 return false;
151 }
152
153 /**
154 * Get the users roles
155 *
156 * @return array
157 */
158 public function getRoles()
159 {
160 return $this->user->roles;
161 }
162
163 /**
164 * Get the users first role
165 *
166 * @return string|false
167 */
168 public function getRole()
169 {
170 foreach ($this->getRoles() as $role) {
171 return $role;
172 }
173 }
174
175 }
176