Skip to content

Commit f4f33f7

Browse files
committed
Customize: Use wp_check_filetype() instead of substr() to extract file extension.
This fixes compatibility with image files that have 4-character extensions, such as `.webp` and `.avif`. Follow-up to [57524], [50810], [30309]. Props buutqn, westonruter. See #51228, #35725, #21483. Fixes #64557. git-svn-id: https://develop.svn.wordpress.org/trunk@61576 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 3f0ee44 commit f4f33f7

File tree

2 files changed

+64
-2
lines changed

2 files changed

+64
-2
lines changed

src/wp-includes/customize/class-wp-customize-media-control.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,9 @@ public function to_json() {
9292
* Fake an attachment model - needs all fields used by template.
9393
* Note that the default value must be a URL, NOT an attachment ID.
9494
*/
95-
$ext = substr( $this->setting->default, -3 );
96-
$type = in_array( $ext, array( 'jpg', 'png', 'gif', 'bmp', 'webp', 'avif' ), true ) ? 'image' : 'document';
95+
$ext = wp_check_filetype( $this->setting->default )['ext'];
96+
$ext_types = wp_get_ext_types();
97+
$type = isset( $ext_types['image'] ) && in_array( $ext, $ext_types['image'], true ) ? 'image' : 'document';
9798

9899
$default_attachment = array(
99100
'id' => 1,
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
/**
3+
* Tests for the WP_Customize_Media_Control class.
4+
*
5+
* @coversDefaultClass WP_Customize_Media_Control
6+
*
7+
* @group customize
8+
*/
9+
class Test_WP_Customize_Media_Control extends WP_UnitTestCase {
10+
11+
/**
12+
* Set up.
13+
*/
14+
public function set_up() {
15+
parent::set_up();
16+
require_once ABSPATH . WPINC . '/class-wp-customize-manager.php';
17+
}
18+
19+
/**
20+
* @ticket 64557
21+
*
22+
* @covers ::to_json
23+
*/
24+
public function test_to_json() {
25+
$manager = new WP_Customize_Manager();
26+
27+
$manager->add_setting(
28+
'some_jpg',
29+
array(
30+
'default' => 'https://example.com/image.jpg',
31+
)
32+
);
33+
$manager->add_setting(
34+
'some_avif',
35+
array(
36+
'default' => 'https://example.com/image.avif',
37+
)
38+
);
39+
$manager->add_setting(
40+
'some_pdf',
41+
array(
42+
'default' => 'https://example.com/image.pdf',
43+
)
44+
);
45+
$manager->add_setting( 'no_default' );
46+
47+
$some_jpg_control = $manager->add_control( new WP_Customize_Media_Control( $manager, 'some_jpg' ) );
48+
$some_avif_control = $manager->add_control( new WP_Customize_Media_Control( $manager, 'some_avif' ) );
49+
$some_pdf_control = $manager->add_control( new WP_Customize_Media_Control( $manager, 'some_pdf' ) );
50+
$no_default_control = $manager->add_control( new WP_Customize_Media_Control( $manager, 'no_default' ) );
51+
52+
$some_jpg_control_json = $some_jpg_control->json();
53+
$some_avif_control_json = $some_avif_control->json();
54+
$some_pdf_control_json = $some_pdf_control->json();
55+
56+
$this->assertSame( 'image', $some_jpg_control_json['defaultAttachment']['type'] );
57+
$this->assertSame( 'image', $some_avif_control_json['defaultAttachment']['type'] );
58+
$this->assertSame( 'document', $some_pdf_control_json['defaultAttachment']['type'] );
59+
$this->assertArrayNotHasKey( 'defaultAttachment', $no_default_control->json() );
60+
}
61+
}

0 commit comments

Comments
 (0)