diff --git a/projects/packages/videopress/changelog/add-videopress-channel-empty-states b/projects/packages/videopress/changelog/add-videopress-channel-empty-states
new file mode 100644
index 000000000000..6fd195d37117
--- /dev/null
+++ b/projects/packages/videopress/changelog/add-videopress-channel-empty-states
@@ -0,0 +1,4 @@
+Significance: minor
+Type: added
+
+Channel blocks: Show authors a call to action, and optionally visitors a note, while a channel has no videos or playlists.
diff --git a/projects/packages/videopress/src/class-all-playlists-block.php b/projects/packages/videopress/src/class-all-playlists-block.php
index fcf913146280..fc71d08b53aa 100644
--- a/projects/packages/videopress/src/class-all-playlists-block.php
+++ b/projects/packages/videopress/src/class-all-playlists-block.php
@@ -320,6 +320,7 @@ private static function requested_page() {
public static function render( $block_attributes, $content = '' ) {
$settings = self::settings( $block_attributes );
$playlists = self::ordered_playlists( $settings['order_by'] );
+ $has_any = (bool) $playlists;
if ( $settings['exclude_current'] ) {
$current = Channel::queried_playlist();
$playlists = $current
@@ -335,7 +336,8 @@ function ( $playlist ) use ( $current ) {
}
$total = count( $playlists );
if ( ! $total ) {
- return '';
+ // Only a site without any playlist is empty; "More playlists" next to the only one stays hidden.
+ return $has_any ? '' : Channel_Blocks::render_empty_state( 'playlists', $block_attributes, 'videopress-all-playlists' );
}
$per_page = $settings['per_page'];
diff --git a/projects/packages/videopress/src/class-channel-blocks.php b/projects/packages/videopress/src/class-channel-blocks.php
index 8597d092332f..86e81e40e462 100644
--- a/projects/packages/videopress/src/class-channel-blocks.php
+++ b/projects/packages/videopress/src/class-channel-blocks.php
@@ -108,6 +108,85 @@ public static function enqueue_editor_data() {
);
}
+ /**
+ * Empty state modes, the first being the default ( render nothing ).
+ *
+ * @var string[]
+ */
+ const EMPTY_STATE_MODES = array( 'none', 'author', 'all' );
+
+ /**
+ * A block's `emptyState` mode.
+ *
+ * @param mixed $attributes Block attributes.
+ *
+ * @return string One of EMPTY_STATE_MODES.
+ */
+ public static function empty_state_mode( $attributes ) {
+ return is_array( $attributes ) && isset( $attributes['emptyState'] ) && in_array( $attributes['emptyState'], self::EMPTY_STATE_MODES, true )
+ ? $attributes['emptyState']
+ : self::EMPTY_STATE_MODES[0];
+ }
+
+ /**
+ * The empty state a block shows when the channel has no videos or no playlists:
+ * a call to action for people who can add some, a short note for visitors in "all" mode.
+ *
+ * @param string $kind "videos" or "playlists".
+ * @param mixed $attributes Block attributes, for the `emptyState` mode.
+ * @param string $wrapper_class Classes for the block wrapper, besides `is-empty`.
+ *
+ * @return string Markup, or an empty string when nothing should show.
+ */
+ public static function render_empty_state( $kind, $attributes, $wrapper_class = '' ) {
+ $mode = self::empty_state_mode( $attributes );
+ if ( 'none' === $mode ) {
+ return '';
+ }
+ $is_videos = 'playlists' !== $kind;
+ $can_act = current_user_can( $is_videos ? 'upload_files' : 'edit_posts' );
+ if ( ! $can_act && 'all' !== $mode ) {
+ return '';
+ }
+
+ $action = '';
+ if ( $can_act ) {
+ $copy = $is_videos
+ ? array(
+ __( 'Upload your first video', 'jetpack-videopress-pkg' ),
+ __( 'Videos you upload to the media library appear here, with their own page on your channel.', 'jetpack-videopress-pkg' ),
+ )
+ : array(
+ __( 'Create your first playlist', 'jetpack-videopress-pkg' ),
+ __( 'Add a Video Playlist block to a post or page and give it a title: it is listed here and gets its own page.', 'jetpack-videopress-pkg' ),
+ );
+ $action = sprintf(
+ '%2$s',
+ esc_url( admin_url( $is_videos ? 'media-new.php' : 'post-new.php' ) ),
+ esc_html( $is_videos ? __( 'Upload a video', 'jetpack-videopress-pkg' ) : __( 'Write a post', 'jetpack-videopress-pkg' ) )
+ );
+ } else {
+ $copy = $is_videos
+ ? array( __( 'No videos yet', 'jetpack-videopress-pkg' ), __( 'New videos will appear here.', 'jetpack-videopress-pkg' ) )
+ : array( __( 'No playlists yet', 'jetpack-videopress-pkg' ), __( 'Playlists will appear here.', 'jetpack-videopress-pkg' ) );
+ }
+
+ $markup = sprintf(
+ '
',
+ $is_videos ? 'videos' : 'playlists',
+ $can_act ? 'author' : 'visitor',
+ esc_html( $copy[0] ),
+ esc_html( $copy[1] ),
+ $action
+ );
+
+ return sprintf(
+ '%2$s
',
+ get_block_wrapper_attributes( array( 'class' => trim( $wrapper_class . ' is-empty' ) ) ),
+ $markup
+ );
+ }
+
/**
* A boolean attribute.
*
@@ -273,7 +352,7 @@ public static function render_video_details( $attributes ) {
$is_latest = isset( $attributes['source'] ) && 'latest' === $attributes['source'];
$guid = $post instanceof WP_Post ? Channel::guid( $post ) : '';
if ( ! $post instanceof WP_Post || '' === $guid ) {
- return '';
+ return $is_latest ? self::render_empty_state( 'videos', $attributes, 'videopress-video-details' ) : '';
}
$details = Channel_Data::video_details( $guid, $post );
$record = Channel_Data::record( $post );
@@ -359,7 +438,9 @@ public static function render_playlist_details( $attributes ) {
$source = isset( $attributes['source'] ) && 'index' === $attributes['source'] ? 'index' : 'queried';
$playlist = Channel::source_playlist( $source, isset( $attributes['sourcePlaylistId'] ) ? (string) $attributes['sourcePlaylistId'] : '' );
if ( ! $playlist ) {
- return '';
+ return 'index' === $source && ! Channel::playlists()
+ ? self::render_empty_state( 'playlists', $attributes, 'videopress-playlist-details is-source-index' )
+ : '';
}
$count = count( $playlist['videos'] );
diff --git a/projects/packages/videopress/src/class-initializer.php b/projects/packages/videopress/src/class-initializer.php
index d0b29a062369..eec6f44f1a60 100644
--- a/projects/packages/videopress/src/class-initializer.php
+++ b/projects/packages/videopress/src/class-initializer.php
@@ -742,9 +742,17 @@ public static function render_videopress_latest_videos_playlist_block( $block_at
if ( ! $is_browsed ) {
$block_attributes['videos'] = Data::get_latest_videopress_playlist_entries( $count );
+ if ( ! $block_attributes['videos'] ) {
+ return Channel_Blocks::render_empty_state( 'videos', $block_attributes );
+ }
return self::render_videopress_playlist_block( $block_attributes, '', $block );
}
+ // A site without videos gets the empty state, not filters with nothing to filter.
+ if ( ! Channel_Data::videos() ) {
+ return Channel_Blocks::render_empty_state( 'videos', $block_attributes );
+ }
+
$state = Channel_Data::browse_request( $controls, $paginated, $order );
$results = Channel_Data::browse( $state, $count, $exclude, $show_meta );
@@ -1049,7 +1057,10 @@ public static function render_videopress_playlist_block( $block_attributes, $con
$entries = self::sanitize_playlist_entries( $block_attributes['videos'] ?? null );
if ( ! $entries ) {
- return '';
+ // A playlist picked from the index shows the empty state while the site has none.
+ return isset( $block_attributes['source'] ) && 'index' === $block_attributes['source'] && ! Channel::playlists()
+ ? Channel_Blocks::render_empty_state( 'playlists', $block_attributes )
+ : '';
}
// Record the rendered GUIDs in the post's cached GUID list so private playlist
diff --git a/projects/packages/videopress/src/client/block-editor/blocks/all-playlists/block.json b/projects/packages/videopress/src/client/block-editor/blocks/all-playlists/block.json
index c825171300b1..c0559a38a7aa 100644
--- a/projects/packages/videopress/src/client/block-editor/blocks/all-playlists/block.json
+++ b/projects/packages/videopress/src/client/block-editor/blocks/all-playlists/block.json
@@ -37,6 +37,11 @@
}
},
"attributes": {
+ "emptyState": {
+ "type": "string",
+ "enum": [ "none", "author", "all" ],
+ "default": "none"
+ },
"layout": {
"type": "string",
"enum": [ "gallery", "list" ],
diff --git a/projects/packages/videopress/src/client/block-editor/blocks/all-playlists/edit.tsx b/projects/packages/videopress/src/client/block-editor/blocks/all-playlists/edit.tsx
index a52d16908de6..96df40b30137 100644
--- a/projects/packages/videopress/src/client/block-editor/blocks/all-playlists/edit.tsx
+++ b/projects/packages/videopress/src/client/block-editor/blocks/all-playlists/edit.tsx
@@ -17,6 +17,7 @@ import { __ } from '@wordpress/i18n';
/**
* Internal dependencies
*/
+import { EmptyStateControl } from '../../channel/editor-controls';
import { blockStyleVars } from './block-style-vars';
import { hydratePoster } from './hydrate-poster';
import { MAX_COLUMNS, MAX_PER_PAGE, MIN_COLUMNS, MIN_PER_PAGE } from './types';
@@ -183,6 +184,11 @@ export default function AllPlaylistsEdit( {
] }
onChange={ ( value: string ) => setAttributes( { orderBy: value as AllPlaylistsOrder } ) }
/>
+ setAttributes( { emptyState } ) }
+ />
diff --git a/projects/packages/videopress/src/client/block-editor/blocks/all-playlists/types.ts b/projects/packages/videopress/src/client/block-editor/blocks/all-playlists/types.ts
index 98b7b08a3ebb..3b2da4605540 100644
--- a/projects/packages/videopress/src/client/block-editor/blocks/all-playlists/types.ts
+++ b/projects/packages/videopress/src/client/block-editor/blocks/all-playlists/types.ts
@@ -1,3 +1,5 @@
+import type { EmptyStateMode } from '../../channel/editor-controls';
+
export type AllPlaylistsLayout = 'gallery' | 'list';
export type AllPlaylistsOrder = 'newest' | 'oldest' | 'title' | 'updated';
export type AllPlaylistsPagination = 'numbered' | 'load-more';
@@ -28,6 +30,8 @@ export type AllPlaylistsAttributes = BlockStyleAttributes & {
excludeCurrent?: boolean;
// "Updated 2 weeks ago · 1 hr 12 min" under each card.
showUpdated?: boolean;
+ // What the block shows while the site has no playlists.
+ emptyState?: EmptyStateMode;
};
/**
diff --git a/projects/packages/videopress/src/client/block-editor/blocks/latest-videos-playlist/block.json b/projects/packages/videopress/src/client/block-editor/blocks/latest-videos-playlist/block.json
index 212a7109acce..b5b6375be7a2 100644
--- a/projects/packages/videopress/src/client/block-editor/blocks/latest-videos-playlist/block.json
+++ b/projects/packages/videopress/src/client/block-editor/blocks/latest-videos-playlist/block.json
@@ -19,6 +19,11 @@
}
},
"attributes": {
+ "emptyState": {
+ "type": "string",
+ "enum": [ "none", "author", "all" ],
+ "default": "none"
+ },
"count": {
"type": "integer",
"default": 5
diff --git a/projects/packages/videopress/src/client/block-editor/blocks/latest-videos-playlist/edit.tsx b/projects/packages/videopress/src/client/block-editor/blocks/latest-videos-playlist/edit.tsx
index a0634166d083..a936afe17e30 100644
--- a/projects/packages/videopress/src/client/block-editor/blocks/latest-videos-playlist/edit.tsx
+++ b/projects/packages/videopress/src/client/block-editor/blocks/latest-videos-playlist/edit.tsx
@@ -13,6 +13,7 @@ import { __, _x } from '@wordpress/i18n';
/**
* Internal dependencies
*/
+import { EmptyStateControl } from '../../channel/editor-controls';
import ServerPreview from '../../channel/server-preview';
import { PlaylistSettingsPanels, PlaylistStylesControls } from '../playlist/inspector-controls';
import usePublishTracking from '../playlist/use-publish-tracking';
@@ -163,6 +164,11 @@ export default function LatestVideosPlaylistEdit( {
checked={ !! showMeta }
onChange={ ( value: boolean ) => setAttributes( { showMeta: value } ) }
/>
+ setAttributes( { emptyState } ) }
+ />
{ 'index' === source && (
- setAttributes( { sourcePlaylistId: value } ) }
- />
+ <>
+ setAttributes( { sourcePlaylistId: value } ) }
+ />
+ setAttributes( { emptyState } ) }
+ />
+ >
) }
setAttributes( { source: value as PlaylistSource } ) }
/>
{ 'index' === source && (
- setAttributes( { sourcePlaylistId: value } ) }
- />
+ <>
+ setAttributes( { sourcePlaylistId: value } ) }
+ />
+ setAttributes( { emptyState } ) }
+ />
+ >
) }
{ 'own' !== source && (
+ { 'latest' === source && (
+ setAttributes( { emptyState } ) }
+ />
+ ) }
playlist.key === key ) : playlists[ 0 ] ) ?? null;
}
+type EmptyStateControlProps = {
+ value?: EmptyStateMode;
+ onChange: ( value: EmptyStateMode ) => void;
+ // What the empty state asks for: uploading videos or creating playlists.
+ kind: 'videos' | 'playlists';
+};
+
+/**
+ * Choose what the block shows while the channel has no videos or no playlists.
+ *
+ * @param props - Component props.
+ * @param props.value - Current mode.
+ * @param props.onChange - Mode setter.
+ * @param props.kind - What is missing.
+ * @return The control.
+ */
+export function EmptyStateControl( { value, onChange, kind }: EmptyStateControlProps ) {
+ return (
+ onChange( mode as EmptyStateMode ) }
+ />
+ );
+}
+
type PlaylistPickerProps = {
value?: string;
onChange: ( value: string ) => void;
diff --git a/projects/packages/videopress/tests/php/Channel_Empty_State_Test.php b/projects/packages/videopress/tests/php/Channel_Empty_State_Test.php
new file mode 100644
index 000000000000..01d62f5d7eac
--- /dev/null
+++ b/projects/packages/videopress/tests/php/Channel_Empty_State_Test.php
@@ -0,0 +1,131 @@
+ 'videopress/video-details',
+ 'attrs' => array(),
+ );
+ }
+
+ /**
+ * Log in a user with a role.
+ *
+ * @param string $role Role.
+ */
+ private function log_in( $role ) {
+ $user_id = wp_insert_user(
+ array(
+ 'user_login' => 'channel-' . $role,
+ 'user_pass' => 'password',
+ 'role' => $role,
+ )
+ );
+ wp_set_current_user( $user_id );
+ }
+
+ /**
+ * "none" renders nothing; "author" only for people who can act; "all" tells visitors too.
+ */
+ public function test_modes_and_audiences() {
+ $this->assertSame( '', Channel_Blocks::render_empty_state( 'videos', array() ) );
+ $this->assertSame( '', Channel_Blocks::render_empty_state( 'videos', array( 'emptyState' => 'author' ) ) );
+
+ $visitor = Channel_Blocks::render_empty_state( 'videos', array( 'emptyState' => 'all' ) );
+ $this->assertStringContainsString( 'videopress-empty-state is-videos is-for-visitor', $visitor );
+ $this->assertStringContainsString( 'No videos yet', $visitor );
+ $this->assertStringNotContainsString( 'videopress-empty-state__action', $visitor );
+
+ $this->log_in( 'contributor' );
+ $this->assertSame( '', Channel_Blocks::render_empty_state( 'videos', array( 'emptyState' => 'author' ) ), 'Contributors cannot upload.' );
+ $this->assertStringContainsString( 'is-for-author', Channel_Blocks::render_empty_state( 'playlists', array( 'emptyState' => 'author' ) ) );
+
+ $this->log_in( 'author' );
+ $author = Channel_Blocks::render_empty_state( 'videos', array( 'emptyState' => 'author' ), 'videopress-video-details' );
+ $this->assertStringContainsString( 'videopress-video-details is-empty"', $author );
+ $this->assertStringContainsString( 'Upload your first video', $author );
+ $this->assertStringContainsString( 'href="' . esc_url( admin_url( 'media-new.php' ) ) . '"', $author );
+ }
+
+ /**
+ * Each block shows its empty state when the channel has nothing for it, and only when asked to.
+ */
+ public function test_blocks_render_their_empty_state() {
+ $this->log_in( 'administrator' );
+
+ $this->assertStringContainsString(
+ 'is-videos is-for-author',
+ Channel_Blocks::render_video_details(
+ array(
+ 'source' => 'latest',
+ 'emptyState' => 'author',
+ )
+ )
+ );
+ $this->assertSame( '', Channel_Blocks::render_video_details( array( 'emptyState' => 'author' ) ), 'The video page has no empty state.' );
+
+ $this->assertStringContainsString(
+ 'is-playlists is-for-author',
+ Channel_Blocks::render_playlist_details(
+ array(
+ 'source' => 'index',
+ 'emptyState' => 'author',
+ )
+ )
+ );
+
+ $latest = Initializer::render_videopress_latest_videos_playlist_block(
+ array(
+ 'showBrowseControls' => true,
+ 'emptyState' => 'all',
+ )
+ );
+ $this->assertStringContainsString( 'Upload your first video', $latest );
+ $this->assertStringNotContainsString( 'videopress-video-browser', $latest, 'No filters without videos to filter.' );
+ $this->assertSame( '', Initializer::render_videopress_latest_videos_playlist_block( array( 'showBrowseControls' => true ) ) );
+
+ $this->assertStringContainsString(
+ 'Create your first playlist',
+ Initializer::render_videopress_playlist_block(
+ array(
+ 'source' => 'index',
+ 'emptyState' => 'author',
+ )
+ )
+ );
+ $this->assertStringContainsString( 'videopress-all-playlists is-empty', All_Playlists_Block::render( array( 'emptyState' => 'all' ) ) );
+ $this->assertSame( '', All_Playlists_Block::render( array() ) );
+ }
+}
diff --git a/projects/plugins/jetpack/changelog/add-videopress-channel-empty-states b/projects/plugins/jetpack/changelog/add-videopress-channel-empty-states
new file mode 100644
index 000000000000..6d67039b05b0
--- /dev/null
+++ b/projects/plugins/jetpack/changelog/add-videopress-channel-empty-states
@@ -0,0 +1,4 @@
+Significance: minor
+Type: enhancement
+
+VideoPress: Let channel blocks show authors a call to action while a channel has no videos or playlists.
diff --git a/projects/plugins/videopress/changelog/add-videopress-channel-empty-states b/projects/plugins/videopress/changelog/add-videopress-channel-empty-states
new file mode 100644
index 000000000000..2ac0265a2f04
--- /dev/null
+++ b/projects/plugins/videopress/changelog/add-videopress-channel-empty-states
@@ -0,0 +1,4 @@
+Significance: minor
+Type: added
+
+Let channel blocks show authors a call to action while a channel has no videos or playlists.