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/bookings/api-booking.php +254 -78 1.0.61 → 1.0.64 View file →
@@ -5,8 +5,10 @@
5 5 * @package Timetics
6 6 */
7 7 namespace Timetics\Core\Bookings;
8 8
9 +defined( 'ABSPATH' ) || exit;
10 +
9 11 use Error;
10 12 use Timetics\Base\Api;
11 13 use Timetics\Core\Appointments\Api_Appointment;
12 14 use Timetics\Core\Appointments\Appointment;
@@ -140,9 +142,11 @@
140 142 [
141 143 'methods' => \WP_REST_Server::READABLE,
142 144 'callback' => [$this, 'search_items'],
143 145 'permission_callback' => function () {
144 - return current_user_can( 'edit_posts' );
146 + // edit_booking is admin-only in this plugin (see get_items()) —
147 + // staff need manage_timetics to search their own bookings at all.
148 + return current_user_can( 'manage_timetics' ) || current_user_can( 'manage_options' );
145 149 },
146 150 ],
147 151 ]
148 152 );
@@ -205,9 +209,9 @@
205 209 $bookings = Booking::all( $args );
206 210 $items = [];
207 211
208 212 foreach ( $bookings['items'] as $item ) {
209 - $items[] = $this->prepare_item( $item->ID );
213 + $items[] = $this->prepare_item( $item->ID, false );
210 214 }
211 215
212 216 /**
213 217 * Added temporary for leagacy sass. It will remove in future.
@@ -435,16 +439,25 @@
435 439 $per_page = ! empty( $request['per_page'] ) ? intval( $request['per_page'] ) : 20;
436 440 $paged = ! empty( $request['paged'] ) ? intval( $request['paged'] ) : 1;
437 441 $search = ! empty( $request['search'] ) ? sanitize_text_field( $request['search'] ) : '';
438 442
443 + $query_args = array(
444 + 'post_type' => 'timetics-booking',
445 + 'posts_per_page' => $per_page,
446 + 'paged' => $paged,
447 + 'post_status' => 'any',
448 + );
449 +
450 + if ( ! current_user_can( 'manage_options' ) ) {
451 + $allowed_ids = Booking::get_visible_ids_for_user( get_current_user_id() );
452 + $query_args['post__in'] = ! empty( $allowed_ids ) ? $allowed_ids : [ 0 ];
453 + }
454 +
439 455 // Get search.
440 456 $booking = new WP_Query(
441 - array(
442 - 'post_type' => 'timetics-booking',
443 - 'posts_per_page' => $per_page,
444 - 'paged' => $paged,
445 - 'post_status' => 'any',
446 -
457 + array_merge(
458 + $query_args,
459 + array(
447 460 // @codingStandardsIgnoreStart
448 461 'meta_query' => array(
449 462 'relation' => 'OR',
450 463 array(
@@ -498,8 +511,9 @@
498 511 'compare' => 'LIKE',
499 512 ),
500 513 ),
501 514 // @codingStandardsIgnoreEnd
515 + )
502 516 )
503 517 );
504 518
505 519 // Prepare items for response.
@@ -505,9 +519,9 @@
505 519 // Prepare items for response.
506 520 $items = [];
507 521
508 522 foreach ( $booking->posts as $item ) {
509 - $items[] = $this->prepare_item( $item->ID );
523 + $items[] = $this->prepare_item( $item->ID, false );
510 524 }
511 525
512 526 /**
513 527 * Added temporary for leagacy sass. It will remove in future.
@@ -602,10 +616,12 @@
602 616 );
603 617 }
604 618
605 619 // Idempotency: refuse re-approval of a booking that already finalized.
620 + // 'failed' is deliberately not in this list — a declined card is a failed
621 + // attempt, not a finished booking, and the customer retries on the same one.
606 622 $current_status = (string) $booking->get_status();
607 - $finalized_statuses = [ 'approved', 'completed', 'failed', 'cancelled', 'cancel' ];
623 + $finalized_statuses = [ 'approved', 'completed', 'cancelled', 'cancel' ];
608 624 if ( in_array( $current_status, $finalized_statuses, true ) ) {
609 625 return new WP_HTTP_Response(
610 626 [
611 627 'success' => 0,
@@ -703,8 +719,18 @@
703 719 }
704 720 } elseif ( 'failed' === $client_status ) {
705 721 // Marking the user's own attempt as failed never grants access; safe to honor.
706 722 $verified_status = 'failed';
723 + } else {
724 + // Gateways that live outside this plugin ( PayPal ) check the payment
725 + // against their own API and answer with the status they trust. The
726 + // default stays 'pending', so a client that sends nothing verifiable
727 + // cannot talk its way to 'succeeded'.
728 + $verified_status = (string) apply_filters( 'timetics_verify_payment', $verified_status, $payment_method, $data, $booking );
729 +
730 + if ( ! in_array( $verified_status, ['pending', 'failed', 'succeeded'], true ) ) {
731 + $verified_status = 'pending';
732 + }
707 733 }
708 734 // Other payment methods (cash, on-site, etc.) stay pending here. They
709 735 // are approved through their own authenticated/admin paths.
710 736 $post_status = 'succeeded' === $verified_status
@@ -713,11 +739,14 @@
713 739
714 740 $finalizing = 'succeeded' === $verified_status && '' !== $stored_intent_id;
715 741
716 742 if ( $finalizing ) {
717 - $claimed = add_post_meta( $booking_id, '_tt_stripe_payment_intent_id', $stored_intent_id, true );
743 + // Separate key from _tt_stripe_payment_intent_id: that one is written at
744 + // bind time (before payment) so the cleanup sweep can see it, so it can't
745 + // double as a "not yet finalized" marker here — it always already exists.
746 + $claimed = add_post_meta( $booking_id, '_tt_stripe_payment_finalized_intent_id', $stored_intent_id, true );
718 747 if ( false === $claimed ) {
719 - $existing = (string) get_post_meta( $booking_id, '_tt_stripe_payment_intent_id', true );
748 + $existing = (string) get_post_meta( $booking_id, '_tt_stripe_payment_finalized_intent_id', true );
720 749 if ( $existing !== $stored_intent_id ) {
721 750 return new WP_HTTP_Response(
722 751 [
723 752 'success' => 0,
@@ -752,9 +781,9 @@
752 781
753 782 if ( is_wp_error( $update ) ) {
754 783 // Roll back the claim so a retry can finalize cleanly.
755 784 if ( $finalizing ) {
756 - delete_post_meta( $booking_id, '_tt_stripe_payment_intent_id', $stored_intent_id );
785 + delete_post_meta( $booking_id, '_tt_stripe_payment_finalized_intent_id', $stored_intent_id );
757 786 }
758 787 return new WP_HTTP_Response(
759 788 [
760 789 'success' => 0,
@@ -771,9 +800,14 @@
771 800 if ( 'failed' === $post_status ) {
772 801 $booking->release_slot();
773 802 }
774 803
775 - if ( $default_booking_status === $post_status ) {
804 + // Approve, notify and burn the token only when the payment actually
805 + // cleared. This used to compare $post_status against the site default,
806 + // which is the very same string on a site whose default booking status
807 + // is 'pending' - so an unverified attempt still sent the "meeting
808 + // scheduled" emails and rotated the token without a penny being paid.
809 + if ( 'succeeded' === $verified_status ) {
776 810 // Rotate the security token so the same one cannot drive a second
777 811 // approval after this booking has finalized.
778 812 $booking->rotate_security_token();
779 813
@@ -901,9 +935,23 @@
901 935 } else {
902 936 $status = $default_status;
903 937 }
904 938 } else {
905 - $current_status = ( new Booking( $id ) )->get_status();
939 + $current_booking = new Booking( $id );
940 +
941 + // Reschedule only moves time.
942 + if ( (int) $current_booking->get_appointment() !== $appointment ) {
943 + return new WP_HTTP_Response(
944 + [
945 + 'status_code' => 403,
946 + 'success' => 0,
947 + 'message' => esc_html__( 'You can not change the appointment of a booking.', 'timetics' ),
948 + ],
949 + 403
950 + );
951 + }
952 +
953 + $current_status = $current_booking->get_status();
906 954 if ( 'cancel' === $client_status ) {
907 955 $status = 'cancel';
908 956 } else {
909 957 $status = $current_status;
@@ -927,20 +975,28 @@
927 975 // Use the validated email from the security check
928 976 $email = $email_validation;
929 977 }
930 978
931 - $validate = $this->validate(
932 - $data, [
933 - 'first_name',
934 - 'email',
935 - 'payment_method',
936 - 'appointment',
937 - 'start_date',
938 - 'start_time',
939 - 'end_time',
940 - ]
941 - );
979 + $required_fields = [
980 + 'first_name',
981 + 'email',
982 + 'appointment',
983 + 'start_date',
984 + 'start_time',
985 + 'end_time',
986 + ];
942 987
988 + // Payment method is only chosen once, at booking creation. Later
989 + // updates (status change, reschedule, staff swap, ...) shouldn't have
990 + // to resubmit it — requiring it here made admin actions like
991 + // cancelling from the calendar popover fail whenever the form didn't
992 + // carry the original payment method in its state.
993 + if ( 'created' === $action ) {
994 + $required_fields[] = 'payment_method';
995 + }
996 +
997 + $validate = $this->validate( $data, $required_fields );
998 +
943 999 if ( is_wp_error( $validate ) ) {
944 1000 $data = [
945 1001 'status_code' => 403,
946 1002 'success' => 0,
@@ -996,30 +1052,40 @@
996 1052 'phone' => $phone,
997 1053 ]
998 1054 );
999 1055
1000 - // Update booking schedule.
1056 + // Update booking schedule. Release the slot the booking currently holds;
1057 + // the new one is taken further below.
1001 1058 if ( $id ) {
1059 + // Entries are stored in the meeting's timezone, so the booking's own
1060 + // date/time has to be converted before the lookup. Without this the
1061 + // entry is missed whenever the two timezones differ and it stays
1062 + // behind blocking a slot nobody holds.
1063 + $old_meeting = new Appointment( $booking->get_appointment() );
1064 + $old_datetime = timetics_convert_timezone(
1065 + $booking->get_start_date() . ' ' . $booking->get_start_time(),
1066 + $booking->get_timezone(),
1067 + $old_meeting->get_timezone()
1068 + );
1002 1069
1003 1070 $entries = $booking_entry->find(
1004 1071 [
1005 1072 'staff_id' => $booking->get_staff_id(),
1006 1073 'meeting_id' => $booking->get_appointment(),
1007 - 'date' => $booking->get_start_date(),
1008 - 'start' => $booking->get_start_time(),
1074 + 'date' => $old_datetime->format( 'Y-m-d' ),
1075 + 'start' => $old_datetime->format( 'h:i a' ),
1009 1076 ]
1010 -
1011 1077 );
1012 1078
1013 1079 if ( $entries ) {
1014 1080 $entry = $booking_entry->first();
1015 1081
1016 - if ( 'one-to-one' == strtolower( $meeting->get_type() ) ) {
1082 + if ( 'one-to-one' == strtolower( $old_meeting->get_type() ) ) {
1017 1083 $entry->delete();
1018 1084 } else {
1019 1085 $booked = intval( $entry->get_booked() ) - 1;
1020 1086 $booked_data = apply_filters( 'timetics_booking_update_schedule', $entry, ['booked' => $booked], $data, $booking );
1021 - $entry->update( $booked_data );
1087 + $entry->update( $this->normalize_schedule_update( $booked_data, $booked ) );
1022 1088 }
1023 1089 }
1024 1090 }
1025 1091
@@ -1048,9 +1114,9 @@
1048 1114 'date' => $date,
1049 1115 'end_date' => $end_date,
1050 1116 'start_time' => $start_time,
1051 1117 'end_time' => $end_time,
1052 - 'order_total' => $this->calculate_order_total( $data ),
1118 + 'order_total' => ( $id && ! $is_privileged ) ? $booking->get_total() : $this->calculate_order_total( $data ),
1053 1119 'post_status' => $status,
1054 1120 'location' => $location,
1055 1121 'location_type' => $location_type,
1056 1122 'timezone' => $timezone,
@@ -1056,8 +1122,12 @@
1056 1122 'timezone' => $timezone,
1057 1123 'cancel_reason' => $cancel_reason,
1058 1124 ];
1059 1125
1126 + if ( 'created' === $action && '' !== $payment_method ) {
1127 + $booking_props['payment_method'] = $payment_method;
1128 + }
1129 +
1060 1130 $old_meeting_timestamp = 0;
1061 1131
1062 1132 if ( $id ) {
1063 1133 $old_start_date = $booking->get_start_date();
@@ -1111,8 +1181,21 @@
1111 1181 /**
1112 1182 * Added temporary for leagacy sass. It will remove in future.
1113 1183 */
1114 1184 do_action( 'timetics/admin/booking/after_delete_item', $booking );
1185 +
1186 + /**
1187 + * Fired when an existing booking is cancelled.
1188 + *
1189 + * Cancel had no dedicated hook before, so integrations could
1190 + * only react to create/reschedule/delete.
1191 + *
1192 + * @param int $booking_id Booking ID.
1193 + * @param int $customer_id Customer ID.
1194 + * @param int $meeting_id Meeting (appointment) ID.
1195 + * @param array $data Request data.
1196 + */
1197 + do_action( 'timetics_after_booking_cancel', $booking->get_id(), $customer->get_id(), $meeting->get_id(), $data );
1115 1198 } else {
1116 1199 // Check if the booking date/time was actually changed
1117 1200 $date_time_changed = (
1118 1201 $old_start_date !== $start_date ||
@@ -1150,8 +1233,22 @@
1150 1233 $reschedule_hook_data['previous_meeting_date_timestamp'] = $old_meeting_timestamp;
1151 1234 }
1152 1235
1153 1236 do_action( 'timetics_gln_hook', 'booking_rescheduled', $reschedule_hook_data );
1237 +
1238 + /**
1239 + * Fired when a booking's date or time actually changed.
1240 + *
1241 + * `timetics_after_booking_schedule` runs on every save, so
1242 + * it cannot tell a reschedule from an edit of the phone
1243 + * number. This one only fires on a real time change.
1244 + *
1245 + * @param int $booking_id Booking ID.
1246 + * @param int $customer_id Customer ID.
1247 + * @param int $meeting_id Meeting (appointment) ID.
1248 + * @param array $data Request data.
1249 + */
1250 + do_action( 'timetics_after_booking_reschedule', $booking->get_id(), $customer->get_id(), $meeting->get_id(), $data );
1154 1251 }
1155 1252 }
1156 1253 }
1157 1254
@@ -1158,49 +1255,44 @@
1158 1255 // Convert booking time to staff/meeting time.
1159 1256 $date_time = timetics_convert_timezone( $start_date . ' ' . $start_time, $timezone, $meeting->get_timezone() );
1160 1257 $end_time = timetics_convert_timezone( $start_date . ' ' . $end_time, $timezone, $meeting->get_timezone() );
1161 1258
1162 - // Create booking schedule.
1163 - $entries = $booking_entry->find(
1164 - [
1165 - 'staff_id' => $staff->get_id(),
1166 - 'meeting_id' => $meeting->get_id(),
1167 - 'date' => $date_time->format( 'Y-m-d' ),
1168 - 'start' => $date_time->format( 'h:i a' ),
1169 - ]
1170 - );
1259 + // Create booking schedule. Skipped on cancel — the slot for this
1260 + // booking was already released above, and re-running this block would
1261 + // either recreate the just-deleted entry (one-to-one) or double the
1262 + // decrement (group), re-blocking or over-freeing the slot.
1263 + if ( 'cancel' !== $status ) {
1264 + $entries = $booking_entry->find(
1265 + [
1266 + 'staff_id' => $staff->get_id(),
1267 + 'meeting_id' => $meeting->get_id(),
1268 + 'date' => $date_time->format( 'Y-m-d' ),
1269 + 'start' => $date_time->format( 'h:i a' ),
1270 + ]
1271 + );
1171 1272
1172 - if ( $entries ) {
1173 - $entry = $booking_entry->first();
1273 + if ( $entries ) {
1274 + $entry = $booking_entry->first();
1174 1275
1175 - if ( 'cancel' === $status ) {
1176 - $booked = intval( $entry->get_booked() ) - 1;
1276 + $booked = intval( $entry->get_booked() ) + 1;
1277 + $booked_data = apply_filters( 'timetics_booking_update_schedule', $entry, ['booked' => $booked], $data, $booking );
1278 +
1279 + $entry->update( $this->normalize_schedule_update( $booked_data, $booked ) );
1177 1280 } else {
1178 - $booked = intval( $entry->get_booked() ) + 1;
1179 - }
1281 + $book_entry_data = [
1282 + 'meeting_id' => $meeting->get_id(),
1283 + 'staff_id' => $staff->get_id(),
1284 + 'customer_id' => $customer->get_id(),
1285 + 'booking_id' => $booking->get_id(),
1286 + 'booked' => 1,
1287 + 'date' => $date_time->format( 'Y-m-d' ),
1288 + 'start' => $date_time->format( 'h:i a' ),
1289 + 'end' => $end_time->format( 'h:i a' ),
1290 + ];
1180 1291
1181 - $booked_data = apply_filters( 'timetics_booking_update_schedule', $entry, ['booked' => $booked], $data, $booking );
1182 -
1183 - if ( 'cancel' === $status && 'one-to-one' == strtolower( $meeting->get_type() ) ) {
1184 - $entry->delete();
1185 - } else {
1186 - $entry->update( $booked_data );
1292 + $book_entry_data = apply_filters( 'timetics_booking_schedule', $book_entry_data, $data );
1293 + $booking_entry->create( $book_entry_data );
1187 1294 }
1188 -
1189 - } else {
1190 - $book_entry_data = [
1191 - 'meeting_id' => $meeting->get_id(),
1192 - 'staff_id' => $staff->get_id(),
1193 - 'customer_id' => $customer->get_id(),
1194 - 'booking_id' => $booking->get_id(),
1195 - 'booked' => 1,
1196 - 'date' => $date_time->format( 'Y-m-d' ),
1197 - 'start' => $date_time->format( 'h:i a' ),
1198 - 'end' => $end_time->format( 'h:i a' ),
1199 - ];
1200 -
1201 - $book_entry_data = apply_filters( 'timetics_booking_schedule', $book_entry_data, $data );
1202 - $booking_entry->create( $book_entry_data );
1203 1295 }
1204 1296
1205 1297 // For newly created bookings, create the calendar event now that the
1206 1298 // booking schedule entry exists. This generates the Google Meet link
@@ -1205,9 +1297,20 @@
1205 1297 // For newly created bookings, create the calendar event now that the
1206 1298 // booking schedule entry exists. This generates the Google Meet link
1207 1299 // (stored in booking meta) so it can be shown on the success page and
1208 1300 // included in the notification emails sent below.
1209 - if ( 'created' === $action && 'cancel' !== $status ) {
1301 + //
1302 + // Skipped while an online gateway payment is still outstanding — the
1303 + // real event gets created once payment confirms, in make_payment() and
1304 + // Hooks::update_booking_payment_status(). Based on payment_method and
1305 + // amount alone, NOT $status: a privileged (logged-in admin/staff) user
1306 + // gets $default_status regardless of gateway, which can be 'approved'
1307 + // even though no payment happened yet — checking $status here would
1308 + // miss that and create the event before the customer actually pays.
1309 + $is_awaiting_online_payment = 'created' === $action && $server_total > 0
1310 + && in_array( $payment_method_l, [ 'stripe', 'woocommerce', 'paypal' ], true );
1311 +
1312 + if ( 'created' === $action && 'cancel' !== $status && ! $is_awaiting_online_payment ) {
1210 1313 $booking->create_event();
1211 1314 }
1212 1315
1213 1316 // Send booking creation emails for new bookings not processed through
@@ -1252,9 +1355,9 @@
1252 1355 * @param integer $booking_id
1253 1356 *
1254 1357 * @return array
1255 1358 */
1256 - public function prepare_item( $booking_id ) {
1359 + public function prepare_item( $booking_id, $expose_token = true ) {
1257 1360 $booking = new Booking( $booking_id );
1258 1361 $appointment = new Appointment( $booking->get_appointment() );
1259 1362 $staff = new Staff( $booking->get_staff_id() );
1260 1363 $customer = new Customer( $booking->get_customer_id() );
@@ -1287,9 +1390,12 @@
1287 1390 'location' => $booking->get_location(),
1288 1391 'location_type' => $booking->get_location_type(),
1289 1392 'description' => $booking->get_description(),
1290 1393 'cancel_reason' => $booking->get_cancel_reason(),
1291 - 'security_token' => $booking->get_security_token(),
1394 + // Listing endpoints (get_items / get_booking_list) pass $expose_token = false —
1395 + // a viewer browsing many bookings at once has no legitimate need for every
1396 + // one's bearer token; single-booking reads (create/get/update) keep it.
1397 + 'security_token' => $expose_token ? $booking->get_security_token() : '',
1292 1398 'payment_method' => $booking->get_payment_method(),
1293 1399 'payment_status' => $booking->get_payment_status(),
1294 1400 'payment_details' => $payment_details,
1295 1401 'customer' => [
@@ -1361,8 +1467,23 @@
1361 1467
1362 1468 $booking->release_slot();
1363 1469
1364 1470 $recurrences = $booking->get_recurrence();
1471 +
1472 + /**
1473 + * Fired before a booking is deleted, while its data can still be read.
1474 + *
1475 + * `timetics_after_booking_delete` runs after the post has already gone
1476 + * and only receives the recurrence data, so an integration that needs
1477 + * the booking, customer or meeting has to listen here instead.
1478 + *
1479 + * @param int $booking_id Booking ID.
1480 + * @param int $customer_id Customer ID.
1481 + * @param int $meeting_id Meeting (appointment) ID.
1482 + * @param array $data Request data.
1483 + */
1484 + do_action( 'timetics_before_booking_delete', $booking->get_id(), $booking->get_customer_id(), $meeting->get_id(), [] );
1485 +
1365 1486 $booking->delete_event();
1366 1487 $booking->delete();
1367 1488
1368 1489 $is_email_to_customer = timetics_get_option( 'booking_canceled_customer');
@@ -1429,8 +1550,26 @@
1429 1550 return (bool) apply_filters( 'timetics_is_slot_available', true, $meeting, $booking_data );
1430 1551 }
1431 1552
1432 1553 /**
1554 + * Resolve what `timetics_booking_update_schedule` returned into an update payload.
1555 + *
1556 + * The filter passes the entry as its filtered value and the payload only as
1557 + * an extra argument, so with nothing hooked it hands back the entry object.
1558 + * Booking_Entry::update() then matches none of its keys and silently writes
1559 + * nothing, leaving group counters frozen. Keep the published signature and
1560 + * fall back to the payload whenever the result is not usable.
1561 + *
1562 + * @param mixed $filtered Whatever the filter returned.
1563 + * @param integer $booked Counter this call meant to store.
1564 + *
1565 + * @return array
1566 + */
1567 + private function normalize_schedule_update( $filtered, $booked ) {
1568 + return is_array( $filtered ) ? $filtered : [ 'booked' => $booked ];
1569 + }
1570 +
1571 + /**
1433 1572 * Validates a booking.
1434 1573 *
1435 1574 * @param int $appointment_id The ID of the appointment.
1436 1575 * @param array $data The data for the booking.
@@ -1547,10 +1686,16 @@
1547 1686 if (empty($booking_id) || ! wp_verify_nonce($nonce, 'wp_rest')) {
1548 1687 return false;
1549 1688 }
1550 1689
1551 - // Allow booking owner or admins/managers.
1552 - if ( (int) $booking->get_customer_id() === get_current_user_id() || current_user_can( 'manage_timetics' )) {
1690 + // manage_timetics is not admin-only — every staff account holds it — so it
1691 + // cannot stand in for an ownership check. Real admins, the booking's own
1692 + // customer, or staff this specific booking is actually visible to.
1693 + if (
1694 + ( get_current_user_id() > 0 && (int) $booking->get_customer_id() === get_current_user_id() )
1695 + || timetics_can_view_all_data()
1696 + || in_array( $booking_id, timetics_get_visible_booking_ids(), true )
1697 + ) {
1553 1698 return true;
1554 1699 }
1555 1700
1556 1701 return false;
@@ -1579,11 +1724,20 @@
1579 1724 return true;
1580 1725 }
1581 1726 }
1582 1727
1583 - if (wp_verify_nonce($nonce, 'wp_rest') && current_user_can( 'manage_timetics' ) ) {
1728 + if ( ! wp_verify_nonce( $nonce, 'wp_rest' ) ) {
1729 + return false;
1730 + }
1731 +
1732 + if (
1733 + ( get_current_user_id() > 0 && (int) $booking->get_customer_id() === get_current_user_id() )
1734 + || timetics_can_view_all_data()
1735 + || in_array( $booking_id, timetics_get_visible_booking_ids(), true )
1736 + ) {
1584 1737 return true;
1585 1738 }
1739 +
1586 1740 return false;
1587 1741 }
1588 1742
1589 1743 /**
@@ -1597,10 +1751,10 @@
1597 1751 *
1598 1752 * @return string|WP_Error Returns the validated email on success, WP_Error on failure.
1599 1753 */
1600 1754 private function validate_email_change_permission( $booking_id, $new_email ) {
1601 - // Admin users have full permission to change email addresses
1602 - if ( current_user_can( 'manage_timetics' ) ) {
1755 + // manage_timetics is not admin-only — every staff account holds it.
1756 + if ( timetics_can_view_all_data() ) {
1603 1757 return $new_email;
1604 1758 }
1605 1759
1606 1760 $existing_booking = new Booking( $booking_id );
@@ -1717,8 +1871,23 @@
1717 1871 409
1718 1872 );
1719 1873 }
1720 1874
1875 + // A previous decline released this booking's slot. Bind runs before the card
1876 + // is charged, so it is the last safe point to take the slot back — refusing
1877 + // here costs the customer nothing, refusing after payment would take their
1878 + // money for a time somebody else now holds.
1879 + if ( ! $booking->reserve_slot() ) {
1880 + return new WP_HTTP_Response(
1881 + [
1882 + 'success' => 0,
1883 + 'status_code' => 409,
1884 + 'message' => esc_html__( 'This time slot is no longer available. Please pick another time.', 'timetics' ),
1885 + ],
1886 + 409
1887 + );
1888 + }
1889 +
1721 1890 $result = $stripe->update_payment_intent(
1722 1891 $intent_id,
1723 1892 [
1724 1893 'booking_id' => $booking_id,
@@ -1736,8 +1905,12 @@
1736 1905 502
1737 1906 );
1738 1907 }
1739 1908
1909 + // Record the intent id now (not just at make_payment finalize) so the
1910 + // unpaid-booking cleanup sweep can check Stripe before cancelling.
1911 + $booking->set_stripe_payment_intent_id( $intent_id );
1912 +
1740 1913 return new WP_HTTP_Response(
1741 1914 [
1742 1915 'success' => 1,
1743 1916 'status_code' => 200,
@@ -1771,9 +1944,12 @@
1771 1944 // constant-time comparison
1772 1945 if ( ! hash_equals( $stored_token, $appointment_token ) ) {
1773 1946 return false;
1774 1947 }
1775 - if ( 'pending' !== (string) $booking->get_status() ) {
1948 + // A declined card leaves the booking 'failed' and the customer retries on that
1949 + // same booking, so 'failed' has to pass too. Anything further along
1950 + // ( approved / completed / cancelled ) is finished and must never be payable.
1951 + if ( ! in_array( (string) $booking->get_status(), [ 'pending', 'failed' ], true ) ) {
1776 1952 return false;
1777 1953 }
1778 1954
1779 1955 return true;