abstract-booking-ability.php
1 week ago
approve-booking.php
4 months ago
cancel-booking.php
4 months ago
change-booking-status.php
1 week ago
create-booking.php
4 months ago
delete-booking.php
1 week ago
get-booking-stats.php
1 week ago
get-booking-statuses.php
4 months ago
get-booking.php
1 week ago
get-bookings-for-date.php
4 months ago
get-bookings-per-day.php
1 week ago
get-upcoming-bookings.php
4 months ago
list-bookings.php
4 months ago
reschedule-booking.php
1 week ago
update-booking.php
1 week ago
reschedule-booking.php
78 lines
| 1 | <?php |
| 2 | if ( ! defined( 'ABSPATH' ) ) { |
| 3 | exit; } |
| 4 | |
| 5 | class LatePointAbilityRescheduleBooking extends LatePointAbstractBookingAbility { |
| 6 | |
| 7 | protected function configure(): void { |
| 8 | $this->id = 'latepoint/reschedule-booking'; |
| 9 | $this->label = __( 'Reschedule booking', 'latepoint' ); |
| 10 | $this->description = __( 'Moves an existing booking to a new date and/or time. May trigger rescheduling notifications to the customer.', 'latepoint' ); |
| 11 | $this->permission = 'booking__edit'; |
| 12 | $this->read_only = false; |
| 13 | $this->destructive = false; |
| 14 | $this->idempotent = true; |
| 15 | } |
| 16 | |
| 17 | public function get_input_schema(): array { |
| 18 | return [ |
| 19 | 'type' => 'object', |
| 20 | 'properties' => [ |
| 21 | 'id' => [ |
| 22 | 'type' => 'integer', |
| 23 | 'description' => __( 'Booking ID.', 'latepoint' ), |
| 24 | ], |
| 25 | 'start_date' => [ |
| 26 | 'type' => 'string', |
| 27 | 'format' => 'date', |
| 28 | 'description' => __( 'New date (Y-m-d).', 'latepoint' ), |
| 29 | ], |
| 30 | 'start_time' => [ |
| 31 | 'type' => 'integer', |
| 32 | 'description' => __( 'New start time (minutes from midnight).', 'latepoint' ), |
| 33 | ], |
| 34 | 'end_time' => [ |
| 35 | 'type' => 'integer', |
| 36 | 'description' => __( 'New end time (minutes from midnight).', 'latepoint' ), |
| 37 | ], |
| 38 | ], |
| 39 | 'required' => [ 'id' ], |
| 40 | ]; |
| 41 | } |
| 42 | |
| 43 | public function get_output_schema(): array { |
| 44 | return $this->booking_output_schema(); |
| 45 | } |
| 46 | |
| 47 | public function execute( array $args ) { |
| 48 | $booking = new OsBookingModel( (int) $args['id'] ); |
| 49 | if ( $booking->is_new_record() ) { |
| 50 | return new WP_Error( 'not_found', __( 'Booking not found.', 'latepoint' ), [ 'status' => 404 ] ); |
| 51 | } |
| 52 | $auth = $this->authorize_record( $booking, 'edit' ); |
| 53 | if ( is_wp_error( $auth ) ) { |
| 54 | return $auth; |
| 55 | } |
| 56 | |
| 57 | if ( isset( $args['start_date'] ) ) { |
| 58 | $booking->start_date = sanitize_text_field( $args['start_date'] ); |
| 59 | } |
| 60 | if ( isset( $args['start_time'] ) ) { |
| 61 | $booking->start_time = (int) $args['start_time']; |
| 62 | } |
| 63 | if ( isset( $args['end_time'] ) ) { |
| 64 | $booking->end_time = (int) $args['end_time']; |
| 65 | } |
| 66 | |
| 67 | if ( ! $booking->save() ) { |
| 68 | return new WP_Error( |
| 69 | 'save_failed', |
| 70 | __( 'Failed to reschedule booking.', 'latepoint' ), |
| 71 | WP_DEBUG ? [ 'errors' => $booking->get_error_messages() ] : [ 'status' => 422 ] |
| 72 | ); |
| 73 | } |
| 74 | |
| 75 | return $this->serialize_booking( new OsBookingModel( $booking->id ) ); |
| 76 | } |
| 77 | } |
| 78 |