mirrored from git://develop.git.wordpress.org/
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add cache invalidation for header images
Introduce `invalidate_header_images_cache` function to clear cached header images when custom header meta is updated or deleted. Add corresponding unit tests to ensure cache invalidation logic works correctly.
- Loading branch information
Paul Bearne
committed
Sep 12, 2024
1 parent
dfc0e2a
commit b859689
Showing
3 changed files
with
137 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
tests/phpunit/tests/theme/invalidateHeaderImagesCache.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
<?php | ||
/** | ||
* @group themes | ||
* | ||
* @coversDefaultClass ::invalidate_header_images_cache | ||
*/ | ||
class Tests_Theme_invalidateHeaderImagesCache extends WP_UnitTestCase { | ||
public $custom_image_header; | ||
|
||
public function set_up() { | ||
parent::set_up(); | ||
$this->custom_image_header = new Custom_Image_Header( '__return_null' ); | ||
} | ||
|
||
/** | ||
* @ticket 49446 | ||
*/ | ||
public function test_invalidate_header_images_cache_meta_updated() { | ||
$id = wp_insert_attachment( | ||
array( | ||
'post_status' => 'publish', | ||
'post_title' => 'foo.png', | ||
'post_type' => 'post', | ||
'guid' => 'http://localhost/foo.png', | ||
) | ||
); | ||
|
||
// Create initial crop object. | ||
$cropped_1 = 'foo-cropped-1.png'; | ||
$object = wp_copy_parent_attachment_properties( $cropped_1, $id, 'custom-header' ); | ||
|
||
// Ensure no previous crop exists. | ||
$previous = $this->custom_image_header->get_previous_crop( $object ); | ||
$this->assertFalse( $previous ); | ||
|
||
// Create the initial crop attachment and set it as the header. | ||
$cropped_1_id = $this->custom_image_header->insert_attachment( $object, $cropped_1 ); | ||
$key = '_wp_attachment_custom_header_last_used_' . get_stylesheet(); | ||
update_post_meta( $cropped_1_id, $key, time() ); | ||
update_post_meta( $cropped_1_id, '_wp_attachment_is_custom_header', get_stylesheet() ); | ||
|
||
$expected = array( | ||
$cropped_1_id => array( | ||
'attachment_id' => $cropped_1_id, | ||
'url' => 'http://example.org/wp-content/uploads/foo-cropped-1.png', | ||
'thumbnail_url' => 'http://example.org/wp-content/uploads/foo-cropped-1.png', | ||
'alt_text' => '', | ||
'attachment_parent' => $id, | ||
), | ||
); | ||
|
||
$num_queries = get_num_queries() + 4; | ||
|
||
$this->assertSame( $expected, get_uploaded_header_images() ); | ||
|
||
$this->assertSame( $num_queries, get_num_queries() ); | ||
update_post_meta( $cropped_1_id, '_wp_attachment_is_custom_header', 'updated' ); | ||
|
||
$stylesheet = get_option( 'stylesheet' ); | ||
$transient_key = 'uploaded_header_images' . $stylesheet; | ||
$this->assertFalse( get_transient( $transient_key ), 'cache cleared' ); | ||
} | ||
|
||
/** | ||
* @ticket 49446 | ||
*/ | ||
public function test_invalidate_header_images_cache_meta_deleted() { | ||
$id = wp_insert_attachment( | ||
array( | ||
'post_status' => 'publish', | ||
'post_title' => 'foo.png', | ||
'post_type' => 'post', | ||
'guid' => 'http://localhost/foo.png', | ||
) | ||
); | ||
|
||
// Create initial crop object. | ||
$cropped_1 = 'foo-cropped-1.png'; | ||
$object = wp_copy_parent_attachment_properties( $cropped_1, $id, 'custom-header' ); | ||
|
||
// Ensure no previous crop exists. | ||
$previous = $this->custom_image_header->get_previous_crop( $object ); | ||
$this->assertFalse( $previous ); | ||
|
||
// Create the initial crop attachment and set it as the header. | ||
$cropped_1_id = $this->custom_image_header->insert_attachment( $object, $cropped_1 ); | ||
$key = '_wp_attachment_custom_header_last_used_' . get_stylesheet(); | ||
update_post_meta( $cropped_1_id, $key, time() ); | ||
update_post_meta( $cropped_1_id, '_wp_attachment_is_custom_header', get_stylesheet() ); | ||
|
||
$expected = array( | ||
$cropped_1_id => array( | ||
'attachment_id' => $cropped_1_id, | ||
'url' => 'http://example.org/wp-content/uploads/foo-cropped-1.png', | ||
'thumbnail_url' => 'http://example.org/wp-content/uploads/foo-cropped-1.png', | ||
'alt_text' => '', | ||
'attachment_parent' => $id, | ||
), | ||
); | ||
|
||
$num_queries = get_num_queries() + 4; | ||
|
||
$this->assertSame( $expected, get_uploaded_header_images() ); | ||
|
||
$this->assertSame( $num_queries, get_num_queries() ); | ||
delete_post_meta( $cropped_1_id, '_wp_attachment_is_custom_header' ); | ||
|
||
$stylesheet = get_option( 'stylesheet' ); | ||
$transient_key = 'uploaded_header_images' . $stylesheet; | ||
$this->assertFalse( get_transient( $transient_key ), 'cache cleared' ); | ||
} | ||
} |