*/ public static function expand_query_terms( string $query ): array { $normalized = strtolower( trim( preg_replace( '/\s+/', ' ', $query ) ) ); if ( $normalized === '' ) { return array(); } $terms = array(); $split = preg_split( '/\s+/', $normalized ); foreach ( false === $split ? array() : $split as $word ) { $word = trim( $word ); if ( $word === '' || strlen( $word ) < 2 ) { continue; } foreach ( self::expand_word_variants( $word ) as $variant ) { $terms[] = $variant; } } return array_values( array_unique( $terms ) ); } /** * @return list */ public static function expand_word_variants( string $word ): array { $word = strtolower( trim( $word ) ); if ( $word === '' || strlen( $word ) < 2 ) { return array(); } $variants = array( $word ); if ( strlen( $word ) > 3 && str_ends_with( $word, 'ies' ) ) { $variants[] = substr( $word, 0, -3 ) . 'y'; } if ( strlen( $word ) > 3 && str_ends_with( $word, 'es' ) ) { $variants[] = substr( $word, 0, -2 ); $variants[] = substr( $word, 0, -1 ); } if ( strlen( $word ) > 3 && str_ends_with( $word, 's' ) && ! str_ends_with( $word, 'ss' ) ) { $variants[] = substr( $word, 0, -1 ); } if ( ! str_ends_with( $word, 's' ) ) { if ( str_ends_with( $word, 'y' ) && strlen( $word ) > 2 && ! in_array( substr( $word, -2, 1 ), array( 'a', 'e', 'i', 'o', 'u' ), true ) ) { $variants[] = substr( $word, 0, -1 ) . 'ies'; } $variants[] = $word . 's'; } return array_values( array_unique( array_filter( $variants ) ) ); } /** * Whether every query word (with plural variants) matches the product text. */ public static function product_matches_query( WC_Product $product, string $query ): bool { $normalized = strtolower( trim( preg_replace( '/\s+/', ' ', $query ) ) ); if ( $normalized === '' ) { return false; } $haystack = self::product_search_haystack( $product ); $words = preg_split( '/\s+/', $normalized ); $words = false === $words ? array() : $words; foreach ( $words as $word ) { $word = trim( $word ); if ( $word === '' || strlen( $word ) < 2 ) { continue; } if ( ! self::word_matches_haystack( $word, $haystack ) ) { return false; } } return true; } /** * Rank higher = better match for sorting. */ public static function product_match_score( WC_Product $product, string $query ): int { $normalized = strtolower( trim( preg_replace( '/\s+/', ' ', $query ) ) ); if ( $normalized === '' ) { return 0; } $title = strtolower( $product->get_name() ); $haystack = self::product_search_haystack( $product ); if ( $title === $normalized ) { return 100; } if ( str_contains( $title, $normalized ) ) { return 90; } $words = preg_split( '/\s+/', $normalized ); $words = false === $words ? array() : array_values( array_filter( $words, static function ( string $word ): bool { return strlen( $word ) >= 2; } ) ); if ( $words !== array() ) { $title_hits = 0; foreach ( $words as $word ) { if ( self::word_matches_haystack( $word, $title ) ) { ++$title_hits; } } if ( $title_hits === count( $words ) ) { return 85; } } if ( self::product_matches_query( $product, $query ) ) { return 70; } if ( str_contains( $haystack, $normalized ) ) { return 50; } return 0; } private static function word_matches_haystack( string $word, string $haystack ): bool { foreach ( self::expand_word_variants( $word ) as $variant ) { if ( str_contains( $haystack, $variant ) ) { return true; } } return false; } private static function product_search_haystack( WC_Product $product ): string { $parts = array( strtolower( $product->get_name() ), strtolower( $product->get_slug() ), ); $sku = strtolower( $product->get_sku() ); if ( $sku !== '' ) { $parts[] = $sku; } $description = strtolower( wp_strip_all_tags( $product->get_short_description() . ' ' . $product->get_description() ) ); if ( $description !== '' ) { $parts[] = $description; } foreach ( $product->get_category_ids() as $term_id ) { $term = get_term( (int) $term_id, 'product_cat' ); if ( $term && ! is_wp_error( $term ) ) { $parts[] = strtolower( (string) $term->name ); $parts[] = strtolower( (string) $term->slug ); } } foreach ( $product->get_attributes() as $attribute ) { if ( $attribute->is_taxonomy() ) { $terms = wc_get_product_terms( $product->get_id(), $attribute->get_name(), array( 'fields' => 'names' ) ); if ( is_array( $terms ) ) { foreach ( $terms as $term_name ) { $parts[] = strtolower( (string) $term_name ); } } continue; } foreach ( $attribute->get_options() as $option ) { $parts[] = strtolower( (string) $option ); } } return trim( preg_replace( '/\s+/', ' ', implode( ' ', $parts ) ) ); } }