From fd8d6cddcd2386aeabd4355731fcac964e5dfea0 Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Wed, 12 Feb 2014 04:48:14 +0000 Subject: [PATCH] Don't iterate over `$wp_query->posts` in `update_post_thumbnail_cache()` if it is empty. Adds unit tests. Props SergeyBiryukov, for the original patch. Fixes #26321. git-svn-id: https://develop.svn.wordpress.org/trunk@27166 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/post-thumbnail-template.php | 4 +-- tests/phpunit/tests/post/thumbnails.php | 30 +++++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 tests/phpunit/tests/post/thumbnails.php diff --git a/src/wp-includes/post-thumbnail-template.php b/src/wp-includes/post-thumbnail-template.php index 91201e7994..64c8494ca6 100644 --- a/src/wp-includes/post-thumbnail-template.php +++ b/src/wp-includes/post-thumbnail-template.php @@ -57,7 +57,7 @@ function update_post_thumbnail_cache( $wp_query = null ) { if ( ! $wp_query ) $wp_query = $GLOBALS['wp_query']; - if ( $wp_query->thumbnails_cached ) + if ( $wp_query->thumbnails_cached || ! $wp_query->posts ) return; $thumb_ids = array(); @@ -69,7 +69,7 @@ function update_post_thumbnail_cache( $wp_query = null ) { if ( ! empty ( $thumb_ids ) ) { _prime_post_caches( $thumb_ids, false, true ); } - + $wp_query->thumbnails_cached = true; } diff --git a/tests/phpunit/tests/post/thumbnails.php b/tests/phpunit/tests/post/thumbnails.php new file mode 100644 index 0000000000..2584bbad2d --- /dev/null +++ b/tests/phpunit/tests/post/thumbnails.php @@ -0,0 +1,30 @@ +assertFalse( $GLOBALS['wp_query']->thumbnails_cached ); + + $this->factory->post->create_many( 3 ); + $GLOBALS['wp_query'] = new WP_Query( array( 'post_type' => 'post' ) ); + + update_post_thumbnail_cache(); + + $this->assertTrue( $GLOBALS['wp_query']->thumbnails_cached ); + + $q = new WP_Query(); + update_post_thumbnail_cache( $q ); + $this->assertFalse( $q->thumbnails_cached ); + + $p = new WP_Query( array( 'post_type' => 'post' ) ); + update_post_thumbnail_cache( $p ); + $this->assertTrue( $p->thumbnails_cached ); + } +}