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

Channel blocks: Show authors a call to action, and optionally visitors a note, while a channel has no videos or playlists.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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'];
Expand Down
85 changes: 83 additions & 2 deletions projects/packages/videopress/src/class-channel-blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
'<a class="videopress-empty-state__action wp-element-button" href="%1$s">%2$s</a>',
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(
'<div class="videopress-empty-state is-%1$s is-for-%2$s"><p class="videopress-empty-state__title">%3$s</p><p class="videopress-empty-state__text">%4$s</p>%5$s</div>',
$is_videos ? 'videos' : 'playlists',
$can_act ? 'author' : 'visitor',
esc_html( $copy[0] ),
esc_html( $copy[1] ),
$action
);

return sprintf(
'<div %1$s>%2$s</div>',
get_block_wrapper_attributes( array( 'class' => trim( $wrapper_class . ' is-empty' ) ) ),
$markup
);
}

/**
* A boolean attribute.
*
Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -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'] );
Expand Down
13 changes: 12 additions & 1 deletion projects/packages/videopress/src/class-initializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );

Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@
}
},
"attributes": {
"emptyState": {
"type": "string",
"enum": [ "none", "author", "all" ],
"default": "none"
},
"layout": {
"type": "string",
"enum": [ "gallery", "list" ],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -183,6 +184,11 @@ export default function AllPlaylistsEdit( {
] }
onChange={ ( value: string ) => setAttributes( { orderBy: value as AllPlaylistsOrder } ) }
/>
<EmptyStateControl
kind="playlists"
value={ attributes.emptyState }
onChange={ emptyState => setAttributes( { emptyState } ) }
/>
</PanelBody>

<PanelBody title={ __( 'Show on each playlist', 'jetpack-videopress-pkg' ) }>
Expand Down
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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;
};

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
}
},
"attributes": {
"emptyState": {
"type": "string",
"enum": [ "none", "author", "all" ],
"default": "none"
},
"count": {
"type": "integer",
"default": 5
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -163,6 +164,11 @@ export default function LatestVideosPlaylistEdit( {
checked={ !! showMeta }
onChange={ ( value: boolean ) => setAttributes( { showMeta: value } ) }
/>
<EmptyStateControl
kind="videos"
value={ attributes.emptyState }
onChange={ emptyState => setAttributes( { emptyState } ) }
/>
<ToggleControl
__nextHasNoMarginBottom
label={ __( 'Hide the video being viewed', 'jetpack-videopress-pkg' ) }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { EmptyStateMode } from '../../channel/editor-controls';
import type { PlaylistDisplayAttributes } from '../playlist/types';

export type LatestVideosOrder = 'newest' | 'oldest' | 'popular';
Expand All @@ -13,4 +14,6 @@ export type LatestVideosPlaylistAttributes = PlaylistDisplayAttributes & {
excludeCurrent?: boolean;
// "12K views · 3 days ago" under each entry.
showMeta?: boolean;
// What the block shows while the site has no videos.
emptyState?: EmptyStateMode;
};
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@
}
},
"attributes": {
"emptyState": {
"type": "string",
"enum": [ "none", "author", "all" ],
"default": "none"
},
"source": {
"type": "string",
"enum": [ "queried", "index" ],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ import { Stack, SwitchControl } from '@wordpress/ui';
/**
* Internal dependencies
*/
import { PlaylistPicker } from '../../channel/editor-controls';
import { EmptyStateControl, PlaylistPicker } from '../../channel/editor-controls';
import ServerPreview from '../../channel/server-preview';
import { HEADING_LEVEL_OPTIONS } from '../video-details/edit';
/**
* Types
*/
import type { EmptyStateMode } from '../../channel/editor-controls';
import type { BlockEditProps } from '@wordpress/blocks';

export type PlaylistDetailsAttributes = {
Expand All @@ -26,6 +27,8 @@ export type PlaylistDetailsAttributes = {
showMeta: boolean;
showDescription: boolean;
showActions: boolean;
// What an indexed playlist shows while the channel has no playlists.
emptyState?: EmptyStateMode;
};

/**
Expand Down Expand Up @@ -75,10 +78,17 @@ export default function PlaylistDetailsEdit( {
}
/>
{ 'index' === source && (
<PlaylistPicker
value={ sourcePlaylistId }
onChange={ value => setAttributes( { sourcePlaylistId: value } ) }
/>
<>
<PlaylistPicker
value={ sourcePlaylistId }
onChange={ value => setAttributes( { sourcePlaylistId: value } ) }
/>
<EmptyStateControl
kind="playlists"
value={ attributes.emptyState }
onChange={ emptyState => setAttributes( { emptyState } ) }
/>
</>
) }
<SelectControl
__next40pxDefaultSize
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@
}
},
"attributes": {
"emptyState": {
"type": "string",
"enum": [ "none", "author", "all" ],
"default": "none"
},
"playlistId": {
"type": "string",
"default": ""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ import { __ } from '@wordpress/i18n';
/**
* Internal dependencies
*/
import { editorSourcePlaylist, PlaylistPicker } from '../../channel/editor-controls';
import {
editorSourcePlaylist,
EmptyStateControl,
PlaylistPicker,
} from '../../channel/editor-controls';
import { VideoPressIcon } from '../video/components/icons';
import { PlaylistSettingsPanels, PlaylistStylesControls } from './inspector-controls';
import PlaylistPreview from './preview';
Expand Down Expand Up @@ -54,10 +58,17 @@ export function PlaylistSourcePanel( { attributes, setAttributes }: Props ) {
onChange={ value => setAttributes( { source: value as PlaylistSource } ) }
/>
{ 'index' === source && (
<PlaylistPicker
value={ attributes.sourcePlaylistId ?? '' }
onChange={ value => setAttributes( { sourcePlaylistId: value } ) }
/>
<>
<PlaylistPicker
value={ attributes.sourcePlaylistId ?? '' }
onChange={ value => setAttributes( { sourcePlaylistId: value } ) }
/>
<EmptyStateControl
kind="playlists"
value={ attributes.emptyState }
onChange={ emptyState => setAttributes( { emptyState } ) }
/>
</>
) }
{ 'own' !== source && (
<ToggleControl
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { EmptyStateMode } from '../../channel/editor-controls';
import type { VideoGUID } from '../video/types';

/**
Expand Down Expand Up @@ -74,6 +75,8 @@ export type PlaylistAttributes = PlaylistDisplayAttributes & {
sourcePlaylistId?: string;
// "12K views · 3 days ago" under each entry, for sourced playlists.
showMeta?: boolean;
// What an indexed playlist shows while the channel has no playlists.
emptyState?: EmptyStateMode;
};

export type PlaylistSource = 'own' | 'queried' | 'index';
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@
}
},
"attributes": {
"emptyState": {
"type": "string",
"enum": [ "none", "author", "all" ],
"default": "none"
},
"source": {
"type": "string",
"enum": [ "queried", "latest" ],
Expand Down
Loading
Loading