forked from netmix/radio-station
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
radio-station-admin.php
1846 lines (1564 loc) · 73 KB
/
radio-station-admin.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
/*
* Radio Station Plugin Admin Functions
* @Since: 2.2.7
*/
if ( !defined( 'ABSPATH' ) ) exit;
// === Admin Setup ===
// - Enqueue Admin Scripts
// - Admin Style Fixes
// - Filter Plugin Action Links
// === Admin Menu ===
// - Setting Page Capability Check
// - Add Admin Menu and Submenu Items
// - Fix to Expand Main Menu for Submenu Items
// - Taxonomy Submenu Item Fix
// - Fix to Redirect Plugin Settings Menu Link
// - Display Import/Export Show Page
// - Display Plugin Docs Page
// - Parse Markdown Doc File
// x Display Playlist Export Page
// === Update Notices ===
// - Get Plugin Upgrade Notice
// - Parse Plugin Update Notice
// - Plugin Page Update Message
// - Display Admin Update Notice
// - Display Plugin Notice
// - Get Plugin Notices
// - AJAX Mark Notice Read
// === Admin Notices ===
// - Plugin Settings Page Top
// - Plugin Settings Page Bottom
// - Directory Listing Offer Notice
// - Launch Offer Notice
// - Directory Listing Offer Content
// - Launch Offer Content
// - Dismiss Free Listing Offer
// - Plugin Takeover Announcement Notice
// - Plugin Takeover Announcement Content
// - Dismiss Plugin Takeover Announcement
// - Show Shift Conflict Notice
// - MailChimp Subscriber Form
// - AJAX Record Subscriber
// - AJAX Clear Subscriber
// -------------------
// === Admin Setup ===
// -------------------
// ---------------------
// Enqueue Admin Scripts
// ---------------------
// 2.2.7: removed from frontend as datepicker is only used on the backend
add_action( 'admin_enqueue_scripts', 'radio_station_enqueue_admin_scripts' );
function radio_station_enqueue_admin_scripts() {
// --- enqueue admin js file ---
$script = radio_station_get_template( 'both', 'radio-station-admin.js', 'js' );
$version = filemtime( $script['file'] );
$deps = array( 'jquery' );
wp_enqueue_script( 'radio-station-admin', $script['url'], $deps, $version, true );
if ( RADIO_STATION_DEBUG ) {
$js = "radio_admin.debug = true;";
// 2.5.0: use radio_station_add_inline_script
radio_station_add_inline_script( 'radio-station-admin', $js );
}
// --- enqueue admin styles ---
radio_station_enqueue_style( 'admin' );
// 2.5.0: maybe enqueue pricing page styles
if ( isset( $_REQUEST['page'] ) && ( 'radio-station-pricing' == sanitize_text_field( $_REQUEST['page'] ) ) ) {
$style_url = plugins_url( 'freemius-pricing/freemius-pricing.css', RADIO_STATION_FILE );
$style_path = RADIO_STATION_DIR . '/freemius-pricing/freemius-pricing.css';
$version = filemtime( $style_path );
wp_enqueue_style( 'freemius-pricing', $style_url, array(), $version, 'all' );
}
}
// -----------------
// Admin Style Fixes
// -----------------
add_action( 'admin_print_styles', 'radio_station_admin_styles' );
function radio_station_admin_styles() {
// --- hide first admin submenu item to prevent duplicate of main menu item ---
$css = '#toplevel_page_radio-station .wp-first-item {display: none;}' . "\n";
$css .= '#toplevel_page_radio-station-pro .wp-first-item {display: none;}' . "\n";
// --- reduce the height of the playlist editor area ---
// 2.3.0: also reduce height of override editor area
// 2.3.0: change isset to is_object and property exists check
global $post;
if ( is_object( $post ) && property_exists( $post, 'post_type' ) ) {
if ( ( RADIO_STATION_PLAYLIST_SLUG === $post->post_type )
|| ( RADIO_STATION_OVERRIDE_SLUG === $post->post_type ) ) {
$css .= '.wp-editor-container textarea.wp-editor-area {height: 100px;}' . "\n";
}
}
// --- filter admin styles ---
// 2.5.0: added admin style filter
$css = apply_filters( 'radio_station_admin_styles', $css );
// --- output admin styles ---
// 2.5.6: use wp_kses_post instead of wp_strip_all_tags
// 2.5.6: use radio_station_add_inline_style
// echo '<style>' . wp_kses_post( $css ) . '</style>' . "\n";
radio_station_add_inline_style( 'rs-admin', $css );
}
// --------------------------
// Filter Plugin Action Links
// --------------------------
// 2.4.0.3: filter license activation link for free version on plugin page
// note: this is because Pro is a separate plugin not a replacement one
// add_filter( 'plugin_action_links', 'radio_station_plugin_links', 99, 2 );
/* function radio_station_plugin_links( $links, $file ) {
if ( RADIO_STATION_BASENAME == $file ) {
echo '<span style="display:none;">RS Plugin Links: ' . esc_html( print_r( $links, true ) ) . '</span>';
}
return $links;
} */
add_filter( 'plugin_action_links_' . RADIO_STATION_BASENAME, 'radio_station_plugin_page_links', 20, 2 );
// add_filter( 'network_admin_plugin_action_links_' . RADIO_STATION_BASENAME, , 'radio_station_license_activation_link', 20, 2 );
function radio_station_plugin_page_links( $links, $file ) {
global $radio_station_data;
if ( RADIO_STATION_DEBUG ) {
echo '<span style="display:none;">RS Plugin Links A: ' . esc_html( print_r( $links, true ) ) . '</span>' . "\n";
}
foreach ( $links as $key => $link ) {
// if ( RADIO_STATION_DEBUG ) {
// echo '<span style="display:none;">Plugin Link ' . $key . ': ' . $link . '</span>' . PHP_EOL;
// }
// 2.4.0.6: remove addons link from Free by default
if ( strstr( $link, '-addons' ) ) {
if ( !$radio_station_data['settings']['hasaddons'] ) {
unset( $links[$key] );
}
}
// --- remove activate premium license link from free version ---
// 2.4.0.8: moved handling of this to within Pro
// if ( 'activate-license radio-station' == $key ) {
// if ( !defined( 'RADIO_STATION_PRO_FILE' ) && !$radio_station_data['settings']['hasaddons'] ) {
// unset( $links[$key] );
// }
// }
// --- remove upgrade link if Pro is already installed ---
if ( defined( 'RADIO_STATION_PRO_FILE' ) && strstr( $key, 'upgrade' ) ) {
unset( $links[$key] );
}
}
if ( RADIO_STATION_DEBUG ) {
echo '<span style="display:none;">RS Plugin Links B: ' . esc_html( print_r( $links, true ) ) . '</span>' . "\n";
}
return $links;
}
// ------------------
// === Admin Menu ===
// ------------------
// ------------------------------
// Settings Page Capability Check
// ------------------------------
// (recheck permissions for main menu item click)
add_action( 'admin_init', 'radio_station_settings_cap_check' );
function radio_station_settings_cap_check() {
if ( isset( $_REQUEST['page'] ) && ( RADIO_STATION_SLUG == sanitize_text_field( $_REQUEST['page'] ) ) ) {
$settingscap = apply_filters( 'radio_station_settings_capability', 'manage_options' );
if ( !current_user_can( $settingscap ) ) {
wp_die( esc_html( __( 'You do not have permissions to access that page.', 'radio-station' ) ) );
}
}
}
// --------------------------------
// Add Admin Menu and Submenu Items
// --------------------------------
add_action( 'admin_menu', 'radio_station_add_admin_menus' );
function radio_station_add_admin_menus() {
$icon = plugins_url( 'images/radio-station-icon.png', RADIO_STATION_FILE );
$position = apply_filters( 'radio_station_menu_position', 5 );
$settingscap = apply_filters( 'radio_station_manage_options_capability', 'manage_options' );
$rs = __( 'Radio Station', 'radio-station' );
// ---- main menu item ----
// 2.3.0: set to new plugin admin page (via plugin loader class)
// (added with publish_playlists capability so that other submenu items remain accessible)
add_menu_page( $rs . ' ' . __( 'Settings', 'radio-station' ), $rs, 'publish_playlists', 'radio-station', 'radio_station_settings_page', $icon, $position );
// --- settings submenu item ---
// 2.3.0: added for ease of access to plugin settings
add_options_page( $rs . ' ' . __( 'Settings', 'radio-station' ), $rs, $settingscap, 'radio-station', 'radio_station_settings_page' );
// --- submenu items ---
// 2.3.0: prefix plugin name on page titles (but not menu items)
// 2.3.0: remove add playlist and add override to reduce clutter
// 2.3.0: added actions for adding of other plugin submenu items in position
// 2.5.0: disabled Add Show submenu item to reduce clutter
add_submenu_page( 'radio-station', $rs . ' ' . __( 'Shows', 'radio-station' ), __( 'Shows', 'radio-station' ), 'edit_shows', 'shows' );
// add_submenu_page( 'radio-station', $rs . ' ' . __( 'Add Show', 'radio-station' ), __( 'Add Show', 'radio-station' ), 'publish_shows', 'add-show' );
do_action( 'radio_station_admin_submenu_top' );
add_submenu_page( 'radio-station', $rs . ' ' . __( 'Playlists', 'radio-station' ), __( 'Playlists', 'radio-station' ), 'edit_playlists', 'playlists' );
// add_submenu_page( 'radio-station', $rs . ' ' . __( 'Add Playlist', 'radio-station' ), __( 'Add Playlist', 'radio-station' ), 'publish_playlists', 'add-playlist' );
add_submenu_page( 'radio-station', $rs . ' ' . __( 'Genres', 'radio-station' ), __( 'Genres', 'radio-station' ), 'publish_playlists', 'genres' );
add_submenu_page( 'radio-station', $rs . ' ' . __( 'Schedule Overrides', 'radio-station' ), __( 'Schedule Overrides', 'radio-station' ), 'edit_shows', 'schedule-overrides' );
// add_submenu_page( 'radio-station', $rs . ' ' . __( 'Add Override', 'radio-station' ), __( 'Add Override', 'radio-station' ), 'publish_shows', 'add-override' );
do_action( 'radio_station_admin_submenu_middle' );
// add_submenu_page( 'radio-station', $rs . ' ' . __( 'Hosts', 'radio-station' ), __( 'Hosts', 'radio-station' ), 'edit_hosts', 'hosts' );
// add_submenu_page( 'radio-station', $rs . ' ' . __( 'Producers', 'radio-station' ), __( 'Producers', 'radio-station' ), 'edit_producers', 'producers' );
// 2.3.2: as temporarily disabled, allow enabling export playlists via filter
$export_playlists = apply_filters( 'radio_station_export_playlists', false );
if ( $export_playlists ) {
add_submenu_page( 'radio-station', $rs . ' ' . __( 'Export Playlists', 'radio-station' ), __( 'Export Playlists', 'radio-station' ), $settingscap, 'playlist-export', 'radio_station_playlist_export_page' );
}
// --- import / export shows feature ---
// note: not yet implemented in free version
if ( file_exists( RADIO_STATION_DIR . '/includes/import-export.php' ) ) {
add_submenu_page( 'radio-station', $rs . ' ' . __( 'Import/Export Shows', 'radio-station' ), __( 'Import/Export', 'radio-station' ), 'manage_options', 'import-export-shows', 'radio_station_import_export_show_page' );
}
add_submenu_page( 'radio-station', $rs . ' ' . __( 'Settings', 'radio-station' ), __( 'Settings', 'radio-station' ), $settingscap, 'radio-station', 'radio_station_settings_page' );
// 2.3.0: rename Help page to Documentation
add_submenu_page( 'radio-station', $rs . ' ' . __( 'Documentation', 'radio-station' ), __( 'Help', 'radio-station' ), 'publish_playlists', 'radio-station-docs', 'radio_station_plugin_docs_page' );
do_action( 'radio_station_admin_submenu_bottom' );
// --- hack the submenu global to add post type add/edit URLs ---
global $submenu;
foreach ( $submenu as $i => $menu ) {
if ( 'radio-station' === $i ) {
foreach ( $menu as $j => $item ) {
switch ( $item[2] ) {
case 'add-show':
// 2.3.0: removed capability check as menu cap is already publish_shows
$submenu[$i][$j][2] = 'post-new.php?post_type=' . RADIO_STATION_SHOW_SLUG;
break;
case 'shows':
$submenu[$i][$j][2] = 'edit.php?post_type=' . RADIO_STATION_SHOW_SLUG;
break;
case 'episodes':
if ( defined( 'RADIO_STATION_EPISODE_SLUG' ) ) {
$submenu[$i][$j][2] = 'edit.php?post_type=' . RADIO_STATION_EPISODE_SLUG;
}
break;
case 'playlists':
$submenu[$i][$j][2] = 'edit.php?post_type=' . RADIO_STATION_PLAYLIST_SLUG;
break;
case 'add-playlist':
$submenu[$i][$j][2] = 'post-new.php?post_type=' . RADIO_STATION_PLAYLIST_SLUG;
break;
case 'genres':
$submenu[$i][$j][2] = 'edit-tags.php?taxonomy=' . RADIO_STATION_GENRES_SLUG;
break;
case 'schedule-overrides':
$submenu[$i][$j][2] = 'edit.php?post_type=' . RADIO_STATION_OVERRIDE_SLUG;
break;
case 'add-override':
$submenu[$i][$j][2] = 'post-new.php?post_type=' . RADIO_STATION_OVERRIDE_SLUG;
break;
case 'hosts':
$submenu[$i][$j][2] = 'edit.php?post_type=' . RADIO_STATION_HOST_SLUG;
break;
case 'producers':
$submenu[$i][$j][2] = 'edit.php?post_type=' . RADIO_STATION_PRODUCER_SLUG;
break;
}
}
}
}
}
// -----------------------------------------
// Fix to Expand Main Menu for Submenu Items
// -----------------------------------------
// 2.2.2: added fix for genre taxonomy page and post type editing
// 2.2.8: remove strict in_array checking
add_filter( 'parent_file', 'radio_station_fix_genre_parent', 11 );
function radio_station_fix_genre_parent( $parent_file = '' ) {
global $pagenow, $post;
$post_types = array( RADIO_STATION_SHOW_SLUG, RADIO_STATION_PLAYLIST_SLUG, RADIO_STATION_OVERRIDE_SLUG );
$taxonomies = array( RADIO_STATION_GENRES_SLUG, RADIO_STATION_LANGUAGES_SLUG );
if ( ( 'edit-tags.php' === $pagenow ) && isset( $_GET['taxonomy'] ) && in_array( sanitize_text_field( $_GET['taxonomy'] ), $taxonomies ) ) {
// 2.3.0: also apply to language taxonomy
$parent_file = 'radio-station';
} elseif ( ( 'post.php' === $pagenow ) && in_array( $post->post_type, $post_types ) ) {
$parent_file = 'radio-station';
}
return $parent_file;
}
// -------------------------
// Taxonomy Submenu Item Fix
// -------------------------
// 2.2.2: so genre submenu item link is set to current (bold)
// 2.3.0: change action to admin_enqueue_scripts
add_action( 'admin_enqueue_scripts', 'radio_station_taxonomy_submenu_fix', 11 );
function radio_station_taxonomy_submenu_fix() {
global $pagenow;
if ( ( 'edit-tags.php' === $pagenow ) && isset( $_GET['taxonomy'] ) ) {
$js = '';
if ( RADIO_STATION_GENRES_SLUG == sanitize_text_field( $_GET['taxonomy'] ) ) {
$js = "jQuery('#toplevel_page_radio-station ul li').each(function() {
if (jQuery(this).find('a').attr('href') == 'edit-tags.php?taxonomy=" . esc_js( RADIO_STATION_GENRES_SLUG ) . "') {
jQuery(this).addClass('current').find('a').addClass('current').attr('aria-current', 'page');
}
});";
}
// 2.3.0: add fix for language taxonomy also
if ( RADIO_STATION_LANGUAGES_SLUG == sanitize_text_field( $_GET['taxonomy'] ) ) {
$js = "jQuery('#toplevel_page_radio-station ul li').each(function() {
if (jQuery(this).find('a').attr('href') == 'edit-tags.php?taxonomy=" . esc_js( RADIO_STATION_LANGUAGES_SLUG ) . "') {
jQuery(this).addClass('current').find('a').addClass('current').attr('aria-current', 'page');
}
});";
}
// --- enqueue script inline ---
// 2.3.0: enqueue instead of echoing
if ( '' != $js ) {
// 2.5.0: use radio_station_add_inline_script
radio_station_add_inline_script( 'radio-station-admin', $js );
}
}
}
// -----------------------------------------
// Fix to Redirect Plugin Settings Menu Link
// -----------------------------------------
// 2.3.2: added settings submenu page redirection fix
// 2.5.0: moved to admin file and changed to admin_init hook
add_action( 'admin_init', 'radio_station_settings_page_redirect' );
function radio_station_settings_page_redirect() {
// --- bug out if not plugin settings page ---
if ( !isset( $_REQUEST['page'] ) || ( RADIO_STATION_SLUG != sanitize_text_field( $_REQUEST['page'] ) ) ) {
return;
}
// --- check if link is for options-general.php ---
if ( strstr( filter_var( $_SERVER['REQUEST_URI'], FILTER_SANITIZE_URL ), '/options-general.php' ) ) {
// --- redirect to plugin settings page (admin.php) ---
$url = add_query_arg( 'page', RADIO_STATION_SLUG, admin_url( 'admin.php' ) );
// TODO: maybe use wp_safe_redirect here ?
wp_redirect( $url );
exit;
}
}
// --------------------
// Role Assignment Info
// --------------------
// 2.3.0: added section for upcoming (Pro) role editor feature
add_action( 'radio_station_admin_page_section_permissions_bottom', 'radio_station_role_editor' );
function radio_station_role_editor() {
// 2.3.2: add filter role editor message
$display = apply_filters( 'radio_station_role_editor_message', true );
if ( !$display ) {
return;
}
// --- role assignment message ---
echo '<h3>' . esc_html( __( 'Role Assignments', 'radio-station' ) ) . '</h3>';
echo esc_html( __( 'You can assign a Radio Station role to users through the WordPress User editor.', 'radio-station' ) ) . '<br>';
// --- info regarding Pro role assignment interface ---
// 2.4.0.3: change text to reflect inclusion in Pro
echo esc_html( __( 'Radio Station Pro includes a Role Assignment Interface so you can easily assign Radio Station roles to any user.', 'radio-station' ) ) . '<br>';
// TODO: maybe add picture of role editing interface ?
// --- Pro upgrade link ---
// 2.5.0: add direct upgrade link
$pricing_url = add_query_arg( 'page', 'radio-station-pricing', admin_url( 'admin.php' ) );
echo '<a href="' . esc_url( $pricing_url ) . '" target="_blank">';
echo esc_html( __( 'Upgrade Now', 'radio-station' ) );
echo '</a>';
echo ' | ';
// --- Pro details/pricing link ---
$upgrade_url = radio_station_get_upgrade_url();
echo '<a href="' . esc_url( $upgrade_url ) . '" target="_blank">';
// echo esc_html( __( 'Upgrade to Radio Station Pro', 'radio-station' ) );
echo esc_html( __( 'Find out more about Radio Station Pro', 'radio-station' ) );
echo ' →</a>.';
echo '<br><br>';
}
// -------------------------------
// Display Import/Export Show Page
// -------------------------------
function radio_station_import_export_show_page() {
$importexport = RADIO_STATION_DIR . '/templates/import-export-shows.php';
if ( file_exists( $importexport ) ) {
// --- display the import/export page ---
include $importexport;
// --- enqueue Semenatic UI ---
// 2.3.2: conditional enqueue to be safe
if ( function_exists( 'radio_station_enqueue_semantic' ) ) {
radio_station_enqueue_semantic();
}
}
}
// ------------------------
// Display Plugin Docs Page
// ------------------------
function radio_station_plugin_docs_page() {
// --- show MailChimp signup form ---
echo "<p> </p>";
radio_station_mailchimp_form();
// --- include help file template ---
// include RADIO_STATION_DIR . '/templates/help.php';
// --- include markdown reader ---
include_once RADIO_STATION_DIR . '/reader.php';
$docs = scandir( RADIO_STATION_DIR . '/docs/' );
$docs[] = 'CHANGELOG.md';
foreach ( $docs as $doc ) {
if ( !in_array( $doc, array( '.', '..' ) ) ) {
$id = str_replace( '.md', '', $doc );
echo '<div id="doc-page-' . esc_attr( strtolower( $id ) ) . '" class="doc-page"';
if ( 'index' != $id ) {
echo ' style="display:none;"';
}
echo '>';
// 2.5.6: use wp_kses with allowed HTML
$allowed = radio_station_allowed_html( 'content', 'docs' );
echo wp_kses( radio_station_parse_doc( $id ), $allowed );
echo '</div>' . "\n";
}
}
// 2.5.6: added jquery onclick function to replace onclick attributes
echo "<script>jQuery('.doc-link').on('click',function(){ref = jQuery(this).attr('id').replace('-doc-link',''); radio_load_doc(ref);});
function radio_load_doc(id) {
pages = document.getElementsByClassName('doc-page');
for (i = 0; i < pages.length; i++) {pages[i].style.display = 'none';}
hash = '';
if (id.indexOf('#') > -1) {
parts = id.split('#');
id = parts[0]; hash = parts[1];
} else if (id == 'index') {hash = 'index-top';}
document.getElementById('doc-page-'+id).style.display = 'block';
if (hash != '') {
anchor = document.getElementById(hash);
atop = anchor.offsetTop; /* do not use 'top'! */
window.scrollTo(0, (atop-20));
}
}</script>" . "\n";
echo '<style>.doc-page {padding: 20px 40px 20px 10px;}
.doc-page, .doc-page p {font-size: 14px;}
.doc-page table {padding: 10px; background-color: #F9F9F9; border: 1px solid #CCC; border-radius: 10px;}
.doc-page th {text-align: left; padding: 7px 14px;}
.doc-page td {font-size:16px; padding: 7px 14px;}
.doc-page td a {text-decoration: none; font-weight: bold;}
.doc-page td a:hover {text-decoration: underline;}
h1.docs-heading {font-size: 1.65em; margin-bottom: 1.65em;}
h2.docs-heading {font-size: 1.5em; margin-top: 1.5em;}
h3.docs-heading {font-size: 1.3em;}
h4.docs-heading {font-size: 1.1em;}
</style>' . "\n";
// --- output announcement content ---
radio_station_announcement_content( false );
}
// -----------------------
// Parse Markdown Doc File
// -----------------------
function radio_station_parse_doc( $id ) {
// --- get docs page contents ---
if ( 'CHANGELOG' == $id ) {
$path = RADIO_STATION_DIR . '/CHANGELOG.md';
} else {
$path = RADIO_STATION_DIR . '/docs/' . $id . '.md';
}
$contents = file_get_contents( $path );
// --- strip top level heading to prevent duplicate title ---
$sep = '***';
$backlink = '';
if ( 'index' != $id ) {
// 2.5.6: replace onlick attribute with class
// $backlink = '<alink href="javascript:void(0);" onclick="radio_load_doc(\'index\');">← ';
$backlink = '<alink class="doc-link" id="index-doc-link">← ';
$backlink .= esc_html( __( 'Back to Documentation Index', 'radio-station' ) );
$backlink .= '</a><br>';
}
$contents = str_replace( $sep, $backlink, $contents );
// --- replace relative links ---
$contents = str_replace( '(#', '(./' . $id . '#', $contents );
$contents = str_replace( '.md)', ')', $contents );
$contents = str_replace( '.md#', '#', $contents );
// --- process markdown formatting ---
$formatted = Markdown( $contents );
// --- a # name links to headings ---
for ( $i = 1; $i < 7; $i++ ) {
$tag_start = '<h' . $i . '>';
$tag_end = '</h' . $i . '>';
if ( stristr( $formatted, $tag_start ) ) {
while ( stristr( $formatted, $tag_start ) ) {
$pos = stripos( $formatted, $tag_start );
$pos2 = $pos + strlen( $tag_start );
$before = substr( $formatted, 0, $pos );
$after = substr( $formatted, $pos2, strlen( $formatted ) );
$pos3 = stripos( $after, $tag_end );
$anchor = sanitize_title( substr( $after, 0, $pos3 ) );
$alink = '<a id="' . esc_attr( $anchor ) . '" name="' . esc_attr( $anchor ) . '"></a>';
$newheading = $alink . '<h' . $i . ' class="docs-heading">';
$formatted = $before . $newheading . $after;
}
}
}
// --- replace links with javascript ---
$tag_start = '<a href="./';
$tag_end = '"';
$placeholder = '<alink ';
if ( stristr( $formatted, $tag_start ) ) {
while ( stristr( $formatted, $tag_start ) ) {
$pos = strpos( $formatted, $tag_start );
$before = substr( $formatted, 0, $pos );
$pos = strpos( $formatted, $tag_start ) + strlen( $tag_start );
$after = substr( $formatted, $pos, strlen( $formatted ) );
$pos2 = strpos( $after, $tag_end );
$url = substr( $after, 0, $pos2 );
$url = strtolower( $url );
// 2.5.6: replace onclick with class and id
// $onclick = ' onclick="radio_load_doc(\'' . esc_js( $url ) . '\');';
$class = ' class="doc-link" id="' . esc_attr( $url ) . '-doc-link"';
$after = substr( $after, ($pos2 + 1), strlen( $after ) );
$formatted = $before . $placeholder . $class . $after;
}
}
$formatted = str_replace( '<a href="', '<a target="_blank" href="', $formatted );
if ( 'index' != $id ) {
$formatted .= $backlink . '<br>';
} else {
$formatted = '<a id="index-top" name="index-top"></a>' . $formatted;
}
$formatted = str_replace( '<alink ', '<a ', $formatted );
return $formatted;
}
// ----------------------------
// Display Playlist Export Page
// ----------------------------
// note: this is a legacy export playlist function
// TODO: rewrite playlist export function ?
function radio_station_playlist_export_page() {
global $wpdb;
// first, delete any old exports from the export directory
$dir = RADIO_STATION_DIR . '/export/';
if ( is_dir( $dir ) ) {
$get_contents = opendir( $dir );
while ( $file = readdir( $get_contents ) ) {
if ( '.' !== $file && '..' !== $file ) {
// 2.5.6: use wp_delete_file
// unlink( $dir . $file );
wp_delete_file( $dir . $file );
}
}
closedir( $get_contents );
}
// --- watch for form submission ---
if ( isset( $_POST['export_action'] ) && ( 'station_playlist_export' === sanitize_text_field( $_POST['export_action'] ) ) ) {
// --- validate referrer and nonce field ---
check_admin_referer( 'station_export_valid' );
$start = sanitize_text_field( $_POST['station_export_start_year'] ) . '-';
$start .= sanitize_text_field( $_POST['station_export_start_month'] ) . '-';
$start .= sanitize_text_field( $_POST['station_export_start_day'] );
$start .= ' 00:00:00';
$end = sanitize_text_field( $_POST['station_export_end_year'] ) . '-';
$end .= sanitize_text_field( $_POST['station_export_end_month'] ) . '-';
$end .= sanitize_text_field( $_POST['station_export_end_day'] );
$end .= ' 23:59:59';
// fetch all records that were created between the start and end dates
$sql = "SELECT ID,post_date FROM " . $wpdb->posts . " WHERE post_type = %s AND post_status = 'publish' AND TO_DAYS(post_date) >= TO_DAYS(%s) AND TO_DAYS(post_date) <= TO_DAYS(%s) ORDER BY post_date ASC";
// prepare query before executing
$playlists = $wpdb->get_results( $wpdb->prepare( $sql, array( RADIO_STATION_PLAYLIST_SLUG, $start, $end ) ) );
if ( !$playlists ) {
// 2.5.6: output translated and escaped message
echo esc_html( __( 'No playlists found for this period.', 'radio-station' ) );
}
// fetch the tracks for each playlist from the wp_postmeta table
foreach ( $playlists as $i => $playlist ) {
$songs = get_post_meta( $playlist->ID, 'playlist', true );
// remove any entries that are marked as 'queued'
foreach ( $songs as $j => $entry ) {
if ( 'queued' === $entry['playlist_entry_status'] ) {
unset( $songs[$j] );
}
}
$playlists[$i]->songs = $songs;
}
$output = '';
$date = '';
foreach ( $playlists as $playlist ) {
if ( ! isset( $playlist->post_date ) ) {
continue;
}
$playlist_datetime = explode( ' ', $playlist->post_date );
$playlist_post_date = array_shift( $playlist_datetime );
if ( empty( $date ) || $date !== $playlist_post_date ) {
$date = $playlist_post_date;
$output .= $date . "\n\n";
}
foreach ( $playlist->songs as $song ) {
$output .= $song['playlist_entry_artist'] . ' || ' . $song['playlist_entry_song'] . ' || ' . $song['playlist_entry_album'] . ' || ' . $song['playlist_entry_label'] . "\n";
}
}
// save as file
// TODO: use WP Filesystem for writing
$dir = RADIO_STATION_DIR . '/export/';
$file = $date . '-export.txt';
if ( ! file_exists( $dir ) ) {
wp_mkdir_p( $dir );
}
// TODO: use WP Filesystem for writing
$f = fopen( $dir . $file, 'w' );
fwrite( $f, $output );
fclose( $f );
// display link to file
$url = get_bloginfo( 'url' ) . '/wp-content/plugins/radio-station/tmp/' . $file;
echo '<div id="message" class="updated"><p><strong>';
echo '<a href="' . esc_url( $url ) . '">';
echo esc_html( __( 'Right-click and download this file to save your export', 'radio-station' ) );
echo '</a>';
echo '</strong></p></div>';
}
// display the export page
include RADIO_STATION_DIR . '/templates/playlist-export.php';
}
// ----------------------
// === Update Notices ===
// ----------------------
// -------------------------
// Get Plugin Upgrade Notice
// -------------------------
// 2.3.0: added to get plugin upgrade notice
function radio_station_get_upgrade_notice() {
// --- check updates transient for upgrade notices ---
$notice = false;
$pluginslug = RADIO_STATION_SLUG;
$pluginupdates = get_site_transient( 'update_plugins' );
if ( RADIO_STATION_DEBUG ) {
echo '<span style="display:none;">Update Transient: ' . esc_html( print_r( $pluginupdates, true ) ) . '</span>';
}
// 2.4.0.9: check for object for PHP8
if ( $pluginupdates && is_object( $pluginupdates ) && property_exists( $pluginupdates, 'response' ) ) {
foreach ( $pluginupdates->response as $file => $update ) {
if ( is_object( $update ) && property_exists( $update, 'slug' ) ) {
if ( $update->slug == $pluginslug ) {
if ( property_exists( $update, 'upgrade_notice' ) ) {
// 2.3.3.9: compare new version with installed version
$new_version = $update->new_version;
$version = radio_station_plugin_version();
if ( version_compare( $version, $new_version, '<' ) ) {
// --- parse upgrade notice ---
$notice = $update->upgrade_notice;
$notice = radio_station_parse_upgrade_notice( $notice );
$notice['update_id'] = str_replace( '.', '', $new_version );
if ( property_exists( $update, 'icons' ) && isset( $update->icons['1x'] ) ) {
$notice['icon_url'] = $update->icons['1x'];
}
$notice['plugin_file'] = $file;
break;
}
}
}
}
}
}
if ( RADIO_STATION_DEBUG && $notice ) {
echo '<span style="display:none;">Update Notice: ' . esc_html( print_r( $notice, true ) ) . '</span>';
}
return $notice;
}
// --------------------
// Parse Upgrade Notice
// --------------------
function radio_station_parse_upgrade_notice( $notice ) {
$lines = $content_lines = array();
$notice_url = '';
if ( strstr( $notice, "\n" ) ) {
$contents = explode( "\n", $notice );
} else {
$contents = array( $notice );
}
foreach ( $contents as $content ) {
if ( '' != trim( $content ) ) {
// --- extract link from line ---
if ( strstr( $content, 'http' ) ) {
$pos = strpos( $content, 'http' );
$chunks = str_split( $content, $pos );
unset( $chunks[0] );
$remainder = implode( '', $chunks );
$breaks = array( ' ', '<', "\n", "\r" );
$pos = array();
foreach ( $breaks as $i => $urlbreak ) {
if ( strstr( $remainder, $urlbreak ) ) {
$pos[$i] = strpos( $remainder, $urlbreak );
}
}
if ( count( $pos ) > 0 ) {
$pos = min( $pos );
$chunks = str_split( $remainder, $pos );
$notice_url = $chunks[0];
} else {
$notice_url = $remainder;
}
$content = str_replace( $notice_url, '', $content );
$content = str_replace( '<li>', '', $content );
$content = str_replace( '</li>', '', $content );
}
}
if ( '' != trim( $content ) ) {
$content_lines[] = $content;
if ( !in_array( $content, array( '<ul>', '</ul>' ) ) ) {
$line = str_replace( array( '<li>', '</li>' ), array( '', '' ), $content );
$lines[] = $line;
}
}
}
// --- recombine lines and return ---
$content = implode( "\n", $content_lines );
$notice = array(
'content' => $content,
'lines' => $lines,
'url' => $notice_url,
);
return $notice;
}
// --------------------------
// Plugin Page Update Message
// --------------------------
add_action( 'in_plugin_update_message-' . RADIO_STATION_BASENAME, 'radio_station_plugin_update_message', 10, 2 );
function radio_station_plugin_update_message( $plugin_data, $response ) {
// --- bug out if no update plugins capability ---
if ( !current_user_can( 'update_plugins' ) ) {
return;
}
// --- get upgrade notice ---
$notice = radio_station_get_upgrade_notice();
// --- bug out if no upgrade notice ---
if ( !$notice ) {
return;
}
// --- output update available message ---
echo '<br><b>' . esc_html( __( 'Take a moment to Update for a better experience. In this update', 'radio-station' ) ) . ":</b><br>";
// 2.5.6: added missing index variable $i to foreach
foreach ( $notice['lines'] as $i => $line ) {
// 2.5.0: maybe output link to notice URL
if ( ( '' != $notice['url'] ) && ( 0 == $i ) ) {
// 2.5.6: fix variable notice_url to notice['url']
echo '• <a href="' . esc_url( $notice['url'] ) . '" target="_blank" title="' . esc_attr( __( 'Read full update details.', 'radio-station' ) ) . '">' . esc_html( $line ) . '</a><br>';
} else {
echo '• ' . esc_html( $line ) . '<br>';
}
}
}
// ---------------------------
// Display Admin Update Notice
// ---------------------------
add_action( 'admin_notices', 'radio_station_admin_update_notice' );
function radio_station_admin_update_notice() {
// --- do not display on settings page ---
if ( isset( $_GET['page'] ) && ( 'radio-station' === sanitize_text_field( $_GET['page'] ) ) ) {
return;
}
radio_station_update_notice();
}
// ---------------------------
// Display Admin Update Notice
// ---------------------------
function radio_station_update_notice() {
// --- bug out if no update plugins capability ---
if ( !current_user_can( 'update_plugins' ) ) {
return;
}
// --- get upgrade notice ---
$notice = radio_station_get_upgrade_notice();
// --- bug out if no upgrade notice ---
if ( !$notice ) {
return;
}
// --- ignore if updating now ---
if ( isset( $_GET['action'] ) && ( 'upgrade-plugin' === sanitize_text_field( $_GET['action'] ) )
&& isset( $_GET['plugin'] ) && ( $notice['plugin_file'] === sanitize_text_field( $_GET['plugin'] ) ) ) {
return;
}
// --- bug out if already read ---
$read = get_option( 'radio_station_read_upgrades' );
if ( $read && is_array( $read ) && isset( $read[$notice['update_id']] ) && ( '1' == $read[$notice['update_id']] ) ) {
return;
}
// --- set plugin update URL ---
$update_url = admin_url( 'update.php' ) . '?action=upgrade-plugin&plugin=' . $notice['plugin_file'];
$update_url = wp_nonce_url( $update_url, 'upgrade-plugin_' . $notice['plugin_file'] );
// --- output update available notice ---
echo '<div id="radio-station-update-' . esc_attr( $notice['update_id'] ) . '" class="notice update-nag" style="position:relative;">' . "\n";
echo '<ul style="list-style:none;">' . "\n";
if ( isset( $notice['icon_url'] ) ) {
echo '<li style="display:inline-block; vertical-align:top; margin-right:40px;">' . "\n";
echo '<img src="' . esc_url( $notice['icon_url'] ) . '" style="width:75px; height: 75px;">' . "\n";
echo '</li>' . "\n";
}
echo '<li style="display:inline-block; text-align:center; vertical-align:top; margin-right:40px; line-height:1.8em;">' . "\n";
echo esc_html( __( 'A new version of', 'radio-station' ) ) . '<br>' . "\n";
echo '<b><span style="font-size:1.2em;">' . esc_html( __( 'Radio Station', 'radio-station' ) ) . '</span></b><br>' . "\n";
echo esc_html( __( 'is available.', 'radio-station' ) ) . "\n";
echo '</li>' . "\n";
echo '<li style="display:inline-block; vertical-align:top; margin-right:40px; max-width:600px;">' . "\n";
echo '<b>' . esc_html( __( 'Take a moment to Update for a better experience. In this update', 'radio-station' ) ) . ":</b><br>" . "\n";
echo '<ul style="padding:0; list-style:disc;">' . "\n";
foreach ( $notice['lines'] as $i => $line ) {
if ( ( '' != $notice['url'] ) && ( 0 == $i ) ) {
echo '<li style="text-indent:20px;"><a href="' . esc_url( $notice['url'] ) . '" target="_blank" title="' . esc_attr( __( 'Read full update details.', 'stream-player' ) ) . '">' . esc_html( $line ) . '</li>' . "\n";
} else {
echo '<li style="text-indent:20px;">' . esc_html( $line ) . '</li>' . "\n";
}
}
echo '</ul>' . "\n";
echo '</li>' . "\n";
echo '<li style="display:inline-block; text-align:center; vertical-align:top;">' . "\n";
echo '<a class="button button-primary" href="' . esc_url( $update_url ) . '">' . esc_html( __( 'Update Now', 'radio-station' ) ) . '</a>' . "\n";
if ( '' != $notice['url'] ) {
echo '<br><br>' . "\n";
echo '<a class="button" href="' . esc_url( $notice['url'] ) . '" target="_blank">' . esc_html( __( 'Full Update Details', 'radio-station' ) ) . ' →</a>' . "\n";
}
echo '</li>' . "\n";
echo '</ul>' . "\n";
// --- dismiss notice link ---
// 2.5.8: add nonce to dismissal link
$dismiss_url = add_query_arg( 'action', 'radio_station_notice_dismiss', admin_url( 'admin-ajax.php' ) );
$dismiss_url = add_query_arg( 'upgrade', $notice['update_id'], $dismiss_url );
$dismiss_url = add_query_arg( 'nonce', wp_create_nonce( 'radio_station_notice' ), $dismiss_url );
echo '<div style="position:absolute; top:20px; right: 20px;">' . "\n";
echo '<a href="' . esc_url( $dismiss_url ) . '" target="radio-station-notice-iframe" style="text-decoration:none;">' . "\n";
echo '<span class="dashicons dashicons-dismiss" title="' . esc_attr( __( 'Dismiss this Notice', 'radio-station' ) ) . '"></span></a>' . "\n";
echo '</div>' . "\n";
echo '</div>' . "\n";
// --- notice dismissal iframe (once only) ---
radio_station_admin_notice_iframe();
}
// ---------------------
// Display Plugin Notice
// ---------------------
// 2.3.0: added 2.3.0 template update announcement notice
add_action( 'admin_notices', 'radio_station_notice' );
function radio_station_notice() {
// 2.5.0: check for user capability
if ( !current_user_can( 'update_plugins' ) ) {
return;
}
// --- get latest notice ---
$notices = radio_station_get_notices();
if ( RADIO_STATION_DEBUG ) {
echo '<span style="display:none;">Notices: ' . esc_html( print_r( $notices, true ) ) . '</span>' . PHP_EOL;
}
if ( count( $notices ) < 1 ) {
return;
}
$notice_ids = array_keys( $notices );
$notice_id = max( $notice_ids );
$notice = $notices[$notice_id];
// --- bug out if already read ---
$read = get_option( 'radio_station_read_notices' );
if ( RADIO_STATION_DEBUG ) {
echo '<span style="display:none;">Read Notices: ' . esc_html( print_r( $read, true ) ) . '</span>' . "\n";
echo '<span style="display:none;">Latest Notices: ' . esc_html( print_r( $notice, true ) ) . '</span>' . "\n";
}
if ( $read && isset( $read[$notice_id] ) && ( '1' == $read[$notice_id] ) ) {
return;
}
// --- display plugin notice ---
echo '<div id="radio-station-notice-' . esc_attr( $notice['id'] ) . '" class="notice notice-info" style="position:relative;">' . "\n";
// --- output plugin notice text ---
echo '<ul style="list-style:none;">' . "\n";
// --- plugin icon ---
$icon_url = plugins_url( 'images/radio-station.png', RADIO_STATION_FILE );
echo '<li style="display:inline-block; text-align:center; vertical-align:top; margin-right:40px; line-height:1.8em;">' . "\n";
echo '<img src="' . esc_url( $icon_url ) . '" style="width:75px; height:75px;">' . "\n";
echo '</li>' . "\n";
// --- notice title ---
echo '<li style="display:inline-block; text-align:center; vertical-align:top; margin-right:40px; line-height:1.8em;">' . "\n";
echo '<b><span style="font-size:1.2em;">' . esc_html( __( 'Radio Station', 'radio-station' ) ) . '</span></b><br>' . "\n";
echo '<b>' . esc_html( __( 'Update Notice', 'radio-station' ) ) . '</b>' . "\n";
echo '</li>' . "\n";
// --- notice details ---
echo '<li style="display:inline-block; vertical-align:top; margin-right:40px; font-size:16px; line-height:22px; max-width:600px;">' . "\n";
echo '<div style="margin-bottom:10px;">' . "\n";
echo '<b>' . esc_html( __( 'Thanks for Updating! You can enjoy these improvements now', 'radio-station' ) ) . '</b>:' . "\n";
echo '</div>' . "\n";