route( '/admin/settings', array( array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'get' ), 'permission' => $settings, ), array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( $this, 'save' ), 'permission' => $settings, ), ) ); $this->route( '/admin/emails', array( array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'emails' ), 'permission' => $settings, ), array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( $this, 'save_emails' ), 'permission' => $settings, ), ) ); foreach ( array( 'preview', 'test' ) as $action ) { $this->route( '/admin/emails/' . $action, array( array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( $this, $action . '_email' ), 'permission' => $settings, ), ) ); } $this->route( '/admin/coupons', array( array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'coupons' ), 'permission' => $manage, ), array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( $this, 'save_coupon' ), 'permission' => $manage, ), ) ); $this->route( '/admin/coupons/(?P\d+)', array( array( 'methods' => WP_REST_Server::EDITABLE, 'callback' => array( $this, 'save_coupon' ), 'permission' => $manage, ), array( 'methods' => WP_REST_Server::DELETABLE, 'callback' => array( $this, 'delete_coupon' ), 'permission' => $manage, ), ) ); $this->route( '/admin/calendars', array( array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'calendars' ), 'permission' => array( $this, 'can_enter' ), ), array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( $this, 'add_calendar' ), 'permission' => array( $this, 'can_enter' ), ), ) ); $this->route( '/admin/calendars/(?P\d+)', array( array( 'methods' => WP_REST_Server::DELETABLE, 'callback' => array( $this, 'delete_calendar' ), 'permission' => array( $this, 'can_enter' ), ), ) ); $this->route( '/admin/calendars/(?P\d+)/sync', array( array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( $this, 'sync_calendar' ), 'permission' => array( $this, 'can_enter' ), ), ) ); $this->route( '/admin/health', array( array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'health' ), 'permission' => $manage, ), ) ); register_rest_route( self::NAMESPACE, '/ical/(?Pstaff|bookable|all)-(?P\d+)-(?P[a-f0-9]{24})\.ics', array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'feed' ), // The secret token in the address is the permission. 'permission_callback' => '__return_true', ) ); } /** * Every setting, grouped as the settings screen shows them. * * @return array */ public function get(): array { $methods = array(); foreach ( Services::methods()->all() as $method ) { $methods[] = array( 'id' => $method->id(), 'label' => $method instanceof OnlinePaymentMethod && ! str_contains( $method->label(), $method->provider() ) ? $method->label() . ' (' . $method->provider() . ')' : $method->label(), 'online' => $method instanceof OnlinePaymentMethod, 'configured' => ! $method instanceof OnlinePaymentMethod || $method->is_configured(), 'hint' => method_exists( $method, 'setup_hint' ) ? (string) $method->setup_hint() : '', 'enabled' => in_array( $method->id(), Settings::payment_methods(), true ), ); } $cancellation = Settings::cancellation(); return array( 'general' => array( 'business' => Settings::business(), 'currency' => Settings::currency(), 'country' => Settings::country(), 'timezone' => Settings::timezone()->getName(), 'notify_email' => Settings::notify_email(), 'booking_page' => Settings::booking_page(), 'portal_page' => (int) get_option( Settings::PORTAL_PAGE, 0 ), 'approval' => Settings::approval(), ), 'scheduling' => Settings::scheduling(), 'fields' => Settings::fields(), 'policies' => array( 'cancellation' => $cancellation?->to_array(), 'changes' => Settings::changes()->to_array(), 'text' => Format::policy( $cancellation, Settings::changes() ), ), 'payments' => array( 'methods' => $methods, 'deposit' => Settings::deposit()->to_array(), 'bank_details' => Settings::bank_details(), ), 'taxes' => array( 'rules' => array_map( static fn( TaxRule $t ): array => $t->to_array(), Settings::taxes() ), 'prices_include_tax' => Settings::prices_include_tax(), ), 'appearance' => Settings::appearance(), 'security' => array_merge( Settings::security(), array( 'secret_key' => '' !== Settings::security()['secret_key'] ? '••••' : '' ) ), 'privacy' => Settings::privacy() + array( 'remove_data' => (bool) get_option( Settings::REMOVE_DATA, false ) ), 'edition' => Edition::for_script(), ); } /** * Save the groups sent. * * @param WP_REST_Request $request Request. * @return array * @throws InvalidArgumentException When something is malformed; nothing is saved then. */ public function save( WP_REST_Request $request ): array { $body = self::body( $request ); $writes = array(); if ( isset( $body['general'] ) ) { $g = (array) $body['general']; if ( isset( $g['business'] ) ) { $b = (array) $g['business']; $writes[ Settings::BUSINESS ] = array( 'name' => mb_substr( sanitize_text_field( (string) ( $b['name'] ?? '' ) ), 0, 191 ), 'template' => sanitize_key( (string) ( $b['template'] ?? Settings::business()['template'] ) ), 'phone' => mb_substr( sanitize_text_field( (string) ( $b['phone'] ?? '' ) ), 0, 40 ), 'address' => mb_substr( sanitize_textarea_field( (string) ( $b['address'] ?? '' ) ), 0, 500 ), 'logo_id' => max( 0, (int) ( $b['logo_id'] ?? 0 ) ), ); } if ( isset( $g['currency'] ) ) { $currency = strtoupper( (string) $g['currency'] ); if ( 1 !== preg_match( '/^[A-Z]{3}$/', $currency ) ) { throw new InvalidArgumentException( 'currency must be a three-letter code such as USD.' ); } $writes[ Settings::CURRENCY ] = $currency; } if ( isset( $g['country'] ) ) { $writes[ Settings::COUNTRY ] = 1 === preg_match( '/^[A-Z]{2}$/', strtoupper( (string) $g['country'] ) ) ? strtoupper( (string) $g['country'] ) : ''; } if ( isset( $g['notify_email'] ) ) { if ( '' !== $g['notify_email'] && ! is_email( (string) $g['notify_email'] ) ) { throw new InvalidArgumentException( __( 'Enter a valid email address for notifications.', 'schedulereed' ) ); } $writes[ Settings::NOTIFY_EMAIL ] = sanitize_email( (string) $g['notify_email'] ); } foreach ( array( 'booking_page' => Settings::BOOKING_PAGE, 'portal_page' => Settings::PORTAL_PAGE, ) as $key => $option ) { if ( isset( $g[ $key ] ) ) { $writes[ $option ] = max( 0, (int) $g[ $key ] ); } } if ( isset( $g['approval'] ) ) { $writes[ Settings::APPROVAL ] = 'manual' === $g['approval'] ? 'manual' : 'auto'; } } if ( isset( $body['scheduling'] ) ) { $s = array_intersect_key( (array) $body['scheduling'], Settings::SCHEDULING_DEFAULTS ); foreach ( array( 'lead_time', 'horizon', 'hold_minutes', 'pay_minutes' ) as $key ) { if ( isset( $s[ $key ] ) ) { $s[ $key ] = max( 0, (int) $s[ $key ] ); } } if ( isset( $s['clock'] ) && ! in_array( $s['clock'], array( 'auto', '12', '24' ), true ) ) { throw new InvalidArgumentException( 'clock must be auto, 12 or 24.' ); } $writes[ Settings::SCHEDULING ] = array_merge( (array) get_option( Settings::SCHEDULING, array() ), $s ); } if ( isset( $body['fields'] ) ) { $f = array_intersect_key( (array) $body['fields'], Settings::FIELD_DEFAULTS ); foreach ( array( 'phone', 'last_name', 'notes', 'address' ) as $key ) { if ( isset( $f[ $key ] ) && ! in_array( $f[ $key ], array( 'required', 'optional', 'hidden' ), true ) ) { throw new InvalidArgumentException( "fields.{$key} must be required, optional or hidden." ); } } if ( isset( $f['terms'] ) ) { $f['terms'] = mb_substr( sanitize_text_field( (string) $f['terms'] ), 0, 300 ); } if ( isset( $f['terms_url'] ) ) { $f['terms_url'] = esc_url_raw( (string) $f['terms_url'] ); } if ( isset( $f['marketing'] ) ) { $f['marketing'] = (bool) $f['marketing']; } $writes[ Settings::FIELDS ] = array_merge( (array) get_option( Settings::FIELDS, array() ), $f ); } if ( isset( $body['policies'] ) ) { $p = (array) $body['policies']; if ( array_key_exists( 'cancellation', $p ) ) { $policy = CancellationPolicy::from_array( $p['cancellation'] ); $writes[ Settings::CANCELLATION ] = null === $policy ? '' : $policy->to_array(); } if ( isset( $p['changes'] ) ) { $writes[ Settings::CHANGES ] = ChangeRules::from_array( $p['changes'] )->to_array(); } } if ( isset( $body['payments'] ) ) { $p = (array) $body['payments']; $writes['schedulereed_payments_reviewed'] = true; if ( isset( $p['enabled'] ) ) { $known = array_keys( Services::methods()->all() ); $ids = array_values( array_intersect( array_map( 'strval', (array) $p['enabled'] ), $known ) ); if ( array() === $ids ) { throw new InvalidArgumentException( __( 'Offer at least one way to pay.', 'schedulereed' ) ); } $writes[ Settings::PAYMENT_METHODS ] = $ids; } if ( isset( $p['deposit'] ) ) { $writes[ Settings::DEPOSIT ] = DepositPolicy::from_array( (array) $p['deposit'] )->to_array(); } if ( isset( $p['bank_details'] ) ) { $writes[ Settings::BANK_DETAILS ] = mb_substr( sanitize_textarea_field( (string) $p['bank_details'] ), 0, 2000 ); } } if ( isset( $body['taxes'] ) ) { $t = (array) $body['taxes']; if ( isset( $t['rules'] ) ) { $writes[ Settings::TAXES ] = array_map( static fn( TaxRule $r ): array => $r->to_array(), TaxRule::list_from( (array) $t['rules'] ) ); } if ( isset( $t['prices_include_tax'] ) ) { $writes[ Settings::PRICES_INC_TAX ] = (bool) $t['prices_include_tax']; } } if ( isset( $body['appearance'] ) ) { $writes[ Settings::APPEARANCE ] = array_intersect_key( (array) $body['appearance'], array_flip( array( 'color', 'radius', 'style' ) ) ); } if ( isset( $body['security'] ) ) { $s = (array) $body['security']; $current = Settings::security(); $secret = (string) ( $s['secret_key'] ?? '••••' ); $writes[ Settings::SECURITY ] = array( 'captcha' => in_array( $s['captcha'] ?? '', array( 'turnstile', 'recaptcha' ), true ) ? $s['captcha'] : '', 'site_key' => sanitize_text_field( (string) ( $s['site_key'] ?? $current['site_key'] ) ), 'secret_key' => '••••' === $secret ? $current['secret_key'] : sanitize_text_field( $secret ), 'limit' => (int) ( $s['limit'] ?? $current['limit'] ), ); } if ( isset( $body['privacy'] ) ) { $p = (array) $body['privacy']; $writes[ Settings::PRIVACY ] = array( 'retention_months' => max( 0, min( 240, (int) ( $p['retention_months'] ?? 0 ) ) ), 'consent_text' => mb_substr( sanitize_text_field( (string) ( $p['consent_text'] ?? '' ) ), 0, 500 ), ); if ( isset( $p['remove_data'] ) ) { $writes[ Settings::REMOVE_DATA ] = (bool) $p['remove_data']; } } $secret = array( Settings::SECURITY ); foreach ( $writes as $option => $value ) { update_option( $option, $value, ! in_array( $option, $secret, true ) ); } Cache::bump(); /** * Fires after settings are saved. * * @param array $writes Option name => saved value. */ do_action( 'schedulereed_settings_saved', $writes ); return $this->get(); } /** * Every email with its wording. * * @return array> */ public function emails(): array { return EmailTemplates::all(); } /** * Save email wording. * * @param WP_REST_Request $request Request. * @return array> * @throws InvalidArgumentException When a message uses unknown placeholders. */ public function save_emails( WP_REST_Request $request ): array { $emails = (array) ( self::body( $request )['emails'] ?? array() ); $defaults = EmailTemplates::defaults(); foreach ( $emails as $type => $email ) { if ( ! isset( $defaults[ $type ] ) || ! is_array( $email ) ) { continue; } foreach ( array( 'subject', 'message' ) as $field ) { $unknown = Placeholders::unknown( (string) ( $email[ $field ] ?? '' ), array_merge( $defaults[ $type ]['placeholders'], array( 'amount' ) ) ); if ( array() !== $unknown ) { /* translators: %s: list of placeholders. */ throw new InvalidArgumentException( sprintf( __( 'Unknown placeholder: %s', 'schedulereed' ), '{' . implode( '}, {', $unknown ) . '}' ) ); } } } EmailTemplates::save( $emails ); return EmailTemplates::all(); } /** * An email filled in, for the preview pane. * * @param WP_REST_Request $request Request. * @return array * @throws BookingException When there is no booking to preview with. */ public function preview_email( WP_REST_Request $request ): array { $body = self::body( $request ); $booking = $this->sample_booking( (int) ( $body['booking_id'] ?? 0 ) ); $email = Services::notifier()->compose( (string) ( $body['type'] ?? '' ), $booking, array( 'amount' => Format::money( 5000, Settings::currency() ) ), (array) ( $body['template'] ?? array() ) ); if ( null === $email ) { throw new BookingException( 'schedulereed_unknown_email', __( 'Unknown email.', 'schedulereed' ) ); } return array( 'subject' => $email['subject'], 'body' => $email['body'], 'to' => $email['to'], ); } /** * Send a preview to the current user. * * @param WP_REST_Request $request Request. * @return array * @throws BookingException When it cannot be sent. */ public function test_email( WP_REST_Request $request ): array { $preview = $this->preview_email( $request ); $to = (string) wp_get_current_user()->user_email; if ( ! Services::notifier()->send_plain( 'test', $to, '[' . __( 'Test', 'schedulereed' ) . '] ' . $preview['subject'], $preview['body'] ) ) { throw new BookingException( 'schedulereed_email_failed', __( 'WordPress could not send the email. Your site may need an SMTP plugin to send email reliably.', 'schedulereed' ), 502 ); } update_option( 'schedulereed_test_email_sent', true, false ); return array( 'sent' => true, 'to' => $to, ); } /** * Coupons. * * @return array> */ public function coupons(): array { return Services::coupons()->all(); } /** * Add or change a coupon. * * @param WP_REST_Request $request Request. * @return array> * @throws InvalidArgumentException When the code is taken or malformed. */ public function save_coupon( WP_REST_Request $request ): array { $body = self::body( $request ); $id = (int) ( $request['id'] ?? 0 ); $coupon = Coupon::from_array( (string) ( $body['code'] ?? '' ), $body ); if ( Services::coupons()->code_taken( $coupon->code, $id ) ) { throw new InvalidArgumentException( __( 'Another coupon already uses that code.', 'schedulereed' ) ); } Services::coupons()->save( $coupon, max( 0, (int) ( $body['max_uses'] ?? 0 ) ), ! isset( $body['active'] ) || (bool) $body['active'], $id ); return Services::coupons()->all(); } /** * Remove a coupon. * * @param WP_REST_Request $request Request. * @return array> */ public function delete_coupon( WP_REST_Request $request ): array { Services::coupons()->delete( (int) $request['id'] ); return Services::coupons()->all(); } /** * Linked calendars and the feeds to subscribe to. * * @return array */ public function calendars(): array { $own = Capabilities::can_manage() ? 0 : Capabilities::own_staff_id(); $links = array_values( array_filter( Services::calendars()->links(), static fn( array $l ): bool => 0 === $own || ( 'staff' === $l['owner_type'] && $l['owner_id'] === $own ) ) ); $feeds = array(); foreach ( Services::staff()->all( true ) as $member ) { if ( 0 === $own || $member['id'] === $own ) { $feeds[] = array( 'owner_type' => 'staff', 'owner_id' => $member['id'], 'name' => $member['name'], 'url' => CalendarSync::export_url( 'staff', $member['id'] ), ); } } if ( 0 === $own ) { $feeds[] = array( 'owner_type' => 'all', 'owner_id' => 0, 'name' => __( 'All bookings', 'schedulereed' ), 'url' => CalendarSync::export_url( 'all', 0 ), ); } return array( // Tokens stay on the server. 'links' => array_map( static fn( array $l ): array => array_diff_key( $l, array_flip( array( 'credentials', 'sync_token' ) ) ), $links ), 'feeds' => $feeds, /** * Calendar providers offered besides iCal, e.g. Pro's Google and Outlook. * * @param array $providers [{id, label, connect_url}]. */ 'providers' => (array) apply_filters( 'schedulereed_calendar_providers', array() ), 'interval' => \ScheduleReed\Jobs::intervals()[ CalendarSync::CRON_HOOK ], ); } /** * Import a calendar by its iCal address. * * @param WP_REST_Request $request Request. * @return array * @throws InvalidArgumentException When the address or owner is wrong. */ public function add_calendar( WP_REST_Request $request ): array { $body = self::body( $request ); $owner = (string) ( $body['owner_type'] ?? 'staff' ); $id = (int) ( $body['owner_id'] ?? 0 ); if ( ! in_array( $owner, CalendarSync::OWNERS, true ) || $id <= 0 ) { throw new InvalidArgumentException( 'owner_type must be staff, resource or bookable, with owner_id.' ); } if ( ! Capabilities::can_manage() && ( 'staff' !== $owner || Capabilities::own_staff_id() !== $id ) ) { throw new InvalidArgumentException( __( 'You can only link calendars to your own profile.', 'schedulereed' ) ); } $url = CalendarSync::normalize_url( (string) ( $body['url'] ?? '' ) ); if ( ! wp_http_validate_url( $url ) ) { throw new InvalidArgumentException( __( 'Enter the calendar\'s iCal address. It usually ends in .ics.', 'schedulereed' ) ); } $link_id = Services::calendars()->create( array( 'owner_type' => $owner, 'owner_id' => $id, 'provider' => 'ical', 'name' => mb_substr( sanitize_text_field( (string) ( $body['name'] ?? __( 'Calendar', 'schedulereed' ) ) ), 0, 191 ), 'url' => esc_url_raw( $url ), ) ); return array( 'sync' => Services::calendar_sync()->sync( $link_id ) ) + $this->calendars(); } /** * Stop importing a calendar. * * @param WP_REST_Request $request Request. * @return array */ public function delete_calendar( WP_REST_Request $request ): array { $this->own_link( (int) $request['id'] ); Services::calendars()->delete( (int) $request['id'] ); Cache::bump(); return $this->calendars(); } /** * Import a calendar now. * * @param WP_REST_Request $request Request. * @return array */ public function sync_calendar( WP_REST_Request $request ): array { $link = $this->own_link( (int) $request['id'] ); if ( 'ical' !== $link['provider'] ) { /** * Sync a calendar from another provider now, e.g. Pro's Google sync. * * @param array|null $result Result, or null when nobody handled it. * @param array $link The link. */ return (array) apply_filters( 'schedulereed_sync_calendar', null, $link ); } return Services::calendar_sync()->sync( $link['id'] ); } /** * Things that stop bookings or emails from working, with how to fix them. * * @return array */ public function health(): array { $issues = array(); $add = static function ( string $id, string $level, string $message, string $fix = '' ) use ( &$issues ): void { $issues[] = array( 'id' => $id, 'level' => $level, 'message' => $message, 'fix' => $fix, ); }; $active = Services::bookables()->all( true ); if ( array() === $active ) { $add( 'no_services', 'error', __( 'There is nothing to book yet.', 'schedulereed' ), '#/services' ); } foreach ( $active as $row ) { $bookable = Services::catalog()->bookable( (int) $row['id'] ); if ( null === $bookable || $bookable->uses_sessions() || $bookable->by_day() ) { continue; } $from = time(); if ( array() === Services::availability()->slots( $bookable, $from, $from + 14 * DAY_IN_SECONDS )['slots'] ) { /* translators: %s: service name. */ $add( 'no_slots_' . $bookable->id, 'warning', sprintf( __( '"%s" has no free times in the next two weeks. Check its hours and staff.', 'schedulereed' ), $bookable->name ), '#/services/' . $bookable->id ); } } if ( Settings::booking_page() <= 0 || 'publish' !== get_post_status( Settings::booking_page() ) ) { $add( 'no_page', 'warning', __( 'The booking form is not on a published page yet.', 'schedulereed' ), '#/settings/general' ); } if ( '' === Settings::notify_email() ) { $add( 'no_email', 'warning', __( 'No address is set for new booking notifications.', 'schedulereed' ), '#/settings/general' ); } $fails = Services::messages()->failures_since( time() - WEEK_IN_SECONDS ); if ( $fails > 0 ) { /* translators: %d: number of emails. */ $add( 'mail_failing', 'error', sprintf( _n( '%d email failed to send this week. An SMTP plugin usually fixes this.', '%d emails failed to send this week. An SMTP plugin usually fixes this.', $fails, 'schedulereed' ), $fails ), 'https://wordpress.org/plugins/search/smtp/' ); } foreach ( Services::methods()->all() as $method ) { if ( $method instanceof OnlinePaymentMethod && in_array( $method->id(), Settings::payment_methods(), true ) && ! $method->is_configured() ) { /* translators: %s: provider name. */ $add( 'payment_' . $method->id(), 'error', sprintf( __( '%s is switched on but not connected, so customers cannot pay online.', 'schedulereed' ), $method->provider() ), '#/settings/payments' ); } } if ( in_array( 'stripe', Settings::payment_methods(), true ) && '' === Settings::stripe()['webhook_secret'] ) { $add( 'stripe_webhook', 'warning', __( 'Add the Stripe webhook so payments are recorded even if a customer closes the page after paying.', 'schedulereed' ), '#/settings/payments' ); } if ( ! is_ssl() && in_array( 'stripe', Settings::payment_methods(), true ) ) { $add( 'ssl', 'error', __( 'Your site does not use HTTPS, which online payments need.', 'schedulereed' ) ); } if ( ( defined( 'DISABLE_WP_CRON' ) && DISABLE_WP_CRON ) && ! function_exists( 'as_schedule_recurring_action' ) ) { $add( 'cron', 'warning', __( 'WP-Cron is turned off. Make sure a server cron job runs wp-cron.php every few minutes, or reminders will not be sent.', 'schedulereed' ) ); } foreach ( Services::calendars()->links() as $link ) { if ( '' !== $link['last_error'] ) { /* translators: 1: calendar name; 2: error. */ $add( 'calendar_' . $link['id'], 'warning', sprintf( __( 'Calendar "%1$s" could not be imported: %2$s', 'schedulereed' ), $link['name'], $link['last_error'] ), '#/calendars' ); } } /** * Add health checks. * * @param array $issues [{id, level (error|warning), message, fix}]. */ return (array) apply_filters( 'schedulereed_health', $issues ); } /** * Serve a calendar feed. * * @param WP_REST_Request $request Request. */ public function feed( WP_REST_Request $request ): void { $owner = (string) $request['owner']; $id = (int) $request['id']; if ( ! hash_equals( CalendarSync::token( $owner, $id ), (string) $request['token'] ) ) { status_header( 404 ); exit; } $ics = Services::calendar_sync()->export( $owner, $id, 'bookable' !== $owner ); nocache_headers(); header( 'Content-Type: text/calendar; charset=utf-8' ); header( 'Content-Disposition: inline; filename="bookings.ics"' ); echo $ics; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- An iCal file, not HTML. exit; } /** * A linked calendar the current user may change. * * @param int $id Link. * @return array * @throws BookingException When missing or not theirs. */ private function own_link( int $id ): array { $link = Services::calendars()->find( $id ); if ( null === $link || ( ! Capabilities::can_manage() && ( 'staff' !== $link['owner_type'] || Capabilities::own_staff_id() !== $link['owner_id'] ) ) ) { throw new BookingException( 'schedulereed_not_found', __( 'Not found.', 'schedulereed' ), 404 ); } return $link; } /** * A booking for email previews: the one asked for, the latest, or a made-up one. * * @param int $id Booking, or 0. * @return array */ private function sample_booking( int $id ): array { $booking = $id > 0 ? Services::bookings()->find( $id ) : null; if ( null === $booking ) { $latest = Services::bookings()->search( array( 'per_page' => 1 ) )['items'][0] ?? null; $booking = $latest; } if ( null !== $booking ) { return $booking; } $start = strtotime( 'tomorrow 10:00' ); return array( 'id' => 0, 'uid' => 'K7Q2M9XA', 'token' => 'sample', 'customer_id' => 0, 'status' => 'confirmed', 'source' => 'online', 'start' => $start, 'end' => $start + HOUR_IN_SECONDS, 'total' => 5000, 'paid' => 0, 'deposit' => 0, 'currency' => Settings::currency(), 'payment_method' => 'onsite', 'charges' => array(), 'policy' => array( 'cancellation' => Settings::cancellation()?->to_array() ), 'fields' => array(), 'timezone' => '', 'notes' => '', 'meeting_url' => '', 'coupon' => '', 'items' => array(), 'created_at' => time(), ); } }