From 936d6454c46a5a41b6dc55859855580765ffcbf0 Mon Sep 17 00:00:00 2001 From: Oscar Sanchez Date: Mon, 6 Sep 2021 17:27:52 -0500 Subject: [PATCH 01/11] Exclude default taxonomies from WP_Query root tax check --- includes/classes/Indexable/Post/Post.php | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/includes/classes/Indexable/Post/Post.php b/includes/classes/Indexable/Post/Post.php index d1463c81a1..9ff219fd47 100644 --- a/includes/classes/Indexable/Post/Post.php +++ b/includes/classes/Indexable/Post/Post.php @@ -1000,8 +1000,14 @@ function( $tax_query ) use ( $args ) { $taxonomies = get_taxonomies( array(), 'objects' ); foreach ( $taxonomies as $tax_slug => $tax ) { - // Exclude the category taxonomy from this check if we are performing a Tax Query as category_name will be set by core - if ( $tax->query_var && ! empty( $args[ $tax->query_var ] ) && 'category' !== $tax->name ) { + // Prevents duplication of core's default taxonomies post_tag and category in ES query. + apply_filters( 'ep_post_tax_excluded_wp_query_root_check', + $excluded_tax_from_root_check = [ + 'category', + 'post_tag' + ] ); + + if ( $tax->query_var && ! empty( $args[ $tax->query_var ] ) && ! in_array( $tax->name, $excluded_tax_from_root_check ) ) { $args['tax_query'][] = array( 'taxonomy' => $tax_slug, 'terms' => (array) $args[ $tax->query_var ], From 8d9ab857eb390c2c00197401f2e9534da73f5c14 Mon Sep 17 00:00:00 2001 From: Oscar Sanchez Date: Mon, 6 Sep 2021 18:06:47 -0500 Subject: [PATCH 02/11] Explode comma separated tag slug query for proper ES formatting --- includes/classes/Indexable/Post/Post.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/includes/classes/Indexable/Post/Post.php b/includes/classes/Indexable/Post/Post.php index 9ff219fd47..ba51b2a3e7 100644 --- a/includes/classes/Indexable/Post/Post.php +++ b/includes/classes/Indexable/Post/Post.php @@ -938,9 +938,12 @@ public function format_args( $args, $wp_query ) { } if ( isset( $args['tag'] ) && ! empty( $args['tag'] ) ) { + if ( ! is_array( $args['tag'] ) && strpos( $args['tag'], ',' ) ) { + $args['tag'] = explode( ',', $args['tag'] ); + } $args['tax_query'][] = array( 'taxonomy' => 'post_tag', - 'terms' => array( $args['tag'] ), + 'terms' => (array) $args['tag'], 'field' => 'slug', ); } From cbdf17ddd3a8ab66925574b080fae7ef2a4896af Mon Sep 17 00:00:00 2001 From: Oscar Sanchez Date: Tue, 7 Sep 2021 00:03:13 -0500 Subject: [PATCH 03/11] Add tag slug query unit test --- tests/php/indexables/TestPost.php | 38 +++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/tests/php/indexables/TestPost.php b/tests/php/indexables/TestPost.php index 154172dbfe..6a6d0a2745 100644 --- a/tests/php/indexables/TestPost.php +++ b/tests/php/indexables/TestPost.php @@ -4975,6 +4975,44 @@ public function testNestedTaxQuery() { $this->assertEquals( 2, count( $query->posts ) ); } + /** + * Test a tag query by slug using array and comma separated string as arguments. + * + * @group post + */ + public function testTagSlugQuery() { + $post_id_1 = Functions\create_and_sync_post( + array( + 'post_content' => 'findme test 1', + 'tags_input' => array( 'slug1', 'slug2' ), + ) + ); + $post_id_2 = Functions\create_and_sync_post( + array( + 'post_content' => 'findme test 2', + 'tags_input' => array( 'slug1', 'slug2', 'slug3', 'slug4' ), + ) + ); + + $query1_args = [ + 's' => 'findme', + 'tag' => 'slug1,slug2', + ]; + + $query2_args = [ + 's' => 'findme', + 'tag' => ['slug1 , slug2'], + ]; + + $query1 = new \WP_Query( $query1_args ); + $query2 = new \WP_Query( $query2_args ); + + $this->assertEquals( 1, $query1->post_count ); + $this->assertEquals( 1, $query1->found_posts ); + $this->assertEquals( 1, $query2->post_count ); + $this->assertEquals( 1, $query2->found_posts ); + } + /** * Test a query with tag__and and tag_id params * From c69ecacd59015e86ece4ec6eb3d51096fad67217 Mon Sep 17 00:00:00 2001 From: Oscar Sanchez Date: Mon, 13 Sep 2021 18:17:58 -0500 Subject: [PATCH 04/11] Tests: Refresh indices after post creation --- includes/classes/Indexable/Post/Post.php | 14 ++++++++------ tests/php/indexables/TestPost.php | 10 ++++++---- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/includes/classes/Indexable/Post/Post.php b/includes/classes/Indexable/Post/Post.php index ba51b2a3e7..e5bd78084f 100644 --- a/includes/classes/Indexable/Post/Post.php +++ b/includes/classes/Indexable/Post/Post.php @@ -1004,13 +1004,15 @@ function( $tax_query ) use ( $args ) { foreach ( $taxonomies as $tax_slug => $tax ) { // Prevents duplication of core's default taxonomies post_tag and category in ES query. - apply_filters( 'ep_post_tax_excluded_wp_query_root_check', - $excluded_tax_from_root_check = [ - 'category', - 'post_tag' - ] ); + apply_filters( + 'ep_post_tax_excluded_wp_query_root_check', + $excluded_tax_from_root_check = [ + 'category', + 'post_tag', + ] + ); - if ( $tax->query_var && ! empty( $args[ $tax->query_var ] ) && ! in_array( $tax->name, $excluded_tax_from_root_check ) ) { + if ( $tax->query_var && ! empty( $args[ $tax->query_var ] ) && ! in_array( $tax->name, $excluded_tax_from_root_check, true ) ) { $args['tax_query'][] = array( 'taxonomy' => $tax_slug, 'terms' => (array) $args[ $tax->query_var ], diff --git a/tests/php/indexables/TestPost.php b/tests/php/indexables/TestPost.php index 6a6d0a2745..a0f048cc6c 100644 --- a/tests/php/indexables/TestPost.php +++ b/tests/php/indexables/TestPost.php @@ -4994,6 +4994,8 @@ public function testTagSlugQuery() { ) ); + ElasticPress\Elasticsearch::factory()->refresh_indices(); + $query1_args = [ 's' => 'findme', 'tag' => 'slug1,slug2', @@ -5007,10 +5009,10 @@ public function testTagSlugQuery() { $query1 = new \WP_Query( $query1_args ); $query2 = new \WP_Query( $query2_args ); - $this->assertEquals( 1, $query1->post_count ); - $this->assertEquals( 1, $query1->found_posts ); - $this->assertEquals( 1, $query2->post_count ); - $this->assertEquals( 1, $query2->found_posts ); + $this->assertEquals( 2, $query1->post_count ); + $this->assertEquals( 2, $query1->found_posts ); + $this->assertEquals( 2, $query2->post_count ); + $this->assertEquals( 2, $query2->found_posts ); } /** From ebb370b27820c9d685b787a4f66d93959d729682 Mon Sep 17 00:00:00 2001 From: Oscar Sanchez Date: Mon, 13 Sep 2021 18:42:44 -0500 Subject: [PATCH 05/11] Fix unit test --- tests/php/indexables/TestPost.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/php/indexables/TestPost.php b/tests/php/indexables/TestPost.php index a0f048cc6c..fef7cdbf3b 100644 --- a/tests/php/indexables/TestPost.php +++ b/tests/php/indexables/TestPost.php @@ -5003,7 +5003,7 @@ public function testTagSlugQuery() { $query2_args = [ 's' => 'findme', - 'tag' => ['slug1 , slug2'], + 'tag' => ['slug1', 'slug2'], ]; $query1 = new \WP_Query( $query1_args ); From 2a87ddeba33187dfd774e353e17971e1753b098d Mon Sep 17 00:00:00 2001 From: Oscar Sanchez Date: Mon, 13 Sep 2021 19:07:13 -0500 Subject: [PATCH 06/11] Fix linting issues --- includes/classes/Indexable/Post/Post.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/includes/classes/Indexable/Post/Post.php b/includes/classes/Indexable/Post/Post.php index e5bd78084f..1f63ef0f97 100644 --- a/includes/classes/Indexable/Post/Post.php +++ b/includes/classes/Indexable/Post/Post.php @@ -1005,11 +1005,11 @@ function( $tax_query ) use ( $args ) { foreach ( $taxonomies as $tax_slug => $tax ) { // Prevents duplication of core's default taxonomies post_tag and category in ES query. apply_filters( - 'ep_post_tax_excluded_wp_query_root_check', - $excluded_tax_from_root_check = [ + 'ep_post_tax_excluded_wp_query_root_check', + $excluded_tax_from_root_check = [ 'category', 'post_tag', - ] + ] ); if ( $tax->query_var && ! empty( $args[ $tax->query_var ] ) && ! in_array( $tax->name, $excluded_tax_from_root_check, true ) ) { From bbf5f9b114ad9fd10ea66f0df57576fc2740bd80 Mon Sep 17 00:00:00 2001 From: Oscar Sanchez Date: Tue, 21 Sep 2021 00:21:09 -0500 Subject: [PATCH 07/11] Add elasticsearch powered search check --- tests/php/indexables/TestPost.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/php/indexables/TestPost.php b/tests/php/indexables/TestPost.php index fef7cdbf3b..9000001d48 100644 --- a/tests/php/indexables/TestPost.php +++ b/tests/php/indexables/TestPost.php @@ -5003,12 +5003,15 @@ public function testTagSlugQuery() { $query2_args = [ 's' => 'findme', - 'tag' => ['slug1', 'slug2'], + 'tag' => [ 'slug1', 'slug2' ], ]; $query1 = new \WP_Query( $query1_args ); $query2 = new \WP_Query( $query2_args ); + $this->assertTrue( isset( $query1->posts[0]->elasticsearch ) ); + $this->assertTrue( isset( $query2->posts[0]->elasticsearch ) ); + $this->assertEquals( 2, $query1->post_count ); $this->assertEquals( 2, $query1->found_posts ); $this->assertEquals( 2, $query2->post_count ); From 1d71d8a622aef3eec3c580ace6ae69cbe46785e0 Mon Sep 17 00:00:00 2001 From: Oscar Sanchez Date: Tue, 21 Sep 2021 00:26:38 -0500 Subject: [PATCH 08/11] Evaluate strpos more strictly --- includes/classes/Indexable/Post/Post.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/classes/Indexable/Post/Post.php b/includes/classes/Indexable/Post/Post.php index 1f63ef0f97..b052acdb2f 100644 --- a/includes/classes/Indexable/Post/Post.php +++ b/includes/classes/Indexable/Post/Post.php @@ -938,7 +938,7 @@ public function format_args( $args, $wp_query ) { } if ( isset( $args['tag'] ) && ! empty( $args['tag'] ) ) { - if ( ! is_array( $args['tag'] ) && strpos( $args['tag'], ',' ) ) { + if ( ! is_array( $args['tag'] ) && false !== strpos( $args['tag'], ',' ) ) { $args['tag'] = explode( ',', $args['tag'] ); } $args['tax_query'][] = array( From 9546e1e9e215fa6af1211932c9c3e0e4c39d3bb1 Mon Sep 17 00:00:00 2001 From: Oscar Sanchez Date: Wed, 22 Sep 2021 23:40:21 -0500 Subject: [PATCH 09/11] Move filter outside of foreach --- includes/classes/Indexable/Post/Post.php | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/includes/classes/Indexable/Post/Post.php b/includes/classes/Indexable/Post/Post.php index b052acdb2f..26051b14ae 100644 --- a/includes/classes/Indexable/Post/Post.php +++ b/includes/classes/Indexable/Post/Post.php @@ -1002,15 +1002,16 @@ function( $tax_query ) use ( $args ) { */ $taxonomies = get_taxonomies( array(), 'objects' ); + // Prevents duplication of core's default taxonomies post_tag and category in ES query. + $excluded_tax_from_root_check = apply_filters( + 'ep_post_tax_excluded_wp_query_root_check', + [ + 'category', + 'post_tag', + ] + ); + foreach ( $taxonomies as $tax_slug => $tax ) { - // Prevents duplication of core's default taxonomies post_tag and category in ES query. - apply_filters( - 'ep_post_tax_excluded_wp_query_root_check', - $excluded_tax_from_root_check = [ - 'category', - 'post_tag', - ] - ); if ( $tax->query_var && ! empty( $args[ $tax->query_var ] ) && ! in_array( $tax->name, $excluded_tax_from_root_check, true ) ) { $args['tax_query'][] = array( From 1191f01b0d3b3a2e9e7f9bfd4bec237e350129ac Mon Sep 17 00:00:00 2001 From: Oscar Sanchez Date: Thu, 23 Sep 2021 11:11:50 -0500 Subject: [PATCH 10/11] Add filter documentation --- includes/classes/Indexable/Post/Post.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/includes/classes/Indexable/Post/Post.php b/includes/classes/Indexable/Post/Post.php index 7a811d9bcc..0a92f3697a 100644 --- a/includes/classes/Indexable/Post/Post.php +++ b/includes/classes/Indexable/Post/Post.php @@ -951,7 +951,13 @@ function( $tax_query ) use ( $args ) { */ $taxonomies = get_taxonomies( array(), 'objects' ); - // Prevents duplication of core's default taxonomies post_tag and category in ES query. + /** + * Filter taxonomies to exclude from tax root check. + * Default values prevent duplication of core's default taxonomies post_tag and category in ES query. + * + * @hook ep_post_tax_excluded_wp_query_root_check + * @param {array} $taxonomies Taxonomies + */ $excluded_tax_from_root_check = apply_filters( 'ep_post_tax_excluded_wp_query_root_check', [ From 5f45eb3a15eaf029ab90c093c2a3c6351b1dfa68 Mon Sep 17 00:00:00 2001 From: Oscar Sanchez Date: Thu, 23 Sep 2021 18:05:33 -0500 Subject: [PATCH 11/11] Add since tag to DockBlock --- includes/classes/Indexable/Post/Post.php | 1 + 1 file changed, 1 insertion(+) diff --git a/includes/classes/Indexable/Post/Post.php b/includes/classes/Indexable/Post/Post.php index 0a92f3697a..6a847ac872 100644 --- a/includes/classes/Indexable/Post/Post.php +++ b/includes/classes/Indexable/Post/Post.php @@ -955,6 +955,7 @@ function( $tax_query ) use ( $args ) { * Filter taxonomies to exclude from tax root check. * Default values prevent duplication of core's default taxonomies post_tag and category in ES query. * + * @since 3.6.3 * @hook ep_post_tax_excluded_wp_query_root_check * @param {array} $taxonomies Taxonomies */