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 | } |