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: fixed

Channel: Only open a channel video page for a video uploaded to this site.
38 changes: 33 additions & 5 deletions projects/packages/videopress/src/class-channel.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,42 @@ public static function attachment_id( $guid ) {
if ( ! is_string( $guid ) || ! preg_match( '/^[a-zA-Z0-9]{8}$/', $guid ) ) {
return 0;
}
$id = function_exists( 'videopress_get_post_id_by_guid' ) ? videopress_get_post_id_by_guid( $guid ) : false;
if ( ! is_int( $id ) && function_exists( 'video_get_info_by_guid' ) ) {

/*
* WordPress.com's videopress_get_post_id_by_guid() returns the post ID of
* whichever blog owns the GUID, so every candidate must prove it is this
* blog's attachment for that GUID before it is used.
*/
$candidates = array();
if ( function_exists( 'videopress_get_post_id_by_guid' ) ) {
$candidates[] = videopress_get_post_id_by_guid( $guid );
}
if ( function_exists( 'video_get_info_by_guid' ) ) {
$info = video_get_info_by_guid( $guid );
if ( is_object( $info ) && ! empty( $info->post_id ) && (int) $info->blog_id === get_current_blog_id() ) {
$id = (int) $info->post_id;
if ( is_object( $info ) && ! empty( $info->post_id ) && isset( $info->blog_id ) && (int) $info->blog_id === get_current_blog_id() ) {
$candidates[] = $info->post_id;
}
}
return is_int( $id ) && $id > 0 ? $id : 0;

foreach ( $candidates as $candidate ) {
$id = is_numeric( $candidate ) ? (int) $candidate : 0;
if ( $id > 0 && self::is_attachment_for( $id, $guid ) ) {
return $id;
}
}
return 0;
}

/**
* Whether a post ID is this blog's VideoPress attachment for a GUID.
*
* @param int $id Post ID.
* @param string $guid The GUID.
* @return bool
*/
private static function is_attachment_for( $id, $guid ) {
$post = get_post( $id );
return $post instanceof WP_Post && 'attachment' === $post->post_type && self::guid( $post ) === $guid;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
/**
* Tests that the channel's GUID lookup stays on its own site.
*
* @package automattic/jetpack-videopress
*/

namespace Automattic\Jetpack;

use Automattic\Jetpack\VideoPress\Channel;
use WorDBless\BaseTestCase;

/**
* Test suite for Channel's GUID lookup.
*/
class Channel_Data_Scoping_Test extends BaseTestCase {

/**
* A VideoPress attachment.
*
* @param string $guid VideoPress GUID.
* @return int Attachment ID.
*/
private function video( $guid ) {
$id = wp_insert_post(
array(
'post_type' => 'attachment',
'post_status' => 'inherit',
'post_mime_type' => Channel::MIME_TYPE,
'post_title' => $guid,
)
);
update_post_meta( $id, 'videopress_guid', $guid );
return $id;
}

/**
* WordPress.com's GUID lookup returns another blog's post ID; a local post with
* that ID but another GUID, or one that is not an attachment, is never used.
*/
public function test_attachment_id_only_returns_this_blogs_attachment_for_the_guid() {
$local = $this->video( 'localGU1' );
$page = wp_insert_post(
array(
'post_type' => 'page',
'post_status' => 'publish',
'post_title' => 'Not a video',
)
);

set_transient( 'videopress_get_post_id_by_guid_localGU1', $local, HOUR_IN_SECONDS );
set_transient( 'videopress_get_post_id_by_guid_remoteG1', $local, HOUR_IN_SECONDS );
set_transient( 'videopress_get_post_id_by_guid_remoteG2', $page, HOUR_IN_SECONDS );

$this->assertSame( $local, Channel::attachment_id( 'localGU1' ) );
$this->assertSame( 0, Channel::attachment_id( 'remoteG1' ) );
$this->assertSame( 0, Channel::attachment_id( 'remoteG2' ) );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: bugfix

VideoPress: Only open a channel video page for a video uploaded to this site.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fixed

Only open a channel video page for a video uploaded to this site.
Loading