Skip to content
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: added

Add a Playlist Details block.
80 changes: 77 additions & 3 deletions projects/packages/videopress/src/class-channel-blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ class Channel_Blocks {
* @var array<string, string>
*/
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',
);

/**
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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(
'<div class="videopress-playlist-details__cover"><span class="videopress-playlist-details__deck" aria-hidden="true"></span><span class="videopress-playlist-details__frame">%1$s<span class="videopress-playlist-details__badge">%2$s</span></span></div>',
'' !== $poster ? sprintf( '<img class="videopress-playlist-details__poster" src="%s" alt="" loading="lazy" />', esc_url( $poster ) ) : '',
esc_html( $count_label )
);
}
if ( self::flag( $attributes, 'showEyebrow', true ) ) {
$markup .= sprintf( '<span class="videopress-playlist-details__eyebrow">%s</span>', esc_html__( 'Playlist', 'jetpack-videopress-pkg' ) );
}
$level = self::heading_level( $attributes, 'index' === $source ? 3 : 1 );
$markup .= sprintf(
'<h%1$d class="videopress-playlist-details__title">%2$s</h%1$d>',
$level,
'index' === $source ? sprintf( '<a href="%1$s">%2$s</a>', esc_url( $page_url ), esc_html( $title ) ) : esc_html( $title )
);
if ( self::flag( $attributes, 'showChannel', true ) ) {
$markup .= sprintf( '<a class="videopress-playlist-details__channel" href="%1$s">%2$s</a>', 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( '<div class="videopress-playlist-details__meta">%s</div>', esc_html( implode( ' · ', array_filter( array( $count_label, $meta ) ) ) ) );
}
if ( self::flag( $attributes, 'showDescription', true ) && '' !== $playlist['description'] ) {
$markup .= sprintf( '<p class="videopress-playlist-details__description">%s</p>', esc_html( $playlist['description'] ) );
}
if ( self::flag( $attributes, 'showActions', true ) ) {
$play_url = $first_id ? Channel::video_url( $first_id, $playlist['key'] ) : '';
$markup .= sprintf(
'<div class="videopress-playlist-details__actions wp-block-buttons">%1$s<div class="wp-block-button is-style-outline"><a class="wp-block-button__link wp-element-button videopress-playlist-details__share" href="%2$s" data-share-url="%2$s">%3$s</a></div></div>',
'' !== $play_url ? sprintf( '<div class="wp-block-button"><a class="wp-block-button__link wp-element-button videopress-playlist-details__play" href="%1$s">%2$s</a></div>', esc_url( $play_url ), esc_html__( 'Play all', 'jetpack-videopress-pkg' ) ) : '',
esc_url( $page_url ),
esc_html__( 'Share', 'jetpack-videopress-pkg' )
);
}

return sprintf(
'<div %1$s>%2$s</div>',
get_block_wrapper_attributes( array( 'class' => 'videopress-playlist-details is-source-' . $source ) ),
$markup
);
}
}
Original file line number Diff line number Diff line change
@@ -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": "<svg viewBox='0 0 24 24' width='24' height='24' xmlns='http://www.w3.org/2000/svg'><path fill-rule='evenodd' clip-rule='evenodd' d='M4 5.5h9A1.5 1.5 0 0 1 14.5 7v6A1.5 1.5 0 0 1 13 14.5H4A1.5 1.5 0 0 1 2.5 13V7A1.5 1.5 0 0 1 4 5.5Zm3.5 2.6v3.8L10.7 10 7.5 8.1ZM16.5 7H21v1.5h-4.5V7Zm0 4H21v1.5h-4.5V11Zm-14 6H21v1.5H2.5V17Z'></path></svg>",
"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"
}
Original file line number Diff line number Diff line change
@@ -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 (
<div { ...useBlockProps() }>
<InspectorControls>
<PanelBody title={ __( 'Playlist', 'jetpack-videopress-pkg' ) }>
<Stack direction="column" gap="md">
<SelectControl
__next40pxDefaultSize
__nextHasNoMarginBottom
label={ __( 'Playlist', 'jetpack-videopress-pkg' ) }
value={ source }
options={ [
{
value: 'queried',
label: __( 'The playlist being viewed', 'jetpack-videopress-pkg' ),
},
{
value: 'index',
label: __( 'A playlist on this site', 'jetpack-videopress-pkg' ),
},
] }
onChange={ value =>
setAttributes( { source: value as PlaylistDetailsAttributes[ 'source' ] } )
}
/>
{ 'index' === source && (
<PlaylistPicker
value={ sourcePlaylistId }
onChange={ value => setAttributes( { sourcePlaylistId: value } ) }
/>
) }
<SelectControl
__next40pxDefaultSize
__nextHasNoMarginBottom
label={ __( 'Title heading level', 'jetpack-videopress-pkg' ) }
value={ String( headingLevel ) }
options={ HEADING_LEVEL_OPTIONS }
onChange={ value => setAttributes( { headingLevel: Number( value ) } ) }
/>
<Stack direction="column" gap="sm">
{ toggles.map( ( [ key, label ] ) => (
<SwitchControl
key={ key }
label={ label }
checked={ !! attributes[ key ] }
onCheckedChange={ checked => setAttributes( { [ key ]: checked } ) }
/>
) ) }
</Stack>
</Stack>
</PanelBody>
</InspectorControls>
<ServerPreview
block="videopress/playlist-details"
attributes={ attributes }
label={ __( 'Playlist Details', 'jetpack-videopress-pkg' ) }
emptyInstructions={
'index' === source
? __(
'Publish a Video Playlist with a title to show it here.',
'jetpack-videopress-pkg'
)
: _x(
'On a playlist page, shows that playlist’s cover, title and details.',
'',
'jetpack-videopress-pkg'
)
}
/>
</div>
);
}
Original file line number Diff line number Diff line change
@@ -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 );
Original file line number Diff line number Diff line change
@@ -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( <PlaylistDetailsEdit { ...props } /> );

await userEvent.click( screen.getByRole( 'switch', { name } ) );
expect( setAttributes ).toHaveBeenCalledWith( { [ attribute ]: ! from } );
} );
} );
Original file line number Diff line number Diff line change
@@ -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();
} );
} );
Loading
Loading