Make WordPress Core

Ticket #27292: 27292.3.diff

File 27292.3.diff, 2.9 KB (added by jond3r, 12 years ago)

New focus: Restore the use of img_caption_shortcode_width filter for HTML5

  • src/wp-includes/media.php

     
    765765        if ( ! empty( $atts['id'] ) )
    766766                $atts['id'] = 'id="' . esc_attr( $atts['id'] ) . '" ';
    767767
    768         $class = trim( 'wp-caption ' . $atts['align'] . ' ' . $atts['class'] );
     768        $caption_width = $atts['width'];
     769        $html5 = current_theme_supports( 'html5', 'caption' );
    769770
    770         if ( current_theme_supports( 'html5', 'caption' ) ) {
    771                 return '<figure ' . $atts['id'] . 'style="width: ' . (int) $atts['width'] . 'px;" class="' . esc_attr( $class ) . '">'
    772                 . do_shortcode( $content ) . '<figcaption class="wp-caption-text">' . $atts['caption'] . '</figcaption></figure>';
     771        if ( ! $html5 ) {
     772                $caption_width += 10;
    773773        }
    774774
    775         $caption_width = 10 + $atts['width'];
    776 
    777775        /**
    778776         * Filter the width of an image's caption.
    779777         *
     
    795793        if ( $caption_width )
    796794                $style = 'style="width: ' . (int) $caption_width . 'px" ';
    797795
    798         return '<div ' . $atts['id'] . $style . 'class="' . esc_attr( $class ) . '">'
    799         . do_shortcode( $content ) . '<p class="wp-caption-text">' . $atts['caption'] . '</p></div>';
     796        $class = trim( 'wp-caption ' . $atts['align'] . ' ' . $atts['class'] );
     797
     798        $itemtag = $html5 ? 'figure' : 'div';
     799        $captiontag = $html5 ? 'figcaption' : 'p';
     800       
     801        return "<$itemtag " . $atts['id'] . $style . 'class="' . esc_attr( $class ) . '">'
     802        . do_shortcode( $content ) . "<$captiontag " . 'class="wp-caption-text">' . $atts['caption'] . "</$captiontag></$itemtag>";
    800803}
    801804
    802805add_shortcode('gallery', 'gallery_shortcode');
  • tests/phpunit/tests/media.php

     
    108108                $this->assertEquals( 1, preg_match_all( "~wp-caption-text.*{$content_preg}~", $result, $_r ) );
    109109        }
    110110
     111        /**
     112         * @ticket 27292
     113         */
     114        function test_new_img_caption_shortcode_new_format_html5() {
     115                add_theme_support( 'html5', array( 'caption' ) );
     116                add_filter( 'img_caption_shortcode_width', array( $this, 'filter_img_caption_shortcode_width' ) );
     117
     118                $result = img_caption_shortcode(
     119                        array( 'width' => 20 ),
     120                        $this->img_content . $this->html_content
     121                );
     122
     123                $expected = '<figure style="width: 30px" class="wp-caption alignnone">' . $this->img_content .
     124                                                '<figcaption class="wp-caption-text">' . $this->html_content . '</figcaption></figure>';
     125                $this->assertEquals( $expected, $result );
     126
     127                remove_filter( 'img_caption_shortcode_width', array( $this, 'filter_img_caption_shortcode_width' ) );
     128                remove_theme_support( 'html5' );
     129        }
     130
     131        function filter_img_caption_shortcode_width( $caption_width ) {
     132                return 30;
     133        }
     134
    111135        function test_add_remove_oembed_provider() {
    112136                wp_oembed_add_provider( 'http://foo.bar/*', 'http://foo.bar/oembed' );
    113137                $this->assertTrue( wp_oembed_remove_provider( 'http://foo.bar/*' ) );