| @@ -13,8 +13,10 @@ | ||
| 13 | 13 | /** |
| 14 | 14 | * Keeps v1 and v2 product search fields identical. |
| 15 | 15 | */ |
| 16 | 16 | final class Product_Search { |
| 17 | + private const MAX_SEARCH_TERMS = 10; | |
| 18 | + | |
| 17 | 19 | /** |
| 18 | 20 | * Search product titles, SKUs, and the configured barcode field. |
| 19 | 21 | * |
| 20 | 22 | * @param string $search Search SQL. |
| @@ -22,17 +24,28 @@ | ||
| 22 | 24 | * @return string |
| 23 | 25 | */ |
| 24 | 26 | public static function posts_search( string $search, WP_Query $wp_query ): string { |
| 25 | 27 | global $wpdb; |
| 26 | - if ( empty( $search ) ) { | |
| 28 | + $q = $wp_query->query_vars; | |
| 29 | + $phrase = $q['wcpos_search_phrase'] ?? null; | |
| 30 | + if ( empty( $search ) && null === $phrase ) { | |
| 27 | 31 | return $search; |
| 28 | 32 | } |
| 29 | - $q = $wp_query->query_vars; | |
| 33 | + $terms = null !== $phrase | |
| 34 | + ? preg_split( '/[\s\p{Z}\p{C}]+/u', $phrase, -1, PREG_SPLIT_NO_EMPTY ) | |
| 35 | + : (array) $q['search_terms']; | |
| 36 | + if ( null !== $phrase && ( false === $terms || array() === $terms ) ) { | |
| 37 | + return ' AND 1=0 '; | |
| 38 | + } | |
| 39 | + // Like WP_Query::parse_search() and WooCommerce's search_products(), collapse over-long lists to the phrase. | |
| 40 | + if ( null !== $phrase && self::MAX_SEARCH_TERMS < \count( $terms ) ) { | |
| 41 | + $terms = array( $phrase ); | |
| 42 | + } | |
| 30 | 43 | $n = ! empty( $q['exact'] ) ? '' : '%'; |
| 31 | 44 | $meta_fields = Barcode_Field::search_keys(); |
| 32 | 45 | $meta_placeholders = implode( ', ', array_fill( 0, \count( $meta_fields ), '%s' ) ); |
| 33 | 46 | $search_conditions = array(); |
| 34 | - foreach ( (array) $q['search_terms'] as $term ) { | |
| 47 | + foreach ( $terms as $term ) { | |
| 35 | 48 | $term = $n . $wpdb->esc_like( $term ) . $n; |
| 36 | 49 | // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Table names come from $wpdb; $meta_placeholders is a generated list of %s placeholders, and the keys themselves are passed to prepare() as arguments. |
| 37 | 50 | $search_conditions[] = $wpdb->prepare( |
| 38 | 51 | "( {$wpdb->posts}.post_title LIKE %s OR EXISTS ( |
| @@ -95,9 +108,9 @@ | ||
| 95 | 108 | } |
| 96 | 109 | $keys = Barcode_Field::search_keys(); |
| 97 | 110 | return $wpdb->prepare( |
| 98 | 111 | 'MIN(CASE WHEN pm1.meta_key IN (' . implode( ', ', array_fill( 0, count( $keys ), '%s' ) ) . ') AND pm1.meta_value = %s THEN 0 ELSE 1 END) ASC', |
| 99 | - array_merge( $keys, array( trim( (string) $query->query_vars['s'] ) ) ) | |
| 112 | + array_merge( $keys, array( trim( (string) ( $query->query_vars['wcpos_search_phrase'] ?? $query->query_vars['s'] ) ) ) ) | |
| 100 | 113 | ) . ', ' . ( '' === trim( $orderby ) ? "{$wpdb->posts}.ID DESC" : $orderby ); |
| 101 | 114 | } |
| 102 | 115 | |
| 103 | 116 | /** |
| @@ -106,7 +119,7 @@ | ||
| 106 | 119 | * @param WP_Query $query Query instance. |
| 107 | 120 | * @return bool |
| 108 | 121 | */ |
| 109 | 122 | private static function is_searching( WP_Query $query ): bool { |
| 110 | - return isset( $query->query_vars['s'] ) && '' !== (string) $query->query_vars['s']; | |
| 123 | + return '' !== (string) ( $query->query_vars['wcpos_search_phrase'] ?? $query->query_vars['s'] ?? '' ); | |
| 111 | 124 | } |
| 112 | 125 | } |