PluginProbe
Gutenberg / 24.0.0
Gutenberg v24.0.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 +109 -44 23.4.024.0.0 View file →
@@ -18,8 +18,14 @@
18 18 if ( ! gutenberg_is_client_side_media_processing_enabled() ) {
19 19 return;
20 20 }
21 21
22 +// Animated GIF → video: clean up the sideloaded companion video and
23 +// poster when their GIF attachment is deleted. The GIF→video swap itself
24 +// happens in the editor (the converted block is a real core/video), so no
25 +// render-time filtering is needed.
26 +require_once __DIR__ . '/animated-gif-to-video.php';
27 +
22 28 // ── Tier 1: HEIC infrastructure (always loaded) ─────────────────────
23 29
24 30 /**
25 31 * Registers HEIC/HEIF as allowed upload MIME types.
@@ -165,11 +171,27 @@
165 171 function gutenberg_media_processing_filter_rest_index( WP_REST_Response $response ) {
166 172 /** This filter is documented in wp-admin/includes/image.php */
167 173 $image_size_threshold = (int) apply_filters( 'big_image_size_threshold', 2560, array( 0, 0 ), '', 0 ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
168 174
175 + /** This filter is documented in wp-includes/class-wp-image-editor-imagick.php */
176 + $image_strip_meta = (bool) apply_filters( 'image_strip_meta', true ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
177 +
178 + /*
179 + * On the server, this filter receives the decoded image's actual bit depth.
180 + * The client path never decodes the image on the server, so the filter is
181 + * applied with 16 (the maximum depth vips can produce) as both the value
182 + * and the current depth. The client caps its output bit depth at the
183 + * filtered value, so a plugin lowering it (e.g. to 8) takes effect on
184 + * client-generated images too.
185 + */
186 + /** This filter is documented in wp-includes/class-wp-image-editor-imagick.php */
187 + $image_max_bit_depth = (int) apply_filters( 'image_max_bit_depth', 16, 16 ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
188 +
169 189 if ( current_user_can( 'upload_files' ) ) {
170 190 $response->data['image_sizes'] = gutenberg_get_all_image_sizes();
171 191 $response->data['image_size_threshold'] = $image_size_threshold;
192 + $response->data['image_strip_meta'] = $image_strip_meta;
193 + $response->data['image_max_bit_depth'] = $image_max_bit_depth;
172 194 }
173 195
174 196 return $response;
175 197 }
@@ -176,48 +198,64 @@
176 198
177 199 add_filter( 'rest_index', 'gutenberg_media_processing_filter_rest_index' );
178 200
179 201 /**
180 - * Sets a global JS variable to indicate that HEIC canvas-based upload support is available.
202 + * Sets a global JS variable to indicate that client-side media processing is enabled.
181 203 *
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.
204 + * The flag gates both processing modes: the full VIPS/WASM pipeline (browsers
205 + * that pass feature detection) and the HEIC canvas fallback used by browsers
206 + * such as Safari that can decode HEIC via createImageBitmap() but lack
207 + * SharedArrayBuffer support. The browser-capability check happens client-side.
186 208 */
187 -function gutenberg_set_heic_upload_support_flag() {
188 - wp_add_inline_script( 'wp-block-editor', 'window.__heicUploadSupport = true', 'before' );
209 +function gutenberg_set_client_side_media_processing_flag() {
210 + // Re-check the filter at action time, since other plugins (loaded after Gutenberg)
211 + // may have added a filter to disable client-side media processing.
212 + if ( ! gutenberg_is_client_side_media_processing_enabled() ) {
213 + return;
214 + }
215 + wp_add_inline_script( 'wp-block-editor', 'window.__clientSideMediaProcessing = true', 'before' );
189 216 }
190 -add_action( 'admin_init', 'gutenberg_set_heic_upload_support_flag' );
217 +add_action( 'admin_init', 'gutenberg_set_client_side_media_processing_flag' );
191 218
192 219 /**
193 - * Deletes the HEIC companion file when its attachment is deleted.
220 + * Deletes the source-format companion file when its attachment is deleted.
194 221 *
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.
222 + * When the client-side media flow sideloads a source-format original (such as
223 + * a HEIC file) alongside a web-viewable derivative, the original's filename is
224 + * recorded in the 'source_image' metadata key. WordPress only tracks
225 + * 'original_image' in wp_delete_attachment_files(), so without this hook the
226 + * companion file would linger on disk after the attachment is deleted.
199 227 *
200 228 * @param int $post_id Attachment ID being deleted.
229 + * @return bool Whether a companion file was deleted.
201 230 */
202 -function gutenberg_delete_heic_companion_file( int $post_id ): void {
231 +function gutenberg_delete_heic_companion_file( int $post_id ): bool {
203 232 $metadata = wp_get_attachment_metadata( $post_id, true );
204 233
205 - if ( empty( $metadata['original'] ) || ! is_string( $metadata['original'] ) ) {
206 - return;
234 + $source_image = $metadata['source_image'] ?? null;
235 + if ( ! is_string( $source_image ) || '' === $source_image ) {
236 + return false;
207 237 }
208 238
209 239 $attached_file = get_attached_file( $post_id, true );
210 240
211 241 if ( ! $attached_file ) {
212 - return;
242 + return false;
213 243 }
214 244
215 - $heic_path = path_join( dirname( $attached_file ), $metadata['original'] );
245 + $uploads = wp_get_upload_dir();
216 246
217 - if ( file_exists( $heic_path ) ) {
218 - wp_delete_file( $heic_path );
247 + if ( empty( $uploads['basedir'] ) ) {
248 + return false;
219 249 }
250 +
251 + $companion_path = path_join( dirname( $attached_file ), wp_basename( $source_image ) );
252 +
253 + if ( ! file_exists( $companion_path ) ) {
254 + return false;
255 + }
256 +
257 + return wp_delete_file_from_directory( $companion_path, $uploads['basedir'] );
220 258 }
221 259
222 260 add_action( 'delete_attachment', 'gutenberg_delete_heic_companion_file' );
223 261
@@ -225,19 +263,8 @@
225 263 // Everything below requires cross-origin isolation (Document-Isolation-Policy)
226 264 // and SharedArrayBuffer support, which is only available in Chromium 137+.
227 265
228 266 /**
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 267 * Filters the list of rewrite rules formatted for output to an .htaccess file.
241 268 *
242 269 * Adds support for serving wasm-vips locally.
243 270 *
@@ -435,12 +462,60 @@
435 462 return $processor->get_updated_html();
436 463 }
437 464
438 465 /**
466 + * Updates `crossorigin` attributes in the printed media templates.
467 + *
468 + * Adds `crossorigin="anonymous"` to AUDIO and VIDEO tags inside the
469 + * Backbone `<script type="text/html">` templates so the media modal can
470 + * play cross-origin audio and video under cross-origin isolation. Tags
471 + * that already have the attribute are left untouched so the output does
472 + * not gain duplicates on WordPress versions where Core adds it itself.
473 + *
474 + * IMG is intentionally excluded: under
475 + * `Document-Isolation-Policy: isolate-and-credentialless` the browser
476 + * already loads cross-origin images in credentialless mode, so forcing
477 + * `crossorigin="anonymous"` triggers a CORS request that breaks previews
478 + * of images served without CORS headers, such as media offloaded to a
479 + * CDN. See https://core.trac.wordpress.org/ticket/65673.
480 + *
481 + * @param string $html The printed media templates.
482 + *
483 + * @return string Modified media templates.
484 + */
485 +function gutenberg_update_media_template_crossorigin_attributes( string $html ): string {
486 + /*
487 + * The media templates are inside <script type="text/html"> tags,
488 + * whose content is treated as raw text by the HTML Tag Processor.
489 + * Extract each script block's content, process it separately,
490 + * then reassemble the full output.
491 + */
492 + $script_processor = new WP_HTML_Tag_Processor( $html );
493 + while ( $script_processor->next_tag( 'SCRIPT' ) ) {
494 + if ( 'text/html' !== $script_processor->get_attribute( 'type' ) ) {
495 + continue;
496 + }
497 + $template_processor = new WP_HTML_Tag_Processor( $script_processor->get_modifiable_text() );
498 + while ( $template_processor->next_tag() ) {
499 + if (
500 + in_array( $template_processor->get_tag(), array( 'AUDIO', 'VIDEO' ), true )
501 + && ! is_string( $template_processor->get_attribute( 'crossorigin' ) )
502 + ) {
503 + $template_processor->set_attribute( 'crossorigin', 'anonymous' );
504 + }
505 + }
506 + $script_processor->set_modifiable_text( $template_processor->get_updated_html() );
507 + }
508 +
509 + return $script_processor->get_updated_html();
510 +}
511 +
512 +/**
439 513 * Overrides templates from wp_print_media_templates with custom ones.
440 514 *
441 - * Adds `crossorigin` attribute to all tags that
442 - * could have assets loaded from a different domain.
515 + * Updates the `crossorigin` attributes on media tags so cross-origin
516 + * audio and video can be processed under cross-origin isolation without
517 + * breaking previews of images served without CORS headers.
443 518 */
444 519 function gutenberg_override_media_templates(): void {
445 520 remove_action( 'admin_footer', 'wp_print_media_templates' );
446 521 add_action(
@@ -449,19 +524,9 @@
449 524 ob_start();
450 525 wp_print_media_templates();
451 526 $html = (string) ob_get_clean();
452 527
453 - $tags = array(
454 - 'audio',
455 - 'img',
456 - 'video',
457 - );
458 -
459 - foreach ( $tags as $tag ) {
460 - $html = (string) str_replace( "<$tag", "<$tag crossorigin=\"anonymous\"", $html );
461 - }
462 -
463 - echo $html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
528 + echo gutenberg_update_media_template_crossorigin_attributes( $html ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
464 529 }
465 530 );
466 531 }
467 532