| @@ -1,0 +1,360 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * @package VikWP - Libraries | |
| 4 | + * @subpackage adapter.user | |
| 5 | + * @author E4J s.r.l. | |
| 6 | + * @copyright Copyright (C) 2023 E4J s.r.l. All Rights Reserved. | |
| 7 | + * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL | |
| 8 | + * @link https://vikwp.com | |
| 9 | + */ | |
| 10 | + | |
| 11 | +// No direct access | |
| 12 | +defined('ABSPATH') or die('No script kiddies please!'); | |
| 13 | + | |
| 14 | +JLoader::import('adapter.application.object'); | |
| 15 | + | |
| 16 | +/** | |
| 17 | + * User adapter class. | |
| 18 | + * | |
| 19 | + * @since 10.0 | |
| 20 | + * @link https://codex.wordpress.org/Class_Reference/WP_User | |
| 21 | + */ | |
| 22 | +class JUser extends JObject | |
| 23 | +{ | |
| 24 | + /** | |
| 25 | + * The wordpress user wrapper. | |
| 26 | + * | |
| 27 | + * @var WP_User | |
| 28 | + */ | |
| 29 | + private $user; | |
| 30 | + | |
| 31 | + /** | |
| 32 | + * Data object to save the user. | |
| 33 | + * | |
| 34 | + * @var object | |
| 35 | + */ | |
| 36 | + private $data = null; | |
| 37 | + | |
| 38 | + /** | |
| 39 | + * A list of user instances. | |
| 40 | + * | |
| 41 | + * @var array | |
| 42 | + */ | |
| 43 | + protected static $instances = array(); | |
| 44 | + | |
| 45 | + /** | |
| 46 | + * Class constructor. | |
| 47 | + * | |
| 48 | + * @param integer $id The primary key value of the user to load. | |
| 49 | + */ | |
| 50 | + public function __construct($id = 0) | |
| 51 | + { | |
| 52 | + // load current logged user when the ID is not provided | |
| 53 | + if (is_null($id)) | |
| 54 | + { | |
| 55 | + if (!function_exists('wp_get_current_user')) | |
| 56 | + { | |
| 57 | + // it is not yet possible to access current user details | |
| 58 | + throw new RuntimeException('It is not yet possible to access current user details', 500); | |
| 59 | + } | |
| 60 | + | |
| 61 | + $this->user = wp_get_current_user(); | |
| 62 | + } | |
| 63 | + // otherwise load user by id | |
| 64 | + else | |
| 65 | + { | |
| 66 | + $this->user = new WP_User($id); | |
| 67 | + } | |
| 68 | + } | |
| 69 | + | |
| 70 | + /** | |
| 71 | + * Returns the global user object, only creating it if it doesn't already exist. | |
| 72 | + * | |
| 73 | + * @param integer $identifier The primary key of the user to load (optional). | |
| 74 | + * | |
| 75 | + * @return self The user instance. | |
| 76 | + */ | |
| 77 | + public static function getInstance($id = null) | |
| 78 | + { | |
| 79 | + // always try to retrieve user data | |
| 80 | + $user = new static($id); | |
| 81 | + | |
| 82 | + // dispatch parent method to check if the user is empty | |
| 83 | + if (!$user->exists()) | |
| 84 | + { | |
| 85 | + // do not cache | |
| 86 | + return $user; | |
| 87 | + } | |
| 88 | + | |
| 89 | + $key = (int) $id; | |
| 90 | + | |
| 91 | + // cache user object | |
| 92 | + if (!isset(static::$instances[$key])) | |
| 93 | + { | |
| 94 | + static::$instances[$key] = $user; | |
| 95 | + } | |
| 96 | + | |
| 97 | + return static::$instances[$key]; | |
| 98 | + } | |
| 99 | + | |
| 100 | + /** | |
| 101 | + * Magic method to access internal properties. | |
| 102 | + * | |
| 103 | + * @param string $name The property to read. | |
| 104 | + * | |
| 105 | + * @return mixed The property value. | |
| 106 | + */ | |
| 107 | + public function __get($name) | |
| 108 | + { | |
| 109 | + // property switch for Joomla portability | |
| 110 | + switch ($name) | |
| 111 | + { | |
| 112 | + case 'guest': | |
| 113 | + return $this->user->exists() ? 0 : 1; | |
| 114 | + | |
| 115 | + case 'username': | |
| 116 | + return $this->user->user_login; | |
| 117 | + | |
| 118 | + case 'name': | |
| 119 | + return $this->user->display_name; | |
| 120 | + | |
| 121 | + case 'email': | |
| 122 | + return $this->user->user_email; | |
| 123 | + | |
| 124 | + /** | |
| 125 | + * It is now possible to retrieve the roles | |
| 126 | + * to which the user is assigned. | |
| 127 | + * | |
| 128 | + * @since 10.1.24 | |
| 129 | + */ | |
| 130 | + case 'groups': | |
| 131 | + return (array) $this->user->roles; | |
| 132 | + | |
| 133 | + case 'id': | |
| 134 | + /** | |
| 135 | + * WP_User->id is deprecated since wp 2.1.0. | |
| 136 | + * Use uppercase ID instead. | |
| 137 | + */ | |
| 138 | + return $this->user->ID; | |
| 139 | + } | |
| 140 | + | |
| 141 | + // otherwise try to get the property from the wrapped object | |
| 142 | + if (isset($this->user->{$name})) | |
| 143 | + { | |
| 144 | + return $this->user->{$name}; | |
| 145 | + } | |
| 146 | + | |
| 147 | + return null; | |
| 148 | + } | |
| 149 | + | |
| 150 | + /** | |
| 151 | + * Magic method to check whether a property is set. | |
| 152 | + * | |
| 153 | + * @param string $name The property to check. | |
| 154 | + * | |
| 155 | + * @return boolesn True if set, false otherwise. | |
| 156 | + * | |
| 157 | + * @since 10.1.30 | |
| 158 | + */ | |
| 159 | + public function __isset($name) | |
| 160 | + { | |
| 161 | + return $this->{$name} !== null; | |
| 162 | + } | |
| 163 | + | |
| 164 | + /** | |
| 165 | + * Magic method to access internal methods. | |
| 166 | + * | |
| 167 | + * @param string $name The method to execute. | |
| 168 | + * @param array $args A list of arguments. | |
| 169 | + * | |
| 170 | + * @return mixed The method result. | |
| 171 | + */ | |
| 172 | + public function __call($name, $args) | |
| 173 | + { | |
| 174 | + // dispatch wrapped object like a proxy | |
| 175 | + if (method_exists($this->user, $name)) | |
| 176 | + { | |
| 177 | + return call_user_func_array(array($this->user, $name), $args); | |
| 178 | + } | |
| 179 | + } | |
| 180 | + | |
| 181 | + /** | |
| 182 | + * Method to bind an associative array of data to a user object. | |
| 183 | + * | |
| 184 | + * @param array &$array The associative array to bind to the object. | |
| 185 | + * | |
| 186 | + * @return boolean True on success, otherwise false. | |
| 187 | + */ | |
| 188 | + public function bind(array &$array) | |
| 189 | + { | |
| 190 | + $reg = new JObject($array); | |
| 191 | + | |
| 192 | + $this->data = new stdClass; | |
| 193 | + | |
| 194 | + $group = (array) $reg->get('groups'); | |
| 195 | + | |
| 196 | + $this->data->user_nicename = $reg->get('name'); | |
| 197 | + $this->data->display_name = $reg->get('name'); | |
| 198 | + $this->data->user_login = $reg->get('username'); | |
| 199 | + $this->data->user_email = $reg->get('email'); | |
| 200 | + $this->data->user_pass = $reg->get('password'); | |
| 201 | + $this->data->role = array_shift($group); | |
| 202 | + | |
| 203 | + /** | |
| 204 | + * Skip notification only in case 'sendEmail' attribute | |
| 205 | + * has been specified with a false boolean. | |
| 206 | + * | |
| 207 | + * @since 10.1.24 | |
| 208 | + */ | |
| 209 | + $this->data->notify = $reg->get('sendEmail', true); | |
| 210 | + | |
| 211 | + return true; | |
| 212 | + } | |
| 213 | + | |
| 214 | + /** | |
| 215 | + * Method to save the user object to the database. | |
| 216 | + * | |
| 217 | + * @param boolean $updateOnly Save the object only if not a new user. | |
| 218 | + * | |
| 219 | + * @return boolean True on success, otherwise false. | |
| 220 | + */ | |
| 221 | + public function save($updateOnly = false) | |
| 222 | + { | |
| 223 | + // never called bind() method to fill $data property | |
| 224 | + if (is_null($this->data)) | |
| 225 | + { | |
| 226 | + return false; | |
| 227 | + } | |
| 228 | + | |
| 229 | + // if the user exists, proceed with the update | |
| 230 | + if ($this->user->exists()) | |
| 231 | + { | |
| 232 | + $id = wp_update_user($this->data); | |
| 233 | + } | |
| 234 | + // otherwise make sure we can insert the user | |
| 235 | + else if (!$updateOnly) | |
| 236 | + { | |
| 237 | + $id = wp_insert_user($this->data); | |
| 238 | + } | |
| 239 | + | |
| 240 | + // check if the insert/update raised an error | |
| 241 | + if (is_wp_error($id)) | |
| 242 | + { | |
| 243 | + // push the error in the list | |
| 244 | + $this->setError($id); | |
| 245 | + | |
| 246 | + return false; | |
| 247 | + } | |
| 248 | + | |
| 249 | + // reload the user object on success | |
| 250 | + $this->user = new WP_User($id); | |
| 251 | + | |
| 252 | + /** | |
| 253 | + * Evaluates if the user should be notified. | |
| 254 | + * | |
| 255 | + * @since 10.1.24 | |
| 256 | + */ | |
| 257 | + if (!empty($this->data->notify)) | |
| 258 | + { | |
| 259 | + // 'user' for sending notification only to the user created | |
| 260 | + wp_send_new_user_notifications($id, 'user'); | |
| 261 | + } | |
| 262 | + | |
| 263 | + return true; | |
| 264 | + } | |
| 265 | + | |
| 266 | + /** | |
| 267 | + * Method to check User object authorisation against an access control | |
| 268 | + * object and optionally an access extension object. | |
| 269 | + * | |
| 270 | + * @param string $action The name of the action to check for permission. | |
| 271 | + * @param string $asset The name of the asset on which to perform the action. | |
| 272 | + * | |
| 273 | + * @return boolean True if authorised. | |
| 274 | + */ | |
| 275 | + public function authorise($action, $asset = null) | |
| 276 | + { | |
| 277 | + JLoader::import('adapter.acl.access'); | |
| 278 | + | |
| 279 | + // $groups = JAccess::getGroupsByUser($this->id); | |
| 280 | + $groups = $this->roles; | |
| 281 | + | |
| 282 | + $cap = JAccess::adjustCapability($action, $asset); | |
| 283 | + | |
| 284 | + foreach ($groups as $group) | |
| 285 | + { | |
| 286 | + if (JAccess::checkGroup($group, $action, $asset)) | |
| 287 | + { | |
| 288 | + return true; | |
| 289 | + } | |
| 290 | + | |
| 291 | + /** | |
| 292 | + * This statement is used to check the permissions of an administrator | |
| 293 | + * that shares the same account on a multi-site instance. | |
| 294 | + * In this way, WP_User should check (on cascade) all the permissions | |
| 295 | + * related to the similar accounts. | |
| 296 | + * | |
| 297 | + * WP_User::has_cap() is used by current_user_can(). | |
| 298 | + * | |
| 299 | + * @since 10.1.1 | |
| 300 | + */ | |
| 301 | + if ($this->has_cap($cap)) | |
| 302 | + { | |
| 303 | + return true; | |
| 304 | + } | |
| 305 | + } | |
| 306 | + | |
| 307 | + return false; | |
| 308 | + } | |
| 309 | + | |
| 310 | + /** | |
| 311 | + * Method to get the user timezone. | |
| 312 | + * | |
| 313 | + * @return DateTimeZone | |
| 314 | + * | |
| 315 | + * @since 10.1.35 | |
| 316 | + */ | |
| 317 | + public function getTimezone() | |
| 318 | + { | |
| 319 | + // get default system timezone, since WordPress doesn't support | |
| 320 | + // a feature to differentiate the timezone per user | |
| 321 | + $timezone = JFactory::getApplication()->get('offset', 'UTC'); | |
| 322 | + | |
| 323 | + return new DateTimeZone($timezone); | |
| 324 | + } | |
| 325 | + | |
| 326 | + /** | |
| 327 | + * Gets an array of the authorised access levels for the user. | |
| 328 | + * | |
| 329 | + * @return array | |
| 330 | + * | |
| 331 | + * @since 10.1.39 | |
| 332 | + */ | |
| 333 | + public function getAuthorisedViewLevels() | |
| 334 | + { | |
| 335 | + $levels = []; | |
| 336 | + | |
| 337 | + if (in_array('administrator', $this->user->roles)) | |
| 338 | + { | |
| 339 | + // super user | |
| 340 | + $levels = [1, 2, 3, 6]; | |
| 341 | + } | |
| 342 | + else if (in_array('editor', $this->user->roles) || in_array('author', $this->user->roles)) | |
| 343 | + { | |
| 344 | + // special | |
| 345 | + $levels = [1, 2, 3]; | |
| 346 | + } | |
| 347 | + else if ($this->user->roles) | |
| 348 | + { | |
| 349 | + // registered | |
| 350 | + $levels = [1, 2]; | |
| 351 | + } | |
| 352 | + else | |
| 353 | + { | |
| 354 | + // guest user | |
| 355 | + $levels = [1, 5]; | |
| 356 | + } | |
| 357 | + | |
| 358 | + return $levels; | |
| 359 | + } | |
| 360 | +} | |