Skip to content

Commit ee01a8d

Browse files
authored
Merge pull request #393 from WordPress/fix/361-webp-image-mime-transforms-filter
Ensure incorrect usage of `webp_uploads_upload_image_mime_transforms` filter is treated correctly
2 parents 7352b80 + 2787cfc commit ee01a8d

2 files changed

Lines changed: 76 additions & 1 deletion

File tree

‎modules/images/webp-uploads/helper.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ function webp_uploads_get_upload_image_mime_transforms() {
3232
*
3333
* @param array $default_transforms A map with the valid mime transforms.
3434
*/
35-
$transforms = (array) apply_filters( 'webp_uploads_upload_image_mime_transforms', $default_transforms );
35+
$transforms = apply_filters( 'webp_uploads_upload_image_mime_transforms', $default_transforms );
3636

3737
// Return the default mime transforms if a non-array result is returned from the filter.
3838
if ( ! is_array( $transforms ) ) {

‎tests/modules/images/webp-uploads/helper-tests.php‎

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,4 +335,79 @@ function () {
335335
$this->assertSame( 'image_additional_generated_error', $result->get_error_code() );
336336
}
337337

338+
/**
339+
* Returns an empty array when the overwritten with empty array by webp_uploads_upload_image_mime_transforms filter.
340+
*
341+
* @test
342+
*/
343+
public function it_should_return_empty_array_when_filter_returns_empty_array() {
344+
add_filter( 'webp_uploads_upload_image_mime_transforms', '__return_empty_array' );
345+
346+
$transforms = webp_uploads_get_upload_image_mime_transforms();
347+
348+
$this->assertIsArray( $transforms );
349+
$this->assertSame( array(), $transforms );
350+
}
351+
352+
/**
353+
* Returns default transforms when the overwritten with non array type by webp_uploads_upload_image_mime_transforms filter.
354+
*
355+
* @test
356+
*/
357+
public function it_should_return_default_transforms_when_filter_returns_non_array_type() {
358+
add_filter(
359+
'webp_uploads_upload_image_mime_transforms',
360+
function () {
361+
return;
362+
}
363+
);
364+
365+
$default_transforms = array(
366+
'image/jpeg' => array( 'image/jpeg', 'image/webp' ),
367+
'image/webp' => array( 'image/webp', 'image/jpeg' ),
368+
);
369+
370+
$transforms = webp_uploads_get_upload_image_mime_transforms();
371+
372+
$this->assertIsArray( $transforms );
373+
$this->assertSame( $default_transforms, $transforms );
374+
}
375+
376+
/**
377+
* Returns transforms array with fallback to original mime with invalid transforms array.
378+
*
379+
* @test
380+
*/
381+
public function it_should_return_fallback_transforms_when_overwritten_invalid_transforms() {
382+
add_filter(
383+
'webp_uploads_upload_image_mime_transforms',
384+
function () {
385+
return array( 'image/jpeg' => array() );
386+
}
387+
);
388+
389+
$transforms = webp_uploads_get_upload_image_mime_transforms();
390+
391+
$this->assertIsArray( $transforms );
392+
$this->assertSame( array( 'image/jpeg' => array( 'image/jpeg' ) ), $transforms );
393+
}
394+
395+
/**
396+
* Returns custom transforms array when overwritten by webp_uploads_upload_image_mime_transforms filter.
397+
*
398+
* @test
399+
*/
400+
public function it_should_return_custom_transforms_when_overwritten_by_filter() {
401+
add_filter(
402+
'webp_uploads_upload_image_mime_transforms',
403+
function () {
404+
return array( 'image/jpeg' => array( 'image/jpeg', 'image/webp' ) );
405+
}
406+
);
407+
408+
$transforms = webp_uploads_get_upload_image_mime_transforms();
409+
410+
$this->assertIsArray( $transforms );
411+
$this->assertSame( array( 'image/jpeg' => array( 'image/jpeg', 'image/webp' ) ), $transforms );
412+
}
338413
}

0 commit comments

Comments
 (0)