Skip to content

Commit cc116cc

Browse files
Media: Sideload and clean up the web-safe companion of a transcoded video upload.
Add server-side support for the client-side video transcoding flow. When client-side media processing is enabled, the editor transcodes an uploaded video that is not already web-safe to MP4/WebM in the browser and sideloads the result as a companion of the original video attachment, which remains the attachment itself. - Accept a video attachment as the sideload parent for the new `optimized_video` companion size, and only for that size; every other size still requires an image or PDF parent. - Record the companion under the `optimized_video` attachment metadata key on finalize, and treat it as a name the request is allowed to store. - Delete the companion file alongside the attachment in `wp_delete_attachment_files()`. - Expose the new `wp_video_transcoding_keep_original` filter (default `true`) on the REST API index as `video_keep_original`, so the editor knows whether to keep the original upload or transcode before uploading, and preload it with the other media processing settings. See WordPress/gutenberg#79375.
1 parent 02778fd commit cc116cc

8 files changed

Lines changed: 327 additions & 16 deletions

File tree

‎src/wp-admin/edit-form-blocks.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ static function ( $classes ) {
106106
'site_logo',
107107
'timezone_string',
108108
'url',
109+
'video_keep_original',
109110
'page_for_posts',
110111
'page_on_front',
111112
'show_on_front',

‎src/wp-admin/site-editor.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ static function ( $classes ) {
232232
'site_logo',
233233
'timezone_string',
234234
'url',
235+
'video_keep_original',
235236
'page_for_posts',
236237
'page_on_front',
237238
'show_on_front',

‎src/wp-includes/post.php‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6998,13 +6998,16 @@ function wp_delete_attachment_files( $post_id, $meta, $backup_sizes, $file ) {
69986998
}
69996999

70007000
/*
7001-
* Delete the animated-GIF video companions. When the client-side media flow
7002-
* converts an opaque animated GIF to a web-safe video, the converted MP4/WebM
7003-
* and a static first-frame JPEG poster are sideloaded alongside the GIF and
7004-
* recorded under the 'animated_video' and 'animated_video_poster' keys. These
7005-
* are kept separate from 'original_image', which continues to point at the GIF.
7001+
* Delete the video companions. When the client-side media flow converts an
7002+
* opaque animated GIF to a web-safe video, the converted MP4/WebM and a
7003+
* static first-frame JPEG poster are sideloaded alongside the GIF and
7004+
* recorded under the 'animated_video' and 'animated_video_poster' keys.
7005+
* When it transcodes a video upload to a web-safe format, the transcode is
7006+
* sideloaded alongside the original video and recorded under the
7007+
* 'optimized_video' key. These are kept separate from 'original_image',
7008+
* which continues to point at the upload itself.
70067009
*/
7007-
foreach ( array( 'animated_video', 'animated_video_poster' ) as $companion_key ) {
7010+
foreach ( array( 'animated_video', 'animated_video_poster', 'optimized_video' ) as $companion_key ) {
70087011
if ( empty( $meta[ $companion_key ] ) || ! is_string( $meta[ $companion_key ] ) ) {
70097012
continue;
70107013
}

‎src/wp-includes/rest-api/class-wp-rest-server.php‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1397,6 +1397,20 @@ public function get_index( $request ) {
13971397
*/
13981398
/** This filter is documented in wp-includes/class-wp-image-editor-imagick.php */
13991399
$available['image_max_bit_depth'] = (int) apply_filters( 'image_max_bit_depth', 16, 16 );
1400+
1401+
/**
1402+
* Filters whether the original video upload is kept when a video is transcoded.
1403+
*
1404+
* When true (default), the original video is stored as the attachment and
1405+
* the transcoded web-safe version is sideloaded as a companion file. When
1406+
* false, the video is transcoded before upload so only the optimized file
1407+
* is stored.
1408+
*
1409+
* @since 7.2.0
1410+
*
1411+
* @param bool $keep_original Whether to keep the original video upload. Default true.
1412+
*/
1413+
$available['video_keep_original'] = (bool) apply_filters( 'wp_video_transcoding_keep_original', true );
14001414
}
14011415

14021416
$response = new WP_REST_Response( $available );

‎src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php‎

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2603,6 +2603,8 @@ private static function get_special_image_sizes(): array {
26032603
// Converted-video companions for an animated GIF (the MP4/WebM and its poster).
26042604
'animated_video',
26052605
'animated_video_poster',
2606+
// Web-safe transcode kept alongside the original video upload.
2607+
'optimized_video',
26062608
);
26072609
}
26082610

@@ -2758,13 +2760,28 @@ public function sideload_item( WP_REST_Request $request ) {
27582760
return $post;
27592761
}
27602762

2763+
/** @var non-empty-string|non-empty-list<non-empty-string> $image_size */
2764+
$image_size = $request['image_size'];
2765+
2766+
/*
2767+
* Sideloads extend an existing attachment with a file derived from it,
2768+
* which is an image or PDF sub-size in every case but one: the web-safe
2769+
* transcode of a video is sideloaded as a companion of the video
2770+
* attachment it was produced from, so a video parent is valid for that
2771+
* size and no other.
2772+
*/
2773+
$is_optimized_video_companion =
2774+
'optimized_video' === $image_size &&
2775+
wp_attachment_is( 'video', $post );
2776+
27612777
if (
27622778
! wp_attachment_is_image( $post ) &&
2763-
! wp_attachment_is( 'pdf', $post )
2779+
! wp_attachment_is( 'pdf', $post ) &&
2780+
! $is_optimized_video_companion
27642781
) {
27652782
return new WP_Error(
27662783
'rest_post_invalid_id',
2767-
__( 'Invalid post ID. Only images and PDFs can be sideloaded.' ),
2784+
__( 'Invalid post ID. Only images, PDFs and videos can be sideloaded.' ),
27682785
array( 'status' => 400 )
27692786
);
27702787
}
@@ -2847,21 +2864,23 @@ public function sideload_item( WP_REST_Request $request ) {
28472864
$type = $file['type'];
28482865
$path = $file['file'];
28492866

2850-
/** @var non-empty-string|non-empty-list<non-empty-string> $image_size */
2851-
$image_size = $request['image_size'];
2852-
28532867
/*
2854-
* Validate raster sub-sizes before storing them. Two companion sizes
2868+
* Validate raster sub-sizes before storing them. Three companion sizes
28552869
* are exempt because wp_getimagesize() may not be able to read the
2856-
* file at all: the 'animated_video' companion of an animated GIF is a
2857-
* video (MP4/WebM), and a source-format original (e.g. a HEIC or JXL
2858-
* kept next to its JPEG derivative) may be an unreadable format. Their
2870+
* file at all: the 'animated_video' companion of an animated GIF and
2871+
* the 'optimized_video' companion of a video upload are videos
2872+
* (MP4/WebM), and a source-format original (e.g. a HEIC or JXL kept
2873+
* next to its JPEG derivative) may be an unreadable format. Their
28592874
* dimensions are neither validated nor recorded. The
28602875
* 'animated_video_poster' companion is a real image, so it is still
28612876
* read and rejected if unreadable; validate_image_dimensions() skips
28622877
* only the registered-size constraint for it.
28632878
*/
2864-
$skip_dimension_read = self::IMAGE_SIZE_SOURCE_ORIGINAL === $image_size || 'animated_video' === $image_size;
2879+
$skip_dimension_read = in_array(
2880+
$image_size,
2881+
array( self::IMAGE_SIZE_SOURCE_ORIGINAL, 'animated_video', 'optimized_video' ),
2882+
true
2883+
);
28652884
$size = false;
28662885

28672886
if ( ! $skip_dimension_read ) {
@@ -2935,6 +2954,13 @@ public function sideload_item( WP_REST_Request $request ) {
29352954
* finalize_item can store it under its dedicated meta key.
29362955
*/
29372956
$sub_size_data['file'] = wp_basename( $path );
2957+
} elseif ( 'optimized_video' === $image_size ) {
2958+
/*
2959+
* Web-safe transcode of a video upload. The original video stays
2960+
* the attachment; record the companion's filename so finalize_item
2961+
* can store it under its dedicated meta key.
2962+
*/
2963+
$sub_size_data['file'] = wp_basename( $path );
29382964
} elseif ( 'scaled' === $image_size || 'original' === $image_size ) {
29392965
/*
29402966
* 'scaled' and 'original' both replace the attachment's main file
@@ -3165,6 +3191,7 @@ protected function get_sideloaded_file_names( int $attachment_id, bool $include_
31653191
$metadata[ self::META_KEY_SOURCE_IMAGE ] ?? null,
31663192
$metadata['animated_video'] ?? null,
31673193
$metadata['animated_video_poster'] ?? null,
3194+
$metadata['optimized_video'] ?? null,
31683195
);
31693196

31703197
if ( ! empty( $metadata['sizes'] ) && is_array( $metadata['sizes'] ) ) {
@@ -3379,6 +3406,17 @@ public function finalize_item( WP_REST_Request $request ) {
33793406

33803407
// Static first-frame poster for the converted video.
33813408
$metadata['animated_video_poster'] = $sub_size['file'];
3409+
} elseif ( 'optimized_video' === $image_size ) {
3410+
if ( empty( $sub_size['file'] ) ) {
3411+
continue;
3412+
}
3413+
3414+
/*
3415+
* Web-safe transcode of a video upload. Stored under its own
3416+
* meta key; the original video stays the attachment. Cleanup on
3417+
* attachment delete is handled by wp_delete_attachment_files().
3418+
*/
3419+
$metadata['optimized_video'] = $sub_size['file'];
33823420
} else {
33833421
if ( empty( $sub_size['file'] ) ) {
33843422
continue;
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
<?php
2+
3+
/**
4+
* Tests that wp_delete_attachment_files() removes the transcoded video companion.
5+
*
6+
* @group media
7+
* @covers ::wp_delete_attachment_files
8+
*/
9+
class Tests_Media_wpDeleteAttachmentOptimizedVideo extends WP_UnitTestCase {
10+
11+
public function tear_down(): void {
12+
$this->remove_added_uploads();
13+
14+
parent::tear_down();
15+
}
16+
17+
/**
18+
* @ticket 65998
19+
*/
20+
public function test_deletes_companion_recorded_in_metadata(): void {
21+
$attachment_id = self::factory()->attachment->create_upload_object( DIR_TESTDATA . '/uploads/small-video.mov' );
22+
$this->assertIsInt( $attachment_id );
23+
24+
$attached_file = get_attached_file( $attachment_id, true );
25+
$this->assertIsString( $attached_file );
26+
$dir = dirname( $attached_file );
27+
$video_name = 'optimized-' . wp_generate_password( 6, false ) . '.mp4';
28+
$video_path = $dir . '/' . $video_name;
29+
30+
// Create a dummy companion file on disk.
31+
file_put_contents( $video_path, 'test' );
32+
$this->assertFileExists( $video_path, 'Video fixture should be on disk.' );
33+
34+
// Record the companion as the finalize route does.
35+
$metadata = (array) wp_get_attachment_metadata( $attachment_id, true );
36+
$metadata['optimized_video'] = $video_name;
37+
wp_update_attachment_metadata( $attachment_id, $metadata );
38+
39+
wp_delete_attachment( $attachment_id, true );
40+
41+
$this->assertNull( get_post( $attachment_id ) );
42+
$this->assertFileDoesNotExist( $video_path, 'Video companion should be deleted alongside the attachment.' );
43+
$this->assertFileDoesNotExist( $attached_file, 'The original video should be deleted as before.' );
44+
}
45+
46+
/**
47+
* @ticket 65998
48+
*/
49+
public function test_noop_when_no_companion_metadata(): void {
50+
$attachment_id = self::factory()->attachment->create_upload_object( DIR_TESTDATA . '/uploads/small-video.mov' );
51+
$this->assertIsInt( $attachment_id );
52+
53+
$metadata = (array) wp_get_attachment_metadata( $attachment_id, true );
54+
$this->assertArrayNotHasKey( 'optimized_video', $metadata );
55+
56+
// Deletion should complete cleanly even though no companion file is recorded.
57+
wp_delete_attachment( $attachment_id, true );
58+
59+
$this->assertNull( get_post( $attachment_id ) );
60+
}
61+
62+
/**
63+
* A path-traversal value in the metadata only ever resolves inside the
64+
* attachment's own directory, so a file outside it is never touched.
65+
*
66+
* @ticket 65998
67+
*/
68+
public function test_traversal_value_does_not_delete_outside_attachment_dir(): void {
69+
$attachment_id = self::factory()->attachment->create_upload_object( DIR_TESTDATA . '/uploads/small-video.mov' );
70+
$this->assertIsInt( $attachment_id );
71+
72+
$attached_file = get_attached_file( $attachment_id, true );
73+
$this->assertIsString( $attached_file );
74+
75+
// A file one level above the attachment's directory.
76+
$outside_path = dirname( $attached_file, 2 ) . '/outside-' . wp_generate_password( 6, false ) . '.mp4';
77+
file_put_contents( $outside_path, 'test' );
78+
$this->assertFileExists( $outside_path, 'Test fixture should be on disk.' );
79+
80+
$metadata = (array) wp_get_attachment_metadata( $attachment_id, true );
81+
$metadata['optimized_video'] = '../' . wp_basename( $outside_path );
82+
wp_update_attachment_metadata( $attachment_id, $metadata );
83+
84+
wp_delete_attachment( $attachment_id, true );
85+
86+
$this->assertNull( get_post( $attachment_id ) );
87+
$this->assertFileExists( $outside_path, 'A file outside the attachment directory must not be deleted.' );
88+
89+
wp_delete_file( $outside_path );
90+
}
91+
92+
/**
93+
* Guards against the companion key holding a non-string value.
94+
*
95+
* @ticket 65998
96+
*/
97+
public function test_noop_when_companion_metadata_is_not_a_string(): void {
98+
$attachment_id = self::factory()->attachment->create_upload_object( DIR_TESTDATA . '/uploads/small-video.mov' );
99+
$this->assertIsInt( $attachment_id );
100+
$attached_file = get_attached_file( $attachment_id, true );
101+
$this->assertIsString( $attached_file );
102+
103+
$bystander_path = dirname( $attached_file ) . '/should-not-delete.mp4';
104+
file_put_contents( $bystander_path, 'test' );
105+
$this->assertFileExists( $bystander_path, 'Test fixture should be on disk.' );
106+
107+
$metadata = (array) wp_get_attachment_metadata( $attachment_id, true );
108+
$metadata['optimized_video'] = array( 'file' => 'should-not-delete.mp4' );
109+
wp_update_attachment_metadata( $attachment_id, $metadata );
110+
111+
wp_delete_attachment( $attachment_id, true );
112+
113+
$this->assertNull( get_post( $attachment_id ) );
114+
$this->assertFileExists( $bystander_path, 'The non-string guard must prevent any file deletion.' );
115+
116+
wp_delete_file( $bystander_path );
117+
}
118+
}

0 commit comments

Comments
 (0)