Skip to content

Commit

Permalink
Merge pull request #2366 from 10up/fix/issue-2365
Browse files Browse the repository at this point in the history
Fix Term deletion
  • Loading branch information
felipeelia authored Sep 25, 2021
2 parents 28b2720 + be10395 commit 4dfceb4
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 5 deletions.
16 changes: 15 additions & 1 deletion includes/classes/Indexable/Term/QueryIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,21 @@ public function maybe_filter_query( $results, WP_Term_Query $query ) {
return $results;
}

$query->found_terms = $ep_query['found_documents'];
/**
* Elasticsearch 5 will return found_documents as a number,
* ES 7+ will return it as an object with `value`. If any of that is found,
* fallback to a simple count of returned documents.
*
* @since 3.6.3
*/
if ( is_integer( $ep_query['found_documents'] ) ) {
$query->found_terms = $ep_query['found_documents'];
} elseif ( is_array( $ep_query['found_documents'] ) && isset( $ep_query['found_documents']['value'] ) ) {
$query->found_terms = $ep_query['found_documents']['value'];
} else {
$query->found_terms = count( $ep_query['documents'] );
}

$query->elasticsearch_success = true;

// Determine how we should format the results from ES based on the fields parameter.
Expand Down
2 changes: 1 addition & 1 deletion includes/classes/Indexable/Term/SyncManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function setup() {
add_action( 'deleted_term_meta', [ $this, 'action_queue_meta_sync' ], 10, 2 );
add_action( 'updated_term_meta', [ $this, 'action_queue_meta_sync' ], 10, 2 );
add_action( 'pre_delete_term', [ $this, 'action_queue_children_sync' ] );
add_action( 'delete_term', [ $this, 'action_sync_on_delete' ] );
add_action( 'pre_delete_term', [ $this, 'action_sync_on_delete' ] );
add_action( 'set_object_terms', [ $this, 'action_sync_on_object_update' ], 10, 2 );
}

Expand Down
49 changes: 46 additions & 3 deletions tests/php/indexables/TestTerm.php
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,7 @@ public function testTermQueryOrderParent() {
public function testTermQueryParent() {
$parent_term_id = Functions\create_and_sync_term( 'parent-term-no-post', 'Parent Category', 'Parent/Child Terms', 'category' );
$child_term_id = Functions\create_and_sync_term( 'child-term-post', 'Child Category', 'Parent/Child Terms', 'category', [], $parent_term_id );
ElasticPress\Elasticsearch::factory()->refresh_indices();

$term_query = new \WP_Term_Query(
[
Expand All @@ -689,7 +690,7 @@ public function testTermQueryParent() {
);

$this->assertTrue( $term_query->elasticsearch_success );
$this->assertCount( 2, $term_query->terms ); // "Uncategorized" is also returned in this case.
$this->assertCount( 1, $term_query->terms );
$this->assertContains( $parent_term_id, wp_list_pluck( $term_query->terms, 'term_id' ) );
}

Expand All @@ -711,9 +712,12 @@ public function testTermQueryHideEmpty() {
]
);

$term = wp_insert_term( 'term name', 'post_tag' );
$term_id = Functions\create_and_sync_term( 'term-name', 'term name', '', 'post_tag' );

wp_set_object_terms( $post, $term['term_id'], 'post_tag', true );
wp_set_object_terms( $post, $term_id, 'post_tag', true );

ElasticPress\Indexables::factory()->get( 'term' )->index( $term_id, true );
ElasticPress\Elasticsearch::factory()->refresh_indices();

$term_query = new \WP_Term_Query(
[
Expand Down Expand Up @@ -1442,4 +1446,43 @@ public function testPutMapping() {

remove_filter( 'ep_elasticsearch_version', '__return_false' );
}

/**
* Test term deletion.
*
* @since 3.6.3
* @group term
*/
public function testDeleteTerm() {
$term_id = Functions\create_and_sync_term( 'test-delete-term', 'Test Delete Term', 'Test', 'category' );
ElasticPress\Elasticsearch::factory()->refresh_indices();

$term_query = new \WP_Term_Query(
[
'hide_empty' => false,
'taxonomy' => 'category',
'include' => [ $term_id ],
'ep_integrate' => true,
]
);

$this->assertTrue( $term_query->elasticsearch_success );
$this->assertEquals( 1, $term_query->found_terms );
$this->assertEquals( $term_id, $term_query->terms[0]->term_id );

wp_delete_term( $term_id, 'category' );
ElasticPress\Elasticsearch::factory()->refresh_indices();

$term_query = new \WP_Term_Query(
[
'hide_empty' => false,
'taxonomy' => 'category',
'include' => [ $term_id ],
'ep_integrate' => true,
]
);

$this->assertTrue( $term_query->elasticsearch_success );
$this->assertEquals( 0, $term_query->found_terms );
}
}

0 comments on commit 4dfceb4

Please sign in to comment.