Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 51 additions & 3 deletions packages/block-library/src/video/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,24 @@ function render_block_core_video( array $attributes, string $content ): string {
return $content;
}

$p->set_attribute( 'width', (string) $metadata['width'] );
$p->set_attribute( 'height', (string) $metadata['height'] );
/*
* For videos with 90° or 270° rotation metadata (common for portrait videos recorded by
* mobile devices that physically encode frames as landscape with a container-level rotation
* flag), the stored width and height are the pre-rotation dimensions of the video file, not
* the displayed dimensions. Swap them to get the correct displayed dimensions.
*
* The rotation is stored by the `gutenberg_add_video_rotation_to_metadata` filter below,
* which reads it from the raw getID3 data at upload time.
*/
$width = $metadata['width'];
$height = $metadata['height'];
if ( isset( $metadata['rotate'] ) && in_array( abs( (int) $metadata['rotate'] ), array( 90, 270 ), true ) ) {
$width = $metadata['height'];
$height = $metadata['width'];
}

$p->set_attribute( 'width', (string) $width );
$p->set_attribute( 'height', (string) $height );

/*
* The aspect-ratio style is needed due to an issue with the CSS spec: <https://github.com/w3c/csswg-drafts/issues/7524>.
Expand All @@ -64,17 +80,48 @@ function render_block_core_video( array $attributes, string $content ): string {
* }
*
* This is because this attr() is yet only implemented in Chromium: <https://caniuse.com/css3-attr>.
*
* Note: the `auto` keyword cannot be prepended here (i.e. `aspect-ratio: auto W / H`) because
* for `<video>` the default object size (300×150) is always treated as the natural aspect ratio
* before the video data loads. The `auto` keyword would therefore resolve to 300/150 instead
* of the W/H fallback, reintroducing the CLS that this style was added to prevent.
* See: <https://github.com/w3c/csswg-drafts/issues/7524>.
*/
$style = $p->get_attribute( 'style' );
if ( ! is_string( $style ) ) {
$style = '';
}
$aspect_ratio_style = sprintf( 'aspect-ratio: %d / %d;', $metadata['width'], $metadata['height'] );
$aspect_ratio_style = sprintf( 'aspect-ratio: %d / %d;', $width, $height );
$p->set_attribute( 'style', $aspect_ratio_style . $style );

return $p->get_updated_html();
}

/**
* Adds rotation information to video attachment metadata during upload.
*
* The core `wp_read_video_metadata()` function does not include the video rotation,
* but getID3 computes it from the container metadata (e.g. the QuickTime tkhd atom's
* transform matrix). This filter persists the rotation in the stored attachment metadata
* so the Video block render function can correct the aspect ratio for portrait videos
* that are physically encoded in landscape orientation with a rotation flag.
*
* @since 6.9.0
*
* @param array $metadata Filtered video metadata.
* @param string $file Path to the video file.
* @param string|null $file_format File format of the video, as analyzed by getID3.
* @param array $data Raw metadata from getID3.
* @return array Updated video metadata.
*/
function gutenberg_add_video_rotation_to_metadata( array $metadata, string $file, ?string $file_format, array $data ): array {
if ( ! isset( $metadata['rotate'] ) && ! empty( $data['video']['rotate'] ) ) {
$metadata['rotate'] = (int) $data['video']['rotate'];
}
return $metadata;
}
add_filter( 'wp_read_video_metadata', 'gutenberg_add_video_rotation_to_metadata', 10, 4 );

/**
* Registers the `core/video` block on server.
*
Expand All @@ -89,3 +136,4 @@ function register_block_core_video(): void {
);
}
add_action( 'init', 'register_block_core_video' );

307 changes: 307 additions & 0 deletions phpunit/blocks/render-block-video-test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,307 @@
<?php
/**
* Video block rendering tests.
*
* @package WordPress
* @subpackage Blocks
*/

