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 Video Details block, and let the VideoPress Video block play the newest upload.
130 changes: 130 additions & 0 deletions projects/packages/videopress/src/class-channel-blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -22,6 +24,7 @@ class Channel_Blocks {
*/
const BLOCKS = array(
'videopress/channel-details' => 'render_channel_details',
'videopress/video-details' => 'render_video_details',
);

/**
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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( '<span class="videopress-video-details__eyebrow">%s</span>', 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( '<a href="%1$s">%2$s</a>', esc_url( Channel::video_url( $post ) ), esc_html( $details['title'] ) )
: esc_html( $details['title'] );
$markup .= sprintf( '<h%1$d class="videopress-video-details__title">%2$s</h%1$d>', $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( '<div class="videopress-video-details__meta">%s</div>', esc_html( $meta ) );
}
}
if ( self::flag( $attributes, 'showDescription', true ) && '' !== trim( wp_strip_all_tags( $details['description'] ) ) ) {
$markup .= sprintf( '<div class="videopress-video-details__description">%s</div>', 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(
'<a class="videopress-video-details__chip" href="%1$s">%2$s</a>',
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( '<div class="videopress-video-details__chips">%s</div>', $chips );
}
}
if ( '' === $markup ) {
return '';
}

return sprintf( '<div %1$s>%2$s</div>', 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 ) );
}
}
32 changes: 28 additions & 4 deletions projects/packages/videopress/src/class-channel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'] = '';
Expand Down Expand Up @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -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": "<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 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"
}
Original file line number Diff line number Diff line change
@@ -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 (
<div { ...useBlockProps() }>
<InspectorControls>
<PanelBody title={ __( 'Video', 'jetpack-videopress-pkg' ) }>
<Stack direction="column" gap="md">
<SelectControl
__next40pxDefaultSize
__nextHasNoMarginBottom
label={ __( 'Video', 'jetpack-videopress-pkg' ) }
value={ source }
options={ [
{
value: 'queried',
label: __( 'The video being viewed', 'jetpack-videopress-pkg' ),
},
{ value: 'latest', label: __( 'The newest upload', 'jetpack-videopress-pkg' ) },
] }
onChange={ value =>
setAttributes( { source: value as VideoDetailsAttributes[ 'source' ] } )
}
/>
<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/video-details"
attributes={ attributes }
label={ __( 'Video Details', 'jetpack-videopress-pkg' ) }
emptyInstructions={
'latest' === source
? __( 'Upload a VideoPress video to see its details here.', 'jetpack-videopress-pkg' )
: _x(
'On a video page, shows that video’s title, views, description and playlists.',
'',
'jetpack-videopress-pkg'
)
}
/>
</div>
);
}
Loading
Loading