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: patch
Type: changed

Video Playlist: Open this site's video pages in the same tab, and mark the entry of the video being viewed.
33 changes: 33 additions & 0 deletions projects/packages/videopress/src/class-channel.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,17 @@ public static function video_url( $attachment ) {
return '' === $guid ? '' : add_query_arg( 'v', $guid, user_trailingslashit( home_url( '/' . self::ENDPOINT ) ) );
}

/**
* Whether a URL points at this site rather than, say, videopress.com.
*
* @param string $url The URL.
* @return bool
*/
public static function is_site_url( $url ) {
$host = wp_parse_url( (string) $url, PHP_URL_HOST );
return is_string( $host ) && strtolower( $host ) === strtolower( (string) wp_parse_url( home_url(), PHP_URL_HOST ) );
}

/**
* Public query vars: `v` selects the video, `videopress_video` marks the page.
*
Expand Down Expand Up @@ -302,4 +313,26 @@ public static function queried_video_block( $parsed_block ) {
}
return $parsed_block;
}

/**
* Whether this request is a channel video page.
*
* @return bool
*/
public static function is_video_page() {
return (bool) get_query_var( self::QUERY_VAR );
}

/**
* The GUID of the video page's video, or an empty string.
*
* @return string
*/
public static function queried_guid() {
if ( ! self::is_video_page() ) {
return '';
}
$post = self::current_video();
return $post instanceof WP_Post ? self::guid( $post ) : '';
}
}
35 changes: 22 additions & 13 deletions projects/packages/videopress/src/class-initializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -911,6 +911,7 @@ public static function render_videopress_playlist_block( $block_attributes, $con
$show_number = $enabled( 'showPositionNumber', false );
$show_runtime = $enabled( 'showTotalRuntime' );
$muted = $enabled( 'muteByDefault', false );
$current_guid = Channel::is_enabled() ? Channel::queried_guid() : '';

$classes = array( 'videopress-playlist', 'is-layout-' . $layout );
if ( $enabled( 'darkPlayer', false ) ) {
Expand Down Expand Up @@ -1011,18 +1012,25 @@ public static function render_videopress_playlist_block( $block_attributes, $con
);
$entry_close = '</button>';
} else {
// No player to load the video into: the entry opens its VideoPress page instead.
// No player to load the video into: the entry opens the video's page instead.
/**
* Filters where a playlist entry opens when the block has no player.
*
* @since 0.55.0
*
* @param string $url The video's page on videopress.com.
* @param string $guid The video GUID.
*/
$entry_url = apply_filters( 'videopress_playlist_entry_url', 'https://videopress.com/v/' . $entry['guid'], $entry['guid'] );
$on_site = Channel::is_site_url( $entry_url );
$is_current = '' !== $current_guid && $entry['guid'] === $current_guid;
$entry_open = sprintf(
'<a class="videopress-playlist__select" href="%1$s" target="_blank" rel="noopener noreferrer" data-guid="%2$s" data-title="%3$s" data-position="%4$s" data-details="%5$s">',
/**
* Filters where a playlist entry opens when the block has no player.
*
* @since 0.55.0
*
* @param string $url The video's page on videopress.com.
* @param string $guid The video GUID.
*/
esc_url( apply_filters( 'videopress_playlist_entry_url', 'https://videopress.com/v/' . $entry['guid'], $entry['guid'] ) ),
'<a class="videopress-playlist__select%1$s" href="%2$s"%3$s%4$s data-guid="%5$s" data-title="%6$s" data-position="%7$s" data-details="%8$s">',
$is_current ? ' is-current' : '',
esc_url( $entry_url ),
// Channel pages stay in the tab; videopress.com opens a new one.
$on_site ? '' : ' target="_blank" rel="noopener noreferrer"',
$is_current ? ' aria-current="true"' : '',
esc_attr( $entry['guid'] ),
esc_attr( $title ),
esc_attr( $position ),
Expand All @@ -1032,7 +1040,7 @@ public static function render_videopress_playlist_block( $block_attributes, $con
}

$items .= sprintf(
'<li class="videopress-playlist__entry">%1$s' .
'<li class="videopress-playlist__entry%10$s">%1$s' .
'%2$s<span class="videopress-playlist__entry-thumb"><span class="videopress-playlist__entry-flag">%3$s</span>%4$s%5$s</span>' .
'<span class="videopress-playlist__entry-body"><span class="videopress-playlist__entry-title">%6$s</span><span class="videopress-playlist__entry-meta">%7$s%8$s</span></span>' .
'%9$s</li>',
Expand All @@ -1044,7 +1052,8 @@ public static function render_videopress_playlist_block( $block_attributes, $con
esc_html( $title ),
$resolution_markup,
$duration_markup,
$entry_close
$entry_close,
! $has_player && '' !== $current_guid && $entry['guid'] === $current_guid ? ' is-current' : ''
);
}

Expand Down
50 changes: 50 additions & 0 deletions projects/packages/videopress/tests/php/Playlist_Block_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,56 @@ public function test_render_without_player_links_entries_to_videopress() {
);
}

/**
* Entries mapped to a page on this site open in the same tab and mark the
* video being viewed; videopress.com keeps a new tab.
*/
public function test_render_without_player_keeps_site_links_in_the_tab() {
add_filter(
'videopress_playlist_entry_url',
function ( $url, $guid ) {
return 'abcDEF12' === $guid ? home_url( '/videopress/?v=' . $guid ) : $url;
},
10,
2
);
add_filter( 'videopress_channel_enabled', '__return_true' );
set_query_var( \Automattic\Jetpack\VideoPress\Channel::QUERY_VAR, 1 );
$GLOBALS['post'] = get_post( $this->video_attachment( 'abcDEF12' ) ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited

$markup = VideoPress_Initializer::render_videopress_playlist_block( $this->attributes( array( 'showPlayer' => false ) ) );

$this->assertStringContainsString(
'<a class="videopress-playlist__select is-current" href="' . esc_url( home_url( '/videopress/?v=abcDEF12' ) ) . '" aria-current="true" data-guid="abcDEF12"',
$markup
);
$this->assertStringContainsString( '<li class="videopress-playlist__entry is-current">', $markup );
$this->assertStringContainsString(
'<a class="videopress-playlist__select" href="https://videopress.com/v/ghiJKL34" target="_blank" rel="noopener noreferrer" data-guid="ghiJKL34"',
$markup
);
$this->assertSame( 1, substr_count( $markup, 'target="_blank"' ) );
}

/**
* A VideoPress attachment whose GUID lookup is pre-cached.
*
* @param string $guid VideoPress GUID.
* @return int Attachment ID.
*/
private function video_attachment( $guid ) {
$id = wp_insert_post(
array(
'post_type' => 'attachment',
'post_status' => 'inherit',
'post_mime_type' => 'video/videopress',
'post_title' => 'Video ' . $guid,
)
);
update_post_meta( $id, 'videopress_guid', $guid );
return $id;
}

/**
* A hidden player set to show on click renders click-to-play entries and an
* empty, hidden stage for the view script to reveal.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: enhancement

VideoPress: Open this site's video pages from a Video Playlist in the same tab, and mark the video being viewed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: changed

Open this site's video pages from a Video Playlist in the same tab, and mark the video being viewed.
Loading