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

Video Playlist block: Add a source setting to show a playlist published on the site, picked from a list and previewed in the editor. All Playlists block: Add a "Recently updated" order and an option to show each playlist's last update and length.
49 changes: 43 additions & 6 deletions projects/packages/videopress/src/class-all-playlists-block.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class All_Playlists_Block {
*
* @var string[]
*/
const ORDERS = array( 'newest', 'oldest', 'title' );
const ORDERS = array( 'newest', 'oldest', 'title', 'updated' );

/**
* Supported pagination styles, the first being the default.
Expand Down Expand Up @@ -108,7 +108,7 @@ public static function register( $metadata_file = null ) {
*
* @param mixed $block_attributes Block attributes; anything but an array counts as none.
*
* @return array{layout: string, columns: int, per_page: int, order_by: string, show_description: bool, show_video_count: bool, pagination: string}
* @return array{layout: string, columns: int, per_page: int, order_by: string, show_description: bool, show_video_count: bool, pagination: string, show_updated: bool}
*/
public static function settings( $block_attributes ) {
$block_attributes = is_array( $block_attributes ) ? $block_attributes : array();
Expand All @@ -135,6 +135,7 @@ public static function settings( $block_attributes ) {
'show_description' => $flag( 'showDescription', true ),
'show_video_count' => $flag( 'showVideoCount', true ),
'pagination' => $pick( 'pagination', self::PAGINATIONS ),
'show_updated' => $flag( 'showUpdated', false ),
);
}

Expand Down Expand Up @@ -186,9 +187,12 @@ function ( $a, $b ) {
}
);
} else {
// Newest first by the source post's publish date; playlists without a post go last.
$date_of = function ( $playlist ) {
return $playlist['post'] ? $playlist['post']->post_date_gmt : '';
// By the source post's publish ( or, for "updated", modified ) date; playlists without a post go last.
$date_of = function ( $playlist ) use ( $order_by ) {
if ( ! $playlist['post'] ) {
return '';
}
return 'updated' === $order_by ? $playlist['post']->post_modified_gmt : $playlist['post']->post_date_gmt;
};
usort(
$playlists,
Expand All @@ -198,7 +202,7 @@ function ( $a, $b ) use ( $date_of, $order_by ) {
if ( '' === $date_a || '' === $date_b ) {
$result = strcmp( $date_b, $date_a );
} else {
$result = 'newest' === $order_by ? strcmp( $date_b, $date_a ) : strcmp( $date_a, $date_b );
$result = 'oldest' === $order_by ? strcmp( $date_a, $date_b ) : strcmp( $date_b, $date_a );
}
return 0 !== $result ? $result : strcmp( $a['key'], $b['key'] );
}
Expand Down Expand Up @@ -473,6 +477,12 @@ private static function render_item( $playlist, $settings, $page, $hidden ) {
if ( $settings['show_description'] && '' !== $playlist['description'] ) {
$body .= sprintf( '<p class="videopress-all-playlists__description">%s</p>', esc_html( $playlist['description'] ) );
}
if ( $settings['show_updated'] ) {
$meta = self::updated_meta( $playlist );
if ( '' !== $meta ) {
$body .= sprintf( '<span class="videopress-all-playlists__meta">%s</span>', esc_html( $meta ) );
}
}
$body .= sprintf(
'<span class="videopress-all-playlists__poster-note">%s</span>',
esc_html__( 'First video is private or was deleted.', 'jetpack-videopress-pkg' )
Expand All @@ -496,6 +506,33 @@ private static function render_item( $playlist, $settings, $page, $hidden ) {
);
}

/**
* "Updated 2 weeks ago · 1 hr 12 min" for a card.
*
* @param array $playlist A record from ordered_playlists().
*
* @return string
*/
public static function updated_meta( $playlist ) {
$parts = array();
if ( $playlist['post'] instanceof WP_Post ) {
$ago = Channel_Data::ago( (int) strtotime( $playlist['post']->post_modified_gmt . ' UTC' ) );
if ( '' !== $ago ) {
/* translators: %s: how long ago, e.g. "2 weeks ago". */
$parts[] = sprintf( __( 'Updated %s', 'jetpack-videopress-pkg' ), $ago );
}
}
$total_ms = 0;
foreach ( Initializer::sanitize_playlist_entries( $playlist['videos'] ) as $entry ) {
$total_ms += $entry['durationMs'];
}
$runtime = Initializer::playlist_runtime_label( $total_ms );
if ( '' !== $runtime ) {
$parts[] = $runtime;
}
return implode( ' · ', $parts );
}

/**
* Render the numbered pagination.
*
Expand Down
72 changes: 72 additions & 0 deletions projects/packages/videopress/src/class-channel-blocks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
/**
* The VideoPress channel blocks.
*
* @package automattic/jetpack-videopress
*/

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.
*/
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.
*
* @return void
*/
public static function register() {
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 ( ! has_action( 'enqueue_block_editor_assets', array( __CLASS__, 'enqueue_editor_data' ) ) ) {
add_action( 'enqueue_block_editor_assets', array( __CLASS__, 'enqueue_editor_data' ) );
}
}

/**
* Hand the editor the site's playlists, most recently updated first, for the playlist pickers
* of the blocks that show one and for their canvas previews. An inline script rather than a
* REST route, so WordPress.com Simple's editor gets it too.
*
* @return void
*/
public static function enqueue_editor_data() {
if ( ! current_user_can( 'edit_posts' ) ) {
return;
}
$handle = '';
foreach ( array( Playlist_Index::BLOCK_NAME ) 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];
break;
}
}
if ( '' === $handle ) {
return;
}

$playlists = array();
foreach ( Channel::playlists() as $playlist ) {
$playlists[] = array(
'key' => $playlist['key'],
'title' => $playlist['title'],
'videos' => $playlist['videos'],
);
}
wp_add_inline_script(
$handle,
'window.videopressChannelEditor = ' . wp_json_encode( array( 'playlists' => $playlists ), JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ) . ';',
'before'
);
}
}
120 changes: 116 additions & 4 deletions projects/packages/videopress/src/class-channel.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@
*/
class Channel {

const MIME_TYPE = 'video/videopress';
const ENDPOINT = 'videopress';
const QUERY_VAR = 'videopress_video';
const TEMPLATE = 'videopress-video';
const MIME_TYPE = 'video/videopress';
const ENDPOINT = 'videopress';
const QUERY_VAR = 'videopress_video';
const TEMPLATE = 'videopress-video';
const PLAYLIST_SOURCES = array( 'own', 'index' );

/**
* Whether the feature has been wired up in this request.
Expand Down Expand Up @@ -335,4 +336,115 @@ public static function queried_guid() {
$post = self::current_video();
return $post instanceof WP_Post ? self::guid( $post ) : '';
}

/**
* One indexed playlist with its key, or null.
*
* @param string $key Playlist key.
* @return array|null Record with key, title, description, videos and post_id.
*/
public static function playlist_record( $key ) {
$key = is_string( $key ) ? sanitize_key( $key ) : '';
if ( '' === $key ) {
return null;
}
$index = Playlist_Index::get_playlists();
if ( ! isset( $index[ $key ] ) || ! is_array( $index[ $key ] ) ) {
return null;
}
return self::normalize_record( $key, $index[ $key ] );
}

/**
* Every indexed playlist whose source post is published, most recently updated first.
*
* @return array[] Records as playlist_record() returns them, plus `updated` ( unix time ).
*/
public static function playlists() {
$playlists = array();
foreach ( Playlist_Index::get_playlists() as $key => $record ) {
if ( ! is_array( $record ) ) {
continue;
}
$record = self::normalize_record( (string) $key, $record );
$post = $record['post_id'] ? get_post( $record['post_id'] ) : null;
if ( ! $post instanceof WP_Post || 'publish' !== $post->post_status ) {
continue;
}
$record['updated'] = (int) strtotime( $post->post_modified_gmt . ' UTC' );
$playlists[] = $record;
}
usort(
$playlists,
function ( $a, $b ) {
return $b['updated'] !== $a['updated'] ? $b['updated'] - $a['updated'] : strcmp( $a['key'], $b['key'] );
}
);
return $playlists;
}

/**
* Normalize an index record.
*
* @param string $key Playlist key.
* @param array $record Index record.
* @return array
*/
private static function normalize_record( $key, array $record ) {
return array(
'key' => $key,
'title' => isset( $record['title'] ) && is_string( $record['title'] ) ? $record['title'] : '',
'description' => isset( $record['description'] ) && is_string( $record['description'] ) ? $record['description'] : '',
'videos' => Initializer::sanitize_playlist_entries( $record['videos'] ?? null ),
'post_id' => isset( $record['post_id'] ) ? absint( $record['post_id'] ) : 0,
);
}

/**
* The playlist a block shows: an indexed one by key ( the most recently
* updated when no key is set ).
*
* @param string $source One of PLAYLIST_SOURCES.
* @param string $key Playlist key for the `index` source.
* @return array|null
*/
public static function source_playlist( $source, $key = '' ) {
if ( 'index' !== $source ) {
return null;
}
if ( is_string( $key ) && '' !== $key ) {
return self::playlist_record( $key );
}
$playlists = self::playlists();
return $playlists ? $playlists[0] : null;
}

/**
* A Video Playlist block whose `source` is `index` shows that playlist. Hooked by
* Channel_Blocks::register() on every site with the block, channel theme or not.
*
* @param array $parsed_block The block being rendered.
* @return array
*/
public static function playlist_source_block( $parsed_block ) {
if ( ! isset( $parsed_block['blockName'] ) || Playlist_Index::BLOCK_NAME !== $parsed_block['blockName'] ) {
return $parsed_block;
}
$attrs = isset( $parsed_block['attrs'] ) && is_array( $parsed_block['attrs'] ) ? $parsed_block['attrs'] : array();
$source = isset( $attrs['source'] ) && in_array( $attrs['source'], self::PLAYLIST_SOURCES, true ) ? $attrs['source'] : 'own';
if ( 'own' === $source ) {
return $parsed_block;
}

$playlist = self::source_playlist( $source, isset( $attrs['sourcePlaylistId'] ) ? (string) $attrs['sourcePlaylistId'] : '' );
if ( ! $playlist ) {
$parsed_block['attrs']['videos'] = array();
return $parsed_block;
}

$parsed_block['attrs']['videos'] = $playlist['videos'];
$parsed_block['attrs']['playlistTitle'] = $playlist['title'];
$parsed_block['attrs']['playlistDescription'] = $playlist['description'];
return $parsed_block;
}
}
5 changes: 4 additions & 1 deletion projects/packages/videopress/src/class-initializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,9 @@ 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.
Channel_Blocks::register();
}

