Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add WebP for uploads module #32

Merged
merged 21 commits into from
Dec 7, 2021
Merged
Changes from 6 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
2cdbd52
Add a webp-default module, first pass
adamsilverstein Dec 2, 2021
087fb18
Fixes for phpcs
adamsilverstein Dec 2, 2021
1d613b8
separate file doc header to please phpcs
adamsilverstein Dec 2, 2021
6c434c1
Move functionality to load.php
adamsilverstein Dec 3, 2021
9429e06
Revert temporary changes to root load.php
adamsilverstein Dec 3, 2021
f0e8fec
update description
adamsilverstein Dec 3, 2021
4c17af6
Merge branch 'trunk' into add/webp-default-module
adamsilverstein Dec 3, 2021
ad35694
Add test validating filter added as expected
adamsilverstein Dec 3, 2021
336da5e
Formatting for phpcs
adamsilverstein Dec 3, 2021
fe536d2
move cap check into folter callback.
adamsilverstein Dec 3, 2021
2620b57
Cleanup
adamsilverstein Dec 3, 2021
aa753c0
Add tests that mock WebP support enabled/disabled
adamsilverstein Dec 3, 2021
f2cc640
Remove `.` after name
adamsilverstein Dec 3, 2021
4b60267
move testdata into module folder
adamsilverstein Dec 3, 2021
da4a30c
Simplify WebP Uploads module header to only contain module name once …
felixarntz Dec 6, 2021
5133658
Merge branch 'trunk' into add/webp-default-module
felixarntz Dec 6, 2021
8ad877e
Update tests/modules/webp-uploads/webp-uploads-test.php
adamsilverstein Dec 7, 2021
b1e0a67
Update tests/modules/webp-uploads/webp-uploads-test.php
adamsilverstein Dec 7, 2021
45ad216
Update tests/modules/webp-uploads/webp-uploads-test.php
adamsilverstein Dec 7, 2021
331e540
Update tests/modules/webp-uploads/webp-uploads-test.php
adamsilverstein Dec 7, 2021
f8c9e2f
remove unnecessary filter cleanup
adamsilverstein Dec 7, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions modules/webp-uploads/load.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
/**
* WebP-Uploads Module
*
* @package performance-lab
* @since 1.0.0
*
* Module Name: WebP uploads.
adamsilverstein marked this conversation as resolved.
Show resolved Hide resolved
* Description: Uses WebP as the default format for new JPEG image uploads if the server supports it.
* Focus: images
* Experimental: No
*/

// Only enable if the server supports WebP.
if ( wp_image_editor_supports( array( 'mime_type' => 'image/webp' ) ) ) {
adamsilverstein marked this conversation as resolved.
Show resolved Hide resolved
add_filter( 'image_editor_output_format', 'webp_default_filter_image_editor_output_format', 10, 3 );
}

/**
* Filter the image editor default output format mapping.
*
* For uploaded JPEG images, map the default output format to WebP.
*
* @since 1.0.0
*
* @param string $output_format The image editor default output format mapping.
* @param string $filename Path to the image.
* @param string $mime_type The source image mime type.
* @return string The new output format mapping.
*/
function webp_default_filter_image_editor_output_format( $output_format, $filename, $mime_type ) {
adamsilverstein marked this conversation as resolved.
Show resolved Hide resolved

// WebP lossless support is still limited on servers, so only apply to JPEGs.
if ( 'image/jpeg' !== $mime_type ) {
return $output_format;
}

$output_format['image/jpeg'] = 'image/webp';

return $output_format;
}