User.php
| 1 | <?php |
| 2 | namespace SureCart\Models; |
| 3 | |
| 4 | use ArrayAccess; |
| 5 | use JsonSerializable; |
| 6 | |
| 7 | /** |
| 8 | * User class. |
| 9 | */ |
| 10 | class User implements ArrayAccess, JsonSerializable { |
| 11 | /** |
| 12 | * Holds the user. |
| 13 | * |
| 14 | * @var \WP_User|null; |
| 15 | */ |
| 16 | protected $user; |
| 17 | |
| 18 | /** |
| 19 | * Holds the cutomser |
| 20 | * |
| 21 | * @var \SureCart\Models\Customer; |
| 22 | */ |
| 23 | protected $customer; |
| 24 | |
| 25 | /** |
| 26 | * Stores the customer id key. |
| 27 | * |
| 28 | * @var string |
| 29 | */ |
| 30 | protected $customer_id_key = 'sc_customer_ids'; |
| 31 | |
| 32 | /** |
| 33 | * Get the customer meta key. |
| 34 | * |
| 35 | * @return string |
| 36 | */ |
| 37 | protected function getCustomerMetaKey() { |
| 38 | return $this->customer_id_key; |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Get the user's customer id. |
| 43 | * |
| 44 | * @return int|null |
| 45 | */ |
| 46 | protected function customerId( $mode = 'live' ) { |
| 47 | if ( empty( $this->user->ID ) ) { |
| 48 | return ''; |
| 49 | } |
| 50 | $meta = (array) get_user_meta( $this->user->ID, $this->customer_id_key, true ); |
| 51 | if ( isset( $meta[ $mode ] ) ) { |
| 52 | return $meta[ $mode ]; |
| 53 | } |
| 54 | if ( isset( $meta[0] ) ) { |
| 55 | return $meta[0]; |
| 56 | } |
| 57 | return null; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * List the users customer ids. |
| 62 | * |
| 63 | * @return array |
| 64 | */ |
| 65 | protected function customerIds() { |
| 66 | if ( empty( $this->user->ID ) ) { |
| 67 | return []; |
| 68 | } |
| 69 | return array_filter( (array) get_user_meta( $this->user->ID, $this->customer_id_key, true ) ); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Is this user a customer? |
| 74 | * |
| 75 | * @return boolean |
| 76 | */ |
| 77 | protected function isCustomer() { |
| 78 | return ! empty( array_filter( (array) $this->customerIds() ) ); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Does the user have this customer id? |
| 83 | * |
| 84 | * @param string $id The customer id. |
| 85 | * |
| 86 | * @return boolean |
| 87 | */ |
| 88 | protected function hasCustomerId( $id ) { |
| 89 | foreach ( (array) $this->customerIds() as $saved_id ) { |
| 90 | if ( $saved_id === $id ) { |
| 91 | return true; |
| 92 | } |
| 93 | } |
| 94 | return false; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Set the customer id in the user meta. |
| 99 | * |
| 100 | * @param string $id Customer id. |
| 101 | * @return $this|bool |
| 102 | */ |
| 103 | protected function setCustomerId( $id, $mode = 'live', $force = false ) { |
| 104 | $meta = (array) get_user_meta( $this->user->ID, $this->customer_id_key, true ); |
| 105 | |
| 106 | // if we are setting something here. |
| 107 | if ( ! empty( $id ) ) { |
| 108 | // if they already have one set for this mode. |
| 109 | if ( ! empty( $meta[ $mode ] ) ) { |
| 110 | // if we have not passed force = true. |
| 111 | if ( ! $force ) { |
| 112 | return new \WP_Error( 'already_linked', __( 'This user is already linked to a customer.', 'surecart' ) ); |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | // update id. |
| 118 | $meta[ $mode ] = $id; |
| 119 | // update meta. |
| 120 | update_user_meta( $this->user->ID, $this->customer_id_key, $meta ); |
| 121 | return $this; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Disallow overriding the constructor in child classes and make the code safe that way. |
| 126 | */ |
| 127 | final public function __construct() { |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Get a user's subscriptions |
| 132 | * |
| 133 | * @return mixed |
| 134 | */ |
| 135 | protected function subscriptions() { |
| 136 | return Subscription::where( [ 'customer_ids' => [ $this->customerId() ] ] ); |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Get a users orders |
| 141 | * |
| 142 | * @param array $query Query args. |
| 143 | * @return SureCart\Models\Order[]; |
| 144 | */ |
| 145 | protected function orders() { |
| 146 | return Order::where( [ 'customer_ids' => [ $this->customerId() ] ] ); |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Login the user. |
| 151 | * |
| 152 | * @return void |
| 153 | */ |
| 154 | protected function login() { |
| 155 | if ( empty( $this->user->ID ) ) { |
| 156 | return new \Error( 'not_found', esc_html__( 'This user could not be found.', 'surecart' ) ); |
| 157 | } |
| 158 | |
| 159 | $current_user = wp_get_current_user(); |
| 160 | if ( $current_user && $current_user->ID === $this->user->ID ) { |
| 161 | return; |
| 162 | } |
| 163 | |
| 164 | wp_clear_auth_cookie(); |
| 165 | wp_set_current_user( $this->user->ID ); |
| 166 | wp_set_auth_cookie( $this->user->ID ); |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Create a new user and return this model context |
| 171 | * If the user already exists, just set the customer and role in that case. |
| 172 | */ |
| 173 | protected function create( $args ) { |
| 174 | $args = wp_parse_args( |
| 175 | $args, |
| 176 | [ |
| 177 | 'user_name' => '', |
| 178 | 'user_email' => '', |
| 179 | 'user_password' => '', |
| 180 | 'first_name' => '', |
| 181 | 'last_name' => '', |
| 182 | ] |
| 183 | ); |
| 184 | |
| 185 | // use the username or the email as a fallback. |
| 186 | $name = ! empty( sanitize_user( $args['user_name'], true ) ) ? sanitize_user( $args['user_name'], true ) : $args['user_email']; |
| 187 | $username = $this->createUniqueUsername( sanitize_user( $name, true ) ); |
| 188 | |
| 189 | $user_password = trim( $args['user_password'] ); |
| 190 | $user_created = false; |
| 191 | |
| 192 | // password is not provided. |
| 193 | if ( empty( $user_password ) ) { |
| 194 | $user_password = wp_generate_password( 12, false ); |
| 195 | $user_id = wp_create_user( sanitize_user( $username, true ), $user_password, $args['user_email'] ); |
| 196 | // turn off this feature with a filter. |
| 197 | if ( apply_filters( 'surecart/default_password_nag', true, $user_id ) ) { |
| 198 | update_user_meta( $user_id, 'default_password_nag', true ); |
| 199 | } |
| 200 | $user_created = true; |
| 201 | } else { |
| 202 | // Password has been provided. |
| 203 | $user_id = wp_create_user( sanitize_user( $username, true ), $user_password, $args['user_email'] ); |
| 204 | $user_created = true; |
| 205 | } |
| 206 | |
| 207 | if ( is_wp_error( $user_id ) ) { |
| 208 | return $user_id; |
| 209 | } |
| 210 | |
| 211 | $user = new \WP_User( $user_id ); |
| 212 | $user->add_role( 'sc_customer' ); |
| 213 | |
| 214 | if ( $args['first_name'] ) { |
| 215 | $user->first_name = $args['first_name']; |
| 216 | } |
| 217 | if ( $args['last_name'] ) { |
| 218 | $user->last_name = $args['last_name']; |
| 219 | } |
| 220 | |
| 221 | if ( $user_created ) { |
| 222 | wp_update_user( $user ); |
| 223 | } |
| 224 | |
| 225 | return $this->find( $user_id ); |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * Create a unique username for the user. |
| 230 | * |
| 231 | * @param string $name The username. |
| 232 | * |
| 233 | * @return string |
| 234 | */ |
| 235 | protected function createUniqueUsername( $name ) { |
| 236 | $base_name = $name; |
| 237 | $i = 0; |
| 238 | while ( username_exists( $name ) ) { |
| 239 | $name = $base_name . ( ++$i ); |
| 240 | } |
| 241 | return $name; |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * Retrieve user info by a given field |
| 246 | * |
| 247 | * @param string $field The field to retrieve the user with. id | ID | slug | email | login. |
| 248 | * @param int|string $value A value for $field. A user ID, slug, email address, or login name. |
| 249 | * @return this|false This object on success, false on failure. |
| 250 | */ |
| 251 | protected function getUserBy( $field, $value ) { |
| 252 | $this->user = get_user_by( $field, $value ); |
| 253 | return $this->user ? $this : false; |
| 254 | } |
| 255 | |
| 256 | /** |
| 257 | * Get the customer from the user. |
| 258 | * |
| 259 | * @return \SureCart\Models\Customer|false |
| 260 | */ |
| 261 | protected function customer( $mode = 'live' ) { |
| 262 | $id = $this->customerId( $mode ); |
| 263 | if ( ! $id ) { |
| 264 | return false; |
| 265 | } |
| 266 | return Customer::find( $this->customerId( $mode ) ); |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * Get the current user |
| 271 | * |
| 272 | * @return $this |
| 273 | */ |
| 274 | protected function current() { |
| 275 | $this->user = wp_get_current_user(); |
| 276 | return $this; |
| 277 | } |
| 278 | |
| 279 | /** |
| 280 | * Find the user. |
| 281 | * |
| 282 | * @param integer $id ID of the WordPress user. |
| 283 | * @return $this |
| 284 | */ |
| 285 | protected function find( $id ) { |
| 286 | $this->user = get_user_by( 'id', $id ); |
| 287 | return $this; |
| 288 | } |
| 289 | |
| 290 | /** |
| 291 | * Find the user by customer id. |
| 292 | * |
| 293 | * @param string $id Customer id string. |
| 294 | * @return $this |
| 295 | */ |
| 296 | protected function findByCustomerId( $id ) { |
| 297 | $users = new \WP_User_Query( |
| 298 | [ |
| 299 | 'meta_query' => [ |
| 300 | [ |
| 301 | 'key' => $this->customer_id_key, |
| 302 | 'value' => $id, |
| 303 | 'compare' => 'LIKE', |
| 304 | ], |
| 305 | ], |
| 306 | ] |
| 307 | ); |
| 308 | |
| 309 | if ( empty( $users->results ) ) { |
| 310 | return false; |
| 311 | } |
| 312 | |
| 313 | $this->user = $users->results[0]; |
| 314 | return $this; |
| 315 | } |
| 316 | |
| 317 | /** |
| 318 | * Get the WordPress user. |
| 319 | * |
| 320 | * @return \WP_User|null; |
| 321 | */ |
| 322 | public function getWPUser() { |
| 323 | return $this->user; |
| 324 | } |
| 325 | |
| 326 | /** |
| 327 | * Get a specific attribute |
| 328 | * |
| 329 | * @param string $key Attribute name. |
| 330 | * |
| 331 | * @return mixed |
| 332 | */ |
| 333 | public function getAttribute( $key ) { |
| 334 | $attribute = null; |
| 335 | |
| 336 | if ( $this->hasAttribute( $key ) ) { |
| 337 | $attribute = $this->user->$key; |
| 338 | } |
| 339 | |
| 340 | return $attribute; |
| 341 | } |
| 342 | |
| 343 | /** |
| 344 | * Sets a user attribute |
| 345 | * Optionally calls a mutator based on set{Attribute}Attribute |
| 346 | * |
| 347 | * @param string $key Attribute key. |
| 348 | * @param mixed $value Attribute value. |
| 349 | * |
| 350 | * @return mixed|void |
| 351 | */ |
| 352 | public function setAttribute( $key, $value ) { |
| 353 | $this->user->$key = $value; |
| 354 | } |
| 355 | |
| 356 | /** |
| 357 | * Does it have the attribute |
| 358 | * |
| 359 | * @param string $key Attribute key. |
| 360 | * |
| 361 | * @return boolean |
| 362 | */ |
| 363 | public function hasAttribute( $key ) { |
| 364 | return $this->user->$key; |
| 365 | } |
| 366 | |
| 367 | /** |
| 368 | * Serialize to json. |
| 369 | * |
| 370 | * @return Array |
| 371 | */ |
| 372 | public function jsonSerialize() { |
| 373 | return $this->user->to_array(); |
| 374 | } |
| 375 | |
| 376 | /** |
| 377 | * Get the attribute |
| 378 | * |
| 379 | * @param string $key Attribute name. |
| 380 | * |
| 381 | * @return mixed |
| 382 | */ |
| 383 | public function __get( $key ) { |
| 384 | return $this->getAttribute( $key ); |
| 385 | } |
| 386 | |
| 387 | /** |
| 388 | * Set the attribute |
| 389 | * |
| 390 | * @param string $key Attribute name. |
| 391 | * @param mixed $value Value of attribute. |
| 392 | * |
| 393 | * @return void |
| 394 | */ |
| 395 | public function __set( $key, $value ) { |
| 396 | $this->setAttribute( $key, $value ); |
| 397 | } |
| 398 | |
| 399 | /** |
| 400 | * Determine if the given attribute exists. |
| 401 | * |
| 402 | * @param mixed $offset Name. |
| 403 | * @return bool |
| 404 | */ |
| 405 | public function offsetExists( $offset ) { |
| 406 | return ! is_null( $this->getAttribute( $offset ) ); |
| 407 | } |
| 408 | |
| 409 | /** |
| 410 | * Get the value for a given offset. |
| 411 | * |
| 412 | * @param mixed $offset Name. |
| 413 | * @return mixed |
| 414 | */ |
| 415 | public function offsetGet( $offset ) { |
| 416 | return $this->getAttribute( $offset ); |
| 417 | } |
| 418 | |
| 419 | /** |
| 420 | * Set the value for a given offset. |
| 421 | * |
| 422 | * @param mixed $offset Name. |
| 423 | * @param mixed $value Value. |
| 424 | * @return void |
| 425 | */ |
| 426 | public function offsetSet( $offset, $value ) { |
| 427 | $this->setAttribute( $offset, $value ); |
| 428 | } |
| 429 | |
| 430 | /** |
| 431 | * Unset the value for a given offset. |
| 432 | * |
| 433 | * @param mixed $offset Name. |
| 434 | * @return void |
| 435 | */ |
| 436 | public function offsetUnset( $offset ) { |
| 437 | $this->user->$offset = null; |
| 438 | } |
| 439 | |
| 440 | public function getUser() { |
| 441 | return $this->user; |
| 442 | } |
| 443 | |
| 444 | /** |
| 445 | * Determine if an attribute or relation exists on the model. |
| 446 | * |
| 447 | * @param string $key Name. |
| 448 | * @return bool |
| 449 | */ |
| 450 | public function __isset( $key ) { |
| 451 | return $this->offsetExists( $key ); |
| 452 | } |
| 453 | |
| 454 | /** |
| 455 | * Unset an attribute on the model. |
| 456 | * |
| 457 | * @param string $key Name. |
| 458 | * @return void |
| 459 | */ |
| 460 | public function __unset( $key ) { |
| 461 | $this->offsetUnset( $key ); |
| 462 | } |
| 463 | |
| 464 | /** |
| 465 | * Forward call to method |
| 466 | * |
| 467 | * @param string $method Method to call. |
| 468 | * @param mixed $params Method params. |
| 469 | */ |
| 470 | public function __call( $method, $params ) { |
| 471 | return call_user_func_array( [ $this, $method ], $params ); |
| 472 | } |
| 473 | |
| 474 | /** |
| 475 | * Static Facade Accessor |
| 476 | * |
| 477 | * @param string $method Method to call. |
| 478 | * @param mixed $params Method params. |
| 479 | * |
| 480 | * @return mixed |
| 481 | */ |
| 482 | public static function __callStatic( $method, $params ) { |
| 483 | return call_user_func_array( [ new static(), $method ], $params ); |
| 484 | } |
| 485 | } |
| 486 |