Skip to content

Commit

Permalink
Delete previous auto-generated thumbnail on retranscoding,
Browse files Browse the repository at this point in the history
Prevent to set auto-generated thumbnail if editor has set custom thumbnail to video
  • Loading branch information
dhaval-parekh committed May 19, 2020
1 parent d0ab50c commit 4d226d0
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion admin/rt-retranscode-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -806,6 +806,17 @@ public function transcoded_thumbnails_added( $media_id = '' ) {
return;
}

// Meta key for meta data, Which store the last generated thumbnail (by transcoder).
$last_generated_thumbnail_id_key = 'transcoder_generated_thumbnail_id';

$last_generated_thumbnail_id = get_post_meta( $media_id, $last_generated_thumbnail_id_key, true );
$last_generated_thumbnail_id = ( ! empty( $last_generated_thumbnail_id ) && 0 < intval( $last_generated_thumbnail_id ) ) ? intval( $last_generated_thumbnail_id ) : 0;

$post_thumbnail_id = get_post_thumbnail_id( $media_id );
$post_thumbnail_id = ( ! empty( $post_thumbnail_id ) && 0 < intval( $post_thumbnail_id ) ) ? intval( $post_thumbnail_id ) : 0;

$has_custom_thumbnail = ( ! empty( $last_generated_thumbnail_id ) && $post_thumbnail_id !== $last_generated_thumbnail_id );

$is_retranscoding_job = get_post_meta( $media_id, '_rt_retranscoding_sent', true );

if ( $is_retranscoding_job && ! rtt_is_override_thumbnail() ) {
Expand Down Expand Up @@ -858,10 +869,31 @@ public function transcoded_thumbnails_added( $media_id = '' ) {

// Generate attachment metadata for thumbnail and set post thumbnail for video/audio files.
if ( ! is_wp_error( $attachment_id ) && 0 !== $attachment_id ) {

/**
* Save generated thumbnail ID.
* In additional meta data. so we can identify which if current one is auto generated or custom.
*/
update_post_meta( $media_id, $last_generated_thumbnail_id_key, $attachment_id );

/**
* Delete previously generated attachment.
*/
if ( ! empty( $last_generated_thumbnail_id ) ) {
wp_delete_attachment( $last_generated_thumbnail_id );
}

$attach_data = wp_generate_attachment_metadata( $attachment_id, $thumbnail_src );
wp_update_attachment_metadata( $attachment_id, $attach_data );
set_post_thumbnail( $media_id, $attachment_id );
update_post_meta( $attachment_id, 'amp_is_poster', true );

/**
* Set newly generate thumbnail to attachment only if is was set automatically.
*/
if ( ! $has_custom_thumbnail ) {
set_post_thumbnail( $media_id, $attachment_id );
}

}
}
}
Expand Down

0 comments on commit 4d226d0

Please sign in to comment.