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