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( $this->customerIds() ) ); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Set the customer id in the user meta. |
| 83 | * |
| 84 | * @param string $id Customer id. |
| 85 | * @return int|bool |
| 86 | */ |
| 87 | protected function setCustomerId( $id, $mode = 'live' ) { |
| 88 | $meta = (array) get_user_meta( $this->user->ID, $this->customer_id_key, true ); |
| 89 | $meta[ $mode ] = $id; |
| 90 | update_user_meta( $this->user->ID, $this->customer_id_key, $meta ); |
| 91 | return $this; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Disallow overriding the constructor in child classes and make the code safe that way. |
| 96 | */ |
| 97 | final public function __construct() { |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Get a user's subscriptions |
| 102 | * |
| 103 | * @return mixed |
| 104 | */ |
| 105 | protected function subscriptions() { |
| 106 | return Subscription::where( [ 'customer_ids' => [ $this->customerId() ] ] ); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Get a users orders |
| 111 | * |
| 112 | * @param array $query Query args. |
| 113 | * @return SureCart\Models\Order[]; |
| 114 | */ |
| 115 | protected function orders() { |
| 116 | return Order::where( [ 'customer_ids' => [ $this->customerId() ] ] ); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Login the user. |
| 121 | * |
| 122 | * @return void |
| 123 | */ |
| 124 | protected function login() { |
| 125 | if ( empty( $this->user->ID ) ) { |
| 126 | return new \Error( 'not_found', esc_html__( 'This user could not be found.', 'surecart' ) ); |
| 127 | } |
| 128 | |
| 129 | $current_user = wp_get_current_user(); |
| 130 | if ( $current_user && $current_user->ID === $this->user->ID ) { |
| 131 | return; |
| 132 | } |
| 133 | |
| 134 | wp_clear_auth_cookie(); |
| 135 | wp_set_current_user( $this->user->ID ); |
| 136 | wp_set_auth_cookie( $this->user->ID ); |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Create a new user and return this model context |
| 141 | * If the user already exists, just set the customer and role in that case. |
| 142 | */ |
| 143 | protected function create( $args ) { |
| 144 | $args = wp_parse_args( |
| 145 | $args, |
| 146 | [ |
| 147 | 'user_name' => '', |
| 148 | 'user_email' => '', |
| 149 | 'user_password' => '', |
| 150 | ] |
| 151 | ); |
| 152 | |
| 153 | $user_id = username_exists( $args['user_name'] ); |
| 154 | $user_password = trim( $args['user_password'] ); |
| 155 | $user_created = false; |
| 156 | |
| 157 | if ( ! $user_id && empty( $user_password ) ) { |
| 158 | $user_password = wp_generate_password( 12, false ); |
| 159 | $user_id = wp_create_user( $args['user_name'], $user_password, $args['user_email'] ); |
| 160 | update_user_meta( $user_id, 'default_password_nag', true ); |
| 161 | $user_created = true; |
| 162 | } elseif ( ! $user_id ) { |
| 163 | // Password has been provided. |
| 164 | $user_id = wp_create_user( $args['user_name'], $user_password, $args['user_email'] ); |
| 165 | $user_created = true; |
| 166 | } |
| 167 | |
| 168 | if ( is_wp_error( $user_id ) ) { |
| 169 | return $user_id; |
| 170 | } |
| 171 | |
| 172 | $user = new \WP_User( $user_id ); |
| 173 | $user->add_role( 'sc_customer' ); |
| 174 | |
| 175 | if ( $user_created ) { |
| 176 | wp_update_user( $user ); |
| 177 | } |
| 178 | |
| 179 | return $this->find( $user_id ); |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Retrieve user info by a given field |
| 184 | * |
| 185 | * @param string $field The field to retrieve the user with. id | ID | slug | email | login. |
| 186 | * @param int|string $value A value for $field. A user ID, slug, email address, or login name. |
| 187 | * @return this|false This object on success, false on failure. |
| 188 | */ |
| 189 | protected function getUserBy( $field, $value ) { |
| 190 | $this->user = get_user_by( $field, $value ); |
| 191 | return $this->user ? $this : false; |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Get the customer from the user. |
| 196 | * |
| 197 | * @return \SureCart\Models\Customer|false |
| 198 | */ |
| 199 | protected function customer( $mode = 'live' ) { |
| 200 | $id = $this->customerId( $mode ); |
| 201 | if ( ! $id ) { |
| 202 | return false; |
| 203 | } |
| 204 | return Customer::find( $this->customerId( $mode ) ); |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Create the customer by email. |
| 209 | * |
| 210 | * @return void |
| 211 | */ |
| 212 | public function createCustomerByEmail() { |
| 213 | $customer = Customer::byEmail( $this->user->user_email ); |
| 214 | if ( $customer ) { |
| 215 | $this->customer = $this->setCustomerId( $customer->id ); |
| 216 | } |
| 217 | return $this; |
| 218 | } |
| 219 | |
| 220 | /** |
| 221 | * Get the current user |
| 222 | * |
| 223 | * @return $this |
| 224 | */ |
| 225 | protected function current() { |
| 226 | $this->user = wp_get_current_user(); |
| 227 | return $this; |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * Find the user. |
| 232 | * |
| 233 | * @param integer $id ID of the WordPress user. |
| 234 | * @return $this |
| 235 | */ |
| 236 | protected function find( $id ) { |
| 237 | $this->user = get_user_by( 'id', $id ); |
| 238 | return $this; |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | * Find the user by customer id. |
| 243 | * |
| 244 | * @param string $id Customer id string. |
| 245 | * @return $this |
| 246 | */ |
| 247 | protected function findByCustomerId( $id ) { |
| 248 | $users = new \WP_User_Query( |
| 249 | [ |
| 250 | 'meta_query' => [ |
| 251 | [ |
| 252 | 'key' => $this->customer_id_key, |
| 253 | 'value' => $id, |
| 254 | 'compare' => 'LIKE', |
| 255 | ], |
| 256 | ], |
| 257 | ] |
| 258 | ); |
| 259 | |
| 260 | if ( empty( $users->results ) ) { |
| 261 | return false; |
| 262 | } |
| 263 | |
| 264 | $this->user = $users->results[0]; |
| 265 | return $this; |
| 266 | } |
| 267 | |
| 268 | /** |
| 269 | * Get the WordPress user. |
| 270 | * |
| 271 | * @return \WP_User|null; |
| 272 | */ |
| 273 | public function getWPUser() { |
| 274 | return $this->user; |
| 275 | } |
| 276 | |
| 277 | /** |
| 278 | * Get a specific attribute |
| 279 | * |
| 280 | * @param string $key Attribute name. |
| 281 | * |
| 282 | * @return mixed |
| 283 | */ |
| 284 | public function getAttribute( $key ) { |
| 285 | $attribute = null; |
| 286 | |
| 287 | if ( $this->hasAttribute( $key ) ) { |
| 288 | $attribute = $this->user->$key; |
| 289 | } |
| 290 | |
| 291 | return $attribute; |
| 292 | } |
| 293 | |
| 294 | /** |
| 295 | * Sets a user attribute |
| 296 | * Optionally calls a mutator based on set{Attribute}Attribute |
| 297 | * |
| 298 | * @param string $key Attribute key. |
| 299 | * @param mixed $value Attribute value. |
| 300 | * |
| 301 | * @return mixed|void |
| 302 | */ |
| 303 | public function setAttribute( $key, $value ) { |
| 304 | $this->user->$key = $value; |
| 305 | } |
| 306 | |
| 307 | /** |
| 308 | * Does it have the attribute |
| 309 | * |
| 310 | * @param string $key Attribute key. |
| 311 | * |
| 312 | * @return boolean |
| 313 | */ |
| 314 | public function hasAttribute( $key ) { |
| 315 | return $this->user->$key; |
| 316 | } |
| 317 | |
| 318 | /** |
| 319 | * Serialize to json. |
| 320 | * |
| 321 | * @return Array |
| 322 | */ |
| 323 | public function jsonSerialize() { |
| 324 | return $this->user->to_array(); |
| 325 | } |
| 326 | |
| 327 | /** |
| 328 | * Get the attribute |
| 329 | * |
| 330 | * @param string $key Attribute name. |
| 331 | * |
| 332 | * @return mixed |
| 333 | */ |
| 334 | public function __get( $key ) { |
| 335 | return $this->getAttribute( $key ); |
| 336 | } |
| 337 | |
| 338 | /** |
| 339 | * Set the attribute |
| 340 | * |
| 341 | * @param string $key Attribute name. |
| 342 | * @param mixed $value Value of attribute. |
| 343 | * |
| 344 | * @return void |
| 345 | */ |
| 346 | public function __set( $key, $value ) { |
| 347 | $this->setAttribute( $key, $value ); |
| 348 | } |
| 349 | |
| 350 | /** |
| 351 | * Determine if the given attribute exists. |
| 352 | * |
| 353 | * @param mixed $offset Name. |
| 354 | * @return bool |
| 355 | */ |
| 356 | public function offsetExists( $offset ) { |
| 357 | return ! is_null( $this->getAttribute( $offset ) ); |
| 358 | } |
| 359 | |
| 360 | /** |
| 361 | * Get the value for a given offset. |
| 362 | * |
| 363 | * @param mixed $offset Name. |
| 364 | * @return mixed |
| 365 | */ |
| 366 | public function offsetGet( $offset ) { |
| 367 | return $this->getAttribute( $offset ); |
| 368 | } |
| 369 | |
| 370 | /** |
| 371 | * Set the value for a given offset. |
| 372 | * |
| 373 | * @param mixed $offset Name. |
| 374 | * @param mixed $value Value. |
| 375 | * @return void |
| 376 | */ |
| 377 | public function offsetSet( $offset, $value ) { |
| 378 | $this->setAttribute( $offset, $value ); |
| 379 | } |
| 380 | |
| 381 | /** |
| 382 | * Unset the value for a given offset. |
| 383 | * |
| 384 | * @param mixed $offset Name. |
| 385 | * @return void |
| 386 | */ |
| 387 | public function offsetUnset( $offset ) { |
| 388 | $this->user->$offset = null; |
| 389 | } |
| 390 | |
| 391 | public function getUser() { |
| 392 | return $this->user; |
| 393 | } |
| 394 | |
| 395 | /** |
| 396 | * Determine if an attribute or relation exists on the model. |
| 397 | * |
| 398 | * @param string $key Name. |
| 399 | * @return bool |
| 400 | */ |
| 401 | public function __isset( $key ) { |
| 402 | return $this->offsetExists( $key ); |
| 403 | } |
| 404 | |
| 405 | /** |
| 406 | * Unset an attribute on the model. |
| 407 | * |
| 408 | * @param string $key Name. |
| 409 | * @return void |
| 410 | */ |
| 411 | public function __unset( $key ) { |
| 412 | $this->offsetUnset( $key ); |
| 413 | } |
| 414 | |
| 415 | /** |
| 416 | * Forward call to method |
| 417 | * |
| 418 | * @param string $method Method to call. |
| 419 | * @param mixed $params Method params. |
| 420 | */ |
| 421 | public function __call( $method, $params ) { |
| 422 | return call_user_func_array( [ $this, $method ], $params ); |
| 423 | } |
| 424 | |
| 425 | /** |
| 426 | * Static Facade Accessor |
| 427 | * |
| 428 | * @param string $method Method to call. |
| 429 | * @param mixed $params Method params. |
| 430 | * |
| 431 | * @return mixed |
| 432 | */ |
| 433 | public static function __callStatic( $method, $params ) { |
| 434 | return call_user_func_array( [ new static(), $method ], $params ); |
| 435 | } |
| 436 | } |
| 437 |