Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Thumbnails are not removed from indexed WooCommerce Products whe… #3267

Merged
merged 4 commits into from
Jan 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions includes/classes/Indexable/Post/SyncManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ public function action_queue_meta_sync( $meta_id, $object_id, $meta_key, $meta_v
'meta_key' => $meta_key,
'meta_value' => $meta_value,
'fields' => 'ids',
'post_type' => $indexable->get_indexable_post_types(),
]
);

Expand Down
67 changes: 67 additions & 0 deletions tests/php/indexables/TestPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -8228,4 +8228,71 @@ protected function setupDistinctMetaFieldKeysDbPerPostType() {
],
);
}

/**
* Tests that deleting a thumbnail updates the meta value of all the linked indexable posts
*
* @since 4.5.0
*/
public function testDeletingThumbnailUpdateRelatedIndexablePost() {
$product_id = $this->ep_factory->post->create( array(
'post_type' => 'product',
) );

$thumbnail_id = $this->factory->attachment->create_object( 'test.jpg', $product_id, array(
'post_mime_type' => 'image/jpeg',
'post_type' => 'attachment',
) );

set_post_thumbnail( $product_id, $thumbnail_id );

$thumbnail_id = get_post_thumbnail_id( $product_id );
$this->assertEquals( $thumbnail_id, get_post_meta( $product_id, '_thumbnail_id', true ) );

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

wp_delete_attachment( $thumbnail_id, true );
$this->assertEquals( '', get_post_meta( $product_id, '_thumbnail_id', true ) );

$ep_post = ElasticPress\Indexables::factory()->get( 'post' )->get( $product_id );
$this->assertArrayNotHasKey( '_thumbnail_id', $ep_post['meta'] );
}


/**
* Tests that deleting a thumbnail does not update the meta value of all the linked non-indexable posts
*
* @since 4.5.0
*/
public function testDeletingThumbnailShouldNotUpdateRelatedNonIndexablePost() {
$product_id = $this->ep_factory->post->create( array(
'post_type' => 'product',
) );

$thumbnail_id = $this->factory->attachment->create_object( 'test.jpg', $product_id, array(
'post_mime_type' => 'image/jpeg',
'post_type' => 'attachment',
) );

set_post_thumbnail( $product_id, $thumbnail_id );

$thumbnail_id = get_post_thumbnail_id( $product_id );
$this->assertEquals( $thumbnail_id, get_post_meta( $product_id, '_thumbnail_id', true ) );

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

// Remove product from indexable post types.
add_filter( 'ep_indexable_post_types', function( $post_types ) {
unset( $post_types['product'] );
return $post_types;
} );

wp_delete_attachment( $thumbnail_id, true );
$this->assertEquals( '', get_post_meta( $product_id, '_thumbnail_id', true ) );

$ep_post = ElasticPress\Indexables::factory()->get( 'post' )->get( $product_id );
$this->assertArrayHasKey( '_thumbnail_id', $ep_post['meta'] );
}
}