/**
Expand Down Expand Up @@ -828,7 +831,7 @@ private static function playlist_timecode( $duration_ms ) {
*
* @return string Runtime label, or an empty string when the duration is unknown.
*/
private static function playlist_runtime_label( $duration_ms ) {
public static function playlist_runtime_label( $duration_ms ) {
if ( $duration_ms <= 0 ) {
return '';
}
Expand Down
7 changes: 6 additions & 1 deletion projects/packages/videopress/src/class-playlist-index.php
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,12 @@ private static function find_playlist_blocks( array $blocks ) {
}

if ( self::BLOCK_NAME === ( $block['blockName'] ?? null ) ) {
$found[] = isset( $block['attrs'] ) && is_array( $block['attrs'] ) ? $block['attrs'] : array();
$attrs = isset( $block['attrs'] ) && is_array( $block['attrs'] ) ? $block['attrs'] : array();
// A block that shows another playlist ( any `source` but own ) is not one itself.
if ( isset( $attrs['source'] ) && 'own' !== $attrs['source'] ) {
continue;
}
$found[] = $attrs;
}

if ( ! empty( $block['innerBlocks'] ) && is_array( $block['innerBlocks'] ) ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
},
"orderBy": {
"type": "string",
"enum": [ "newest", "oldest", "title" ],
"enum": [ "newest", "oldest", "title", "updated" ],
"default": "newest"
},
"showDescription": {
Expand All @@ -67,6 +67,10 @@
"type": "string",
"enum": [ "numbered", "load-more" ],
"default": "numbered"
},
"showUpdated": {
"type": "boolean",
"default": false
}
},
"editorScript": "file:./index.js",
Expand Down
Loading
Loading