From 2fbb6f7b1daaa1a627915a230c625fa73e8cd03c Mon Sep 17 00:00:00 2001 From: Dhaval Parekh Date: Tue, 19 May 2020 19:29:22 +0530 Subject: [PATCH] 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 b176415..f7dea3b 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.