Update post thumbnail functions to allow a WP_Post
to be passed.
Adds unit tests. Props swissspidy, Rahe. Fixes #33723. git-svn-id: https://develop.svn.wordpress.org/trunk@34167 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
1a537a4f6d
commit
c702a71ad1
@ -13,25 +13,30 @@
|
|||||||
* Check if post has an image attached.
|
* Check if post has an image attached.
|
||||||
*
|
*
|
||||||
* @since 2.9.0
|
* @since 2.9.0
|
||||||
|
* @since 4.4.0 `$post` can be a post ID or WP_Post object.
|
||||||
*
|
*
|
||||||
* @param int $post_id Optional. Post ID.
|
* @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global `$post`.
|
||||||
* @return bool Whether post has an image attached.
|
* @return bool Whether the post has an image attached.
|
||||||
*/
|
*/
|
||||||
function has_post_thumbnail( $post_id = null ) {
|
function has_post_thumbnail( $post = null ) {
|
||||||
return (bool) get_post_thumbnail_id( $post_id );
|
return (bool) get_post_thumbnail_id( $post );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieve Post Thumbnail ID.
|
* Retrieve post thumbnail ID.
|
||||||
*
|
*
|
||||||
* @since 2.9.0
|
* @since 2.9.0
|
||||||
|
* @since 4.4.0 `$post` can be a post ID or WP_Post object.
|
||||||
*
|
*
|
||||||
* @param int|null $post_id Optional. Post ID.
|
* @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global `$post`.
|
||||||
* @return mixed
|
* @return string|int Post thumbnail ID or empty string.
|
||||||
*/
|
*/
|
||||||
function get_post_thumbnail_id( $post_id = null ) {
|
function get_post_thumbnail_id( $post = null ) {
|
||||||
$post_id = ( null === $post_id ) ? get_the_ID() : $post_id;
|
$post = get_post( $post );
|
||||||
return get_post_meta( $post_id, '_thumbnail_id', true );
|
if ( ! $post ) {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
return get_post_meta( $post->ID, '_thumbnail_id', true );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -57,7 +62,7 @@ function the_post_thumbnail( $size = 'post-thumbnail', $attr = '' ) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Update cache for thumbnails in the current loop
|
* Update cache for thumbnails in the current loop.
|
||||||
*
|
*
|
||||||
* @since 3.2.0
|
* @since 3.2.0
|
||||||
*
|
*
|
||||||
@ -96,16 +101,20 @@ function update_post_thumbnail_cache( $wp_query = null ) {
|
|||||||
* size is used by default, though a different size can be specified instead as needed.
|
* size is used by default, though a different size can be specified instead as needed.
|
||||||
*
|
*
|
||||||
* @since 2.9.0
|
* @since 2.9.0
|
||||||
|
* @since 4.4.0 `$post` can be a post ID or WP_Post object.
|
||||||
*
|
*
|
||||||
* @param int $post_id Post ID. Default is the ID of the `$post` global.
|
* @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global `$post`.
|
||||||
* @param string|array $size Optional. Registered image size to use, or flat array of height
|
* @param string|array $size Optional. Registered image size to use, or flat array of height
|
||||||
* and width values. Default 'post-thumbnail'.
|
* and width values. Default 'post-thumbnail'.
|
||||||
* @param string|array $attr Optional. Query string or array of attributes. Default empty.
|
* @param string|array $attr Optional. Query string or array of attributes. Default empty.
|
||||||
* @return string
|
* @return string The post thumbnail image tag.
|
||||||
*/
|
*/
|
||||||
function get_the_post_thumbnail( $post_id = null, $size = 'post-thumbnail', $attr = '' ) {
|
function get_the_post_thumbnail( $post = null, $size = 'post-thumbnail', $attr = '' ) {
|
||||||
$post_id = ( null === $post_id ) ? get_the_ID() : $post_id;
|
$post = get_post( $post );
|
||||||
$post_thumbnail_id = get_post_thumbnail_id( $post_id );
|
if ( ! $post ) {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
$post_thumbnail_id = get_post_thumbnail_id( $post );
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Filter the post thumbnail size.
|
* Filter the post thumbnail size.
|
||||||
@ -125,11 +134,11 @@ function get_the_post_thumbnail( $post_id = null, $size = 'post-thumbnail', $att
|
|||||||
*
|
*
|
||||||
* @since 2.9.0
|
* @since 2.9.0
|
||||||
*
|
*
|
||||||
* @param string $post_id The post ID.
|
* @param int $post_id The post ID.
|
||||||
* @param string $post_thumbnail_id The post thumbnail ID.
|
* @param string $post_thumbnail_id The post thumbnail ID.
|
||||||
* @param string $size The post thumbnail size.
|
* @param string $size The post thumbnail size.
|
||||||
*/
|
*/
|
||||||
do_action( 'begin_fetch_post_thumbnail_html', $post_id, $post_thumbnail_id, $size );
|
do_action( 'begin_fetch_post_thumbnail_html', $post->ID, $post_thumbnail_id, $size );
|
||||||
if ( in_the_loop() )
|
if ( in_the_loop() )
|
||||||
update_post_thumbnail_cache();
|
update_post_thumbnail_cache();
|
||||||
$html = wp_get_attachment_image( $post_thumbnail_id, $size, false, $attr );
|
$html = wp_get_attachment_image( $post_thumbnail_id, $size, false, $attr );
|
||||||
@ -139,11 +148,11 @@ function get_the_post_thumbnail( $post_id = null, $size = 'post-thumbnail', $att
|
|||||||
*
|
*
|
||||||
* @since 2.9.0
|
* @since 2.9.0
|
||||||
*
|
*
|
||||||
* @param string $post_id The post ID.
|
* @param int $post_id The post ID.
|
||||||
* @param string $post_thumbnail_id The post thumbnail ID.
|
* @param string $post_thumbnail_id The post thumbnail ID.
|
||||||
* @param string $size The post thumbnail size.
|
* @param string $size The post thumbnail size.
|
||||||
*/
|
*/
|
||||||
do_action( 'end_fetch_post_thumbnail_html', $post_id, $post_thumbnail_id, $size );
|
do_action( 'end_fetch_post_thumbnail_html', $post->ID, $post_thumbnail_id, $size );
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
$html = '';
|
$html = '';
|
||||||
@ -154,10 +163,10 @@ function get_the_post_thumbnail( $post_id = null, $size = 'post-thumbnail', $att
|
|||||||
* @since 2.9.0
|
* @since 2.9.0
|
||||||
*
|
*
|
||||||
* @param string $html The post thumbnail HTML.
|
* @param string $html The post thumbnail HTML.
|
||||||
* @param string $post_id The post ID.
|
* @param int $post_id The post ID.
|
||||||
* @param string $post_thumbnail_id The post thumbnail ID.
|
* @param string $post_thumbnail_id The post thumbnail ID.
|
||||||
* @param string $size The post thumbnail size.
|
* @param string $size The post thumbnail size.
|
||||||
* @param string $attr Query string of attributes.
|
* @param string $attr Query string of attributes.
|
||||||
*/
|
*/
|
||||||
return apply_filters( 'post_thumbnail_html', $html, $post_id, $post_thumbnail_id, $size, $attr );
|
return apply_filters( 'post_thumbnail_html', $html, $post->ID, $post_thumbnail_id, $size, $attr );
|
||||||
}
|
}
|
||||||
|
113
tests/phpunit/tests/post/thumbnails.php
Normal file
113
tests/phpunit/tests/post/thumbnails.php
Normal file
@ -0,0 +1,113 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @group post
|
||||||
|
* @group media
|
||||||
|
*/
|
||||||
|
class Tests_Post_Thumbnail_Template extends WP_UnitTestCase {
|
||||||
|
protected $post;
|
||||||
|
protected $attachment_id;
|
||||||
|
|
||||||
|
function setUp() {
|
||||||
|
parent::setUp();
|
||||||
|
|
||||||
|
$this->post = $this->factory->post->create_and_get();
|
||||||
|
$file = DIR_TESTDATA . '/images/canola.jpg';
|
||||||
|
$this->attachment_id = $this->factory->attachment->create_object( $file, $this->post->ID, array(
|
||||||
|
'post_mime_type' => 'image/jpeg',
|
||||||
|
) );
|
||||||
|
}
|
||||||
|
|
||||||
|
function test_has_post_thumbnail() {
|
||||||
|
$this->assertFalse( has_post_thumbnail( $this->post ) );
|
||||||
|
$this->assertFalse( has_post_thumbnail( $this->post->ID ) );
|
||||||
|
$this->assertFalse( has_post_thumbnail() );
|
||||||
|
|
||||||
|
$GLOBALS['post'] = $this->post;
|
||||||
|
|
||||||
|
$this->assertFalse( has_post_thumbnail() );
|
||||||
|
|
||||||
|
unset( $GLOBALS['post'] );
|
||||||
|
|
||||||
|
set_post_thumbnail( $this->post, $this->attachment_id );
|
||||||
|
|
||||||
|
$this->assertTrue( has_post_thumbnail( $this->post ) );
|
||||||
|
$this->assertTrue( has_post_thumbnail( $this->post->ID ) );
|
||||||
|
$this->assertFalse( has_post_thumbnail() );
|
||||||
|
|
||||||
|
$GLOBALS['post'] = $this->post;
|
||||||
|
|
||||||
|
$this->assertTrue( has_post_thumbnail() );
|
||||||
|
}
|
||||||
|
|
||||||
|
function test_get_post_thumbnail_id() {
|
||||||
|
$this->assertEmpty( get_post_thumbnail_id( $this->post ) );
|
||||||
|
$this->assertEmpty( get_post_thumbnail_id( $this->post->ID ) );
|
||||||
|
$this->assertEmpty( get_post_thumbnail_id() );
|
||||||
|
|
||||||
|
set_post_thumbnail( $this->post, $this->attachment_id );
|
||||||
|
|
||||||
|
$this->assertEquals( $this->attachment_id, get_post_thumbnail_id( $this->post ) );
|
||||||
|
$this->assertEquals( $this->attachment_id, get_post_thumbnail_id( $this->post->ID ) );
|
||||||
|
|
||||||
|
$GLOBALS['post'] = $this->post;
|
||||||
|
|
||||||
|
$this->assertEquals( $this->attachment_id, get_post_thumbnail_id() );
|
||||||
|
}
|
||||||
|
|
||||||
|
function test_update_post_thumbnail_cache() {
|
||||||
|
set_post_thumbnail( $this->post, $this->attachment_id );
|
||||||
|
|
||||||
|
$WP_Query = new WP_Query( array(
|
||||||
|
'post_type' => 'any',
|
||||||
|
'post__in' => array( $this->post->ID ),
|
||||||
|
'orderby' => 'post__in',
|
||||||
|
) );
|
||||||
|
|
||||||
|
$this->assertFalse( $WP_Query->thumbnails_cached );
|
||||||
|
|
||||||
|
update_post_thumbnail_cache( $WP_Query );
|
||||||
|
|
||||||
|
$this->assertTrue( $WP_Query->thumbnails_cached );
|
||||||
|
}
|
||||||
|
|
||||||
|
function test_get_the_post_thumbnail() {
|
||||||
|
$this->assertEquals( '', get_the_post_thumbnail() );
|
||||||
|
$this->assertEquals( '', get_the_post_thumbnail( $this->post ) );
|
||||||
|
set_post_thumbnail( $this->post, $this->attachment_id );
|
||||||
|
|
||||||
|
$expected = wp_get_attachment_image( $this->attachment_id, 'post-thumbnail', false, array( 'class' => 'attachment-post-thumbnail wp-post-image' ) );
|
||||||
|
|
||||||
|
$this->assertEquals( $expected, get_the_post_thumbnail( $this->post ) );
|
||||||
|
|
||||||
|
$GLOBALS['post'] = $this->post;
|
||||||
|
|
||||||
|
$this->assertEquals( $expected, get_the_post_thumbnail() );
|
||||||
|
}
|
||||||
|
|
||||||
|
function test_the_post_thumbnail() {
|
||||||
|
ob_start();
|
||||||
|
the_post_thumbnail();
|
||||||
|
$actual = ob_get_clean();
|
||||||
|
|
||||||
|
$this->assertEquals( '', $actual );
|
||||||
|
|
||||||
|
$GLOBALS['post'] = $this->post;
|
||||||
|
|
||||||
|
ob_start();
|
||||||
|
the_post_thumbnail();
|
||||||
|
$actual = ob_get_clean();
|
||||||
|
|
||||||
|
$this->assertEquals( '', $actual );
|
||||||
|
|
||||||
|
set_post_thumbnail( $this->post, $this->attachment_id );
|
||||||
|
|
||||||
|
$expected = wp_get_attachment_image( $this->attachment_id, 'post-thumbnail', false, array( 'class' => 'attachment-post-thumbnail wp-post-image' ) );
|
||||||
|
|
||||||
|
ob_start();
|
||||||
|
the_post_thumbnail();
|
||||||
|
$actual = ob_get_clean();
|
||||||
|
|
||||||
|
$this->assertEquals( $expected, $actual );
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user