Skip to content

Instantly share code, notes, and snippets.

@mitogh
Last active March 22, 2022 00:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mitogh/54ba6c96c8f83a5922951da4a4646f25 to your computer and use it in GitHub Desktop.
Save mitogh/54ba6c96c8f83a5922951da4a4646f25 to your computer and use it in GitHub Desktop.
Filter description

Usages examples

Filter: webp_uploads_img_url_mime_type

add_filter(
	'webp_uploads_img_url_mime_type',
	function ( $replacement, $attachment_id, $size_name, $mime_type, $image_tag ) {
		// Plugin is interested only on WebP.
		if ( $mime_type !== 'image/webp' ) {
			return $replacement;
		}

		$image = function_that_finds_the_webp_images_with_the_plugin_logic( $attachment_id );
		return $image[ $size_name ];
	},
	10,
	5
);

Filter: webp_uploads_file_path_additional_mime_type

add_filter(
	'webp_uploads_file_path_additional_mime_type',
	function( $image, $attachment_id, $size_name, $mime_type ){
		// Plugin is interested only on WebP.
		if ( $mime_type !== 'image/webp' ) {
			return $image;
		}

		// Image was already provided by other plugin
		if ( $image !== null ) {
			return $image;
		}

		// Logic to find the image from within the plugins.
		return array(
			'path' => '/tmp/location/image.webp',
			'file' => 'image.webp',
		);
	},
	10,
	5
);
The following patch does not include the updates to `webp_uploads_generate_additional_image_source` to
change the passed arguments from an array to a size name instead.
===================================================================
diff --git a/modules/images/webp-uploads/load.php b/modules/images/webp-uploads/load.php
--- a/modules/images/webp-uploads/load.php (revision 4596be0b1d3eb4faceb612b6f39d1f24c6b30dcc)
+++ b/modules/images/webp-uploads/load.php (date 1647907161419)
@@ -303,51 +303,65 @@
* @param string $destination_file_name The path where the file would be stored, including the extension. If empty, `generate_filename` is used to create the destination file name.
* @return array|WP_Error An array with the file and filesize if the image was created correctly otherwise a WP_Error
*/
-function webp_uploads_generate_additional_image_source( $attachment_id, array $size_data, $mime, $destination_file_name = null ) {
- $image_path = wp_get_original_image_path( $attachment_id );
+function webp_uploads_generate_additional_image_source( $attachment_id, $size_name, $mime, $destination_file_name = null ) {
+ $image = apply_filters( 'webp_uploads_file_path_additional_mime_type', null, $attachment_id, $size_name, $mime );
+ if ( $image === null ) {
+ $image_path = wp_get_original_image_path( $attachment_id );
- // File does not exist.
- if ( ! file_exists( $image_path ) ) {
- return new WP_Error( 'original_image_file_not_found', __( 'The original image file does not exists, subsizes are created out of the original image.', 'performance-lab' ) );
- }
+ // File does not exist.
+ if ( ! file_exists( $image_path ) ) {
+ return new WP_Error( 'original_image_file_not_found', __( 'The original image file does not exists, subsizes are created out of the original image.', 'performance-lab' ) );
+ }
- $editor = wp_get_image_editor( $image_path );
+ $editor = wp_get_image_editor( $image_path );
- if ( is_wp_error( $editor ) ) {
- return $editor;
- }
+ if ( is_wp_error( $editor ) ) {
+ return $editor;
+ }
- $allowed_mimes = array_flip( wp_get_mime_types() );
- if ( ! isset( $allowed_mimes[ $mime ] ) || ! is_string( $allowed_mimes[ $mime ] ) ) {
- return new WP_Error( 'image_mime_type_invalid', __( 'The provided mime type is not allowed.', 'performance-lab' ) );
- }
+ $allowed_mimes = array_flip( wp_get_mime_types() );
+ if ( ! isset( $allowed_mimes[ $mime ] ) || ! is_string( $allowed_mimes[ $mime ] ) ) {
+ return new WP_Error( 'image_mime_type_invalid', __( 'The provided mime type is not allowed.', 'performance-lab' ) );
+ }
- if ( ! wp_image_editor_supports( array( 'mime_type' => $mime ) ) ) {
- return new WP_Error( 'image_mime_type_not_supported', __( 'The provided mime type is not supported.', 'performance-lab' ) );
- }
+ if ( ! wp_image_editor_supports( array( 'mime_type' => $mime ) ) ) {
+ return new WP_Error( 'image_mime_type_not_supported', __( 'The provided mime type is not supported.', 'performance-lab' ) );
+ }
- $height = isset( $size_data['height'] ) ? (int) $size_data['height'] : 0;
- $width = isset( $size_data['width'] ) ? (int) $size_data['width'] : 0;
- $crop = isset( $size_data['crop'] ) && $size_data['crop'];
+ if ( $size_name === 'full' ) {
+ $metadata = wp_get_attachment_metadata( $attachment_id );
+ $height = $metadata['height'];
+ $width = $metadata['width'];
+ $crop = false;
+ } else {
+ $sizes = wp_get_registered_image_subsizes();
+ if ( empty( $sizes[ $size_name ] ) ) {
+ return new WP_Error( "The provided size name '{$size_name}' is not registered" );
+ }
+ $height = $sizes[ $size_name ]['height'];
+ $width = $sizes[ $size_name ]['width'];
+ $crop = $sizes[ $size_name ]['crop'];
+ }
- if ( $width <= 0 && $height <= 0 ) {
- return new WP_Error( 'image_wrong_dimensions', __( 'At least one of the dimensions must be a positive number.', 'performance-lab' ) );
- }
+ if ( $width <= 0 && $height <= 0 ) {
+ return new WP_Error( 'image_wrong_dimensions', __( 'At least one of the dimensions must be a positive number.', 'performance-lab' ) );
+ }
- $image_meta = wp_get_attachment_metadata( $attachment_id );
- // If stored EXIF data exists, rotate the source image before creating sub-sizes.
- if ( ! empty( $image_meta['image_meta'] ) ) {
- $editor->maybe_exif_rotate();
- }
+ $image_meta = wp_get_attachment_metadata( $attachment_id );
+ // If stored EXIF data exists, rotate the source image before creating sub-sizes.
+ if ( ! empty( $image_meta['image_meta'] ) ) {
+ $editor->maybe_exif_rotate();
+ }
- $editor->resize( $width, $height, $crop );
+ $editor->resize( $width, $height, $crop );
- if ( null === $destination_file_name ) {
- $extension = explode( '|', $allowed_mimes[ $mime ] );
- $destination_file_name = $editor->generate_filename( null, null, $extension[0] );
- }
+ if ( null === $destination_file_name ) {
+ $extension = explode( '|', $allowed_mimes[ $mime ] );
+ $destination_file_name = $editor->generate_filename( null, null, $extension[0] );
+ }
- $image = $editor->save( $destination_file_name, $mime );
+ $image = $editor->save( $destination_file_name, $mime );
+ }
if ( is_wp_error( $image ) ) {
return $image;
===================================================================
diff --git a/modules/images/webp-uploads/load.php b/modules/images/webp-uploads/load.php
--- a/modules/images/webp-uploads/load.php (revision 4596be0b1d3eb4faceb612b6f39d1f24c6b30dcc)
+++ b/modules/images/webp-uploads/load.php (date 1647907780272)
@@ -632,7 +632,8 @@
// Replace the full size image if present.
if ( isset( $metadata['sources'][ $target_mime ]['file'] ) && strpos( $url, $basename ) !== false ) {
- $image = str_replace( $src_filename, $metadata['sources'][ $target_mime ]['file'], $image );
+ $replacement = str_replace( $src_filename, $metadata['sources'][ $target_mime ]['file'], $image );
+ $image = apply_filters( 'webp_uploads_img_url_mime_type', $replacement, $attachment_id, 'full', $target_mime, $image );
continue;
}
@@ -662,7 +663,10 @@
continue;
}
- $image = str_replace( $src_filename, $size_data['sources'][ $target_mime ]['file'], $image );
+ $replacement = str_replace( $src_filename, $size_data['sources'][ $target_mime ]['file'], $image );
+ $image = apply_filters( 'webp_uploads_img_url_mime_type', $replacement, $attachment_id, $name, $target_mime, $image );
+
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment