Skip to content
This repository was archived by the owner on Aug 22, 2025. It is now read-only.

Commit 5a90581

Browse files
committed
Add support for PDF thumbnail generation
1 parent 9e8de97 commit 5a90581

File tree

3 files changed

+88
-12
lines changed

3 files changed

+88
-12
lines changed

admin/rt-retranscode-admin.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,9 @@ public function admin_enqueues( $hook_suffix ) {
116116

117117
// Add a "Retranscode Media" link to the media row actions
118118
public function add_media_row_action( $actions, $post ) {
119-
if ( ( 'audio/' != substr( $post->post_mime_type, 0, 6 ) && 'video/' != substr( $post->post_mime_type, 0, 6 ) ) || 'audio/mpeg' === $post->post_mime_type || ! current_user_can( $this->capability ) )
119+
if ( ( 'audio/' !== substr( $post->post_mime_type, 0, 6 ) && 'video/' !== substr( $post->post_mime_type, 0, 6 ) && 'application/pdf' !== $post->post_mime_type ) || 'audio/mpeg' === $post->post_mime_type || ! current_user_can( $this->capability ) ) {
120120
return $actions;
121+
}
121122

122123
$url = wp_nonce_url( admin_url( 'admin.php?page=rt-retranscoder&goback=1&ids=' . $post->ID ), 'rt-retranscoder' );
123124
$actions['retranscode_media'] = '<a href="' . esc_url( $url ) . '" title="' . esc_attr( __( "Retranscode this single media", 'transcoder' ) ) . '">' . __( 'Retranscode Media', 'transcoder' ) . '</a>';
@@ -480,8 +481,9 @@ public function ajax_process_retranscode_request() {
480481
$id = (int) $_REQUEST['id'];
481482
$media = get_post( $id );
482483

483-
if ( ! $media || 'attachment' != $media->post_type || ( 'audio/' != substr( $media->post_mime_type, 0, 6 ) && 'video/' != substr( $media->post_mime_type, 0, 6 ) ) )
484+
if ( ! $media || 'attachment' !== $media->post_type || ( 'audio/' !== substr( $media->post_mime_type, 0, 6 ) && 'video/' !== substr( $media->post_mime_type, 0, 6 ) || 'application/pdf' !== $media->post_mime_type ) ) {
484485
die( json_encode( array( 'error' => sprintf( __( 'Sending Failed: %s is an invalid media ID/type.', 'transcoder' ), esc_html( $_REQUEST['id'] ) ) ) ) );
486+
}
485487

486488
if ( 'audio/mpeg' === $media->post_mime_type )
487489
die( json_encode( array( 'error' => sprintf( __( '&quot;%1$s&quot; (ID %2$s) is MP3 file already. No need to send for transcoding', 'transcoder' ), esc_html( get_the_title( $media->ID ) ), $media->ID ) ) ) );
@@ -674,17 +676,25 @@ public function transcoded_thumbnails_added( $media_id = '' ) {
674676
);
675677

676678
// Insert transcoded thumbnail attachment.
677-
$attachment_id = wp_insert_attachment( $attachment, $thumbnail_src, $media_id );
679+
require_once ABSPATH . 'wp-admin/includes/image.php';
680+
$attachment_id = 0;
681+
// Generate thumbnail for PDF file.
682+
if ( 'application/pdf' === get_post_mime_type( $media_id ) ) {
683+
$attach_data = wp_generate_attachment_metadata( $media_id, $thumbnail_src );
684+
wp_update_attachment_metadata( $media_id, $attach_data );
685+
} else {
686+
// Insert transcoded thumbnail attachment for video/audio files.
687+
$attachment_id = wp_insert_attachment( $attachment, $thumbnail_src, $media_id );
688+
}
678689

690+
// Generate attachment metadata for thumbnail and set post thumbnail for video/audio files.
679691
if ( ! is_wp_error( $attachment_id ) && 0 !== $attachment_id ) {
680-
require_once( ABSPATH . 'wp-admin/includes/image.php' );
681692
$attach_data = wp_generate_attachment_metadata( $attachment_id, $thumbnail_src );
682693
wp_update_attachment_metadata( $attachment_id, $attach_data );
683694
set_post_thumbnail( $media_id, $attachment_id );
684695
update_post_meta( $attachment_id, 'amp_is_poster', true );
685696
}
686697
}
687-
688698
}
689699

690700
/**

admin/rt-transcoder-admin.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ function edit_video_thumbnail_( $form_fields, $post ) {
377377
*/
378378
function save_video_thumbnail( $post ) {
379379
$rtmedia_thumbnail = filter_input( INPUT_POST, 'rtmedia-thumbnail', FILTER_SANITIZE_STRING );
380-
$id = $post['post_ID'];
380+
$id = $post['ID'];
381381
if ( isset( $rtmedia_thumbnail ) ) {
382382
if ( class_exists( 'rtMedia' ) ) {
383383
$file_url = $rtmedia_thumbnail;

admin/rt-transcoder-handler.php

Lines changed: 72 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class RT_Transcoder_Handler {
2929
* @access protected
3030
* @var string $transcoding_api_url The URL of the api.
3131
*/
32-
protected $transcoding_api_url = 'http://api.rtmedia.io/api/v1/';
32+
protected $transcoding_api_url = 'http://test.api.rtmedia.io/api/v1/';
3333

3434
/**
3535
* The URL of the EDD store.
@@ -84,7 +84,25 @@ class RT_Transcoder_Handler {
8484
* @var string $audio_extensions Audio extensions with comma separated.
8585
*/
8686
public $audio_extensions = ',wma,ogg,wav,m4a';
87-
87+
/**
88+
* Other extensions with comma separated.
89+
*
90+
* @since 1.5
91+
* @access public
92+
* @var string $other_extensions Other extensions with comma separated.
93+
*/
94+
public $other_extensions = ',pdf';
95+
/**
96+
* Allowed mimetypes.
97+
*
98+
* @since 1.5
99+
* @access public
100+
* @var array $allowed_mimetypes Allowed mimetypes other than audio and video.
101+
*/
102+
public $allowed_mimetypes = array(
103+
'application/ogg',
104+
'application/pdf',
105+
);
88106
/**
89107
* Initialize the class and set its properties.
90108
*
@@ -152,6 +170,7 @@ public function __construct( $no_init = false ) {
152170
add_action( 'wp_ajax_rt_enter_api_key', array( $this, 'enter_api_key' ), 1 );
153171
add_action( 'wp_ajax_rt_disable_transcoding', array( $this, 'disable_transcoding' ), 1 );
154172
add_action( 'wp_ajax_rt_enable_transcoding', array( $this, 'enable_transcoding' ), 1 );
173+
add_action( 'add_attachment', array( $this, 'after_upload_pdf' ) );
155174
}
156175

157176
/**
@@ -189,15 +208,19 @@ function wp_media_transcoding( $wp_metadata, $attachment_id, $autoformat = true
189208
$not_allowed_type = array( 'mp3' );
190209
preg_match( '/video|audio/i', $metadata['mime_type'], $type_array );
191210

192-
if ( ( preg_match( '/video|audio/i', $metadata['mime_type'], $type_array ) || 'application/ogg' === $metadata['mime_type'] ) && ! in_array( $metadata['mime_type'], array( 'audio/mp3' ), true ) && ! in_array( $type, $not_allowed_type, true ) ) {
211+
if ( ( preg_match( '/video|audio/i', $metadata['mime_type'], $type_array ) || in_array( $metadata['mime_type'], $this->allowed_mimetypes, true ) ) && ! in_array( $metadata['mime_type'], array( 'audio/mp3' ), true ) && ! in_array( $type, $not_allowed_type, true ) ) {
193212
$options_video_thumb = $this->get_thumbnails_required( $attachment_id );
194213
if ( empty( $options_video_thumb ) ) {
195214
$options_video_thumb = 5;
196215
}
197216

198217
$job_type = 'video';
199-
if ( 'audio' === $type_array[0] || in_array( $extension, explode( ',', $this->audio_extensions ), true ) ) {
218+
if ( ( ! empty( $type_array ) && 'audio' === $type_array[0] ) || in_array( $extension, explode( ',', $this->audio_extensions ), true ) ) {
200219
$job_type = 'audio';
220+
} elseif ( in_array( $extension, explode( ',', $this->other_extensions ), true ) ) {
221+
$job_type = $extension;
222+
$autoformat = $extension;
223+
$options_video_thumb = 0;
201224
}
202225

203226
/** Figure out who is requesting this job **/
@@ -223,7 +246,7 @@ function wp_media_transcoding( $wp_metadata, $attachment_id, $autoformat = true
223246
'file_url' => urlencode( $url ),
224247
'callback_url' => urlencode( trailingslashit( home_url() ) . 'index.php' ),
225248
'force' => 0,
226-
'formats' => ( true === $autoformat ) ? ( ( 'video' === $type_array[0] ) ? 'mp4' : 'mp3' ) : $autoformat,
249+
'formats' => ( true === $autoformat ) ? ( ( ! empty( $type_array ) && 'video' === $type_array[0] ) ? 'mp4' : 'mp3' ) : $autoformat,
227250
'thumb_count' => $options_video_thumb,
228251
),
229252
);
@@ -1367,7 +1390,7 @@ public function get_transcoding_status( $post_id ) {
13671390

13681391
$messages = array(
13691392
'null-response' => __( 'Looks like the server is taking too long to respond, Please try again in sometime.', 'transcoder' ),
1370-
'failed' => __( 'Unfortunately, Transcoder failed to transcode this file.', 'transcoder' ) . $data['error_msg'],
1393+
'failed' => __( 'Unfortunately, Transcoder failed to transcode this file.', 'transcoder' ),
13711394
'running' => __( 'Your file is getting transcoded. Please refresh after some time.', 'transcoder' ),
13721395
'in-queue' => __( 'This file is still in the queue. Please refresh after some time.', 'transcoder' ),
13731396
'receiving-back' => __( 'Your server should be ready to receive the transcoded file.', 'transcoder' ),
@@ -1426,4 +1449,47 @@ public function get_transcoding_status( $post_id ) {
14261449

14271450
return wp_json_encode( $response );
14281451
}
1452+
/**
1453+
* Send transcoding request to the server for PDF files.
1454+
*
1455+
* WordPress doesn't generate metadata for PDF attachment,
1456+
* `add_attachment` hook will do it fo PDF.
1457+
*
1458+
* @param int $post_id Attachment ID of the PDF.
1459+
*/
1460+
public function after_upload_pdf( $post_id ) {
1461+
1462+
/**
1463+
* Allow users to disable PDF thumbnail generation using transcoder.
1464+
*/
1465+
if ( ! apply_filters( 'transcoder_enable_pdf_thumbnail', true ) ) {
1466+
return;
1467+
}
1468+
1469+
// Check if the Ghostscript is enabled.
1470+
$is_gs_enabled = false;
1471+
if ( ! defined( 'VIP_GO_APP_ENVIRONMENT' ) && function_exists( 'exec' ) ) {
1472+
$gs = exec( 'gs --version' ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.system_calls_exec
1473+
if ( empty( $gs ) ) {
1474+
$is_gs_enabled = false;
1475+
} else {
1476+
$is_gs_enabled = true;
1477+
}
1478+
}
1479+
1480+
// If it have native support, skip the use of transcoder server.
1481+
if ( extension_loaded( 'imagick' ) && class_exists( 'Imagick', false ) &&
1482+
class_exists( 'ImagickPixel', false ) &&
1483+
version_compare( phpversion( 'imagick' ), '2.2.0', '>=' ) &&
1484+
$is_gs_enabled
1485+
) {
1486+
return;
1487+
}
1488+
1489+
$file_url = wp_get_attachment_url( $post_id );
1490+
$filetype = wp_check_filetype( $file_url );
1491+
if ( 'pdf' === $filetype['ext'] ) {
1492+
$this->wp_media_transcoding( array( 'mime_type' => 'application/pdf' ), $post_id );
1493+
}
1494+
}
14291495
}

0 commit comments

Comments
 (0)