Make WordPress Core

Ticket #33878: 33878_33070.diff

File 33878_33070.diff, 4.8 KB (added by dipesh.kakadiya, 10 years ago)

wp_get_attachment_image_url #33878, the_post_thumbnail_url and get_the_post_thumbnail_url #33070 function added for get image url

  • src/wp-includes/media.php

     
    793793}
    794794
    795795/**
     796 * Get the URL of an image attachment.
     797 *
     798 * @since 4.4.0
     799 *
     800 * @param int          $attachment_id Image attachment ID.
     801 * @param string|array $size          Optional. Registered image size to retrieve the source for or a flat
     802 *                                    array of height and width dimensions. Default 'thumbnail'.
     803 * @param bool         $icon          Optional. Whether the image should be treated as an icon. Default false.
     804 * @return string|false Attachment URL or false if no image is available.
     805 */
     806function wp_get_attachment_image_url( $attachment_id, $size = 'thumbnail', $icon = false ) {
     807        $image = wp_get_attachment_image_src( $attachment_id, $size, $icon );
     808        return isset( $image['0'] ) ? $image['0'] : false;
     809}
     810
     811/**
    796812 * Adds a 'wp-post-image' class to post thumbnails. Internal use only.
    797813 *
    798814 * Uses the 'begin_fetch_post_thumbnail_html' and 'end_fetch_post_thumbnail_html' action hooks to
  • src/wp-includes/post-thumbnail-template.php

     
    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 Post thumbnail URL or empty string.
     183 */
     184function get_the_post_thumbnail_url( $post = null, $size = 'post-thumbnail' ) {
     185        $image = wp_get_attachment_image_url( get_post_thumbnail_id( $post ), $size );
     186
     187        return isset( $image['0'] ) ? $image['0'] : '';
     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_the_post_thumbnail_url( null, $size );
     200}
  • tests/phpunit/tests/media.php

     
    718718
    719719                remove_filter( 'embed_maybe_make_link', array( $this, 'filter_wp_embed_shortcode_custom' ), 10 );
    720720        }
     721
     722        /**
     723         * @ticket 33878
     724         */
     725        function test_wp_get_attachment_image_url() {
     726                $this->assertFalse( wp_get_attachment_image_url( 0 ) );
     727
     728                $post_id = $this->factory->post->create();
     729                $attachment_id = $this->factory->attachment->create_object( $this->img_name, $post_id, array(
     730                        'post_mime_type' => 'image/jpeg',
     731                        'post_type' => 'attachment',
     732                ) );
     733
     734                $image = wp_get_attachment_image_src( $attachment_id, 'thumbnail', false );
     735
     736                $this->assertEquals( $image[0], wp_get_attachment_image_url( $attachment_id ) );
     737        }
    721738}
  • tests/phpunit/tests/post/thumbnails.php

     
    110110
    111111                $this->assertEquals( $expected, $actual );
    112112        }
     113
     114        /**
     115         * @ticket 33070
     116         */
     117        function test_get_the_post_thumbnail_url() {
     118                $this->assertEquals( '', get_the_post_thumbnail_url() );
     119                $this->assertEquals( '', get_the_post_thumbnail_url( $this->post ) );
     120
     121                set_post_thumbnail( $this->post, $this->attachment_id );
     122
     123                $this->assertEquals( '', get_the_post_thumbnail_url() );
     124                $this->assertEquals( wp_get_attachment_url( $this->attachment_id ), get_the_post_thumbnail_url( $this->post ) );
     125
     126                $GLOBALS['post'] = $this->post;
     127
     128                $this->assertEquals( wp_get_attachment_url( $this->attachment_id ), get_the_post_thumbnail_url() );
     129        }
     130
     131        /**
     132         * @ticket 33070
     133         */
     134        function test_the_post_thumbnail_url() {
     135                $GLOBALS['post'] = $this->post;
     136
     137                ob_start();
     138                the_post_thumbnail_url();
     139                $actual = ob_get_clean();
     140
     141                $this->assertEmpty( $actual );
     142
     143                ob_start();
     144                the_post_thumbnail_url();
     145                $actual = ob_get_clean();
     146
     147                $this->assertEmpty( $actual );
     148
     149                set_post_thumbnail( $this->post, $this->attachment_id );
     150
     151                ob_start();
     152                the_post_thumbnail_url();
     153                $actual = ob_get_clean();
     154
     155                $this->assertEquals( wp_get_attachment_url( $this->attachment_id ), $actual );
     156        }
    113157}