diff --git a/projects/packages/videopress/changelog/add-videopress-channel-details-block b/projects/packages/videopress/changelog/add-videopress-channel-details-block new file mode 100644 index 000000000000..b91f05a62a03 --- /dev/null +++ b/projects/packages/videopress/changelog/add-videopress-channel-details-block @@ -0,0 +1,4 @@ +Significance: minor +Type: added + +Add a Channel Details block to channel sites, with the channel's handle, subscribers, videos, playlists, views and start date. diff --git a/projects/packages/videopress/src/class-channel-blocks.php b/projects/packages/videopress/src/class-channel-blocks.php index 629370a8c760..ac4f6ba051fe 100644 --- a/projects/packages/videopress/src/class-channel-blocks.php +++ b/projects/packages/videopress/src/class-channel-blocks.php @@ -8,25 +8,58 @@ namespace Automattic\Jetpack\VideoPress; /** - * Shared pieces of the blocks a VideoPress channel theme builds its pages - * from: for now, the Video Playlist block's `source` ( a playlist published - * on the site ) and the data its playlist picker reads in the editor. + * Registers and renders the server-side blocks a VideoPress channel theme + * builds its pages from, on sites with the channel feature enabled. Each renders + * nothing when it has nothing to show. Also resolves the Video Playlist block's + * `source` ( a playlist published on the site ) on any theme. */ class Channel_Blocks { /** - * Resolve the Video Playlist block's `source` when it renders, and hand the editor the picker - * data. Like the All Playlists block, only where the Video Playlist block is; any theme. + * Blocks, by name, with their render method. + * + * @var array + */ + const BLOCKS = array( + 'videopress/channel-details' => 'render_channel_details', + ); + + /** + * Channel details items, the first ones being the default selection. + * + * @var string[] + */ + const CHANNEL_ITEMS = array( 'handle', 'subscribers', 'videos', 'playlists', 'views', 'joined' ); + const DEFAULT_CHANNEL_ITEMS = array( 'handle', 'subscribers', 'videos', 'playlists' ); + + /** + * Register the Video Playlist block's `source` on any theme, and the blocks where the channel + * feature is enabled ( see Channel::is_enabled() ). Like the All Playlists block, only where + * the Video Playlist block is. + * + * @param string|null $build_dir Directory holding each block's block.json; tests can point it at fixtures. * * @return void */ - public static function register() { + public static function register( $build_dir = null ) { if ( ! \WP_Block_Type_Registry::get_instance()->is_registered( Playlist_Index::BLOCK_NAME ) ) { return; } if ( ! has_filter( 'render_block_data', array( Channel::class, 'playlist_source_block' ) ) ) { add_filter( 'render_block_data', array( Channel::class, 'playlist_source_block' ) ); } + if ( null === $build_dir ) { + $build_dir = __DIR__ . '/../build/block-editor/blocks'; + } + + foreach ( Channel::is_enabled() ? self::BLOCKS : array() as $name => $method ) { + $metadata_file = $build_dir . '/' . substr( $name, strlen( 'videopress/' ) ) . '/block.json'; + if ( ! file_exists( $metadata_file ) || \WP_Block_Type_Registry::get_instance()->is_registered( $name ) ) { + continue; + } + register_block_type( $metadata_file, array( 'render_callback' => array( __CLASS__, $method ) ) ); + } + if ( ! has_action( 'enqueue_block_editor_assets', array( __CLASS__, 'enqueue_editor_data' ) ) ) { add_action( 'enqueue_block_editor_assets', array( __CLASS__, 'enqueue_editor_data' ) ); } @@ -69,4 +102,116 @@ public static function enqueue_editor_data() { 'before' ); } + + /** + * Channel details: the handle, subscribers, videos, playlists, views and join date. + * + * @param array $attributes Block attributes. + * + * @return string + */ + public static function render_channel_details( $attributes ) { + $attributes = is_array( $attributes ) ? $attributes : array(); + $items = isset( $attributes['items'] ) && is_array( $attributes['items'] ) + ? array_values( array_intersect( array_unique( $attributes['items'] ), self::CHANNEL_ITEMS ) ) + : self::DEFAULT_CHANNEL_ITEMS; + $layout = isset( $attributes['layout'] ) && 'list' === $attributes['layout'] ? 'list' : 'inline'; + + $markup = ''; + foreach ( $items as $item ) { + $value = self::channel_item( $item ); + if ( null === $value ) { + continue; + } + $markup .= 'list' === $layout + ? sprintf( + '
%2$s
%3$s
', + esc_attr( $item ), + esc_html( $value['label'] ), + esc_html( $value['value'] ) + ) + : sprintf( + '%2$s', + esc_attr( $item ), + esc_html( $value['inline'] ) + ); + } + if ( '' === $markup ) { + return ''; + } + + $tag = 'list' === $layout ? 'dl' : 'div'; + return sprintf( + '<%1$s %2$s>%3$s', + $tag, + get_block_wrapper_attributes( array( 'class' => 'videopress-channel-details is-layout-' . $layout ) ), + $markup + ); + } + + /** + * One channel details item, or null when its value is unknown. + * + * @param string $item One of CHANNEL_ITEMS. + * + * @return array{label: string, value: string, inline: string}|null + */ + private static function channel_item( $item ) { + switch ( $item ) { + case 'handle': + $handle = Channel_Data::handle(); + return '' === $handle ? null : array( + 'label' => __( 'Handle', 'jetpack-videopress-pkg' ), + 'value' => $handle, + 'inline' => $handle, + ); + case 'subscribers': + $count = Channel_Data::subscriber_count(); + if ( null === $count ) { + return null; + } + return array( + 'label' => __( 'Subscribers', 'jetpack-videopress-pkg' ), + 'value' => Channel_Data::compact_number( $count ), + /* translators: %s: number of subscribers, e.g. "48K". */ + 'inline' => sprintf( _n( '%s subscriber', '%s subscribers', $count, 'jetpack-videopress-pkg' ), Channel_Data::compact_number( $count ) ), + ); + case 'videos': + $count = Channel_Data::video_count(); + return array( + 'label' => __( 'Videos', 'jetpack-videopress-pkg' ), + 'value' => number_format_i18n( $count ), + /* translators: %s: number of videos. */ + 'inline' => sprintf( _n( '%s video', '%s videos', $count, 'jetpack-videopress-pkg' ), number_format_i18n( $count ) ), + ); + case 'playlists': + $count = count( Channel::playlists() ); + return array( + 'label' => __( 'Playlists', 'jetpack-videopress-pkg' ), + 'value' => number_format_i18n( $count ), + /* translators: %s: number of playlists. */ + 'inline' => sprintf( _n( '%s playlist', '%s playlists', $count, 'jetpack-videopress-pkg' ), number_format_i18n( $count ) ), + ); + case 'views': + $count = Channel_Data::total_views(); + return array( + 'label' => __( 'Total views', 'jetpack-videopress-pkg' ), + 'value' => Channel_Data::compact_number( $count ), + /* translators: %s: number of views, e.g. "1.2M". */ + 'inline' => sprintf( _n( '%s view', '%s views', $count, 'jetpack-videopress-pkg' ), Channel_Data::compact_number( $count ) ), + ); + default: // 'joined', the last of CHANNEL_ITEMS. + $time = Channel_Data::joined(); + if ( ! $time ) { + return null; + } + $date = date_i18n( get_option( 'date_format' ), $time ); + return array( + 'label' => __( 'Joined', 'jetpack-videopress-pkg' ), + 'value' => $date, + /* translators: %s: date the channel started. */ + 'inline' => sprintf( __( 'Joined %s', 'jetpack-videopress-pkg' ), $date ), + ); + } + } } diff --git a/projects/packages/videopress/src/class-initializer.php b/projects/packages/videopress/src/class-initializer.php index 956a320589ed..d0b29a062369 100644 --- a/projects/packages/videopress/src/class-initializer.php +++ b/projects/packages/videopress/src/class-initializer.php @@ -297,7 +297,7 @@ public static function register_videopress_blocks() { // Register All Playlists block. self::register_videopress_all_playlists_block(); - // Register the channel blocks' shared pieces: the playlist block's `source` and the editor data its picker reads. + // Register the playlist block's `source` and, where the channel feature is enabled, the channel blocks. Channel_Blocks::register(); } diff --git a/projects/packages/videopress/src/client/block-editor/blocks/channel-details/block.json b/projects/packages/videopress/src/client/block-editor/blocks/channel-details/block.json new file mode 100644 index 000000000000..82bcd8008809 --- /dev/null +++ b/projects/packages/videopress/src/client/block-editor/blocks/channel-details/block.json @@ -0,0 +1,49 @@ +{ + "$schema": "https://schemas.wp.org/trunk/block.json", + "apiVersion": 3, + "name": "videopress/channel-details", + "version": "0.1.0", + "title": "Channel Details", + "category": "media", + "icon": "", + "description": "The channel’s handle, subscribers, videos, playlists, views and join date.", + "keywords": [ "channel", "stats", "subscribers", "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": { + "items": { + "type": "array", + "items": { + "type": "string", + "enum": [ "handle", "subscribers", "videos", "playlists", "views", "joined" ] + }, + "default": [ "handle", "subscribers", "videos", "playlists" ] + }, + "layout": { + "type": "string", + "enum": [ "inline", "list" ], + "default": "inline" + } + }, + "editorScript": "file:./index.js" +} diff --git a/projects/packages/videopress/src/client/block-editor/blocks/channel-details/edit.tsx b/projects/packages/videopress/src/client/block-editor/blocks/channel-details/edit.tsx new file mode 100644 index 000000000000..c0affbec897e --- /dev/null +++ b/projects/packages/videopress/src/client/block-editor/blocks/channel-details/edit.tsx @@ -0,0 +1,92 @@ +/** + * WordPress dependencies + */ +import { InspectorControls, useBlockProps } from '@wordpress/block-editor'; +import { PanelBody, SelectControl } from '@wordpress/components'; +import { __ } from '@wordpress/i18n'; +import { CheckboxControl, Stack } from '@wordpress/ui'; +/** + * Internal dependencies + */ +import ServerPreview from '../../channel/server-preview'; +/** + * Types + */ +import type { BlockEditProps } from '@wordpress/blocks'; + +export type ChannelItem = 'handle' | 'subscribers' | 'videos' | 'playlists' | 'views' | 'joined'; + +export type ChannelDetailsAttributes = { + items: ChannelItem[]; + layout: 'inline' | 'list'; +}; + +const ITEM_LABELS: Record< ChannelItem, string > = { + handle: __( 'Handle', 'jetpack-videopress-pkg' ), + subscribers: __( 'Subscribers', 'jetpack-videopress-pkg' ), + videos: __( 'Videos', 'jetpack-videopress-pkg' ), + playlists: __( 'Playlists', 'jetpack-videopress-pkg' ), + views: __( 'Total views', 'jetpack-videopress-pkg' ), + joined: __( 'Joined', 'jetpack-videopress-pkg' ), +}; + +/** + * Channel 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 ChannelDetailsEdit( { + attributes, + setAttributes, +}: BlockEditProps< ChannelDetailsAttributes > ) { + const { items, layout } = attributes; + const toggle = ( item: ChannelItem, checked: boolean ) => + setAttributes( { + items: ( Object.keys( ITEM_LABELS ) as ChannelItem[] ).filter( key => + key === item ? checked : items.includes( key ) + ), + } ); + + return ( +
+ + + + + setAttributes( { layout: value as ChannelDetailsAttributes[ 'layout' ] } ) + } + /> + + { ( Object.keys( ITEM_LABELS ) as ChannelItem[] ).map( item => ( + toggle( item, checked ) } + /> + ) ) } + + + + + +
+ ); +} diff --git a/projects/packages/videopress/src/client/block-editor/blocks/channel-details/index.tsx b/projects/packages/videopress/src/client/block-editor/blocks/channel-details/index.tsx new file mode 100644 index 000000000000..889e82cc2d5d --- /dev/null +++ b/projects/packages/videopress/src/client/block-editor/blocks/channel-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/channel-details/test/edit.test.tsx b/projects/packages/videopress/src/client/block-editor/blocks/channel-details/test/edit.test.tsx new file mode 100644 index 000000000000..daca0b82f3ec --- /dev/null +++ b/projects/packages/videopress/src/client/block-editor/blocks/channel-details/test/edit.test.tsx @@ -0,0 +1,43 @@ +import { render, screen } from '@testing-library/react'; +import userEvent from '@testing-library/user-event'; +import ChannelDetailsEdit from '../edit'; +import type { ChannelDetailsAttributes } 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, +} ) ); + +/** + * Render the edit component with the given details picked. + * + * @param items - Picked details. + * @param setAttributes - setAttributes mock. + */ +function renderEdit( items: ChannelDetailsAttributes[ 'items' ], setAttributes: jest.Mock ) { + const props = { + attributes: { items, layout: 'inline' }, + setAttributes, + } as unknown as BlockEditProps< ChannelDetailsAttributes >; + + render( ); +} + +describe( 'ChannelDetailsEdit', () => { + it.each( [ + { name: 'Subscribers', expected: [ 'handle', 'subscribers', 'videos' ] }, + { name: 'Videos', expected: [ 'handle' ] }, + ] )( 'toggles $name and keeps the details in display order', async ( { name, expected } ) => { + const setAttributes = jest.fn(); + renderEdit( [ 'videos', 'handle' ], setAttributes ); + + await userEvent.click( screen.getByRole( 'checkbox', { name } ) ); + expect( setAttributes ).toHaveBeenCalledWith( { items: expected } ); + } ); +} ); diff --git a/projects/packages/videopress/src/client/block-editor/blocks/channel-details/test/index.test.ts b/projects/packages/videopress/src/client/block-editor/blocks/channel-details/test/index.test.ts new file mode 100644 index 000000000000..5b16a69dcc12 --- /dev/null +++ b/projects/packages/videopress/src/client/block-editor/blocks/channel-details/test/index.test.ts @@ -0,0 +1,34 @@ +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, +} ) ); + +const registerBlockTypeMock = registerBlockType as jest.Mock; + +describe( 'channel details block registration', () => { + it( 'registers videopress/channel-details as a dynamic block', () => { + expect( registerBlockTypeMock ).toHaveBeenCalledTimes( 1 ); + + const [ name, settings ] = registerBlockTypeMock.mock.calls[ 0 ]; + expect( name ).toBe( 'videopress/channel-details' ); + expect( settings.title ).toBe( 'Channel Details' ); + expect( settings.category ).toBe( 'media' ); + expect( settings.icon ).toBeDefined(); + expect( settings.attributes.items.default ).toEqual( [ + 'handle', + 'subscribers', + 'videos', + 'playlists', + ] ); + // 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/channel/register.tsx b/projects/packages/videopress/src/client/block-editor/channel/register.tsx new file mode 100644 index 000000000000..e6bfc7f4fed8 --- /dev/null +++ b/projects/packages/videopress/src/client/block-editor/channel/register.tsx @@ -0,0 +1,38 @@ +/** + * WordPress dependencies + */ +import { registerBlockType } from '@wordpress/blocks'; +/** + * Internal dependencies + */ +import { VideoPressIcon as icon } from '../blocks/video/components/icons'; +/** + * Types + */ +import type { BlockConfiguration, BlockEditProps } from '@wordpress/blocks'; +import type { ComponentType } from 'react'; + +/** + * Register a server-rendered channel block from its metadata. + * + * @param metadata - The block.json contents. + * @param metadata.name - Block name. + * @param metadata.title - Block title. + * @param metadata.category - Block category. + * @param metadata.attributes - Attribute definitions. + * @param edit - Edit component. + */ +export default function registerChannelBlock< T extends Record< string, unknown > >( + metadata: { name: string; title: string; category: string; attributes: unknown }, + edit: ComponentType< BlockEditProps< T > > +) { + registerBlockType< T >( metadata.name, { + title: metadata.title, + category: metadata.category, + attributes: metadata.attributes as BlockConfiguration< T >[ 'attributes' ], + icon, + edit, + // Dynamic block: the markup comes from Channel_Blocks on the server. + save: () => null, + } ); +} diff --git a/projects/packages/videopress/tests/php/Channel_Blocks_Test.php b/projects/packages/videopress/tests/php/Channel_Blocks_Test.php index e042897a7aea..06944f7abe79 100644 --- a/projects/packages/videopress/tests/php/Channel_Blocks_Test.php +++ b/projects/packages/videopress/tests/php/Channel_Blocks_Test.php @@ -184,6 +184,75 @@ public function test_source_playlist_renders_without_a_channel_theme() { unregister_block_type( 'videopress/playlist' ); } + /** + * Inline and list layouts, skipping items whose value is unknown. + */ + public function test_channel_details() { + add_filter( 'videopress_channel_handle', fn() => '@jtf' ); + add_filter( 'videopress_channel_subscriber_count', fn() => 48210 ); + + $inline = Channel_Blocks::render_channel_details( array() ); + $this->assertStringContainsString( 'videopress-channel-details is-layout-inline"', $inline ); + $this->assertStringContainsString( + '@jtf48K subscribers3 videos1 playlist', + $inline + ); + + add_filter( 'videopress_channel_subscriber_count', '__return_null', 20 ); + $list = Channel_Blocks::render_channel_details( + array( + 'items' => array( 'subscribers', 'views', 'bogus' ), + 'layout' => 'list', + ) + ); + $this->assertStringStartsWith( '
assertStringContainsString( '
Total views
1.5K
', $list ); + $this->assertStringNotContainsString( 'is-subscribers', $list ); + $this->assertStringNotContainsString( 'bogus', $list ); + } + + /** + * The join date shows in the site's date format, and an unknown date leaves no empty block. + */ + public function test_channel_details_joined() { + update_option( 'date_format', 'F j, Y' ); + $attributes = array( 'items' => array( 'joined' ) ); + + add_filter( 'videopress_channel_joined', '__return_zero' ); + $this->assertSame( '', Channel_Blocks::render_channel_details( $attributes ) ); + + add_filter( 'videopress_channel_joined', fn() => gmmktime( 12, 0, 0, 3, 5, 2024 ), 20 ); + $this->assertStringContainsString( 'Joined March 5, 2024', Channel_Blocks::render_channel_details( $attributes ) ); + $this->assertStringContainsString( '
Joined
March 5, 2024
', Channel_Blocks::render_channel_details( $attributes + array( 'layout' => 'list' ) ) ); + } + + /** + * Each block registers from its block.json in the build, rendered by Channel_Blocks, only where + * the channel feature is enabled. + */ + public function test_register_registers_the_blocks_where_the_channel_is_enabled() { + $build = get_temp_dir() . uniqid( 'videopress-channel-blocks-' ); + $metadata = json_decode( (string) file_get_contents( __DIR__ . '/../../src/client/block-editor/blocks/channel-details/block.json' ), true ); + unset( $metadata['editorScript'] ); // The built script isn't there to register. + mkdir( $build . '/channel-details', 0777, true ); + file_put_contents( $build . '/channel-details/block.json', wp_json_encode( $metadata, JSON_UNESCAPED_SLASHES ) ); + + register_block_type( 'videopress/playlist', array() ); + remove_all_filters( 'videopress_channel_enabled' ); + Channel_Blocks::register( $build ); + $this->assertFalse( \WP_Block_Type_Registry::get_instance()->is_registered( 'videopress/channel-details' ) ); + + add_filter( 'videopress_channel_enabled', '__return_true' ); + Channel_Blocks::register( $build ); + unlink( $build . '/channel-details/block.json' ); + rmdir( $build . '/channel-details' ); + rmdir( $build ); + + $block = \WP_Block_Type_Registry::get_instance()->get_registered( 'videopress/channel-details' ); + $this->assertInstanceOf( \WP_Block_Type::class, $block ); + $this->assertSame( array( Channel_Blocks::class, 'render_channel_details' ), $block->render_callback ); + } + /** * 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/webpack.config.js b/projects/packages/videopress/webpack.config.js index 34f90104b4d6..8cc3ed079e9a 100644 --- a/projects/packages/videopress/webpack.config.js +++ b/projects/packages/videopress/webpack.config.js @@ -80,6 +80,10 @@ module.exports = [ 'block-editor/blocks/all-playlists/view': './src/client/block-editor/blocks/all-playlists/view.ts', + // Channel blocks: server-rendered, editor script only. + 'block-editor/blocks/channel-details/index': + './src/client/block-editor/blocks/channel-details/index.tsx', + 'lib/token-bridge': './src/client/lib/token-bridge/index.ts', 'lib/player-bridge': './src/client/lib/player-bridge/index.ts', 'lib/inline-player': './src/client/lib/inline-player/index.ts', diff --git a/projects/plugins/jetpack/changelog/add-videopress-channel-details-block b/projects/plugins/jetpack/changelog/add-videopress-channel-details-block new file mode 100644 index 000000000000..d3159509d4ec --- /dev/null +++ b/projects/plugins/jetpack/changelog/add-videopress-channel-details-block @@ -0,0 +1,4 @@ +Significance: minor +Type: enhancement + +VideoPress: Add a Channel Details block to channel sites, with the channel's handle, subscribers, videos, playlists, views and start date. diff --git a/projects/plugins/jetpack/extensions/extended-blocks/videopress-video/videopress-video.php b/projects/plugins/jetpack/extensions/extended-blocks/videopress-video/videopress-video.php index a0900c67787d..8e928b797791 100644 --- a/projects/plugins/jetpack/extensions/extended-blocks/videopress-video/videopress-video.php +++ b/projects/plugins/jetpack/extensions/extended-blocks/videopress-video/videopress-video.php @@ -58,7 +58,7 @@ function register_videopress_blocks( $playlist_metadata_file = null, $latest_vid VideoPress_Pkg_Initializer::register_videopress_all_playlists_block(); } - // The channel blocks' shared pieces ( the playlist block's `source`, the editor data its picker reads ) also need the playlist block. + // The channel theme's blocks ( channel, video and playlist details, episode navigation ) and the playlist block's `source` also need the playlist block. if ( method_exists( 'Automattic\Jetpack\VideoPress\Channel_Blocks', 'register' ) ) { VideoPress_Pkg_Channel_Blocks::register(); } diff --git a/projects/plugins/videopress/changelog/add-videopress-channel-details-block b/projects/plugins/videopress/changelog/add-videopress-channel-details-block new file mode 100644 index 000000000000..b91f05a62a03 --- /dev/null +++ b/projects/plugins/videopress/changelog/add-videopress-channel-details-block @@ -0,0 +1,4 @@ +Significance: minor +Type: added + +Add a Channel Details block to channel sites, with the channel's handle, subscribers, videos, playlists, views and start date.