-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathclass-rtgodam-transcoder-admin.php
More file actions
985 lines (864 loc) · 35.6 KB
/
class-rtgodam-transcoder-admin.php
File metadata and controls
985 lines (864 loc) · 35.6 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
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
<?php
/**
* The admin-specific functionality of the plugin.
*
* @since 1.0.0
*
* @package GoDAM
* @subpackage GoDAM/Admin
*/
defined( 'ABSPATH' ) || exit;
/**
* The admin-specific functionality of the plugin.
*
* @since 1.0.0
*
* @package GoDAM
* @subpackage GoDAM/Admin
*/
class RTGODAM_Transcoder_Admin {
/**
* Initialize the class and set its properties.
*
* @since 1.0.0
*/
public function __construct() {
include_once RTGODAM_PATH . 'admin/class-rtgodam-transcoder-handler.php'; // phpcs:ignore WordPressVIPMinimum.Files.IncludingFile.UsingCustomConstant
include_once RTGODAM_PATH . 'admin/godam-transcoder-actions.php'; // phpcs:ignore WordPressVIPMinimum.Files.IncludingFile.UsingCustomConstant
new RTGODAM_Transcoder_Handler();
if ( is_admin() ) {
if ( is_multisite() ) {
add_action( 'network_admin_notices', array( $this, 'api_activation_admin_notice' ) );
}
add_action( 'admin_notices', array( $this, 'api_activation_admin_notice' ) );
add_action( 'admin_notices', array( $this, 'dashboard_offer_banner' ) );
add_action( 'admin_notices', array( $this, 'free_plan_admin_notice' ) );
add_action( 'admin_notices', array( $this, 'usage_limit_notices' ) );
add_action( 'admin_notices', array( $this, 'posthog_tracking_notice' ) );
add_action( 'admin_notices', array( $this, 'api_key_status_notice' ) );
add_action( 'admin_notices', array( $this, 'expired_api_key_notice' ) );
add_action( 'admin_notices', array( $this, 'woo_integration_promo_notice' ) );
add_action( 'admin_init', array( $this, 'handle_posthog_tracking_action' ) );
add_action( 'admin_init', array( $this, 'handle_clear_godam_cache' ) );
add_action( 'wp_ajax_rtgodam_dismiss_free_plan_notice', array( $this, 'dismiss_free_plan_notice' ) );
add_action( 'wp_ajax_rtgodam_dismiss_woo_promo_notice', array( $this, 'dismiss_woo_promo_notice' ) );
}
}
/**
* Display admin notice for activating the api key and handle dismissal.
*
* @since 1.0.0
*/
public function api_activation_admin_notice() {
if ( ! $this->is_dashboard_screen() ) {
return;
}
// Get the api key key from the site options.
$api_key = \RTGODAM\Inc\Helpers\Api_Key::get_key();
// Admin URL to the video editor settings page.
$video_editor_settings_url = admin_url( 'admin.php?page=rtgodam_settings#video-settings' );
// Get plugin activation time.
$activation_time = get_option( 'rtgodam_plugin_activation_time', 0 );
$days_since_activation = ( time() - $activation_time ) / DAY_IN_SECONDS;
// Show regular admin notice.
if ( empty( $api_key ) ) {
return;
}
// Get stored usage data.
$usage_data = get_option( 'rtgodam-usage', array() );
$usage_data = isset( $usage_data[ $api_key ] ) ? (array) $usage_data[ $api_key ] : null;
if ( empty( $usage_data ) ) {
$this->render_admin_notice(
sprintf(
// translators: %s is the URL to the plugin settings page where the API key can be activated.
__( 'Enjoy using our <strong>DAM and Video Editor</strong> features for free! To unlock Transcoding, Analytics and more, <a href="%s">please activate your API key.</a>', 'godam' ),
esc_url( $video_editor_settings_url )
),
'warning',
true,
false
);
return;
}
$status = $usage_data['status'] ?? '';
$subscription_status = $usage_data['subscription_status'] ?? '';
$subscription_end = isset( $usage_data['end_date'] ) ? strtotime( $usage_data['end_date'] ) : null;
$trial_end = isset( $usage_data['trial_end_date'] ) ? strtotime( $usage_data['trial_end_date'] ) : null;
$current_time = time();
$grace_period_days = 30;
// If the user is in trial mode, show trial expiry notice.
if ( 'Trialling' === $subscription_status && $trial_end ) {
$days_until_trial_end = ceil( ( $trial_end - $current_time ) / DAY_IN_SECONDS );
if ( $days_until_trial_end > 0 ) {
$this->render_admin_notice(
sprintf(
/* translators: %d: Number of days until trial ends */
__( 'Your product is under trial. You will be charged after <strong>%d days</strong>. If you wish to cancel, please visit <a href="https://app.godam.io/subscription/my-account" target="_blank">your subscription settings</a>.', 'godam' ),
$days_until_trial_end
),
'warning',
false,
false
);
return;
}
}
// If the API key is Active, no need to show any notice.
if ( 'Active' === $status ) {
return;
}
// Check subscription expiry and display appropriate messages.
if ( $subscription_end ) {
$days_until_deletion = floor( ( $subscription_end + ( $grace_period_days * DAY_IN_SECONDS ) - $current_time ) / DAY_IN_SECONDS );
if ( $days_until_deletion > 0 ) {
$this->render_admin_notice(
sprintf(
/* translators: %d: Number of days until transcoded videos are deleted after subscription ends */
__( 'Your subscription has ended. No further transcoding can be done. Transcoded videos will be removed after <strong>%d days</strong>, and Pro features will not be accessible. After the 30-day grace period, already transcoded videos will no longer be served from the CDN. Renew your subscription to keep it up and running.', 'godam' ),
$days_until_deletion
),
'error',
true,
false,
'activate'
);
return;
} else {
$this->render_admin_notice(
sprintf(
// translators: %s is the URL to the plugin settings page where the API key can be activated.
__( 'Enjoy using our <strong>DAM and Video Editor</strong> features for free! To unlock Transcoding, Analytics and more, <a href="%s">please activate your API key.</a>', 'godam' ),
esc_url( $video_editor_settings_url )
),
'error',
true,
false
);
return;
}
}
// Default fallback notice (if none of the above conditions are met).
$this->render_admin_notice(
sprintf(
// translators: %s is the URL to the plugin settings page where the API key can be activated.
__( 'Enjoy using our <strong>DAM and Video Editor</strong> features for free! To unlock Transcoding, Analytics and more, <a href="%s">please activate your API key.</a>', 'godam' ),
esc_url( $video_editor_settings_url )
),
'warning',
true,
false
);
}
/**
* Render an admin notice for API key activation or expiry warnings.
*
* @param string $message The message to display.
* @param string $notice_type Type of notice (warning, error, etc.).
* @param bool $include_buttons Whether to include action buttons (default: false).
* @param bool $show_godam_message Whether to show the GoDAM update message (default: false).
* @param string $button_type The type of button to display (default: 'editor').
*/
private function render_admin_notice( $message, $notice_type = 'warning', $include_buttons = false, $show_godam_message = false, $button_type = 'editor' ) {
// Get the GoDAM logo URL.
$logo_url = plugins_url( 'assets/src/images/godam-logo.png', __DIR__ );
$button_label = ( 'activate' === $button_type ) ? esc_html__( 'Activate API Key', 'godam' ) : esc_html__( 'Use Video Editor', 'godam' );
$button_link = ( 'activate' === $button_type ) ? admin_url( 'admin.php?page=rtgodam_settings' ) : admin_url( 'admin.php?page=rtgodam_video_editor' );
?>
<div class="notice notice-<?php echo esc_attr( $notice_type ); ?> is-dismissible rt-transcoder-api-key-notice">
<div class="godam-notice-header">
<img src="<?php echo esc_url( $logo_url ); ?>" alt="<?php echo esc_attr__( 'GoDAM Logo', 'godam' ); ?>" class="godam-logo">
<div>
<?php if ( $show_godam_message ) : ?>
<p>
<?php esc_html_e( 'Welcome to GoDAM! Thank you for using our product.', 'godam' ); ?>
</p>
<?php endif; ?>
<p>
<?php
echo wp_kses(
$message,
array(
'strong' => array(),
'a' => array(
'href' => array(),
'target' => array(),
),
)
);
?>
</p>
<?php if ( $include_buttons ) : ?>
<p>
<a href="<?php echo esc_url( $button_link ); ?>" class="button button-primary">
<?php echo esc_html( $button_label ); ?>
</a>
<a href="https://godam.io/" class="button button-secondary" target="_blank">
<?php esc_html_e( 'Learn More', 'godam' ); ?>
</a>
</p>
<?php endif; ?>
</div>
</div>
</div>
<?php
}
/**
* Display New Year Sale offer banner on the WordPress dashboard.
*
* @return void
*/
public function dashboard_offer_banner() {
if ( ! $this->is_dashboard_screen() ) {
return;
}
$show_offer_banner = get_option( 'rtgodam-offer-banner', 1 );
$timezone = wp_timezone();
$current_time = new \DateTime( 'now', $timezone );
$end_time = new \DateTime( '2026-01-20 23:59:59', $timezone );
if ( $current_time <= $end_time && $show_offer_banner ) {
$host = wp_parse_url( home_url(), PHP_URL_HOST );
$banner_image = RTGODAM_URL . 'assets/src/images/new-year-sale-2026.webp';
$banner_html = sprintf(
'<div class="notice annual-plan-offer-banner">
<a
href="%1$s"
class="annual-plan-offer-banner__link"
target="_blank"
rel="noopener noreferrer"
aria-label="%2$s"
>
<img
src="%3$s"
class="annual-plan-offer-banner__image"
alt="%4$s"
loading="lazy"
/>
</a>
<button
type="button"
class="annual-plan-offer-banner__dismiss"
aria-label="%5$s"
>
×
</button>
</div>',
esc_url( RTGODAM_IO_API_BASE . '/pricing?utm_campaign=new-year-sale-2026&utm_source=' . $host . '&utm_medium=plugin&utm_content=dashboard-banner' ),
esc_attr__( 'Claim the GoDAM New Year Sale 2026 offer', 'godam' ),
esc_url( $banner_image ),
esc_attr__( 'New Year Sale 2026 offer from GoDAM', 'godam' ),
esc_html__( 'Dismiss banner', 'godam' )
);
echo wp_kses(
$banner_html,
array(
'div' => array( 'class' => array() ),
'a' => array(
'href' => array(),
'class' => array(),
'target' => array(),
'rel' => array(),
'aria-label' => array(),
),
'img' => array(
'src' => array(),
'alt' => array(),
'class' => array(),
'loading' => array(),
),
'button' => array(
'type' => array(),
'class' => array(),
'aria-label' => array(),
),
)
);
}
}
/**
* Display free plan admin notice for users without API key.
*
* @return void
*/
public function free_plan_admin_notice() {
if ( ! $this->is_dashboard_screen() ) {
return;
}
// Only show to users without an API key.
if ( rtgodam_is_api_key_valid() ) {
return;
}
// Check if user has dismissed this notice recently (within 7 days).
$dismissed_timestamp = get_option( 'rtgodam_free_plan_notice_dismissed_timestamp', false );
if ( $dismissed_timestamp && ( time() - $dismissed_timestamp ) < ( 7 * DAY_IN_SECONDS ) ) {
return;
}
// Get the GoDAM logo URL.
$logo_url = plugins_url( 'assets/src/images/godam-logo.png', __DIR__ );
?>
<div class="notice notice-info is-dismissible rtgodam-free-plan-notice">
<div class="godam-notice-header">
<img src="<?php echo esc_url( $logo_url ); ?>" alt="<?php esc_attr_e( 'GoDAM Logo', 'godam' ); ?>" class="godam-logo" />
<div>
<p style="font-size: 16px; font-weight: 600; margin-bottom: 15px;">
<?php esc_html_e( 'Unlock Video Analytics, Transcoding and more with GoDAM Pro. Start with a free 60-day trial.', 'godam' ); ?>
</p>
<div style="display: flex; gap: 10px; margin-top: 10px; margin-bottom: 15px;">
<a href="<?php echo esc_url( RTGODAM_IO_API_BASE . '/pricing?utm_campaign=free-plan-notice&utm_source=plugin&utm_medium=admin-notice&utm_content=dashboard' ); ?>" target="_blank" rel="noopener noreferrer" class="button button-primary">
<?php esc_html_e( 'Get your Free Plan', 'godam' ); ?>
</a>
<a href="<?php echo esc_url( admin_url( 'admin.php?page=rtgodam_settings#video-settings' ) ); ?>" class="button button-secondary">
<?php esc_html_e( 'Activate API Key', 'godam' ); ?>
</a>
</div>
</div>
</div>
</div>
<script>
jQuery(document).ready(function($) {
// Use event delegation to handle the click even if the button is added dynamically
$(document).on('click', '.rtgodam-free-plan-notice .notice-dismiss', function() {
var $notice = $(this).closest('.rtgodam-free-plan-notice');
// Hide the notice immediately when clicked
$notice.fadeOut();
// Store dismissal timestamp via AJAX
var url = (typeof ajaxurl !== 'undefined') ? ajaxurl : 'admin-ajax.php';
$.post(url, {
action: 'rtgodam_dismiss_free_plan_notice',
nonce: '<?php echo esc_js( wp_create_nonce( 'dismiss_free_plan_notice' ) ); ?>'
});
});
});
</script>
<?php
}
/**
* Dismiss the free plan notice.
*
* @return void
*/
public function dismiss_free_plan_notice() {
check_ajax_referer( 'dismiss_free_plan_notice', 'nonce' );
// Store timestamp for 7-day temporary dismissal.
update_option( 'rtgodam_free_plan_notice_dismissed_timestamp', time() );
wp_die();
}
/**
* Dismiss the WooCommerce integration promo notice.
*
* @return void
*/
public function dismiss_woo_promo_notice() {
check_ajax_referer( 'dismiss_woo_promo_notice', 'nonce' );
// Store timestamp for 30-day temporary dismissal.
update_option( 'rtgodam_woo_promo_dismissed_timestamp', time() );
wp_die();
}
/**
* Check if the current screen is the dashboard.
*
* @return bool
*/
public function is_dashboard_screen() {
$current_screen = get_current_screen();
return isset( $current_screen->id ) && 'dashboard' === $current_screen->id;
}
/**
* Handle the cache clearing request for GoDAM usage data.
* This runs on admin_init to ensure headers haven't been sent yet.
*
* @since 1.5.0
*/
public function handle_clear_godam_cache() {
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Nonce is verified below.
if ( ! isset( $_GET['clear_godam_cache'] ) ) {
return;
}
// Verify user capabilities.
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
// Verify nonce.
if ( ! isset( $_GET['_wpnonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ), 'clear_godam_cache' ) ) {
return;
}
// Clear the cache.
delete_option( 'rtgodam_user_data' );
// Build redirect URL safely.
$current_url = remove_query_arg( array( 'clear_godam_cache', '_wpnonce' ) );
// Perform the redirect with error handling.
if ( ! headers_sent() ) {
wp_safe_redirect( $current_url );
exit;
}
}
/**
* Display usage limit notices when bandwidth/storage usage is high.
*/
public function usage_limit_notices() {
// Only show on admin pages.
if ( ! is_admin() || wp_doing_ajax() ) {
return;
}
// Only show to users who can manage options.
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
// Don't show usage notices if no API key is configured.
$api_key = \RTGODAM\Inc\Helpers\Api_Key::get_key();
if ( empty( $api_key ) ) {
return;
}
// Get user data with usage information.
$user_data = rtgodam_get_user_data( true );
// Check if we have valid usage data.
if ( empty( $user_data ) || ! isset( $user_data['bandwidthUsed'] ) || ! isset( $user_data['totalBandwidth'] ) ||
! isset( $user_data['storageUsed'] ) || ! isset( $user_data['totalStorage'] ) ) {
return;
}
// Calculate usage percentages.
$bandwidth_used = floatval( $user_data['bandwidthUsed'] );
$bandwidth_total = floatval( $user_data['totalBandwidth'] );
$storage_used = floatval( $user_data['storageUsed'] );
$storage_total = floatval( $user_data['totalStorage'] );
$bandwidth_percentage = $bandwidth_total > 0 ? ( $bandwidth_used / $bandwidth_total ) * 100 : 0;
$storage_percentage = $storage_total > 0 ? ( $storage_used / $storage_total ) * 100 : 0;
// Check for exceeded limits (highest priority) - only storage blocks transcoding.
$bandwidth_exceeded = $bandwidth_used > $bandwidth_total;
$storage_exceeded = $storage_used > $storage_total;
if ( $storage_exceeded ) {
$this->display_limit_exceeded_notice( $bandwidth_exceeded, $storage_exceeded, $bandwidth_percentage, $storage_percentage );
return; // Don't show warning notice if storage limits are exceeded.
}
// Check for bandwidth exceeded (warning but transcoding still works).
if ( $bandwidth_exceeded ) {
$this->display_bandwidth_exceeded_notice( $bandwidth_percentage );
return; // Don't show regular warning notice if bandwidth is exceeded.
}
// Check for 80% warning level.
$bandwidth_warning = $bandwidth_percentage >= 80;
$storage_warning = $storage_percentage >= 80;
if ( $bandwidth_warning || $storage_warning ) {
$this->display_limit_warning_notice( $bandwidth_warning, $storage_warning, $bandwidth_percentage, $storage_percentage );
}
}
/**
* Display notice when usage limits are exceeded.
*
* @param bool $bandwidth_exceeded Whether bandwidth limit is exceeded.
* @param bool $storage_exceeded Whether storage limit is exceeded.
* @param float $bandwidth_percentage Current bandwidth usage percentage.
* @param float $storage_percentage Current storage usage percentage.
*/
private function display_limit_exceeded_notice( $bandwidth_exceeded, $storage_exceeded, $bandwidth_percentage, $storage_percentage ) {
$message_parts = array();
if ( $bandwidth_exceeded && $storage_exceeded ) {
$message_parts[] = sprintf(
/* translators: %1$s: bandwidth percentage, %2$s: storage percentage */
__( 'Your bandwidth (%1$s%%) and storage (%2$s%%) usage have exceeded your plan limits.', 'godam' ),
'<strong>' . number_format( $bandwidth_percentage, 1 ) . '</strong>',
'<strong>' . number_format( $storage_percentage, 1 ) . '</strong>'
);
} elseif ( $bandwidth_exceeded ) {
$message_parts[] = sprintf(
/* translators: %s: bandwidth percentage */
__( 'Your bandwidth usage (%s%%) has exceeded your plan limit.', 'godam' ),
'<strong>' . number_format( $bandwidth_percentage, 1 ) . '</strong>'
);
} elseif ( $storage_exceeded ) {
$message_parts[] = sprintf(
/* translators: %s: storage percentage */
__( 'Your storage usage (%s%%) has exceeded your plan limit.', 'godam' ),
'<strong>' . number_format( $storage_percentage, 1 ) . '</strong>'
);
}
$message_parts[] = __( 'Transcoding requests may be blocked until you upgrade your plan.', 'godam' );
$message = implode( ' ', $message_parts );
// Get the GoDAM logo URL.
$logo_url = plugins_url( 'assets/src/images/godam-logo.png', __DIR__ );
?>
<div class="notice notice-error is-dismissible">
<div class="godam-notice-header">
<img src="<?php echo esc_url( $logo_url ); ?>" alt="GoDAM Logo" class="godam-logo">
<div>
<p><strong><?php esc_html_e( 'GoDAM Usage Limit Exceeded', 'godam' ); ?></strong></p>
<p><?php echo wp_kses( $message, array( 'strong' => array() ) ); ?></p>
<p>
<a href="https://app.godam.io/web/billing?tab=Plans" target="_blank" rel="noopener noreferrer" class="button button-primary">
<?php esc_html_e( 'Upgrade Your Plan', 'godam' ); ?>
</a>
<a href="https://godam.io/docs/troubleshooting/bandwidth-storage?utm_source=wordpress-plugin&utm_medium=admin-notice&utm_campaign=limit-exceeded&utm_content=learn-more-button" target="_blank" rel="noopener noreferrer" class="button button-secondary">
<?php esc_html_e( 'Learn More', 'godam' ); ?>
</a>
<a href="<?php echo esc_url( wp_nonce_url( add_query_arg( 'clear_godam_cache', '1' ), 'clear_godam_cache' ) ); ?>" class="button button-secondary">
<?php esc_html_e( 'Refresh Status', 'godam' ); ?>
</a>
</p>
</div>
</div>
</div>
<?php
}
/**
* Display warning notice when usage reaches 80%.
*
* @param bool $bandwidth_warning Whether bandwidth is at warning level.
* @param bool $storage_warning Whether storage is at warning level.
* @param float $bandwidth_percentage Current bandwidth usage percentage.
* @param float $storage_percentage Current storage usage percentage.
*/
private function display_limit_warning_notice( $bandwidth_warning, $storage_warning, $bandwidth_percentage, $storage_percentage ) {
$message_parts = array();
if ( $bandwidth_warning && $storage_warning ) {
$message_parts[] = sprintf(
/* translators: %1$s: bandwidth percentage, %2$s: storage percentage */
__( 'Your bandwidth (%1$s%%) and storage (%2$s%%) usage are approaching your plan limits.', 'godam' ),
'<strong>' . number_format( $bandwidth_percentage, 1 ) . '</strong>',
'<strong>' . number_format( $storage_percentage, 1 ) . '</strong>'
);
} elseif ( $bandwidth_warning ) {
$message_parts[] = sprintf(
/* translators: %s: bandwidth percentage */
__( 'Your bandwidth usage (%s%%) is approaching your plan limit.', 'godam' ),
'<strong>' . number_format( $bandwidth_percentage, 1 ) . '</strong>'
);
} elseif ( $storage_warning ) {
$message_parts[] = sprintf(
/* translators: %s: storage percentage */
__( 'Your storage usage (%s%%) is approaching your plan limit.', 'godam' ),
'<strong>' . number_format( $storage_percentage, 1 ) . '</strong>'
);
}
$message_parts[] = __( 'Consider upgrading your plan to avoid service interruptions.', 'godam' );
$message = implode( ' ', $message_parts );
// Get the GoDAM logo URL.
$logo_url = plugins_url( 'assets/src/images/godam-logo.png', __DIR__ );
?>
<div class="notice notice-warning is-dismissible">
<div class="godam-notice-header">
<img src="<?php echo esc_url( $logo_url ); ?>" alt="GoDAM Logo" class="godam-logo">
<div>
<p><strong><?php esc_html_e( 'GoDAM Usage Warning', 'godam' ); ?></strong></p>
<p><?php echo wp_kses( $message, array( 'strong' => array() ) ); ?></p>
<p>
<a href="https://app.godam.io/web/billing?tab=Plans" target="_blank" rel="noopener noreferrer" class="button button-primary">
<?php esc_html_e( 'View Plans', 'godam' ); ?>
</a>
<a href="https://godam.io/docs/troubleshooting/bandwidth-storage?utm_source=wordpress-plugin&utm_medium=admin-notice&utm_campaign=usage-warning&utm_content=learn-more-button" target="_blank" rel="noopener noreferrer" class="button button-secondary">
<?php esc_html_e( 'Learn More', 'godam' ); ?>
</a>
<a href="<?php echo esc_url( wp_nonce_url( add_query_arg( 'clear_godam_cache', '1' ), 'clear_godam_cache' ) ); ?>" class="button button-secondary">
<?php esc_html_e( 'Refresh Status', 'godam' ); ?>
</a>
</p>
</div>
</div>
</div>
<?php
}
/**
* Display notice when bandwidth limit is exceeded (but transcoding still works).
*
* @param float $bandwidth_percentage Current bandwidth usage percentage.
*/
private function display_bandwidth_exceeded_notice( $bandwidth_percentage ) {
$message = sprintf(
/* translators: %s: bandwidth percentage */
__( 'Your bandwidth usage (%s%%) has exceeded your plan limit. Videos might not be served from CDN on frontend.', 'godam' ),
'<strong>' . number_format( $bandwidth_percentage, 1 ) . '</strong>'
);
// Get the GoDAM logo URL.
$logo_url = plugins_url( 'assets/src/images/godam-logo.png', __DIR__ );
?>
<div class="notice notice-error is-dismissible">
<div class="godam-notice-header">
<img src="<?php echo esc_url( $logo_url ); ?>" alt="GoDAM Logo" class="godam-logo">
<div>
<p><strong><?php esc_html_e( 'GoDAM Bandwidth Exceeded', 'godam' ); ?></strong></p>
<p><?php echo wp_kses( $message, array( 'strong' => array() ) ); ?> <?php esc_html_e( 'Transcoding will continue to work.', 'godam' ); ?></p>
<p>
<a href="https://app.godam.io/web/billing?tab=Plans" target="_blank" rel="noopener noreferrer" class="button button-primary">
<?php esc_html_e( 'Upgrade Your Plan', 'godam' ); ?>
</a>
<a href="https://godam.io/docs/troubleshooting/bandwidth-storage?utm_source=wordpress-plugin&utm_medium=admin-notice&utm_campaign=bandwidth-exceeded&utm_content=learn-more-button" target="_blank" rel="noopener noreferrer" class="button button-secondary">
<?php esc_html_e( 'Learn More', 'godam' ); ?>
</a>
<a href="<?php echo esc_url( wp_nonce_url( add_query_arg( 'clear_godam_cache', '1' ), 'clear_godam_cache' ) ); ?>" class="button button-secondary">
<?php esc_html_e( 'Refresh Status', 'godam' ); ?>
</a>
</p>
</div>
</div>
</div>
<?php
}
/**
* Display PostHog tracking notice.
*
* Only shown on the WP Dashboard.
*
* @since 1.5.0
*/
public function posthog_tracking_notice() {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
// Only show on the WP Dashboard screen.
if ( ! $this->is_dashboard_screen() ) {
return;
}
$settings = get_option( 'rtgodam-settings', array() );
$api_key = \RTGODAM\Inc\Helpers\Api_Key::get_key();
// If API key is present, we don't show the notice (it's auto-enabled or already handled).
if ( ! empty( $api_key ) ) {
return;
}
// If posthog_initialized is true, it means the user has already made a choice or it was auto-enabled.
if ( ! empty( $settings['general']['posthog_initialized'] ) ) {
return;
}
$optin_url = wp_nonce_url( add_query_arg( 'godam_tracker_optin', 'true' ), 'godam_tracker_action' );
$optout_url = wp_nonce_url( add_query_arg( 'godam_tracker_optout', 'true' ), 'godam_tracker_action' );
$notice = sprintf(
__( 'Want to help make <strong>GoDAM</strong> even more awesome? Allow GoDAM to collect anonymous diagnostic data and usage information.', 'godam' )
);
$policy_url = 'https://posthog.com/privacy';
echo '<div class="updated"><p>';
echo wp_kses_post( $notice );
echo ' (<a class="godam-insights-data-we-collect" href="#">' . esc_html__( 'what we collect', 'godam' ) . '</a>)';
echo '<div class="description" style="display:none; margin-top: 10px; padding: 10px; background: #f9f9f9; border: 1px solid #e5e5e5; border-radius: 4px;">';
echo '<p style="margin-top: 0;"><strong>' . esc_html__( 'Data we collect:', 'godam' ) . '</strong></p>';
echo '<ul style="list-style: disc; margin-left: 20px;">';
echo '<li>' . esc_html__( 'Server environment details (PHP, MySQL, Server, and WordPress versions)', 'godam' ) . '</li>';
echo '<li>' . esc_html__( 'Site and user count (Language and number of users)', 'godam' ) . '</li>';
echo '<li>' . esc_html__( 'Plugin details (Number of active and inactive plugins)', 'godam' ) . '</li>';
echo '<li>' . esc_html__( 'Usage data (Interactions, navigation, session replays, heatmaps, and errors via PostHog)', 'godam' ) . '</li>';
echo '</ul>';
echo '<p style="margin-bottom: 0;">';
printf(
wp_kses(
/* translators: %s: PostHog privacy policy URL */
__( 'We are using PostHog to collect anonymous data. <a href="%s" target="_blank">Learn more</a>. This can be disabled from the settings on the Help page.', 'godam' ),
array(
'a' => array(
'href' => array(),
'target' => array(),
),
)
),
esc_url( $policy_url )
);
echo '</p></div>';
echo '</p><p class="submit">';
echo ' <a href="' . esc_url( $optin_url ) . '" class="button-primary button-large">' . esc_html__( 'Allow', 'godam' ) . '</a>';
echo ' <a href="' . esc_url( $optout_url ) . '" class="button-secondary button-large">' . esc_html__( 'No thanks', 'godam' ) . '</a>';
echo '</p></div>';
}
/**
* Display admin notice when API key is expired or invalid in media library.
*
* @since 1.7.0
*/
public function api_key_status_notice() {
// Only show in media library and upload pages.
$screen = get_current_screen();
if ( ! $screen || ! in_array( $screen->id, array( 'upload', 'media' ), true ) ) {
return;
}
// Get API key.
$api_key = \RTGODAM\Inc\Helpers\Api_Key::get_key();
if ( empty( $api_key ) ) {
return; // No API key, no need to show status notice.
}
// Get user data which includes the current status including temporary verification_failed.
$user_data = rtgodam_get_user_data( true );
// Check API key status from user data (includes transient verification_failed).
$api_key_status = isset( $user_data['apiKeyStatus'] ) ? $user_data['apiKeyStatus'] : \RTGODAM\Inc\Enums\Api_Key_Status::VALID;
// Only show notice for expired or verification_failed status.
if ( \RTGODAM\Inc\Enums\Api_Key_Status::VALID === $api_key_status ) {
return;
}
$settings_url = admin_url( 'admin.php?page=rtgodam_settings#video-settings' );
$status_messages = array(
\RTGODAM\Inc\Enums\Api_Key_Status::EXPIRED => sprintf(
/* translators: %s: URL to settings page */
__( 'Your GoDAM API key has expired. Transcoding and other Pro features are currently disabled. Please <a href="%s">renew your subscription</a> to restore access.', 'godam' ),
esc_url( $settings_url )
),
\RTGODAM\Inc\Enums\Api_Key_Status::VERIFICATION_FAILED => sprintf(
/* translators: %s: URL to settings page */
__( 'Temporarily unable to verify your GoDAM API key. Transcoding may not work. Please <a href="%s">check your settings</a> or try refreshing the status.', 'godam' ),
esc_url( $settings_url )
),
);
$message = isset( $status_messages[ $api_key_status ] )
? $status_messages[ $api_key_status ]
: sprintf(
/* translators: %s: URL to settings page */
__( 'There is an issue with your GoDAM API key. Transcoding may not work. Please <a href="%s">check your settings</a>.', 'godam' ),
esc_url( $settings_url )
);
$notice_type = ( \RTGODAM\Inc\Enums\Api_Key_Status::EXPIRED === $api_key_status ) ? 'error' : 'warning';
echo '<div class="notice notice-' . esc_attr( $notice_type ) . ' is-dismissible">';
echo '<p>' . wp_kses_post( $message ) . '</p>';
echo '</div>';
}
/**
* Handle PostHog tracking action.
*
* @since 1.4.9
*/
public function handle_posthog_tracking_action() {
if ( ! isset( $_GET['godam_tracker_optin'] ) && ! isset( $_GET['godam_tracker_optout'] ) ) {
return;
}
$nonce = isset( $_GET['_wpnonce'] ) ? sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ) : '';
if ( ! wp_verify_nonce( $nonce, 'godam_tracker_action' ) ) {
return;
}
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
$settings = get_option( 'rtgodam-settings', array() );
if ( ! isset( $settings['general'] ) ) {
$settings['general'] = array();
}
if ( isset( $_GET['godam_tracker_optin'] ) && 'true' === $_GET['godam_tracker_optin'] ) {
$settings['general']['enable_posthog_tracking'] = true;
$settings['general']['posthog_initialized'] = true;
} elseif ( isset( $_GET['godam_tracker_optout'] ) && 'true' === $_GET['godam_tracker_optout'] ) {
$settings['general']['enable_posthog_tracking'] = false;
$settings['general']['posthog_initialized'] = true;
}
update_option( 'rtgodam-settings', $settings );
wp_safe_redirect( esc_url_raw( remove_query_arg( array( 'godam_tracker_optin', 'godam_tracker_optout', '_wpnonce' ) ) ) );
exit;
}
/**
* Show a warning notice on all admin pages when the GoDAM API key has expired.
*
* Only displayed when:
* - An API key is configured but is no longer valid.
*
* @since 1.8.0
*/
public function expired_api_key_notice() {
$api_key = \RTGODAM\Inc\Helpers\Api_Key::get_key();
if ( empty( $api_key ) || rtgodam_is_api_key_valid() ) {
return;
}
// rtgodam_get_api_key_status() only reads the persistent DB status option
// ('rtgodam-api-key-status') which can NEVER contain 'verification_failed'
// because that state is transient and not persistable. The actual runtime
// status — including transient 'verification_failed' — is stored in the
// 'rtgodam_user_data' option by rtgodam_get_user_data(). Read from there
// so we don't show a false "expired" banner when the server is temporarily
// unreachable.
$cached_user_data = get_option( 'rtgodam_user_data', array() );
$runtime_status = isset( $cached_user_data['api_key_status'] )
? $cached_user_data['api_key_status']
: rtgodam_get_api_key_status();
if ( \RTGODAM\Inc\Enums\Api_Key_Status::VERIFICATION_FAILED === $runtime_status ) {
return;
}
$pricing_url = add_query_arg(
array(
'utm_campaign' => 'expired-key',
'utm_source' => rawurlencode( site_url() ),
'utm_medium' => 'plugin',
'utm_content' => 'godam-admin-notice',
),
'https://godam.io/pricing'
);
printf(
'<div class="notice notice-warning"><p>%s</p></div>',
wp_kses_post(
sprintf(
/* translators: 1: Opening link tag, 2: Closing link tag */
__( '<strong>GoDAM API key has expired.</strong> Pro features in GoDAM are currently disabled. %1$sPurchase a new key%2$s to restore access.', 'godam' ),
'<a href="' . esc_url( $pricing_url ) . '" target="_blank" rel="noopener noreferrer">',
'</a>'
)
)
);
}
/**
* Display a contextual admin notice promoting WooCommerce integration.
*
* The notice adapts based on whether:
* - The GoDAM for Woo add-on is installed.
* - WooCommerce is installed & active.
* - The API key is valid.
*
* Once the add-on is installed the notice is permanently hidden.
*
* @since 1.8.0
*/
public function woo_integration_promo_notice() {
if ( ! function_exists( 'is_plugin_active' ) ) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
}
// If the add-on is already installed, no promo needed.
$addon_slug = 'godam-for-woo/godam-for-woo.php';
if ( file_exists( WP_PLUGIN_DIR . '/' . $addon_slug ) ) {
return;
}
// Check if user dismissed the notice (re-show after 30 days).
$dismissed = get_option( 'rtgodam_woo_promo_dismissed_timestamp', false );
if ( $dismissed && ( time() - (int) $dismissed ) < ( 30 * DAY_IN_SECONDS ) ) {
return;
}
$is_woo_active = is_plugin_active( 'woocommerce/woocommerce.php' );
$has_valid_key = rtgodam_is_api_key_valid();
$pricing_url = add_query_arg(
array(
'utm_campaign' => 'woo-promo',
'utm_source' => rawurlencode( site_url() ),
'utm_medium' => 'plugin',
'utm_content' => 'woo-admin-notice',
),
'https://godam.io/pricing'
);
$integrations_url = admin_url( 'admin.php?page=rtgodam_settings#integrations-settings' );
$woo_plugin_url = admin_url( 'plugin-install.php?s=woocommerce&tab=search&type=term' );
if ( $is_woo_active && $has_valid_key ) {
// Case 4: Required active + valid key → link to integrations page.
$message = sprintf(
/* translators: 1: opening link tag, 2: closing link tag */
__( '<strong>Shoppable video for WooCommerce is live!</strong> %1$sActivate%2$s to start selling directly from your videos.', 'godam' ),
'<a href="' . esc_url( $integrations_url ) . '">',
'</a>'
);
} elseif ( ! $is_woo_active && $has_valid_key ) {
// Case 2: WooCommerce not active + valid key → install/activate WooCommerce.
$message = sprintf(
/* translators: 1: opening link tag, 2: closing link tag */
__( '<strong>GoDAM now supports WooCommerce!</strong> %1$sInstall WooCommerce%2$s to start using shoppable videos with GoDAM.', 'godam' ),
'<a href="' . esc_url( $woo_plugin_url ) . '">',
'</a>'
);
} else {
// Cases 1 & 3: No valid key → purchase.
$message = sprintf(
/* translators: 1: opening link tag, 2: closing link tag */
__( '<strong>Add shoppable videos to your WooCommerce store!</strong> %1$sUpgrade your plan%2$s to unlock this feature.', 'godam' ),
'<a href="' . esc_url( $pricing_url ) . '" target="_blank" rel="noopener noreferrer">',
'</a>'
);
}
printf(
'<div class="notice notice-info is-dismissible rtgodam-for-woo-promo-notice"><p>%s</p></div>',
wp_kses_post( $message )
);
// Inline script to persist dismissal via AJAX.
?>
<script>
jQuery( document ).on( 'click', '.rtgodam-for-woo-promo-notice .notice-dismiss', function() {
jQuery.post( ajaxurl, {
action: 'rtgodam_dismiss_woo_promo_notice',
nonce: '<?php echo esc_js( wp_create_nonce( 'dismiss_woo_promo_notice' ) ); ?>'
} );
} );
</script>
<?php
}
}