From 2a7438f1b5718cc3f20a0e0c856534582c37b23f Mon Sep 17 00:00:00 2001 From: Felipe Elia Date: Fri, 14 Jan 2022 14:59:55 -0300 Subject: [PATCH 01/20] Fix WooCommerce Orders search --- .../Feature/WooCommerce/WooCommerce.php | 33 +++---------------- 1 file changed, 5 insertions(+), 28 deletions(-) diff --git a/includes/classes/Feature/WooCommerce/WooCommerce.php b/includes/classes/Feature/WooCommerce/WooCommerce.php index f8c5e3ee70..fa5eb8ac08 100644 --- a/includes/classes/Feature/WooCommerce/WooCommerce.php +++ b/includes/classes/Feature/WooCommerce/WooCommerce.php @@ -127,15 +127,6 @@ public function whitelist_meta_keys( $meta, $post ) { ); } - /** - * Prevent order fields search meta query - * - * @since 2.1 - */ - public function shop_order_search_fields() { - return []; - } - /** * Make sure all loop shop post ins are IDS. We have to pass post objects here since we override * the fields=>id query for the layered filter nav query @@ -394,7 +385,7 @@ public function translate_args( $query ) { // Search query if ( 'shop_order' === $post_type ) { - $search_fields = $query->get( 'search_fields', array( 'post_title', 'post_content', 'post_excerpt' ) ); + $search_fields = $query->get( 'search_fields', array( 'ID^999999', 'post_title', 'post_content', 'post_excerpt' ) ); $search_fields['meta'] = array_map( 'wc_clean', @@ -605,7 +596,7 @@ public function bypass_order_permissions_check( $override, $post_id ) { * @param \WP_Query $query Current query */ public function maybe_hook_woocommerce_search_fields( $query ) { - global $pagenow, $wp; + global $pagenow, $wp, $wc_list_table; if ( ! $this->should_integrate_with_query( $query ) ) { return; @@ -615,7 +606,7 @@ public function maybe_hook_woocommerce_search_fields( $query ) { return; } - add_filter( 'woocommerce_shop_order_search_fields', [ $this, 'shop_order_search_fields' ], 9999 ); + remove_action( 'parse_query', [ $wc_list_table, 'search_custom_fields' ] ); } /** @@ -640,22 +631,8 @@ public function search_order( $wp ) { } $search_key_safe = str_replace( array( 'Order #', '#' ), '', wc_clean( $_GET['s'] ) ); // phpcs:ignore WordPress.Security.NonceVerification - $order_id = absint( $search_key_safe ); - - /** - * Order ID 0 is not valid value. - */ - $order = $order_id > 0 ? wc_get_order( $order_id ) : false; - - // If the order doesn't exist, fallback to other fields - if ( ! $order ) { - unset( $wp->query_vars['post__in'] ); - $wp->query_vars['s'] = $search_key_safe; - } else { - // we found the order. don't query ES - unset( $wp->query_vars['s'] ); - $wp->query_vars['post__in'] = array( absint( $search_key_safe ) ); - } + unset( $wp->query_vars['post__in'] ); + $wp->query_vars['s'] = $search_key_safe; } /** From 26716425cd09e224d165e163816a8cf25e1542a6 Mon Sep 17 00:00:00 2001 From: Felipe Elia Date: Fri, 14 Jan 2022 15:17:33 -0300 Subject: [PATCH 02/20] Add a new ep_woocommerce_shop_order_search_fields filter --- .../classes/Feature/WooCommerce/WooCommerce.php | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/includes/classes/Feature/WooCommerce/WooCommerce.php b/includes/classes/Feature/WooCommerce/WooCommerce.php index fa5eb8ac08..6df15f92f5 100644 --- a/includes/classes/Feature/WooCommerce/WooCommerce.php +++ b/includes/classes/Feature/WooCommerce/WooCommerce.php @@ -385,12 +385,12 @@ public function translate_args( $query ) { // Search query if ( 'shop_order' === $post_type ) { - $search_fields = $query->get( 'search_fields', array( 'ID^999999', 'post_title', 'post_content', 'post_excerpt' ) ); + $search_fields = $query->get( 'search_fields', array( 'ID', 'post_title', 'post_content', 'post_excerpt' ) ); $search_fields['meta'] = array_map( 'wc_clean', /** - * Filter shop order fields to search for WooCommerce + * Filter shop order meta fields to search for WooCommerce * * @hook shop_order_search_fields * @param {array} $fields Shop order fields @@ -424,7 +424,18 @@ public function translate_args( $query ) { ) ); - $query->set( 'search_fields', $search_fields ); + $query->set( + 'search_fields', + /** + * Filter all the shop order fields to search for WooCommerce + * + * @hook ep_woocommerce_shop_order_search_fields + * @since 4.0.0 + * @param {array} $fields Shop order fields + * @return {array} New fields + */ + apply_filters( 'ep_woocommerce_shop_order_search_fields', $search_fields ) + ); } elseif ( 'product' === $post_type && defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ) { $search_fields = $query->get( 'search_fields', array( 'post_title', 'post_content', 'post_excerpt' ) ); From a391c8985f2606ca1cfebf95c9cd7fd90d979868 Mon Sep 17 00:00:00 2001 From: Felipe Elia Date: Mon, 24 Jan 2022 12:02:58 -0300 Subject: [PATCH 03/20] Shop orders; Conditionally add ID to search field + docs --- .../classes/Feature/WooCommerce/WooCommerce.php | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/includes/classes/Feature/WooCommerce/WooCommerce.php b/includes/classes/Feature/WooCommerce/WooCommerce.php index 6df15f92f5..5fca469197 100644 --- a/includes/classes/Feature/WooCommerce/WooCommerce.php +++ b/includes/classes/Feature/WooCommerce/WooCommerce.php @@ -385,7 +385,11 @@ public function translate_args( $query ) { // Search query if ( 'shop_order' === $post_type ) { - $search_fields = $query->get( 'search_fields', array( 'ID', 'post_title', 'post_content', 'post_excerpt' ) ); + $default_search_fields = array( 'post_title', 'post_content', 'post_excerpt' ); + if ( is_int( $s ) ) { + $default_search_fields[] = 'ID'; + } + $search_fields = $query->get( 'search_fields', $default_search_fields ); $search_fields['meta'] = array_map( 'wc_clean', @@ -431,10 +435,11 @@ public function translate_args( $query ) { * * @hook ep_woocommerce_shop_order_search_fields * @since 4.0.0 - * @param {array} $fields Shop order fields - * @return {array} New fields + * @param {array} $fields Shop order fields + * @param {WP_Query} $query WP Query + * @return {array} New fields */ - apply_filters( 'ep_woocommerce_shop_order_search_fields', $search_fields ) + apply_filters( 'ep_woocommerce_shop_order_search_fields', $search_fields, $query ) ); } elseif ( 'product' === $post_type && defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ) { $search_fields = $query->get( 'search_fields', array( 'post_title', 'post_content', 'post_excerpt' ) ); From d0f8530027888b1a50858a58477d9e053cc62e02 Mon Sep 17 00:00:00 2001 From: Felipe Elia Date: Mon, 24 Jan 2022 12:03:26 -0300 Subject: [PATCH 04/20] Shop Orders: tests for searches with numbers --- tests/php/features/TestWooCommerce.php | 76 +++++++++++++++++++++++++- 1 file changed, 74 insertions(+), 2 deletions(-) diff --git a/tests/php/features/TestWooCommerce.php b/tests/php/features/TestWooCommerce.php index fe77d80703..c943e8f69b 100644 --- a/tests/php/features/TestWooCommerce.php +++ b/tests/php/features/TestWooCommerce.php @@ -140,8 +140,6 @@ public function testSearchOnShopOrderAdmin() { ElasticPress\Elasticsearch::factory()->refresh_indices(); - add_action( 'ep_wp_query_search', array( $this, 'action_wp_query_search' ), 10, 0 ); - $args = array( 's' => 'findme', 'post_type' => 'shop_order', @@ -154,6 +152,80 @@ public function testSearchOnShopOrderAdmin() { $this->assertEquals( 1, $query->found_posts ); } + /** + * Test search for shop orders by order ID + * + * @since 4.0.0 + * @group woocommerce + */ + public function testSearchShopOrderById() { + ElasticPress\Features::factory()->activate_feature( 'protected_content' ); + ElasticPress\Features::factory()->activate_feature( 'woocommerce' ); + ElasticPress\Features::factory()->setup_features(); + + $shop_order_id = Functions\create_and_sync_post( + array( + 'post_type' => 'shop_order', + ) + ); + + ElasticPress\Elasticsearch::factory()->refresh_indices(); + + $args = array( + 's' => $shop_order_id, + 'post_type' => 'shop_order', + ); + + $query = new \WP_Query( $args ); + + $this->assertTrue( $query->elasticsearch_success ); + $this->assertEquals( 1, $query->post_count ); + $this->assertEquals( 1, $query->found_posts ); + } + + /** + * Test search for shop orders matching field and ID. + * + * If searching for a number that is an order ID and part of another order's metadata, + * both should be returned. + * + * @since 4.0.0 + * @group woocommerce + */ + public function testSearchShopOrderByMetaFieldAndId() { + ElasticPress\Features::factory()->activate_feature( 'protected_content' ); + ElasticPress\Features::factory()->activate_feature( 'woocommerce' ); + ElasticPress\Features::factory()->setup_features(); + + $shop_order_id_1 = Functions\create_and_sync_post( + array( + 'post_type' => 'shop_order', + ) + ); + + Functions\create_and_sync_post( + array( + 'post_type' => 'shop_order', + ), + array( + '_billing_phone' => 'Phone number that matches an order ID: ' . $shop_order_id_1, + ) + ); + + ElasticPress\Elasticsearch::factory()->refresh_indices(); + + $args = array( + 's' => $shop_order_id_1, + 'post_type' => 'shop_order', + ); + + $query = new \WP_Query( $args ); + + $this->assertTrue( $query->elasticsearch_success ); + $this->assertEquals( 2, $query->post_count ); + $this->assertEquals( 2, $query->found_posts ); + } + /** * Test search integration is on in general for product searches * From a08189308553f70aa1413a33def6b50bab9eaa20 Mon Sep 17 00:00:00 2001 From: Felipe Elia Date: Mon, 24 Jan 2022 14:34:10 -0300 Subject: [PATCH 05/20] [Temporary] Log the request and the query on errors --- tests/php/features/TestWooCommerce.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tests/php/features/TestWooCommerce.php b/tests/php/features/TestWooCommerce.php index c943e8f69b..ac5fd3d33a 100644 --- a/tests/php/features/TestWooCommerce.php +++ b/tests/php/features/TestWooCommerce.php @@ -178,6 +178,19 @@ public function testSearchShopOrderById() { $query = new \WP_Query( $args ); + // Log problems in the + add_action( + 'ep_invalid_response', + function( $request, $query ) { + echo '
';
+				print_r( $request );
+				echo '
'; + echo '
';
+				print_r( $query );
+				echo '
'; + } + ); + $this->assertTrue( $query->elasticsearch_success ); $this->assertEquals( 1, $query->post_count ); $this->assertEquals( 1, $query->found_posts ); From 8823b8977c8e1da875ccf6fac0f9b8d9ee4bfbf2 Mon Sep 17 00:00:00 2001 From: Felipe Elia Date: Mon, 24 Jan 2022 14:40:54 -0300 Subject: [PATCH 06/20] Add params to log function --- tests/php/features/TestWooCommerce.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/php/features/TestWooCommerce.php b/tests/php/features/TestWooCommerce.php index ac5fd3d33a..b35fb39b20 100644 --- a/tests/php/features/TestWooCommerce.php +++ b/tests/php/features/TestWooCommerce.php @@ -188,7 +188,9 @@ function( $request, $query ) { echo '
';
 				print_r( $query );
 				echo '
'; - } + }, + 10, + 2 ); $this->assertTrue( $query->elasticsearch_success ); From 315b8b92337237fc684de3e28ac838d0b63f523e Mon Sep 17 00:00:00 2001 From: Felipe Elia Date: Mon, 24 Jan 2022 14:57:21 -0300 Subject: [PATCH 07/20] Fix place of logging function --- tests/php/features/TestWooCommerce.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/php/features/TestWooCommerce.php b/tests/php/features/TestWooCommerce.php index b35fb39b20..d4a59821e9 100644 --- a/tests/php/features/TestWooCommerce.php +++ b/tests/php/features/TestWooCommerce.php @@ -176,8 +176,6 @@ public function testSearchShopOrderById() { 'post_type' => 'shop_order', ); - $query = new \WP_Query( $args ); - // Log problems in the add_action( 'ep_invalid_response', @@ -193,6 +191,8 @@ function( $request, $query ) { 2 ); + $query = new \WP_Query( $args ); + $this->assertTrue( $query->elasticsearch_success ); $this->assertEquals( 1, $query->post_count ); $this->assertEquals( 1, $query->found_posts ); From a70bbc3b16afeb0da717ccc1fd891babff3be943 Mon Sep 17 00:00:00 2001 From: Felipe Elia Date: Mon, 24 Jan 2022 15:29:00 -0300 Subject: [PATCH 08/20] Use ES's _id instead of ID --- includes/classes/Feature/WooCommerce/WooCommerce.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/includes/classes/Feature/WooCommerce/WooCommerce.php b/includes/classes/Feature/WooCommerce/WooCommerce.php index 5fca469197..cef0418432 100644 --- a/includes/classes/Feature/WooCommerce/WooCommerce.php +++ b/includes/classes/Feature/WooCommerce/WooCommerce.php @@ -386,8 +386,9 @@ public function translate_args( $query ) { // Search query if ( 'shop_order' === $post_type ) { $default_search_fields = array( 'post_title', 'post_content', 'post_excerpt' ); - if ( is_int( $s ) ) { - $default_search_fields[] = 'ID'; + if ( is_numeric( $s ) ) { + // The regular ID field only accepts numbers, while the ES _id will work fine with strings. + $default_search_fields[] = '_id'; } $search_fields = $query->get( 'search_fields', $default_search_fields ); From a38f32296a106b7e4c93a14ed2756c472d1fffad Mon Sep 17 00:00:00 2001 From: Felipe Elia Date: Mon, 24 Jan 2022 16:16:58 -0300 Subject: [PATCH 09/20] Revert back to use ID --- includes/classes/Feature/WooCommerce/WooCommerce.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/includes/classes/Feature/WooCommerce/WooCommerce.php b/includes/classes/Feature/WooCommerce/WooCommerce.php index cef0418432..5fca469197 100644 --- a/includes/classes/Feature/WooCommerce/WooCommerce.php +++ b/includes/classes/Feature/WooCommerce/WooCommerce.php @@ -386,9 +386,8 @@ public function translate_args( $query ) { // Search query if ( 'shop_order' === $post_type ) { $default_search_fields = array( 'post_title', 'post_content', 'post_excerpt' ); - if ( is_numeric( $s ) ) { - // The regular ID field only accepts numbers, while the ES _id will work fine with strings. - $default_search_fields[] = '_id'; + if ( is_int( $s ) ) { + $default_search_fields[] = 'ID'; } $search_fields = $query->get( 'search_fields', $default_search_fields ); From b74a336ffdd859b55975bd26bef08d83f2cf6876 Mon Sep 17 00:00:00 2001 From: Felipe Elia Date: Fri, 14 Jan 2022 14:59:55 -0300 Subject: [PATCH 10/20] Fix WooCommerce Orders search --- .../Feature/WooCommerce/WooCommerce.php | 33 +++---------------- 1 file changed, 5 insertions(+), 28 deletions(-) diff --git a/includes/classes/Feature/WooCommerce/WooCommerce.php b/includes/classes/Feature/WooCommerce/WooCommerce.php index d88281d9ec..c9a534b2e8 100644 --- a/includes/classes/Feature/WooCommerce/WooCommerce.php +++ b/includes/classes/Feature/WooCommerce/WooCommerce.php @@ -127,15 +127,6 @@ public function whitelist_meta_keys( $meta, $post ) { ); } - /** - * Prevent order fields search meta query - * - * @since 2.1 - */ - public function shop_order_search_fields() { - return []; - } - /** * Make sure all loop shop post ins are IDS. We have to pass post objects here since we override * the fields=>id query for the layered filter nav query @@ -394,7 +385,7 @@ public function translate_args( $query ) { // Search query if ( 'shop_order' === $post_type ) { - $search_fields = $query->get( 'search_fields', array( 'post_title', 'post_content', 'post_excerpt' ) ); + $search_fields = $query->get( 'search_fields', array( 'ID^999999', 'post_title', 'post_content', 'post_excerpt' ) ); $search_fields['meta'] = array_map( 'wc_clean', @@ -605,7 +596,7 @@ public function bypass_order_permissions_check( $override, $post_id ) { * @param \WP_Query $query Current query */ public function maybe_hook_woocommerce_search_fields( $query ) { - global $pagenow, $wp; + global $pagenow, $wp, $wc_list_table; if ( ! $this->should_integrate_with_query( $query ) ) { return; @@ -615,7 +606,7 @@ public function maybe_hook_woocommerce_search_fields( $query ) { return; } - add_filter( 'woocommerce_shop_order_search_fields', [ $this, 'shop_order_search_fields' ], 9999 ); + remove_action( 'parse_query', [ $wc_list_table, 'search_custom_fields' ] ); } /** @@ -640,22 +631,8 @@ public function search_order( $wp ) { } $search_key_safe = str_replace( array( 'Order #', '#' ), '', wc_clean( $_GET['s'] ) ); // phpcs:ignore WordPress.Security.NonceVerification - $order_id = absint( $search_key_safe ); - - /** - * Order ID 0 is not valid value. - */ - $order = $order_id > 0 ? wc_get_order( $order_id ) : false; - - // If the order doesn't exist, fallback to other fields - if ( ! $order ) { - unset( $wp->query_vars['post__in'] ); - $wp->query_vars['s'] = $search_key_safe; - } else { - // we found the order. don't query ES - unset( $wp->query_vars['s'] ); - $wp->query_vars['post__in'] = array( absint( $search_key_safe ) ); - } + unset( $wp->query_vars['post__in'] ); + $wp->query_vars['s'] = $search_key_safe; } /** From 0e65c853cc497ea3fa47b3a8f0529b57f78d0849 Mon Sep 17 00:00:00 2001 From: Felipe Elia Date: Fri, 14 Jan 2022 15:17:33 -0300 Subject: [PATCH 11/20] Add a new ep_woocommerce_shop_order_search_fields filter --- .../classes/Feature/WooCommerce/WooCommerce.php | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/includes/classes/Feature/WooCommerce/WooCommerce.php b/includes/classes/Feature/WooCommerce/WooCommerce.php index c9a534b2e8..bc1110bbeb 100644 --- a/includes/classes/Feature/WooCommerce/WooCommerce.php +++ b/includes/classes/Feature/WooCommerce/WooCommerce.php @@ -385,12 +385,12 @@ public function translate_args( $query ) { // Search query if ( 'shop_order' === $post_type ) { - $search_fields = $query->get( 'search_fields', array( 'ID^999999', 'post_title', 'post_content', 'post_excerpt' ) ); + $search_fields = $query->get( 'search_fields', array( 'ID', 'post_title', 'post_content', 'post_excerpt' ) ); $search_fields['meta'] = array_map( 'wc_clean', /** - * Filter shop order fields to search for WooCommerce + * Filter shop order meta fields to search for WooCommerce * * @hook shop_order_search_fields * @param {array} $fields Shop order fields @@ -424,7 +424,18 @@ public function translate_args( $query ) { ) ); - $query->set( 'search_fields', $search_fields ); + $query->set( + 'search_fields', + /** + * Filter all the shop order fields to search for WooCommerce + * + * @hook ep_woocommerce_shop_order_search_fields + * @since 4.0.0 + * @param {array} $fields Shop order fields + * @return {array} New fields + */ + apply_filters( 'ep_woocommerce_shop_order_search_fields', $search_fields ) + ); } elseif ( 'product' === $post_type && defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ) { $search_fields = $query->get( 'search_fields', array( 'post_title', 'post_content', 'post_excerpt' ) ); From 7634e5be2e3a9dda318192d3f61ea9c185ef3c79 Mon Sep 17 00:00:00 2001 From: Felipe Elia Date: Mon, 24 Jan 2022 12:02:58 -0300 Subject: [PATCH 12/20] Shop orders; Conditionally add ID to search field + docs --- .../classes/Feature/WooCommerce/WooCommerce.php | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/includes/classes/Feature/WooCommerce/WooCommerce.php b/includes/classes/Feature/WooCommerce/WooCommerce.php index bc1110bbeb..6bbf84c462 100644 --- a/includes/classes/Feature/WooCommerce/WooCommerce.php +++ b/includes/classes/Feature/WooCommerce/WooCommerce.php @@ -385,7 +385,11 @@ public function translate_args( $query ) { // Search query if ( 'shop_order' === $post_type ) { - $search_fields = $query->get( 'search_fields', array( 'ID', 'post_title', 'post_content', 'post_excerpt' ) ); + $default_search_fields = array( 'post_title', 'post_content', 'post_excerpt' ); + if ( is_int( $s ) ) { + $default_search_fields[] = 'ID'; + } + $search_fields = $query->get( 'search_fields', $default_search_fields ); $search_fields['meta'] = array_map( 'wc_clean', @@ -431,10 +435,11 @@ public function translate_args( $query ) { * * @hook ep_woocommerce_shop_order_search_fields * @since 4.0.0 - * @param {array} $fields Shop order fields - * @return {array} New fields + * @param {array} $fields Shop order fields + * @param {WP_Query} $query WP Query + * @return {array} New fields */ - apply_filters( 'ep_woocommerce_shop_order_search_fields', $search_fields ) + apply_filters( 'ep_woocommerce_shop_order_search_fields', $search_fields, $query ) ); } elseif ( 'product' === $post_type && defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ) { $search_fields = $query->get( 'search_fields', array( 'post_title', 'post_content', 'post_excerpt' ) ); From 4b565039d3ccdc8d75c43934a6b0ea743ef6ff61 Mon Sep 17 00:00:00 2001 From: Felipe Elia Date: Mon, 24 Jan 2022 12:03:26 -0300 Subject: [PATCH 13/20] Shop Orders: tests for searches with numbers --- tests/php/features/TestWooCommerce.php | 76 +++++++++++++++++++++++++- 1 file changed, 74 insertions(+), 2 deletions(-) diff --git a/tests/php/features/TestWooCommerce.php b/tests/php/features/TestWooCommerce.php index fe77d80703..c943e8f69b 100644 --- a/tests/php/features/TestWooCommerce.php +++ b/tests/php/features/TestWooCommerce.php @@ -140,8 +140,6 @@ public function testSearchOnShopOrderAdmin() { ElasticPress\Elasticsearch::factory()->refresh_indices(); - add_action( 'ep_wp_query_search', array( $this, 'action_wp_query_search' ), 10, 0 ); - $args = array( 's' => 'findme', 'post_type' => 'shop_order', @@ -154,6 +152,80 @@ public function testSearchOnShopOrderAdmin() { $this->assertEquals( 1, $query->found_posts ); } + /** + * Test search for shop orders by order ID + * + * @since 4.0.0 + * @group woocommerce + */ + public function testSearchShopOrderById() { + ElasticPress\Features::factory()->activate_feature( 'protected_content' ); + ElasticPress\Features::factory()->activate_feature( 'woocommerce' ); + ElasticPress\Features::factory()->setup_features(); + + $shop_order_id = Functions\create_and_sync_post( + array( + 'post_type' => 'shop_order', + ) + ); + + ElasticPress\Elasticsearch::factory()->refresh_indices(); + + $args = array( + 's' => $shop_order_id, + 'post_type' => 'shop_order', + ); + + $query = new \WP_Query( $args ); + + $this->assertTrue( $query->elasticsearch_success ); + $this->assertEquals( 1, $query->post_count ); + $this->assertEquals( 1, $query->found_posts ); + } + + /** + * Test search for shop orders matching field and ID. + * + * If searching for a number that is an order ID and part of another order's metadata, + * both should be returned. + * + * @since 4.0.0 + * @group woocommerce + */ + public function testSearchShopOrderByMetaFieldAndId() { + ElasticPress\Features::factory()->activate_feature( 'protected_content' ); + ElasticPress\Features::factory()->activate_feature( 'woocommerce' ); + ElasticPress\Features::factory()->setup_features(); + + $shop_order_id_1 = Functions\create_and_sync_post( + array( + 'post_type' => 'shop_order', + ) + ); + + Functions\create_and_sync_post( + array( + 'post_type' => 'shop_order', + ), + array( + '_billing_phone' => 'Phone number that matches an order ID: ' . $shop_order_id_1, + ) + ); + + ElasticPress\Elasticsearch::factory()->refresh_indices(); + + $args = array( + 's' => $shop_order_id_1, + 'post_type' => 'shop_order', + ); + + $query = new \WP_Query( $args ); + + $this->assertTrue( $query->elasticsearch_success ); + $this->assertEquals( 2, $query->post_count ); + $this->assertEquals( 2, $query->found_posts ); + } + /** * Test search integration is on in general for product searches * From d33c987e893084758afae41edc6b045d3821b5e4 Mon Sep 17 00:00:00 2001 From: Felipe Elia Date: Mon, 24 Jan 2022 14:34:10 -0300 Subject: [PATCH 14/20] [Temporary] Log the request and the query on errors --- tests/php/features/TestWooCommerce.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tests/php/features/TestWooCommerce.php b/tests/php/features/TestWooCommerce.php index c943e8f69b..ac5fd3d33a 100644 --- a/tests/php/features/TestWooCommerce.php +++ b/tests/php/features/TestWooCommerce.php @@ -178,6 +178,19 @@ public function testSearchShopOrderById() { $query = new \WP_Query( $args ); + // Log problems in the + add_action( + 'ep_invalid_response', + function( $request, $query ) { + echo '
';
+				print_r( $request );
+				echo '
'; + echo '
';
+				print_r( $query );
+				echo '
'; + } + ); + $this->assertTrue( $query->elasticsearch_success ); $this->assertEquals( 1, $query->post_count ); $this->assertEquals( 1, $query->found_posts ); From d944cac88b538a1cb32875ea3204f5f24a89e1cb Mon Sep 17 00:00:00 2001 From: Felipe Elia Date: Mon, 24 Jan 2022 14:40:54 -0300 Subject: [PATCH 15/20] Add params to log function --- tests/php/features/TestWooCommerce.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/php/features/TestWooCommerce.php b/tests/php/features/TestWooCommerce.php index ac5fd3d33a..b35fb39b20 100644 --- a/tests/php/features/TestWooCommerce.php +++ b/tests/php/features/TestWooCommerce.php @@ -188,7 +188,9 @@ function( $request, $query ) { echo '
';
 				print_r( $query );
 				echo '
'; - } + }, + 10, + 2 ); $this->assertTrue( $query->elasticsearch_success ); From c3e11d5f280f4d0035c2cb72033800aa40144e86 Mon Sep 17 00:00:00 2001 From: Felipe Elia Date: Mon, 24 Jan 2022 14:57:21 -0300 Subject: [PATCH 16/20] Fix place of logging function --- tests/php/features/TestWooCommerce.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/php/features/TestWooCommerce.php b/tests/php/features/TestWooCommerce.php index b35fb39b20..d4a59821e9 100644 --- a/tests/php/features/TestWooCommerce.php +++ b/tests/php/features/TestWooCommerce.php @@ -176,8 +176,6 @@ public function testSearchShopOrderById() { 'post_type' => 'shop_order', ); - $query = new \WP_Query( $args ); - // Log problems in the add_action( 'ep_invalid_response', @@ -193,6 +191,8 @@ function( $request, $query ) { 2 ); + $query = new \WP_Query( $args ); + $this->assertTrue( $query->elasticsearch_success ); $this->assertEquals( 1, $query->post_count ); $this->assertEquals( 1, $query->found_posts ); From 64d70d82abeff0ae707cdaa6f9a64aac72336a10 Mon Sep 17 00:00:00 2001 From: Felipe Elia Date: Mon, 24 Jan 2022 15:29:00 -0300 Subject: [PATCH 17/20] Use ES's _id instead of ID --- includes/classes/Feature/WooCommerce/WooCommerce.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/includes/classes/Feature/WooCommerce/WooCommerce.php b/includes/classes/Feature/WooCommerce/WooCommerce.php index 6bbf84c462..944a4fd1d7 100644 --- a/includes/classes/Feature/WooCommerce/WooCommerce.php +++ b/includes/classes/Feature/WooCommerce/WooCommerce.php @@ -386,8 +386,9 @@ public function translate_args( $query ) { // Search query if ( 'shop_order' === $post_type ) { $default_search_fields = array( 'post_title', 'post_content', 'post_excerpt' ); - if ( is_int( $s ) ) { - $default_search_fields[] = 'ID'; + if ( is_numeric( $s ) ) { + // The regular ID field only accepts numbers, while the ES _id will work fine with strings. + $default_search_fields[] = '_id'; } $search_fields = $query->get( 'search_fields', $default_search_fields ); From 71caf578b503f542064b50d238198eeb1492528f Mon Sep 17 00:00:00 2001 From: Felipe Elia Date: Mon, 24 Jan 2022 16:16:58 -0300 Subject: [PATCH 18/20] Revert back to use ID --- includes/classes/Feature/WooCommerce/WooCommerce.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/includes/classes/Feature/WooCommerce/WooCommerce.php b/includes/classes/Feature/WooCommerce/WooCommerce.php index 944a4fd1d7..6bbf84c462 100644 --- a/includes/classes/Feature/WooCommerce/WooCommerce.php +++ b/includes/classes/Feature/WooCommerce/WooCommerce.php @@ -386,9 +386,8 @@ public function translate_args( $query ) { // Search query if ( 'shop_order' === $post_type ) { $default_search_fields = array( 'post_title', 'post_content', 'post_excerpt' ); - if ( is_numeric( $s ) ) { - // The regular ID field only accepts numbers, while the ES _id will work fine with strings. - $default_search_fields[] = '_id'; + if ( is_int( $s ) ) { + $default_search_fields[] = 'ID'; } $search_fields = $query->get( 'search_fields', $default_search_fields ); From 6e2de08dbb985886b32f007461a38a52b6decbf4 Mon Sep 17 00:00:00 2001 From: Felipe Elia Date: Wed, 26 Jan 2022 12:56:42 -0300 Subject: [PATCH 19/20] Adjust query fuzziness when using fields with type `long` --- includes/classes/Indexable/Post/Post.php | 35 ++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/includes/classes/Indexable/Post/Post.php b/includes/classes/Indexable/Post/Post.php index d05be77865..92d79c43e9 100644 --- a/includes/classes/Indexable/Post/Post.php +++ b/includes/classes/Indexable/Post/Post.php @@ -1625,6 +1625,8 @@ function( $tax_query ) use ( $args ) { */ if ( ! empty( $args['s'] ) ) { + add_filter( 'ep_formatted_args_query', [ $this, 'adjust_query_fuzziness' ], 100, 4 ); + /** * Filter formatted Elasticsearch post query (only contains query part) * @@ -1896,6 +1898,39 @@ function( $tax_query ) use ( $args ) { return $formatted_args; } + /** + * Adjust the fuzziness parameter if needed. + * + * If using fields with type `long`, queries should not have a fuzziness parameter. + * + * @param array $query Current query + * @param array $query_vars Query variables + * @param string $search_text Search text + * @param array $search_fields Search fields + * @return array New query + */ + public function adjust_query_fuzziness( $query, $query_vars, $search_text, $search_fields ) { + if ( empty( array_intersect( $search_fields, [ 'ID', 'post_id', 'post_parent' ] ) ) ) { + return $query; + } + + if ( ! isset( $query['bool'] ) || ! isset( $query['bool']['should'] ) ) { + return $query; + } + + foreach ( $query['bool']['should'] as &$clause ) { + if ( ! isset( $clause['multi_match'] ) ) { + continue; + } + + if ( isset( $clause['multi_match']['fuzziness'] ) ) { + unset( $clause['multi_match']['fuzziness'] ); + } + } + + return $query; + } + /** * Parse and build out our tax query. * From 80030a20c4e48b25ad177374d97044e950c221f6 Mon Sep 17 00:00:00 2001 From: Felipe Elia Date: Wed, 26 Jan 2022 14:53:54 -0300 Subject: [PATCH 20/20] Remove logging --- tests/php/features/TestWooCommerce.php | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/tests/php/features/TestWooCommerce.php b/tests/php/features/TestWooCommerce.php index d4a59821e9..c943e8f69b 100644 --- a/tests/php/features/TestWooCommerce.php +++ b/tests/php/features/TestWooCommerce.php @@ -176,21 +176,6 @@ public function testSearchShopOrderById() { 'post_type' => 'shop_order', ); - // Log problems in the - add_action( - 'ep_invalid_response', - function( $request, $query ) { - echo '
';
-				print_r( $request );
-				echo '
'; - echo '
';
-				print_r( $query );
-				echo '
'; - }, - 10, - 2 - ); - $query = new \WP_Query( $args ); $this->assertTrue( $query->elasticsearch_success );