Make WordPress Core

Ticket #12235: 12235.2.diff

File 12235.2.diff, 5.1 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 esc_html( $caption );
     241        }
     242}
  • src/wp-includes/post.php

     
    51095109}
    51105110
    51115111/**
     5112 * Retrieves caption for an attachment.
     5113 *
     5114 * @since 4.6.0
     5115 *
     5116 * @param int $post_id Optional. Attachment ID. Default 0.
     5117 * @return string|false False on failure. Attachment caption on success.
     5118 */
     5119function wp_get_attachment_caption( $post_id = 0 ) {
     5120        $post_id = (int) $post_id;
     5121        if ( ! $post = get_post( $post_id ) ) {
     5122                return false;
     5123        }
     5124
     5125        if ( 'attachment' !== $post->post_type ) {
     5126                return false;
     5127        }
     5128
     5129        $caption = $post->post_excerpt;
     5130
     5131        /**
     5132         * Filters the attachment caption.
     5133         *
     5134         * @since 4.6.0
     5135         *
     5136         * @param string $caption Caption for the given attachment.
     5137         * @param int    $post_id Attachment ID.
     5138         */
     5139        $caption = apply_filters( 'wp_get_attachment_caption', $caption, $post->ID );
     5140
     5141        if ( empty( $caption ) ) {
     5142                return false;
     5143        }
     5144
     5145        return $caption;
     5146}
     5147
     5148/**
    51125149 * Retrieve thumbnail for an attachment.
    51135150 *
    51145151 * @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'   => '',
     917                ) );
     918
     919                $this->assertFalse( wp_get_attachment_caption( $post_id ) );
     920                $this->assertFalse( wp_get_attachment_caption( $attachment_id ) );
     921
     922                self::factory()->attachment->update_object( $attachment_id, array( 'post_excerpt' => $caption ) );
     923
     924                $this->assertEquals( $caption, wp_get_attachment_caption( $attachment_id ) );
     925        }
     926
     927        /**
    905928         * Helper function to get image size array from size "name"
    906929         */
    907930        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'   => '',
     90                ) );
     91
     92                $this->assertFalse( get_the_post_thumbnail_caption( $post_id ) );
     93
     94                set_post_thumbnail( $post_id, $attachment_id );
     95
     96                $this->assertFalse( get_the_post_thumbnail_caption( $post_id ) );
     97
     98                self::factory()->attachment->update_object( $attachment_id, array( 'post_excerpt' => $caption ) );
     99
     100                $this->assertEquals( $caption, get_the_post_thumbnail_caption( $post_id ) );
     101        }
     102
     103        /**
     104         * @ticket 12235
     105         */
     106        function test_the_post_thumbnail_caption() {
     107                $caption = 'This is a caption.';
     108
     109                $post_id = self::factory()->post->create();
     110                $attachment_id = self::factory()->attachment->create_object( 'image.jpg', $post_id, array(
     111                        'post_mime_type' => 'image/jpeg',
     112                        'post_type'      => 'attachment',
     113                        'post_excerpt'   => $caption,
     114                ) );
     115
     116                set_post_thumbnail( $post_id, $attachment_id );
     117
     118                ob_start();
     119                the_post_thumbnail_caption( $post_id );
     120
     121                $this->assertEquals( $caption, ob_get_clean() );
     122        }
     123
    77124        function test_get_the_post_thumbnail() {
    78125                $this->assertEquals( '', get_the_post_thumbnail() );
    79126                $this->assertEquals( '', get_the_post_thumbnail( self::$post ) );