From ed25b09942efa37518666ef02b6683c48f648269 Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Tue, 15 Sep 2015 03:46:25 +0000 Subject: [PATCH] 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 --- wp-includes/post-thumbnail-template.php | 53 +++++++++++++++---------- wp-includes/version.php | 2 +- 2 files changed, 32 insertions(+), 23 deletions(-) diff --git a/wp-includes/post-thumbnail-template.php b/wp-includes/post-thumbnail-template.php index 7b8d1a2f25..f07302df3a 100644 --- a/wp-includes/post-thumbnail-template.php +++ b/wp-includes/post-thumbnail-template.php @@ -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 ); } diff --git a/wp-includes/version.php b/wp-includes/version.php index 4012a01fb7..61e66b4f2c 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -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.