Skip to content

Commit

Permalink
Merge pull request #3145 from 10up/fix/parent-term-sync-issue-3105
Browse files Browse the repository at this point in the history
Auto sync parent terms
  • Loading branch information
felipeelia authored Jan 20, 2023
2 parents 9404270 + 0abe68e commit 1fe570b
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 3 deletions.
34 changes: 31 additions & 3 deletions includes/classes/Indexable/Post/SyncManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -397,9 +397,29 @@ public function maybe_display_notice_edit_single_term( $notices ) {
}

if ( IndexHelper::factory()->get_index_default_per_page() >= $tag->count ) {

$child_tags = get_term_children( $tag->term_id, $tag->taxonomy );
if ( empty( $child_tags ) ) {
return $notices;
}
foreach ( $child_tags as $child_tag_id ) {
$child_tag = get_term( $child_tag_id );
if ( ! is_wp_error( $child_tag ) && IndexHelper::factory()->get_index_default_per_page() < $child_tag->count && ! isset( $notices['edited_single_parent_term'] ) ) {
$notices['edited_single_parent_term'] = [
'html' => sprintf(
/* translators: Sync Page URL */
__( 'Due to the number of posts associated with its child terms, you will need to <a href="%s">resync</a> after editing or deleting it.', 'elasticpress' ),
Utils\get_sync_url()
),
'type' => 'warning',
'dismiss' => true,
];
break;
}
}

return $notices;
}

$notices['edited_single_term'] = [
'html' => sprintf(
/* translators: Sync Page URL */
Expand Down Expand Up @@ -555,8 +575,16 @@ public function action_edited_term( $term_id, $tt_id, $taxonomy ) {
}

// Find ID of all attached posts (query lifted from wp_delete_term())
$object_ids = (array) $wpdb->get_col( $wpdb->prepare( "SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $tt_id ) );

$object_ids = (array) $wpdb->get_col( $wpdb->prepare( "SELECT object_id FROM {$wpdb->term_relationships} WHERE term_taxonomy_id = %d", $tt_id ) );

// If the current term is not attached, check if the child terms are attached to the post
if ( empty( $object_ids ) ) {
$child_terms = get_term_children( $term_id, $taxonomy );
if ( ! empty( $child_terms ) ) {
$in_id = join( ',', array_fill( 0, count( $child_terms ), '%d' ) );
$object_ids = (array) $wpdb->get_col( $wpdb->prepare( "SELECT object_id FROM {$wpdb->term_relationships} WHERE term_taxonomy_id IN ( {$in_id} )", $child_terms ) ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare
}
}
if ( ! count( $object_ids ) ) {
return;
}
Expand Down
42 changes: 42 additions & 0 deletions tests/php/indexables/TestPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -7378,6 +7378,48 @@ public function testPostEditedTerm() {
$this->assertEquals( 'Different Tag Name', $document['terms']['post_tag'][0]['name'] );
}

/**
* Tests parent term edition when child term is attached to post
*
* @return void
* @group post
*/
public function testParentEditedTerm() {
$post = $this->ep_factory->post->create_and_get();

$tax_name = rand_str( 32 );
register_taxonomy( $tax_name, $post->post_type, array( 'label' => $tax_name ) );
register_taxonomy_for_object_type( $tax_name, $post->post_type );

$term_1_name = rand_str( 32 );
$term_1 = wp_insert_term( $term_1_name, $tax_name );

$term_2_name = rand_str( 32 );
$term_2 = wp_insert_term( $term_2_name, $tax_name, array( 'parent' => $term_1['term_id'] ) );

wp_set_object_terms( $post->ID, array( $term_2['term_id'] ), $tax_name, true );

ElasticPress\Elasticsearch::factory()->refresh_indices();

$test_tag = get_term_by( 'id', $term_1['term_id'], $tax_name );

wp_update_term(
$test_tag->term_id,
$tax_name,
[
'slug' => 'parent-term',
'name' => 'Parent Term',
]
);

ElasticPress\Indexables::factory()->get( 'post' )->sync_manager->index_sync_queue();
ElasticPress\Elasticsearch::factory()->refresh_indices();

$document = ElasticPress\Indexables::factory()->get( 'post' )->get( $post->ID );
$this->assertEquals( 'parent-term', $document['terms'][ $tax_name ][1]['slug'] );
$this->assertEquals( 'Parent Term', $document['terms'][ $tax_name ][1]['name'] );
}

/**
* Tests post without meta value.
*
Expand Down

0 comments on commit 1fe570b

Please sign in to comment.