diff --git a/inc/class-img-shortcode.php b/inc/class-img-shortcode.php index 25f57ea..36bf499 100644 --- a/inc/class-img-shortcode.php +++ b/inc/class-img-shortcode.php @@ -176,37 +176,44 @@ public static function callback( $attr, $_null, $shortcode_tag ) { * @param array Shortcode attributes, decoded and merged with defaults. */ $attr = apply_filters( 'img_shortcode_attrs', $attr ); - - $image_html = ' null, + 'attachment' => null, + 'linkto' => null, + 'url' => null, + 'size' => null, + 'align' => null, + 'src' => null, + 'classes' => null, + ); + $attr_for_responsive = array_diff_key( $attr, $attrs_for_removal ); + $attr_for_responsive['class'] = $image_classes_string; + $image_html = wp_get_attachment_image( $attr['attachment'], $attr['size'], false, $attr_for_responsive ); + } else if ( ! empty( $attr['src'] ) ) { + $image_html = ' $attr['alt'], - 'class' => trim( implode( ' ', $image_classes ) ), - ); + $image_attr = array( + 'alt' => $attr['alt'], + 'class' => $image_classes_string, + 'src' => esc_url( $attr['src'] ), + ); - if ( isset( $attr['attachment'] ) && - $attachment = wp_get_attachment_image_src( (int) $attr['attachment'], $attr['size'] ) ) { - $image_attr['src'] = esc_url( $attachment[0] ); - $image_attr['width'] = intval( $attachment[1] ); - $image_attr['height'] = intval( $attachment[2] ); - } else if ( ! empty( $attr['src'] ) ) { - $image_attr['src'] = esc_url( $attr['src'] ); + foreach ( $image_attr as $attr_name => $attr_value ) { + if ( ! empty( $attr_value ) ) { + $image_html .= sanitize_key( $attr_name ) . '="' . esc_attr( $attr_value ) . '" '; + } + } + + $image_html .= '/>'; } else { return; // An image without a src isn't much of an image } - - foreach ( $image_attr as $attr_name => $attr_value ) { - if ( ! empty( $attr_value ) ) { - $image_html .= sanitize_key( $attr_name ) . '="' . esc_attr( $attr_value ) . '" '; - } - } - - $image_html .= '/>'; - /** * Filter the output of the tag before wrapping it in link or caption * @@ -234,8 +241,10 @@ public static function callback( $attr, $_null, $shortcode_tag ) { if ( ! empty( $attr['caption'] ) ) { // The WP caption element requires a width defined - if ( empty( $attr['width'] ) ) { - $attr['width'] = $image_attr['width']; + if ( empty( $attr['width'] ) && isset( $attr['attachment'] ) && + $attachment = wp_get_attachment_image_src( (int) $attr['attachment'], $attr['size'] ) + ) { + $attr['width'] = intval( $attachment[1] ); } $image_html = self::captionify( $image_html, $attr ); diff --git a/tests/test-img-shortcode.php b/tests/test-img-shortcode.php index 70b8d95..8cb3589 100644 --- a/tests/test-img-shortcode.php +++ b/tests/test-img-shortcode.php @@ -9,6 +9,7 @@ class Test_Img_Shortcode extends WP_UnitTestCase { // @codingStandardsIgnoreStart public function setUp() { parent::setUp(); + switch_theme('twentyfifteen'); $this->attachment_id = $this->insert_attachment( null, dirname( __FILE__ ) . '/data/fusion_image_placeholder_16x9_h2000.png', @@ -22,7 +23,6 @@ public function setUp() { ); $upload_dir = wp_upload_dir(); - $this->image_src = $upload_dir['url'] . '/fusion_image_placeholder_16x9_h2000.png'; $this->image_path = $upload_dir['path'] . '/fusion_image_placeholder_16x9_h2000.png'; } @@ -38,6 +38,10 @@ function test_construct_ui() { // replace this with some actual testing code $this->assertTrue( true ); } + function test_theme() { + // replace this with some actual testing code + $this->assertContains( 'Twenty Fifteen', wp_get_theme()->name ); + } /* @@ -79,6 +83,11 @@ function test_img_shortcode_from_attachment() { $expected_href_attr = get_permalink( $attachment_id ); $this->assertContains( 'href="' . $expected_href_attr . '"', $content ); + // Test srcset + $content = apply_filters( 'the_content', '[img attachment="' . $attachment_id . '" linkto="attachment" /]' ); + $srcset = 'srcset="http://example.org/wp-content/uploads/2015/12/fusion_image_placeholder_16x9_h2000-300x169.png 300w, http://example.org/wp-content/uploads/2015/12/fusion_image_placeholder_16x9_h2000-768x432.png 768w, http://example.org/wp-content/uploads/2015/12/fusion_image_placeholder_16x9_h2000-1024x576.png 1024w" sizes="(max-width: 2000px) 100vw, 2000px"'; + $this->assertContains( $srcset, $content ); + // Test caption attribute $caption = <<caption". It should contain HTML and markup.