From 50319baf21e16cf52cfea1ce2aa2e9d51918f6d9 Mon Sep 17 00:00:00 2001 From: Joseph Fusco Date: Thu, 13 Aug 2020 16:41:09 -0400 Subject: [PATCH] Fix download image rendering (#29) * Fix image rendering for older 'download' images * Update changelog --- CHANGELOG.md | 6 +++++ lib/Admin.php | 72 +++++++++++++++++++++++++++++++++++++++++++++++++ lib/Plugin.php | 2 ++ widen-media.php | 2 +- 4 files changed, 81 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bc62e98..40891ff 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/Admin.php b/lib/Admin.php index 101c291..0e31e2c 100644 --- a/lib/Admin.php +++ b/lib/Admin.php @@ -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 = '' . esc_attr( $alt ) . ''; + + return $html; + } + /** * Save a collection to the wp_collection post type. * This is called via ajax. diff --git a/lib/Plugin.php b/lib/Plugin.php index 9680786..8913f06 100644 --- a/lib/Plugin.php +++ b/lib/Plugin.php @@ -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' ); diff --git a/widen-media.php b/widen-media.php index 59a9318..de0f379 100644 --- a/widen-media.php +++ b/widen-media.php @@ -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+