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 +98 -181 23.2.222.7.0 View file →
@@ -1,51 +1,107 @@
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 -// Client-side media processing is currently plugin-only while the feature matures.
19 -if ( ! defined( 'IS_GUTENBERG_PLUGIN' ) || ! IS_GUTENBERG_PLUGIN ) {
8 +if ( ! gutenberg_is_client_side_media_processing_enabled() ) {
20 9 return;
21 10 }
22 11
23 -if ( ! gutenberg_is_client_side_media_processing_enabled() ) {
24 - return;
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' );
25 20 }
21 +add_action( 'admin_init', 'gutenberg_set_client_side_media_processing_flag' );
26 22
27 -// ── Tier 1: HEIC infrastructure (always loaded) ─────────────────────
23 +/**
24 + * Returns a list of all available image sizes.
25 + *
26 + * @return array Existing image sizes.
27 + */
28 +function gutenberg_get_all_image_sizes(): array {
29 + $sizes = wp_get_registered_image_subsizes();
28 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 +
29 41 /**
30 - * Registers HEIC/HEIF as allowed upload MIME types.
42 + * Returns the default output format mapping for the supported image formats.
31 43 *
32 - * HEIC images can be decoded in the browser (via canvas/VideoDecoder).
33 - * Registering these MIME types ensures the file picker's accept attribute
34 - * includes them, preventing macOS from silently converting HEIC to JPEG
35 - * on selection.
44 + * @return array<string,string> Map of input formats to output formats.
45 + */
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;
69 +}
70 +
71 +/**
72 + * Filters the REST API root index data to add custom settings.
36 73 *
37 - * @param array $mimes Allowed MIME types (extension => type).
38 - * @return array Modified MIME types.
74 + * @param WP_REST_Response $response Response data.
39 75 */
40 -function gutenberg_add_heic_upload_mimes( array $mimes ): array {
41 - $mimes['heic'] = 'image/heic';
42 - $mimes['heif'] = 'image/heif';
43 - return $mimes;
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
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;
44 99 }
45 100
46 -add_filter( 'upload_mimes', 'gutenberg_add_heic_upload_mimes' );
101 +add_filter( 'rest_index', 'gutenberg_media_processing_filter_rest_index' );
47 102
103 +
48 104 /**
49 105 * Overrides the REST controller for the attachment post type.
50 106 *
51 107 * @param array $args Array of arguments for registering a post type.
@@ -63,8 +119,9 @@
63 119 }
64 120
65 121 add_filter( 'register_post_type_args', 'gutenberg_filter_attachment_post_type_args', 10, 2 );
66 122
123 +
67 124 /**
68 125 * Registers additional REST fields for attachments.
69 126 */
70 127 function gutenberg_media_processing_register_rest_fields(): void {
@@ -144,105 +201,8 @@
144 201 return null;
145 202 }
146 203
147 204 /**
148 - * Returns a list of all available image sizes.
149 - *
150 - * @return array Existing image sizes.
151 - */
152 -function gutenberg_get_all_image_sizes(): array {
153 - $sizes = wp_get_registered_image_subsizes();
154 -
155 - foreach ( $sizes as $name => &$size ) {
156 - $size['height'] = (int) $size['height'];
157 - $size['width'] = (int) $size['width'];
158 - $size['name'] = $name;
159 - }
160 - unset( $size );
161 -
162 - return $sizes;
163 -}
164 -
165 -/**
166 - * Filters the REST API root index data to add custom settings.
167 - *
168 - * @param WP_REST_Response $response Response data.
169 - */
170 -function gutenberg_media_processing_filter_rest_index( WP_REST_Response $response ) {
171 - /** This filter is documented in wp-admin/includes/images.php */
172 - $image_size_threshold = (int) apply_filters( 'big_image_size_threshold', 2560, array( 0, 0 ), '', 0 ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
173 -
174 - if ( current_user_can( 'upload_files' ) ) {
175 - $response->data['image_sizes'] = gutenberg_get_all_image_sizes();
176 - $response->data['image_size_threshold'] = $image_size_threshold;
177 - }
178 -
179 - return $response;
180 -}
181 -
182 -add_filter( 'rest_index', 'gutenberg_media_processing_filter_rest_index' );
183 -
184 -/**
185 - * Sets a global JS variable to indicate that HEIC canvas-based upload support is available.
186 - *
187 - * This flag is set whenever the media processing feature is enabled,
188 - * regardless of whether the browser supports full VIPS-based processing.
189 - * Browsers like Safari can use createImageBitmap() to decode HEIC images
190 - * and convert them to JPEG for server-side sub-size generation.
191 - */
192 -function gutenberg_set_heic_upload_support_flag() {
193 - wp_add_inline_script( 'wp-block-editor', 'window.__heicUploadSupport = true', 'before' );
194 -}
195 -add_action( 'admin_init', 'gutenberg_set_heic_upload_support_flag' );
196 -
197 -/**
198 - * Deletes the HEIC companion file when its attachment is deleted.
199 - *
200 - * The HEIC is sideloaded alongside a JPEG derivative and recorded in
201 - * $metadata['original']. WordPress core's wp_delete_attachment_files()
202 - * only knows about 'original_image', so without this hook the HEIC
203 - * would linger on disk after the attachment is deleted.
204 - *
205 - * @param int $post_id Attachment ID being deleted.
206 - */
207 -function gutenberg_delete_heic_companion_file( int $post_id ): void {
208 - $metadata = wp_get_attachment_metadata( $post_id, true );
209 -
210 - if ( empty( $metadata['original'] ) || ! is_string( $metadata['original'] ) ) {
211 - return;
212 - }
213 -
214 - $attached_file = get_attached_file( $post_id, true );
215 -
216 - if ( ! $attached_file ) {
217 - return;
218 - }
219 -
220 - $heic_path = path_join( dirname( $attached_file ), $metadata['original'] );
221 -
222 - if ( file_exists( $heic_path ) ) {
223 - wp_delete_file( $heic_path );
224 - }
225 -}
226 -
227 -add_action( 'delete_attachment', 'gutenberg_delete_heic_companion_file' );
228 -
229 -// ── Tier 2: Full client-side processing (VIPS/WASM) ─────────────────
230 -// Everything below requires cross-origin isolation (Document-Isolation-Policy)
231 -// and SharedArrayBuffer support, which is only available in Chromium 137+.
232 -
233 -/**
234 - * Sets a global JS variable to indicate that client-side media processing is enabled.
235 - */
236 -function gutenberg_set_client_side_media_processing_flag() {
237 - if ( ! gutenberg_is_client_side_media_processing_enabled() ) {
238 - return;
239 - }
240 - wp_add_inline_script( 'wp-block-editor', 'window.__clientSideMediaProcessing = true', 'before' );
241 -}
242 -add_action( 'admin_init', 'gutenberg_set_client_side_media_processing_flag' );
243 -
244 -/**
245 205 * Filters the list of rewrite rules formatted for output to an .htaccess file.
246 206 *
247 207 * Adds support for serving wasm-vips locally.
248 208 *
@@ -259,30 +219,14 @@
259 219
260 220 add_filter( 'mod_rewrite_rules', 'gutenberg_filter_mod_rewrite_rules' );
261 221
262 222 /**
263 - * Returns the major Chromium version from the current request's User-Agent.
264 - *
265 - * Matches all Chromium-based browsers (Chrome, Edge, Opera, Brave).
266 - *
267 - * @return int|null The major Chromium version, or null if not a Chromium browser.
268 - */
269 -function gutenberg_get_chromium_major_version(): ?int {
270 - if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
271 - return null;
272 - }
273 - if ( preg_match( '/Chrome\/(\d+)/', $_SERVER['HTTP_USER_AGENT'], $matches ) ) {
274 - return (int) $matches[1];
275 - }
276 - return null;
277 -}
278 -
279 -/**
280 223 * Enables cross-origin isolation in the block editor.
281 224 *
282 225 * Required for enabling SharedArrayBuffer for WebAssembly-based
283 - * media processing in the editor. Uses Document-Isolation-Policy
284 - * on supported browsers (Chromium 137+).
226 + * media processing in the editor.
227 + *
228 + * @link https://web.dev/coop-coep/
285 229 */
286 230 function gutenberg_set_up_cross_origin_isolation() {
287 231 // Re-check the filter at action time, since other plugins (loaded after Gutenberg)
288 232 // may have added a filter to disable client-side media processing.
@@ -299,16 +243,8 @@
299 243 if ( ! $screen->is_block_editor() && 'site-editor' !== $screen->id && ! ( 'widgets' === $screen->id && wp_use_widgets_block_editor() ) ) {
300 244 return;
301 245 }
302 246
303 - // Skip when a third-party page builder overrides the block editor.
304 - // DIP isolates the document into its own agent cluster,
305 - // which blocks same-origin iframe access that these editors rely on.
306 - // phpcs:ignore WordPress.Security.NonceVerification.Recommended
307 - if ( isset( $_GET['action'] ) && 'edit' !== $_GET['action'] ) {
308 - return;
309 - }
310 -
311 247 $user_id = get_current_user_id();
312 248 if ( ! $user_id ) {
313 249 return;
314 250 }
@@ -325,46 +261,26 @@
325 261 add_action( 'load-post-new.php', 'gutenberg_set_up_cross_origin_isolation' );
326 262 add_action( 'load-site-editor.php', 'gutenberg_set_up_cross_origin_isolation' );
327 263 add_action( 'load-widgets.php', 'gutenberg_set_up_cross_origin_isolation' );
328 264
329 -// Remove core's COEP/COOP-based cross-origin isolation in favor of
330 -// Gutenberg's DIP-based approach, which also skips third-party editors.
331 -remove_action( 'load-post.php', 'wp_set_up_cross_origin_isolation' );
332 -remove_action( 'load-post-new.php', 'wp_set_up_cross_origin_isolation' );
333 -remove_action( 'load-site-editor.php', 'wp_set_up_cross_origin_isolation' );
334 -remove_action( 'load-widgets.php', 'wp_set_up_cross_origin_isolation' );
335 -
336 265 /**
337 - * Sends the Document-Isolation-Policy header for cross-origin isolation.
266 + * Sends headers for cross-origin isolation.
338 267 *
339 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
340 273 */
341 274 function gutenberg_start_cross_origin_isolation_output_buffer(): void {
342 - $chromium_version = gutenberg_get_chromium_major_version();
275 + global $is_safari;
343 276
344 - /**
345 - * Filters whether to use Document-Isolation-Policy for cross-origin isolation.
346 - *
347 - * Document-Isolation-Policy provides per-document cross-origin isolation
348 - * without affecting other iframes on the page, avoiding breakage of plugins
349 - * whose iframes lose credentials/DOM access.
350 - *
351 - * @since 21.8.0
352 - *
353 - * @param bool $use_dip Whether DIP is supported and should be used.
354 - */
355 - $use_dip = apply_filters(
356 - 'gutenberg_use_document_isolation_policy',
357 - null !== $chromium_version && $chromium_version >= 137
358 - );
277 + $coep = $is_safari ? 'require-corp' : 'credentialless';
359 278
360 - if ( ! $use_dip ) {
361 - return;
362 - }
363 -
364 279 ob_start(
365 - function ( string $output ): string {
366 - 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" );
367 283
368 284 return gutenberg_add_crossorigin_attributes( $output );
369 285 }
370 286 );
@@ -384,8 +300,9 @@
384 300
385 301 // See https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/crossorigin.
386 302 $tags = array(
387 303 'AUDIO' => 'src',
304 + 'IMG' => 'src',
388 305 'LINK' => 'href',
389 306 'SCRIPT' => 'src',
390 307 'VIDEO' => 'src',
391 308 'SOURCE' => 'src',