/**
* Tests for the Video block.
*
* @group blocks
*/
class Tests_Blocks_Render_Video extends WP_UnitTestCase {

/**
* The landscape video attachment ID used in tests.
*
* @var int
*/
private $attachment_id;

public function set_up() {
parent::set_up();

// Create a landscape video attachment (width=1920, height=1080, no rotation).
$this->attachment_id = $this->factory->attachment->create(
array(
'post_mime_type' => 'video/mp4',
'post_title' => 'Test Landscape Video',
)
);

wp_update_attachment_metadata(
$this->attachment_id,
array(
'width' => 1920,
'height' => 1080,
)
);
}

public function tear_down() {
wp_delete_attachment( $this->attachment_id, true );
parent::tear_down();
}

/**
* @covers ::render_block_core_video
*/
public function test_render_block_core_video_returns_content_unchanged_when_no_video_tag() {
$attributes = array( 'id' => $this->attachment_id );
$content = '<figure class="wp-block-video"></figure>';

$result = render_block_core_video( $attributes, $content );
$this->assertSame( $content, $result );
}

/**
* @covers ::render_block_core_video
*/
public function test_render_block_core_video_returns_content_unchanged_when_no_id() {
$attributes = array();
$content = '<figure class="wp-block-video"><video src="test.mp4"></video></figure>';

$result = render_block_core_video( $attributes, $content );
$this->assertSame( $content, $result );
}

/**
* @covers ::render_block_core_video
*/
public function test_render_block_core_video_adds_landscape_aspect_ratio() {
$attributes = array( 'id' => $this->attachment_id );
$content = '<figure class="wp-block-video"><video src="test.mp4"></video></figure>';

$result = render_block_core_video( $attributes, $content );
$this->assertStringContainsString( 'aspect-ratio: 1920 / 1080;', $result );
$this->assertStringNotContainsString( 'aspect-ratio: auto', $result );
}

/**
* @covers ::render_block_core_video
*/
public function test_render_block_core_video_adds_width_and_height_attributes() {
$attributes = array( 'id' => $this->attachment_id );
$content = '<figure class="wp-block-video"><video src="test.mp4"></video></figure>';

$result = render_block_core_video( $attributes, $content );
$this->assertStringContainsString( 'width="1920"', $result );
$this->assertStringContainsString( 'height="1080"', $result );
}

/**
* Tests that a portrait video whose rotation is stored in attachment metadata renders
* with the correct portrait aspect ratio.
*
* Some devices (e.g. older mobile phones) encode video frames as landscape but embed a
* 90° or 270° rotation flag in the container metadata. WordPress stores the physical
* (pre-rotation) dimensions, so the render function must swap them to produce the
* correct displayed aspect ratio and avoid a landscape-shaped layout box for a portrait
* video.
*
* @covers ::render_block_core_video
*/
public function test_render_block_core_video_portrait_video_with_rotation_metadata() {
// Simulate a portrait video physically encoded as landscape (1920x1080) with a 90°
// rotation flag. The rendered aspect ratio and HTML attributes should reflect the
// displayed (portrait) dimensions: width=1080, height=1920.
$portrait_id = $this->factory->attachment->create(
array(
'post_mime_type' => 'video/mp4',
'post_title' => 'Portrait Video (90° rotation)',
)
);
wp_update_attachment_metadata(
$portrait_id,
array(
'width' => 1920,
'height' => 1080,
'rotate' => 90,
)
);

$attributes = array( 'id' => $portrait_id );
$content = '<figure class="wp-block-video"><video src="portrait.mp4"></video></figure>';

$result = render_block_core_video( $attributes, $content );

// The aspect-ratio must use the swapped (portrait) dimensions.
$this->assertStringContainsString( 'aspect-ratio: 1080 / 1920;', $result );
// The `auto` keyword must NOT be added (it would reintroduce CLS via the 300×150 default object size).
$this->assertStringNotContainsString( 'aspect-ratio: auto', $result );
// Width and height attributes must also reflect the displayed (portrait) dimensions.
$this->assertStringContainsString( 'width="1080"', $result );
$this->assertStringContainsString( 'height="1920"', $result );

wp_delete_attachment( $portrait_id, true );
}

/**
* Tests that a 270° rotation is treated identically to 90° (both require swapping).
*
* @covers ::render_block_core_video
*/
public function test_render_block_core_video_portrait_video_with_270_rotation() {
$portrait_id = $this->factory->attachment->create(
array(
'post_mime_type' => 'video/mp4',
'post_title' => 'Portrait Video (270° rotation)',
)
);
wp_update_attachment_metadata(
$portrait_id,
array(
'width' => 1920,
'height' => 1080,
'rotate' => 270,
)
);

$attributes = array( 'id' => $portrait_id );
$content = '<figure class="wp-block-video"><video src="portrait.mp4"></video></figure>';

$result = render_block_core_video( $attributes, $content );

$this->assertStringContainsString( 'aspect-ratio: 1080 / 1920;', $result );
$this->assertStringContainsString( 'width="1080"', $result );
$this->assertStringContainsString( 'height="1920"', $result );

wp_delete_attachment( $portrait_id, true );
}

/**
* Tests that a 180° rotation does NOT swap the dimensions (the video is upside down
* but still landscape).
*
* @covers ::render_block_core_video
*/
public function test_render_block_core_video_180_rotation_does_not_swap_dimensions() {
$flipped_id = $this->factory->attachment->create(
array(
'post_mime_type' => 'video/mp4',
'post_title' => 'Flipped Video (180° rotation)',
)
);
wp_update_attachment_metadata(
$flipped_id,
array(
'width' => 1920,
'height' => 1080,
'rotate' => 180,
)
);

$attributes = array( 'id' => $flipped_id );
$content = '<figure class="wp-block-video"><video src="flipped.mp4"></video></figure>';

$result = render_block_core_video( $attributes, $content );

// 180° rotation flips the video upside-down but does not change the aspect ratio.
$this->assertStringContainsString( 'aspect-ratio: 1920 / 1080;', $result );
$this->assertStringContainsString( 'width="1920"', $result );
$this->assertStringContainsString( 'height="1080"', $result );

wp_delete_attachment( $flipped_id, true );
}

/**
* Tests that a negative rotation value (e.g. -90°, equivalent to 270°) is handled correctly.
*
* @covers ::render_block_core_video
*/
public function test_render_block_core_video_negative_90_rotation_swaps_dimensions() {
$portrait_id = $this->factory->attachment->create(
array(
'post_mime_type' => 'video/mp4',
'post_title' => 'Portrait Video (-90° rotation)',
)
);
wp_update_attachment_metadata(
$portrait_id,
array(
'width' => 1920,
'height' => 1080,
'rotate' => -90,
)
);

$attributes = array( 'id' => $portrait_id );
$content = '<figure class="wp-block-video"><video src="portrait.mp4"></video></figure>';

$result = render_block_core_video( $attributes, $content );

$this->assertStringContainsString( 'aspect-ratio: 1080 / 1920;', $result );
$this->assertStringContainsString( 'width="1080"', $result );
$this->assertStringContainsString( 'height="1920"', $result );

wp_delete_attachment( $portrait_id, true );
}

/**
* Tests that the `gutenberg_add_video_rotation_to_metadata` filter adds the rotation
* from getID3 raw data to the stored attachment metadata.
*
* @covers ::gutenberg_add_video_rotation_to_metadata
*/
public function test_gutenberg_add_video_rotation_to_metadata_adds_rotate_from_getid3_data() {
$metadata = array(
'width' => 1920,
'height' => 1080,
);
$data = array(
'video' => array(
'rotate' => 90,
),
);

$result = gutenberg_add_video_rotation_to_metadata( $metadata, '/fake/path.mp4', 'mp4', $data );

$this->assertArrayHasKey( 'rotate', $result );
$this->assertSame( 90, $result['rotate'] );
}

/**
* Tests that the filter does not overwrite a rotate value already present in metadata.
*
* @covers ::gutenberg_add_video_rotation_to_metadata
*/
public function test_gutenberg_add_video_rotation_to_metadata_does_not_overwrite_existing_rotate() {
$metadata = array(
'width' => 1920,
'height' => 1080,
'rotate' => 0,
);
$data = array(
'video' => array(
'rotate' => 90,
),
);

$result = gutenberg_add_video_rotation_to_metadata( $metadata, '/fake/path.mp4', 'mp4', $data );

// The pre-existing rotate=0 must not be overwritten.
$this->assertSame( 0, $result['rotate'] );
}

/**
* Tests that the filter does nothing when no rotation is present in the raw getID3 data.
*
* @covers ::gutenberg_add_video_rotation_to_metadata
*/
public function test_gutenberg_add_video_rotation_to_metadata_does_nothing_when_no_rotation_in_data() {
$metadata = array(
'width' => 1920,
'height' => 1080,
);
$data = array(
'video' => array(),
);

$result = gutenberg_add_video_rotation_to_metadata( $metadata, '/fake/path.mp4', 'mp4', $data );

$this->assertArrayNotHasKey( 'rotate', $result );
}
}