*/ private array $bookables = array(); public function bookable( int $id ): ?Bookable { if ( ! array_key_exists( $id, $this->bookables ) ) { $this->bookables[ $id ] = Services::bookables()->bookable( $id ); } return $this->bookables[ $id ]; } /** * Forget loaded bookables, e.g. after one is edited. */ public function flush(): void { $this->bookables = array(); } /** * Active staff who offer a bookable, optionally at one location. Staff * linked to no location work at all of them. * * @param int $bookable_id Bookable. * @param int $location_id Location or 0. * @return array> Keyed by staff ID, with `price` and `duration` overrides. */ public function staff_for( int $bookable_id, int $location_id = 0 ): array { $links = Services::links()->targets( 'bookable', $bookable_id, 'staff' ); if ( array() === $links ) { return array(); } $staff = array_filter( Services::staff()->many( array_keys( $links ) ), static fn( array $s ): bool => 'active' === $s['status'] ); if ( $location_id > 0 && array() !== $staff ) { $where = Services::links()->map( 'staff', array_keys( $staff ), 'location' ); $staff = array_filter( $staff, static fn( array $s ): bool => array() === ( $where[ $s['id'] ] ?? array() ) || in_array( $location_id, $where[ $s['id'] ], true ) ); } uasort( $staff, static fn( array $a, array $b ): int => array( $a['sort'], $a['name'] ) <=> array( $b['sort'], $b['name'] ) ); foreach ( $staff as $id => $member ) { $staff[ $id ]['price_override'] = $links[ $id ]['price']; $staff[ $id ]['duration_override'] = $links[ $id ]['duration']; } return $staff; } /** * Active locations a bookable is offered at; empty when it has none. * * @param int $bookable_id Bookable. * @return array> Keyed by ID. */ public function locations_for( int $bookable_id ): array { $ids = array_keys( Services::links()->targets( 'bookable', $bookable_id, 'location' ) ); return array_filter( Services::locations()->many( $ids ), static fn( array $l ): bool => 'active' === $l['status'] ); } /** * Active resources by ID. * * @param int[] $ids IDs. * @return array> */ public function resources( array $ids ): array { return array_filter( Services::resources()->many( $ids ), static fn( array $r ): bool => 'active' === $r['status'] ); } /** * What the booking widget needs to start: bookables with their choices, * without anything private (emails, phone numbers, user links). * * @param int[] $only Bookable IDs to include; empty for all. * @return array */ public function public_catalog( array $only = array() ): array { $rows = Services::bookables()->all( true ); $bookables = array(); $locations = array(); $staff = array(); foreach ( $rows as $row ) { if ( array() !== $only && ! in_array( (int) $row['id'], $only, true ) ) { continue; } $bookable = Bookable::from_row( $row ); /** * Whether to show a bookable to this visitor, e.g. members-only classes. * * @param bool $show Show it. * @param Bookable $bookable Bookable. */ if ( ! apply_filters( 'schedulereed_show_bookable', ! $bookable->settings['members_only'], $bookable ) ) { continue; } $staff_for = $this->staff_for( $bookable->id ); $locations_of = $this->locations_for( $bookable->id ); foreach ( $locations_of as $location ) { $locations[ $location['id'] ] = self::public_location( $location ); } $staff_list = array(); foreach ( $staff_for as $member ) { $staff[ $member['id'] ] = self::public_staff( $member ); $staff_list[] = array( 'id' => (int) $member['id'], 'price' => $member['price_override'], 'duration' => $member['duration_override'], ); } $bookables[] = self::public_bookable( $row, $bookable, $staff_list, array_keys( $locations_of ) ); } return array( 'bookables' => $bookables, 'locations' => array_values( $locations ), 'staff' => array_values( $staff ), ); } /** * A bookable for the widget. * * @param array $row Row. * @param Bookable $bookable Domain object. * @param array> $staff Staff choices. * @param int[] $locations Location IDs. * @return array */ public static function public_bookable( array $row, Bookable $bookable, array $staff, array $locations ): array { $s = $bookable->settings; return array( 'id' => $bookable->id, 'type' => $bookable->type, 'name' => Multilingual::text( $bookable->name, 'service_' . $bookable->id . '_name' ), 'description' => wp_kses_post( Multilingual::text( (string) ( $row['description'] ?? '' ), 'service_' . $bookable->id . '_description' ) ), 'image' => ! empty( $row['image_id'] ) ? (string) wp_get_attachment_image_url( (int) $row['image_id'], 'medium' ) : '', 'color' => (string) ( $row['color'] ?? '' ), 'duration' => $bookable->duration, 'duration_mode' => $bookable->duration_mode, 'min_units' => (int) $s['min_units'], 'max_units' => (int) $s['max_units'], 'unit_step' => (int) $s['unit_step'], 'capacity' => $bookable->capacity, 'max_people' => (int) $s['max_people'], 'price' => $bookable->price, 'price_basis' => $bookable->price_basis, 'extras' => array_values( $s['extras'] ), 'person_types' => apply_filters( 'schedulereed_person_types', array(), $bookable ), 'tickets' => Bookable::EVENT === $bookable->type ? array_values( $s['tickets'] ) : array(), 'staff_mode' => self::staff_mode( $bookable ), 'staff' => $staff, 'locations' => $locations, 'check_in' => (string) $s['check_in'], 'check_out' => (string) $s['check_out'], 'online' => '' !== $s['meeting'], 'recurring' => (bool) apply_filters( 'schedulereed_recurring_enabled', false, $bookable ), 'waitlist' => (bool) apply_filters( 'schedulereed_waitlist_enabled', false, $bookable ), ); } /** * How staff are chosen, falling back to the free modes without Pro. * * @param Bookable $bookable Bookable. */ public static function staff_mode( Bookable $bookable ): string { $mode = (string) $bookable->settings['staff_mode']; /** * Staff assignment modes this site supports; Pro adds round_robin and least_busy. * * @param string[] $modes Modes. */ $modes = (array) apply_filters( 'schedulereed_staff_modes', array( Bookable::STAFF_CUSTOMER, Bookable::STAFF_ANY ) ); return in_array( $mode, $modes, true ) ? $mode : ( Bookable::STAFF_CUSTOMER === $mode ? $mode : Bookable::STAFF_ANY ); } /** * A staff member for the widget. * * @param array $member Row. * @return array */ public static function public_staff( array $member ): array { return array( 'id' => (int) $member['id'], 'name' => (string) $member['name'], 'bio' => wp_kses_post( (string) $member['bio'] ), 'photo' => $member['photo_id'] ? (string) wp_get_attachment_image_url( (int) $member['photo_id'], 'thumbnail' ) : '', 'color' => (string) $member['color'], ); } /** * A location for the widget. * * @param array $location Row. * @return array */ public static function public_location( array $location ): array { return array( 'id' => (int) $location['id'], 'name' => (string) $location['name'], 'address' => (string) $location['address'], 'timezone' => (string) $location['timezone'], 'online' => (bool) $location['online'], ); } }