@@ -806,6 +806,17 @@ public function transcoded_thumbnails_added( $media_id = '' ) {
806806 return ;
807807 }
808808
809+ // Meta key for meta data, Which store the last generated thumbnail (by transcoder).
810+ $ last_generated_thumbnail_id_key = 'transcoder_generated_thumbnail_id ' ;
811+
812+ $ last_generated_thumbnail_id = get_post_meta ( $ media_id , $ last_generated_thumbnail_id_key , true );
813+ $ last_generated_thumbnail_id = ( ! empty ( $ last_generated_thumbnail_id ) && 0 < intval ( $ last_generated_thumbnail_id ) ) ? intval ( $ last_generated_thumbnail_id ) : 0 ;
814+
815+ $ post_thumbnail_id = get_post_thumbnail_id ( $ media_id );
816+ $ post_thumbnail_id = ( ! empty ( $ post_thumbnail_id ) && 0 < intval ( $ post_thumbnail_id ) ) ? intval ( $ post_thumbnail_id ) : 0 ;
817+
818+ $ has_custom_thumbnail = ( ! empty ( $ last_generated_thumbnail_id ) && $ post_thumbnail_id !== $ last_generated_thumbnail_id );
819+
809820 $ is_retranscoding_job = get_post_meta ( $ media_id , '_rt_retranscoding_sent ' , true );
810821
811822 if ( $ is_retranscoding_job && ! rtt_is_override_thumbnail () ) {
@@ -858,10 +869,31 @@ public function transcoded_thumbnails_added( $media_id = '' ) {
858869
859870 // Generate attachment metadata for thumbnail and set post thumbnail for video/audio files.
860871 if ( ! is_wp_error ( $ attachment_id ) && 0 !== $ attachment_id ) {
872+
873+ /**
874+ * Save generated thumbnail ID.
875+ * In additional meta data. so we can identify which if current one is auto generated or custom.
876+ */
877+ update_post_meta ( $ media_id , $ last_generated_thumbnail_id_key , $ attachment_id );
878+
879+ /**
880+ * Delete previously generated attachment.
881+ */
882+ if ( ! empty ( $ last_generated_thumbnail_id ) ) {
883+ wp_delete_attachment ( $ last_generated_thumbnail_id );
884+ }
885+
861886 $ attach_data = wp_generate_attachment_metadata ( $ attachment_id , $ thumbnail_src );
862887 wp_update_attachment_metadata ( $ attachment_id , $ attach_data );
863- set_post_thumbnail ( $ media_id , $ attachment_id );
864888 update_post_meta ( $ attachment_id , 'amp_is_poster ' , true );
889+
890+ /**
891+ * Set newly generate thumbnail to attachment only if is was set automatically.
892+ */
893+ if ( ! $ has_custom_thumbnail ) {
894+ set_post_thumbnail ( $ media_id , $ attachment_id );
895+ }
896+
865897 }
866898 }
867899 }
0 commit comments