Skip to content

Commit

Permalink
Add fallback of featured image to auto generated image for video atta…
Browse files Browse the repository at this point in the history
…chment
  • Loading branch information
dhaval-parekh committed May 19, 2020
1 parent 4d226d0 commit 2fbb6f7
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions admin/rt-retranscode-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' );

Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 2fbb6f7

Please sign in to comment.