diff --git a/packages/block-library/src/latest-posts/index.php b/packages/block-library/src/latest-posts/index.php index 05a9d634379af..cbeafb6677a13 100644 --- a/packages/block-library/src/latest-posts/index.php +++ b/packages/block-library/src/latest-posts/index.php @@ -37,11 +37,12 @@ function render_block_core_latest_posts( $attributes ) { global $post, $block_core_latest_posts_excerpt_length; $args = array( - 'posts_per_page' => $attributes['postsToShow'], - 'post_status' => 'publish', - 'order' => $attributes['order'], - 'orderby' => $attributes['orderBy'], - 'suppress_filters' => false, + 'posts_per_page' => $attributes['postsToShow'], + 'post_status' => 'publish', + 'order' => $attributes['order'], + 'orderby' => $attributes['orderBy'], + 'ignore_sticky_posts' => true, + 'no_found_rows' => true, ); $block_core_latest_posts_excerpt_length = $attributes['excerptLength']; @@ -54,7 +55,12 @@ function render_block_core_latest_posts( $attributes ) { $args['author'] = $attributes['selectedAuthor']; } - $recent_posts = get_posts( $args ); + $query = new WP_Query; + $recent_posts = $query->query( $args ); + + if ( isset( $attributes['displayFeaturedImage'] ) && $attributes['displayFeaturedImage'] ) { + update_post_thumbnail_cache( $query ); + } $list_items_markup = ''; diff --git a/phpunit/blocks/render-last-posts-test.php b/phpunit/blocks/render-last-posts-test.php new file mode 100644 index 0000000000000..49734adb515fd --- /dev/null +++ b/phpunit/blocks/render-last-posts-test.php @@ -0,0 +1,90 @@ +post->create_and_get( + array( + 'post_title' => 'Sticky post', + 'post_date' => '2008-09-03 00:00:00', + ) + ); + stick_post( self::$sticky_post->ID ); + + $file = DIR_TESTDATA . '/images/canola.jpg'; + + for ( $i = 0; $i < 5; $i ++ ) { + self::$posts[ $i ] = $factory->post->create_and_get(); + self::$attachment_ids[ $i ] = $factory->attachment->create_upload_object( $file, self::$posts[ $i ]->ID ); + set_post_thumbnail( self::$posts[ $i ], self::$attachment_ids[ $i ] ); + } + } + + public static function wpTearDownAfterClass() { + foreach ( self::$attachment_ids as $attachment_id ) { + wp_delete_post( $attachment_id, true ); + } + } + + /** + * @covers ::render_block_core_latest_posts + */ + public function test_render_block_core_latest_posts() { + $action = new MockAction(); + add_filter( 'update_post_metadata_cache', array( $action, 'filter' ), 10, 2 ); + $attributes = array( + 'displayFeaturedImage' => true, + 'postsToShow' => 5, + 'orderBy' => 'date', + 'order' => 'DESC', + ); + + gutenberg_render_block_core_latest_posts( $attributes ); + $args = $action->get_args(); + $last_args = end( $args ); + $this->assertSameSets( self::$attachment_ids, $last_args[1] ); + } + + /** + * @covers ::render_block_core_latest_posts + */ + public function test_render_block_core_latest_posts_no_priming() { + $action = new MockAction(); + add_filter( 'update_post_metadata_cache', array( $action, 'filter' ), 10, 2 ); + $attributes = array( + 'displayFeaturedImage' => false, + 'postsToShow' => 5, + 'orderBy' => 'date', + 'order' => 'DESC', + ); + + gutenberg_render_block_core_latest_posts( $attributes ); + $args = $action->get_args(); + $last_args = end( $args ); + $this->assertContains( self::$posts[0]->ID, $last_args[1], 'Ensure that post is in array of post ids that are primed' ); + $this->assertNotContains( self::$sticky_post->ID, $last_args[1], 'Ensure that sticky post is not in array of post ids that are primed' ); + } +}