From 4d226d08ebe3fef9ae94d766a3cc8697d9d79913 Mon Sep 17 00:00:00 2001 From: Dhaval Parekh Date: Thu, 14 May 2020 19:17:48 +0530 Subject: [PATCH 1/2] Delete previous auto-generated thumbnail on retranscoding, Prevent to set auto-generated thumbnail if editor has set custom thumbnail to video --- admin/rt-retranscode-admin.php | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/admin/rt-retranscode-admin.php b/admin/rt-retranscode-admin.php index 2183874c..b176415e 100644 --- a/admin/rt-retranscode-admin.php +++ b/admin/rt-retranscode-admin.php @@ -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() ) { @@ -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 ); + } + } } } From 2fbb6f7b1daaa1a627915a230c625fa73e8cd03c Mon Sep 17 00:00:00 2001 From: Dhaval Parekh Date: Tue, 19 May 2020 19:29:22 +0530 Subject: [PATCH 2/2] Add fallback of featured image to auto generated image for video attachment --- admin/rt-retranscode-admin.php | 38 ++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/admin/rt-retranscode-admin.php b/admin/rt-retranscode-admin.php index b176415e..f7dea3b8 100644 --- a/admin/rt-retranscode-admin.php +++ b/admin/rt-retranscode-admin.php @@ -78,6 +78,8 @@ public function __construct() { add_filter( 'amp_story_allowed_video_types', array( $this, 'add_amp_video_extensions' ) ); // Extend allowed video mime type extensions for AMP Story Background. add_filter( 'render_block', array( $this, 'update_amp_story_video_url' ), 10, 2 ); // Filter block content and replace video URLs. + add_filter( 'get_post_metadata', [ $this, 'get_post_thumbnail_metadata' ], 10, 4 ); + // Allow people to change what capability is required to use this feature. $this->capability = apply_filters( 'retranscode_media_cap', 'manage_options' ); @@ -1049,6 +1051,42 @@ public function add_search_mime_types( $where ) { return $where; } + /** + * Set default preview of image attachment is image it self. + * + * @param string|int|array $value Meta value. + * @param int $post_id Post ID. + * @param string $meta_key Meta key. + * @param bool $single If request single value or all the value. + * + * @return string|int|array Meta value. + */ + public function get_post_thumbnail_metadata( $value, $post_id, $meta_key, $single ) { + + if ( empty( $post_id ) || 0 >= intval( $post_id ) || '_thumbnail_id' !== $meta_key ) { + return $value; + } + + $post = get_post( $post_id ); + + // Only for attachment and if it. We don't want to touch any other post types. + if ( empty( $post ) || ! is_a( $post, 'WP_Post' ) || 'attachment' !== $post->post_type || false === strpos( $post->post_mime_type, 'video/' ) ) { + return $value; + } + + remove_filter( 'get_post_metadata', [ $this, 'get_post_thumbnail_metadata' ], 10 ); + + $thumbnail_id = get_post_thumbnail_id( $post_id ); + + add_filter( 'get_post_metadata', [ $this, 'get_post_thumbnail_metadata' ], 10, 4 ); + + if ( empty( $thumbnail_id ) || 0 >= intval( $thumbnail_id ) ) { + $value = get_post_meta( $post_id, 'transcoder_generated_thumbnail_id', $single ); + } + + return $value; + } + } // Start up this plugin.