User.php
336 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 | * separate 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 | * |
| 34 | * @var type |
| 35 | */ |
| 36 | protected $parent = null; |
| 37 | |
| 38 | /** |
| 39 | * |
| 40 | * @param type $id |
| 41 | */ |
| 42 | public function __construct($id) { |
| 43 | parent::__construct($id); |
| 44 | |
| 45 | if (get_current_user_id() == $id) { |
| 46 | //check if user is locked |
| 47 | if ($this->user_status == 1) { |
| 48 | wp_logout(); |
| 49 | } |
| 50 | |
| 51 | //check if user's role expired |
| 52 | $expire = get_user_option('aam-role-expires'); |
| 53 | if ($expire && ($expire <= time())) { |
| 54 | $this->restoreRoles(); |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Block User |
| 61 | * |
| 62 | * @return boolean |
| 63 | * |
| 64 | * @access public |
| 65 | * @global wpdb $wpdb |
| 66 | */ |
| 67 | public function block() { |
| 68 | global $wpdb; |
| 69 | |
| 70 | $response = false; |
| 71 | if (current_user_can('edit_users')) { |
| 72 | $status = ($this->getSubject()->user_status ? 0 : 1); |
| 73 | $result = $wpdb->update( |
| 74 | $wpdb->users, |
| 75 | array('user_status' => $status), |
| 76 | array('ID' => $this->getId()) |
| 77 | ); |
| 78 | if ($result) { |
| 79 | $this->getSubject()->user_status = $status; |
| 80 | clean_user_cache($this->getSubject()); |
| 81 | $response = true; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | return $response; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * |
| 90 | */ |
| 91 | public function restoreRoles() { |
| 92 | $roles = get_user_option('aam-original-roles'); |
| 93 | |
| 94 | //remove curren roles |
| 95 | foreach((array) $this->roles as $role) { |
| 96 | $this->remove_role($role); |
| 97 | } |
| 98 | |
| 99 | //add original roles |
| 100 | foreach(($roles ? $roles : array('subscriber')) as $role) { |
| 101 | $this->add_role($role); |
| 102 | } |
| 103 | |
| 104 | //delete options |
| 105 | delete_user_option($this->getId(), 'aam-role-expires'); |
| 106 | delete_user_option($this->getId(), 'aam-original-roles'); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Retrieve User based on ID |
| 111 | * |
| 112 | * @return WP_Role |
| 113 | * |
| 114 | * @access protected |
| 115 | */ |
| 116 | protected function retrieveSubject() { |
| 117 | $subject = new WP_User($this->getId()); |
| 118 | |
| 119 | //retrieve aam capabilities if are not retrieved yet |
| 120 | $caps = get_user_option(self::AAM_CAPKEY, $this->getId()); |
| 121 | if (is_array($caps)) { |
| 122 | $caps = array_merge($subject->caps, $caps); |
| 123 | $allcaps = array_merge($subject->allcaps, $caps); |
| 124 | |
| 125 | //reset the user capabilities |
| 126 | $subject->allcaps = $allcaps; |
| 127 | $subject->caps = $caps; |
| 128 | |
| 129 | if (wp_get_current_user()->ID == $subject->ID) { |
| 130 | wp_get_current_user()->allcaps = $allcaps; |
| 131 | wp_get_current_user()->caps = $caps; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | return $subject; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Get user capabilities |
| 140 | * |
| 141 | * @return array |
| 142 | * |
| 143 | * @access public |
| 144 | */ |
| 145 | public function getCapabilities() { |
| 146 | return $this->getSubject()->allcaps; |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Check if user has a capability |
| 151 | * |
| 152 | * @param string $capability |
| 153 | * |
| 154 | * @return boolean |
| 155 | * |
| 156 | * @access public |
| 157 | */ |
| 158 | public function hasCapability($capability) { |
| 159 | return user_can($this->getSubject(), $capability); |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Add capability |
| 164 | * |
| 165 | * @param string $capability |
| 166 | * |
| 167 | * @return boolean |
| 168 | * |
| 169 | * @access public |
| 170 | */ |
| 171 | public function addCapability($capability) { |
| 172 | return $this->updateCapability($capability, true); |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Remove Capability |
| 177 | * |
| 178 | * @param string $capability |
| 179 | * |
| 180 | * @return boolean |
| 181 | * |
| 182 | * @access public |
| 183 | */ |
| 184 | public function removeCapability($capability) { |
| 185 | return $this->updateCapability($capability, false); |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Update User's Capability Set |
| 190 | * |
| 191 | * @param string $capability |
| 192 | * @param boolean $grand |
| 193 | * |
| 194 | * @return boolean |
| 195 | * |
| 196 | * @access public |
| 197 | */ |
| 198 | public function updateCapability($capability, $grand) { |
| 199 | //update capability |
| 200 | $caps = $this->getSubject()->caps; |
| 201 | $caps[$capability] = $grand; |
| 202 | |
| 203 | //save and return the result of operation |
| 204 | return update_user_option($this->getId(), self::AAM_CAPKEY, $caps); |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Undocumented function |
| 209 | * |
| 210 | * @param string $object |
| 211 | * @return void |
| 212 | */ |
| 213 | public function resetObject($object) { |
| 214 | if ($object == 'capability') { |
| 215 | $result = delete_user_option($this->getId(), self::AAM_CAPKEY); |
| 216 | } else { |
| 217 | $result = $this->deleteOption($object); |
| 218 | } |
| 219 | |
| 220 | return $result; |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Update user's option |
| 225 | * |
| 226 | * @param mixed $value |
| 227 | * @param string $object |
| 228 | * @param string $id |
| 229 | * |
| 230 | * @return boolean |
| 231 | * |
| 232 | * @access public |
| 233 | */ |
| 234 | public function updateOption($value, $object, $id = 0) { |
| 235 | return update_user_option( |
| 236 | $this->getId(), $this->getOptionName($object, $id), $value |
| 237 | ); |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * Read user's option |
| 242 | * |
| 243 | * @param string $object |
| 244 | * @param string $id |
| 245 | * |
| 246 | * @return mixed |
| 247 | * |
| 248 | * @access public |
| 249 | */ |
| 250 | public function readOption($object, $id = '') { |
| 251 | return get_user_option( |
| 252 | $this->getOptionName($object, $id), $this->getId() |
| 253 | ); |
| 254 | } |
| 255 | |
| 256 | /** |
| 257 | * Read user's option |
| 258 | * |
| 259 | * @param string $object |
| 260 | * @param string $id |
| 261 | * |
| 262 | * @return mixed |
| 263 | * |
| 264 | * @access public |
| 265 | */ |
| 266 | public function deleteOption($object, $id = 0) { |
| 267 | return delete_user_option( |
| 268 | $this->getId(), $this->getOptionName($object, $id) |
| 269 | ); |
| 270 | } |
| 271 | |
| 272 | /** |
| 273 | * @inheritdoc |
| 274 | */ |
| 275 | public function getParent() { |
| 276 | if (is_null($this->parent)) { |
| 277 | //try to get this option from the User's Role |
| 278 | $roles = $this->getSubject()->roles; |
| 279 | //first user role is counted only. AAM does not support multi-roles |
| 280 | $parent = array_shift($roles); |
| 281 | |
| 282 | //in case of multisite & current user does not belong to the site |
| 283 | if ($parent) { |
| 284 | $this->parent = new AAM_Core_Subject_Role($parent); |
| 285 | } else { |
| 286 | $this->parent = null; |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | return $this->parent; |
| 291 | } |
| 292 | |
| 293 | /** |
| 294 | * Prepare option's name |
| 295 | * |
| 296 | * @param string $object |
| 297 | * @param string|int $id |
| 298 | * |
| 299 | * @return string |
| 300 | * |
| 301 | * @access public |
| 302 | */ |
| 303 | public function getOptionName($object, $id) { |
| 304 | return "aam_{$object}" . ($id ? "_{$id}" : ''); |
| 305 | } |
| 306 | |
| 307 | /** |
| 308 | * Get Subject UID |
| 309 | * |
| 310 | * @return string |
| 311 | * |
| 312 | * @access public |
| 313 | */ |
| 314 | public function getUID() { |
| 315 | return self::UID; |
| 316 | } |
| 317 | |
| 318 | /** |
| 319 | * |
| 320 | * @return type |
| 321 | */ |
| 322 | public function getName() { |
| 323 | $display = $this->display_name; |
| 324 | |
| 325 | return ($display ? $display : $this->user_nicename); |
| 326 | } |
| 327 | |
| 328 | /** |
| 329 | * |
| 330 | * @return type |
| 331 | */ |
| 332 | public function getMaxLevel() { |
| 333 | return AAM_Core_API::maxLevel($this->allcaps); |
| 334 | } |
| 335 | |
| 336 | } |