| 1 |
<?php |
| 2 |
/** |
| 3 |
* Cutomer Class |
| 4 |
* |
| 5 |
* @package Timetics |
| 6 |
*/ |
| 7 |
namespace Timetics\Core\Customers; |
| 8 |
|
| 9 |
use Timetics\Core\Bookings\Booking; |
| 10 |
|
| 11 |
/** |
| 12 |
* Customer class |
| 13 |
* |
| 14 |
* @since 1.0.0 |
| 15 |
*/ |
| 16 |
class Customer { |
| 17 |
/** |
| 18 |
* Store customer id |
| 19 |
* |
| 20 |
* @var integer |
| 21 |
*/ |
| 22 |
protected $id; |
| 23 |
|
| 24 |
/** |
| 25 |
* Store meta prefix |
| 26 |
* |
| 27 |
* @var string |
| 28 |
*/ |
| 29 |
protected $meta_prefix = '_timetics_customer_'; |
| 30 |
|
| 31 |
/** |
| 32 |
* Store error |
| 33 |
* |
| 34 |
* @var Object |
| 35 |
*/ |
| 36 |
public $error; |
| 37 |
|
| 38 |
/** |
| 39 |
* Customer data |
| 40 |
* |
| 41 |
* @var array |
| 42 |
*/ |
| 43 |
protected $data = [ |
| 44 |
'image' => '', |
| 45 |
'first_name' => '', |
| 46 |
'last_name' => '', |
| 47 |
'email' => '', |
| 48 |
'phone' => '', |
| 49 |
'password' => '', |
| 50 |
'quantity' => [] |
| 51 |
]; |
| 52 |
|
| 53 |
/** |
| 54 |
* Customer constructor |
| 55 |
* |
| 56 |
* @return void |
| 57 |
*/ |
| 58 |
public function __construct( $customer = 0 ) { |
| 59 |
if ( $customer instanceof self ) { |
| 60 |
$this->set_id( $customer->get_id() ); |
| 61 |
} elseif ( ! empty( $customer->ID ) ) { |
| 62 |
$this->set_id( $customer->ID ); |
| 63 |
} elseif ( is_numeric( $customer ) && $customer > 0 ) { |
| 64 |
$this->set_id( $customer ); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Get customer id |
| 70 |
* |
| 71 |
* @return integer |
| 72 |
*/ |
| 73 |
public function get_id() { |
| 74 |
return $this->id; |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Get first name |
| 79 |
* |
| 80 |
* @return string |
| 81 |
*/ |
| 82 |
public function get_first_name() { |
| 83 |
return get_user_meta( $this->id, 'first_name', true ); |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Get last name |
| 88 |
* |
| 89 |
* @return string |
| 90 |
*/ |
| 91 |
public function get_last_name() { |
| 92 |
return get_user_meta( $this->id, 'last_name', true ); |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Get customer display name |
| 97 |
* |
| 98 |
* @return string |
| 99 |
*/ |
| 100 |
public function get_display_name() { |
| 101 |
return $this->get_first_name() . ' ' . $this->get_last_name(); |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Get email |
| 106 |
* |
| 107 |
* @return string |
| 108 |
*/ |
| 109 |
public function get_email() { |
| 110 |
$customer = get_userdata( $this->id ); |
| 111 |
|
| 112 |
if ( $customer ) { |
| 113 |
return $customer->user_email; |
| 114 |
} |
| 115 |
|
| 116 |
return ''; |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Get phone |
| 121 |
* |
| 122 |
* @return string |
| 123 |
*/ |
| 124 |
public function get_phone() { |
| 125 |
return $this->get_prop( 'phone' ); |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Get customer image |
| 130 |
* |
| 131 |
* @return string |
| 132 |
*/ |
| 133 |
public function get_image() { |
| 134 |
$image = $this->get_prop( 'image' ); |
| 135 |
|
| 136 |
if ( ! $image ) { |
| 137 |
return get_avatar_url( $this->id ); |
| 138 |
} |
| 139 |
|
| 140 |
return wp_get_attachment_image_url( $image ); |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* Get password |
| 145 |
* |
| 146 |
* @return string |
| 147 |
*/ |
| 148 |
public function get_password() { |
| 149 |
$user = get_user_by( 'id', $this->id ); |
| 150 |
return $user->user_pass; |
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* Get quantity |
| 155 |
* |
| 156 |
* @return array |
| 157 |
*/ |
| 158 |
public function get_quantity() { |
| 159 |
$all_quantity = $this->get_prop( 'quantity' ); |
| 160 |
$quantities = []; |
| 161 |
if ( is_array( $all_quantity ) ){ |
| 162 |
foreach( $all_quantity as $key => $quantity){ |
| 163 |
$quantities[$key] = $quantity; |
| 164 |
} |
| 165 |
} |
| 166 |
return $quantities; |
| 167 |
|
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* Get Attendees |
| 172 |
* |
| 173 |
* @return array |
| 174 |
*/ |
| 175 |
public function get_attendees() { |
| 176 |
$all_attendees = $this->get_prop( 'attendees' ); |
| 177 |
$attendees = []; |
| 178 |
|
| 179 |
if ( is_array( $all_attendees ) ) { |
| 180 |
foreach( $all_attendees as $key => $attendee ) { |
| 181 |
$attendees[] = [ |
| 182 |
'name' => $attendee['name'], |
| 183 |
'email' => $attendee['email'] |
| 184 |
]; |
| 185 |
} |
| 186 |
} |
| 187 |
return $attendees; |
| 188 |
|
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* Get prop |
| 193 |
* |
| 194 |
* @param string |
| 195 |
* |
| 196 |
* @return string |
| 197 |
*/ |
| 198 |
public function get_prop( $prop = '' ) { |
| 199 |
return $this->get_metadata( $prop ); |
| 200 |
} |
| 201 |
|
| 202 |
/** |
| 203 |
* Get customer metadata |
| 204 |
* |
| 205 |
* @param string $prop |
| 206 |
* |
| 207 |
* @return string |
| 208 |
*/ |
| 209 |
public function get_metadata( $prop = '' ) { |
| 210 |
if( $prop == 'first_name' || $prop == 'last_name' ) { |
| 211 |
$meta_key = $prop; |
| 212 |
} else { |
| 213 |
$meta_key = $this->meta_prefix . $prop; |
| 214 |
} |
| 215 |
|
| 216 |
return get_user_meta( $this->id, $meta_key, true ); |
| 217 |
|
| 218 |
} |
| 219 |
|
| 220 |
// Setters. |
| 221 |
/** |
| 222 |
* Set customer id |
| 223 |
* |
| 224 |
* @param integer $id |
| 225 |
* |
| 226 |
* @return void |
| 227 |
*/ |
| 228 |
public function set_id( $id ) { |
| 229 |
$this->id = $id; |
| 230 |
} |
| 231 |
|
| 232 |
/** |
| 233 |
* Set props |
| 234 |
* |
| 235 |
* @param array $args Customer Data |
| 236 |
* |
| 237 |
* @return void |
| 238 |
*/ |
| 239 |
public function set_props( $args = [] ) { |
| 240 |
$this->data = wp_parse_args( $args, $this->data ); |
| 241 |
} |
| 242 |
|
| 243 |
/** |
| 244 |
* Update meta data |
| 245 |
* |
| 246 |
* @return void |
| 247 |
*/ |
| 248 |
public function save_metadata() { |
| 249 |
foreach ( $this->data as $key => $value ) { |
| 250 |
// Prepare meta key. |
| 251 |
if( $key == 'first_name' || $key == 'last_name' ) { |
| 252 |
$meta_key = $key; |
| 253 |
} else { |
| 254 |
$meta_key = $this->meta_prefix . $key; |
| 255 |
} |
| 256 |
|
| 257 |
// Update appointment meta data. |
| 258 |
update_user_meta( $this->id, $meta_key, $value ); |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
/** |
| 263 |
* Check user is valid staff |
| 264 |
* |
| 265 |
* @return bool |
| 266 |
*/ |
| 267 |
public function is_customer() { |
| 268 |
$user = get_userdata( $this->id ); |
| 269 |
|
| 270 |
if ( $user ) { |
| 271 |
return in_array( 'timetics-customer', $user->roles, true ); |
| 272 |
} |
| 273 |
|
| 274 |
return true; |
| 275 |
} |
| 276 |
|
| 277 |
/** |
| 278 |
* Save customer |
| 279 |
* |
| 280 |
* @return void |
| 281 |
*/ |
| 282 |
public function save() { |
| 283 |
$args = [ |
| 284 |
'first_name' => $this->data['first_name'], |
| 285 |
'last_name' => $this->data['last_name'], |
| 286 |
'user_login' => $this->data['email'], |
| 287 |
'user_email' => $this->data['email'], |
| 288 |
]; |
| 289 |
|
| 290 |
$user_data = get_user_by( 'email', $this->data['email'] ); |
| 291 |
|
| 292 |
if ( $user_data && ! in_array( 'timetics-customer', $user_data->roles, true ) ) { |
| 293 |
$user_data->add_role( 'timetics-customer' ); |
| 294 |
|
| 295 |
return $user_data->ID; |
| 296 |
} |
| 297 |
|
| 298 |
if ( $this->id ) { |
| 299 |
$args['ID'] = $this->id; |
| 300 |
$args['user_pass'] = $this->data['password']; |
| 301 |
$customer_id = wp_update_user( $args ); |
| 302 |
} else { |
| 303 |
$args['role'] = 'timetics-customer'; |
| 304 |
$args['user_pass'] = wp_generate_password(); |
| 305 |
$customer_id = wp_insert_user( $args ); |
| 306 |
} |
| 307 |
|
| 308 |
if ( is_wp_error( $customer_id ) ) { |
| 309 |
$this->error = $customer_id; |
| 310 |
return; |
| 311 |
} |
| 312 |
|
| 313 |
$this->set_id( $customer_id ); |
| 314 |
$this->save_metadata(); |
| 315 |
} |
| 316 |
|
| 317 |
/** |
| 318 |
* Delete customer |
| 319 |
* |
| 320 |
* @return bool |
| 321 |
*/ |
| 322 |
public function delete() { |
| 323 |
require_once ABSPATH . 'wp-admin/includes/user.php'; |
| 324 |
$user = get_userdata( $this->id ); |
| 325 |
|
| 326 |
// Get all bookings for this customer. |
| 327 |
$bookings = $this->get_bookings(); |
| 328 |
|
| 329 |
// Delete all bookings for this customer. |
| 330 |
foreach ( $bookings as $booking ) { |
| 331 |
$booking = new Booking( $booking ); |
| 332 |
$booking->delete(); |
| 333 |
} |
| 334 |
|
| 335 |
if ( count( $user->roles ) > 1 ) { |
| 336 |
$user->remove_role('timetics-customer'); |
| 337 |
|
| 338 |
return true; |
| 339 |
} |
| 340 |
|
| 341 |
return wp_delete_user( $this->id ); |
| 342 |
} |
| 343 |
|
| 344 |
/** |
| 345 |
* Make customer using email |
| 346 |
* |
| 347 |
* @param array Customer data |
| 348 |
* |
| 349 |
* @return Customer |
| 350 |
*/ |
| 351 |
public function make( $args = [] ) { |
| 352 |
$defaults = [ |
| 353 |
'first_name' => '', |
| 354 |
'last_name' => '', |
| 355 |
'email' => '', |
| 356 |
'phone' => '', |
| 357 |
'password' => '', |
| 358 |
]; |
| 359 |
|
| 360 |
$args = wp_parse_args( $args, $defaults ); |
| 361 |
|
| 362 |
if ( email_exists( $args['email'] ) ) { |
| 363 |
$user = get_user_by( 'email', $args['email'] ); |
| 364 |
|
| 365 |
if ( $user && ! in_array( 'timetics-customer', $user->roles, true ) ) { |
| 366 |
$user->add_role( 'timetics-customer' ); |
| 367 |
$this->set_id( $user->ID ); |
| 368 |
|
| 369 |
return $user->ID; |
| 370 |
} |
| 371 |
|
| 372 |
$this->set_id( $user->ID ); |
| 373 |
$this->set_props( $args ); |
| 374 |
$this->save_metadata( $args ); |
| 375 |
|
| 376 |
} else { |
| 377 |
$this->set_props( $args ); |
| 378 |
$this->save(); |
| 379 |
} |
| 380 |
} |
| 381 |
|
| 382 |
/** |
| 383 |
* Get total booking for a customer |
| 384 |
* |
| 385 |
* @return integer |
| 386 |
*/ |
| 387 |
public function get_total_booking() { |
| 388 |
$bookings = $this->booking_query(); |
| 389 |
|
| 390 |
return $bookings->found_posts; |
| 391 |
} |
| 392 |
|
| 393 |
/** |
| 394 |
* Get all bookings by customer |
| 395 |
* |
| 396 |
* @return array |
| 397 |
*/ |
| 398 |
public function get_bookings() { |
| 399 |
$bookings = $this->booking_query(); |
| 400 |
$data = []; |
| 401 |
|
| 402 |
if ( ! empty( $bookings->posts ) ) { |
| 403 |
foreach( $bookings->posts as $booking ) { |
| 404 |
$object = new Booking( $booking->ID ); |
| 405 |
$data[] = $object->to_array(); |
| 406 |
} |
| 407 |
} |
| 408 |
|
| 409 |
return $data; |
| 410 |
} |
| 411 |
|
| 412 |
/** |
| 413 |
* Get query for bookings |
| 414 |
* |
| 415 |
* @return WP_Query |
| 416 |
*/ |
| 417 |
private function booking_query() { |
| 418 |
// Not tied to "default_booking_status" (that's what status a NEW |
| 419 |
// booking starts in, not which bookings count here) — otherwise an |
| 420 |
// approved booking can vanish from the customer's count/history the |
| 421 |
// moment it's approved, on any site where new bookings default to |
| 422 |
// pending. See core/reports/api-report.php for the same fix. |
| 423 |
$counted_statuses = apply_filters( 'timetics/report/counted_statuses', [ 'approved', 'completed' ] ); |
| 424 |
|
| 425 |
$query = new \WP_Query( |
| 426 |
[ |
| 427 |
'post_type' => 'timetics-booking', |
| 428 |
'post_status' => $counted_statuses, |
| 429 |
// @codingStandardsIgnoreStart |
| 430 |
'meta_key' => '_tt_booking_customer', |
| 431 |
'meta_value' => $this->id, |
| 432 |
// @codingStandardsIgnoreEnd |
| 433 |
] |
| 434 |
); |
| 435 |
|
| 436 |
return $query; |
| 437 |
} |
| 438 |
|
| 439 |
/** |
| 440 |
* Get all customer |
| 441 |
* |
| 442 |
* @param array $args Customer |
| 443 |
* |
| 444 |
* @return array |
| 445 |
*/ |
| 446 |
public static function all( $args = [] ) { |
| 447 |
$defaults = [ |
| 448 |
'role' => 'timetics-customer', |
| 449 |
'number' => 20, |
| 450 |
'paged' => 1, |
| 451 |
]; |
| 452 |
|
| 453 |
$args = wp_parse_args( $args, $defaults ); |
| 454 |
|
| 455 |
$users = new \WP_User_Query( $args ); |
| 456 |
|
| 457 |
return [ |
| 458 |
'total' => $users->get_total(), |
| 459 |
'items' => $users->get_results(), |
| 460 |
]; |
| 461 |
} |
| 462 |
} |
| 463 |
|