From c5aa076cb61c5841bfe8e705025df979d7f46119 Mon Sep 17 00:00:00 2001 From: Jonathan Williams Date: Fri, 9 Feb 2024 12:33:12 -0500 Subject: [PATCH 1/5] add a filter to manually add the sized back to the metadata, based on the current sizes returned by wp_get_registered_image_subsizes() --- src/filters.php | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/filters.php b/src/filters.php index 5729770..76e00aa 100644 --- a/src/filters.php +++ b/src/filters.php @@ -56,6 +56,38 @@ function( $orig_w, $orig_h ) { 6 ); + // Preemptively add the sizes to the attachment metadata. + add_filter( + 'wp_generate_attachment_metadata', + function( $metadata, $attachment_id ) { + // Get the registered image sizes. + $sizes = wp_get_registered_image_subsizes(); + + // Get the pathinfo for the original file. + $pathinfo = pathinfo( $metadata['file'] ); + + // Get the mime type for the original file. + $mime_type = get_post_mime_type( $attachment_id ); + + // Recalculate the sizes that would have been generated and add them to the metadata. + foreach ( $sizes as $size => $size_data ) { + // Calcualte the new filename by adding the size to the original filename using the WordPress convention. + $new_filename = $pathinfo['filename'] . '-' . $size_data['width'] . 'x' . $size_data['height'] . '.' . $pathinfo['extension']; + + // Add the new size to the metadata. + $metadata['sizes'][ $size ] = array( + 'file' => $new_filename, + 'width' => $size_data['width'], + 'height' => $size_data['height'], + 'mime-type' => $mime_type, + ); + } + return $metadata; + }, + 10, + 2 + ); + // We need to pass along the original prefilter value unaltered; we're not actually changing it, just using it as a hook for the resize filter. return $file; } From 4373b6a1cc34a72af1cc2ea796be71a32160d49e Mon Sep 17 00:00:00 2001 From: Jonathan Williams Date: Fri, 9 Feb 2024 13:29:48 -0500 Subject: [PATCH 2/5] move function from anonymous to standalone and add commentary --- src/filters.php | 81 +++++++++++++++++++++++++++++++++---------------- 1 file changed, 55 insertions(+), 26 deletions(-) diff --git a/src/filters.php b/src/filters.php index 76e00aa..e06e954 100644 --- a/src/filters.php +++ b/src/filters.php @@ -1,6 +1,16 @@ $size_data ) { - // Calcualte the new filename by adding the size to the original filename using the WordPress convention. - $new_filename = $pathinfo['filename'] . '-' . $size_data['width'] . 'x' . $size_data['height'] . '.' . $pathinfo['extension']; - - // Add the new size to the metadata. - $metadata['sizes'][ $size ] = array( - 'file' => $new_filename, - 'width' => $size_data['width'], - 'height' => $size_data['height'], - 'mime-type' => $mime_type, - ); - } - return $metadata; - }, + __NAMESPACE__ . '\generate_metadata_sizes', 10, 2 ); @@ -93,6 +80,48 @@ function( $metadata, $attachment_id ) { } ); +/** + * Generate metadata for image sizes without creating the actual resized images. + * + * This function gets the registered image sizes and calculates the filename for each size + * using the WordPress convention for size annotation. It then adds this information to the attachment metadata. + * + * This function is designed to be added to the wp_handle_upload_prefilter in order to restore the metadata that + * would have been generated. Becuase the sizes are being suppressed on upload, the actual resized images + * are not being created, but we still need to add the metadata for the sizes to the attachment. This function + * should have the effect of pre-generating the metadata for the sizes that would have been created. + * + * @param array $metadata The attachment metadata. + * @param int $attachment_id The ID of the attachment. + * + * @return array The modified attachment metadata, with added sizes. + */ +function generate_metadata_sizes( $metadata, $attachment_id ) { + // Get the registered image sizes. + $sizes = wp_get_registered_image_subsizes(); + + // Get the pathinfo for the original file. + $pathinfo = pathinfo( $metadata['file'] ); + + // Get the mime type for the original file. + $mime_type = get_post_mime_type( $attachment_id ); + + // Recalculate the sizes that would have been generated and add them to the metadata. + foreach ( $sizes as $size => $size_data ) { + // Calcualte the new filename by adding the size to the original filename using the WordPress convention. + $new_filename = $pathinfo['filename'] . '-' . $size_data['width'] . 'x' . $size_data['height'] . '.' . $pathinfo['extension']; + + // Add the new size to the metadata. + $metadata['sizes'][ $size ] = array( + 'file' => $new_filename, + 'width' => $size_data['width'], + 'height' => $size_data['height'], + 'mime-type' => $mime_type, + ); + } + return $metadata; +} + // Disable the big image threshold, we don't want WordPress to do any resizing at all. add_filter( 'big_image_size_threshold', '__return_false' ); From 14dd6de39fb35fbe5a0698cb67dd0f43e564e03e Mon Sep 17 00:00:00 2001 From: Jonathan Williams Date: Fri, 9 Feb 2024 13:37:28 -0500 Subject: [PATCH 3/5] :pencil: update readme --- readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/readme.md b/readme.md index 35cce17..326a6c6 100644 --- a/readme.md +++ b/readme.md @@ -6,7 +6,7 @@ BU Media S3 is a WordPress plugin designed to work with the [Human Made S3 Uploa - **S3 Upload Directory**: The plugin filters the `upload_dir` hook and rewrites the values in a way that is compatible with the S3 Uploads plugin and the BU Protected S3 Object Lambda stack, redirecting all media uploads to the S3 bucket. It also changes the default upload location from `wp-content/uploads` to `files`, which is the convention for BU WordPress sites. -- **Prevent Image Scaling**: By default, WordPress generates a scaled derivative for every image size that is defined for the current site. Because the BU Protected S3 Object Lambda stack handles image resizing automatically, the WordPress media library only needs to handle the full sized original upload. This plugin tells WordPress not to generate any derivative sizes on upload. +- **Prevent Image Scaling**: By default, WordPress generates a scaled derivative for every image size that is defined for the current site. Because the BU Protected S3 Object Lambda stack handles image resizing automatically, the WordPress media library only needs to handle the full sized original upload. This plugin tells WordPress not to generate any derivative sizes on upload. It preserves the attachment metadata for all of the defined sizes by generating it directly during the upload process. - **Suppress Big Image Threshold Resizing**: WordPress 5.3 introduced a new feature that automatically resizes images for "web-ready" dimensions. This plugin suppresses this feature, allowing you to upload images at their full size. From 3a520a600e70039a926d2caff659b279b202c431 Mon Sep 17 00:00:00 2001 From: Jonathan Williams Date: Wed, 14 Feb 2024 12:09:06 -0500 Subject: [PATCH 4/5] :pencil: add comment --- bu-media-s3.php | 1 + 1 file changed, 1 insertion(+) diff --git a/bu-media-s3.php b/bu-media-s3.php index 6876f6d..77ba855 100644 --- a/bu-media-s3.php +++ b/bu-media-s3.php @@ -15,6 +15,7 @@ require_once dirname( __FILE__ ) . '/src/s3-assets.php'; require_once dirname( __FILE__ ) . '/src/filters.php'; +// Load the WP-CLI commands if we're running in a CLI environment. if ( defined( 'WP_CLI' ) && WP_CLI ) { require_once dirname( __FILE__ ) . '/src/wp-cli-commands.php'; } From 489858f44b7027ef4c03c0ca37e0a14ec16aa799 Mon Sep 17 00:00:00 2001 From: Jonathan Williams Date: Wed, 14 Feb 2024 12:10:33 -0500 Subject: [PATCH 5/5] :bookmark: version bump --- bu-media-s3.php | 2 +- package-lock.json | 2 +- package.json | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/bu-media-s3.php b/bu-media-s3.php index 77ba855..1e1469b 100644 --- a/bu-media-s3.php +++ b/bu-media-s3.php @@ -3,7 +3,7 @@ * Plugin Name: bu-media-s3 * Plugin URI: https://github.com/bu-ist/bu-media-s3 * Description: A plugin for integrating S3 with WordPress - * Version: 0.1 + * Version: 1.0.0 * Author: Boston University * Author URI: https://developer.bu.edu/ * diff --git a/package-lock.json b/package-lock.json index 4921b37..0741382 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "bu-media-s3", - "version": "0.1.0", + "version": "1.0.0", "lockfileVersion": 3, "requires": true, "packages": { diff --git a/package.json b/package.json index 6eac4f5..0c3261c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "bu-media-s3", - "version": "0.1.0", + "version": "1.0.0", "description": "Integrations between WordPress and AWS S3", "main": "index.js", "scripts": {