abstract-booking-ability.php
5 months ago
approve-booking.php
5 months ago
cancel-booking.php
5 months ago
change-booking-status.php
5 months ago
create-booking.php
5 months ago
delete-booking.php
5 months ago
get-booking-stats.php
5 months ago
get-booking-statuses.php
5 months ago
get-booking.php
5 months ago
get-bookings-for-date.php
5 months ago
get-bookings-per-day.php
5 months ago
get-upcoming-bookings.php
5 months ago
list-bookings.php
5 months ago
reschedule-booking.php
5 months ago
update-booking.php
5 months ago
get-upcoming-bookings.php
66 lines
| 1 | <?php |
| 2 | if ( ! defined( 'ABSPATH' ) ) { |
| 3 | exit; } |
| 4 | |
| 5 | class LatePointAbilityGetUpcomingBookings extends LatePointAbstractBookingAbility { |
| 6 | |
| 7 | protected function configure(): void { |
| 8 | $this->id = 'latepoint/get-upcoming-bookings'; |
| 9 | $this->label = __( 'Get upcoming bookings', 'latepoint' ); |
| 10 | $this->description = __( 'Returns upcoming bookings from today, with optional filters.', 'latepoint' ); |
| 11 | $this->permission = 'booking__view'; |
| 12 | $this->read_only = true; |
| 13 | } |
| 14 | |
| 15 | public function get_input_schema(): array { |
| 16 | $filters = $this->booking_filters_schema(); |
| 17 | return [ |
| 18 | 'type' => 'object', |
| 19 | 'properties' => array_merge( |
| 20 | array_intersect_key( $filters, array_flip( [ 'agent_id', 'service_id', 'location_id', 'customer_id', 'status' ] ) ), |
| 21 | self::pagination() |
| 22 | ), |
| 23 | ]; |
| 24 | } |
| 25 | |
| 26 | public function get_output_schema(): array { |
| 27 | return [ |
| 28 | 'type' => 'object', |
| 29 | 'properties' => [ |
| 30 | 'bookings' => [ |
| 31 | 'type' => 'array', |
| 32 | 'items' => $this->booking_output_schema(), |
| 33 | ], |
| 34 | 'total' => [ 'type' => 'integer' ], |
| 35 | 'page' => [ 'type' => 'integer' ], |
| 36 | 'per_page' => [ 'type' => 'integer' ], |
| 37 | ], |
| 38 | ]; |
| 39 | } |
| 40 | |
| 41 | public function execute( array $args ) { |
| 42 | $page = max( 1, (int) ( $args['page'] ?? 1 ) ); |
| 43 | $per_page = min( 100, max( 1, (int) ( $args['per_page'] ?? 20 ) ) ); |
| 44 | $offset = ( $page - 1 ) * $per_page; |
| 45 | |
| 46 | $query = new OsBookingModel(); |
| 47 | $query->where( [ 'start_date >=' => wp_date( 'Y-m-d' ) ] ); |
| 48 | $query = $this->apply_filters( $query, $args ); |
| 49 | $total = ( clone $query )->count(); |
| 50 | |
| 51 | $bookings = $query |
| 52 | ->order_by( 'start_date ASC, start_time ASC' ) |
| 53 | ->set_limit( $per_page ) |
| 54 | ->set_offset( $offset ) |
| 55 | ->get_results_as_models(); |
| 56 | $bookings = is_array( $bookings ) ? $bookings : ( $bookings ? [ $bookings ] : [] ); |
| 57 | |
| 58 | return [ |
| 59 | 'bookings' => array_map( [ $this, 'serialize_booking' ], $bookings ), |
| 60 | 'total' => (int) $total, |
| 61 | 'page' => $page, |
| 62 | 'per_page' => $per_page, |
| 63 | ]; |
| 64 | } |
| 65 | } |
| 66 |