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 +110 -14 1.0.2 → 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 $this->get_prop( 'first_name' );
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 $this->get_prop( 'last_name' );
92 + return get_user_meta( $this->id, 'last_name', true );
91 93 }
92 94
93 95 /**
94 96 * Get customer display name
@@ -137,9 +139,57 @@
137 139
138 140 return wp_get_attachment_image_url( $image );
139 141 }
140 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 +
141 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 + /**
142 192 * Get prop
143 193 *
144 194 * @param string
145 195 *
@@ -156,11 +206,16 @@
156 206 *
157 207 * @return string
158 208 */
159 209 public function get_metadata( $prop = '' ) {
160 - $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 + }
161 215
162 216 return get_user_meta( $this->id, $meta_key, true );
217 +
163 218 }
164 219
165 220 // Setters.
166 221 /**
@@ -192,9 +247,13 @@
192 247 */
193 248 public function save_metadata() {
194 249 foreach ( $this->data as $key => $value ) {
195 250 // Prepare meta key.
196 - $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 + }
197 256
198 257 // Update appointment meta data.
199 258 update_user_meta( $this->id, $meta_key, $value );
200 259 }
@@ -225,18 +284,27 @@
225 284 'first_name' => $this->data['first_name'],
226 285 'last_name' => $this->data['last_name'],
227 286 'user_login' => $this->data['email'],
228 287 'user_email' => $this->data['email'],
229 - 'user_pass' => wp_generate_password(),
230 - 'role' => 'timetics-customer',
231 288 ];
232 289
233 - if ( $this->id ) {
234 - $args['ID'] = $this->id;
235 - 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;
236 296 }
237 297
238 - $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 + }
239 307
240 308 if ( is_wp_error( $customer_id ) ) {
241 309 $this->error = $customer_id;
242 310 return;
@@ -252,8 +320,9 @@
252 320 * @return bool
253 321 */
254 322 public function delete() {
255 323 require_once ABSPATH . 'wp-admin/includes/user.php';
324 + $user = get_userdata( $this->id );
256 325
257 326 // Get all bookings for this customer.
258 327 $bookings = $this->get_bookings();
259 328
@@ -262,8 +331,14 @@
262 331 $booking = new Booking( $booking );
263 332 $booking->delete();
264 333 }
265 334
335 + if ( count( $user->roles ) > 1 ) {
336 + $user->remove_role('timetics-customer');
337 +
338 + return true;
339 + }
340 +
266 341 return wp_delete_user( $this->id );
267 342 }
268 343
269 344 /**
@@ -278,8 +353,9 @@
278 353 'first_name' => '',
279 354 'last_name' => '',
280 355 'email' => '',
281 356 'phone' => '',
357 + 'password' => '',
282 358 ];
283 359
284 360 $args = wp_parse_args( $args, $defaults );
285 361
@@ -285,14 +361,19 @@
285 361
286 362 if ( email_exists( $args['email'] ) ) {
287 363 $user = get_user_by( 'email', $args['email'] );
288 364
289 - if ( ! $this->is_customer() ) {
365 + if ( $user && ! in_array( 'timetics-customer', $user->roles, true ) ) {
290 366 $user->add_role( 'timetics-customer' );
367 + $this->set_id( $user->ID );
368 +
369 + return $user->ID;
291 370 }
371 +
292 372 $this->set_id( $user->ID );
293 373 $this->set_props( $args );
294 - $this->save_metadata();
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