PluginProbe
Gutenberg / 22.7.0
Gutenberg v22.7.0
24.0.0 23.9.1 23.9.0 23.8.0 23.7.2 23.7.1 23.7.0 23.6.1 23.6.2 23.6.0 23.5.3 23.5.2 23.5.1 23.5.0 23.4.0 23.3.2 23.3.1 23.3.0 23.2.0 23.2.1 23.2.2 23.1.1 23.1.0 23.0.1 12.6.0 All 403 releases
← All changes | lib/media/load.php +99 -184 23.4.022.7.0 View file →
@@ -1,18 +1,8 @@
1 1 <?php
2 2 /**
3 3 * Adds media-related functionality for client-side media processing.
4 4 *
5 - * This file is structured in two tiers:
6 - *
7 - * 1. HEIC infrastructure — loaded whenever the feature filter is enabled.
8 - * Browsers like Safari can decode HEIC via createImageBitmap() even
9 - * without VIPS/SharedArrayBuffer, so HEIC MIME types, the custom REST
10 - * controller, and REST field/index registrations are always needed.
11 - *
12 - * 2. Full VIPS/WASM processing — loaded only when the feature filter is
13 - * enabled AND requires cross-origin isolation (DIP) at runtime.
14 - *
15 5 * @package gutenberg
16 6 */
17 7
18 8 if ( ! gutenberg_is_client_side_media_processing_enabled() ) {
@@ -18,29 +8,100 @@
18 8 if ( ! gutenberg_is_client_side_media_processing_enabled() ) {
19 9 return;
20 10 }
21 11
22 -// ── Tier 1: HEIC infrastructure (always loaded) ─────────────────────
12 +/**
13 + * Sets a global JS variable to indicate that client-side media processing is enabled.
14 + */
15 +function gutenberg_set_client_side_media_processing_flag() {
16 + if ( ! gutenberg_is_client_side_media_processing_enabled() ) {
17 + return;
18 + }
19 + wp_add_inline_script( 'wp-block-editor', 'window.__clientSideMediaProcessing = true', 'before' );
20 +}
21 +add_action( 'admin_init', 'gutenberg_set_client_side_media_processing_flag' );
23 22
24 23 /**
25 - * Registers HEIC/HEIF as allowed upload MIME types.
24 + * Returns a list of all available image sizes.
26 25 *
27 - * HEIC images can be decoded in the browser (via canvas/VideoDecoder).
28 - * Registering these MIME types ensures the file picker's accept attribute
29 - * includes them, preventing macOS from silently converting HEIC to JPEG
30 - * on selection.
26 + * @return array Existing image sizes.
27 + */
28 +function gutenberg_get_all_image_sizes(): array {
29 + $sizes = wp_get_registered_image_subsizes();
30 +
31 + foreach ( $sizes as $name => &$size ) {
32 + $size['height'] = (int) $size['height'];
33 + $size['width'] = (int) $size['width'];
34 + $size['name'] = $name;
35 + }
36 + unset( $size );
37 +
38 + return $sizes;
39 +}
40 +
41 +/**
42 + * Returns the default output format mapping for the supported image formats.
31 43 *
32 - * @param array $mimes Allowed MIME types (extension => type).
33 - * @return array Modified MIME types.
44 + * @return array<string,string> Map of input formats to output formats.
34 45 */
35 -function gutenberg_add_heic_upload_mimes( array $mimes ): array {
36 - $mimes['heic'] = 'image/heic';
37 - $mimes['heif'] = 'image/heif';
38 - return $mimes;
46 +function gutenberg_get_default_image_output_formats() {
47 + $input_formats = array(
48 + 'image/jpeg',
49 + 'image/png',
50 + 'image/gif',
51 + 'image/webp',
52 + 'image/avif',
53 + 'image/heic',
54 + );
55 +
56 + $output_formats = array();
57 +
58 + foreach ( $input_formats as $mime_type ) {
59 + /** This filter is documented in wp-includes/media.php */
60 + $output_formats = apply_filters(
61 + 'image_editor_output_format', // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
62 + $output_formats,
63 + '',
64 + $mime_type
65 + );
66 + }
67 +
68 + return $output_formats;
39 69 }
40 70
41 -add_filter( 'upload_mimes', 'gutenberg_add_heic_upload_mimes' );
71 +/**
72 + * Filters the REST API root index data to add custom settings.
73 + *
74 + * @param WP_REST_Response $response Response data.
75 + */
76 +function gutenberg_media_processing_filter_rest_index( WP_REST_Response $response ) {
77 + /** This filter is documented in wp-admin/includes/images.php */
78 + $image_size_threshold = (int) apply_filters( 'big_image_size_threshold', 2560, array( 0, 0 ), '', 0 ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
42 79
80 + $default_image_output_formats = gutenberg_get_default_image_output_formats();
81 +
82 + /** This filter is documented in wp-includes/class-wp-image-editor-imagick.php */
83 + $jpeg_interlaced = (bool) apply_filters( 'image_save_progressive', false, 'image/jpeg' ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
84 + /** This filter is documented in wp-includes/class-wp-image-editor-imagick.php */
85 + $png_interlaced = (bool) apply_filters( 'image_save_progressive', false, 'image/png' ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
86 + /** This filter is documented in wp-includes/class-wp-image-editor-imagick.php */
87 + $gif_interlaced = (bool) apply_filters( 'image_save_progressive', false, 'image/gif' ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
88 +
89 + if ( current_user_can( 'upload_files' ) ) {
90 + $response->data['image_sizes'] = gutenberg_get_all_image_sizes();
91 + $response->data['image_size_threshold'] = $image_size_threshold;
92 + $response->data['image_output_formats'] = (object) $default_image_output_formats;
93 + $response->data['jpeg_interlaced'] = $jpeg_interlaced;
94 + $response->data['png_interlaced'] = $png_interlaced;
95 + $response->data['gif_interlaced'] = $gif_interlaced;
96 + }
97 +
98 + return $response;
99 +}
100 +
101 +add_filter( 'rest_index', 'gutenberg_media_processing_filter_rest_index' );
102 +
103 +
43 104 /**
44 105 * Overrides the REST controller for the attachment post type.
45 106 *
46 107 * @param array $args Array of arguments for registering a post type.
@@ -58,8 +119,9 @@
58 119 }
59 120
60 121 add_filter( 'register_post_type_args', 'gutenberg_filter_attachment_post_type_args', 10, 2 );
61 122
123 +
62 124 /**
63 125 * Registers additional REST fields for attachments.
64 126 */
65 127 function gutenberg_media_processing_register_rest_fields(): void {
@@ -139,105 +201,8 @@
139 201 return null;
140 202 }
141 203
142 204 /**
143 - * Returns a list of all available image sizes.
144 - *
145 - * @return array Existing image sizes.
146 - */
147 -function gutenberg_get_all_image_sizes(): array {
148 - $sizes = wp_get_registered_image_subsizes();
149 -
150 - foreach ( $sizes as $name => &$size ) {
151 - $size['height'] = (int) $size['height'];
152 - $size['width'] = (int) $size['width'];
153 - $size['name'] = $name;
154 - }
155 - unset( $size );
156 -
157 - return $sizes;
158 -}
159 -
160 -/**
161 - * Filters the REST API root index data to add custom settings.
162 - *
163 - * @param WP_REST_Response $response Response data.
164 - */
165 -function gutenberg_media_processing_filter_rest_index( WP_REST_Response $response ) {
166 - /** This filter is documented in wp-admin/includes/image.php */
167 - $image_size_threshold = (int) apply_filters( 'big_image_size_threshold', 2560, array( 0, 0 ), '', 0 ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
168 -
169 - if ( current_user_can( 'upload_files' ) ) {
170 - $response->data['image_sizes'] = gutenberg_get_all_image_sizes();
171 - $response->data['image_size_threshold'] = $image_size_threshold;
172 - }
173 -
174 - return $response;
175 -}
176 -
177 -add_filter( 'rest_index', 'gutenberg_media_processing_filter_rest_index' );
178 -
179 -/**
180 - * Sets a global JS variable to indicate that HEIC canvas-based upload support is available.
181 - *
182 - * This flag is set whenever the media processing feature is enabled,
183 - * regardless of whether the browser supports full VIPS-based processing.
184 - * Browsers like Safari can use createImageBitmap() to decode HEIC images
185 - * and convert them to JPEG for server-side sub-size generation.
186 - */
187 -function gutenberg_set_heic_upload_support_flag() {
188 - wp_add_inline_script( 'wp-block-editor', 'window.__heicUploadSupport = true', 'before' );
189 -}
190 -add_action( 'admin_init', 'gutenberg_set_heic_upload_support_flag' );
191 -
192 -/**
193 - * Deletes the HEIC companion file when its attachment is deleted.
194 - *
195 - * The HEIC is sideloaded alongside a JPEG derivative and recorded in
196 - * $metadata['original']. WordPress core's wp_delete_attachment_files()
197 - * only knows about 'original_image', so without this hook the HEIC
198 - * would linger on disk after the attachment is deleted.
199 - *
200 - * @param int $post_id Attachment ID being deleted.
201 - */
202 -function gutenberg_delete_heic_companion_file( int $post_id ): void {
203 - $metadata = wp_get_attachment_metadata( $post_id, true );
204 -
205 - if ( empty( $metadata['original'] ) || ! is_string( $metadata['original'] ) ) {
206 - return;
207 - }
208 -
209 - $attached_file = get_attached_file( $post_id, true );
210 -
211 - if ( ! $attached_file ) {
212 - return;
213 - }
214 -
215 - $heic_path = path_join( dirname( $attached_file ), $metadata['original'] );
216 -
217 - if ( file_exists( $heic_path ) ) {
218 - wp_delete_file( $heic_path );
219 - }
220 -}
221 -
222 -add_action( 'delete_attachment', 'gutenberg_delete_heic_companion_file' );
223 -
224 -// ── Tier 2: Full client-side processing (VIPS/WASM) ─────────────────
225 -// Everything below requires cross-origin isolation (Document-Isolation-Policy)
226 -// and SharedArrayBuffer support, which is only available in Chromium 137+.
227 -
228 -/**
229 - * Sets a global JS variable to indicate that client-side media processing is enabled.
230 - */
231 -function gutenberg_set_client_side_media_processing_flag() {
232 - if ( ! gutenberg_is_client_side_media_processing_enabled() ) {
233 - return;
234 - }
235 - wp_add_inline_script( 'wp-block-editor', 'window.__clientSideMediaProcessing = true', 'before' );
236 -}
237 -add_action( 'admin_init', 'gutenberg_set_client_side_media_processing_flag' );
238 -
239 -/**
240 205 * Filters the list of rewrite rules formatted for output to an .htaccess file.
241 206 *
242 207 * Adds support for serving wasm-vips locally.
243 208 *
@@ -254,30 +219,14 @@
254 219
255 220 add_filter( 'mod_rewrite_rules', 'gutenberg_filter_mod_rewrite_rules' );
256 221
257 222 /**
258 - * Returns the major Chromium version from the current request's User-Agent.
259 - *
260 - * Matches all Chromium-based browsers (Chrome, Edge, Opera, Brave).
261 - *
262 - * @return int|null The major Chromium version, or null if not a Chromium browser.
263 - */
264 -function gutenberg_get_chromium_major_version(): ?int {
265 - if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
266 - return null;
267 - }
268 - if ( preg_match( '/Chrome\/(\d+)/', $_SERVER['HTTP_USER_AGENT'], $matches ) ) {
269 - return (int) $matches[1];
270 - }
271 - return null;
272 -}
273 -
274 -/**
275 223 * Enables cross-origin isolation in the block editor.
276 224 *
277 225 * Required for enabling SharedArrayBuffer for WebAssembly-based
278 - * media processing in the editor. Uses Document-Isolation-Policy
279 - * on supported browsers (Chromium 137+).
226 + * media processing in the editor.
227 + *
228 + * @link https://web.dev/coop-coep/
280 229 */
281 230 function gutenberg_set_up_cross_origin_isolation() {
282 231 // Re-check the filter at action time, since other plugins (loaded after Gutenberg)
283 232 // may have added a filter to disable client-side media processing.
@@ -294,23 +243,8 @@
294 243 if ( ! $screen->is_block_editor() && 'site-editor' !== $screen->id && ! ( 'widgets' === $screen->id && wp_use_widgets_block_editor() ) ) {
295 244 return;
296 245 }
297 246
298 - // Skip when rendering the classic-theme home route, which shows the site
299 - // preview in an iframe and must reach its `contentDocument` to neutralize
300 - // interactive elements — DIP would block that.
301 - if ( 'site-editor' === $screen->id && ! wp_is_block_theme() && ( ! isset( $_GET['p'] ) || '/' === $_GET['p'] ) ) {
302 - return;
303 - }
304 -
305 - // Skip when a third-party page builder overrides the block editor.
306 - // DIP isolates the document into its own agent cluster,
307 - // which blocks same-origin iframe access that these editors rely on.
308 - // phpcs:ignore WordPress.Security.NonceVerification.Recommended
309 - if ( isset( $_GET['action'] ) && 'edit' !== $_GET['action'] ) {
310 - return;
311 - }
312 -
313 247 $user_id = get_current_user_id();
314 248 if ( ! $user_id ) {
315 249 return;
316 250 }
@@ -327,46 +261,26 @@
327 261 add_action( 'load-post-new.php', 'gutenberg_set_up_cross_origin_isolation' );
328 262 add_action( 'load-site-editor.php', 'gutenberg_set_up_cross_origin_isolation' );
329 263 add_action( 'load-widgets.php', 'gutenberg_set_up_cross_origin_isolation' );
330 264
331 -// Remove core's COEP/COOP-based cross-origin isolation in favor of
332 -// Gutenberg's DIP-based approach, which also skips third-party editors.
333 -remove_action( 'load-post.php', 'wp_set_up_cross_origin_isolation' );
334 -remove_action( 'load-post-new.php', 'wp_set_up_cross_origin_isolation' );
335 -remove_action( 'load-site-editor.php', 'wp_set_up_cross_origin_isolation' );
336 -remove_action( 'load-widgets.php', 'wp_set_up_cross_origin_isolation' );
337 -
338 265 /**
339 - * Sends the Document-Isolation-Policy header for cross-origin isolation.
266 + * Sends headers for cross-origin isolation.
340 267 *
341 268 * Uses an output buffer to add crossorigin="anonymous" where needed.
269 + *
270 + * @link https://web.dev/coop-coep/
271 + *
272 + * @global bool $is_safari
342 273 */
343 274 function gutenberg_start_cross_origin_isolation_output_buffer(): void {
344 - $chromium_version = gutenberg_get_chromium_major_version();
275 + global $is_safari;
345 276
346 - /**
347 - * Filters whether to use Document-Isolation-Policy for cross-origin isolation.
348 - *
349 - * Document-Isolation-Policy provides per-document cross-origin isolation
350 - * without affecting other iframes on the page, avoiding breakage of plugins
351 - * whose iframes lose credentials/DOM access.
352 - *
353 - * @since 21.8.0
354 - *
355 - * @param bool $use_dip Whether DIP is supported and should be used.
356 - */
357 - $use_dip = apply_filters(
358 - 'gutenberg_use_document_isolation_policy',
359 - null !== $chromium_version && $chromium_version >= 137
360 - );
277 + $coep = $is_safari ? 'require-corp' : 'credentialless';
361 278
362 - if ( ! $use_dip ) {
363 - return;
364 - }
365 -
366 279 ob_start(
367 - function ( string $output ): string {
368 - header( 'Document-Isolation-Policy: isolate-and-credentialless' );
280 + function ( string $output ) use ( $coep ): string {
281 + header( 'Cross-Origin-Opener-Policy: same-origin' );
282 + header( "Cross-Origin-Embedder-Policy: $coep" );
369 283
370 284 return gutenberg_add_crossorigin_attributes( $output );
371 285 }
372 286 );
@@ -386,8 +300,9 @@
386 300
387 301 // See https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/crossorigin.
388 302 $tags = array(
389 303 'AUDIO' => 'src',
304 + 'IMG' => 'src',
390 305 'LINK' => 'href',
391 306 'SCRIPT' => 'src',
392 307 'VIDEO' => 'src',
393 308 'SOURCE' => 'src',