Skip to content

Commit d74fe2b

Browse files
committed
Update fallback poster, replace poster in post edit
1 parent 269b549 commit d74fe2b

8 files changed

+228
-21
lines changed
2.1 KB
Loading
2.43 KB
Loading

admin/js/rt-transcoder-block-editor-support.js

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,43 @@
1-
const { rt_transcoder_block_editor_support } = window;
1+
import apiFetch from '@wordpress/api-fetch';
2+
3+
const { rtTranscoderBlockEditorSupport } = window;
24

35
const updateAMPStoryPoster = ( BlockEdit ) => {
46
return ( props ) => {
57
const mediaAttributes = props.attributes;
6-
if ( 'amp/amp-story-page' === props.name && typeof mediaAttributes.mediaType !== 'undefined' && 'video' === mediaAttributes.mediaType ) {
7-
if ( typeof mediaAttributes.poster === 'undefined' && typeof rt_transcoder_block_editor_support.amp_story_fallback_poster !== 'undefined' && ! mediaAttributes.mediaUrl.endsWith( 'mp4' ) ) {
8-
props.attributes.poster = rt_transcoder_block_editor_support.amp_story_fallback_poster;
9-
}
10-
} else if ( 'core/video' === props.name ) {
11-
if ( typeof mediaAttributes.poster === 'undefined' && typeof mediaAttributes.src !== 'undefined' && mediaAttributes.src.indexOf( 'blob:' ) !== 0 ) {
12-
if ( ! mediaAttributes.src.endsWith( 'mp4' ) ) {
13-
props.attributes.poster = rt_transcoder_block_editor_support.amp_video_fallback_poster;
8+
const isAMPStory = 'amp/amp-story-page' === props.name;
9+
const isVideoBlock = 'core/video' === props.name;
10+
const mediaId = isAMPStory ? mediaAttributes.mediaId : mediaAttributes.id;
11+
if ( typeof mediaId !== 'undefined' ) {
12+
if ( typeof mediaAttributes.poster === 'undefined' ) {
13+
if ( isAMPStory && typeof mediaAttributes.mediaType !== 'undefined' &&
14+
'video' === mediaAttributes.mediaType && ! mediaAttributes.mediaUrl.endsWith( 'mp4' ) ) {
15+
props.attributes.poster = rtTranscoderBlockEditorSupport.amp_story_fallback_poster;
16+
} else if ( isVideoBlock && typeof mediaAttributes.src !== 'undefined' &&
17+
mediaAttributes.src.indexOf( 'blob:' ) !== 0 && ! mediaAttributes.src.endsWith( 'mp4' ) ) {
18+
props.attributes.poster = rtTranscoderBlockEditorSupport.amp_video_fallback_poster;
1419
}
20+
} else if ( mediaAttributes.poster.endsWith( '-fallback-poster.png' ) ) {
21+
const restBase = '/wp-json/transcoder/v1/amp-media';
22+
apiFetch( {
23+
path: `${ restBase }/${ mediaId }`,
24+
} ).then( data => {
25+
if ( false !== data && null !== data ) {
26+
if ( data.poster.length ) {
27+
if ( isAMPStory && typeof mediaAttributes.mediaType !== 'undefined' && 'video' === mediaAttributes.mediaType ) {
28+
props.attributes.poster = data.poster;
29+
} else if ( isVideoBlock ) {
30+
props.attributes.poster = data.poster;
31+
}
32+
}
33+
}
34+
} );
1535
}
1636
}
1737

1838
return (
1939
<BlockEdit { ...props } />
20-
)
40+
);
2141
};
2242
};
2343

admin/rt-retranscode-admin.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,24 @@ public function __construct() {
5656

5757
// Allow people to change what capability is required to use this feature
5858
$this->capability = apply_filters( 'retranscode_media_cap', 'manage_options' );
59+
60+
// Load Rest Endpoints.
61+
$this->load_rest_endpoints();
5962
}
6063

64+
/**
65+
* Function to load rest api endpoints.
66+
*
67+
* @return void
68+
*/
69+
public function load_rest_endpoints() {
70+
$rest_class_file_path = RT_TRANSCODER_PATH . 'admin/rt-transcoder-rest-routes.php';
71+
include_once $rest_class_file_path;
72+
73+
// Create class object and register routes.
74+
$transcoder_rest_routes = new Transcoder_Rest_Routes();
75+
add_action( 'rest_api_init', array( $transcoder_rest_routes, 'register_routes' ) );
76+
}
6177

6278
// Register the management page
6379
public function add_admin_menu() {
@@ -665,6 +681,7 @@ public function transcoded_thumbnails_added( $media_id = '' ) {
665681
$attach_data = wp_generate_attachment_metadata( $attachment_id, $thumbnail_src );
666682
wp_update_attachment_metadata( $attachment_id, $attach_data );
667683
set_post_thumbnail( $media_id, $attachment_id );
684+
update_post_meta( $attachment_id, 'amp_is_poster', true );
668685
}
669686
}
670687

@@ -783,7 +800,23 @@ public function update_amp_story_video_url( $block_content, $block ) {
783800
}
784801
}
785802
}
803+
}
786804

