diff --git a/projects/packages/videopress/changelog/add-videopress-video-details-block b/projects/packages/videopress/changelog/add-videopress-video-details-block new file mode 100644 index 000000000000..618d5db7878a --- /dev/null +++ b/projects/packages/videopress/changelog/add-videopress-video-details-block @@ -0,0 +1,4 @@ +Significance: minor +Type: added + +Add a Video Details block, and let the VideoPress Video block play the newest upload. diff --git a/projects/packages/videopress/src/class-channel-blocks.php b/projects/packages/videopress/src/class-channel-blocks.php index ac4f6ba051fe..3c32110e1d39 100644 --- a/projects/packages/videopress/src/class-channel-blocks.php +++ b/projects/packages/videopress/src/class-channel-blocks.php @@ -7,6 +7,8 @@ namespace Automattic\Jetpack\VideoPress; +use WP_Post; + /** * Registers and renders the server-side blocks a VideoPress channel theme * builds its pages from, on sites with the channel feature enabled. Each renders @@ -22,6 +24,7 @@ class Channel_Blocks { */ const BLOCKS = array( 'videopress/channel-details' => 'render_channel_details', + 'videopress/video-details' => 'render_video_details', ); /** @@ -103,6 +106,32 @@ public static function enqueue_editor_data() { ); } + /** + * A boolean attribute. + * + * @param array $attributes Block attributes. + * @param string $key Attribute. + * @param bool $default_value Value when unset. + * + * @return bool + */ + private static function flag( $attributes, $key, $default_value ) { + return isset( $attributes[ $key ] ) ? (bool) $attributes[ $key ] : $default_value; + } + + /** + * A heading level attribute, 1 to 6; anything else ( 0 = automatic ) picks the default. + * + * @param array $attributes Block attributes. + * @param int $default_value Level when unset. + * + * @return int + */ + private static function heading_level( $attributes, $default_value ) { + $level = isset( $attributes['headingLevel'] ) && is_numeric( $attributes['headingLevel'] ) ? (int) $attributes['headingLevel'] : 0; + return $level >= 1 && $level <= 6 ? $level : $default_value; + } + /** * Channel details: the handle, subscribers, videos, playlists, views and join date. * @@ -214,4 +243,105 @@ private static function channel_item( $item ) { ); } } + + /** + * The video a block shows: the video page's, or the newest upload. + * + * @param array $attributes Block attributes. + * + * @return WP_Post|null + */ + private static function block_video( $attributes ) { + if ( isset( $attributes['source'] ) && 'latest' === $attributes['source'] ) { + return Channel_Data::latest_video(); + } + return Channel::is_video_page() ? Channel::current_video() : null; + } + + /** + * Video details: eyebrow, title, views and age, description and playlist chips. + * + * @param array $attributes Block attributes. + * + * @return string + */ + public static function render_video_details( $attributes ) { + $attributes = is_array( $attributes ) ? $attributes : array(); + $post = self::block_video( $attributes ); + $is_latest = isset( $attributes['source'] ) && 'latest' === $attributes['source']; + $guid = $post instanceof WP_Post ? Channel::guid( $post ) : ''; + if ( ! $post instanceof WP_Post || '' === $guid ) { + return ''; + } + $details = Channel_Data::video_details( $guid, $post ); + $record = Channel_Data::record( $post ); + $playlist = $is_latest ? null : Channel::queried_playlist(); + if ( $playlist && ! in_array( $guid, array_column( $playlist['videos'], 'guid' ), true ) ) { + $playlist = null; + } + + $markup = ''; + if ( self::flag( $attributes, 'showEyebrow', true ) ) { + $markup .= sprintf( '%s', esc_html( self::episode_label( $guid, $playlist ) ) ); + } + if ( self::flag( $attributes, 'showTitle', true ) && '' !== $details['title'] ) { + $level = self::heading_level( $attributes, $is_latest ? 2 : 1 ); + $title = $is_latest + ? sprintf( '%2$s', esc_url( Channel::video_url( $post ) ), esc_html( $details['title'] ) ) + : esc_html( $details['title'] ); + $markup .= sprintf( '%2$s', $level, $title ); + } + if ( self::flag( $attributes, 'showMeta', true ) && $record ) { + $meta = Channel_Data::views_and_age( Channel_Data::with_views( array( $record ) )[0], ! $is_latest ); + if ( '' !== $meta ) { + $markup .= sprintf( '
%s
', esc_html( $meta ) ); + } + } + if ( self::flag( $attributes, 'showDescription', true ) && '' !== trim( wp_strip_all_tags( $details['description'] ) ) ) { + $markup .= sprintf( '
%s
', wpautop( esc_html( wp_strip_all_tags( $details['description'] ) ) ) ); + } + if ( self::flag( $attributes, 'showPlaylists', true ) ) { + $chips = ''; + foreach ( Channel::playlists_with_video( $guid ) as $holder ) { + $chips .= sprintf( + '%2$s', + esc_url( Channel::playlist_url( $holder['key'] ) ), + /* translators: %s: playlist title. */ + esc_html( sprintf( __( 'Playlist: %s', 'jetpack-videopress-pkg' ), '' !== $holder['title'] ? $holder['title'] : __( 'Untitled playlist', 'jetpack-videopress-pkg' ) ) ) + ); + } + if ( '' !== $chips ) { + $markup .= sprintf( '
%s
', $chips ); + } + } + if ( '' === $markup ) { + return ''; + } + + return sprintf( '
%2$s
', get_block_wrapper_attributes( array( 'class' => 'videopress-video-details' ) ), $markup ); + } + + /** + * "Episode 12" by upload order, or "Episode 3 · Playlist name" within a playlist. + * + * @param string $guid The video. + * @param array|null $playlist The playlist the video is played from. + * + * @return string + */ + private static function episode_label( $guid, $playlist ) { + if ( $playlist ) { + $position = array_search( $guid, array_column( $playlist['videos'], 'guid' ), true ); + $label = sprintf( + /* translators: %s: episode number. */ + __( 'Episode %s', 'jetpack-videopress-pkg' ), + number_format_i18n( (int) $position + 1 ) + ); + return '' !== $playlist['title'] ? $label . ' · ' . $playlist['title'] : $label; + } + $order = array_reverse( array_column( Channel_Data::videos(), 'guid' ) ); + $position = array_search( $guid, $order, true ); + /* translators: %s: episode number. */ + return sprintf( __( 'Episode %s', 'jetpack-videopress-pkg' ), number_format_i18n( false === $position ? count( $order ) + 1 : $position + 1 ) ); + } } diff --git a/projects/packages/videopress/src/class-channel.php b/projects/packages/videopress/src/class-channel.php index d7e9b5256145..f1d28050f0f4 100644 --- a/projects/packages/videopress/src/class-channel.php +++ b/projects/packages/videopress/src/class-channel.php @@ -357,17 +357,24 @@ public static function current_video( $context_post_id = 0 ) { } /** - * A video block with `useQueriedVideo` plays the current video. Its title - * and poster are read from VideoPress at render time, not from the blog. + * A video block with `useQueriedVideo` plays the current video, and one + * with `useLatestVideo` the newest upload. Its title and poster are read + * from VideoPress at render time, not from the blog. * * @param array $parsed_block The block being rendered. * @return array */ public static function queried_video_block( $parsed_block ) { - if ( ! isset( $parsed_block['blockName'] ) || 'videopress/video' !== $parsed_block['blockName'] || empty( $parsed_block['attrs']['useQueriedVideo'] ) ) { + if ( ! isset( $parsed_block['blockName'] ) || 'videopress/video' !== $parsed_block['blockName'] ) { + return $parsed_block; + } + if ( ! empty( $parsed_block['attrs']['useQueriedVideo'] ) ) { + $post = self::current_video(); + } elseif ( ! empty( $parsed_block['attrs']['useLatestVideo'] ) ) { + $post = Channel_Data::latest_video(); + } else { return $parsed_block; } - $post = self::current_video(); $guid = $post instanceof WP_Post ? self::guid( $post ) : ''; if ( ! $post instanceof WP_Post || '' === $guid ) { $parsed_block['attrs']['guid'] = ''; @@ -515,6 +522,23 @@ public static function source_playlist( $source, $key = '' ) { return $playlists ? $playlists[0] : null; } + /** + * The published playlists that hold a video. + * + * @param string $guid The video GUID. + * @return array[] + */ + public static function playlists_with_video( $guid ) { + return array_values( + array_filter( + self::playlists(), + function ( $playlist ) use ( $guid ) { + return in_array( $guid, array_column( $playlist['videos'], 'guid' ), true ); + } + ) + ); + } + /** * Add views and upload time to playlist entries, for their meta line. * diff --git a/projects/packages/videopress/src/client/block-editor/blocks/video-details/block.json b/projects/packages/videopress/src/client/block-editor/blocks/video-details/block.json new file mode 100644 index 000000000000..03c28df5ba72 --- /dev/null +++ b/projects/packages/videopress/src/client/block-editor/blocks/video-details/block.json @@ -0,0 +1,65 @@ +{ + "$schema": "https://schemas.wp.org/trunk/block.json", + "apiVersion": 3, + "name": "videopress/video-details", + "version": "0.1.0", + "title": "Video Details", + "category": "media", + "icon": "", + "description": "The title, views, description and playlists of the video being viewed, or of the newest upload.", + "keywords": [ "video", "details", "description", "episode", "videopress" ], + "textdomain": "jetpack-videopress-pkg", + "supports": { + "html": false, + "align": [ "wide", "full" ], + "anchor": true, + "spacing": { + "margin": true, + "padding": true + }, + "color": { + "text": true, + "background": true, + "link": true + }, + "typography": { + "fontSize": true, + "lineHeight": true, + "__experimentalDefaultControls": { + "fontSize": true + } + } + }, + "attributes": { + "source": { + "type": "string", + "enum": [ "queried", "latest" ], + "default": "queried" + }, + "showEyebrow": { + "type": "boolean", + "default": true + }, + "showTitle": { + "type": "boolean", + "default": true + }, + "headingLevel": { + "type": "integer", + "default": 0 + }, + "showMeta": { + "type": "boolean", + "default": true + }, + "showDescription": { + "type": "boolean", + "default": true + }, + "showPlaylists": { + "type": "boolean", + "default": true + } + }, + "editorScript": "file:./index.js" +} diff --git a/projects/packages/videopress/src/client/block-editor/blocks/video-details/edit.tsx b/projects/packages/videopress/src/client/block-editor/blocks/video-details/edit.tsx new file mode 100644 index 000000000000..4bfc26f78d18 --- /dev/null +++ b/projects/packages/videopress/src/client/block-editor/blocks/video-details/edit.tsx @@ -0,0 +1,111 @@ +/** + * WordPress dependencies + */ +import { InspectorControls, useBlockProps } from '@wordpress/block-editor'; +import { PanelBody, SelectControl } from '@wordpress/components'; +import { __, _x } from '@wordpress/i18n'; +import { Stack, SwitchControl } from '@wordpress/ui'; +/** + * Internal dependencies + */ +import ServerPreview from '../../channel/server-preview'; +/** + * Types + */ +import type { BlockEditProps } from '@wordpress/blocks'; + +export type VideoDetailsAttributes = { + source: 'queried' | 'latest'; + showEyebrow: boolean; + showTitle: boolean; + headingLevel: number; + showMeta: boolean; + showDescription: boolean; + showPlaylists: boolean; +}; + +export const HEADING_LEVEL_OPTIONS = [ + { value: '0', label: __( 'Automatic', 'jetpack-videopress-pkg' ) }, + ...[ 1, 2, 3, 4, 5, 6 ].map( level => ( { value: String( level ), label: `H${ level }` } ) ), +]; + +/** + * Video Details block edit component. + * + * @param props - Block edit props. + * @param props.attributes - Block attributes. + * @param props.setAttributes - Attribute setter. + * @return Edit component. + */ +export default function VideoDetailsEdit( { + attributes, + setAttributes, +}: BlockEditProps< VideoDetailsAttributes > ) { + const { source, headingLevel } = attributes; + const toggles: Array< [ keyof VideoDetailsAttributes, string ] > = [ + [ 'showEyebrow', __( 'Episode number', 'jetpack-videopress-pkg' ) ], + [ 'showTitle', __( 'Title', 'jetpack-videopress-pkg' ) ], + [ 'showMeta', __( 'Views and upload date', 'jetpack-videopress-pkg' ) ], + [ 'showDescription', __( 'Description', 'jetpack-videopress-pkg' ) ], + [ 'showPlaylists', __( 'Playlists', 'jetpack-videopress-pkg' ) ], + ]; + + return ( +
+ + + + + setAttributes( { source: value as VideoDetailsAttributes[ 'source' ] } ) + } + /> + setAttributes( { headingLevel: Number( value ) } ) } + /> + + { toggles.map( ( [ key, label ] ) => ( + setAttributes( { [ key ]: checked } ) } + /> + ) ) } + + + + + +
+ ); +} diff --git a/projects/packages/videopress/src/client/block-editor/blocks/video-details/index.tsx b/projects/packages/videopress/src/client/block-editor/blocks/video-details/index.tsx new file mode 100644 index 000000000000..889e82cc2d5d --- /dev/null +++ b/projects/packages/videopress/src/client/block-editor/blocks/video-details/index.tsx @@ -0,0 +1,10 @@ +/** + * Internal dependencies + */ +// Overrides Webpack's publicPath before any lazy chunk loads on wpcom. +import '../../set-webpack-public-path'; +import registerChannelBlock from '../../channel/register'; +import metadata from './block.json'; +import Edit from './edit'; + +registerChannelBlock( metadata, Edit ); diff --git a/projects/packages/videopress/src/client/block-editor/blocks/video-details/test/edit.test.tsx b/projects/packages/videopress/src/client/block-editor/blocks/video-details/test/edit.test.tsx new file mode 100644 index 000000000000..c91ed512a6a9 --- /dev/null +++ b/projects/packages/videopress/src/client/block-editor/blocks/video-details/test/edit.test.tsx @@ -0,0 +1,41 @@ +import { render, screen } from '@testing-library/react'; +import userEvent from '@testing-library/user-event'; +import VideoDetailsEdit from '../edit'; +import type { VideoDetailsAttributes } from '../edit'; +import type { BlockEditProps } from '@wordpress/blocks'; + +jest.mock( '@wordpress/block-editor', () => ( { + useBlockProps: ( props: Record< string, unknown > = {} ) => props, + InspectorControls: ( { children }: { children: React.ReactNode } ) => <>{ children }, +} ) ); + +jest.mock( '../../../channel/server-preview', () => ( { + __esModule: true, + default: () => null, +} ) ); + +describe( 'VideoDetailsEdit', () => { + it.each( [ + { name: 'Description', attribute: 'showDescription', from: true }, + { name: 'Playlists', attribute: 'showPlaylists', from: false }, + ] )( 'switches $name', async ( { name, attribute, from } ) => { + const setAttributes = jest.fn(); + const props = { + attributes: { + source: 'queried', + headingLevel: 0, + showEyebrow: true, + showTitle: true, + showMeta: true, + showDescription: true, + showPlaylists: false, + [ attribute ]: from, + }, + setAttributes, + } as unknown as BlockEditProps< VideoDetailsAttributes >; + render( ); + + await userEvent.click( screen.getByRole( 'switch', { name } ) ); + expect( setAttributes ).toHaveBeenCalledWith( { [ attribute ]: ! from } ); + } ); +} ); diff --git a/projects/packages/videopress/src/client/block-editor/blocks/video-details/test/index.test.ts b/projects/packages/videopress/src/client/block-editor/blocks/video-details/test/index.test.ts new file mode 100644 index 000000000000..36164d1343f8 --- /dev/null +++ b/projects/packages/videopress/src/client/block-editor/blocks/video-details/test/index.test.ts @@ -0,0 +1,23 @@ +import { registerBlockType } from '@wordpress/blocks'; +import '../index'; + +jest.mock( '@wordpress/blocks', () => ( { + registerBlockType: jest.fn(), +} ) ); + +// Keep the registration test from dragging in the whole block editor. +jest.mock( '../edit', () => ( { + __esModule: true, + default: () => null, +} ) ); + +describe( 'video details block registration', () => { + it( 'registers videopress/video-details as a dynamic block', () => { + const [ name, settings ] = ( registerBlockType as jest.Mock ).mock.calls[ 0 ]; + expect( name ).toBe( 'videopress/video-details' ); + expect( settings.title ).toBe( 'Video Details' ); + expect( settings.attributes.source.default ).toBe( 'queried' ); + // The markup comes from the PHP render callback, so nothing is serialized. + expect( settings.save() ).toBeNull(); + } ); +} ); diff --git a/projects/packages/videopress/src/client/block-editor/blocks/video/block.json b/projects/packages/videopress/src/client/block-editor/blocks/video/block.json index e9afc53a2330..95bdbdec5ee8 100644 --- a/projects/packages/videopress/src/client/block-editor/blocks/video/block.json +++ b/projects/packages/videopress/src/client/block-editor/blocks/video/block.json @@ -127,6 +127,10 @@ "useQueriedVideo": { "type": "boolean", "default": false + }, + "useLatestVideo": { + "type": "boolean", + "default": false } }, "textdomain": "jetpack-videopress-pkg", diff --git a/projects/packages/videopress/src/client/block-editor/blocks/video/edit.tsx b/projects/packages/videopress/src/client/block-editor/blocks/video/edit.tsx index 8853b8f544bc..1b8e401269fd 100644 --- a/projects/packages/videopress/src/client/block-editor/blocks/video/edit.tsx +++ b/projects/packages/videopress/src/client/block-editor/blocks/video/edit.tsx @@ -24,7 +24,7 @@ import { import { store as coreStore } from '@wordpress/core-data'; import { useDispatch } from '@wordpress/data'; import { useEffect, useState, useCallback, useRef } from '@wordpress/element'; -import { __ } from '@wordpress/i18n'; +import { __, _x } from '@wordpress/i18n'; import { caption as captionIcon } from '@wordpress/icons'; import clsx from 'clsx'; import debugFactory from 'debug'; @@ -168,6 +168,7 @@ export default function VideoPressEdit( { caption, isExample, useQueriedVideo, + useLatestVideo, } = attributes; const videoPressUrl = getVideoPressUrl( guid, { @@ -422,21 +423,43 @@ export default function VideoPressEdit( { checked={ !! useQueriedVideo } onChange={ ( value: boolean ) => setAttributes( { useQueriedVideo: value } ) } /> + setAttributes( { useLatestVideo: value } ) } + /> ); - if ( useQueriedVideo ) { + if ( useQueriedVideo || useLatestVideo ) { return (
{ queriedVideoControls }
); diff --git a/projects/packages/videopress/src/client/block-editor/blocks/video/types.ts b/projects/packages/videopress/src/client/block-editor/blocks/video/types.ts index ec2dfa36c25d..1ad9292f7e4f 100644 --- a/projects/packages/videopress/src/client/block-editor/blocks/video/types.ts +++ b/projects/packages/videopress/src/client/block-editor/blocks/video/types.ts @@ -121,6 +121,8 @@ export type VideoBlockAttributes = VideoBlockColorAttributesProps & { isExample?: boolean; // Render the video of the page being viewed ( channel video pages ). useQueriedVideo?: boolean; + // Plays the site's newest VideoPress upload, e.g. a channel home's featured video. + useLatestVideo?: boolean; }; export type VideoBlockEditProps = { diff --git a/projects/packages/videopress/tests/php/Channel_Blocks_Test.php b/projects/packages/videopress/tests/php/Channel_Blocks_Test.php index 06944f7abe79..ec6a39ef7fd8 100644 --- a/projects/packages/videopress/tests/php/Channel_Blocks_Test.php +++ b/projects/packages/videopress/tests/php/Channel_Blocks_Test.php @@ -184,6 +184,18 @@ public function test_source_playlist_renders_without_a_channel_theme() { unregister_block_type( 'videopress/playlist' ); } + /** + * Make this request the video page of a video, optionally within a playlist. + * + * @param string $guid The video. + * @param string $list_key The playlist, if any. + */ + private function view_video( $guid, $list_key = '' ) { + set_query_var( Channel::QUERY_VAR, 1 ); + set_query_var( Channel::LIST_VAR, $list_key ); + $GLOBALS['post'] = get_post( $this->videos[ $guid ] ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited + } + /** * Inline and list layouts, skipping items whose value is unknown. */ @@ -253,6 +265,35 @@ public function test_register_registers_the_blocks_where_the_channel_is_enabled( $this->assertSame( array( Channel_Blocks::class, 'render_channel_details' ), $block->render_callback ); } + /** + * On a video page within a playlist: episode number in the playlist, VideoPress + * title and description, full view count and the playlist chip. + */ + public function test_video_details() { + $this->assertSame( '', Channel_Blocks::render_video_details( array() ) ); + + $this->view_video( 'ccccccc3', 'dungeons' ); + $markup = Channel_Blocks::render_video_details( array() ); + + $this->assertStringContainsString( 'Episode 2 · Classic dungeons', $markup ); + $this->assertStringContainsString( '

Deadmines episode

', $markup ); + $this->assertStringContainsString( '
1,500 views · ', $markup ); + $this->assertStringContainsString( '

About Deadmines.

', $markup ); + $this->assertStringContainsString( + 'Playlist: Classic dungeons', + $markup + ); + + // Outside the playlist, the episode number follows upload order. + $this->view_video( 'aaaaaaa1' ); + $this->assertStringContainsString( '>Episode 1<', Channel_Blocks::render_video_details( array( 'showTitle' => false ) ) ); + + // The newest upload, anywhere, with a linked title. + set_query_var( Channel::QUERY_VAR, 0 ); + $latest = Channel_Blocks::render_video_details( array( 'source' => 'latest' ) ); + $this->assertStringContainsString( '

Deadmines episode

', $latest ); + } + /** * The editor gets the playlists ( key, title and videos ) for the pickers and the canvas * previews, before the playlist block's script. diff --git a/projects/packages/videopress/tests/php/Channel_Test.php b/projects/packages/videopress/tests/php/Channel_Test.php index e013fb8d0c32..60c08b1052c0 100644 --- a/projects/packages/videopress/tests/php/Channel_Test.php +++ b/projects/packages/videopress/tests/php/Channel_Test.php @@ -480,7 +480,7 @@ public function test_playlist_records() { } /** - * Video URLs carry the playlist context. + * Video URLs carry the playlist context, and playlist lookups find the playlists holding a video. */ public function test_playlist_urls_and_membership() { $this->index_playlists(); @@ -489,6 +489,7 @@ public function test_playlist_urls_and_membership() { $this->assertSame( $base . '?v=ghiJKL34&list=mc', Channel::video_url( $id, 'mc' ) ); $this->assertSame( $base . '?list=road', Channel::playlist_url( 'road' ) ); + $this->assertSame( array( 'road', 'mc' ), array_column( Channel::playlists_with_video( 'ghiJKL34' ), 'key' ) ); $this->assertTrue( Channel::is_site_url( $base ) ); $this->assertFalse( Channel::is_site_url( 'https://videopress.com/v/ghiJKL34' ) ); } diff --git a/projects/packages/videopress/webpack.config.js b/projects/packages/videopress/webpack.config.js index 8cc3ed079e9a..0497d2036fa3 100644 --- a/projects/packages/videopress/webpack.config.js +++ b/projects/packages/videopress/webpack.config.js @@ -83,6 +83,8 @@ module.exports = [ // Channel blocks: server-rendered, editor script only. 'block-editor/blocks/channel-details/index': './src/client/block-editor/blocks/channel-details/index.tsx', + 'block-editor/blocks/video-details/index': + './src/client/block-editor/blocks/video-details/index.tsx', 'lib/token-bridge': './src/client/lib/token-bridge/index.ts', 'lib/player-bridge': './src/client/lib/player-bridge/index.ts', diff --git a/projects/plugins/jetpack/changelog/add-videopress-video-details-block b/projects/plugins/jetpack/changelog/add-videopress-video-details-block new file mode 100644 index 000000000000..c3445c694e76 --- /dev/null +++ b/projects/plugins/jetpack/changelog/add-videopress-video-details-block @@ -0,0 +1,4 @@ +Significance: minor +Type: enhancement + +VideoPress: Add a Video Details block, and let the VideoPress Video block play the newest upload. diff --git a/projects/plugins/videopress/changelog/add-videopress-video-details-block b/projects/plugins/videopress/changelog/add-videopress-video-details-block new file mode 100644 index 000000000000..618d5db7878a --- /dev/null +++ b/projects/plugins/videopress/changelog/add-videopress-video-details-block @@ -0,0 +1,4 @@ +Significance: minor +Type: added + +Add a Video Details block, and let the VideoPress Video block play the newest upload.