Update post thumbnail functions to allow a WP_Post to be passed.

Adds unit tests.

Props swissspidy, Rahe.
Fixes #33723.

Built from https://develop.svn.wordpress.org/trunk@34167


git-svn-id: http://core.svn.wordpress.org/trunk@34135 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Scott Taylor 2015-09-15 03:46:25 +00:00
parent 780487556b
commit ed25b09942
2 changed files with 32 additions and 23 deletions

View File

@ -13,25 +13,30 @@
* Check if post has an image attached.
*
* @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.
* @return bool Whether post has an image attached.
* @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global `$post`.
* @return bool Whether the post has an image attached.
*/
function has_post_thumbnail( $post_id = null ) {
return (bool) get_post_thumbnail_id( $post_id );
function has_post_thumbnail( $post = null ) {
return (bool) get_post_thumbnail_id( $post );
}
/**
* Retrieve Post Thumbnail ID.
* Retrieve post thumbnail ID.
*
* @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.
* @return mixed
* @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global `$post`.
* @return string|int Post thumbnail ID or empty string.
*/
function get_post_thumbnail_id( $post_id = null ) {
$post_id = ( null === $post_id ) ? get_the_ID() : $post_id;
return get_post_meta( $post_id, '_thumbnail_id', true );
function get_post_thumbnail_id( $post = null ) {
$post = get_post( $post );
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
*
@ -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.
*
* @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
* and width values. Default 'post-thumbnail'.
* @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 = '' ) {
$post_id = ( null === $post_id ) ? get_the_ID() : $post_id;
$post_thumbnail_id = get_post_thumbnail_id( $post_id );
function get_the_post_thumbnail( $post = null, $size = 'post-thumbnail', $attr = '' ) {
$post = get_post( $post );
if ( ! $post ) {
return '';
}
$post_thumbnail_id = get_post_thumbnail_id( $post );
/**
* 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
*
* @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 $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() )
update_post_thumbnail_cache();
$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
*
* @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 $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 {
$html = '';
@ -154,10 +163,10 @@ function get_the_post_thumbnail( $post_id = null, $size = 'post-thumbnail', $att
* @since 2.9.0
*
* @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 $size The post thumbnail size.
* @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 );
}

View File

@ -4,7 +4,7 @@
*
* @global string $wp_version
*/
$wp_version = '4.4-alpha-34166';
$wp_version = '4.4-alpha-34167';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.