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
list-bookings.php
61 lines
| 1 | <?php |
| 2 | if ( ! defined( 'ABSPATH' ) ) { |
| 3 | exit; } |
| 4 | |
| 5 | class LatePointAbilityListBookings extends LatePointAbstractBookingAbility { |
| 6 | |
| 7 | protected function configure(): void { |
| 8 | $this->id = 'latepoint/list-bookings'; |
| 9 | $this->label = __( 'List bookings', 'latepoint' ); |
| 10 | $this->description = __( 'Returns a paginated, filtered list of bookings.', 'latepoint' ); |
| 11 | $this->permission = 'booking__view'; |
| 12 | $this->read_only = true; |
| 13 | } |
| 14 | |
| 15 | public function get_input_schema(): array { |
| 16 | return [ |
| 17 | 'type' => 'object', |
| 18 | 'properties' => array_merge( $this->booking_filters_schema(), self::pagination() ), |
| 19 | ]; |
| 20 | } |
| 21 | |
| 22 | public function get_output_schema(): array { |
| 23 | return [ |
| 24 | 'type' => 'object', |
| 25 | 'properties' => [ |
| 26 | 'bookings' => [ |
| 27 | 'type' => 'array', |
| 28 | 'items' => $this->booking_output_schema(), |
| 29 | ], |
| 30 | 'total' => [ 'type' => 'integer' ], |
| 31 | 'page' => [ 'type' => 'integer' ], |
| 32 | 'per_page' => [ 'type' => 'integer' ], |
| 33 | ], |
| 34 | ]; |
| 35 | } |
| 36 | |
| 37 | public function execute( array $args ) { |
| 38 | $page = max( 1, (int) ( $args['page'] ?? 1 ) ); |
| 39 | $per_page = min( 100, max( 1, (int) ( $args['per_page'] ?? 20 ) ) ); |
| 40 | $offset = ( $page - 1 ) * $per_page; |
| 41 | |
| 42 | $query = new OsBookingModel(); |
| 43 | $query = $this->apply_filters( $query, $args ); |
| 44 | $total = ( clone $query )->count(); |
| 45 | |
| 46 | $bookings = $query |
| 47 | ->order_by( 'start_date ASC, start_time ASC' ) |
| 48 | ->set_limit( $per_page ) |
| 49 | ->set_offset( $offset ) |
| 50 | ->get_results_as_models(); |
| 51 | $bookings = is_array( $bookings ) ? $bookings : ( $bookings ? [ $bookings ] : [] ); |
| 52 | |
| 53 | return [ |
| 54 | 'bookings' => array_map( [ $this, 'serialize_booking' ], $bookings ), |
| 55 | 'total' => (int) $total, |
| 56 | 'page' => $page, |
| 57 | 'per_page' => $per_page, |
| 58 | ]; |
| 59 | } |
| 60 | } |
| 61 |