Skip to content

Commit

Permalink
Fix download image rendering (#29)
Browse files Browse the repository at this point in the history
* Fix image rendering for older 'download' images

* Update changelog
  • Loading branch information
josephfusco authored Aug 13, 2020
1 parent ebb4810 commit 50319ba
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 1 deletion.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [2.6.2] - 2020-08-13

## Fixed

- Fixes a bug where images that were added to the WP Media Library prior to 2.6.0 were being rendered incorrectly.

## [2.6.1] - 2020-08-12

## Fixed
Expand Down
72 changes: 72 additions & 0 deletions lib/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,78 @@ public function fix_widen_attachment_url( $url, $attachment_id ) {
return $url;
}

/**
* Filters the widen image src result.
* This is needed to fix the doubling of the URLs.
*
* @param array|false $image Either array with src, width & height, icon src, or false.
* @param int $attachment_id Image attachment ID.
* @param string|array $size Size of image. Image size or array of width and height values
* (in that order). Default 'thumbnail'.
* @param bool $icon Whether the image should be treated as an icon. Default false.
*/
public function fix_widen_attachment_urls( $image, $attachment_id, $size, $icon ) {
$widen_media_id = get_post_meta( $attachment_id, 'widen_media_id', true );

// Check if this is an image from Widen.
if ( ! empty( $widen_media_id ) ) {
$image[0] = wp_get_attachment_url( $attachment_id );
}

return $image;
}

/**
* Filters the HTML content for the image tag.
*
* @since 2.6.0
*
* @param string $html HTML content for the image.
* @param int $id Attachment ID.
* @param string $alt Alternate text.
* @param string $title Attachment title.
* @param string $align Part of the class name for aligning the image.
* @param string|array $size Size of image. Image size or array of width and height values (in that order).
* Default 'medium'.
*
* @link https://developer.wordpress.org/reference/hooks/get_image_tag/
*/
public function filter_widen_image_tag( $html, $id, $alt, $title, $align, $size ): string {
$widen_media_id = get_post_meta( $id, 'widen_media_id', true );

// Do nothing special if this is not a Widen image.
if ( empty( $widen_media_id ) ) {
return $html;
}

list( $img_src, $width, $height ) = image_downsize( $id, $size );
$hwstring = image_hwstring( $width, $height );

$title = $title ? 'title="' . esc_attr( $title ) . '" ' : '';

$class = 'align' . esc_attr( $align ) . ' size-' . esc_attr( $size ) . ' wp-image-' . $id;

/**
* Filters the value of the attachment's image tag class attribute.
*
* @since 2.6.0
*
* @param string $class CSS class name or space-separated list of classes.
* @param int $id Attachment ID.
* @param string $align Part of the class name for aligning the image.
* @param string|array $size Size of image. Image size or array of width and height values (in that order).
* Default 'medium'.
*/
$class = apply_filters( 'get_image_tag_class', $class, $id, $align, $size );

// Get correct image src.
$img_src = wp_get_attachment_url( $id );

$html = '<img src="' . esc_attr( $img_src ) . '" alt="' . esc_attr( $alt ) . '" ' . $title . $hwstring . 'class="' . $class . '" />';

return $html;
}

/**
* Save a collection to the wp_collection post type.
* This is called via ajax.
Expand Down
2 changes: 2 additions & 0 deletions lib/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ private function define_admin_hooks(): void {

// Fix URL issues stemming from there being no actual file gets uploaded to WordPress.
$this->loader->add_filter( 'wp_get_attachment_url', $plugin_admin, 'fix_widen_attachment_url', 999, 2 );
$this->loader->add_filter( 'wp_get_attachment_image_src', $plugin_admin, 'fix_widen_attachment_urls', 10, 4 );
$this->loader->add_filter( 'get_image_tag', $plugin_admin, 'filter_widen_image_tag', 10, 6 );

// Prevent user from accessing the native 'add new' button for the WordPress Media Library.
$this->loader->add_action( 'admin_menu', $plugin_admin, 'hide_add_new_media_menu' );
Expand Down
2 changes: 1 addition & 1 deletion widen-media.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* Plugin Name: Widen Media
* Description: Search and add Widen digital assets to your WordPress media library.
* Version: 2.6.1
* Version: 2.6.2
* Author: Masonite
* Author URI: https://www.masonite.com/
* License: GPL-2.0+
Expand Down

0 comments on commit 50319ba

Please sign in to comment.