inactive(); if ( null !== $inactive ) { return $inactive; } $query = trim( (string) $query ); $category = trim( (string) $category ); $limit = max( 1, (int) $this->setting( 'recommendedLimit', 4 ) ); try { $meetings = array_slice( $this->matching( $this->all_meetings(), $query, $category ), 0, $limit ); if ( ! $meetings ) { return [ 'ok' => true, 'total' => 0, 'services' => [], 'note' => 'No services matched. Try fewer words, or an empty query and no category to list everything.', ]; } $short = []; $cards = []; foreach ( $meetings as $meeting ) { // price_text carries the currency: with bare numbers the model // guesses one from the timezone. $price_text = $this->gateway->price_text( (float) $meeting['price'] ); $tickets = $this->tickets( $meeting ); $short[] = array_filter( [ 'tickets' => $tickets ] ) + [ 'id' => $meeting['id'], 'name' => $meeting['name'], 'category' => $meeting['category'], 'duration' => $meeting['duration'], 'price_text' => $price_text, 'type' => $meeting['type'], 'capacity' => $meeting['capacity'], 'staff' => $meeting['staff'], 'locations' => array_map( static fn( array $location ): array => [ 'index' => $location['index'], 'label' => $location['label'], ], $meeting['locations'] ), ]; $cards[] = array_filter( [ 'badge' => self::badge( $meeting['locations'] ), 'details' => array_merge( self::details( $meeting ), self::ticket_line( $tickets ) ), ] ) + [ 'id' => $meeting['id'], 'name' => $meeting['name'], 'category' => $meeting['category'], 'image' => $meeting['image'], 'permalink' => '' !== $meeting['permalink'] ? $meeting['permalink'] : $this->gateway->meeting_url( $meeting['id'] ), 'price_from' => (float) $meeting['price'], 'price_from_text' => $tickets /* translators: %s: lowest ticket price with currency */ ? sprintf( __( 'From %s', 'aisentic' ), $price_text ) : $price_text, 'currency_symbol' => '', 'durations' => [ [ 'id' => 'default', 'label' => $meeting['duration'], 'minutes' => $meeting['minutes'], 'price' => (float) $meeting['price'], 'price_text' => $price_text, ], ], ]; } $result = [ 'ok' => true, 'total' => count( $short ), 'services' => $short, ]; // The prompt rule alone did not stop the model asking "is this the one?"; // a hint inside the result it just received works better. if ( 1 === count( $short ) ) { $result['next'] = 'Exactly one service matched. Use it without asking which service the visitor means; go on to the next step (usually timetics-available-slots).'; } $this->set_card( 'service_list', [ 'services' => $cards, 'button_label' => __( 'Check times', 'aisentic' ), ] ); return $result; } catch ( \Throwable $e ) { Log::error( 'Timetics service search failed: ' . $e->getMessage() ); return [ 'ok' => false, 'error' => 'The service list could not be loaded. Tell the visitor to open the booking page.', ]; } } /** * How the meeting happens, for the card's badge. Meetings have no photo, * so this is what a visitor scans first. * * @param array $locations Shaped locations [ index, type, label ]. * @return array|null [ icon, label ], or null without a location. */ private static function badge( array $locations ): ?array { if ( ! $locations ) { return null; } $icons = [ 'google-meet' => 'video', 'zoom' => 'video', 'in-person-meeting' => 'map-pin', 'phone-call' => 'phone', 'attendee-call' => 'phone', ]; $labels = array_column( $locations, 'label' ); if ( count( $labels ) > 2 ) { /* translators: %d: number of ways to meet, e.g. Zoom, in person, phone */ $label = sprintf( __( '%d ways to meet', 'aisentic' ), count( $labels ) ); } else { /* translators: 1: one way to meet, 2: another, e.g. "In person or Google Meet" */ $label = 2 === count( $labels ) ? sprintf( __( '%1$s or %2$s', 'aisentic' ), $labels[0], $labels[1] ) : $labels[0]; } return [ 'icon' => $icons[ $locations[0]['type'] ] ?? '', 'label' => $label, ]; } /** * Every ticket with its price, when there is more than one to choose from. * * @param array $meeting Shaped meeting. * @return array List of [ name, price_text ], empty for a single ticket. */ private function tickets( array $meeting ): array { if ( count( $meeting['tickets'] ?? [] ) < 2 ) { // A meeting cached before tickets were read has none. return []; } return array_map( fn( array $ticket ): array => [ 'name' => $ticket['name'], 'price_text' => $this->gateway->price_text( $ticket['price'] ), ], $meeting['tickets'] ); } /** * The card line naming each ticket, e.g. "default $10.00, Vip $50.00". * * @param array $tickets From tickets(). * @return array Zero or one [ icon, label ]. */ private static function ticket_line( array $tickets ): array { if ( ! $tickets ) { return []; } return [ [ 'icon' => 'ticket', 'label' => implode( ', ', array_map( static fn( array $ticket ): string => $ticket['name'] . ' ' . $ticket['price_text'], $tickets ) ), ], ]; } /** * Who the meeting is with, and its group size when it is not one to one. * * @param array $meeting Shaped meeting. * @return array List of [ icon, label ]. */ private static function details( array $meeting ): array { $lines = []; if ( $meeting['staff'] ) { $others = count( $meeting['staff'] ) - 1; $lines[] = [ 'icon' => 'user', 'label' => $others > 0 /* translators: 1: host name, 2: number of other hosts */ ? sprintf( _n( 'with %1$s and %2$d more', 'with %1$s and %2$d more', $others, 'aisentic' ), $meeting['staff'][0]['name'], $others ) /* translators: %s: host name */ : sprintf( __( 'with %s', 'aisentic' ), $meeting['staff'][0]['name'] ), ]; } if ( 'One-to-Many' === $meeting['type'] ) { $lines[] = [ 'icon' => 'users', /* translators: %d: most people per time */ 'label' => sprintf( __( 'Group, up to %d', 'aisentic' ), $meeting['capacity'] ), ]; } elseif ( 'seat' === $meeting['type'] ) { $lines[] = [ 'icon' => 'users', /* translators: %d: number of seats */ 'label' => sprintf( __( 'Seats, %d in total', 'aisentic' ), $meeting['capacity'] ), ]; } return $lines; } /** * Every bookable meeting, cached once per meeting data version and language. * * One cache row whatever the visitor types: a row per search wording would * fill wp_options on sites without an object cache. * * @return array Shaped meetings. */ private function all_meetings(): array { $key = 'aisentic_tt_svc_' . md5( (string) wp_json_encode( [ get_locale(), (int) get_option( self::CACHE_GENERATION, 0 ) ] ) ); $cached = get_transient( $key ); if ( is_array( $cached ) ) { return $cached; } $meetings = $this->gateway->meetings(); set_transient( $key, $meetings, self::CACHE_TTL ); return $meetings; } /** * Meetings whose name, description or category holds every word of the * query, and whose category holds the category text. * * @param array $meetings Shaped meetings. * @param string $query Search words ('' for all). * @param string $category Category name ('' for any). * @return array */ private function matching( array $meetings, string $query, string $category ): array { // "Salons and Spas" must find "Salons & Spas". $plain = static fn( string $text ): string => mb_strtolower( (string) preg_replace( '/\s*&\s*/u', ' and ', $text ) ); $words = array_filter( (array) preg_split( '/\s+/u', $plain( $query ) ) ); $category = $plain( $category ); return array_values( array_filter( $meetings, static function ( array $meeting ) use ( $words, $category, $plain ): bool { if ( '' !== $category && false === mb_strpos( $plain( $meeting['category'] ), $category ) ) { return false; } $text = $plain( $meeting['name'] . ' ' . $meeting['description'] . ' ' . $meeting['category'] ); foreach ( $words as $word ) { if ( false === mb_strpos( $text, $word ) ) { return false; } } return true; } ) ); } }