Skip to content

Commit 2fbb6f7

Browse files
committed
Add fallback of featured image to auto generated image for video attachment
1 parent 4d226d0 commit 2fbb6f7

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

admin/rt-retranscode-admin.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ public function __construct() {
7878
add_filter( 'amp_story_allowed_video_types', array( $this, 'add_amp_video_extensions' ) ); // Extend allowed video mime type extensions for AMP Story Background.
7979
add_filter( 'render_block', array( $this, 'update_amp_story_video_url' ), 10, 2 ); // Filter block content and replace video URLs.
8080

81+
add_filter( 'get_post_metadata', [ $this, 'get_post_thumbnail_metadata' ], 10, 4 );
82+
8183
// Allow people to change what capability is required to use this feature.
8284
$this->capability = apply_filters( 'retranscode_media_cap', 'manage_options' );
8385

@@ -1049,6 +1051,42 @@ public function add_search_mime_types( $where ) {
10491051
return $where;
10501052
}
10511053

1054+
/**
1055+
* Set default preview of image attachment is image it self.
1056+
*
1057+
* @param string|int|array $value Meta value.
1058+
* @param int $post_id Post ID.
1059+
* @param string $meta_key Meta key.
1060+
* @param bool $single If request single value or all the value.
1061+
*
1062+
* @return string|int|array Meta value.
1063+
*/
1064+
public function get_post_thumbnail_metadata( $value, $post_id, $meta_key, $single ) {
1065+
1066+
if ( empty( $post_id ) || 0 >= intval( $post_id ) || '_thumbnail_id' !== $meta_key ) {
1067+
return $value;
1068+
}
1069+
1070+
$post = get_post( $post_id );
1071+
1072+
// Only for attachment and if it. We don't want to touch any other post types.
1073+
if ( empty( $post ) || ! is_a( $post, 'WP_Post' ) || 'attachment' !== $post->post_type || false === strpos( $post->post_mime_type, 'video/' ) ) {
1074+
return $value;
1075+
}
1076+
1077+
remove_filter( 'get_post_metadata', [ $this, 'get_post_thumbnail_metadata' ], 10 );
1078+
1079+
$thumbnail_id = get_post_thumbnail_id( $post_id );
1080+
1081+
add_filter( 'get_post_metadata', [ $this, 'get_post_thumbnail_metadata' ], 10, 4 );
1082+
1083+
if ( empty( $thumbnail_id ) || 0 >= intval( $thumbnail_id ) ) {
1084+
$value = get_post_meta( $post_id, 'transcoder_generated_thumbnail_id', $single );
1085+
}
1086+
1087+
return $value;
1088+
}
1089+
10521090
}
10531091

10541092
// Start up this plugin.

0 commit comments

Comments
 (0)