Post Thumbnails: Pass post ID to `post_thumbnail_size` filter.

In addition to the enhancement, tests for the filter usage including the new parameter have been added.

Props NathanAtmoz.
Fixes #39030.


git-svn-id: https://develop.svn.wordpress.org/trunk@41267 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Felix Arntz 2017-08-18 18:18:51 +00:00
parent 677082be4b
commit c56dd07a30
2 changed files with 83 additions and 4 deletions

View File

@ -122,11 +122,13 @@ function get_the_post_thumbnail( $post = null, $size = 'post-thumbnail', $attr =
* Filters the post thumbnail size.
*
* @since 2.9.0
* @since 4.9.0 Added the `$post_id` parameter.
*
* @param string|array $size The post thumbnail size. Image size or array of width and height
* values (in that order). Default 'post-thumbnail'.
* @param string|array $size The post thumbnail size. Image size or array of width and height
* values (in that order). Default 'post-thumbnail'.
* @param int $post_id The post ID.
*/
$size = apply_filters( 'post_thumbnail_size', $size );
$size = apply_filters( 'post_thumbnail_size', $size, $post->ID );
if ( $post_thumbnail_id ) {

View File

@ -6,10 +6,16 @@
*/
class Tests_Post_Thumbnail_Template extends WP_UnitTestCase {
protected static $post;
protected static $different_post;
protected static $attachment_id;
protected $current_size_filter_data = null;
protected $current_size_filter_result = null;
public static function wpSetUpBeforeClass( $factory ) {
self::$post = $factory->post->create_and_get();
self::$post = $factory->post->create_and_get();
self::$different_post = $factory->post->create_and_get();
$file = DIR_TESTDATA . '/images/canola.jpg';
self::$attachment_id = $factory->attachment->create_upload_object( $file, self::$post->ID, array(
'post_mime_type' => 'image/jpeg',
@ -335,4 +341,75 @@ class Tests_Post_Thumbnail_Template extends WP_UnitTestCase {
$thumbnail_id = get_post_thumbnail_id( $post_id );
$this->assertEmpty( $thumbnail_id );
}
/**
* @ticket 39030
*/
function test_post_thumbnail_size_filter_simple() {
$this->current_size_filter_data = 'medium';
add_filter( 'post_thumbnail_size', array( $this, 'filter_post_thumbnail_size' ), 10, 2 );
// This filter is used to capture the $size result.
add_filter( 'post_thumbnail_html', array( $this, 'filter_set_post_thumbnail_size_result' ), 10, 4 );
get_the_post_thumbnail( self::$post );
$result = $this->current_size_filter_result;
$this->current_size_filter_data = null;
$this->current_size_filter_result = null;
$this->assertSame( 'medium', $result );
}
/**
* @ticket 39030
* @dataProvider data_post_thumbnail_size_filter_complex
*/
function test_post_thumbnail_size_filter_complex( $which_post, $expected ) {
$this->current_size_filter_data = array(
self::$post->ID => 'medium',
self::$different_post->ID => 'thumbnail',
);
$post = $which_post === 1 ? self::$different_post : self::$post;
add_filter( 'post_thumbnail_size', array( $this, 'filter_post_thumbnail_size' ), 10, 2 );
// This filter is used to capture the $size result.
add_filter( 'post_thumbnail_html', array( $this, 'filter_set_post_thumbnail_size_result' ), 10, 4 );
get_the_post_thumbnail( $post );
$result = $this->current_size_filter_result;
$this->current_size_filter_data = null;
$this->current_size_filter_result = null;
$this->assertSame( $expected, $result );
}
function data_post_thumbnail_size_filter_complex() {
return array(
array( 0, 'medium' ),
array( 1, 'thumbnail' ),
);
}
function filter_post_thumbnail_size( $size, $post_id ) {
if ( is_array( $this->current_size_filter_data ) && isset( $this->current_size_filter_data[ $post_id ] ) ) {
return $this->current_size_filter_data[ $post_id ];
}
if ( is_string( $this->current_size_filter_data ) ) {
return $this->current_size_filter_data;
}
return $size;
}
function filter_set_post_thumbnail_size_result( $html, $post_id, $post_thumbnail_id, $size ) {
$this->current_size_filter_result = $size;
return $html;
}
}