-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Expand file tree
/
Copy pathload.php
More file actions
491 lines (407 loc) · 15 KB
/
Copy pathload.php
File metadata and controls
491 lines (407 loc) · 15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
<?php
/**
* Adds media-related functionality for client-side media processing.
*
* This file is structured in two tiers:
*
* 1. HEIC infrastructure — loaded whenever the feature filter is enabled.
* Browsers like Safari can decode HEIC via createImageBitmap() even
* without VIPS/SharedArrayBuffer, so HEIC MIME types, the custom REST
* controller, and REST field/index registrations are always needed.
*
* 2. Full VIPS/WASM processing — loaded only when the feature filter is
* enabled AND requires cross-origin isolation (DIP) at runtime.
*
* @package gutenberg
*/
if ( ! gutenberg_is_client_side_media_processing_enabled() ) {
return;
}
// Animated GIF → video: clean up the sideloaded companion video and
// poster when their GIF attachment is deleted. The GIF→video swap itself
// happens in the editor (the converted block is a real core/video), so no
// render-time filtering is needed.
require_once __DIR__ . '/animated-gif-to-video.php';
// Video transcoding: clean up the sideloaded web-safe companion when its
// video attachment is deleted, and expose the "keep original" opt-out filter.
// The swap to the optimized companion happens in the editor (the core/video
// block's src points at the companion), so no render-time filtering is needed.
require_once __DIR__ . '/video-transcoding.php';
// ── Tier 1: HEIC infrastructure (always loaded) ─────────────────────
/**
* Registers HEIC/HEIF as allowed upload MIME types.
*
* HEIC images can be decoded in the browser (via canvas/VideoDecoder).
* Registering these MIME types ensures the file picker's accept attribute
* includes them, preventing macOS from silently converting HEIC to JPEG
* on selection.
*
* @param array $mimes Allowed MIME types (extension => type).
* @return array Modified MIME types.
*/
function gutenberg_add_heic_upload_mimes( array $mimes ): array {
$mimes['heic'] = 'image/heic';
$mimes['heif'] = 'image/heif';
return $mimes;
}
add_filter( 'upload_mimes', 'gutenberg_add_heic_upload_mimes' );
/**
* Overrides the REST controller for the attachment post type.
*
* @param array $args Array of arguments for registering a post type.
* See the register_post_type() function for accepted arguments.
* @param string $post_type Post type key.
*/
function gutenberg_filter_attachment_post_type_args( array $args, string $post_type ): array {
if ( 'attachment' === $post_type ) {
require_once __DIR__ . '/class-gutenberg-rest-attachments-controller.php';
$args['rest_controller_class'] = Gutenberg_REST_Attachments_Controller::class;
}
return $args;
}
add_filter( 'register_post_type_args', 'gutenberg_filter_attachment_post_type_args', 10, 2 );
/**
* Registers additional REST fields for attachments.
*/
function gutenberg_media_processing_register_rest_fields(): void {
register_rest_field(
'attachment',
'filename',
array(
'schema' => array(
'description' => __( 'Original attachment file name', 'gutenberg' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
),
'get_callback' => 'gutenberg_rest_get_attachment_filename',
)
);
register_rest_field(
'attachment',
'filesize',
array(
'schema' => array(
'description' => __( 'Attachment file size', 'gutenberg' ),
'type' => 'number',
'context' => array( 'view', 'edit' ),
),
'get_callback' => 'gutenberg_rest_get_attachment_filesize',
)
);
}
add_action( 'rest_api_init', 'gutenberg_media_processing_register_rest_fields' );
/**
* Returns the attachment's original file name.
*
* @param array $post Post data.
* @return string|null Attachment file name.
*/
function gutenberg_rest_get_attachment_filename( array $post ): ?string {
$path = wp_get_original_image_path( $post['id'] );
if ( $path ) {
return basename( $path );
}
$path = get_attached_file( $post['id'] );
if ( $path ) {
return basename( $path );
}
return null;
}
/**
* Returns the attachment's file size in bytes.
*
* @param array $post Post data.
* @return int|null Attachment file size.
*/
function gutenberg_rest_get_attachment_filesize( array $post ): ?int {
$attachment_id = $post['id'];
$meta = wp_get_attachment_metadata( $attachment_id );
if ( isset( $meta['filesize'] ) ) {
return $meta['filesize'];
}
$original_path = wp_get_original_image_path( $attachment_id );
$attached_file = $original_path ? $original_path : get_attached_file( $attachment_id );
if ( is_string( $attached_file ) && file_exists( $attached_file ) ) {
return wp_filesize( $attached_file );
}
return null;
}
/**
* Returns a list of all available image sizes.
*
* @return array Existing image sizes.
*/
function gutenberg_get_all_image_sizes(): array {
$sizes = wp_get_registered_image_subsizes();
foreach ( $sizes as $name => &$size ) {
$size['height'] = (int) $size['height'];
$size['width'] = (int) $size['width'];
$size['name'] = $name;
}
unset( $size );
return $sizes;
}
/**
* Filters the REST API root index data to add custom settings.
*
* @param WP_REST_Response $response Response data.
*/
function gutenberg_media_processing_filter_rest_index( WP_REST_Response $response ) {
/** This filter is documented in wp-admin/includes/image.php */
$image_size_threshold = (int) apply_filters( 'big_image_size_threshold', 2560, array( 0, 0 ), '', 0 ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
if ( current_user_can( 'upload_files' ) ) {
$response->data['image_sizes'] = gutenberg_get_all_image_sizes();
$response->data['image_size_threshold'] = $image_size_threshold;
}
return $response;
}
add_filter( 'rest_index', 'gutenberg_media_processing_filter_rest_index' );
/**
* Sets a global JS variable to indicate that HEIC canvas-based upload support is available.
*
* This flag is set whenever the media processing feature is enabled,
* regardless of whether the browser supports full VIPS-based processing.
* Browsers like Safari can use createImageBitmap() to decode HEIC images
* and convert them to JPEG for server-side sub-size generation.
*/
function gutenberg_set_heic_upload_support_flag() {
wp_add_inline_script( 'wp-block-editor', 'window.__heicUploadSupport = true', 'before' );
}
add_action( 'admin_init', 'gutenberg_set_heic_upload_support_flag' );
/**
* Deletes the source-format companion file when its attachment is deleted.
*
* When the client-side media flow sideloads a source-format original (such as
* a HEIC file) alongside a web-viewable derivative, the original's filename is
* recorded in the 'source_image' metadata key. WordPress only tracks
* 'original_image' in wp_delete_attachment_files(), so without this hook the
* companion file would linger on disk after the attachment is deleted.
*
* @param int $post_id Attachment ID being deleted.
* @return bool Whether a companion file was deleted.
*/
function gutenberg_delete_heic_companion_file( int $post_id ): bool {
$metadata = wp_get_attachment_metadata( $post_id, true );
$source_image = $metadata['source_image'] ?? null;
if ( ! is_string( $source_image ) || '' === $source_image ) {
return false;
}
$attached_file = get_attached_file( $post_id, true );
if ( ! $attached_file ) {
return false;
}
$uploads = wp_get_upload_dir();
if ( empty( $uploads['basedir'] ) ) {
return false;
}
$companion_path = path_join( dirname( $attached_file ), wp_basename( $source_image ) );
if ( ! file_exists( $companion_path ) ) {
return false;
}
return wp_delete_file_from_directory( $companion_path, $uploads['basedir'] );
}
add_action( 'delete_attachment', 'gutenberg_delete_heic_companion_file' );
// ── Tier 2: Full client-side processing (VIPS/WASM) ─────────────────
// Everything below requires cross-origin isolation (Document-Isolation-Policy)
// and SharedArrayBuffer support, which is only available in Chromium 137+.
/**
* Sets a global JS variable to indicate that client-side media processing is enabled.
*/
function gutenberg_set_client_side_media_processing_flag() {
if ( ! gutenberg_is_client_side_media_processing_enabled() ) {
return;
}
wp_add_inline_script( 'wp-block-editor', 'window.__clientSideMediaProcessing = true', 'before' );
}
add_action( 'admin_init', 'gutenberg_set_client_side_media_processing_flag' );
/**
* Filters the list of rewrite rules formatted for output to an .htaccess file.
*
* Adds support for serving wasm-vips locally.
*
* @param string $rules mod_rewrite Rewrite rules formatted for .htaccess.
* @return string Filtered rewrite rules.
*/
function gutenberg_filter_mod_rewrite_rules( string $rules ): string {
$rules .= "\n# BEGIN Gutenberg client-side media processing\n" .
"AddType application/wasm wasm\n" .
"# END Gutenberg client-side media processing\n";
return $rules;
}
add_filter( 'mod_rewrite_rules', 'gutenberg_filter_mod_rewrite_rules' );
/**
* Returns the major Chromium version from the current request's User-Agent.
*
* Matches all Chromium-based browsers (Chrome, Edge, Opera, Brave).
*
* @return int|null The major Chromium version, or null if not a Chromium browser.
*/
function gutenberg_get_chromium_major_version(): ?int {
if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
return null;
}
if ( preg_match( '/Chrome\/(\d+)/', $_SERVER['HTTP_USER_AGENT'], $matches ) ) {
return (int) $matches[1];
}
return null;
}
/**
* Enables cross-origin isolation in the block editor.
*
* Required for enabling SharedArrayBuffer for WebAssembly-based
* media processing in the editor. Uses Document-Isolation-Policy
* on supported browsers (Chromium 137+).
*/
function gutenberg_set_up_cross_origin_isolation() {
// Re-check the filter at action time, since other plugins (loaded after Gutenberg)
// may have added a filter to disable client-side media processing.
if ( ! gutenberg_is_client_side_media_processing_enabled() ) {
return;
}
$screen = get_current_screen();
if ( ! $screen ) {
return;
}
if ( ! $screen->is_block_editor() && 'site-editor' !== $screen->id && ! ( 'widgets' === $screen->id && wp_use_widgets_block_editor() ) ) {
return;
}
// Skip when rendering the classic-theme home route, which shows the site
// preview in an iframe and must reach its `contentDocument` to neutralize
// interactive elements — DIP would block that.
if ( 'site-editor' === $screen->id && ! wp_is_block_theme() && ( ! isset( $_GET['p'] ) || '/' === $_GET['p'] ) ) {
return;
}
// Skip when a third-party page builder overrides the block editor.
// DIP isolates the document into its own agent cluster,
// which blocks same-origin iframe access that these editors rely on.
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
if ( isset( $_GET['action'] ) && 'edit' !== $_GET['action'] ) {
return;
}
$user_id = get_current_user_id();
if ( ! $user_id ) {
return;
}
// Cross-origin isolation is not needed if users can't upload files anyway.
if ( ! user_can( $user_id, 'upload_files' ) ) {
return;
}
gutenberg_start_cross_origin_isolation_output_buffer();
}
add_action( 'load-post.php', 'gutenberg_set_up_cross_origin_isolation' );
add_action( 'load-post-new.php', 'gutenberg_set_up_cross_origin_isolation' );
add_action( 'load-site-editor.php', 'gutenberg_set_up_cross_origin_isolation' );
add_action( 'load-widgets.php', 'gutenberg_set_up_cross_origin_isolation' );
// Remove core's COEP/COOP-based cross-origin isolation in favor of
// Gutenberg's DIP-based approach, which also skips third-party editors.
remove_action( 'load-post.php', 'wp_set_up_cross_origin_isolation' );
remove_action( 'load-post-new.php', 'wp_set_up_cross_origin_isolation' );
remove_action( 'load-site-editor.php', 'wp_set_up_cross_origin_isolation' );
remove_action( 'load-widgets.php', 'wp_set_up_cross_origin_isolation' );
/**
* Sends the Document-Isolation-Policy header for cross-origin isolation.
*
* Uses an output buffer to add crossorigin="anonymous" where needed.
*/
function gutenberg_start_cross_origin_isolation_output_buffer(): void {
$chromium_version = gutenberg_get_chromium_major_version();
/**
* Filters whether to use Document-Isolation-Policy for cross-origin isolation.
*
* Document-Isolation-Policy provides per-document cross-origin isolation
* without affecting other iframes on the page, avoiding breakage of plugins
* whose iframes lose credentials/DOM access.
*
* @since 21.8.0
*
* @param bool $use_dip Whether DIP is supported and should be used.
*/
$use_dip = apply_filters(
'gutenberg_use_document_isolation_policy',
null !== $chromium_version && $chromium_version >= 137
);
if ( ! $use_dip ) {
return;
}
ob_start(
function ( string $output ): string {
header( 'Document-Isolation-Policy: isolate-and-credentialless' );
return gutenberg_add_crossorigin_attributes( $output );
}
);
}
/**
* Adds crossorigin="anonymous" to relevant tags in the given HTML string.
*
* @param string $html HTML input.
*
* @return string Modified HTML.
*/
function gutenberg_add_crossorigin_attributes( string $html ): string {
$site_url = site_url();
$processor = new WP_HTML_Tag_Processor( $html );
// See https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/crossorigin.
$tags = array(
'AUDIO' => 'src',
'LINK' => 'href',
'SCRIPT' => 'src',
'VIDEO' => 'src',
'SOURCE' => 'src',
);
$tag_names = array_keys( $tags );
while ( $processor->next_tag() ) {
$tag = $processor->get_tag();
if ( ! in_array( $tag, $tag_names, true ) ) {
continue;
}
if ( 'AUDIO' === $tag || 'VIDEO' === $tag ) {
$processor->set_bookmark( 'audio-video-parent' );
}
$processor->set_bookmark( 'resume' );
$sought = false;
$crossorigin = $processor->get_attribute( 'crossorigin' );
$url = $processor->get_attribute( $tags[ $tag ] );
if ( is_string( $url ) && ! str_starts_with( $url, $site_url ) && ! str_starts_with( $url, '/' ) && ! is_string( $crossorigin ) ) {
if ( 'SOURCE' === $tag ) {
$sought = $processor->seek( 'audio-video-parent' );
if ( $sought ) {
$processor->set_attribute( 'crossorigin', 'anonymous' );
}
} else {
$processor->set_attribute( 'crossorigin', 'anonymous' );
}
if ( $sought ) {
$processor->seek( 'resume' );
$processor->release_bookmark( 'audio-video-parent' );
}
}
}
return $processor->get_updated_html();
}
/**
* Overrides templates from wp_print_media_templates with custom ones.
*
* Adds `crossorigin` attribute to all tags that
* could have assets loaded from a different domain.
*/
function gutenberg_override_media_templates(): void {
remove_action( 'admin_footer', 'wp_print_media_templates' );
add_action(
'admin_footer',
static function (): void {
ob_start();
wp_print_media_templates();
$html = (string) ob_get_clean();
$tags = array(
'audio',
'img',
'video',
);
foreach ( $tags as $tag ) {
$html = (string) str_replace( "<$tag", "<$tag crossorigin=\"anonymous\"", $html );
}
echo $html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
);
}
add_action( 'wp_enqueue_media', 'gutenberg_override_media_templates' );