WordPress.org

Make WordPress Core

Changeset 41267


Ignore:
Timestamp:
08/18/17 18:18:51 (5 months ago)
Author:
flixos90
Message:

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.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/post-thumbnail-template.php

    r37915 r41267  
    123123     * 
    124124     * @since 2.9.0 
    125      * 
    126      * @param string|array $size The post thumbnail size. Image size or array of width and height 
    127      *                           values (in that order). Default 'post-thumbnail'. 
     125     * @since 4.9.0 Added the `$post_id` parameter. 
     126     * 
     127     * @param string|array $size    The post thumbnail size. Image size or array of width and height 
     128     *                              values (in that order). Default 'post-thumbnail'. 
     129     * @param int          $post_id The post ID. 
    128130     */ 
    129     $size = apply_filters( 'post_thumbnail_size', $size ); 
     131    $size = apply_filters( 'post_thumbnail_size', $size, $post->ID ); 
    130132 
    131133    if ( $post_thumbnail_id ) { 
  • trunk/tests/phpunit/tests/post/thumbnails.php

    r39919 r41267  
    77class Tests_Post_Thumbnail_Template extends WP_UnitTestCase { 
    88    protected static $post; 
     9    protected static $different_post; 
    910    protected static $attachment_id; 
    1011 
     12    protected $current_size_filter_data = null; 
     13    protected $current_size_filter_result = null; 
     14 
    1115    public static function wpSetUpBeforeClass( $factory ) { 
    12         self::$post = $factory->post->create_and_get(); 
     16        self::$post           = $factory->post->create_and_get(); 
     17        self::$different_post = $factory->post->create_and_get(); 
     18 
    1319        $file = DIR_TESTDATA . '/images/canola.jpg'; 
    1420        self::$attachment_id = $factory->attachment->create_upload_object( $file, self::$post->ID, array( 
     
    336342        $this->assertEmpty( $thumbnail_id ); 
    337343    } 
     344 
     345    /** 
     346     * @ticket 39030 
     347     */ 
     348    function test_post_thumbnail_size_filter_simple() { 
     349        $this->current_size_filter_data = 'medium'; 
     350 
     351        add_filter( 'post_thumbnail_size', array( $this, 'filter_post_thumbnail_size' ), 10, 2 ); 
     352 
     353        // This filter is used to capture the $size result. 
     354        add_filter( 'post_thumbnail_html', array( $this, 'filter_set_post_thumbnail_size_result' ), 10, 4 ); 
     355        get_the_post_thumbnail( self::$post ); 
     356 
     357        $result = $this->current_size_filter_result; 
     358 
     359        $this->current_size_filter_data   = null; 
     360        $this->current_size_filter_result = null; 
     361 
     362        $this->assertSame( 'medium', $result ); 
     363    } 
     364 
     365    /** 
     366     * @ticket 39030 
     367     * @dataProvider data_post_thumbnail_size_filter_complex 
     368     */ 
     369    function test_post_thumbnail_size_filter_complex( $which_post, $expected ) { 
     370        $this->current_size_filter_data = array( 
     371            self::$post->ID           => 'medium', 
     372            self::$different_post->ID => 'thumbnail', 
     373        ); 
     374 
     375        $post = $which_post === 1 ? self::$different_post : self::$post; 
     376 
     377        add_filter( 'post_thumbnail_size', array( $this, 'filter_post_thumbnail_size' ), 10, 2 ); 
     378 
     379        // This filter is used to capture the $size result. 
     380        add_filter( 'post_thumbnail_html', array( $this, 'filter_set_post_thumbnail_size_result' ), 10, 4 ); 
     381        get_the_post_thumbnail( $post ); 
     382 
     383        $result = $this->current_size_filter_result; 
     384 
     385        $this->current_size_filter_data   = null; 
     386        $this->current_size_filter_result = null; 
     387 
     388        $this->assertSame( $expected, $result ); 
     389    } 
     390 
     391    function data_post_thumbnail_size_filter_complex() { 
     392        return array( 
     393            array( 0, 'medium' ), 
     394            array( 1, 'thumbnail' ), 
     395        ); 
     396    } 
     397 
     398    function filter_post_thumbnail_size( $size, $post_id ) { 
     399        if ( is_array( $this->current_size_filter_data ) && isset( $this->current_size_filter_data[ $post_id ] ) ) { 
     400            return $this->current_size_filter_data[ $post_id ]; 
     401        } 
     402 
     403        if ( is_string( $this->current_size_filter_data ) ) { 
     404            return $this->current_size_filter_data; 
     405        } 
     406 
     407        return $size; 
     408    } 
     409 
     410    function filter_set_post_thumbnail_size_result( $html, $post_id, $post_thumbnail_id, $size ) { 
     411        $this->current_size_filter_result = $size; 
     412 
     413        return $html; 
     414    } 
    338415} 
Note: See TracChangeset for help on using the changeset viewer.