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

Commit ae3757c

Browse files
Merge pull request #154 from rtMediaWP/add/pdf-support-dev
Add support for PDF thumbnail generation
2 parents 1d3df54 + 26be5ee commit ae3757c

File tree

3 files changed

+128
-25
lines changed

3 files changed

+128
-25
lines changed

admin/rt-retranscode-admin.php

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88

99
/**
10-
* Media retranscode module.
10+
* Media retranscode module.
1111
*/
1212
class RetranscodeMedia {
1313
/**
@@ -31,7 +31,7 @@ class RetranscodeMedia {
3131
*/
3232
public $stored_api_key;
3333

34-
34+
3535
/**
3636
* Usage info of transcoder subscription.
3737
*
@@ -136,9 +136,9 @@ public function _transcoder_settings_page() {
136136
include_once RT_TRANSCODER_PATH . 'admin/partials/rt-transcoder-admin-display.php'; // phpcs:ignore WordPressVIPMinimum.Files.IncludingFile.UsingCustomConstant
137137
}
138138

139-
/**
139+
/**
140140
* Enqueue the needed Javascript and CSS
141-
*
141+
*
142142
* @param string $hook_suffix Suffix of the hook.
143143
*
144144
* @return void
@@ -172,17 +172,25 @@ public function add_media_row_action( $actions, $post ) {
172172

173173
if ( (
174174
'audio/' !== substr( $post->post_mime_type, 0, 6 ) &&
175-
'video/' !== substr( $post->post_mime_type, 0, 6 )
175+
'video/' !== substr( $post->post_mime_type, 0, 6 ) &&
176+
'application/pdf' !== $post->post_mime_type
176177
) ||
177178
'audio/mpeg' === $post->post_mime_type ||
178179
! current_user_can( $this->capability )
179180
) {
180181
return $actions;
181182
}
182183

184+
$actions = ( ! empty( $actions ) && is_array( $actions ) ) ? $actions : array();
185+
183186
$url = wp_nonce_url( admin_url( 'admin.php?page=rt-retranscoder&goback=1&ids=' . $post->ID ), 'rt-retranscoder' );
184187

185-
$actions['retranscode_media'] = '<a href="' . esc_url( $url ) . '" title="' . esc_attr( __( 'Retranscode this single media', 'transcoder' ) ) . '">' . __( 'Retranscode Media', 'transcoder' ) . '</a>';
188+
$actions['retranscode_media'] = sprintf(
189+
'<a href="%s" title="%s">%s</a>',
190+
esc_url( $url ),
191+
esc_attr__( 'Retranscode this single media', 'transcoder' ),
192+
__( 'Retranscode Media', 'transcoder' )
193+
);
186194

187195
return $actions;
188196
}
@@ -318,7 +326,7 @@ public function retranscode_interface() {
318326
$media = $query->get_posts();
319327
remove_filter( 'posts_where', array( $this, 'add_search_mime_types' ) );
320328
if ( empty( $media ) || is_wp_error( $media ) ) {
321-
329+
322330
// translators: Link to the media page.
323331
echo ' <p>' . sprintf( esc_html__( "Unable to find any media. Are you sure <a href='%s'>some exist</a>?", 'transcoder' ), esc_url( admin_url( 'upload.php' ) ) ) . '</p></div>';
324332
return;
@@ -402,10 +410,10 @@ public function retranscode_interface() {
402410
<?php
403411
$count = count( $media );
404412

405-
413+
406414
// translators: Count of media which were successfully transcoded with the time in seconds.
407415
$text_goback = ( ! empty( $_GET['goback'] ) ) ? sprintf( __( 'To go back to the previous page, <a href="%s">click here</a>.', 'transcoder' ), 'javascript:history.go(-1)' ) : '';
408-
416+
409417
// translators: Count of media which were successfully and media which were failed transcoded with the time in seconds and previout page link.
410418
$text_failures = sprintf( __( 'All done! %1$s media file(s) were successfully sent for transcoding in %2$s seconds and there were %3$s failure(s). To try transcoding the failed media again, <a href="%4$s">click here</a>. %5$s', 'transcoder' ), "' + rt_successes + '", "' + rt_totaltime + '", "' + rt_errors + '", esc_url( wp_nonce_url( admin_url( 'admin.php?page=rt-retranscoder&goback=1' ), 'rt-retranscoder' ) . '&ids=' ) . "' + rt_failedlist + '", $text_goback );
411419
// translators: Count of media which were successfully transcoded with the time in seconds and previout page link.
@@ -560,7 +568,7 @@ function RetranscodeMedia( id ) {
560568
// ]]>
561569
</script>
562570
<?php
563-
} else {
571+
} else {
564572
// No button click? Display the form.
565573
?>
566574
<form method="post" action="">
@@ -571,7 +579,7 @@ function RetranscodeMedia( id ) {
571579
<i><?php printf( esc_html__( 'Sending your entire media library for retranscoding can consume a lot of your bandwidth allowance, so use this tool with care.', 'transcoder' ) ); ?></i></p>
572580

573581
<p>
574-
<?php
582+
<?php
575583
// translators: Placeholder is for admin media section link.
576584
printf( wp_kses( __( "You can retranscode specific media files (rather than ALL media) from the <a href='%s'>Media</a> page using Bulk Action via drop down or mouse hover a specific media (audio/video) file.", 'transcoder' ), array( 'a' => array( 'href' => array() ) ) ), esc_url( admin_url( 'upload.php' ) ) );
577585
?>
@@ -608,9 +616,14 @@ public function ajax_process_retranscode_request() {
608616
}
609617

610618
$media = get_post( $id );
611-
612-
if ( ! $media || 'attachment' !== $media->post_type || ( 'audio/' !== substr( $media->post_mime_type, 0, 6 ) && 'video/' !== substr( $media->post_mime_type, 0, 6 ) ) ) {
613619

620+
if ( ! $media || 'attachment' !== $media->post_type ||
621+
(
622+
'audio/' !== substr( $media->post_mime_type, 0, 6 ) &&
623+
'video/' !== substr( $media->post_mime_type, 0, 6 ) ||
624+
'application/pdf' !== $media->post_mime_type
625+
)
626+
) {
614627
// translators: Media id of the invalid media type.
615628
die( wp_json_encode( array( 'error' => sprintf( __( 'Sending Failed: %d is an invalid media ID/type.', 'transcoder' ), intval( $id ) ) ) ) );
616629
}
@@ -695,9 +708,9 @@ public function die_json_error_msg( $id, $message ) {
695708
}
696709

697710

698-
/**
711+
/**
699712
* Helper function to escape quotes in strings for use in Javascript
700-
*
713+
*
701714
* @param string $string String to escape quotes from.
702715
*/
703716
public function esc_quotes( $string ) {
@@ -825,17 +838,25 @@ public function transcoded_thumbnails_added( $media_id = '' ) {
825838
);
826839

827840
// Insert transcoded thumbnail attachment.
828-
$attachment_id = wp_insert_attachment( $attachment, $thumbnail_src, $media_id );
841+
require_once ABSPATH . 'wp-admin/includes/image.php';
842+
$attachment_id = 0;
843+
// Generate thumbnail for PDF file.
844+
if ( 'application/pdf' === get_post_mime_type( $media_id ) ) {
845+
$attach_data = wp_generate_attachment_metadata( $media_id, $thumbnail_src );
846+
wp_update_attachment_metadata( $media_id, $attach_data );
847+
} else {
848+
// Insert transcoded thumbnail attachment for video/audio files.
849+
$attachment_id = wp_insert_attachment( $attachment, $thumbnail_src, $media_id );
850+
}
829851

852+
// Generate attachment metadata for thumbnail and set post thumbnail for video/audio files.
830853
if ( ! is_wp_error( $attachment_id ) && 0 !== $attachment_id ) {
831-
require_once ABSPATH . 'wp-admin/includes/image.php';
832854
$attach_data = wp_generate_attachment_metadata( $attachment_id, $thumbnail_src );
833855
wp_update_attachment_metadata( $attachment_id, $attach_data );
834856
set_post_thumbnail( $media_id, $attachment_id );
835857
update_post_meta( $attachment_id, 'amp_is_poster', true );
836858
}
837859
}
838-
839860
}
840861

