WordPress.org

Make WordPress Core

Ticket #33070: 33070.2.diff

File 33070.2.diff, 2.8 KB (added by swissspidy, 4 years ago)
  • src/wp-includes/post-thumbnail-template.php

    diff --git src/wp-includes/post-thumbnail-template.php src/wp-includes/post-thumbnail-template.php
    index f07302d..cf87b8c 100644
    function get_the_post_thumbnail( $post = null, $size = 'post-thumbnail', $attr = 
    170170         */
    171171        return apply_filters( 'post_thumbnail_html', $html, $post->ID, $post_thumbnail_id, $size, $attr );
    172172}
     173
     174/**
     175 * Return the post thumbnail URL.
     176 *
     177 * @since 4.4.0
     178 *
     179 * @param int|WP_Post  $post Optional. Post ID or WP_Post object.  Default is global `$post`.
     180 * @param string|array $size Optional. Registered image size to retrieve the source for or a flat
     181 *                           array of height and width dimensions. Default 'post-thumbnail'.
     182 * @return string|false Post thumbnail URL or false.
     183 */
     184function get_post_thumbnail_url( $post = null, $size = 'post-thumbnail' ) {
     185        $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post ), $size );
     186
     187        return isset( $image['0'] ) ? $image['0'] : false;
     188}
     189
     190/**
     191 * Display the post thumbnail URL.
     192 *
     193 * @since 4.4.0
     194 *
     195 * @param string|array $size Optional. Registered image size to retrieve the source for or a flat
     196 *                           array of height and width dimensions. Default 'post-thumbnail'.
     197 */
     198function the_post_thumbnail_url( $size = 'post-thumbnail' ) {
     199        echo get_post_thumbnail_url( null, $size );
     200}
  • tests/phpunit/tests/post/thumbnails.php

    diff --git tests/phpunit/tests/post/thumbnails.php tests/phpunit/tests/post/thumbnails.php
    index ed212fa..33182b2 100644
    class Tests_Post_Thumbnail_Template extends WP_UnitTestCase { 
    110110
    111111                $this->assertEquals( $expected, $actual );
    112112        }
     113
     114        function test_get_post_thumbnail_url() {
     115                $this->assertEquals( false, get_post_thumbnail_url() );
     116                $this->assertEquals( false, get_post_thumbnail_url( $this->post ) );
     117
     118                set_post_thumbnail( $this->post, $this->attachment_id );
     119
     120                $this->assertEquals( false, get_post_thumbnail_url() );
     121                $this->assertEquals( wp_get_attachment_url( $this->attachment_id ), get_post_thumbnail_url( $this->post ) );
     122
     123                $GLOBALS['post'] = $this->post;
     124
     125                $this->assertEquals( wp_get_attachment_url( $this->attachment_id ), get_post_thumbnail_url() );
     126        }
     127
     128        function test_the_post_thumbnail_url() {
     129                $GLOBALS['post'] = $this->post;
     130
     131                ob_start();
     132                the_post_thumbnail_url();
     133                $actual = ob_get_clean();
     134
     135                $this->assertEquals( false, $actual );
     136
     137                ob_start();
     138                the_post_thumbnail_url();
     139                $actual = ob_get_clean();
     140
     141                $this->assertEquals( false, $actual );
     142
     143                set_post_thumbnail( $this->post, $this->attachment_id );
     144
     145                ob_start();
     146                the_post_thumbnail_url();
     147                $actual = ob_get_clean();
     148
     149                $this->assertEquals( wp_get_attachment_url( $this->attachment_id ), $actual );
     150        }
    113151}