diff --git a/includes/classes/Indexable/Post/SyncManager.php b/includes/classes/Indexable/Post/SyncManager.php index 1b55a834bc..96024359bb 100644 --- a/includes/classes/Indexable/Post/SyncManager.php +++ b/includes/classes/Indexable/Post/SyncManager.php @@ -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(), ] ); diff --git a/tests/php/indexables/TestPost.php b/tests/php/indexables/TestPost.php index 235be95285..a51eab134a 100644 --- a/tests/php/indexables/TestPost.php +++ b/tests/php/indexables/TestPost.php @@ -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'] ); + } }