diff --git a/projects/packages/videopress/changelog/add-videopress-playlist-details-block b/projects/packages/videopress/changelog/add-videopress-playlist-details-block new file mode 100644 index 000000000000..c7f8d3945f31 --- /dev/null +++ b/projects/packages/videopress/changelog/add-videopress-playlist-details-block @@ -0,0 +1,4 @@ +Significance: minor +Type: added + +Add a Playlist Details block. diff --git a/projects/packages/videopress/src/class-channel-blocks.php b/projects/packages/videopress/src/class-channel-blocks.php index 3c32110e1d39..33276c5bf31e 100644 --- a/projects/packages/videopress/src/class-channel-blocks.php +++ b/projects/packages/videopress/src/class-channel-blocks.php @@ -23,8 +23,9 @@ class Channel_Blocks { * @var array */ const BLOCKS = array( - 'videopress/channel-details' => 'render_channel_details', - 'videopress/video-details' => 'render_video_details', + 'videopress/channel-details' => 'render_channel_details', + 'videopress/video-details' => 'render_video_details', + 'videopress/playlist-details' => 'render_playlist_details', ); /** @@ -80,7 +81,7 @@ public static function enqueue_editor_data() { return; } $handle = ''; - foreach ( array( Playlist_Index::BLOCK_NAME ) as $name ) { + foreach ( array( Playlist_Index::BLOCK_NAME, 'videopress/playlist-details' ) as $name ) { $block_type = \WP_Block_Type_Registry::get_instance()->get_registered( $name ); if ( $block_type && ! empty( $block_type->editor_script_handles ) ) { $handle = $block_type->editor_script_handles[0]; @@ -344,4 +345,77 @@ private static function episode_label( $guid, $playlist ) { /* translators: %s: episode number. */ return sprintf( __( 'Episode %s', 'jetpack-videopress-pkg' ), number_format_i18n( false === $position ? count( $order ) + 1 : $position + 1 ) ); } + + /** + * Playlist details: cover, eyebrow, title, channel, meta, description, Play all and Share. + * + * @param array $attributes Block attributes. + * + * @return string + */ + public static function render_playlist_details( $attributes ) { + $attributes = is_array( $attributes ) ? $attributes : array(); + $source = isset( $attributes['source'] ) && 'index' === $attributes['source'] ? 'index' : 'queried'; + $playlist = Channel::source_playlist( $source, isset( $attributes['sourcePlaylistId'] ) ? (string) $attributes['sourcePlaylistId'] : '' ); + if ( ! $playlist ) { + return ''; + } + + $count = count( $playlist['videos'] ); + $page_url = Channel::playlist_url( $playlist['key'] ); + $first_id = $count ? Channel::attachment_id( $playlist['videos'][0]['guid'] ) : 0; + $title = '' !== $playlist['title'] ? $playlist['title'] : __( 'Untitled playlist', 'jetpack-videopress-pkg' ); + /* translators: %s: number of videos. */ + $count_label = sprintf( _n( '%s video', '%s videos', $count, 'jetpack-videopress-pkg' ), number_format_i18n( $count ) ); + + $markup = ''; + if ( self::flag( $attributes, 'showCover', true ) ) { + $poster = $count ? Channel_Data::video_details( $playlist['videos'][0]['guid'], $first_id ? $first_id : null )['poster'] : ''; + $markup .= sprintf( + '
%1$s%2$s
', + '' !== $poster ? sprintf( '', esc_url( $poster ) ) : '', + esc_html( $count_label ) + ); + } + if ( self::flag( $attributes, 'showEyebrow', true ) ) { + $markup .= sprintf( '%s', esc_html__( 'Playlist', 'jetpack-videopress-pkg' ) ); + } + $level = self::heading_level( $attributes, 'index' === $source ? 3 : 1 ); + $markup .= sprintf( + '%2$s', + $level, + 'index' === $source ? sprintf( '%2$s', esc_url( $page_url ), esc_html( $title ) ) : esc_html( $title ) + ); + if ( self::flag( $attributes, 'showChannel', true ) ) { + $markup .= sprintf( '%2$s', esc_url( home_url( '/' ) ), esc_html( get_bloginfo( 'name' ) ) ); + } + if ( self::flag( $attributes, 'showMeta', true ) ) { + $post = $playlist['post_id'] ? get_post( $playlist['post_id'] ) : null; + $meta = All_Playlists_Block::updated_meta( + array( + 'post' => $post instanceof WP_Post ? $post : null, + 'videos' => $playlist['videos'], + ) + ); + $markup .= sprintf( '
%s
', esc_html( implode( ' · ', array_filter( array( $count_label, $meta ) ) ) ) ); + } + if ( self::flag( $attributes, 'showDescription', true ) && '' !== $playlist['description'] ) { + $markup .= sprintf( '

%s

', esc_html( $playlist['description'] ) ); + } + if ( self::flag( $attributes, 'showActions', true ) ) { + $play_url = $first_id ? Channel::video_url( $first_id, $playlist['key'] ) : ''; + $markup .= sprintf( + '
%1$s
', + '' !== $play_url ? sprintf( '
%2$s
', esc_url( $play_url ), esc_html__( 'Play all', 'jetpack-videopress-pkg' ) ) : '', + esc_url( $page_url ), + esc_html__( 'Share', 'jetpack-videopress-pkg' ) + ); + } + + return sprintf( + '
%2$s
', + get_block_wrapper_attributes( array( 'class' => 'videopress-playlist-details is-source-' . $source ) ), + $markup + ); + } } diff --git a/projects/packages/videopress/src/client/block-editor/blocks/playlist-details/block.json b/projects/packages/videopress/src/client/block-editor/blocks/playlist-details/block.json new file mode 100644 index 000000000000..ec4cf84d814c --- /dev/null +++ b/projects/packages/videopress/src/client/block-editor/blocks/playlist-details/block.json @@ -0,0 +1,73 @@ +{ + "$schema": "https://schemas.wp.org/trunk/block.json", + "apiVersion": 3, + "name": "videopress/playlist-details", + "version": "0.1.0", + "title": "Playlist Details", + "category": "media", + "icon": "", + "description": "A playlist’s cover, title, length, description, Play all and Share: the playlist being viewed, or one you pick.", + "keywords": [ "playlist", "details", "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", "index" ], + "default": "queried" + }, + "sourcePlaylistId": { + "type": "string", + "default": "" + }, + "showCover": { + "type": "boolean", + "default": true + }, + "showEyebrow": { + "type": "boolean", + "default": true + }, + "headingLevel": { + "type": "integer", + "default": 0 + }, + "showChannel": { + "type": "boolean", + "default": true + }, + "showMeta": { + "type": "boolean", + "default": true + }, + "showDescription": { + "type": "boolean", + "default": true + }, + "showActions": { + "type": "boolean", + "default": true + } + }, + "editorScript": "file:./index.js" +} diff --git a/projects/packages/videopress/src/client/block-editor/blocks/playlist-details/edit.tsx b/projects/packages/videopress/src/client/block-editor/blocks/playlist-details/edit.tsx new file mode 100644 index 000000000000..d2a66e905ec5 --- /dev/null +++ b/projects/packages/videopress/src/client/block-editor/blocks/playlist-details/edit.tsx @@ -0,0 +1,123 @@ +/** + * 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 { PlaylistPicker } from '../../channel/editor-controls'; +import ServerPreview from '../../channel/server-preview'; +import { HEADING_LEVEL_OPTIONS } from '../video-details/edit'; +/** + * Types + */ +import type { BlockEditProps } from '@wordpress/blocks'; + +export type PlaylistDetailsAttributes = { + source: 'queried' | 'index'; + sourcePlaylistId: string; + showCover: boolean; + showEyebrow: boolean; + headingLevel: number; + showChannel: boolean; + showMeta: boolean; + showDescription: boolean; + showActions: boolean; +}; + +/** + * Playlist 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 PlaylistDetailsEdit( { + attributes, + setAttributes, +}: BlockEditProps< PlaylistDetailsAttributes > ) { + const { source, sourcePlaylistId, headingLevel } = attributes; + const toggles: Array< [ keyof PlaylistDetailsAttributes, string ] > = [ + [ 'showCover', __( 'Cover', 'jetpack-videopress-pkg' ) ], + [ 'showEyebrow', __( '“Playlist” label', 'jetpack-videopress-pkg' ) ], + [ 'showChannel', __( 'Channel name', 'jetpack-videopress-pkg' ) ], + [ 'showMeta', __( 'Length and last update', 'jetpack-videopress-pkg' ) ], + [ 'showDescription', __( 'Description', 'jetpack-videopress-pkg' ) ], + [ 'showActions', __( 'Play all and Share buttons', 'jetpack-videopress-pkg' ) ], + ]; + + return ( +
+ + + + + setAttributes( { source: value as PlaylistDetailsAttributes[ 'source' ] } ) + } + /> + { 'index' === source && ( + setAttributes( { sourcePlaylistId: value } ) } + /> + ) } + setAttributes( { headingLevel: Number( value ) } ) } + /> + + { toggles.map( ( [ key, label ] ) => ( + setAttributes( { [ key ]: checked } ) } + /> + ) ) } + + + + + +
+ ); +} diff --git a/projects/packages/videopress/src/client/block-editor/blocks/playlist-details/index.tsx b/projects/packages/videopress/src/client/block-editor/blocks/playlist-details/index.tsx new file mode 100644 index 000000000000..889e82cc2d5d --- /dev/null +++ b/projects/packages/videopress/src/client/block-editor/blocks/playlist-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/playlist-details/test/edit.test.tsx b/projects/packages/videopress/src/client/block-editor/blocks/playlist-details/test/edit.test.tsx new file mode 100644 index 000000000000..4344bfa8e596 --- /dev/null +++ b/projects/packages/videopress/src/client/block-editor/blocks/playlist-details/test/edit.test.tsx @@ -0,0 +1,43 @@ +import { render, screen } from '@testing-library/react'; +import userEvent from '@testing-library/user-event'; +import PlaylistDetailsEdit from '../edit'; +import type { PlaylistDetailsAttributes } 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( 'PlaylistDetailsEdit', () => { + it.each( [ + { name: 'Cover', attribute: 'showCover', from: true }, + { name: 'Play all and Share buttons', attribute: 'showActions', from: false }, + ] )( 'switches $name', async ( { name, attribute, from } ) => { + const setAttributes = jest.fn(); + const props = { + attributes: { + source: 'queried', + sourcePlaylistId: '', + headingLevel: 0, + showCover: true, + showEyebrow: true, + showChannel: true, + showMeta: true, + showDescription: true, + showActions: true, + [ attribute ]: from, + }, + setAttributes, + } as unknown as BlockEditProps< PlaylistDetailsAttributes >; + render( ); + + await userEvent.click( screen.getByRole( 'switch', { name } ) ); + expect( setAttributes ).toHaveBeenCalledWith( { [ attribute ]: ! from } ); + } ); +} ); diff --git a/projects/packages/videopress/src/client/block-editor/blocks/playlist-details/test/index.test.ts b/projects/packages/videopress/src/client/block-editor/blocks/playlist-details/test/index.test.ts new file mode 100644 index 000000000000..ac1176e04bdd --- /dev/null +++ b/projects/packages/videopress/src/client/block-editor/blocks/playlist-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( 'playlist details block registration', () => { + it( 'registers videopress/playlist-details as a dynamic block', () => { + const [ name, settings ] = ( registerBlockType as jest.Mock ).mock.calls[ 0 ]; + expect( name ).toBe( 'videopress/playlist-details' ); + expect( settings.title ).toBe( 'Playlist 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/tests/php/Channel_Blocks_Test.php b/projects/packages/videopress/tests/php/Channel_Blocks_Test.php index ec6a39ef7fd8..bf6ce8aa1ef2 100644 --- a/projects/packages/videopress/tests/php/Channel_Blocks_Test.php +++ b/projects/packages/videopress/tests/php/Channel_Blocks_Test.php @@ -294,6 +294,34 @@ public function test_video_details() { $this->assertStringContainsString( '

Deadmines episode

', $latest ); } + /** + * The queried playlist's cover, meta and actions; Play all opens the first video in the playlist. + */ + public function test_playlist_details() { + $this->assertSame( '', Channel_Blocks::render_playlist_details( array() ) ); + + set_query_var( Channel::PLAYLIST_QUERY_VAR, 1 ); + set_query_var( Channel::LIST_VAR, 'dungeons' ); + $markup = Channel_Blocks::render_playlist_details( array() ); + + $this->assertStringContainsString( 'assertStringContainsString( '2 videos', $markup ); + $this->assertStringContainsString( '

Classic dungeons

', $markup ); + $this->assertStringContainsString( '
2 videos · Updated ', $markup ); + $this->assertStringContainsString( ' · 20 min
', $markup ); + $this->assertStringContainsString( 'videopress-playlist-details__play" href="' . esc_url( Channel::video_url( $this->videos['bbbbbbb2'], 'dungeons' ) ) . '"', $markup ); + $this->assertStringContainsString( 'data-share-url="' . esc_url( Channel::playlist_url( 'dungeons' ) ) . '"', $markup ); + + $picked = Channel_Blocks::render_playlist_details( + array( + 'source' => 'index', + 'showCover' => false, + ) + ); + $this->assertStringContainsString( '