-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathrt-transcoder-handler.php
executable file
·1646 lines (1406 loc) · 59.4 KB
/
rt-transcoder-handler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* The transcoder-specific functionality of the plugin.
*
* @since 1.0.0
*
* @package Transcoder
* @subpackage Transcoder/TranscoderHandler
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Handle request/response with trancoder api.
*
* @since 1.0.0
*
* @package Transcoder
* @subpackage Transcoder/TranscoderHandler
*/
class RT_Transcoder_Handler {
/**
* The transcoder API URL.
*
* @since 1.0.0
* @access protected
* @var string $transcoding_api_url The URL of the api.
*/
protected $transcoding_api_url = 'https://api.rtmedia.io/api/v1/';
/**
* The URL of the EDD store.
*
* @since 1.0.0
* @access protected
* @var string $store_url The URL of the transcoder api.
*/
protected $store_url = 'https://rtmedia.io/';
/**
* Contain uploaded media information.
*
* @since 1.0.0
* @access public
* @var array $uploaded Contain uploaded media information.
*/
public $uploaded = array();
/**
* The api key of transcoding service subscription.
*
* @since 1.0.0
* @access public
* @var string $api_key The api key of transcoding service subscription.
*/
public $api_key = false;
/**
* The api key of transcoding service subscription.
*
* @since 1.0.0
* @access public
* @var string $stored_api_key The api key of transcoding service subscription.
*/
public $stored_api_key = false;
/**
* Video extensions with comma separated.
*
* @since 1.0.0
* @access public
* @var string $video_extensions Video extensions with comma separated.
*/
public $video_extensions = ',mov,m4v,m2v,avi,mpg,flv,wmv,mkv,webm,ogv,mxf,asf,vob,mts,qt,mpeg,x-msvideo,3gp';
/**
* Audio extensions with comma separated.
*
* @since 1.0.0
* @access public
* @var string $audio_extensions Audio extensions with comma separated.
*/
public $audio_extensions = ',wma,ogg,wav,m4a';
/**
* Other extensions with comma separated.
*
* @since 1.5
* @access public
* @var string $other_extensions Other extensions with comma separated.
*/
public $other_extensions = ',pdf';
/**
* Allowed mimetypes.
*
* @since 1.5
* @access public
* @var array $allowed_mimetypes Allowed mimetypes other than audio and video.
*/
public $allowed_mimetypes = array(
'application/ogg',
'application/pdf',
);
/**
* Initialize the class and set its properties.
*
* @since 1.0.0
*
* @param bool $no_init If true then do nothing else continue.
*/
public function __construct( $no_init = false ) {
$this->api_key = get_site_option( 'rt-transcoding-api-key' );
$this->stored_api_key = get_site_option( 'rt-transcoding-api-key-stored' );
/**
* Allow other plugin and wp-config to overwrite API URL.
*/
if ( defined( 'TRANSCODER_API_URL' ) && ! empty( TRANSCODER_API_URL ) ) {
$this->transcoding_api_url = TRANSCODER_API_URL;
}
$this->transcoding_api_url = apply_filters( 'transcoding_api_url', $this->transcoding_api_url );
if ( $no_init ) {
return;
}
if ( is_admin() ) {
add_action( 'rt_transcoder_before_widgets', array( $this, 'usage_widget' ) );
}
add_action( 'admin_init', array( $this, 'save_api_key' ), 10, 1 );
if ( $this->api_key ) {
// Store api key as different db key if user disable transcoding service.
if ( ! $this->stored_api_key ) {
$this->stored_api_key = $this->api_key;
update_site_option( 'rt-transcoding-api-key-stored', $this->stored_api_key );
}
add_filter( 'rtmedia_allowed_types', array( $this, 'allowed_types_admin_settings' ), 10, 1 );
$usage_info = get_site_option( 'rt-transcoding-usage' );
if ( isset( $usage_info ) && is_array( $usage_info ) && array_key_exists( $this->api_key, $usage_info ) ) {
if ( isset( $usage_info[ $this->api_key ]->plan->expires )
&& strtotime( $usage_info[ $this->api_key ]->plan->expires ) < time() ) {
$usage_info = $this->update_usage( $this->api_key );
}
if ( array_key_exists( $this->api_key, $usage_info ) && is_object( $usage_info[ $this->api_key ] ) && isset( $usage_info[ $this->api_key ]->status ) && $usage_info[ $this->api_key ]->status ) {
if ( isset( $usage_info[ $this->api_key ]->remaining ) && $usage_info[ $this->api_key ]->remaining > 0 ) {
// Enable re-transcoding.
include_once RT_TRANSCODER_PATH . 'admin/rt-retranscode-admin.php'; // phpcs:ignore WordPressVIPMinimum.Files.IncludingFile.UsingCustomConstant
if ( $usage_info[ $this->api_key ]->remaining < 524288000 && ! get_site_option( 'rt-transcoding-usage-limit-mail' ) ) {
$this->nearing_usage_limit( $usage_info );
} elseif ( $usage_info[ $this->api_key ]->remaining > 524288000 && get_site_option( 'rt-transcoding-usage-limit-mail' ) ) {
update_site_option( 'rt-transcoding-usage-limit-mail', 0 );
}
if ( strtotime( $usage_info[ $this->api_key ]->plan->expires ) > time() ) {
add_filter( 'wp_generate_attachment_metadata', array( $this, 'wp_media_transcoding' ), 21, 2 );
}
/* Do not let the user to upload non supported media types on localhost */
$blacklist = rtt_get_blacklist_ip_addresses();
$remote_addr = rtt_get_remote_ip_address();
if ( ! in_array( wp_unslash( $remote_addr ), $blacklist, true ) ) {
add_filter( 'rtmedia_plupload_files_filter', array( $this, 'allowed_types' ), 10, 1 );
add_filter( 'rtmedia_allowed_types', array( $this, 'allowed_types_admin_settings' ), 10, 1 );
add_filter( 'rtmedia_valid_type_check', array( $this, 'bypass_video_audio' ), 10, 2 );
}
}
}
}
}
add_action( 'init', array( $this, 'handle_callback' ), 20 );
add_action( 'wp_ajax_rt_disable_transcoding', array( $this, 'disable_transcoding' ), 1 );
add_action( 'wp_ajax_rt_enable_transcoding', array( $this, 'enable_transcoding' ), 1 );
add_action( 'add_attachment', array( $this, 'after_upload_pdf' ) );
}
/**
* Send transcoding request and save transcoding job id get in response for uploaded media in WordPress media library.
*
* @since 1.0.0
*
* @param array $wp_metadata Metadata of the attachment.
* @param int $attachment_id ID of attachment.
* @param string $autoformat If true then generating thumbs only else trancode video.
*/
public function wp_media_transcoding( $wp_metadata, $attachment_id, $autoformat = true ) {
if ( empty( $wp_metadata['mime_type'] ) ) {
return $wp_metadata;
}
$already_sent = get_post_meta( $attachment_id, '_rt_transcoding_job_id', true );
if ( ! empty( $already_sent ) ) {
return $wp_metadata;
}
$path = get_attached_file( $attachment_id );
$url = wp_get_attachment_url( $attachment_id );
/**
* FIX WordPress 3.6 METADATA
*/
require_once ABSPATH . 'wp-admin/includes/media.php';
$metadata = $wp_metadata;
$type_arry = explode( '.', $url );
$type = strtolower( $type_arry[ count( $type_arry ) - 1 ] );
$extension = pathinfo( $path, PATHINFO_EXTENSION );
$not_allowed_type = array( 'mp3' );
preg_match( '/video|audio/i', $metadata['mime_type'], $type_array );
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 )
) {
$options_video_thumb = $this->get_thumbnails_required( $attachment_id );
if ( empty( $options_video_thumb ) ) {
$options_video_thumb = 5;
}
$job_type = 'video';
if ( ( ! empty( $type_array ) && ! empty( $type_array[0] ) && 'audio' === $type_array[0] ) || in_array( $extension, explode( ',', $this->audio_extensions ), true ) ) {
$job_type = 'audio';
} elseif ( in_array( $extension, explode( ',', $this->other_extensions ), true ) ) {
$job_type = $extension;
$autoformat = $extension;
$options_video_thumb = 0;
}
/** Figure out who is requesting this job */
$job_for = 'wp-media';
$post_parent = wp_get_post_parent_id( $attachment_id );
if ( 0 !== $post_parent ) {
$post_type = get_post_type( $post_parent );
if ( class_exists( 'RTMediaModel' ) && function_exists( 'rtmedia_id' ) ) {
if ( 'rtmedia_album' === $post_type ) {
$job_for = 'rtmedia';
}
}
}
$args = array(
'method' => 'POST',
'sslverify' => false,
'timeout' => 60, // phpcs:ignore WordPressVIPMinimum.Performance.RemoteRequestTimeout.timeout_timeout
'body' => array(
'api_token' => $this->api_key,
'job_type' => $job_type,
'job_for' => $job_for,
'file_url' => rawurlencode( $url ),
'callback_url' => rawurlencode( trailingslashit( home_url() ) . 'index.php' ),
'force' => 0,
'formats' => ( true === $autoformat ) ? ( ( ! empty( $type_array[0] ) && 'video' === $type_array[0] ) ? 'mp4' : 'mp3' ) : $autoformat,
'thumb_count' => $options_video_thumb,
),
);
$transcoding_url = $this->transcoding_api_url . 'job/';
$upload_page = wp_remote_post( $transcoding_url, $args );
if ( ! is_wp_error( $upload_page ) &&
(
isset( $upload_page['response']['code'] ) &&
200 === intval( $upload_page['response']['code'] )
)
) {
$upload_info = json_decode( $upload_page['body'] );
if ( isset( $upload_info->status ) && $upload_info->status && isset( $upload_info->job_id ) && $upload_info->job_id ) {
$job_id = $upload_info->job_id;
update_post_meta( $attachment_id, '_rt_transcoding_job_id', $job_id );
}
}
}
return $wp_metadata;
}
/**
* Get number of thumbnails required to generate for video.
*
* @since 1.0.0
*
* @param int $attachment_id ID of attachment.
*
* @return int $thumb_count
*/
public function get_thumbnails_required( $attachment_id = '' ) {
$thumb_count = get_option( 'number_of_thumbs' );
/**
* Allow user to filter number of thumbnails required to generate for video.
*
* @since 1.0.0
*
* @param int $thumb_count Number of thumbnails set in setting.
* @param int $attachment_id ID of attachment.
*/
$thumb_count = apply_filters( 'rt_media_total_video_thumbnails', $thumb_count, $attachment_id );
return $thumb_count > 10 ? 10 : $thumb_count;
}
/**
* Check whether uploaded file is valid audio/video file or not.
*
* @since 1.0.0
*
* @param boolean $flag File valid or not.
* @param array $file Media file.
*
* @return boolean
*/
public function bypass_video_audio( $flag, $file ) {
if ( isset( $file['type'] ) ) {
$fileinfo = explode( '/', $file['type'] );
if ( in_array( $fileinfo[0], array( 'audio', 'video' ), true ) ) {
$flag = true;
}
}
return $flag;
}
/**
* Check api key is valid or not.
*
* @since 1.0.0
*
* @param string $key Api Key.
*
* @return boolean $status If true then key is valid else key is not valid.
*/
public function is_valid_key( $key ) {
$validate_url = trailingslashit( $this->store_url ) . 'rt-eddsl-api/?rt-eddsl-license-key=' . $key;
if ( function_exists( 'vip_safe_wp_remote_get' ) ) {
$validation_page = vip_safe_wp_remote_get( $validate_url, '', 3, 3 );
} else {
$validation_page = wp_safe_remote_get( $validate_url ); // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.wp_remote_get_wp_remote_get
}
if ( ! is_wp_error( $validation_page ) ) {
$validation_info = json_decode( $validation_page['body'] );
if ( isset( $validation_info->status ) ) {
$status = $validation_info->status;
}
} else {
$status = false;
}
return $status;
}
/**
* Save usage information.
*
* @since 1.0.0
*
* @param string $key Api key.
*
* @return array $usage_info An array containing usage information.
*/
public function update_usage( $key ) {
$usage_url = trailingslashit( $this->transcoding_api_url ) . 'usage/' . $key;
if ( function_exists( 'vip_safe_wp_remote_get' ) ) {
$usage_page = vip_safe_wp_remote_get( $usage_url, '', 3, 3 );
} else {
$usage_page = wp_safe_remote_get( $usage_url ); // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.wp_remote_get_wp_remote_get
}
if ( ! is_wp_error( $usage_page ) ) {
$usage_info = json_decode( $usage_page['body'] );
} else {
$usage_info = null;
}
update_site_option( 'rt-transcoding-usage', array( $key => $usage_info ) );
return $usage_info;
}
/**
* Send email to admin when trancoding quota near to limit.
*
* @since 1.0.0
*
* @param array $usage_details Usage informataion.
*/
public function nearing_usage_limit( $usage_details ) {
if ( defined( 'RT_TRANSCODER_NO_MAIL' ) ) {
return;
}
$subject = esc_html__( 'Transcoding: Nearing quota limit.', 'transcoder' );
$message = '<p>' . esc_html__( 'You are nearing the quota limit for your transcoding service.', 'transcoder' ) . '</p><p>' . esc_html__( 'Following are the details:', 'transcoder' ) . '</p><p><strong>Used:</strong> %s</p><p><strong>' . esc_html__( 'Remaining', 'transcoder' ) . '</strong>: %s</p><p><strong>' . esc_html__( 'Total:', 'transcoder' ) . '</strong> %s</p>';
$users = get_users( array( 'role' => 'administrator' ) );
if ( $users ) {
$admin_email_ids = array();
foreach ( $users as $user ) {
$admin_email_ids[] = $user->user_email;
}
add_filter( 'wp_mail_content_type', array( $this, 'wp_mail_content_type' ) );
wp_mail( $admin_email_ids, $subject, sprintf( $message, size_format( $usage_details[ $this->api_key ]->used, 2 ), size_format( $usage_details[ $this->api_key ]->remaining, 2 ), size_format( $usage_details[ $this->api_key ]->total, 2 ) ) ); // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.wp_mail_wp_mail
remove_filter( 'wp_mail_content_type', array( $this, 'wp_mail_content_type' ) );
}
update_site_option( 'rt-transcoding-usage-limit-mail', 1 );
}
/**
* Send email to admin when trancoding quota is over.
*
* @since 1.0.0
*/
public function usage_quota_over() {
if ( defined( 'RT_TRANSCODER_NO_MAIL' ) ) {
return;
}
$usage_details = get_site_option( 'rt-transcoding-usage' );
if ( ! $usage_details[ $this->api_key ]->remaining ) {
$subject = esc_html__( 'Transcoding: Usage quota over.', 'transcoder' );
$message = '<p>' . esc_html__( 'Your usage quota is over. Upgrade your plan', 'transcoder' ) . '</p><p>' . esc_html__( 'Following are the details:', 'transcoder' ) . '</p><p><strong>' . esc_html__( 'Used:', 'transcoder' ) . '</strong> %s</p><p><strong>' . esc_html__( 'Remaining', 'transcoder' ) . '</strong>: %s</p><p><strong>' . esc_html__( 'Total:', 'transcoder' ) . '</strong> %s</p>';
$users = get_users( array( 'role' => 'administrator' ) );
if ( $users ) {
$admin_email_ids = array();
foreach ( $users as $user ) {
$admin_email_ids[] = $user->user_email;
}
add_filter( 'wp_mail_content_type', array( $this, 'wp_mail_content_type' ) );
wp_mail( $admin_email_ids, $subject, sprintf( $message, size_format( $usage_details[ $this->api_key ]->used, 2 ), 0, size_format( $usage_details[ $this->api_key ]->total, 2 ) ) ); // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.wp_mail_wp_mail
remove_filter( 'wp_mail_content_type', array( $this, 'wp_mail_content_type' ) );
}
update_site_option( 'rt-transcoding-usage-limit-mail', 1 );
}
}
/**
* Check whether key is valid or not and save api key.
*
* @since 1.0.0
*/
public function save_api_key() {
$is_api_key_updated = transcoder_filter_input( INPUT_GET, 'api-key-updated', FILTER_SANITIZE_FULL_SPECIAL_CHARS );
$is_invalid_license_key = transcoder_filter_input( INPUT_GET, 'invalid-license-key', FILTER_SANITIZE_FULL_SPECIAL_CHARS );
$is_localhost = transcoder_filter_input( INPUT_GET, 'need-public-host', FILTER_SANITIZE_FULL_SPECIAL_CHARS );
if ( $is_api_key_updated ) {
if ( is_multisite() ) {
add_action( 'network_admin_notices', array( $this, 'successfully_subscribed_notice' ) );
}
add_action( 'admin_notices', array( $this, 'successfully_subscribed_notice' ) );
} elseif ( $is_invalid_license_key ) {
if ( is_multisite() ) {
add_action( 'network_admin_notices', array( $this, 'invalid_license_notice' ) );
}
add_action( 'admin_notices', array( $this, 'invalid_license_notice' ) );
} elseif ( $is_localhost ) {
if ( is_multisite() ) {
add_action( 'network_admin_notices', array( $this, 'public_host_needed_notice' ) );
}
add_action( 'admin_notices', array( $this, 'public_host_needed_notice' ) );
}
$filtered_apikey = transcoder_filter_input( INPUT_GET, 'apikey', FILTER_SANITIZE_FULL_SPECIAL_CHARS );
$apikey = ! empty( $filtered_apikey ) ? trim( $filtered_apikey ) : '';
$page = transcoder_filter_input( INPUT_GET, 'page', FILTER_SANITIZE_FULL_SPECIAL_CHARS );
if ( ! empty( $apikey ) && is_admin() && ! empty( $page ) && ( 'rt-transcoder' === $page ) ) {
/* Do not activate transcoding service on localhost */
$blacklist = rtt_get_blacklist_ip_addresses();
$remote_addr = rtt_get_remote_ip_address();
if ( in_array( wp_unslash( $remote_addr ), $blacklist, true ) ) {
$return_page = add_query_arg(
array(
'page' => 'rt-transcoder',
'need-public-host' => '1',
),
admin_url( 'admin.php' )
);
wp_safe_redirect( esc_url_raw( $return_page ) );
die();
}
if ( $this->is_valid_key( $apikey ) ) {
update_site_option( 'rt-transcoding-api-key', $apikey );
update_site_option( 'rt-transcoding-api-key-stored', $apikey );
$usage_info = $this->update_usage( $apikey );
$return_page = add_query_arg(
array(
'page' => 'rt-transcoder',
'api-key-updated' => $usage_info->plan->name ? ucfirst( strtolower( $usage_info->plan->name ) ) : 'Free',
),
admin_url( 'admin.php' )
);
wp_safe_redirect( esc_url_raw( $return_page ) );
die();
} else {
$return_page = add_query_arg(
array(
'page' => 'rt-transcoder',
'invalid-license-key' => '1',
),
admin_url( 'admin.php' )
);
wp_safe_redirect( esc_url_raw( $return_page ) );
die();
}
}
}
/**
* Allow user to upload other types media files.
*
* @since 1.0.0
*
* @param array $types Mime types.
*
* @return array $types Mime types.
*/
public function allowed_types( $types ) {
if ( isset( $types[0] ) && isset( $types[0]['extensions'] ) ) {
if ( is_rtmedia_upload_video_enabled() && strpos( $this->video_extensions, $types[0]['extensions'] ) ) {
$types[0]['extensions'] .= $this->video_extensions; // Allow all types of video file to be uploded.
}
if ( is_rtmedia_upload_music_enabled() && strpos( $this->audio_extensions, $types[0]['extensions'] ) ) {
$types[0]['extensions'] .= $this->audio_extensions; // Allow all types of music file to be uploded.
}
}
return $types;
}
/**
* Allow user to upload other types media files.
*
* @since 1.0.0
*
* @param array $types Mime types.
*
* @return array Mime types.
*/
public function allowed_types_admin_settings( $types ) {
$allowed_video_string = implode( ',', $types['video']['extn'] );
$allowed_audio_string = implode( ',', $types['music']['extn'] );
$allowed_video = explode( ',', $allowed_video_string . $this->video_extensions );
$allowed_audio = explode( ',', $allowed_audio_string . $this->audio_extensions );
$types['video']['extn'] = array_unique( $allowed_video );
$types['music']['extn'] = array_unique( $allowed_audio );
return $types;
}
/**
* Display message when user subscribed successfully.
*
* @since 1.0.0
*/
public function successfully_subscribed_notice() {
?>
<div class="updated">
<p>
<?php
$api_key_updated = transcoder_filter_input( INPUT_GET, 'api-key-updated', FILTER_SANITIZE_FULL_SPECIAL_CHARS );
printf(
wp_kses(
__( 'You have successfully subscribed.', 'transcoder' ),
array(
'strong' => array(),
)
),
esc_html( sanitize_text_field( wp_unslash( $api_key_updated ) ) )
);
?>
</p>
</div>
<?php
}
/**
* Display message when license key is not valid.
*
* @since 1.0.0
*/
public function invalid_license_notice() {
?>
<div class="error">
<p>
<?php esc_html_e( 'This license key is invalid.', 'transcoder' ); ?>
</p>
</div>
<?php
}
/**
* Display message when user tries to activate license key on localhost.
*
* @since 1.0.6
*/
public function public_host_needed_notice() {
?>
<div class="error">
<p>
<?php esc_html_e( 'Transcoding service can not be activated on the localhost', 'transcoder' ); ?>
</p>
</div>
<?php
}
/**
* Display usage widget in sidebar on rtmedia transcoder settings page.
*
* @since 1.0.0
*/
public function usage_widget() {
$usage_details = get_site_option( 'rt-transcoding-usage' );
$content = '';
$api_key = '';
if ( ! empty( $this->api_key ) ) {
$api_key = $this->api_key;
} elseif ( ! empty( $this->stored_api_key ) ) {
$api_key = $this->stored_api_key;
}
if ( ! empty( $api_key ) ) {
if ( $usage_details && isset( $usage_details[ $api_key ]->status ) && $usage_details[ $api_key ]->status && 'error' !== $usage_details[ $api_key ]->status ) {
if ( isset( $usage_details[ $api_key ]->plan->name ) ) {
$plan_name = strtolower( $usage_details[ $api_key ]->plan->name );
$content .= '<p><strong>' . esc_html__( 'Current Plan', 'transcoder' ) . ':</strong> ' . esc_html( ucfirst( $plan_name ) ) . ( $usage_details[ $api_key ]->sub_status ? '' : ' (' . esc_html__( 'Unsubscribed', 'transcoder' ) . ')' ) . '</p>';
} else {
$plan_name = '';
}
if ( isset( $usage_details[ $api_key ]->plan->expires ) && 'free' !== $plan_name ) {
$content .= '<p><strong>' . esc_html__( 'Expires On', 'transcoder' ) . ':</strong> ' . date_i18n( 'F j, Y', strtotime( $usage_details[ $api_key ]->plan->expires ) ) . '</p>';
}
if ( isset( $usage_details[ $api_key ]->used ) ) {
$used_size = size_format( $usage_details[ $api_key ]->used, 2 );
$content .= '<p><span class="transcoding-used"></span><strong>' . esc_html__( 'Used', 'transcoder' ) . ':</strong> ' . ( ( ! empty( $used_size ) ) ? esc_html( $used_size ) : '0MB' ) . '</p>';
}
if ( isset( $usage_details[ $api_key ]->remaining ) ) {
$content .= '<p><span class="transcoding-remaining"></span><strong>' . esc_html__( 'Remaining', 'transcoder' ) . ':</strong> ';
if ( $usage_details[ $api_key ]->remaining >= 0 ) {
$content .= size_format( $usage_details[ $api_key ]->remaining, 2 );
} else {
$content .= $usage_details[ $api_key ]->remaining . '0MB';
}
}
if ( isset( $usage_details[ $api_key ]->total ) ) {
$content .= '<p><strong>' . esc_html__( 'Total', 'transcoder' ) . ':</strong> ';
if ( $usage_details[ $api_key ]->total >= 0 ) {
$content .= size_format( $usage_details[ $api_key ]->total, 2 );
} elseif ( $usage_details[ $api_key ]->total <= -1 ) {
$content .= 'Unlimited';
} else {
$content .= '';
}
}
$usage = new RT_Progress();
if ( empty( $usage_details[ $api_key ]->used ) ) {
$usage_details[ $api_key ]->used = 0;
}
if ( empty( $usage_details[ $api_key ]->total ) ) {
$usage_details[ $api_key ]->total = 0;
}
if ( ! isset( $usage_details[ $api_key ]->remaining ) ) {
$usage_details[ $api_key ]->remaining = 0;
}
$content .= $usage->progress_ui( $usage->progress( $usage_details[ $api_key ]->used, $usage_details[ $api_key ]->total ), false );
$content .= '<p>' . esc_html__( 'Usage will automatically reset at the end of every month.', 'transcoder' ) . '</p>';
if ( 'free' === $plan_name ) {
$content .= '<p>' . esc_html__( 'Upgrade for more bandwidth.', 'transcoder' ) . '</p>';
}
if ( ( 0 >= $usage_details[ $api_key ]->remaining ) ) {
$content .= '<div class="error below-h2"><p>' . esc_html__( 'Your usage limit has been reached. Upgrade your plan.', 'transcoder' ) . '</p></div>';
}
if ( ( isset( $usage_details[ $api_key ]->plan->expires ) && strtotime( $usage_details[ $api_key ]->plan->expires ) < time() ) ) {
$content .= '<div class="error below-h2"><p>' . esc_html__( 'Your plan has expired. Please consider upgrading if you need more bandwidth.', 'transcoder' ) . '</p></div>';
}
} else {
$content .= '<div class="error below-h2"><p>' . esc_html__( 'Your API key is not valid or is expired.', 'transcoder' ) . '</p></div>';
}
} else {
$content .= '<p>' . esc_html__( 'Currently, You are not subscribed to transcoding service. Please subscribe.', 'transcoder' ) . '</p>';
}
?>
<div class="postbox" id="transcoder-usage">
<h3 class="hndle">
<span>
<?php esc_html_e( 'Transcoding usage this month', 'transcoder' ); ?>
</span>
</h3>
<div class="inside">
<?php
// Already escaped variable.
echo $content; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
?>
</div>
</div>
<?php
}
/**
* Save thumbnails for transcoded video.
*
* @since 1.0.0
*
* @param array $post_array Attachment data.
*
* @return string
*/
public function add_media_thumbnails( $post_array ) {
$defaults = array(
'post_id' => '',
'job_for' => '',
);
// Parse incoming $post_array into an array and merge it with $defaults.
$post_array = wp_parse_args( $post_array, $defaults );
do_action( 'rtt_before_thumbnail_store', $post_array['post_id'], $post_array );
$post_id = $post_array['post_id'];
$post_thumbs = $post_array;
$post_thumbs_array = maybe_unserialize( $post_thumbs );
$largest_thumb_size = 0;
if ( 'rtmedia' === $post_thumbs_array['job_for'] && class_exists( 'RTMediaModel' ) ) {
$model = new RTMediaModel();
$media = $model->get( array( 'media_id' => $post_id ) );
$media_id = $media[0]->id;
$this->media_author = $media[0]->media_author;
$this->uploaded['context'] = $media[0]->context;
$this->uploaded['context_id'] = $media[0]->context_id;
$this->uploaded['media_author'] = $media[0]->media_author;
}
$largest_thumb = false;
$largest_thumb_url = false;
$upload_thumbnail_array = array();
$failed_thumbnails = false;
foreach ( $post_thumbs_array['thumbnail'] as $thumbnail ) {
$thumbresource = function_exists( 'vip_safe_wp_remote_get' ) ? vip_safe_wp_remote_get( $thumbnail, '', 3, 3 ) : wp_remote_get( $thumbnail, array( 'timeout' => 120 ) ); // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.wp_remote_get_wp_remote_get, WordPressVIPMinimum.Performance.RemoteRequestTimeout.timeout_timeout
$thumbinfo = pathinfo( $thumbnail );
$temp_name = $thumbinfo['basename'];
$temp_name = urldecode( $temp_name );
$temp_name_array = explode( '/', $temp_name );
$thumbinfo['basename'] = $temp_name_array[ count( $temp_name_array ) - 1 ];
/**
* Filter: 'transcoded_temp_filename' - Allows changes for the thumbnail name.
*
* @deprecated 1.3.2. Use the {@see 'transcoded_thumb_filename'} filter instead.
*/
$thumbinfo['basename'] = apply_filters_deprecated( 'transcoded_temp_filename', array( $thumbinfo['basename'] ), '1.3.2', 'transcoded_thumb_filename', __( 'Use transcoded_thumb_filename filter to modify video thumbnail name and transcoded_video_filename filter to modify video file name.', 'transcoder' ) );
/**
* Allows users/plugins to filter the thumbnail Name
*
* @since 1.3.2
*
* @param string $temp_name Contains the thumbnail public name
*/
$thumbinfo['basename'] = apply_filters( 'transcoded_thumb_filename', $thumbinfo['basename'] );
// Verify Extension.
if ( empty( pathinfo( $thumbinfo['basename'], PATHINFO_EXTENSION ) ) ) {
$thumbinfo['basename'] .= '.' . $thumbinfo['extension'];
}
if ( 'wp-media' !== $post_thumbs_array['job_for'] ) {
add_filter( 'upload_dir', array( $this, 'upload_dir' ) );
}
// Create a file in the upload folder with given content.
$thumb_upload_info = wp_upload_bits( $thumbinfo['basename'], null, $thumbresource['body'] );
// Check error.
if ( ! empty( $thumb_upload_info['error'] ) ) {
$failed_thumbnails = $thumb_upload_info;
}
/**
* Allow users to filter/perform action on uploaded transcoded file.
*
* @since 1.0.5
*
* @param array $thumb_upload_info Array contains the uploaded file url and Path
* i.e $thumb_upload_info['url'] contains the file URL
* and $thumb_upload_info['file'] contains the file physical path
* @param int $post_id Contains the attachment ID for which transcoded file is uploaded
*/
$thumb_upload_info = apply_filters( 'transcoded_file_stored', $thumb_upload_info, $post_id );
if ( 'wp-media' !== $post_thumbs_array['job_for'] ) {
remove_filter( 'upload_dir', array( $this, 'upload_dir' ) );
}
$file = _wp_relative_upload_path( $thumb_upload_info['file'] );
/**
* Allows users/plugins to filter the file URL
*
* @since 1.0.5
*
* @param string $thumb_upload_info['url'] Contains the file public URL
* @param int $post_id Contains the attachment ID for which transcoded file has been uploaded
*/
$thumb_upload_info['url'] = apply_filters( 'transcoded_file_url', $thumb_upload_info['url'], $post_id );
if ( $file ) {
$upload_thumbnail_array[] = $file;
}
$current_thumb_size = filesize( $thumb_upload_info['file'] );
if ( $current_thumb_size >= $largest_thumb_size ) {
$largest_thumb_size = $current_thumb_size;
$largest_thumb = $thumb_upload_info['url']; // Absolute URL of the thumb.
$largest_thumb_url = $file ? $file : ''; // Relative URL of the thumb.
}
}
if ( false !== $failed_thumbnails && ! empty( $failed_thumbnails['error'] ) ) {
$this->nofity_transcoding_failed( $post_array['job_id'], sprintf( 'Failed saving of Thumbnail for %1$s.', $post_array['file_name'] ) );
}
update_post_meta( $post_id, '_rt_media_source', $post_thumbs_array['job_for'] );
update_post_meta( $post_id, '_rt_media_thumbnails', $upload_thumbnail_array );
do_action( 'transcoded_thumbnails_added', $post_id );
if ( $largest_thumb_url ) {
$is_retranscoding_job = get_post_meta( $post_id, '_rt_retranscoding_sent', true );
if ( ! $is_retranscoding_job || rtt_is_override_thumbnail() ) {
update_post_meta( $post_id, '_rt_media_video_thumbnail', $largest_thumb_url );
if ( 'rtmedia' === $post_thumbs_array['job_for'] && class_exists( 'RTMediaModel' ) ) {
$model->update( array( 'cover_art' => $largest_thumb ), array( 'media_id' => $post_id ) );
update_activity_after_thumb_set( $media_id );
}
}
/**
* Allow users/plugins to access the thumbnail file which is got stored as a thumbnail
*
* @since 1.0.7
*
* @param string $largest_thumb Absolute URL of the thumbnail
* @param int $post_id Attachment ID of the video for which thumbnail has been set
*/
do_action( 'transcoded_thumb_added', $largest_thumb, $post_id );
}
return $largest_thumb_url;
}
/**
* Save transcoded media files.
*
* @since 1.0.0
*
* @param array $file_post_array Transcoded files.
* @param int $attachment_id ID of attachment.
* @param string $job_for Whether media uploaded through rtmedia plugin or WordPress media.
*/
public function add_transcoded_files( $file_post_array, $attachment_id, $job_for = '' ) {
$transcoded_files = false;
$mail = true;
if ( defined( 'RT_TRANSCODER_NO_MAIL' ) ) {
$mail = false;
}
global $wpdb;
do_action( 'rtt_before_transcoded_media_store', $attachment_id, $file_post_array );
if ( isset( $file_post_array ) && is_array( $file_post_array ) && ( count( $file_post_array ) > 0 ) ) {
foreach ( $file_post_array as $key => $format ) {
if ( is_array( $format ) && ( count( $format ) > 0 ) ) {
foreach ( $format as $file ) {
$flag = false;
if ( isset( $file ) ) {
if ( 'rtmedia' === $job_for ) {
$model = new RTMediaModel();
$media = $model->get_media( array( 'media_id' => $attachment_id ), 0, 1 );
$this->media_author = $media[0]->media_author;
$this->uploaded['context'] = $media[0]->context;
$this->uploaded['context_id'] = $media[0]->context_id;
$this->uploaded['media_author'] = $media[0]->media_author;
}
$download_url = urldecode( urldecode( $file ) );
$new_wp_attached_file_pathinfo = pathinfo( $download_url );
$post_mime_type = 'mp4' === $new_wp_attached_file_pathinfo['extension'] ? 'video/mp4' : 'audio/mp3';
$attachemnt_url = wp_get_attachment_url( $attachment_id );
$timeout = 5;
if ( 'video/mp4' === $post_mime_type ) {
$timeout = 120;
}
try {
$response = function_exists( 'vip_safe_wp_remote_get' ) ? vip_safe_wp_remote_get( $download_url, '', 3, 3 ) : wp_remote_get( $download_url, array( 'timeout' => $timeout ) ); // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.wp_remote_get_wp_remote_get
} catch ( Exception $e ) {
$flag = $e->getMessage();
}
$file_content = wp_remote_retrieve_body( $response );
if ( ! empty( $file_content ) ) {
if ( 'wp-media' !== $job_for ) {
add_filter( 'upload_dir', array( $this, 'upload_dir' ) );
}
/**
* Allows users/plugins to filter the transcoded file Name
*
* @since 1.3.2
*
* @param string $new_wp_attached_file_pathinfo['basename'] Contains the file public name
*/
$file_name = apply_filters( 'transcoded_video_filename', $new_wp_attached_file_pathinfo['basename'] );
// Verify Extension.
if ( empty( pathinfo( $file_name, PATHINFO_EXTENSION ) ) ) {
$file_name .= '.' . $new_wp_attached_file_pathinfo['extension'];
}
$upload_info = wp_upload_bits( $file_name, null, $file_content );
/**
* Allow users to filter/perform action on uploaded transcoded file.
*
* @since 1.0.5
*
* @param array $upload_info Array contains the uploaded file url and Path
* i.e $upload_info['url'] contains the file URL
* and $upload_info['file'] contains the file physical path
* @param int $attachment_id Contains the attachment ID for which transcoded file is uploaded
*/
$upload_info = apply_filters( 'transcoded_file_stored', $upload_info, $attachment_id );
if ( 'wp-media' !== $job_for ) {
remove_filter( 'upload_dir', array( $this, 'upload_dir' ) );
}
$uploaded_file = _wp_relative_upload_path( $upload_info['file'] );
if ( ! empty( $uploaded_file ) ) {
$transcoded_files[ $key ][] = $uploaded_file;
update_post_meta( $attachment_id, '_wp_attached_file', $uploaded_file );
}
} else {
$flag = esc_html__( 'Could not read file.', 'transcoder' );
if ( $flag && $mail ) {
$download_link = esc_url(
add_query_arg(