805+
// Replace fallback poster with generated thumbnail for video block.
806+
$video_story_poster = '/<video (.*?) poster="(?<poster>.*?)" (.*?)>/m';
807+
preg_match_all( $video_story_poster, $block_content, $video_poster_matches, PREG_SET_ORDER, 0);
808+
809+
if ( ! empty( $video_poster_matches ) ) {
810+
foreach ( $video_poster_matches as $video_poster_match ) {
811+
if ( isset( $video_poster_match['poster'] ) ) {
812+
if ( false !== strpos( $video_poster_match['poster'], 'amp-story-video-fallback-poster.png' ) ) {
813+
$video_thumbnail_url = get_the_post_thumbnail_url( $mediaID );
814+
if ( false !== $video_thumbnail_url ) {
815+
$block_content = str_replace( $video_poster_match['poster'], $video_thumbnail_url, $block_content );
816+
}
817+
}
818+
}
819+
}
787820
}
788821

789822
}

admin/rt-transcoder-functions.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -808,7 +808,7 @@ function rt_transcoder_enqueue_block_editor_assets() {
808808
// Localize fallback poster image for use in our enqueued script.
809809
wp_localize_script(
810810
'rt-transcoder-block-editor-support',
811-
'rt_transcoder_block_editor_support',
811+
'rtTranscoderBlockEditorSupport',
812812
[
813813
'amp_story_fallback_poster' => plugins_url( '/images/amp-story-fallback-poster.png', __FILE__ ),
814814
'amp_video_fallback_poster' => plugins_url( '/images/amp-story-video-fallback-poster.png', __FILE__ )

admin/rt-transcoder-rest-routes.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
/**
4+
* Class Transcoder_Rest_Routes
5+
* Handle REST Routes for Transcoder.
6+
*/
7+
class Transcoder_Rest_Routes extends WP_REST_Controller {
8+
9+
public $version = 1;
10+
11+
public $namespace_prefix = 'transcoder/v';
12+
13+
/**
14+
* Function to register routes.
15+
*/
16+
public function register_routes() {
17+
18+
// Register `amp-media` route to get media poster info.
19+
register_rest_route(
20+
$this->namespace_prefix . $this->version,
21+
'/amp-media/(?P<id>\d+)',
22+
array(
23+
'methods' => WP_REST_Server::READABLE,
24+
'callback' => array( $this, 'get_media_data' ),
25+
) );
26+
}
27+
28+
/**
29+
* Return poster url for requested media if exists.
30+
*
31+
* @param WP_REST_Request $request Object of WP_REST_Request
32+
*
33+
* @return array|bool
34+
*/
35+
public function get_media_data( WP_REST_Request $request ) {
36+
$media_id = $request->get_param( 'id' );
37+
38+
// Check media id.
39+
if ( ! empty( $media_id ) ) {
40+
// Check if thumbnail exists for the passed attachment.
41+
$thumbnail_id = get_post_thumbnail_id( $media_id );
42+
if ( ! empty( $thumbnail_id ) && true === (bool) get_post_meta( $thumbnail_id, 'amp_is_poster', true ) ) {
43+
return [
44+
'poster' => get_the_post_thumbnail_url( $media_id )
45+
];
46+
}
47+
} else {
48+
return false;
49+
}
50+
51+
return false;
52+
}
53+
54+
}

package-lock.json

Lines changed: 107 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,8 @@
3434
"load-grunt-tasks": "5.1.0",
3535
"uglifyjs-webpack-plugin": "1.1.6",
3636
"webpack": "3.10.0"
37+
},
38+
"dependencies": {
39+
"@wordpress/api-fetch": "3.6.0"
3740
}
3841
}

0 commit comments

Comments
 (0)