@@ -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