Make WordPress Core

Ticket #12235: 12235.4.diff

File 12235.4.diff, 5.6 KB (added by flixos90, 9 years ago)
  • src/wp-includes/post-thumbnail-template.php

     
    210210                echo esc_url( $url );
    211211        }
    212212}
     213
     214/**
     215 * Returns the post thumbnail caption.
     216 *
     217 * @since 4.6.0
     218 *
     219 * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global `$post`.
     220 * @return string|false Post thumbnail caption or false if no caption is available.
     221 */
     222function get_the_post_thumbnail_caption( $post = null ) {
     223        $post_thumbnail_id = get_post_thumbnail_id( $post );
     224        if ( ! $post_thumbnail_id ) {
     225                return false;
     226        }
     227        return wp_get_attachment_caption( $post_thumbnail_id );
     228}
     229
     230/**
     231 * Displays the post thumbnail caption.
     232 *
     233 * @since 4.6.0
     234 *
     235 * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global `$post`.
     236 */
     237function the_post_thumbnail_caption( $post = null ) {
     238        $caption = get_the_post_thumbnail_caption( $post );
     239        if ( $caption ) {
     240                echo wp_kses( $caption, 'post' );
     241        }
     242}
  • src/wp-includes/post.php

     
    49374937}
    49384938
    49394939/**
     4940 * Retrieves caption for an attachment.
     4941 *
     4942 * @since 4.6.0
     4943 *
     4944 * @param int $post_id Optional. Attachment ID. Default 0.
     4945 * @return string|false False on failure. Attachment caption on success.
     4946 */
     4947function wp_get_attachment_caption( $post_id = 0 ) {
     4948        $post_id = (int) $post_id;
     4949        if ( ! $post = get_post( $post_id ) ) {
     4950                return false;
     4951        }
     4952
     4953        if ( 'attachment' !== $post->post_type ) {
     4954                return false;
     4955        }
     4956
     4957        $caption = $post->post_excerpt;
     4958
     4959        /**
     4960         * Filters the attachment caption.
     4961         *
     4962         * @since 4.6.0
     4963         *
     4964         * @param string $caption Caption for the given attachment.
     4965         * @param int    $post_id Attachment ID.
     4966         */
     4967        $caption = apply_filters( 'wp_get_attachment_caption', $caption, $post->ID );
     4968
     4969        if ( empty( $caption ) ) {
     4970                return false;
     4971        }
     4972
     4973        return $caption;
     4974}
     4975
     4976/**
    49404977 * Retrieve thumbnail for an attachment.
    49414978 *
    49424979 * @since 2.1.0
  • tests/phpunit/tests/media.php

     
    902902        }
    903903
    904904        /**
     905         * @ticket 12235
     906         */
     907        function test_wp_get_attachment_caption() {
     908                $this->assertFalse( wp_get_attachment_caption( 0 ) );
     909
     910                $caption = 'This is a caption.';
     911
     912                $post_id = self::factory()->post->create();
     913                $attachment_id = self::factory()->attachment->create_object( $this->img_name, $post_id, array(
     914                        'post_mime_type' => 'image/jpeg',
     915                        'post_type'      => 'attachment',
     916                        'post_excerpt'   => $caption,
     917                ) );
     918
     919                $this->assertFalse( wp_get_attachment_caption( $post_id ) );
     920
     921                $this->assertEquals( $caption, wp_get_attachment_caption( $attachment_id ) );
     922        }
     923
     924        /**
     925         * @ticket 12235
     926         */
     927        function test_wp_get_attachment_caption_empty() {
     928                $post_id = self::factory()->post->create();
     929                $attachment_id = self::factory()->attachment->create_object( $this->img_name, $post_id, array(
     930                        'post_mime_type' => 'image/jpeg',
     931                        'post_type'      => 'attachment',
     932                        'post_excerpt'   => '',
     933                ) );
     934
     935                $this->assertFalse( wp_get_attachment_caption( $attachment_id ) );
     936        }
     937
     938        /**
    905939         * Helper function to get image size array from size "name"
    906940         */
    907941        function _get_image_size_array_from_meta( $image_meta, $size_name ) {
  • tests/phpunit/tests/post/thumbnails.php

     
    7474                $this->assertTrue( $WP_Query->thumbnails_cached );
    7575        }
    7676
     77        /**
     78         * @ticket 12235
     79         */
     80        function test_get_the_post_thumbnail_caption() {
     81                $this->assertFalse( get_the_post_thumbnail_caption() );
     82
     83                $caption = 'This is a caption.';
     84
     85                $post_id = self::factory()->post->create();
     86                $attachment_id = self::factory()->attachment->create_object( 'image.jpg', $post_id, array(
     87                        'post_mime_type' => 'image/jpeg',
     88                        'post_type'      => 'attachment',
     89                        'post_excerpt'   => $caption,
     90                ) );
     91
     92                $this->assertFalse( get_the_post_thumbnail_caption( $post_id ) );
     93
     94                set_post_thumbnail( $post_id, $attachment_id );
     95
     96                $this->assertEquals( $caption, get_the_post_thumbnail_caption( $post_id ) );
     97        }
     98
     99        /**
     100         * @ticket 12235
     101         */
     102        function test_get_the_post_thumbnail_caption_empty() {
     103                $post_id = self::factory()->post->create();
     104                $attachment_id = self::factory()->attachment->create_object( 'image.jpg', $post_id, array(
     105                        'post_mime_type' => 'image/jpeg',
     106                        'post_type'      => 'attachment',
     107                        'post_excerpt'   => '',
     108                ) );
     109
     110                set_post_thumbnail( $post_id, $attachment_id );
     111
     112                $this->assertFalse( get_the_post_thumbnail_caption( $post_id ) );
     113        }
     114
     115        /**
     116         * @ticket 12235
     117         */
     118        function test_the_post_thumbnail_caption() {
     119                $caption = 'This is a caption.';
     120
     121                $post_id = self::factory()->post->create();
     122                $attachment_id = self::factory()->attachment->create_object( 'image.jpg', $post_id, array(
     123                        'post_mime_type' => 'image/jpeg',
     124                        'post_type'      => 'attachment',
     125                        'post_excerpt'   => $caption,
     126                ) );
     127
     128                set_post_thumbnail( $post_id, $attachment_id );
     129
     130                ob_start();
     131                the_post_thumbnail_caption( $post_id );
     132
     133                $this->assertEquals( $caption, ob_get_clean() );
     134        }
     135
    77136        function test_get_the_post_thumbnail() {
    78137                $this->assertEquals( '', get_the_post_thumbnail() );
    79138                $this->assertEquals( '', get_the_post_thumbnail( self::$post ) );