abstract-booking-ability.php
6 days ago
approve-booking.php
3 months ago
cancel-booking.php
3 months ago
change-booking-status.php
6 days ago
create-booking.php
3 months ago
delete-booking.php
6 days ago
get-booking-stats.php
6 days ago
get-booking-statuses.php
3 months ago
get-booking.php
6 days ago
get-bookings-for-date.php
3 months ago
get-bookings-per-day.php
6 days ago
get-upcoming-bookings.php
3 months ago
list-bookings.php
3 months ago
reschedule-booking.php
6 days ago
update-booking.php
6 days ago
update-booking.php
85 lines
| 1 | <?php |
| 2 | if ( ! defined( 'ABSPATH' ) ) { |
| 3 | exit; } |
| 4 | |
| 5 | class LatePointAbilityUpdateBooking extends LatePointAbstractBookingAbility { |
| 6 | |
| 7 | protected function configure(): void { |
| 8 | $this->id = 'latepoint/update-booking'; |
| 9 | $this->label = __( 'Update booking', 'latepoint' ); |
| 10 | $this->description = __( 'Updates one or more fields on an existing booking. Only provided fields are changed. Use reschedule-booking to change date/time specifically.', '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 to update.', 'latepoint' ), |
| 24 | ], |
| 25 | 'start_date' => [ |
| 26 | 'type' => 'string', |
| 27 | 'format' => 'date', |
| 28 | ], |
| 29 | 'start_time' => [ 'type' => 'integer' ], |
| 30 | 'end_time' => [ 'type' => 'integer' ], |
| 31 | 'agent_id' => [ 'type' => 'integer' ], |
| 32 | 'location_id' => [ 'type' => 'integer' ], |
| 33 | 'notes' => [ 'type' => 'string' ], |
| 34 | 'status' => [ |
| 35 | 'type' => 'string', |
| 36 | 'enum' => [ 'approved', 'pending', 'cancelled', 'no_show', 'completed' ], |
| 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 | $allowed = [ 'start_date', 'start_time', 'end_time', 'agent_id', 'location_id', 'status' ]; |
| 58 | foreach ( $allowed as $field ) { |
| 59 | if ( isset( $args[ $field ] ) ) { |
| 60 | $booking->$field = in_array( $field, [ 'agent_id', 'location_id', 'start_time', 'end_time' ], true ) |
| 61 | ? (int) $args[ $field ] |
| 62 | : sanitize_text_field( $args[ $field ] ); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | if ( ! $booking->save() ) { |
| 67 | return new WP_Error( |
| 68 | 'save_failed', |
| 69 | __( 'Failed to update booking.', 'latepoint' ), |
| 70 | WP_DEBUG ? [ 'errors' => $booking->get_error_messages() ] : [ 'status' => 422 ] |
| 71 | ); |
| 72 | } |
| 73 | |
| 74 | if ( isset( $args['notes'] ) ) { |
| 75 | $order = $booking->get_order(); |
| 76 | if ( $order && ! $order->is_new_record() ) { |
| 77 | $order->customer_comment = sanitize_textarea_field( $args['notes'] ); |
| 78 | $order->save(); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | return $this->serialize_booking( new OsBookingModel( $booking->id ) ); |
| 83 | } |
| 84 | } |
| 85 |