| @@ -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,84 +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 | - $search_conditions = array(); | |
| 33 | - foreach ( (array) $q['search_terms'] as $term ) { | |
| 34 | - $term = $n . $wpdb->esc_like( $term ) . $n; | |
| 35 | - $search_conditions[] = $wpdb->prepare( "({$wpdb->posts}.post_title LIKE %s)", $term ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- table name is safe. | |
| 36 | - foreach ( $meta_fields as $field ) { | |
| 37 | - $search_conditions[] = $wpdb->prepare( '(pm1.meta_value LIKE %s AND pm1.meta_key = %s)', $term, $field ); | |
| 38 | - } | |
| 39 | - } | |
| 40 | - | |
| 41 | - if ( ! empty( $search_conditions ) ) { | |
| 42 | - $search = ' AND (' . implode( ' OR ', $search_conditions ) . ') '; | |
| 43 | - if ( ! is_user_logged_in() ) { | |
| 44 | - $search .= " AND ($wpdb->posts.post_password = '') "; | |
| 45 | - } | |
| 46 | - } | |
| 47 | - return $search; | |
| 30 | + return Search_Rule::posts_search( $search, $wp_query->query_vars, Collection_Rules::rules( 'products' )['search'] ); | |
| 48 | 31 | } |
| 49 | - | |
| 50 | 32 | /** |
| 51 | - * Join product meta while searching. | |
| 33 | + * Join product search metadata. | |
| 52 | 34 | * |
| 35 | + * @deprecated Use the Collection Rules plan. | |
| 36 | + * | |
| 53 | 37 | * @param string $join JOIN SQL. |
| 54 | 38 | * @param WP_Query $query Query instance. |
| 55 | 39 | * @return string |
| 56 | 40 | */ |
| 57 | 41 | public static function posts_join( string $join, WP_Query $query ): string { |
| 58 | - global $wpdb; | |
| 59 | - if ( self::is_searching( $query ) && false === strpos( $join, 'pm1' ) ) { | |
| 60 | - $join .= " LEFT JOIN {$wpdb->postmeta} pm1 ON {$wpdb->posts}.ID = pm1.post_id "; | |
| 61 | - } | |
| 62 | - return $join; | |
| 42 | + return Search_Rule::posts_join( $join, $query->query_vars ); | |
| 63 | 43 | } |
| 64 | - | |
| 65 | 44 | /** |
| 66 | - * Group product search results after joining meta. | |
| 45 | + * Group product search results. | |
| 67 | 46 | * |
| 47 | + * @deprecated Use the Collection Rules plan. | |
| 48 | + * | |
| 68 | 49 | * @param string $groupby GROUP BY SQL. |
| 69 | 50 | * @param WP_Query $query Query instance. |
| 70 | 51 | * @return string |
| 71 | 52 | */ |
| 72 | 53 | public static function posts_groupby( string $groupby, WP_Query $query ): string { |
| 73 | - global $wpdb; | |
| 74 | - if ( self::is_searching( $query ) ) { | |
| 75 | - $groupby = "{$wpdb->posts}.ID"; | |
| 76 | - } | |
| 77 | - return $groupby; | |
| 54 | + return Search_Rule::posts_groupby( $groupby, $query->query_vars ); | |
| 78 | 55 | } |
| 79 | - | |
| 80 | 56 | /** |
| 81 | - * Whether the query carries a search term. Not empty(): the literal term "0" is a search. | |
| 57 | + * Rank exact product matches. | |
| 82 | 58 | * |
| 83 | - * @param WP_Query $query Query instance. | |
| 84 | - * @return bool | |
| 59 | + * @deprecated Use the Collection Rules plan. | |
| 60 | + * | |
| 61 | + * @param string $orderby ORDER BY SQL. | |
| 62 | + * @param WP_Query $query Query instance. | |
| 63 | + * @return string | |
| 85 | 64 | */ |
| 86 | - private static function is_searching( WP_Query $query ): bool { | |
| 87 | - return isset( $query->query_vars['s'] ) && '' !== (string) $query->query_vars['s']; | |
| 65 | + public static function posts_orderby( string $orderby, WP_Query $query ): string { | |
| 66 | + return Search_Rule::posts_orderby( $orderby, $query->query_vars, Collection_Rules::rules( 'products' )['search'] ); | |
| 88 | 67 | } |
| 89 | 68 | } |