| @@ -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,19 +198,24 @@ | ||
| 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 | 220 | * Deletes the source-format companion file when its attachment is deleted. |
| 194 | 221 | * |
| @@ -236,19 +263,8 @@ | ||
| 236 | 263 | // Everything below requires cross-origin isolation (Document-Isolation-Policy) |
| 237 | 264 | // and SharedArrayBuffer support, which is only available in Chromium 137+. |
| 238 | 265 | |
| 239 | 266 | /** |
| 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 | 267 | * Filters the list of rewrite rules formatted for output to an .htaccess file. |
| 252 | 268 | * |
| 253 | 269 | * Adds support for serving wasm-vips locally. |
| 254 | 270 | * |
| @@ -446,12 +462,60 @@ | ||
| 446 | 462 | return $processor->get_updated_html(); |
| 447 | 463 | } |
| 448 | 464 | |
| 449 | 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 | +/** | |
| 450 | 513 | * Overrides templates from wp_print_media_templates with custom ones. |
| 451 | 514 | * |
| 452 | - * Adds `crossorigin` attribute to all tags that | |
| 453 | - * 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. | |
| 454 | 518 | */ |
| 455 | 519 | function gutenberg_override_media_templates(): void { |
| 456 | 520 | remove_action( 'admin_footer', 'wp_print_media_templates' ); |
| 457 | 521 | add_action( |
| @@ -460,19 +524,9 @@ | ||
| 460 | 524 | ob_start(); |
| 461 | 525 | wp_print_media_templates(); |
| 462 | 526 | $html = (string) ob_get_clean(); |
| 463 | 527 | |
| 464 | - $tags = array( | |
| 465 | - 'audio', | |
| 466 | - 'img', | |
| 467 | - 'video', | |
| 468 | - ); | |
| 469 | - | |
| 470 | - foreach ( $tags as $tag ) { | |
| 471 | - $html = (string) str_replace( "<$tag", "<$tag crossorigin=\"anonymous\"", $html ); | |
| 472 | - } | |
| 473 | - | |
| 474 | - echo $html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped | |
| 528 | + echo gutenberg_update_media_template_crossorigin_attributes( $html ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped | |
| 475 | 529 | } |
| 476 | 530 | ); |
| 477 | 531 | } |
| 478 | 532 | |