| @@ -1,7 +1,7 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | - * Shared order 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,75 +6,55 @@ | ||
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | 8 | namespace WCPOS\WooCommercePOS\API; |
| 9 | 9 | |
| 10 | -/** Builds the POS order search for both WooCommerce storage engines. */ | |
| 10 | +use WCPOS\WooCommercePOS\Sync\Collection_Rules; | |
| 11 | +use WCPOS\WooCommercePOS\Sync\Order_Search as Search_Rule; | |
| 12 | + | |
| 13 | +/** | |
| 14 | + * Legacy search entry points. | |
| 15 | + * | |
| 16 | + * @deprecated Search is owned by Sync\Collection_Rules. | |
| 17 | + */ | |
| 11 | 18 | final class Order_Search { |
| 12 | 19 | /** |
| 13 | - * Split a search string into at most ten whitespace-separated terms. | |
| 20 | + * Split order search terms. | |
| 14 | 21 | * |
| 15 | - * Unicode-aware: a pasted non-breaking space (U+00A0) separates terms like a | |
| 16 | - * space does, and an all-whitespace string yields no terms. Malformed UTF-8 | |
| 17 | - * makes preg_split() return false, which also yields no terms. | |
| 22 | + * @deprecated Use the Collection Rules plan. | |
| 18 | 23 | * |
| 19 | 24 | * @param string $search Search text. |
| 20 | 25 | * @return string[] |
| 21 | 26 | */ |
| 22 | 27 | public static function terms( string $search ): array { |
| 23 | - return array_slice( (array) preg_split( '/[\s\p{Z}]+/u', $search, -1, PREG_SPLIT_NO_EMPTY ), 0, 10 ); | |
| 28 | + return Search_Rule::terms( $search, Collection_Rules::rules( 'orders' )['search'] ); | |
| 24 | 29 | } |
| 25 | 30 | /** |
| 26 | - * Build an HPOS where fragment with AND-across-terms semantics. | |
| 31 | + * Build HPOS order search SQL. | |
| 27 | 32 | * |
| 33 | + * @deprecated Use the Collection Rules plan. | |
| 34 | + * | |
| 28 | 35 | * @param string $search Search text. |
| 29 | - * @param mixed $query Orders table query. | |
| 36 | + * @param object $query Orders table query. | |
| 30 | 37 | * @return string |
| 31 | 38 | */ |
| 32 | 39 | public static function hpos_where( string $search, $query ): string { |
| 33 | - global $wpdb; | |
| 34 | - $conditions = array(); | |
| 35 | - $orders = $query->get_table_name( 'orders' ); | |
| 36 | - $addresses = $query->get_table_name( 'addresses' ); | |
| 37 | - foreach ( self::terms( $search ) as $term ) { | |
| 38 | - $like = '%' . $wpdb->esc_like( $term ) . '%'; | |
| 39 | - $id = ctype_digit( $term ) ? "`{$orders}`.id = %d OR " : ''; | |
| 40 | - $args = array_fill( 0, 6, $like ); | |
| 41 | - if ( '' !== $id ) { | |
| 42 | - array_unshift( $args, (int) $term ); | |
| 43 | - } | |
| 44 | - // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Table names come from WooCommerce. | |
| 45 | - $conditions[] = $wpdb->prepare( | |
| 46 | - "( {$id}`{$orders}`.billing_email LIKE %s OR `{$orders}`.id IN ( | |
| 47 | - SELECT order_id FROM `{$addresses}` WHERE address_type = 'billing' | |
| 48 | - AND ( first_name LIKE %s OR last_name LIKE %s OR company LIKE %s OR email LIKE %s OR phone LIKE %s ) | |
| 49 | - ) )", | |
| 50 | - $args | |
| 51 | - ); | |
| 52 | - // phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared | |
| 53 | - } | |
| 54 | - return implode( ' AND ', $conditions ); | |
| 40 | + return Search_Rule::hpos_where( | |
| 41 | + $search, | |
| 42 | + array( | |
| 43 | + 'orders' => $query->get_table_name( 'orders' ), | |
| 44 | + 'addresses' => $query->get_table_name( 'addresses' ), | |
| 45 | + ), | |
| 46 | + Collection_Rules::rules( 'orders' )['search'] | |
| 47 | + ); | |
| 55 | 48 | } |
| 56 | 49 | /** |
| 57 | - * Build a legacy posts where fragment with AND-across-terms semantics. | |
| 50 | + * Build legacy order search SQL. | |
| 58 | 51 | * |
| 52 | + * @deprecated Use the Collection Rules plan. | |
| 53 | + * | |
| 59 | 54 | * @param string $search Search text. |
| 60 | 55 | * @return string |
| 61 | 56 | */ |
| 62 | 57 | public static function posts_where( string $search ): string { |
| 63 | - global $wpdb; | |
| 64 | - $conditions = array(); | |
| 65 | - foreach ( self::terms( $search ) as $term ) { | |
| 66 | - $like = '%' . $wpdb->esc_like( $term ) . '%'; | |
| 67 | - $id = ctype_digit( $term ) ? "{$wpdb->posts}.ID = %d OR " : ''; | |
| 68 | - $args = '' === $id ? array( $like ) : array( (int) $term, $like ); | |
| 69 | - // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Table names come from $wpdb. | |
| 70 | - $conditions[] = $wpdb->prepare( | |
| 71 | - "( {$id}EXISTS ( | |
| 72 | - SELECT 1 FROM {$wpdb->postmeta} AS wcpos_order_search_meta WHERE wcpos_order_search_meta.post_id = {$wpdb->posts}.ID AND wcpos_order_search_meta.meta_key IN ( '_billing_first_name', '_billing_last_name', '_billing_company', '_billing_email', '_billing_phone' ) AND wcpos_order_search_meta.meta_value LIKE %s | |
| 73 | - ) )", | |
| 74 | - $args | |
| 75 | - ); | |
| 76 | - // phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared | |
| 77 | - } | |
| 78 | - return implode( ' AND ', $conditions ); | |
| 58 | + return Search_Rule::posts_where( $search, Collection_Rules::rules( 'orders' )['search'] ); | |
| 79 | 59 | } |
| 80 | 60 | } |