PluginProbe
Timetics – Appointment Booking Calendar & Scheduling / 1.0.64
Timetics – Appointment Booking Calendar & Scheduling v1.0.64
1.0.64 1.0.62 1.0.63 1.0.61 1.0.60 1.0.59 1.0.58 1.0.57 1.0.56 trunk 1.0.0 1.0.1 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.15 1.0.16 1.0.17 1.0.18 1.0.19 1.0.2 1.0.20 1.0.21 All 65 releases
← All changes | core/customers/customer.php +119 -23 1.0.0 → 1.0.64 View file →
@@ -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
@@ -112,9 +106,15 @@
112 106 *
113 107 * @return string
114 108 */
115 109 public function get_email() {
116 - return get_userdata( $this->id )->user_email;
110 + $customer = get_userdata( $this->id );
111 +
112 + if ( $customer ) {
113 + return $customer->user_email;
114 + }
115 +
116 + return '';
117 117 }
118 118
119 119 /**
120 120 * Get phone
@@ -139,9 +139,57 @@
139 139
140 140 return wp_get_attachment_image_url( $image );
141 141 }
142 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 +
143 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 + /**
144 192 * Get prop
145 193 *
146 194 * @param string
147 195 *
@@ -158,11 +206,16 @@
158 206 *
159 207 * @return string
160 208 */
161 209 public function get_metadata( $prop = '' ) {
162 - $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 + }
163 215
164 216 return get_user_meta( $this->id, $meta_key, true );
217 +
165 218 }
166 219
167 220 // Setters.
168 221 /**
@@ -194,9 +247,13 @@
194 247 */
195 248 public function save_metadata() {
196 249 foreach ( $this->data as $key => $value ) {
197 250 // Prepare meta key.
198 - $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 + }
199 256
200 257 // Update appointment meta data.
201 258 update_user_meta( $this->id, $meta_key, $value );
202 259 }
@@ -227,18 +284,27 @@
227 284 'first_name' => $this->data['first_name'],
228 285 'last_name' => $this->data['last_name'],
229 286 'user_login' => $this->data['email'],
230 287 'user_email' => $this->data['email'],
231 - 'user_pass' => wp_generate_password(),
232 - 'role' => 'timetics-customer',
233 288 ];
234 289
235 - if ( $this->id ) {
236 - $args['ID'] = $this->id;
237 - 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;
238 296 }
239 297
240 - $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 + }
241 307
242 308 if ( is_wp_error( $customer_id ) ) {
243 309 $this->error = $customer_id;
244 310 return;
@@ -254,8 +320,9 @@
254 320 * @return bool
255 321 */
256 322 public function delete() {
257 323 require_once ABSPATH . 'wp-admin/includes/user.php';
324 + $user = get_userdata( $this->id );
258 325
259 326 // Get all bookings for this customer.
260 327 $bookings = $this->get_bookings();
261 328
@@ -264,8 +331,14 @@
264 331 $booking = new Booking( $booking );
265 332 $booking->delete();
266 333 }
267 334
335 + if ( count( $user->roles ) > 1 ) {
336 + $user->remove_role('timetics-customer');
337 +
338 + return true;
339 + }
340 +
268 341 return wp_delete_user( $this->id );
269 342 }
270 343
271 344 /**
@@ -280,8 +353,9 @@
280 353 'first_name' => '',
281 354 'last_name' => '',
282 355 'email' => '',
283 356 'phone' => '',
357 + 'password' => '',
284 358 ];
285 359
286 360 $args = wp_parse_args( $args, $defaults );
287 361
@@ -287,12 +361,19 @@
287 361
288 362 if ( email_exists( $args['email'] ) ) {
289 363 $user = get_user_by( 'email', $args['email'] );
290 364
291 - if ( ! $this->is_customer() ) {
365 + if ( $user && ! in_array( 'timetics-customer', $user->roles, true ) ) {
292 366 $user->add_role( 'timetics-customer' );
367 + $this->set_id( $user->ID );
368 +
369 + return $user->ID;
293 370 }
371 +
294 372 $this->set_id( $user->ID );
373 + $this->set_props( $args );
374 + $this->save_metadata( $args );
375 +
295 376 } else {
296 377 $this->set_props( $args );
297 378 $this->save();
298 379 }
@@ -315,10 +396,18 @@
315 396 * @return array
316 397 */
317 398 public function get_bookings() {
318 399 $bookings = $this->booking_query();
400 + $data = [];
319 401
320 - 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;
321 410 }
322 411
323 412 /**
324 413 * Get query for bookings
@@ -325,12 +414,19 @@
325 414 *
326 415 * @return WP_Query
327 416 */
328 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 +
329 425 $query = new \WP_Query(
330 426 [
331 427 'post_type' => 'timetics-booking',
332 - 'post_status' => 'publish',
428 + 'post_status' => $counted_statuses,
333 429 // @codingStandardsIgnoreStart
334 430 'meta_key' => '_tt_booking_customer',
335 431 'meta_value' => $this->id,
336 432 // @codingStandardsIgnoreEnd