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 Channel Details block to channel sites, with the channel's handle, subscribers, videos, playlists, views and start date.
157 changes: 151 additions & 6 deletions projects/packages/videopress/src/class-channel-blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string>
*/
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' ) );
}
Expand Down Expand Up @@ -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(
'<div class="videopress-channel-details__item is-%1$s"><dt>%2$s</dt><dd>%3$s</dd></div>',
esc_attr( $item ),
esc_html( $value['label'] ),
esc_html( $value['value'] )
)
: sprintf(
'<span class="videopress-channel-details__item is-%1$s">%2$s</span>',
esc_attr( $item ),
esc_html( $value['inline'] )
);
}
if ( '' === $markup ) {
return '';
}

$tag = 'list' === $layout ? 'dl' : 'div';
return sprintf(
'<%1$s %2$s>%3$s</%1$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 ),
);
}
}
}
2 changes: 1 addition & 1 deletion projects/packages/videopress/src/class-initializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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": "<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": "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"
}
Original file line number Diff line number Diff line change
@@ -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 (
<div { ...useBlockProps() }>
<InspectorControls>
<PanelBody title={ __( 'Details', 'jetpack-videopress-pkg' ) }>
<Stack direction="column" gap="md">
<SelectControl
__next40pxDefaultSize
__nextHasNoMarginBottom
label={ __( 'Layout', 'jetpack-videopress-pkg' ) }
value={ layout }
options={ [
{ value: 'inline', label: __( 'One line', 'jetpack-videopress-pkg' ) },
{ value: 'list', label: __( 'List with labels', 'jetpack-videopress-pkg' ) },
] }
onChange={ value =>
setAttributes( { layout: value as ChannelDetailsAttributes[ 'layout' ] } )
}
/>
<Stack direction="column" gap="sm">
{ ( Object.keys( ITEM_LABELS ) as ChannelItem[] ).map( item => (
<CheckboxControl
key={ item }
label={ ITEM_LABELS[ item ] }
checked={ items.includes( item ) }
onCheckedChange={ checked => toggle( item, checked ) }
/>
) ) }
</Stack>
</Stack>
</PanelBody>
</InspectorControls>
<ServerPreview
block="videopress/channel-details"
attributes={ attributes }
label={ __( 'Channel Details', 'jetpack-videopress-pkg' ) }
emptyInstructions={ __( 'Pick at least one detail to show.', '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 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( <ChannelDetailsEdit { ...props } /> );
}

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 } );
} );
} );
Loading
Loading