Skip to content
This repository was archived by the owner on Apr 2, 2020. It is now read-only.
54 changes: 32 additions & 22 deletions inc/class-img-shortcode.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,37 +176,42 @@ 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 = '<img ';

$image_classes = explode( ' ', $attr['classes'] );
$image_classes[] = 'size-' . $attr['size'];
$image_classes[] = $attr['align'];
$image_classes_string = trim( implode( ' ', $image_classes ) );
unset( $attr['classes'] );

if ( isset( $attr['attachment'] ) ) {
$attr_for_responsive = $attr;
unset( $attr_for_responsive['caption'] );
unset( $attr_for_responsive['attachment'] );
unset( $attr_for_responsive['linkto'] );
unset( $attr_for_responsive['url'] );
unset( $attr_for_responsive['size'] );
unset( $attr_for_responsive['align'] );
unset( $attr_for_responsive['src'] );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect it'd be easier to array_intersect_key with a whitelist of allowed attributes than try to unset any potential attributes that wouldn't work here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@goldenapples Agreed, but we would nullify any new attributes added in img_shortcode_attrs. Kosher?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, yeah. true. I can't think offhand what custom attributes you'd want to enable on an image, but in case there are any, we should allow them to pass through. At the very least, you could clean up this logic using array_diff_key.


$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 = '<img ';

$image_attr = array(
'alt' => $attr['alt'],
'class' => trim( implode( ' ', $image_classes ) ),
);
$image_attr = array(
'alt' => $attr['alt'],
'class' => $image_classes_string,
);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the src value is missing from this array...


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'] );
} 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 ) . '" ';
foreach ( $image_attr as $attr_name => $attr_value ) {
if ( ! empty( $attr_value ) ) {
$image_html .= sanitize_key( $attr_name ) . '="' . esc_attr( $attr_value ) . '" ';
}
}
}

$image_html .= '/>';

$image_html .= '/>';
}
/**
* Filter the output of the <img> tag before wrapping it in link or caption
*
Expand All @@ -233,6 +238,11 @@ public static function callback( $attr, $_null, $shortcode_tag ) {
// If a caption is specified, wrap the image in the appropriat caption markup.
if ( ! empty( $attr['caption'] ) ) {

// We need to generate width for WP captions
if ( isset( $attr['attachment'] ) &&
$attachment = wp_get_attachment_image_src( (int) $attr['attachment'], $attr['size'] ) ) {
$image_attr['width'] = intval( $attachment[1] );
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd move this inside the ( empty( $attr['width'] ) ) conditional after it, so that the logic is easier to read.

// The WP caption element requires a width defined
if ( empty( $attr['width'] ) ) {
$attr['width'] = $image_attr['width'];
Expand Down