841862
/**

admin/rt-transcoder-admin.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,8 +375,10 @@ public function edit_video_thumbnail_( $form_fields, $post ) {
375375
* @return array $form_fields
376376
*/
377377
public function save_video_thumbnail( $post ) {
378+
378379
$rtmedia_thumbnail = transcoder_filter_input( INPUT_POST, 'rtmedia-thumbnail', FILTER_SANITIZE_STRING );
379-
$id = $post['post_ID'];
380+
$id = ( ! empty( $post['ID'] ) && 0 < intval( $post['ID'] ) ) ? intval( $post['ID'] ) : 0;
381+
380382
if ( isset( $rtmedia_thumbnail ) ) {
381383
if ( class_exists( 'rtMedia' ) ) {
382384
$file_url = $rtmedia_thumbnail;

admin/rt-transcoder-handler.php

Lines changed: 86 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,27 @@ class RT_Transcoder_Handler {
8585
*/
8686
public $audio_extensions = ',wma,ogg,wav,m4a';
8787

88+
/**
89+
* Other extensions with comma separated.
90+
*
91+
* @since 1.5
92+
* @access public
93+
* @var string $other_extensions Other extensions with comma separated.
94+
*/
95+
public $other_extensions = ',pdf';
96+
97+
/**
98+
* Allowed mimetypes.
99+
*
100+
* @since 1.5
101+
* @access public
102+
* @var array $allowed_mimetypes Allowed mimetypes other than audio and video.
103+
*/
104+
public $allowed_mimetypes = array(
105+
'application/ogg',
106+
'application/pdf',
107+
);
108+
88109
/**
89110
* Initialize the class and set its properties.
90111
*
@@ -97,12 +118,23 @@ public function __construct( $no_init = false ) {
97118
$this->api_key = get_site_option( 'rt-transcoding-api-key' );
98119
$this->stored_api_key = get_site_option( 'rt-transcoding-api-key-stored' );
99120

121+
/**
122+
* Allow other plugin and wp-config to overwrite API URL.
123+
*/
124+
if ( defined( 'TRANSCODER_API_URL' ) && ! empty( TRANSCODER_API_URL ) ) {
125+
$this->transcoding_api_url = TRANSCODER_API_URL;
126+
}
127+
128+
$this->transcoding_api_url = apply_filters( 'transcoding_api_url', $this->transcoding_api_url );
129+
100130
if ( $no_init ) {
101131
return;
102132
}
133+
103134
if ( is_admin() ) {
104135
add_action( 'rt_transcoder_before_widgets', array( $this, 'usage_widget' ) );
105136
}
137+
106138
add_action( 'admin_init', array( $this, 'save_api_key' ), 10, 1 );
107139

108140
if ( $this->api_key ) {
@@ -152,6 +184,7 @@ public function __construct( $no_init = false ) {
152184
add_action( 'wp_ajax_rt_enter_api_key', array( $this, 'enter_api_key' ), 1 );
153185
add_action( 'wp_ajax_rt_disable_transcoding', array( $this, 'disable_transcoding' ), 1 );
154186
add_action( 'wp_ajax_rt_enable_transcoding', array( $this, 'enable_transcoding' ), 1 );
187+
add_action( 'add_attachment', array( $this, 'after_upload_pdf' ) );
155188
}
156189

157190
/**
@@ -189,15 +222,28 @@ public function wp_media_transcoding( $wp_metadata, $attachment_id, $autoformat
189222
$not_allowed_type = array( 'mp3' );
190223
preg_match( '/video|audio/i', $metadata['mime_type'], $type_array );
191224

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 ) ) {
193-
$options_video_thumb = $this->get_thumbnails_required( $attachment_id );
225+
if ( (
226+
preg_match( '/video|audio/i', $metadata['mime_type'], $type_array ) ||
227+
in_array( $metadata['mime_type'], $this->allowed_mimetypes, true )
228+
) &&
229+
! in_array( $metadata['mime_type'], array( 'audio/mp3' ), true ) &&
230+
! in_array( $type, $not_allowed_type, true )
231+
) {
232+
233+
$options_video_thumb = $this->get_thumbnails_required( $attachment_id );
234+
194235
if ( empty( $options_video_thumb ) ) {
195236
$options_video_thumb = 5;
196237
}
197238

198239
$job_type = 'video';
199-
if ( 'audio' === $type_array[0] || in_array( $extension, explode( ',', $this->audio_extensions ), true ) ) {
240+
241+
if ( ( ! empty( $type_array ) && 'audio' === $type_array[0] ) || in_array( $extension, explode( ',', $this->audio_extensions ), true ) ) {
200242
$job_type = 'audio';
243+
} elseif ( in_array( $extension, explode( ',', $this->other_extensions ), true ) ) {
244+
$job_type = $extension;
245+
$autoformat = $extension;
246+
$options_video_thumb = 0;
201247
}
202248

203249
/** Figure out who is requesting this job */
@@ -232,7 +278,12 @@ public function wp_media_transcoding( $wp_metadata, $attachment_id, $autoformat
232278

233279
$upload_page = wp_remote_post( $transcoding_url, $args );
234280

235-
if ( ! is_wp_error( $upload_page ) && ( ( isset( $upload_page['response']['code'] ) && ( 200 === intval( $upload_page['response']['code'] ) ) ) ) ) {
281+
if ( ! is_wp_error( $upload_page ) &&
282+
(
283+
isset( $upload_page['response']['code'] ) &&
284+
200 === intval( $upload_page['response']['code'] )
285+
)
286+
) {
236287
$upload_info = json_decode( $upload_page['body'] );
237288
if ( isset( $upload_info->status ) && $upload_info->status && isset( $upload_info->job_id ) && $upload_info->job_id ) {
238289
$job_id = $upload_info->job_id;
@@ -985,7 +1036,7 @@ public function get_post_id_by_meta_key_and_value( $key, $value ) {
9851036
$meta = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->postmeta} WHERE meta_key = %s AND meta_value = %s", $key, $value ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery
9861037
wp_cache_set( $cache_key, $meta, 'transcoder', 3600 );
9871038
}
988-
1039+
9891040
if ( is_array( $meta ) && ! empty( $meta ) && isset( $meta[0] ) ) {
9901041
$meta = $meta[0];
9911042
}
@@ -1080,7 +1131,7 @@ public function handle_callback() {
10801131
array(
10811132
'meta_value' => sanitize_text_field( wp_unslash( $job_id ) ), // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value
10821133
'meta_key' => 'rtmedia-transcoding-job-id', // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
1083-
)
1134+
)
10841135
);
10851136

10861137
if ( ! isset( $meta_details[0] ) ) {
@@ -1460,6 +1511,35 @@ public function get_transcoding_status( $post_id ) {
14601511
return wp_json_encode( $response );
14611512
}
14621513

1514+
/**
1515+
* Send transcoding request to the server for PDF files.
1516+
*
1517+
* WordPress doesn't generate metadata for PDF attachment,
1518+
* `add_attachment` hook will do it fo PDF.
1519+
*
1520+
* @param int $post_id Attachment ID of the PDF.
1521+
*/
1522+
public function after_upload_pdf( $post_id ) {
1523+
1524+
// If it have native support, skip the use of transcoder server.
1525+
if ( extension_loaded( 'imagick' ) &&
1526+
class_exists( 'Imagick', false ) &&
1527+
class_exists( 'ImagickPixel', false ) &&
1528+
version_compare( phpversion( 'imagick' ), '2.2.0', '>=' )
1529+
) {
1530+
return;
1531+
}
1532+
1533+
$file_url = wp_get_attachment_url( $post_id );
1534+
$filetype = wp_check_filetype( $file_url );
1535+
1536+
$filetype['ext'] = strtolower( $filetype['ext'] );
1537+
1538+
if ( 'pdf' === $filetype['ext'] ) {
1539+
$this->wp_media_transcoding( array( 'mime_type' => 'application/pdf' ), $post_id );
1540+
}
1541+
}
1542+
14631543
/**
14641544
* Sanitize transcoder post respopnse array.
14651545
*/

0 commit comments

Comments
 (0)