Skip to content

Commit 8946881

Browse files
Media: Expose image_strip_meta and image_max_bit_depth in the REST index.
When client-side media processing is active, image decoding, scaling, and encoding happen in the browser, so no server-side `WP_Image_Editor` is instantiated. As a result the `image_strip_meta` and `image_max_bit_depth` filters never influence generated images, and plugins relying on them have no effect on client-generated sub-sizes. Expose the filtered values of both hooks on the REST API index inside the existing client-side media processing block in `WP_REST_Server::get_index()`, alongside the already-exported `image_size_threshold`, so the client encoder can honor them. The client-side consumption of these values ships via the Gutenberg package updates in WordPress/gutenberg#80218. Props westonruter, soyebsalar01. Fixes #65623. git-svn-id: https://develop.svn.wordpress.org/trunk@62806 602fd350-edb4-49c9-b593-d223f7449a82
1 parent e26ca28 commit 8946881

3 files changed

Lines changed: 86 additions & 0 deletions

File tree

src/wp-includes/rest-api/class-wp-rest-server.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1384,6 +1384,20 @@ public function get_index( $request ) {
13841384

13851385
/** This filter is documented in wp-admin/includes/image.php */
13861386
$available['image_size_threshold'] = (int) apply_filters( 'big_image_size_threshold', 2560, array( 0, 0 ), '', 0 );
1387+
1388+
/** This filter is documented in wp-includes/class-wp-image-editor-imagick.php */
1389+
$available['image_strip_meta'] = (bool) apply_filters( 'image_strip_meta', true );
1390+
1391+
/*
1392+
* On the server, this filter receives the decoded image's actual bit depth.
1393+
* The client path never decodes the image on the server, so the filter is
1394+
* applied with 16 (the maximum depth the client encoder can produce) as
1395+
* both the value and the current depth. The client caps its output bit
1396+
* depth at the filtered value, so a plugin lowering it (e.g. to 8) takes
1397+
* effect on client-generated images too.
1398+
*/
1399+
/** This filter is documented in wp-includes/class-wp-image-editor-imagick.php */
1400+
$available['image_max_bit_depth'] = (int) apply_filters( 'image_max_bit_depth', 16, 16 );
13871401
}
13881402

13891403
$response = new WP_REST_Response( $available );

tests/phpunit/tests/rest-api/rest-server.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1207,6 +1207,76 @@ public function test_get_index() {
12071207
$this->assertArrayHasKey( 'site_icon_url', $data );
12081208
}
12091209

1210+
/**
1211+
* @ticket 64804
1212+
*
1213+
* @covers WP_REST_Server::get_index
1214+
*/
1215+
public function test_get_index_should_include_media_processing_settings(): void {
1216+
$user_id = self::factory()->user->create( array( 'role' => 'administrator' ) );
1217+
$this->assertIsInt( $user_id );
1218+
wp_set_current_user( $user_id );
1219+
add_filter( 'wp_client_side_media_processing_enabled', '__return_true' );
1220+
1221+
$server = new WP_REST_Server();
1222+
$request = new WP_REST_Request( 'GET', '/' );
1223+
$index = $server->dispatch( $request );
1224+
$data = $index->get_data();
1225+
$this->assertIsArray( $data );
1226+
1227+
$this->assertArrayHasKey( 'image_sizes', $data );
1228+
$this->assertArrayHasKey( 'image_size_threshold', $data );
1229+
$this->assertArrayHasKey( 'image_strip_meta', $data );
1230+
$this->assertTrue( $data['image_strip_meta'] );
1231+
$this->assertArrayHasKey( 'image_max_bit_depth', $data );
1232+
$this->assertSame( 16, $data['image_max_bit_depth'] );
1233+
}
1234+
1235+
/**
1236+
* @ticket 64804
1237+
*
1238+
* @covers WP_REST_Server::get_index
1239+
*/
1240+
public function test_get_index_should_not_include_media_processing_settings_without_caps(): void {
1241+
add_filter( 'wp_client_side_media_processing_enabled', '__return_true' );
1242+
1243+
$server = new WP_REST_Server();
1244+
$request = new WP_REST_Request( 'GET', '/' );
1245+
$index = $server->dispatch( $request );
1246+
$data = $index->get_data();
1247+
$this->assertIsArray( $data );
1248+
1249+
$this->assertArrayNotHasKey( 'image_sizes', $data );
1250+
$this->assertArrayNotHasKey( 'image_size_threshold', $data );
1251+
$this->assertArrayNotHasKey( 'image_strip_meta', $data );
1252+
$this->assertArrayNotHasKey( 'image_max_bit_depth', $data );
1253+
}
1254+
1255+
/**
1256+
* @ticket 64804
1257+
*
1258+
* @covers WP_REST_Server::get_index
1259+
*/
1260+
public function test_get_index_should_honor_media_processing_filters(): void {
1261+
$user_id = self::factory()->user->create( array( 'role' => 'administrator' ) );
1262+
$this->assertIsInt( $user_id );
1263+
wp_set_current_user( $user_id );
1264+
add_filter( 'wp_client_side_media_processing_enabled', '__return_true' );
1265+
add_filter( 'image_strip_meta', '__return_false' );
1266+
add_filter(
1267+
'image_max_bit_depth',
1268+
static fn ( int $max_depth ) => min( 8, $max_depth )
1269+
);
1270+
1271+
$server = new WP_REST_Server();
1272+
$request = new WP_REST_Request( 'GET', '/' );
1273+
$index = $server->dispatch( $request );
1274+
$data = $index->get_data();
1275+
1276+
$this->assertFalse( $data['image_strip_meta'] );
1277+
$this->assertSame( 8, $data['image_max_bit_depth'] );
1278+
}
1279+
12101280
/**
12111281
* @ticket 57902
12121282
*

tests/qunit/fixtures/wp-api-generated.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13061,6 +13061,8 @@ mockedApiResponse.Schema = {
1306113061
}
1306213062
},
1306313063
"image_size_threshold": 2560,
13064+
"image_strip_meta": true,
13065+
"image_max_bit_depth": 16,
1306413066
"site_logo": 0,
1306513067
"site_icon": 0,
1306613068
"site_icon_url": ""

0 commit comments

Comments
 (0)