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:
parent
780487556b
commit
ed25b09942
|
@ -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 );
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,7 +4,7 @@
|
||||||
*
|
*
|
||||||
* @global string $wp_version
|
* @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.
|
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
|
||||||
|
|
Loading…
Reference in New Issue