WordPress.org

Make WordPress Core

Changeset 34167


Ignore:
Timestamp:
09/15/2015 03:45:23 AM (4 years ago)
Author:
wonderboymusic
Message:

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

Adds unit tests.

Props swissspidy, Rahe.
Fixes #33723.

Location:
trunk
Files:
1 added
1 edited

Legend:

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

    r32618 r34167  
    1414 *
    1515 * @since 2.9.0
     16 * @since 4.4.0 `$post` can be a post ID or WP_Post object.
    1617 *
    17  * @param int $post_id Optional. Post ID.
    18  * @return bool Whether post has an image attached.
     18 * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global `$post`.
     19 * @return bool Whether the post has an image attached.
    1920 */
    20 function has_post_thumbnail( $post_id = null ) {
    21     return (bool) get_post_thumbnail_id( $post_id );
     21function has_post_thumbnail( $post = null ) {
     22    return (bool) get_post_thumbnail_id( $post );
    2223}
    2324
    2425/**
    25  * Retrieve Post Thumbnail ID.
     26 * Retrieve post thumbnail ID.
    2627 *
    2728 * @since 2.9.0
     29 * @since 4.4.0 `$post` can be a post ID or WP_Post object.
    2830 *
    29  * @param int|null $post_id Optional. Post ID.
    30  * @return mixed
     31 * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global `$post`.
     32 * @return string|int Post thumbnail ID or empty string.
    3133 */
    32 function get_post_thumbnail_id( $post_id = null ) {
    33     $post_id = ( null === $post_id ) ? get_the_ID() : $post_id;
    34     return get_post_meta( $post_id, '_thumbnail_id', true );
     34function get_post_thumbnail_id( $post = null ) {
     35    $post = get_post( $post );
     36    if ( ! $post ) {
     37        return '';
     38    }
     39    return get_post_meta( $post->ID, '_thumbnail_id', true );
    3540}
    3641
     
    5863
    5964/**
    60  * Update cache for thumbnails in the current loop
     65 * Update cache for thumbnails in the current loop.
    6166 *
    6267 * @since 3.2.0
     
    97102 *
    98103 * @since 2.9.0
     104 * @since 4.4.0 `$post` can be a post ID or WP_Post object.
    99105 *
    100  * @param int $post_id       Post ID. Default is the ID of the `$post` global.
     106 * @param int|WP_Post  $post Optional. Post ID or WP_Post object.  Default is global `$post`.
    101107 * @param string|array $size Optional. Registered image size to use, or flat array of height
    102108 *                           and width values. Default 'post-thumbnail'.
    103109 * @param string|array $attr Optional. Query string or array of attributes. Default empty.
    104  * @return string
     110 * @return string The post thumbnail image tag.
    105111 */
    106 function get_the_post_thumbnail( $post_id = null, $size = 'post-thumbnail', $attr = '' ) {
    107     $post_id = ( null === $post_id ) ? get_the_ID() : $post_id;
    108     $post_thumbnail_id = get_post_thumbnail_id( $post_id );
     112function get_the_post_thumbnail( $post = null, $size = 'post-thumbnail', $attr = '' ) {
     113    $post = get_post( $post );
     114    if ( ! $post ) {
     115        return '';
     116    }
     117    $post_thumbnail_id = get_post_thumbnail_id( $post );
    109118
    110119    /**
     
    126135         * @since 2.9.0
    127136         *
    128          * @param string $post_id           The post ID.
     137         * @param int    $post_id           The post ID.
    129138         * @param string $post_thumbnail_id The post thumbnail ID.
    130139         * @param string $size              The post thumbnail size.
    131140         */
    132         do_action( 'begin_fetch_post_thumbnail_html', $post_id, $post_thumbnail_id, $size );
     141        do_action( 'begin_fetch_post_thumbnail_html', $post->ID, $post_thumbnail_id, $size );
    133142        if ( in_the_loop() )
    134143            update_post_thumbnail_cache();
     
    140149         * @since 2.9.0
    141150         *
    142          * @param string $post_id           The post ID.
     151         * @param int    $post_id           The post ID.
    143152         * @param string $post_thumbnail_id The post thumbnail ID.
    144153         * @param string $size              The post thumbnail size.
    145154         */
    146         do_action( 'end_fetch_post_thumbnail_html', $post_id, $post_thumbnail_id, $size );
     155        do_action( 'end_fetch_post_thumbnail_html', $post->ID, $post_thumbnail_id, $size );
    147156
    148157    } else {
     
    155164     *
    156165     * @param string $html              The post thumbnail HTML.
    157      * @param string $post_id           The post ID.
     166     * @param int    $post_id           The post ID.
    158167     * @param string $post_thumbnail_id The post thumbnail ID.
    159168     * @param string $size              The post thumbnail size.
    160169     * @param string $attr              Query string of attributes.
    161170     */
    162     return apply_filters( 'post_thumbnail_html', $html, $post_id, $post_thumbnail_id, $size, $attr );
     171    return apply_filters( 'post_thumbnail_html', $html, $post->ID, $post_thumbnail_id, $size, $attr );
    163172}
Note: See TracChangeset for help on using the changeset viewer.