user.php
359 lines
| 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 | // cache user object |
| 90 | if (!isset(static::$instances[$id])) |
| 91 | { |
| 92 | static::$instances[$id] = $user; |
| 93 | } |
| 94 | |
| 95 | return static::$instances[$id]; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Magic method to access internal properties. |
| 100 | * |
| 101 | * @param string $name The property to read. |
| 102 | * |
| 103 | * @return mixed The property value. |
| 104 | */ |
| 105 | public function __get($name) |
| 106 | { |
| 107 | // property switch for Joomla portability |
| 108 | switch ($name) |
| 109 | { |
| 110 | case 'guest': |
| 111 | return $this->user->exists() ? 0 : 1; |
| 112 | |
| 113 | case 'username': |
| 114 | return $this->user->user_login; |
| 115 | |
| 116 | case 'name': |
| 117 | return $this->user->display_name; |
| 118 | |
| 119 | case 'email': |
| 120 | return $this->user->user_email; |
| 121 | |
| 122 | /** |
| 123 | * It is now possible to retrieve the roles |
| 124 | * to which the user is assigned. |
| 125 | * |
| 126 | * @since 10.1.24 |
| 127 | */ |
| 128 | case 'groups': |
| 129 | return (array) $this->user->roles; |
| 130 | |
| 131 | case 'id': |
| 132 | /** |
| 133 | * WP_User->id is deprecated since wp 2.1.0. |
| 134 | * Use uppercase ID instead. |
| 135 | */ |
| 136 | return $this->user->ID; |
| 137 | } |
| 138 | |
| 139 | // otherwise try to get the property from the wrapped object |
| 140 | if (isset($this->user->{$name})) |
| 141 | { |
| 142 | return $this->user->{$name}; |
| 143 | } |
| 144 | |
| 145 | return null; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Magic method to check whether a property is set. |
| 150 | * |
| 151 | * @param string $name The property to check. |
| 152 | * |
| 153 | * @return boolesn True if set, false otherwise. |
| 154 | * |
| 155 | * @since 10.1.30 |
| 156 | */ |
| 157 | public function __isset($name) |
| 158 | { |
| 159 | return $this->{$name} !== null; |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Magic method to access internal methods. |
| 164 | * |
| 165 | * @param string $name The method to execute. |
| 166 | * @param array $args A list of arguments. |
| 167 | * |
| 168 | * @return mixed The method result. |
| 169 | */ |
| 170 | public function __call($name, $args) |
| 171 | { |
| 172 | // dispatch wrapped object like a proxy |
| 173 | if (method_exists($this->user, $name)) |
| 174 | { |
| 175 | return call_user_func_array(array($this->user, $name), $args); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Method to bind an associative array of data to a user object. |
| 181 | * |
| 182 | * @param array &$array The associative array to bind to the object. |
| 183 | * |
| 184 | * @return boolean True on success, otherwise false. |
| 185 | */ |
| 186 | public function bind(array &$array) |
| 187 | { |
| 188 | $reg = new JObject($array); |
| 189 | |
| 190 | $this->data = new stdClass; |
| 191 | |
| 192 | $group = (array) $reg->get('groups'); |
| 193 | |
| 194 | $this->data->user_nicename = $reg->get('name'); |
| 195 | $this->data->display_name = $reg->get('name'); |
| 196 | $this->data->user_login = $reg->get('username'); |
| 197 | $this->data->user_email = $reg->get('email'); |
| 198 | $this->data->user_pass = $reg->get('password'); |
| 199 | $this->data->role = array_shift($group); |
| 200 | |
| 201 | /** |
| 202 | * Skip notification only in case 'sendEmail' attribute |
| 203 | * has been specified with a false boolean. |
| 204 | * |
| 205 | * @since 10.1.24 |
| 206 | */ |
| 207 | $this->data->notify = $reg->get('sendEmail', true); |
| 208 | |
| 209 | return true; |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * Method to save the user object to the database. |
| 214 | * |
| 215 | * @param boolean $updateOnly Save the object only if not a new user. |
| 216 | * |
| 217 | * @return boolean True on success, otherwise false. |
| 218 | */ |
| 219 | public function save($updateOnly = false) |
| 220 | { |
| 221 | // never called bind() method to fill $data property |
| 222 | if (is_null($this->data)) |
| 223 | { |
| 224 | return false; |
| 225 | } |
| 226 | |
| 227 | // if the user exists, proceed with the update |
| 228 | if ($this->user->exists()) |
| 229 | { |
| 230 | $id = wp_update_user($this->data); |
| 231 | } |
| 232 | // otherwise make sure we can insert the user |
| 233 | else if (!$updateOnly) |
| 234 | { |
| 235 | $id = wp_insert_user($this->data); |
| 236 | } |
| 237 | |
| 238 | // check if the insert/update raised an error |
| 239 | if (is_wp_error($id)) |
| 240 | { |
| 241 | // push the error in the list |
| 242 | $this->setError($id); |
| 243 | |
| 244 | return false; |
| 245 | } |
| 246 | |
| 247 | // reload the user object on success |
| 248 | $this->user = new WP_User($id); |
| 249 | |
| 250 | /** |
| 251 | * Evaluates if the user should be notified. |
| 252 | * |
| 253 | * @since 10.1.24 |
| 254 | */ |
| 255 | if (!empty($this->data->notify)) |
| 256 | { |
| 257 | // 'user' for sending notification only to the user created |
| 258 | wp_send_new_user_notifications($id, 'user'); |
| 259 | } |
| 260 | |
| 261 | return true; |
| 262 | } |
| 263 | |
| 264 | /** |
| 265 | * Method to check User object authorisation against an access control |
| 266 | * object and optionally an access extension object. |
| 267 | * |
| 268 | * @param string $action The name of the action to check for permission. |
| 269 | * @param string $asset The name of the asset on which to perform the action. |
| 270 | * |
| 271 | * @return boolean True if authorised. |
| 272 | */ |
| 273 | public function authorise($action, $asset = null) |
| 274 | { |
| 275 | JLoader::import('adapter.acl.access'); |
| 276 | |
| 277 | // $groups = JAccess::getGroupsByUser($this->id); |
| 278 | $groups = $this->roles; |
| 279 | |
| 280 | $cap = JAccess::adjustCapability($action, $asset); |
| 281 | |
| 282 | foreach ($groups as $group) |
| 283 | { |
| 284 | if (JAccess::checkGroup($group, $action, $asset)) |
| 285 | { |
| 286 | return true; |
| 287 | } |
| 288 | |
| 289 | /** |
| 290 | * This statement is used to check the permissions of an administrator |
| 291 | * that shares the same account on a multi-site instance. |
| 292 | * In this way, WP_User should check (on cascade) all the permissions |
| 293 | * related to the similar accounts. |
| 294 | * |
| 295 | * WP_User::has_cap() is used by current_user_can(). |
| 296 | * |
| 297 | * @since 10.1.1 |
| 298 | */ |
| 299 | if ($this->has_cap($cap)) |
| 300 | { |
| 301 | return true; |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | return false; |
| 306 | } |
| 307 | |
| 308 | /** |
| 309 | * Method to get the user timezone. |
| 310 | * |
| 311 | * @return DateTimeZone |
| 312 | * |
| 313 | * @since 10.1.35 |
| 314 | */ |
| 315 | public function getTimezone() |
| 316 | { |
| 317 | // get default system timezone, since WordPress doesn't support |
| 318 | // a feature to differentiate the timezone per user |
| 319 | $timezone = JFactory::getApplication()->get('offset', 'UTC'); |
| 320 | |
| 321 | return new DateTimeZone($timezone); |
| 322 | } |
| 323 | |
| 324 | /** |
| 325 | * Gets an array of the authorised access levels for the user. |
| 326 | * |
| 327 | * @return array |
| 328 | * |
| 329 | * @since 10.1.39 |
| 330 | */ |
| 331 | public function getAuthorisedViewLevels() |
| 332 | { |
| 333 | $levels = []; |
| 334 | |
| 335 | if (in_array('administrator', $this->user->roles)) |
| 336 | { |
| 337 | // super user |
| 338 | $levels = [1, 2, 3, 6]; |
| 339 | } |
| 340 | else if (in_array('editor', $this->user->roles) || in_array('author', $this->user->roles)) |
| 341 | { |
| 342 | // special |
| 343 | $levels = [1, 2, 3]; |
| 344 | } |
| 345 | else if ($this->user->roles) |
| 346 | { |
| 347 | // registered |
| 348 | $levels = [1, 2]; |
| 349 | } |
| 350 | else |
| 351 | { |
| 352 | // guest user |
| 353 | $levels = [1, 5]; |
| 354 | } |
| 355 | |
| 356 | return $levels; |
| 357 | } |
| 358 | } |
| 359 |