inactive(); if ( null !== $inactive ) { return $inactive; } $query = trim( (string) $query ); $category = trim( (string) $category ); $limit = max( 1, (int) $this->setting( 'recommendedLimit', 4 ) ); try { $services = array_slice( $this->matching( $this->all_services(), $query, $category ), 0, $limit ); if ( ! $services ) { return [ 'ok' => true, 'total' => 0, 'services' => [], 'note' => 'No services matched. Try fewer words, or an empty query and no category to list everything.', ]; } $locations = $this->gateway->locations(); $symbol = $this->gateway->currency_symbol(); $short = []; $cards = []; foreach ( $services as $service ) { // price_text carries the shop's currency: with bare numbers the // model guessed one from the timezone ("BDT" for a USD shop). $durations = array_map( fn( array $d ): array => [ 'id' => $d['id'], 'label' => '' !== $d['name'] ? $d['name'] : $d['minutes'] . ' min', 'minutes' => $d['minutes'], 'price' => $d['price'], 'price_text' => $this->gateway->price_text( (float) $d['price'] ), ], $service['durations'] ); $short[] = [ 'id' => $service['id'], 'name' => $service['title'], 'category' => implode( ', ', $service['categories'] ), 'durations' => $durations, 'staff' => $service['staff'], 'extras' => array_map( fn( array $e ): array => [ 'id' => $e['id'], 'name' => $e['name'], 'price' => $e['price'], 'price_text' => $this->gateway->price_text( (float) $e['price'] ), 'max_qty' => $e['max_qty'], ], $this->gateway->extras( $service['id'] ) ), 'group_max' => $service['group_max'], 'group_min' => $service['group_min'] ?? 1, 'locations' => $this->locations_for( $service['staff'], $locations ), ]; $prices = array_column( $durations, 'price' ); $cards[] = [ 'id' => $service['id'], 'name' => $service['title'], 'category' => implode( ', ', $service['categories'] ), 'image' => $service['image'], 'permalink' => '' !== $service['link'] ? $service['link'] : $this->gateway->service_url( $service['id'] ), 'price_from' => $prices ? (float) min( $prices ) : 0.0, 'price_from_text' => $this->gateway->price_text( $prices ? (float) min( $prices ) : 0.0 ), 'currency_symbol' => $symbol, 'durations' => $durations, ]; } $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 booktics-available-slots).'; } $card = [ 'services' => $cards, 'button_label' => __( 'Check times', 'aisentic' ), ]; $this->set_card( 'service_list', $card ); return $result; } catch ( \Throwable $e ) { Log::error( 'Booktics service search failed: ' . $e->getMessage() ); return [ 'ok' => false, 'error' => 'The service list could not be loaded. Tell the visitor to open the booking page.', ]; } } /** * Branches (id and name) where any of these staff work. * * @param array $staff [ [ id, name ] ]. * @param array $locations [ [ id, name, team_ids ] ]. * @return array [ [ id, name ] ] */ private function locations_for( array $staff, array $locations ): array { $ids = array_column( $staff, 'id' ); return array_values( array_map( static fn( array $location ): array => [ 'id' => $location['id'], 'name' => $location['name'], ], array_filter( $locations, static fn( array $location ): bool => (bool) array_intersect( $ids, $location['team_ids'] ) ) ) ); } /** * Every visible service, cached once per service 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 services. */ private function all_services(): array { $key = 'aisentic_bt_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; } $services = $this->gateway->services( [] ); set_transient( $key, $services, self::CACHE_TTL ); return $services; } /** * Services whose name, description or category holds every word of the * query, and whose category name holds the category text. * * @param array $services Shaped services. * @param string $query Search words ('' for all). * @param string $category Category name ('' for any). * @return array */ private function matching( array $services, string $query, string $category ): array { // "Salons and Spas" must find the category "Salons & Spas Services". $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( $services, static function ( array $service ) use ( $words, $category, $plain ): bool { if ( '' !== $category && ! array_filter( $service['categories'], static fn( string $name ): bool => false !== mb_strpos( $plain( $name ), $category ) ) ) { return false; } // Duration names too: visitors ask for "the VIP one". $text = $plain( $service['title'] . ' ' . $service['description'] . ' ' . implode( ' ', $service['categories'] ) . ' ' . implode( ' ', array_column( $service['durations'], 'name' ) ) ); foreach ( $words as $word ) { if ( false === mb_strpos( $text, $word ) ) { return false; } } return true; } ) ); } }