| @@ -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,35 +8,100 @@ | ||
| 18 | 8 | if ( ! gutenberg_is_client_side_media_processing_enabled() ) { |
| 19 | 9 | return; |
| 20 | 10 | } |
| 21 | 11 | |
| 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'; | |
| 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' ); | |
| 27 | 22 | |
| 28 | -// ── 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(); | |
| 29 | 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 | + | |
| 30 | 41 | /** |
| 31 | - * Registers HEIC/HEIF as allowed upload MIME types. | |
| 42 | + * Returns the default output format mapping for the supported image formats. | |
| 32 | 43 | * |
| 33 | - * HEIC images can be decoded in the browser (via canvas/VideoDecoder). | |
| 34 | - * Registering these MIME types ensures the file picker's accept attribute | |
| 35 | - * includes them, preventing macOS from silently converting HEIC to JPEG | |
| 36 | - * 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. | |
| 37 | 73 | * |
| 38 | - * @param array $mimes Allowed MIME types (extension => type). | |
| 39 | - * @return array Modified MIME types. | |
| 74 | + * @param WP_REST_Response $response Response data. | |
| 40 | 75 | */ |
| 41 | -function gutenberg_add_heic_upload_mimes( array $mimes ): array { | |
| 42 | - $mimes['heic'] = 'image/heic'; | |
| 43 | - $mimes['heif'] = 'image/heif'; | |
| 44 | - 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; | |
| 45 | 99 | } |
| 46 | 100 | |
| 47 | -add_filter( 'upload_mimes', 'gutenberg_add_heic_upload_mimes' ); | |
| 101 | +add_filter( 'rest_index', 'gutenberg_media_processing_filter_rest_index' ); | |
| 48 | 102 | |
| 103 | + | |
| 49 | 104 | /** |
| 50 | 105 | * Overrides the REST controller for the attachment post type. |
| 51 | 106 | * |
| 52 | 107 | * @param array $args Array of arguments for registering a post type. |
| @@ -64,8 +119,9 @@ | ||
| 64 | 119 | } |
| 65 | 120 | |
| 66 | 121 | add_filter( 'register_post_type_args', 'gutenberg_filter_attachment_post_type_args', 10, 2 ); |
| 67 | 122 | |
| 123 | + | |
| 68 | 124 | /** |
| 69 | 125 | * Registers additional REST fields for attachments. |
| 70 | 126 | */ |
| 71 | 127 | function gutenberg_media_processing_register_rest_fields(): void { |
| @@ -145,126 +201,8 @@ | ||
| 145 | 201 | return null; |
| 146 | 202 | } |
| 147 | 203 | |
| 148 | 204 | /** |
| 149 | - * Returns a list of all available image sizes. | |
| 150 | - * | |
| 151 | - * @return array Existing image sizes. | |
| 152 | - */ | |
| 153 | -function gutenberg_get_all_image_sizes(): array { | |
| 154 | - $sizes = wp_get_registered_image_subsizes(); | |
| 155 | - | |
| 156 | - foreach ( $sizes as $name => &$size ) { | |
| 157 | - $size['height'] = (int) $size['height']; | |
| 158 | - $size['width'] = (int) $size['width']; | |
| 159 | - $size['name'] = $name; | |
| 160 | - } | |
| 161 | - unset( $size ); | |
| 162 | - | |
| 163 | - return $sizes; | |
| 164 | -} | |
| 165 | - | |
| 166 | -/** | |
| 167 | - * Filters the REST API root index data to add custom settings. | |
| 168 | - * | |
| 169 | - * @param WP_REST_Response $response Response data. | |
| 170 | - */ | |
| 171 | -function gutenberg_media_processing_filter_rest_index( WP_REST_Response $response ) { | |
| 172 | - /** This filter is documented in wp-admin/includes/image.php */ | |
| 173 | - $image_size_threshold = (int) apply_filters( 'big_image_size_threshold', 2560, array( 0, 0 ), '', 0 ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound | |
| 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 | - | |
| 189 | - if ( current_user_can( 'upload_files' ) ) { | |
| 190 | - $response->data['image_sizes'] = gutenberg_get_all_image_sizes(); | |
| 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; | |
| 194 | - } | |
| 195 | - | |
| 196 | - return $response; | |
| 197 | -} | |
| 198 | - | |
| 199 | -add_filter( 'rest_index', 'gutenberg_media_processing_filter_rest_index' ); | |
| 200 | - | |
| 201 | -/** | |
| 202 | - * Sets a global JS variable to indicate that client-side media processing is enabled. | |
| 203 | - * | |
| 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. | |
| 208 | - */ | |
| 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' ); | |
| 216 | -} | |
| 217 | -add_action( 'admin_init', 'gutenberg_set_client_side_media_processing_flag' ); | |
| 218 | - | |
| 219 | -/** | |
| 220 | - * Deletes the source-format companion file when its attachment is deleted. | |
| 221 | - * | |
| 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. | |
| 227 | - * | |
| 228 | - * @param int $post_id Attachment ID being deleted. | |
| 229 | - * @return bool Whether a companion file was deleted. | |
| 230 | - */ | |
| 231 | -function gutenberg_delete_heic_companion_file( int $post_id ): bool { | |
| 232 | - $metadata = wp_get_attachment_metadata( $post_id, true ); | |
| 233 | - | |
| 234 | - $source_image = $metadata['source_image'] ?? null; | |
| 235 | - if ( ! is_string( $source_image ) || '' === $source_image ) { | |
| 236 | - return false; | |
| 237 | - } | |
| 238 | - | |
| 239 | - $attached_file = get_attached_file( $post_id, true ); | |
| 240 | - | |
| 241 | - if ( ! $attached_file ) { | |
| 242 | - return false; | |
| 243 | - } | |
| 244 | - | |
| 245 | - $uploads = wp_get_upload_dir(); | |
| 246 | - | |
| 247 | - if ( empty( $uploads['basedir'] ) ) { | |
| 248 | - return false; | |
| 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'] ); | |
| 258 | -} | |
| 259 | - | |
| 260 | -add_action( 'delete_attachment', 'gutenberg_delete_heic_companion_file' ); | |
| 261 | - | |
| 262 | -// ── Tier 2: Full client-side processing (VIPS/WASM) ───────────────── | |
| 263 | -// Everything below requires cross-origin isolation (Document-Isolation-Policy) | |
| 264 | -// and SharedArrayBuffer support, which is only available in Chromium 137+. | |
| 265 | - | |
| 266 | -/** | |
| 267 | 205 | * Filters the list of rewrite rules formatted for output to an .htaccess file. |
| 268 | 206 | * |
| 269 | 207 | * Adds support for serving wasm-vips locally. |
| 270 | 208 | * |
| @@ -281,30 +219,14 @@ | ||
| 281 | 219 | |
| 282 | 220 | add_filter( 'mod_rewrite_rules', 'gutenberg_filter_mod_rewrite_rules' ); |
| 283 | 221 | |
| 284 | 222 | /** |
| 285 | - * Returns the major Chromium version from the current request's User-Agent. | |
| 286 | - * | |
| 287 | - * Matches all Chromium-based browsers (Chrome, Edge, Opera, Brave). | |
| 288 | - * | |
| 289 | - * @return int|null The major Chromium version, or null if not a Chromium browser. | |
| 290 | - */ | |
| 291 | -function gutenberg_get_chromium_major_version(): ?int { | |
| 292 | - if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) { | |
| 293 | - return null; | |
| 294 | - } | |
| 295 | - if ( preg_match( '/Chrome\/(\d+)/', $_SERVER['HTTP_USER_AGENT'], $matches ) ) { | |
| 296 | - return (int) $matches[1]; | |
| 297 | - } | |
| 298 | - return null; | |
| 299 | -} | |
| 300 | - | |
| 301 | -/** | |
| 302 | 223 | * Enables cross-origin isolation in the block editor. |
| 303 | 224 | * |
| 304 | 225 | * Required for enabling SharedArrayBuffer for WebAssembly-based |
| 305 | - * media processing in the editor. Uses Document-Isolation-Policy | |
| 306 | - * on supported browsers (Chromium 137+). | |
| 226 | + * media processing in the editor. | |
| 227 | + * | |
| 228 | + * @link https://web.dev/coop-coep/ | |
| 307 | 229 | */ |
| 308 | 230 | function gutenberg_set_up_cross_origin_isolation() { |
| 309 | 231 | // Re-check the filter at action time, since other plugins (loaded after Gutenberg) |
| 310 | 232 | // may have added a filter to disable client-side media processing. |
| @@ -321,23 +243,8 @@ | ||
| 321 | 243 | if ( ! $screen->is_block_editor() && 'site-editor' !== $screen->id && ! ( 'widgets' === $screen->id && wp_use_widgets_block_editor() ) ) { |
| 322 | 244 | return; |
| 323 | 245 | } |
| 324 | 246 | |
| 325 | - // Skip when rendering the classic-theme home route, which shows the site | |
| 326 | - // preview in an iframe and must reach its `contentDocument` to neutralize | |
| 327 | - // interactive elements — DIP would block that. | |
| 328 | - if ( 'site-editor' === $screen->id && ! wp_is_block_theme() && ( ! isset( $_GET['p'] ) || '/' === $_GET['p'] ) ) { | |
| 329 | - return; | |
| 330 | - } | |
| 331 | - | |
| 332 | - // Skip when a third-party page builder overrides the block editor. | |
| 333 | - // DIP isolates the document into its own agent cluster, | |
| 334 | - // which blocks same-origin iframe access that these editors rely on. | |
| 335 | - // phpcs:ignore WordPress.Security.NonceVerification.Recommended | |
| 336 | - if ( isset( $_GET['action'] ) && 'edit' !== $_GET['action'] ) { | |
| 337 | - return; | |
| 338 | - } | |
| 339 | - | |
| 340 | 247 | $user_id = get_current_user_id(); |
| 341 | 248 | if ( ! $user_id ) { |
| 342 | 249 | return; |
| 343 | 250 | } |
| @@ -354,46 +261,26 @@ | ||
| 354 | 261 | add_action( 'load-post-new.php', 'gutenberg_set_up_cross_origin_isolation' ); |
| 355 | 262 | add_action( 'load-site-editor.php', 'gutenberg_set_up_cross_origin_isolation' ); |
| 356 | 263 | add_action( 'load-widgets.php', 'gutenberg_set_up_cross_origin_isolation' ); |
| 357 | 264 | |
| 358 | -// Remove core's COEP/COOP-based cross-origin isolation in favor of | |
| 359 | -// Gutenberg's DIP-based approach, which also skips third-party editors. | |
| 360 | -remove_action( 'load-post.php', 'wp_set_up_cross_origin_isolation' ); | |
| 361 | -remove_action( 'load-post-new.php', 'wp_set_up_cross_origin_isolation' ); | |
| 362 | -remove_action( 'load-site-editor.php', 'wp_set_up_cross_origin_isolation' ); | |
| 363 | -remove_action( 'load-widgets.php', 'wp_set_up_cross_origin_isolation' ); | |
| 364 | - | |
| 365 | 265 | /** |
| 366 | - * Sends the Document-Isolation-Policy header for cross-origin isolation. | |
| 266 | + * Sends headers for cross-origin isolation. | |
| 367 | 267 | * |
| 368 | 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 | |
| 369 | 273 | */ |
| 370 | 274 | function gutenberg_start_cross_origin_isolation_output_buffer(): void { |
| 371 | - $chromium_version = gutenberg_get_chromium_major_version(); | |
| 275 | + global $is_safari; | |
| 372 | 276 | |
| 373 | - /** | |
| 374 | - * Filters whether to use Document-Isolation-Policy for cross-origin isolation. | |
| 375 | - * | |
| 376 | - * Document-Isolation-Policy provides per-document cross-origin isolation | |
| 377 | - * without affecting other iframes on the page, avoiding breakage of plugins | |
| 378 | - * whose iframes lose credentials/DOM access. | |
| 379 | - * | |
| 380 | - * @since 21.8.0 | |
| 381 | - * | |
| 382 | - * @param bool $use_dip Whether DIP is supported and should be used. | |
| 383 | - */ | |
| 384 | - $use_dip = apply_filters( | |
| 385 | - 'gutenberg_use_document_isolation_policy', | |
| 386 | - null !== $chromium_version && $chromium_version >= 137 | |
| 387 | - ); | |
| 277 | + $coep = $is_safari ? 'require-corp' : 'credentialless'; | |
| 388 | 278 | |
| 389 | - if ( ! $use_dip ) { | |
| 390 | - return; | |
| 391 | - } | |
| 392 | - | |
| 393 | 279 | ob_start( |
| 394 | - function ( string $output ): string { | |
| 395 | - 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" ); | |
| 396 | 283 | |
| 397 | 284 | return gutenberg_add_crossorigin_attributes( $output ); |
| 398 | 285 | } |
| 399 | 286 | ); |
| @@ -413,8 +300,9 @@ | ||
| 413 | 300 | |
| 414 | 301 | // See https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/crossorigin. |
| 415 | 302 | $tags = array( |
| 416 | 303 | 'AUDIO' => 'src', |
| 304 | + 'IMG' => 'src', | |
| 417 | 305 | 'LINK' => 'href', |
| 418 | 306 | 'SCRIPT' => 'src', |
| 419 | 307 | 'VIDEO' => 'src', |
| 420 | 308 | 'SOURCE' => 'src', |
| @@ -462,60 +350,12 @@ | ||
| 462 | 350 | return $processor->get_updated_html(); |
| 463 | 351 | } |
| 464 | 352 | |
| 465 | 353 | /** |
| 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 | -/** | |
| 513 | 354 | * Overrides templates from wp_print_media_templates with custom ones. |
| 514 | 355 | * |
| 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. | |
| 356 | + * Adds `crossorigin` attribute to all tags that | |
| 357 | + * could have assets loaded from a different domain. | |
| 518 | 358 | */ |
| 519 | 359 | function gutenberg_override_media_templates(): void { |
| 520 | 360 | remove_action( 'admin_footer', 'wp_print_media_templates' ); |
| 521 | 361 | add_action( |
| @@ -524,9 +364,19 @@ | ||
| 524 | 364 | ob_start(); |
| 525 | 365 | wp_print_media_templates(); |
| 526 | 366 | $html = (string) ob_get_clean(); |
| 527 | 367 | |
| 528 | - echo gutenberg_update_media_template_crossorigin_attributes( $html ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped | |
| 368 | + $tags = array( | |
| 369 | + 'audio', | |
| 370 | + 'img', | |
| 371 | + 'video', | |
| 372 | + ); | |
| 373 | + | |
| 374 | + foreach ( $tags as $tag ) { | |
| 375 | + $html = (string) str_replace( "<$tag", "<$tag crossorigin=\"anonymous\"", $html ); | |
| 376 | + } | |
| 377 | + | |
| 378 | + echo $html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped | |
| 529 | 379 | } |
| 530 | 380 | ); |
| 531 | 381 | } |
| 532 | 382 | |