route( '/catalog', array( array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'catalog' ), 'permission' => $public, 'args' => array( 'ids' => array( 'type' => 'array', 'items' => array( 'type' => 'integer' ), ), ), ), ) ); $this->route( '/config', array( array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'config' ), 'permission' => $public, ), ) ); $choice = array( 'bookable_id' => array( 'type' => 'integer', 'required' => true, ), 'staff_id' => array( 'type' => 'integer' ), 'location_id' => array( 'type' => 'integer' ), 'quantity' => array( 'type' => 'integer' ), 'units' => array( 'type' => 'integer' ), 'extras' => array( 'type' => 'object' ), 'timezone' => array( 'type' => 'string' ), 'except' => array( 'type' => 'string' ), ); $this->route( '/availability/days', array( array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'days' ), 'permission' => $public, 'args' => $choice + array( 'from' => array( 'type' => 'string', 'required' => true, ), 'to' => array( 'type' => 'string', 'required' => true, ), ), ), ) ); $this->route( '/availability/slots', array( array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'slots' ), 'permission' => $public, 'args' => $choice + array( 'date' => array( 'type' => 'string', 'required' => true, ), 'days' => array( 'type' => 'integer', 'minimum' => 1, 'maximum' => 14, ), ), ), ) ); $this->route( '/quote', array( array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( $this, 'quote' ), 'permission' => $public, ), ) ); $this->route( '/holds', array( array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( $this, 'hold' ), 'permission' => $public, ), ) ); $this->route( '/holds/(?P[A-Za-z0-9]{20,64})', array( array( 'methods' => WP_REST_Server::DELETABLE, 'callback' => array( $this, 'release' ), 'permission' => $public, ), ) ); $this->route( '/bookings', array( array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( $this, 'book' ), 'permission' => $public, ), ) ); } /** * The public catalog. * * @param WP_REST_Request $request Request. * @return array */ public function catalog( WP_REST_Request $request ): array { return Services::catalog()->public_catalog( array_map( 'intval', (array) ( $request['ids'] ?? array() ) ) ); } /** * What the widget needs besides the catalog. * * @return array */ public function config(): array { $methods = array(); foreach ( Services::methods()->enabled() as $method ) { $methods[] = array( 'id' => $method->id(), 'label' => $method->label(), 'description' => $method->description(), 'online' => $method instanceof OnlinePaymentMethod, ); } $security = Settings::security(); $fields = Settings::fields(); $sched = Settings::scheduling(); /** * What the booking widget gets from /config, e.g. Pro's cart, gift * cards and other currencies. * * @param array $config Config. */ return (array) apply_filters( 'schedulereed_widget_config', array( 'currency' => Settings::currency(), 'timezone' => Settings::timezone()->getName(), 'locale' => str_replace( '_', '-', determine_locale() ), 'clock' => $sched['clock'], 'week_starts' => (int) $sched['week_starts'], 'hold' => (int) $sched['hold_minutes'], 'fields' => $fields, 'methods' => $methods, 'appearance' => Settings::appearance(), 'captcha' => array( 'type' => '' !== $security['secret_key'] ? $security['captcha'] : '', 'site_key' => $security['site_key'], ), 'portal_url' => Settings::portal_url(), 'business' => array_intersect_key( Settings::business(), array_flip( array( 'name', 'phone' ) ) ), ) ); } /** * Free slots per date. * * @param WP_REST_Request $request Request. * @return array * @throws BookingException When the bookable is unknown or the dates are wrong. */ public function days( WP_REST_Request $request ): array { self::throttle( 'days', 120 ); $bookable = $this->bookable( $request ); $from = (string) $request['from']; $to = (string) $request['to']; if ( ! DateOverride::is_date( $from ) || ! DateOverride::is_date( $to ) || $to < $from || ( strtotime( $to ) - strtotime( $from ) ) > 62 * DAY_IN_SECONDS ) { throw new BookingException( 'schedulereed_bad_dates', __( 'Choose up to two months of dates.', 'schedulereed' ) ); } $opt = $this->options( $request, $bookable ); if ( $bookable->uses_sessions() ) { $tz = Services::availability()->zone( $bookable, $opt ); $start = ( new DateTimeImmutable( $from, $tz ) )->getTimestamp(); $end = ( new DateTimeImmutable( $to, $tz ) )->modify( '+1 day' )->getTimestamp(); $days = array(); foreach ( Services::sessions()->available( $bookable, $start, $end, $opt ) as $session ) { $day = ( new DateTimeImmutable( '@' . $session['start'] ) )->setTimezone( $tz )->format( 'Y-m-d' ); $days[ $day ] = ( $days[ $day ] ?? 0 ) + $session['remaining']; } return array( 'days' => (object) $days, 'timezone' => $tz->getName(), ); } return array( 'days' => (object) Services::availability()->days( $bookable, $from, $to, $opt ), 'timezone' => Services::availability()->zone( $bookable, $opt )->getName(), ); } /** * Free times on a date (or a few dates), or the sessions on it. * * @param WP_REST_Request $request Request. * @return array * @throws BookingException When the bookable is unknown or the date is wrong. */ public function slots( WP_REST_Request $request ): array { self::throttle( 'slots', 120 ); $bookable = $this->bookable( $request ); $date = (string) $request['date']; if ( ! DateOverride::is_date( $date ) ) { throw new BookingException( 'schedulereed_bad_dates', __( 'Choose a date.', 'schedulereed' ) ); } $opt = $this->options( $request, $bookable ); $tz = Services::availability()->zone( $bookable, $opt ); $days = max( 1, min( 14, (int) ( $request['days'] ?? 1 ) ) ); $start = ( new DateTimeImmutable( $date, $tz ) )->getTimestamp(); $end = ( new DateTimeImmutable( $date, $tz ) )->modify( "+{$days} days" )->getTimestamp(); if ( $bookable->uses_sessions() ) { $sessions = array_map( static fn( array $s ): array => array( 'id' => $s['id'], 'start' => $s['start'], 'end' => $s['end'], 'staff_id' => $s['staff_id'], 'location_id' => $s['location_id'], 'capacity' => $s['capacity'], 'remaining' => $s['remaining'], 'tickets' => $s['tickets'], 'title' => (string) ( $s['title'] ?? '' ), ), Services::sessions()->available( $bookable, $start, $end - 1, $opt ) ); return array( 'sessions' => $sessions, 'timezone' => $tz->getName(), ); } $result = Services::availability()->slots( $bookable, $start, $end - 1, $opt ); /** * Change the free times sent to the booking form, e.g. Pro adds the * booked-out times people can join a waiting list for. * * @param array $response slots, truncated, timezone. * @param Bookable $bookable Bookable. * @param array $opt Availability options. * @param int $start First start. * @param int $end End of the range. */ return (array) apply_filters( 'schedulereed_slots_response', array( 'slots' => array_map( static fn( array $s ): array => array( 'start' => $s['start'], 'end' => $s['end'], 'staff' => array_values( $s['staff'] ), 'remaining' => $s['remaining'], ), $result['slots'] ), 'truncated' => $result['truncated'], 'timezone' => $tz->getName(), ), $bookable, $opt, $start, $end ); } /** * Price a choice. * * @param WP_REST_Request $request Request. * @return array */ public function quote( WP_REST_Request $request ): array { self::throttle( 'quote', 60 ); return Services::checkout()->quote( self::body( $request ) ); } /** * Keep a time for a few minutes. * * @param WP_REST_Request $request Request. * @return array */ public function hold( WP_REST_Request $request ): array { self::throttle( 'hold', 30 ); return Services::checkout()->hold( self::body( $request ) ); } /** * Let a hold go. * * @param WP_REST_Request $request Request. * @return array */ public function release( WP_REST_Request $request ): array { Services::checkout()->release( (string) $request['token'] ); return array( 'released' => true ); } /** * Book. * * @param WP_REST_Request $request Request. * @return array * @throws BookingException When the captcha fails or the booking cannot be made. */ public function book( WP_REST_Request $request ): array { self::throttle( 'book', Settings::security()['limit'], HOUR_IN_SECONDS ); $body = self::body( $request ); if ( ! Captcha::passes( (string) ( $body['captcha'] ?? '' ), (string) ( $body['website'] ?? '' ) ) ) { throw new BookingException( 'schedulereed_captcha', __( 'Please confirm you are not a robot and try again.', 'schedulereed' ), 400 ); } $booking = Services::checkout()->book( $body ); return array( 'booking' => Services::portal()->present( $booking ), 'next' => $booking['next'], ); } /** * The bookable asked for, if the visitor may book it. * * @param WP_REST_Request $request Request. * @throws BookingException When it is unknown or hidden. */ private function bookable( WP_REST_Request $request ): Bookable { $bookable = Services::catalog()->bookable( (int) $request['bookable_id'] ); if ( null === $bookable || ! $bookable->is_active() || ! apply_filters( 'schedulereed_show_bookable', ! $bookable->settings['members_only'], $bookable ) ) { throw new BookingException( 'schedulereed_unknown_bookable', __( 'That service is not available.', 'schedulereed' ), 404 ); } return $bookable; } /** * Availability options from the request. * * @param WP_REST_Request $request Request. * @param Bookable $bookable Bookable. * @return array */ private function options( WP_REST_Request $request, Bookable $bookable ): array { $extra = 0; $all = $bookable->extras(); foreach ( (array) ( $request['extras'] ?? array() ) as $id => $qty ) { if ( isset( $all[ $id ] ) ) { $extra += $all[ $id ]->duration * max( 0, min( $all[ $id ]->max, (int) $qty ) ); } } $opt = array( 'location_id' => max( 0, (int) ( $request['location_id'] ?? 0 ) ), 'staff_id' => max( 0, (int) ( $request['staff_id'] ?? 0 ) ), 'quantity' => max( 1, (int) ( $request['quantity'] ?? 1 ) ), 'units' => max( 1, (int) ( $request['units'] ?? 1 ) ), 'extra_minutes' => $extra, ); // The customer's own booking being moved from the portal. $except = (string) ( $request['except'] ?? '' ); if ( '' !== $except && str_contains( $except, ':' ) ) { [ $uid, $key ] = explode( ':', $except, 2 ); try { $opt['except_booking'] = Services::portal()->booking( $uid, $key )['id']; } catch ( BookingException ) { $opt['except_booking'] = 0; } } return $opt; } }