Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VideoPress: Fix VideoPress Attachment Deletions on wp.com #20832

Merged
merged 6 commits into from
Sep 1, 2021
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: bugfix

Fixed a bug related to deleting VideoPress videos.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use Automattic\Jetpack\Assets;
use Automattic\Jetpack\Connection\Client;

/**
* VideoPress in Jetpack
Expand Down Expand Up @@ -51,8 +52,8 @@ public function on_init() {
add_action( 'admin_print_footer_scripts', array( $this, 'print_in_footer_open_media_add_new' ) );
add_action( 'admin_head', array( $this, 'enqueue_admin_styles' ) );

add_filter( 'pre_delete_attachment', array( $this, 'delete_video_wpcom' ), 10, 2 );
add_filter( 'wp_mime_type_icon', array( $this, 'wp_mime_type_icon' ), 10, 3 );

add_filter( 'wp_video_extensions', array( $this, 'add_videopress_extenstion' ) );

VideoPress_Scheduler::init();
Expand Down Expand Up @@ -119,6 +120,63 @@ public function enqueue_admin_styles() {
wp_enqueue_style( 'videopress-admin' );
}

/**
* Attempts to delete a VideoPress video from wp.com.
* Will block the deletion from continuing if certain errors return from the wp.com API.
*
* @param Boolean $delete if the deletion should occur or not (unused).
* @param WP_Post $post the post object.
*
* @return null|WP_Error|Boolean null if deletion should continue.
*/
public function delete_video_wpcom( $delete, $post ) {
if ( ! is_videopress_attachment( $post->ID ) ) {
return null;
}

$guid = get_post_meta( $post->ID, 'videopress_guid', true );
if ( empty( $guid ) ) {
$this->delete_video_poster_attachment( $post->ID );
return null;
}

// Phone home and have wp.com delete the VideoPress entry and files.
$wpcom_response = Client::wpcom_json_api_request_as_blog(
sprintf( '/videos/%s/delete', $guid ),
'1.1',
array( 'method' => 'POST' )
);

if ( is_wp_error( $wpcom_response ) ) {
return $wpcom_response;
}

// Upon success or a 404 (video already deleted on wp.com), return null to allow the deletion to continue.
if ( 200 === $wpcom_response['response']['code'] || 404 === $wpcom_response['response']['code'] ) {
$this->delete_video_poster_attachment( $post->ID );
return null;
}

// Otherwise we stop the deletion from proceeding.
return false;
}

/**
* Deletes a video poster attachment if it exists.
*
* @param int $attachment_id the WP attachment id.
*/
private function delete_video_poster_attachment( $attachment_id ) {
$thumbnail_id = get_post_meta( $attachment_id, '_thumbnail_id', true );
if ( ! empty( $thumbnail_id ) ) {
// Let's ensure this is a VP poster image before we delete it.
if ( '1' === get_post_meta( $thumbnail_id, 'videopress_poster_image', true ) ) {
// This call triggers the `delete_video_wpcom` filter again but it bails early at the is_videopress_attachment() check.
wp_delete_attachment( $thumbnail_id );
}
}
}

/**
* Register VideoPress admin scripts.
*/
Expand Down