This repository was archived by the owner on Aug 22, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathrt-transcoder-admin.php
More file actions
executable file
·569 lines (497 loc) · 20 KB
/
rt-transcoder-admin.php
File metadata and controls
executable file
·569 lines (497 loc) · 20 KB
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
<?php
/**
* The admin-specific functionality of the plugin.
*
* @since 1.0.0
*
* @package Transcoder
* @subpackage Transcoder/Admin
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* The admin-specific functionality of the plugin.
*
* @since 1.0.0
*
* @package Transcoder
* @subpackage Transcoder/Admin
*/
class RT_Transcoder_Admin {
/**
* The object of RT_Transcoder_Handler class.
*
* @since 1.0.0
* @access private
* @var object $transcoder_handler The object of RT_Transcoder_Handler class.
*/
private $transcoder_handler;
/**
* The api key of transcoding service subscription.
*
* @since 1.0.0
* @access private
* @var string $api_key The api key of transcoding service subscription.
*/
private $api_key = false;
/**
* The api key of transcoding service subscription.
*
* @since 1.0.0
* @access private
* @var string $stored_api_key The api key of transcoding service subscription.
*/
private $stored_api_key = false;
/**
* Initialize the class and set its properties.
*
* @since 1.0.0
*/
public function __construct() {
$this->api_key = get_site_option( 'rt-transcoding-api-key' );
$this->stored_api_key = get_site_option( 'rt-transcoding-api-key-stored' );
$this->load_translation();
if ( ! class_exists( 'RT_Progress' ) ) {
include_once RT_TRANSCODER_PATH . 'admin/rt-transcoder-progressbar.php'; // phpcs:ignore WordPressVIPMinimum.Files.IncludingFile.UsingCustomConstant
}
include_once RT_TRANSCODER_PATH . 'admin/rt-transcoder-handler.php'; // phpcs:ignore WordPressVIPMinimum.Files.IncludingFile.UsingCustomConstant
include_once RT_TRANSCODER_PATH . 'admin/rt-transcoder-actions.php'; // phpcs:ignore WordPressVIPMinimum.Files.IncludingFile.UsingCustomConstant
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts_styles' ) );
add_filter( 'attachment_fields_to_edit', array( $this, 'edit_video_thumbnail' ), 11, 2 );
add_filter( 'attachment_fields_to_save', array( $this, 'save_video_thumbnail' ), 11, 1 );
add_action( 'admin_notices', array( $this, 'add_settings_errors' ) );
$this->transcoder_handler = new RT_Transcoder_Handler();
if ( is_admin() ) {
add_action( 'admin_menu', array( $this, 'menu' ) );
add_action( 'admin_init', array( $this, 'register_transcoder_settings' ) );
if ( class_exists( 'RTMediaEncoding' ) ) {
$old_rtmedia_encoding_key = get_site_option( 'rtmedia-encoding-api-key' );
if ( ! empty( $old_rtmedia_encoding_key ) ) {
update_site_option( 'rtmedia-encoding-api-key', '' );
}
add_action( 'init', array( $this, 'disable_encoding' ) );
}
if ( is_multisite() ) {
add_action( 'network_admin_notices', array( $this, 'subscribe_transcoder_admin_notice' ) );
add_action( 'network_admin_notices', array( $this, 'install_godam_admin_notice' ) );
add_action( 'network_admin_enqueue_scripts', array( $this, 'enqueue_thickbox_on_transcoder_settings' ) );
}
add_action( 'admin_notices', array( $this, 'subscribe_transcoder_admin_notice' ) );
add_action( 'admin_notices', array( $this, 'install_godam_admin_notice' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_thickbox_on_transcoder_settings' ) );
}
if ( class_exists( 'RTMedia' ) ) {
if ( ! function_exists( 'get_plugin_data' ) ) {
include_once ABSPATH . 'wp-admin/includes/plugin.php';
}
$rtmedia_plugin_info = get_plugin_data( RTMEDIA_PATH . 'index.php' );
// Show admin notice when Transcoder pluign active and user using rtMedia version 4.0.7.
if ( version_compare( $rtmedia_plugin_info['Version'], '4.0.7', '<=' ) ) {
if ( is_multisite() ) {
add_action( 'network_admin_notices', array( $this, 'transcoder_admin_notice' ) );
}
add_action( 'admin_notices', array( $this, 'transcoder_admin_notice' ) );
add_action( 'wp_ajax_transcoder_hide_admin_notice', array( $this, 'transcoder_hide_admin_notice' ) );
}
add_action( 'admin_head', array( $this, 'rtmedia_hide_encoding_tab' ) );
add_filter( 'wp_mediaelement_fallback', array( $this, 'mediaelement_add_class' ), 20, 2 );
}
}
/**
* Display errors if any while settings are save.
*/
public function add_settings_errors() {
settings_errors( 'rt-transcoder-settings-group' );
}
/**
* Create menu.
*
* @since 1.0.0
*/
public function menu() {
add_menu_page( 'Transcoder', 'Transcoder', 'manage_options', 'rt-transcoder', array( $this, 'settings_page' ), RT_TRANSCODER_URL . 'admin/images/menu-icon.png', '40.2222' );
}
/**
* Register transcoder settings.
*
* @since 1.0.0
*/
public function register_transcoder_settings() {
register_setting( 'rt-transcoder-settings-group', 'number_of_thumbs' );
register_setting( 'rt-transcoder-settings-group', 'rtt_override_thumbnail' );
register_setting( 'rt-transcoder-settings-group', 'rtt_client_check_status_button' );
}
/**
* Display settings page.
*
* @since 1.0.0
*/
public function settings_page() {
include_once RT_TRANSCODER_PATH . 'admin/partials/rt-transcoder-admin-display.php'; // phpcs:ignore WordPressVIPMinimum.Files.IncludingFile.UsingCustomConstant
}
/**
* Load language translation.
*
* @since 1.0.0
*/
public function load_translation() {
load_plugin_textdomain( 'transcoder', false, basename( RT_TRANSCODER_PATH ) . '/languages/' );
}
/**
* Remove actions and filters from old rtMedia (v4.0.2) plugin.
*
* @since 1.0.0
*/
public function disable_encoding() {
global $rtmedia_admin;
$rtmedia_encoding = $rtmedia_admin->rtmedia_encoding;
if ( ! empty( $rtmedia_admin ) ) {
remove_filter( 'media_row_actions', array( $rtmedia_admin, 'add_reencode_link' ) );
remove_action( 'admin_head-upload.php', array( $rtmedia_admin, 'add_bulk_actions_regenerate' ) );
}
if ( isset( $rtmedia_admin->rtmedia_encoding ) ) {
$rtmedia_encoding = $rtmedia_admin->rtmedia_encoding;
remove_action( 'rtmedia_after_add_media', array( $rtmedia_encoding, 'encoding' ) );
remove_action( 'rtmedia_before_default_admin_widgets', array( $rtmedia_encoding, 'usage_widget' ) );
remove_action( 'admin_init', array( $rtmedia_encoding, 'save_api_key' ), 1 );
}
}
/**
* Load styles and scripts
*
* @since 1.0.0
*/
public function enqueue_scripts_styles() {
global $pagenow;
$page = transcoder_filter_input( INPUT_GET, 'page', FILTER_SANITIZE_FULL_SPECIAL_CHARS );
if ( 'admin.php' !== $pagenow || 'rt-transcoder' !== $page ) {
return;
}
$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
wp_enqueue_style( 'rt-transcoder-admin-css', RT_TRANSCODER_URL . 'admin/css/rt-transcoder-admin' . $suffix . '.css', array(), RT_TRANSCODER_VERSION );
wp_register_script( 'rt-transcoder-main', RT_TRANSCODER_URL . 'admin/js/rt-transcoder-admin' . $suffix . '.js', array( 'jquery' ), RT_TRANSCODER_VERSION, true );
$localize_script_data = array(
'admin_url' => esc_url( admin_url() ),
'loader_image' => esc_url( admin_url( 'images/loading.gif' ) ),
'disable_encoding' => esc_html__( 'Are you sure you want to disable the transcoding service?', 'transcoder' ),
'enable_encoding' => esc_html__( 'Are you sure you want to enable the transcoding service?', 'transcoder' ),
'something_went_wrong' => esc_html__( 'Something went wrong. Please ', 'transcoder' ) . '<a href onclick="location.reload();">' . esc_html__( 'refresh', 'transcoder' ) . '</a>' . esc_html__( ' page.', 'transcoder' ),
'error_empty_key' => esc_html__( 'Please enter the license key.', 'transcoder' ),
'security_nonce_for_enabling_encoding' => wp_create_nonce( 'rt_enable_transcoding' ),
'security_nonce_for_disabling_encoding' => wp_create_nonce( 'rt_disable_transcoding' ),
);
wp_localize_script( 'rt-transcoder-main', 'rt_transcoder_script', $localize_script_data );
wp_enqueue_script( 'rt-transcoder-main' );
}
/**
* Create subscription form for various subscription plans.
*
* @since 1.0.0
*
* @param string $name The name of subscription plan.
* @param float $price The price of subscription plan.
* @param bool $force If true then it always show subscriobe form.
* @return string
*/
public function transcoding_subscription_button( $name = 'No Name', $price = '0', $force = false ) {
if ( $this->api_key ) {
$this->transcoder_handler->update_usage( $this->api_key );
}
$usage_details = get_site_option( 'rt-transcoding-usage' );
if ( isset( $usage_details[ $this->api_key ]->plan->name ) && ( strtolower( $usage_details[ $this->api_key ]->plan->name ) === strtolower( $name ) ) && $usage_details[ $this->api_key ]->sub_status && ! $force ) {
$form = '<button disabled="disabled" type="submit" class="button button-primary bpm-unsubscribe">' . esc_html__( 'Current Plan', 'transcoder' ) . '</button>';
} else {
$plan_name = 'free' === $name ? 'Try Now' : 'Subscribe';
$form = '<a href="https://rtmedia.io/?transcoding-plan=' . $name . '" target="_blank" class="button button-primary">
' . esc_html( $plan_name ) . '
</a>';
}
return $form;
}
/**
* Display all video thumbnails on attachment edit page.
*
* @since 1.0.0
*
* @param array $form_fields An array of attachment form fields.
* @param WP_Post $post The WP_Post attachment object.
* @return array $form_fields
*/
public function edit_video_thumbnail( $form_fields, $post ) {
if ( isset( $post->post_mime_type ) ) {
$media_type = explode( '/', $post->post_mime_type );
if ( is_array( $media_type ) && 'video' === $media_type[0] ) {
$media_id = $post->ID;
$thumbnail_array = get_post_meta( $media_id, '_rt_media_thumbnails', true );
if ( empty( $thumbnail_array ) ) {
$thumbnail_array = get_post_meta( $media_id, 'rtmedia_media_thumbnails', true );
}
$wp_video_thumbnail = get_post_meta( $media_id, '_rt_media_video_thumbnail', true );
$video_thumb_html = '';
if ( is_array( $thumbnail_array ) ) {
$video_thumb_html .= '<ul> ';
/* for WordPress backward compatibility */
if ( function_exists( 'wp_get_upload_dir' ) ) {
$uploads = wp_get_upload_dir();
} else {
$uploads = wp_upload_dir();
}
foreach ( $thumbnail_array as $key => $thumbnail_src ) {
$checked = false;
$thumbnail_src_og = $thumbnail_src;
if ( $wp_video_thumbnail === $thumbnail_src ) {
$checked = 'checked=checked';
}
$file_url = $thumbnail_src;
if ( 0 === strpos( $file_url, $uploads['baseurl'] ) ) {
$thumbnail_src = $file_url;
} else {
$thumbnail_src = $uploads['baseurl'] . '/' . $file_url;
}
$thumbnail_src = apply_filters( 'transcoded_file_url', $thumbnail_src, $media_id );
$count = $key + 1;
$video_thumb_html .= '<li style="width: 150px;display: inline-block;"> ' .
'<label for="rtmedia-upload-select-thumbnail-' . esc_attr( $count ) . '"> ' .
'<input type="radio" ' . esc_attr( $checked ) . ' id="rtmedia-upload-select-thumbnail-' . esc_attr( $count ) . '" value="' . esc_attr( $thumbnail_src_og ) . '" name="rtmedia-thumbnail" /> ' .
'<img src=" ' . esc_url( $thumbnail_src ) . '" style="max-height: 120px;max-width: 120px; vertical-align: middle;" /> ' .
'</label></li>';
}
$video_thumb_html .= '</ul>';
$form_fields['rtmedia_video_thumbnail'] = array(
'label' => 'Video Thumbnails',
'input' => 'html',
'html' => $video_thumb_html,
);
}
}
}
return $form_fields;
}
/**
* Display all video thumbnails on attachment edit page.
*
* @since 1.0.0
*
* @param array $form_fields An array of attachment form fields.
* @param WP_Post $post The WP_Post attachment object.
* @return array $form_fields
*/
public function edit_video_thumbnail_( $form_fields, $post ) {
if ( isset( $post->post_mime_type ) ) {
$media_type = explode( '/', $post->post_mime_type );
if ( is_array( $media_type ) && 'video' === $media_type[0] ) {
$media_id = $post->ID;
$thumbnail_array = get_post_meta( $media_id, '_rt_media_thumbnails', true );
if ( empty( $thumbnail_array ) ) {
$thumbnail_array = get_post_meta( $media_id, 'rtmedia_media_thumbnails', true );
}
$rtmedia_model = new RTMediaModel();
$rtmedia_media = $rtmedia_model->get( array( 'media_id' => $media_id ) );
$video_thumb_html = '';
if ( is_array( $thumbnail_array ) ) {
/* for WordPress backward compatibility */
if ( function_exists( 'wp_get_upload_dir' ) ) {
$uploads = wp_get_upload_dir();
} else {
$uploads = wp_upload_dir();
}
$base_url = $uploads['baseurl'];
$video_thumb_html .= '<ul> ';
foreach ( $thumbnail_array as $key => $thumbnail_src ) {
$checked = checked( $thumbnail_src, $rtmedia_media[0]->cover_art, false );
$count = $key + 1;
$final_file_url = $base_url . '/' . $thumbnail_src;
$final_file_url = apply_filters( 'transcoded_file_url', $final_file_url, $media_id );
$video_thumb_html .= '<li style="width: 150px;display: inline-block;">
<label for="rtmedia-upload-select-thumbnail-' . esc_attr( $count ) . '">
<input type="radio" ' . esc_attr( $checked ) . ' id="rtmedia-upload-select-thumbnail-' . esc_attr( $count ) . '" value="' . esc_attr( $thumbnail_src ) . '" name="rtmedia-thumbnail" />
<img src=" ' . esc_url( $final_file_url ) . '" style="max-height: 120px;max-width: 120px; vertical-align: middle;" />
</label></li> ';
}
$video_thumb_html .= ' </ul>';
$form_fields['rtmedia_video_thumbnail'] = array(
'label' => 'Video Thumbnails',
'input' => 'html',
'html' => $video_thumb_html,
);
}
}
}
return $form_fields;
}
/**
* Save selected video thumbnail in attachment meta.
* Selected thumbnail use as cover art for buddypress activity if video was uploaded in activity.
*
* @since 1.0.0
*
* @param array $post An array of post data.
* @return array $form_fields
*/
public function save_video_thumbnail( $post ) {
$rtmedia_thumbnail = transcoder_filter_input( INPUT_POST, 'rtmedia-thumbnail', FILTER_SANITIZE_FULL_SPECIAL_CHARS );
$id = ( ! empty( $post['ID'] ) && 0 < intval( $post['ID'] ) ) ? intval( $post['ID'] ) : 0;
if ( isset( $rtmedia_thumbnail ) ) {
if ( class_exists( 'rtMedia' ) ) {
$file_url = $rtmedia_thumbnail;
/* for WordPress backward compatibility */
if ( function_exists( 'wp_get_upload_dir' ) ) {
$uploads = wp_get_upload_dir();
} else {
$uploads = wp_upload_dir();
}
if ( 0 === strpos( $file_url, $uploads['baseurl'] ) ) {
$final_file_url = $file_url;
} else {
$final_file_url = $uploads['baseurl'] . '/' . $file_url;
}
$rtmedia_model = new RTMediaModel();
$media = $rtmedia_model->get( array( 'media_id' => $id ) );
$media_id = $media[0]->id;
$rtmedia_model->update( array( 'cover_art' => $final_file_url ), array( 'media_id' => $id ) );
rtt_update_activity_after_thumb_set( $media_id );
}
update_post_meta( $id, '_rt_media_video_thumbnail', $rtmedia_thumbnail );
}
return $post;
}
/**
* Display admin notice.
*
* @since 1.0.0
*/
public function transcoder_admin_notice() {
$show_notice = get_site_option( 'transcoder_admin_notice', 1 );
if ( '1' === $show_notice || 1 === $show_notice ) :
?>
<div class="notice notice-info transcoder-notice is-dismissible">
<?php wp_nonce_field( '_transcoder_hide_notice_', 'transcoder_hide_notice_nonce' ); ?>
<p>
<?php esc_html_e( 'rtMedia encoding service has been disabled because you are using Transcoder plugin.', 'transcoder' ); ?>
</p>
</div>
<script type="text/javascript">
jQuery( document ).ready( function() {
jQuery( '.transcoder-notice.is-dismissible' ).on( 'click', '.notice-dismiss', function() {
var data = {
action: 'transcoder_hide_admin_notice',
transcoder_notice_nonce: jQuery('#transcoder_hide_notice_nonce').val()
};
jQuery.post( ajaxurl, data, function ( response ) {
jQuery('.transcoder-notice').remove();
});
});
});
</script>
<?php
endif;
}
/**
* Display subscribe to the transcoding service
*/
public function subscribe_transcoder_admin_notice() {
if ( ! empty( $this->api_key ) ) {
return false;
}
$settings_page_link = 'admin.php?page=rt-transcoder';
$class = 'notice notice-error';
$valid_tags = array(
'div' => array( 'class' => array() ),
'p' => array(),
'strong' => array(),
'a' => array( 'href' => array() ),
);
// translators: Markup to show the info about plugin subscription if no API key is there.
printf( wp_kses( __( '<div class="%1$s"><p><strong>IMPORTANT!</strong> The Transcoder plugin works with active transcoding services subscription plan. <a href="%2$s">Click here</a> to subscribe or enable.</p></div>', 'transcoder' ), $valid_tags ), esc_attr( $class ), esc_url( admin_url( $settings_page_link ) ) );
}
/**
* Set option to hide admin notice when user click on dismiss button.
*
* @since 1.0.0
*/
public function transcoder_hide_admin_notice() {
if ( check_ajax_referer( '_transcoder_hide_notice_', 'transcoder_notice_nonce' ) ) {
update_site_option( 'transcoder_admin_notice', '0' );
}
die();
}
/**
* Hide encoding tab in old rtMedia plugin.
*
* @since 1.0.0
*/
public function rtmedia_hide_encoding_tab() {
?>
<style>
.rtmedia-tab-title.audiovideo-encoding {
display: none;
}
</style>
<?php
}
/**
* Filters the Mediaelement fallback output to add class.
*
* @since 1.0.0
*
* @param type $output Fallback output for no-JS.
* @param type $url Media file URL.
*
* @return string return fallback output.
*/
public function mediaelement_add_class( $output, $url ) {
return sprintf( '<a class="no-popup" href="%1$s">%1$s</a>', esc_url( $url ) );
}
/**
* Display GoDAM installation recommendation admin notice on specific pages.
*/
public function install_godam_admin_notice() {
$current_screen = get_current_screen();
// Define allowed pages
$allowed_pages = array(
'rt-transcoder',
'rt-retranscoder',
'dashboard',
);
// Check if we’re on allowed page using screen ID or $_GET['page']
$current_page = isset( $_GET['page'] ) ? $_GET['page'] : '';
$screen_id = isset( $current_screen->id ) ? $current_screen->id : '';
// Skip if not in our allowed pages
if (
( ! in_array( $current_page, $allowed_pages, true ) && ! in_array( $screen_id, $allowed_pages, true ) ) ||
get_user_meta( get_current_user_id(), '_godam_notice_dismissed', true )
) {
return;
}
// Check if GoDAM is already active
if ( is_plugin_active( 'godam/godam.php' ) ) {
return;
}
$plugin_slug = 'godam';
$plugin_modal_url = is_multisite()
? network_admin_url( 'plugin-install.php?tab=plugin-information&plugin=' . $plugin_slug . '&TB_iframe=true&width=772&height=666' )
: admin_url( 'plugin-install.php?tab=plugin-information&plugin=' . $plugin_slug . '&TB_iframe=true&width=772&height=666' );
$class = 'notice notice-info';
$valid_tags = array(
'div' => array( 'class' => array(), 'id' => array() ),
'p' => array(),
'strong' => array(),
'a' => array( 'href' => array(), 'class' => array(), 'target' => array() ),
);
printf(
wp_kses(
__( '<div class="%1$s"><p><strong>NOTICE:</strong> Transcoder plugin will be retired on <strong>May 31, 2025</strong>. We recommend removing this plugin and switching to our new plugin, <a href="https://godam.io/" target="_blank">GoDAM</a> which includes powerful Digital Asset Management features along with video transcoding services. <a href="%2$s" class="thickbox open-plugin-details-modal">Install GoDAM now</a>!</p></div>', 'transcoder' ),
$valid_tags
),
esc_attr( $class ),
esc_url( $plugin_modal_url )
);
}
/**
* Enqueue thickbox on transcoder settings page.
*/
public function enqueue_thickbox_on_transcoder_settings() {
add_thickbox();
}
}