PluginProbe ʕ •ᴥ•ʔ
Advanced Access Manager – Access Governance for WordPress / 4.0
Advanced Access Manager – Access Governance for WordPress v4.0
6.8.4 6.8.5 6.9.0 6.9.1 6.9.10 6.9.11 6.9.12 6.9.13 6.9.14 6.9.15 6.9.16 6.9.17 6.9.18 6.9.19 6.9.2 6.9.20 6.9.21 6.9.22 6.9.23 6.9.24 6.9.25 6.9.26 6.9.27 6.9.28 6.9.29 6.9.3 6.9.30 6.9.31 6.9.32 6.9.33 6.9.34 6.9.35 6.9.36 6.9.37 6.9.38 6.9.39 6.9.4 6.9.41 6.9.42 6.9.43 6.9.44 6.9.45 6.9.46 6.9.47 6.9.48 6.9.49 6.9.5 6.9.51 6.9.6 6.9.7 6.9.8 6.9.9 7.0.0 7.0.0-alpha.6 7.0.0-alpha.7 7.0.0-beta.1 7.0.0-rc1 7.0.0-rc2 7.0.0-rc3 7.0.1 7.0.10 7.0.11 7.0.2 7.0.3 7.0.4 7.0.5 7.0.6 7.0.7 7.0.8 7.0.9 7.1.0 7.1.1 trunk 3.0 4.0 4.0.1 4.1 4.2 4.3 4.4 4.4.1 4.5 4.6 4.6.1 4.6.2 4.7 4.7.1 4.7.2 4.7.5 4.7.6 4.8 4.8.1 4.9 4.9.1 4.9.2 4.9.3 4.9.4 4.9.5 4.9.5.1 4.9.5.2 5.0 5.0.1 5.0.2 5.0.3 5.0.4 5.0.5 5.0.6 5.0.7 5.0.8 5.1 5.1.1 5.10 5.11 5.2 5.2.1 5.2.5 5.2.6 5.2.7 5.3 5.3.1 5.3.2 5.3.3 5.3.4 5.3.5 5.4 5.4.1 5.4.2 5.4.3 5.4.3.1 5.4.3.2 5.5 5.5.1 5.5.2 5.6 5.6.1 5.6.1.1 5.7 5.7.1 5.7.2 5.7.3 5.8 5.8.1 5.8.2 5.8.3 5.9 5.9.1 5.9.1.1 5.9.2 5.9.2.1 5.9.3 5.9.4 5.9.5 5.9.6 5.9.6.1 5.9.6.2 5.9.6.3 5.9.7 5.9.7.1 5.9.7.2 5.9.7.3 5.9.8 5.9.8.1 5.9.9 5.9.9.1 6.0.0 6.0.1 6.0.2 6.0.3 6.0.4 6.0.5 6.1.0 6.1.1 6.2.0 6.2.1 6.2.2 6.3.0 6.3.1 6.3.2 6.3.3 6.4.0 6.4.1 6.4.2 6.4.3 6.5.0 6.5.1 6.5.2 6.5.3 6.5.4 6.6.0 6.6.1 6.6.2 6.6.3 6.6.4 6.7.0 6.7.1 6.7.2 6.7.3 6.7.4 6.7.5 6.7.6 6.7.7 6.7.8 6.7.9 6.8.0 6.8.1 6.8.2 6.8.3
advanced-access-manager / Application / Core / Subject / User.php
advanced-access-manager / Application / Core / Subject Last commit date
Default.php 9 years ago Role.php 9 years ago User.php 9 years ago Visitor.php 9 years ago
User.php
257 lines
1 <?php
2
3 /**
4 * ======================================================================
5 * LICENSE: This file is subject to the terms and conditions defined in *
6 * file 'license.txt', which is part of this source code package. *
7 * ======================================================================
8 */
9
10 /**
11 * User subject
12 *
13 * @package AAM
14 * @author Vasyl Martyniuk <vasyl@vasyltech.com>
15 */
16 class AAM_Core_Subject_User extends AAM_Core_Subject {
17
18 /**
19 * Subject UID: USER
20 */
21 const UID = 'user';
22
23 /**
24 * AAM Capability Key
25 *
26 * It is very important to have all user capability changes be stored in
27 * seperate options from the wp_capabilities usermeta cause if AAM is not
28 * active as a plugin, it reverts back to the default WordPress settings
29 */
30 const AAM_CAPKEY = 'aam_capability';
31
32 /**
33 * Block User
34 *
35 * @return boolean
36 *
37 * @access public
38 * @global wpdb $wpdb
39 */
40 public function block() {
41 global $wpdb;
42
43 $response = false;
44 if (current_user_can('edit_users')) {
45 $status = ($this->getSubject()->user_status ? 0 : 1);
46 $result = $wpdb->update(
47 $wpdb->users,
48 array('user_status' => $status),
49 array('ID' => $this->getId())
50 );
51 if ($result) {
52 $this->getSubject()->user_status = $status;
53 clean_user_cache($this->getSubject());
54 $response = true;
55 }
56 }
57
58 return $response;
59 }
60
61 /**
62 * Retrieve User based on ID
63 *
64 * @return WP_Role
65 *
66 * @access protected
67 */
68 protected function retrieveSubject() {
69 $subject = new WP_User($this->getId());
70
71 //retrieve aam capabilities if are not retrieved yet
72 $caps = get_user_option(self::AAM_CAPKEY, $this->getId());
73 if (is_array($caps)) {
74 $caps = array_merge($subject->caps, $caps);
75 $allcaps = array_merge($subject->allcaps, $caps);
76
77 //reset the user capabilities
78 $subject->allcaps = $allcaps;
79 $subject->caps = $caps;
80 }
81
82 return $subject;
83 }
84
85 /**
86 * Get user capabilities
87 *
88 * @return array
89 *
90 * @access public
91 */
92 public function getCapabilities() {
93 return $this->getSubject()->allcaps;
94 }
95
96 /**
97 * Check if user has a capability
98 *
99 * @param string $capability
100 *
101 * @return boolean
102 *
103 * @access public
104 */
105 public function hasCapability($capability) {
106 return user_can($this->getSubject(), $capability);
107 }
108
109 /**
110 * Add capability
111 *
112 * @param string $capability
113 *
114 * @return boolean
115 *
116 * @access public
117 */
118 public function addCapability($capability) {
119 //check if user is capable to have this capability
120 $map = call_user_func_array(
121 'map_meta_cap', array($capability, $this->getSubject()->ID)
122 );
123 if (!in_array('do_not_allow', $map)) {
124 $response = $this->updateCapability($capability, true);
125 } else {
126 $response = false;
127 }
128
129 return $response;
130 }
131
132 /**
133 * Remove Capability
134 *
135 * @param string $capability
136 *
137 * @return boolean
138 *
139 * @access public
140 */
141 public function removeCapability($capability) {
142 return $this->updateCapability($capability, false);
143 }
144
145 /**
146 * Update User's Capability Set
147 *
148 * @param string $capability
149 * @param boolean $grand
150 *
151 * @return boolean
152 *
153 * @access public
154 */
155 public function updateCapability($capability, $grand) {
156 //update capability
157 $caps = $this->getSubject()->caps;
158 $caps[$capability] = $grand;
159
160 //save and return the result of operation
161 return update_user_option($this->getId(), self::AAM_CAPKEY, $caps);
162 }
163
164 /**
165 * Update user's option
166 *
167 * @param mixed $value
168 * @param string $object
169 * @param string $id
170 *
171 * @return boolean
172 *
173 * @access public
174 */
175 public function updateOption($value, $object, $id = 0) {
176 return update_user_option(
177 $this->getId(), $this->getOptionName($object, $id), $value
178 );
179 }
180
181 /**
182 * Read user's option
183 *
184 * @param string $object
185 * @param string $id
186 *
187 * @return mixed
188 *
189 * @access public
190 */
191 public function readOption($object, $id = '') {
192 return get_user_option(
193 $this->getOptionName($object, $id), $this->getId()
194 );
195 }
196
197 /**
198 * Read user's option
199 *
200 * @param string $object
201 * @param string $id
202 *
203 * @return mixed
204 *
205 * @access public
206 */
207 public function deleteOption($object, $id = 0) {
208 return delete_user_option(
209 $this->getId(), $this->getOptionName($object, $id)
210 );
211 }
212
213 /**
214 * @inheritdoc
215 */
216 public function getParent() {
217 //try to get this option from the User's Role
218 $roles = $this->getSubject()->roles;
219 //first user role is counted only. AAM does not support multi-roles
220 $parent = array_shift($roles);
221
222 //in case of multisite & current user does not belong to the site
223 if ($parent) {
224 $role = new AAM_Core_Subject_Role($parent);
225 } else {
226 $role = null;
227 }
228
229 return $role;
230 }
231
232 /**
233 * Prepare option's name
234 *
235 * @param string $object
236 * @param string|int $id
237 *
238 * @return string
239 *
240 * @access public
241 */
242 public function getOptionName($object, $id) {
243 return "aam_{$object}" . ($id ? "_{$id}" : '');
244 }
245
246 /**
247 * Get Subject UID
248 *
249 * @return string
250 *
251 * @access public
252 */
253 public function getUID() {
254 return self::UID;
255 }
256
257 }