From 874be93c14b18c7b8949768461cd912ca7876290 Mon Sep 17 00:00:00 2001 From: Christopher Allford Date: Tue, 24 Dec 2019 15:00:40 -0800 Subject: [PATCH] Removed the lowercase conversion of product search terms When searched in a case sensitive manner, the conversion to lowercase causes uppercase matches that would have been found to be lost. This change increases the tolerance of the OR search format while also removing this unintended side-effect. --- includes/data-stores/class-wc-product-data-store-cpt.php | 9 ++++----- tests/unit-tests/product/data-store.php | 6 ++++++ 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/includes/data-stores/class-wc-product-data-store-cpt.php b/includes/data-stores/class-wc-product-data-store-cpt.php index 98fb2c3842c..9d951bd4a90 100644 --- a/includes/data-stores/class-wc-product-data-store-cpt.php +++ b/includes/data-stores/class-wc-product-data-store-cpt.php @@ -880,7 +880,7 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da } return $wpdb->get_results( - // phpcs:disable WordPress.DB.PreparedSQL.NotPrepared + // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared " SELECT posts.ID as id, posts.post_parent as parent_id FROM {$wpdb->posts} AS posts @@ -898,7 +898,7 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da ) GROUP BY posts.ID " - // phpcs:enable WordPress.DB.PreparedSQL.NotPrepared + // phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared ); } @@ -1553,7 +1553,6 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da $type_where = ''; $status_where = ''; $limit_query = ''; - $term = wc_strtolower( $term ); /** * Hook woocommerce_search_products_post_statuses. @@ -1567,8 +1566,8 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da ); // See if search term contains OR keywords. - if ( strstr( $term, ' or ' ) ) { - $term_groups = explode( ' or ', $term ); + if ( stristr( $term, ' or ' ) ) { + $term_groups = preg_split( '/\s+or\s+/i', $term ); } else { $term_groups = array( $term ); } diff --git a/tests/unit-tests/product/data-store.php b/tests/unit-tests/product/data-store.php index f39ca4db78d..b67ad8a4621 100644 --- a/tests/unit-tests/product/data-store.php +++ b/tests/unit-tests/product/data-store.php @@ -797,6 +797,12 @@ class WC_Tests_Product_Data_Store extends WC_Unit_Test_Case { $this->assertContains( $product3->get_id(), $results ); $this->assertContains( $product4->get_id(), $results ); + $results = $data_store->search_products( 'blue-widget-1 OR green-widget', '', true, true ); + $this->assertContains( $product->get_id(), $results ); + $this->assertNotContains( $product2->get_id(), $results ); + $this->assertContains( $product3->get_id(), $results ); + $this->assertContains( $product4->get_id(), $results ); + $results = $data_store->search_products( '"green widget"', '', true, true ); $this->assertNotContains( $product->get_id(), $results ); $this->assertNotContains( $product2->get_id(), $results );