PluginProbe
WCPOS – Point of Sale (POS) plugin for WooCommerce / 1.10.17
WCPOS – Point of Sale (POS) plugin for WooCommerce v1.10.17
1.10.25 1.10.24 1.10.23 1.10.22 1.10.21 1.10.20 1.10.19 1.10.18 1.10.17 1.10.16 1.10.15 1.10.13 1.10.14 1.10.12 1.10.11 1.10.10 1.10.9 1.10.8 untagged-3d9b7ccddc54df87c672 1.10.7 1.10.6 1.10.5 1.10.3 1.10.4 1.10.2 All 169 releases
← All changes | includes/API/Order_Search.php +48 -28 1.10.25 → 1.10.17 View file →
@@ -1,7 +1,7 @@
1 1 <?php
2 2 /**
3 - * Legacy WCPOS search API; retained for Pro for one release.
3 + * Shared order search SQL filters.
4 4 *
5 5 * @package WCPOS\WooCommercePOS\API
6 6 */
7 7
@@ -6,55 +6,75 @@
6 6 */
7 7
8 8 namespace WCPOS\WooCommercePOS\API;
9 9
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 - */
10 +/** Builds the POS order search for both WooCommerce storage engines. */
18 11 final class Order_Search {
19 12 /**
20 - * Split order search terms.
13 + * Split a search string into at most ten whitespace-separated terms.
21 14 *
22 - * @deprecated Use the Collection Rules plan.
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.
23 18 *
24 19 * @param string $search Search text.
25 20 * @return string[]
26 21 */
27 22 public static function terms( string $search ): array {
28 - return Search_Rule::terms( $search, Collection_Rules::rules( 'orders' )['search'] );
23 + return array_slice( (array) preg_split( '/[\s\p{Z}]+/u', $search, -1, PREG_SPLIT_NO_EMPTY ), 0, 10 );
29 24 }
30 25 /**
31 - * Build HPOS order search SQL.
26 + * Build an HPOS where fragment with AND-across-terms semantics.
32 27 *
33 - * @deprecated Use the Collection Rules plan.
34 - *
35 28 * @param string $search Search text.
36 - * @param object $query Orders table query.
29 + * @param mixed $query Orders table query.
37 30 * @return string
38 31 */
39 32 public static function hpos_where( string $search, $query ): string {
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 - );
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 );
48 55 }
49 56 /**
50 - * Build legacy order search SQL.
57 + * Build a legacy posts where fragment with AND-across-terms semantics.
51 58 *
52 - * @deprecated Use the Collection Rules plan.
53 - *
54 59 * @param string $search Search text.
55 60 * @return string
56 61 */
57 62 public static function posts_where( string $search ): string {
58 - return Search_Rule::posts_where( $search, Collection_Rules::rules( 'orders' )['search'] );
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 );
59 79 }
60 80 }