| @@ -1,7 +1,7 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | - * Shared product search SQL filters. | |
| 3 | + * Legacy WCPOS search API; retained for Pro for one release. | |
| 4 | 4 | * |
| 5 | 5 | * @package WCPOS\WooCommercePOS\API |
| 6 | 6 | */ |
| 7 | 7 | |
| @@ -6,107 +6,63 @@ | ||
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | 8 | namespace WCPOS\WooCommercePOS\API; |
| 9 | 9 | |
| 10 | -use WCPOS\WooCommercePOS\Services\Barcode_Field; | |
| 10 | +use WCPOS\WooCommercePOS\Sync\Collection_Rules; | |
| 11 | +use WCPOS\WooCommercePOS\Sync\Product_Search as Search_Rule; | |
| 11 | 12 | use WP_Query; |
| 12 | 13 | |
| 13 | 14 | /** |
| 14 | - * Keeps v1 and v2 product search fields identical. | |
| 15 | + * Legacy search entry points. | |
| 16 | + * | |
| 17 | + * @deprecated Search is owned by Sync\Collection_Rules. | |
| 15 | 18 | */ |
| 16 | 19 | final class Product_Search { |
| 17 | 20 | /** |
| 18 | - * Search product titles, SKUs, and the configured barcode field. | |
| 21 | + * Build product search SQL. | |
| 19 | 22 | * |
| 23 | + * @deprecated Use the Collection Rules plan. | |
| 24 | + * | |
| 20 | 25 | * @param string $search Search SQL. |
| 21 | 26 | * @param WP_Query $wp_query Query instance. |
| 22 | 27 | * @return string |
| 23 | 28 | */ |
| 24 | 29 | public static function posts_search( string $search, WP_Query $wp_query ): string { |
| 25 | - global $wpdb; | |
| 26 | - if ( empty( $search ) ) { | |
| 27 | - return $search; | |
| 28 | - } | |
| 29 | - $q = $wp_query->query_vars; | |
| 30 | - $n = ! empty( $q['exact'] ) ? '' : '%'; | |
| 31 | - $meta_fields = Barcode_Field::search_keys(); | |
| 32 | - $meta_placeholders = implode( ', ', array_fill( 0, \count( $meta_fields ), '%s' ) ); | |
| 33 | - $search_conditions = array(); | |
| 34 | - foreach ( (array) $q['search_terms'] as $term ) { | |
| 35 | - $term = $n . $wpdb->esc_like( $term ) . $n; | |
| 36 | - // 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 | - $search_conditions[] = $wpdb->prepare( | |
| 38 | - "( {$wpdb->posts}.post_title LIKE %s OR EXISTS ( | |
| 39 | - SELECT 1 FROM {$wpdb->postmeta} AS wcpos_search_meta WHERE wcpos_search_meta.post_id = {$wpdb->posts}.ID AND wcpos_search_meta.meta_key IN ($meta_placeholders) AND wcpos_search_meta.meta_value LIKE %s | |
| 40 | - ) )", | |
| 41 | - array_merge( array( $term ), $meta_fields, array( $term ) ) | |
| 42 | - ); | |
| 43 | - // phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared | |
| 44 | - } | |
| 45 | - | |
| 46 | - if ( ! empty( $search_conditions ) ) { | |
| 47 | - $search = ' AND (' . implode( ' AND ', $search_conditions ) . ') '; | |
| 48 | - if ( ! is_user_logged_in() ) { | |
| 49 | - $search .= " AND ($wpdb->posts.post_password = '') "; | |
| 50 | - } | |
| 51 | - } | |
| 52 | - return $search; | |
| 30 | + return Search_Rule::posts_search( $search, $wp_query->query_vars, Collection_Rules::rules( 'products' )['search'] ); | |
| 53 | 31 | } |
| 54 | - | |
| 55 | 32 | /** |
| 56 | - * Join product meta while searching. | |
| 33 | + * Join product search metadata. | |
| 57 | 34 | * |
| 35 | + * @deprecated Use the Collection Rules plan. | |
| 36 | + * | |
| 58 | 37 | * @param string $join JOIN SQL. |
| 59 | 38 | * @param WP_Query $query Query instance. |
| 60 | 39 | * @return string |
| 61 | 40 | */ |
| 62 | 41 | public static function posts_join( string $join, WP_Query $query ): string { |
| 63 | - global $wpdb; | |
| 64 | - if ( self::is_searching( $query ) && false === strpos( $join, 'pm1' ) ) { | |
| 65 | - $join .= " LEFT JOIN {$wpdb->postmeta} pm1 ON {$wpdb->posts}.ID = pm1.post_id "; | |
| 66 | - } | |
| 67 | - return $join; | |
| 42 | + return Search_Rule::posts_join( $join, $query->query_vars ); | |
| 68 | 43 | } |
| 69 | - | |
| 70 | 44 | /** |
| 71 | - * Group product search results after joining meta. | |
| 45 | + * Group product search results. | |
| 72 | 46 | * |
| 47 | + * @deprecated Use the Collection Rules plan. | |
| 48 | + * | |
| 73 | 49 | * @param string $groupby GROUP BY SQL. |
| 74 | 50 | * @param WP_Query $query Query instance. |
| 75 | 51 | * @return string |
| 76 | 52 | */ |
| 77 | 53 | public static function posts_groupby( string $groupby, WP_Query $query ): string { |
| 78 | - global $wpdb; | |
| 79 | - if ( self::is_searching( $query ) ) { | |
| 80 | - $groupby = "{$wpdb->posts}.ID"; | |
| 81 | - } | |
| 82 | - return $groupby; | |
| 54 | + return Search_Rule::posts_groupby( $groupby, $query->query_vars ); | |
| 83 | 55 | } |
| 84 | - | |
| 85 | 56 | /** |
| 86 | - * Rank exact SKU or barcode matches ahead of substring matches. | |
| 57 | + * Rank exact product matches. | |
| 87 | 58 | * |
| 59 | + * @deprecated Use the Collection Rules plan. | |
| 60 | + * | |
| 88 | 61 | * @param string $orderby ORDER BY SQL. |
| 89 | 62 | * @param WP_Query $query Query instance. |
| 63 | + * @return string | |
| 90 | 64 | */ |
| 91 | 65 | public static function posts_orderby( string $orderby, WP_Query $query ): string { |
| 92 | - global $wpdb; | |
| 93 | - if ( ! self::is_searching( $query ) ) { | |
| 94 | - return $orderby; | |
| 95 | - } | |
| 96 | - $keys = Barcode_Field::search_keys(); | |
| 97 | - return $wpdb->prepare( | |
| 98 | - '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'] ) ) ) | |
| 100 | - ) . ', ' . ( '' === trim( $orderby ) ? "{$wpdb->posts}.ID DESC" : $orderby ); | |
| 101 | - } | |
| 102 | - | |
| 103 | - /** | |
| 104 | - * Whether the query carries a search term. Not empty(): the literal term "0" is a search. | |
| 105 | - * | |
| 106 | - * @param WP_Query $query Query instance. | |
| 107 | - * @return bool | |
| 108 | - */ | |
| 109 | - private static function is_searching( WP_Query $query ): bool { | |
| 110 | - return isset( $query->query_vars['s'] ) && '' !== (string) $query->query_vars['s']; | |
| 66 | + return Search_Rule::posts_orderby( $orderby, $query->query_vars, Collection_Rules::rules( 'products' )['search'] ); | |
| 111 | 67 | } |
| 112 | 68 | } |