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

Ensure that post thumbnail is cached in latest post block. #40571

Merged
merged 11 commits into from
May 10, 2022
18 changes: 12 additions & 6 deletions packages/block-library/src/latest-posts/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand All @@ -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'] ) {
spacedmonkey marked this conversation as resolved.
Show resolved Hide resolved
update_post_thumbnail_cache( $query );
}

$list_items_markup = '';

Expand Down
79 changes: 79 additions & 0 deletions phpunit/blocks/render-last-posts-test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php
/**
* Last post block rendering tests.
*
* @package WordPress
* @subpackage Blocks
*/

/**
* Tests for the Last post block.
*
* @group blocks
*/
class Tests_Blocks_RenderLastPosts extends WP_UnitTestCase {
protected static $post;
spacedmonkey marked this conversation as resolved.
Show resolved Hide resolved
protected static $sticky_post;
protected static $attachment_id;

public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
self::$sticky_post = $factory->post->create_and_get(
spacedmonkey marked this conversation as resolved.
Show resolved Hide resolved
array(
'post_title' => 'Sticky post',
'post_date' => '2008-09-03 00:00:00',
)
);
$factory->post->create_many( 2 );
self::$post = $factory->post->create_and_get();

stick_post( self::$sticky_post->ID );

$file = DIR_TESTDATA . '/images/canola.jpg';
self::$attachment_id = $factory->attachment->create_upload_object( $file, self::$post->ID );
set_post_thumbnail( self::$post, self::$attachment_id );
}

public static function tear_down_after_class() {
wp_delete_post( self::$attachment_id, true );
parent::tear_down_after_class();
}
spacedmonkey marked this conversation as resolved.
Show resolved Hide resolved

/**
* @covers render_block_core_latest_posts
spacedmonkey marked this conversation as resolved.
Show resolved Hide resolved
*/
public function test_render_block_core_latest_posts() {
$a = new MockAction();
add_filter( 'update_post_metadata_cache', array( $a, 'filter' ), 10, 2 );
spacedmonkey marked this conversation as resolved.
Show resolved Hide resolved
$attributes = array(
'displayFeaturedImage' => true,
'postsToShow' => 3,
'orderBy' => 'date',
'order' => 'DESC',
);

render_block_core_latest_posts( $attributes );
$args = $a->get_args();
spacedmonkey marked this conversation as resolved.
Show resolved Hide resolved
$last_args = end( $args );
$this->assertContains( self::$attachment_id, $last_args[1] );
}

/**
* @covers render_block_core_latest_posts
spacedmonkey marked this conversation as resolved.
Show resolved Hide resolved
*/
public function test_render_block_core_latest_posts_no_priming() {
$a = new MockAction();
add_filter( 'update_post_metadata_cache', array( $a, 'filter' ), 10, 2 );
spacedmonkey marked this conversation as resolved.
Show resolved Hide resolved
$attributes = array(
'displayFeaturedImage' => false,
'postsToShow' => 3,
'orderBy' => 'date',
'order' => 'DESC',
);

render_block_core_latest_posts( $attributes );
$args = $a->get_args();
spacedmonkey marked this conversation as resolved.
Show resolved Hide resolved
$last_args = end( $args );
$this->assertContains( self::$post->ID, $last_args );
$this->assertNotContains( self::$sticky_post->ID, $last_args[1] );
spacedmonkey marked this conversation as resolved.
Show resolved Hide resolved
}
}