WordPress.org

Make WordPress Core

Ticket #12235: 12235.7.diff

File 12235.7.diff, 6.4 KB (added by joemcgill, 5 years ago)
  • src/wp-includes/default-filters.php

    diff --git src/wp-includes/default-filters.php src/wp-includes/default-filters.php
    index 1b1f01c..70da2f2 100644
    add_filter( 'the_excerpt', 'wpautop' ); 
    143143add_filter( 'the_excerpt',     'shortcode_unautop');
    144144add_filter( 'get_the_excerpt', 'wp_trim_excerpt'  );
    145145
     146add_filter( 'the_post_thumbnail_caption', 'wptexturize'     );
     147add_filter( 'the_post_thumbnail_caption', 'convert_smilies' );
     148add_filter( 'the_post_thumbnail_caption', 'convert_chars'   );
     149
    146150add_filter( 'comment_text', 'wptexturize'            );
    147151add_filter( 'comment_text', 'convert_chars'          );
    148152add_filter( 'comment_text', 'make_clickable',      9 );
  • src/wp-includes/post-thumbnail-template.php

    diff --git src/wp-includes/post-thumbnail-template.php src/wp-includes/post-thumbnail-template.php
    index 2b95f4f..4722565 100644
    function the_post_thumbnail_url( $size = 'post-thumbnail' ) { 
    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 Post thumbnail caption.
     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 '';
     226        }
     227
     228        $caption = wp_get_attachment_caption( $post_thumbnail_id );
     229
     230        if ( ! $caption ) {
     231                $caption = '';
     232        }
     233
     234        return $caption;
     235}
     236
     237/**
     238 * Displays the post thumbnail caption.
     239 *
     240 * @since 4.6.0
     241 *
     242 * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global `$post`.
     243 */
     244function the_post_thumbnail_caption( $post = null ) {
     245        /**
     246         * Filters the displayed post thumbnail caption.
     247         *
     248         * @since 4.6.0
     249         *
     250         * @param string $caption Caption for the given attachment.
     251         */
     252        echo apply_filters( 'the_post_thumbnail_caption', get_the_post_thumbnail_caption( $post ) );
     253}
  • src/wp-includes/post.php

    diff --git src/wp-includes/post.php src/wp-includes/post.php
    index 61f9018..aa15f72 100644
    function wp_get_attachment_url( $post_id = 0 ) { 
    49374937}
    49384938
    49394939/**
     4940 * Retrieves the 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        return apply_filters( 'wp_get_attachment_caption', $caption, $post->ID );
     4968}
     4969
     4970/**
    49404971 * Retrieve thumbnail for an attachment.
    49414972 *
    49424973 * @since 2.1.0
  • tests/phpunit/tests/media.php

    diff --git tests/phpunit/tests/media.php tests/phpunit/tests/media.php
    index 55311a5..7bd9f49 100644
    EOF; 
    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->assertEquals( '', 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

    diff --git tests/phpunit/tests/post/thumbnails.php tests/phpunit/tests/post/thumbnails.php
    index 822154f..ad232c1 100644
    class Tests_Post_Thumbnail_Template extends WP_UnitTestCase { 
    7474                $this->assertTrue( $WP_Query->thumbnails_cached );
    7575        }
    7676
     77        /**
     78         * @ticket 12235
     79         */
     80        function test_get_the_post_thumbnail_caption() {
     81                $this->assertEquals( '', 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                set_post_thumbnail( $post_id, $attachment_id );
     93
     94                $this->assertEquals( $caption, get_the_post_thumbnail_caption( $post_id ) );
     95        }
     96
     97        /**
     98         * @ticket 12235
     99         */
     100        function test_get_the_post_thumbnail_caption_empty() {
     101                $post_id = self::factory()->post->create();
     102                $attachment_id = self::factory()->attachment->create_object( 'image.jpg', $post_id, array(
     103                        'post_mime_type' => 'image/jpeg',
     104                        'post_type'      => 'attachment',
     105                        'post_excerpt'   => '',
     106                ) );
     107
     108                set_post_thumbnail( $post_id, $attachment_id );
     109
     110                $this->assertEquals( '', get_the_post_thumbnail_caption( $post_id ) );
     111        }
     112
     113        /**
     114         * @ticket 12235
     115         */
     116        function test_the_post_thumbnail_caption() {
     117                $caption = 'This is a caption.';
     118
     119                $post_id = self::factory()->post->create();
     120                $attachment_id = self::factory()->attachment->create_object( 'image.jpg', $post_id, array(
     121                        'post_mime_type' => 'image/jpeg',
     122                        'post_type'      => 'attachment',
     123                        'post_excerpt'   => $caption,
     124                ) );
     125
     126                set_post_thumbnail( $post_id, $attachment_id );
     127
     128                ob_start();
     129                the_post_thumbnail_caption( $post_id );
     130
     131                $this->assertEquals( $caption, ob_get_clean() );
     132        }
     133
    77134        function test_get_the_post_thumbnail() {
    78135                $this->assertEquals( '', get_the_post_thumbnail() );
    79136                $this->assertEquals( '', get_the_post_thumbnail( self::$post ) );