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 -195 23.5.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,116 +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 source-format companion file when its attachment is deleted.
194 - *
195 - * When the client-side media flow sideloads a source-format original (such as
196 - * a HEIC file) alongside a web-viewable derivative, the original's filename is
197 - * recorded in the 'source_image' metadata key. WordPress only tracks
198 - * 'original_image' in wp_delete_attachment_files(), so without this hook the
199 - * companion file would linger on disk after the attachment is deleted.
200 - *
201 - * @param int $post_id Attachment ID being deleted.
202 - * @return bool Whether a companion file was deleted.
203 - */
204 -function gutenberg_delete_heic_companion_file( int $post_id ): bool {
205 - $metadata = wp_get_attachment_metadata( $post_id, true );
206 -
207 - $source_image = $metadata['source_image'] ?? null;
208 - if ( ! is_string( $source_image ) || '' === $source_image ) {
209 - return false;
210 - }
211 -
212 - $attached_file = get_attached_file( $post_id, true );
213 -
214 - if ( ! $attached_file ) {
215 - return false;
216 - }
217 -
218 - $uploads = wp_get_upload_dir();
219 -
220 - if ( empty( $uploads['basedir'] ) ) {
221 - return false;
222 - }
223 -
224 - $companion_path = path_join( dirname( $attached_file ), wp_basename( $source_image ) );
225 -
226 - if ( ! file_exists( $companion_path ) ) {
227 - return false;
228 - }
229 -
230 - return wp_delete_file_from_directory( $companion_path, $uploads['basedir'] );
231 -}
232 -
233 -add_action( 'delete_attachment', 'gutenberg_delete_heic_companion_file' );
234 -
235 -// ── Tier 2: Full client-side processing (VIPS/WASM) ─────────────────
236 -// Everything below requires cross-origin isolation (Document-Isolation-Policy)
237 -// and SharedArrayBuffer support, which is only available in Chromium 137+.
238 -
239 -/**
240 - * Sets a global JS variable to indicate that client-side media processing is enabled.
241 - */
242 -function gutenberg_set_client_side_media_processing_flag() {
243 - if ( ! gutenberg_is_client_side_media_processing_enabled() ) {
244 - return;
245 - }
246 - wp_add_inline_script( 'wp-block-editor', 'window.__clientSideMediaProcessing = true', 'before' );
247 -}
248 -add_action( 'admin_init', 'gutenberg_set_client_side_media_processing_flag' );
249 -
250 -/**
251 205 * Filters the list of rewrite rules formatted for output to an .htaccess file.
252 206 *
253 207 * Adds support for serving wasm-vips locally.
254 208 *
@@ -265,30 +219,14 @@
265 219
266 220 add_filter( 'mod_rewrite_rules', 'gutenberg_filter_mod_rewrite_rules' );
267 221
268 222 /**
269 - * Returns the major Chromium version from the current request's User-Agent.
270 - *
271 - * Matches all Chromium-based browsers (Chrome, Edge, Opera, Brave).
272 - *
273 - * @return int|null The major Chromium version, or null if not a Chromium browser.
274 - */
275 -function gutenberg_get_chromium_major_version(): ?int {
276 - if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
277 - return null;
278 - }
279 - if ( preg_match( '/Chrome\/(\d+)/', $_SERVER['HTTP_USER_AGENT'], $matches ) ) {
280 - return (int) $matches[1];
281 - }
282 - return null;
283 -}
284 -
285 -/**
286 223 * Enables cross-origin isolation in the block editor.
287 224 *
288 225 * Required for enabling SharedArrayBuffer for WebAssembly-based
289 - * media processing in the editor. Uses Document-Isolation-Policy
290 - * on supported browsers (Chromium 137+).
226 + * media processing in the editor.
227 + *
228 + * @link https://web.dev/coop-coep/
291 229 */
292 230 function gutenberg_set_up_cross_origin_isolation() {
293 231 // Re-check the filter at action time, since other plugins (loaded after Gutenberg)
294 232 // may have added a filter to disable client-side media processing.
@@ -305,23 +243,8 @@
305 243 if ( ! $screen->is_block_editor() && 'site-editor' !== $screen->id && ! ( 'widgets' === $screen->id && wp_use_widgets_block_editor() ) ) {
306 244 return;
307 245 }
308 246
309 - // Skip when rendering the classic-theme home route, which shows the site
310 - // preview in an iframe and must reach its `contentDocument` to neutralize
311 - // interactive elements — DIP would block that.
312 - if ( 'site-editor' === $screen->id && ! wp_is_block_theme() && ( ! isset( $_GET['p'] ) || '/' === $_GET['p'] ) ) {
313 - return;
314 - }
315 -
316 - // Skip when a third-party page builder overrides the block editor.
317 - // DIP isolates the document into its own agent cluster,
318 - // which blocks same-origin iframe access that these editors rely on.
319 - // phpcs:ignore WordPress.Security.NonceVerification.Recommended
320 - if ( isset( $_GET['action'] ) && 'edit' !== $_GET['action'] ) {
321 - return;
322 - }
323 -
324 247 $user_id = get_current_user_id();
325 248 if ( ! $user_id ) {
326 249 return;
327 250 }
@@ -338,46 +261,26 @@
338 261 add_action( 'load-post-new.php', 'gutenberg_set_up_cross_origin_isolation' );
339 262 add_action( 'load-site-editor.php', 'gutenberg_set_up_cross_origin_isolation' );
340 263 add_action( 'load-widgets.php', 'gutenberg_set_up_cross_origin_isolation' );
341 264
342 -// Remove core's COEP/COOP-based cross-origin isolation in favor of
343 -// Gutenberg's DIP-based approach, which also skips third-party editors.
344 -remove_action( 'load-post.php', 'wp_set_up_cross_origin_isolation' );
345 -remove_action( 'load-post-new.php', 'wp_set_up_cross_origin_isolation' );
346 -remove_action( 'load-site-editor.php', 'wp_set_up_cross_origin_isolation' );
347 -remove_action( 'load-widgets.php', 'wp_set_up_cross_origin_isolation' );
348 -
349 265 /**
350 - * Sends the Document-Isolation-Policy header for cross-origin isolation.
266 + * Sends headers for cross-origin isolation.
351 267 *
352 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
353 273 */
354 274 function gutenberg_start_cross_origin_isolation_output_buffer(): void {
355 - $chromium_version = gutenberg_get_chromium_major_version();
275 + global $is_safari;
356 276
357 - /**
358 - * Filters whether to use Document-Isolation-Policy for cross-origin isolation.
359 - *
360 - * Document-Isolation-Policy provides per-document cross-origin isolation
361 - * without affecting other iframes on the page, avoiding breakage of plugins
362 - * whose iframes lose credentials/DOM access.
363 - *
364 - * @since 21.8.0
365 - *
366 - * @param bool $use_dip Whether DIP is supported and should be used.
367 - */
368 - $use_dip = apply_filters(
369 - 'gutenberg_use_document_isolation_policy',
370 - null !== $chromium_version && $chromium_version >= 137
371 - );
277 + $coep = $is_safari ? 'require-corp' : 'credentialless';
372 278
373 - if ( ! $use_dip ) {
374 - return;
375 - }
376 -
377 279 ob_start(
378 - function ( string $output ): string {
379 - 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" );
380 283
381 284 return gutenberg_add_crossorigin_attributes( $output );
382 285 }
383 286 );
@@ -397,8 +300,9 @@
397 300
398 301 // See https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/crossorigin.
399 302 $tags = array(
400 303 'AUDIO' => 'src',
304 + 'IMG' => 'src',
401 305 'LINK' => 'href',
402 306 'SCRIPT' => 'src',
403 307 'VIDEO' => 'src',
404 308 'SOURCE' => 'src',