| @@ -45,8 +45,10 @@ | ||
| 45 | 45 | 'first_name' => '', |
| 46 | 46 | 'last_name' => '', |
| 47 | 47 | 'email' => '', |
| 48 | 48 | 'phone' => '', |
| 49 | + 'password' => '', | |
| 50 | + 'quantity' => [] | |
| 49 | 51 | ]; |
| 50 | 52 | |
| 51 | 53 | /** |
| 52 | 54 | * Customer constructor |
| @@ -77,9 +79,9 @@ | ||
| 77 | 79 | * |
| 78 | 80 | * @return string |
| 79 | 81 | */ |
| 80 | 82 | public function get_first_name() { |
| 81 | - return get_user_meta( $this->id, 'first_name', true ); | |
| 83 | + return get_user_meta( $this->id, 'first_name', true ); | |
| 82 | 84 | } |
| 83 | 85 | |
| 84 | 86 | /** |
| 85 | 87 | * Get last name |
| @@ -86,9 +88,9 @@ | ||
| 86 | 88 | * |
| 87 | 89 | * @return string |
| 88 | 90 | */ |
| 89 | 91 | public function get_last_name() { |
| 90 | - return get_user_meta( $this->id, 'last_name', true ); | |
| 92 | + return get_user_meta( $this->id, 'last_name', true ); | |
| 91 | 93 | } |
| 92 | 94 | |
| 93 | 95 | /** |
| 94 | 96 | * Get customer display name |
| @@ -95,17 +97,9 @@ | ||
| 95 | 97 | * |
| 96 | 98 | * @return string |
| 97 | 99 | */ |
| 98 | 100 | public function get_display_name() { |
| 99 | - $user = get_userdata( $this->id ); | |
| 100 | - | |
| 101 | - $display_name = ''; | |
| 102 | - | |
| 103 | - if ( $user ) { | |
| 104 | - $display_name = $user->display_name; | |
| 105 | - } | |
| 106 | - | |
| 107 | - return $display_name; | |
| 101 | + return $this->get_first_name() . ' ' . $this->get_last_name(); | |
| 108 | 102 | } |
| 109 | 103 | |
| 110 | 104 | /** |
| 111 | 105 | * Get email |
| @@ -117,9 +111,9 @@ | ||
| 117 | 111 | |
| 118 | 112 | if ( $customer ) { |
| 119 | 113 | return $customer->user_email; |
| 120 | 114 | } |
| 121 | - | |
| 115 | + | |
| 122 | 116 | return ''; |
| 123 | 117 | } |
| 124 | 118 | |
| 125 | 119 | /** |
| @@ -145,9 +139,57 @@ | ||
| 145 | 139 | |
| 146 | 140 | return wp_get_attachment_image_url( $image ); |
| 147 | 141 | } |
| 148 | 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 | + | |
| 149 | 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 | + /** | |
| 150 | 192 | * Get prop |
| 151 | 193 | * |
| 152 | 194 | * @param string |
| 153 | 195 | * |
| @@ -164,11 +206,16 @@ | ||
| 164 | 206 | * |
| 165 | 207 | * @return string |
| 166 | 208 | */ |
| 167 | 209 | public function get_metadata( $prop = '' ) { |
| 168 | - $meta_key = $this->meta_prefix . $prop; | |
| 210 | + if( $prop == 'first_name' || $prop == 'last_name' ) { | |
| 211 | + $meta_key = $prop; | |
| 212 | + } else { | |
| 213 | + $meta_key = $this->meta_prefix . $prop; | |
| 214 | + } | |
| 169 | 215 | |
| 170 | 216 | return get_user_meta( $this->id, $meta_key, true ); |
| 217 | + | |
| 171 | 218 | } |
| 172 | 219 | |
| 173 | 220 | // Setters. |
| 174 | 221 | /** |
| @@ -200,9 +247,13 @@ | ||
| 200 | 247 | */ |
| 201 | 248 | public function save_metadata() { |
| 202 | 249 | foreach ( $this->data as $key => $value ) { |
| 203 | 250 | // Prepare meta key. |
| 204 | - $meta_key = $this->meta_prefix . $key; | |
| 251 | + if( $key == 'first_name' || $key == 'last_name' ) { | |
| 252 | + $meta_key = $key; | |
| 253 | + } else { | |
| 254 | + $meta_key = $this->meta_prefix . $key; | |
| 255 | + } | |
| 205 | 256 | |
| 206 | 257 | // Update appointment meta data. |
| 207 | 258 | update_user_meta( $this->id, $meta_key, $value ); |
| 208 | 259 | } |
| @@ -233,18 +284,27 @@ | ||
| 233 | 284 | 'first_name' => $this->data['first_name'], |
| 234 | 285 | 'last_name' => $this->data['last_name'], |
| 235 | 286 | 'user_login' => $this->data['email'], |
| 236 | 287 | 'user_email' => $this->data['email'], |
| 237 | - 'user_pass' => wp_generate_password(), | |
| 238 | - 'role' => 'timetics-customer', | |
| 239 | 288 | ]; |
| 240 | 289 | |
| 241 | - if ( $this->id ) { | |
| 242 | - $args['ID'] = $this->id; | |
| 243 | - unset( $args['user_email'] ); | |
| 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; | |
| 244 | 296 | } |
| 245 | 297 | |
| 246 | - $customer_id = wp_insert_user( $args ); | |
| 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 | + } | |
| 247 | 307 | |
| 248 | 308 | if ( is_wp_error( $customer_id ) ) { |
| 249 | 309 | $this->error = $customer_id; |
| 250 | 310 | return; |
| @@ -260,8 +320,9 @@ | ||
| 260 | 320 | * @return bool |
| 261 | 321 | */ |
| 262 | 322 | public function delete() { |
| 263 | 323 | require_once ABSPATH . 'wp-admin/includes/user.php'; |
| 324 | + $user = get_userdata( $this->id ); | |
| 264 | 325 | |
| 265 | 326 | // Get all bookings for this customer. |
| 266 | 327 | $bookings = $this->get_bookings(); |
| 267 | 328 | |
| @@ -270,8 +331,14 @@ | ||
| 270 | 331 | $booking = new Booking( $booking ); |
| 271 | 332 | $booking->delete(); |
| 272 | 333 | } |
| 273 | 334 | |
| 335 | + if ( count( $user->roles ) > 1 ) { | |
| 336 | + $user->remove_role('timetics-customer'); | |
| 337 | + | |
| 338 | + return true; | |
| 339 | + } | |
| 340 | + | |
| 274 | 341 | return wp_delete_user( $this->id ); |
| 275 | 342 | } |
| 276 | 343 | |
| 277 | 344 | /** |
| @@ -286,8 +353,9 @@ | ||
| 286 | 353 | 'first_name' => '', |
| 287 | 354 | 'last_name' => '', |
| 288 | 355 | 'email' => '', |
| 289 | 356 | 'phone' => '', |
| 357 | + 'password' => '', | |
| 290 | 358 | ]; |
| 291 | 359 | |
| 292 | 360 | $args = wp_parse_args( $args, $defaults ); |
| 293 | 361 | |
| @@ -293,12 +361,19 @@ | ||
| 293 | 361 | |
| 294 | 362 | if ( email_exists( $args['email'] ) ) { |
| 295 | 363 | $user = get_user_by( 'email', $args['email'] ); |
| 296 | 364 | |
| 297 | - if ( ! $this->is_customer() ) { | |
| 365 | + if ( $user && ! in_array( 'timetics-customer', $user->roles, true ) ) { | |
| 298 | 366 | $user->add_role( 'timetics-customer' ); |
| 367 | + $this->set_id( $user->ID ); | |
| 368 | + | |
| 369 | + return $user->ID; | |
| 299 | 370 | } |
| 371 | + | |
| 300 | 372 | $this->set_id( $user->ID ); |
| 373 | + $this->set_props( $args ); | |
| 374 | + $this->save_metadata( $args ); | |
| 375 | + | |
| 301 | 376 | } else { |
| 302 | 377 | $this->set_props( $args ); |
| 303 | 378 | $this->save(); |
| 304 | 379 | } |
| @@ -321,10 +396,18 @@ | ||
| 321 | 396 | * @return array |
| 322 | 397 | */ |
| 323 | 398 | public function get_bookings() { |
| 324 | 399 | $bookings = $this->booking_query(); |
| 400 | + $data = []; | |
| 325 | 401 | |
| 326 | - return $bookings->posts; | |
| 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; | |
| 327 | 410 | } |
| 328 | 411 | |
| 329 | 412 | /** |
| 330 | 413 | * Get query for bookings |
| @@ -331,12 +414,19 @@ | ||
| 331 | 414 | * |
| 332 | 415 | * @return WP_Query |
| 333 | 416 | */ |
| 334 | 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 | + | |
| 335 | 425 | $query = new \WP_Query( |
| 336 | 426 | [ |
| 337 | 427 | 'post_type' => 'timetics-booking', |
| 338 | - 'post_status' => 'publish', | |
| 428 | + 'post_status' => $counted_statuses, | |
| 339 | 429 | // @codingStandardsIgnoreStart |
| 340 | 430 | 'meta_key' => '_tt_booking_customer', |
| 341 | 431 | 'meta_value' => $this->id, |
| 342 | 432 | // @codingStandardsIgnoreEnd |