-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlunacode-display-dust-data.php
More file actions
1330 lines (1136 loc) · 48.5 KB
/
lunacode-display-dust-data.php
File metadata and controls
1330 lines (1136 loc) · 48.5 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
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* LunaCode Display Dust Data
*
* WordPress plugin for displaying Dust API data including camps, art, schedule, and music
* from regional Burning Man events. Provides shortcodes and customizable layouts.
*
*
* @wordpress-plugin
* Plugin Name: LunaCode Display Dust Data
* Description: Display camps, art, schedule, and music served by the Dust API with customizable shortcodes and layouts.
* Version: 0.5.0
* @version 0.5.0
* Requires at least: 4.6
* Requires PHP: 7.0
* Author: Complex Confusion
* @author Complex Confusion
* Author URI: https://lunacode.com
* @link https://lunacode.com
* @package LunaCode\DisplayDustData
* @copyright 2025 LunaCode
* License: GPL v2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* @license GPL-2.0-or-later
* Text Domain: lunacode-display-dust-data
*/
namespace LunaCode;
// WordPress functions
use function add_action, add_options_page, add_settings_field, add_settings_section, add_shortcode, admin_url,
check_ajax_referer, current_user_can, do_settings_sections, esc_attr, esc_html, esc_url, get_option, get_transient,
is_admin, is_wp_error, plugin_dir_url, register_post_type, register_setting, sanitize_text_field, set_transient,
settings_fields, shortcode_atts, submit_button, wp_create_nonce, wp_die, wp_enqueue_script, wp_enqueue_style,
wp_kses_post, wp_localize_script, wp_remote_get, wp_remote_retrieve_body, wp_remote_retrieve_response_code,
wp_send_json_error, wp_send_json_success;
// PHP functions
use function array_keys, array_slice, defined, explode, header, implode, in_array, intval, json_decode, json_last_error, md5,
ob_get_clean, ob_start, str_replace, strcmp, strip_tags, strpos, trim, uasort, ucfirst, uniqid;
// Classes
use WP_Error, DateTime, DateInterval, DateTimeZone;
// Constants
use const ABSPATH, DOING_AJAX, HOUR_IN_SECONDS, JSON_ERROR_NONE;
// Prevent direct access
if (!defined('ABSPATH')) {
wp_die('Direct access is not allowed. Return through the website; stop with the hackery.');
}
class DisplayDustData {
const PLUGIN_VERSION = '0.5.0';
const IMAGE_BASE_URL = 'https://data.dust.events/';
const API_BASE_URL = 'https://data.dust.events/';
public function __construct() {
add_action('init', array($this, 'init'));
add_action('wp_enqueue_scripts', array($this, 'enqueue_scripts'));
add_shortcode('dust_camps', array($this, 'display_camps_shortcode'));
add_shortcode('dust_art', array($this, 'display_art_shortcode'));
add_shortcode('dust_schedule', array($this, 'display_schedule_shortcode'));
add_shortcode('dust_music', array($this, 'display_music_shortcode'));
add_shortcode('dust_schedule_ics_button', array($this, 'display_ics_button_shortcode'));
add_shortcode('dust_schedule_csv_button', array($this, 'display_csv_button_shortcode'));
// Add admin menu for settings
add_action('admin_menu', array($this, 'add_admin_menu'));
add_action('admin_init', array($this, 'settings_init'));
// AJAX handlers - public read-only data from Dust API (no authentication required)
// @security-ignore CWE-285: These endpoints access public APIs that do not require authentication
add_action('wp_ajax_get_dust_data', array($this, 'ajax_get_data'));
add_action('wp_ajax_nopriv_get_dust_data', array($this, 'ajax_get_data'));
add_action('wp_ajax_export_schedule_ics', array($this, 'export_schedule_ics'));
add_action('wp_ajax_nopriv_export_schedule_ics', array($this, 'export_schedule_ics'));
add_action('wp_ajax_export_schedule_csv', array($this, 'export_schedule_csv'));
add_action('wp_ajax_nopriv_export_schedule_csv', array($this, 'export_schedule_csv'));
}
public function init() {
// Register custom post types for caching only if needed
if (is_admin() || (defined('DOING_AJAX') && DOING_AJAX)) {
$types = array('camp', 'art', 'schedule', 'music');
foreach ($types as $type) {
register_post_type('dust_' . $type, array(
'labels' => array(
'name' => 'Dust ' . ucfirst($type),
'singular_name' => 'Dust ' . ucfirst($type)
),
'public' => false,
'show_in_admin' => true,
'supports' => array('title', 'editor', 'thumbnail', 'custom-fields')
));
}
}
}
public function enqueue_scripts() {
wp_enqueue_script('jquery');
wp_enqueue_script('dust-events-js', plugin_dir_url(__FILE__) . 'lunacode-display-dust-data.js', array('jquery'), self::PLUGIN_VERSION, true);
wp_enqueue_script('dust-ics-export', plugin_dir_url(__FILE__) . 'schedule-ics-button.js', array('jquery'), self::PLUGIN_VERSION, true);
wp_enqueue_style('dust-events-css', plugin_dir_url(__FILE__) . 'lunacode-display-dust-data.css', array(), self::PLUGIN_VERSION);
wp_localize_script('dust-events-js', 'dust_events_ajax', array(
'ajax_url' => admin_url('admin-ajax.php'),
'nonce' => wp_create_nonce('dust_events_nonce')
));
}
public function add_admin_menu() {
add_options_page(
'LunaCode Display Dust Data Settings',
'LunaCode Display Dust Data',
'manage_options',
'dust-events',
array($this, 'options_page')
);
}
public function settings_init() {
register_setting('dust_events', 'dust_events_event_name');
register_setting('dust_events', 'dust_events_timezone');
add_settings_section(
'dust_events_section',
'API Configuration',
array($this, 'settings_section_callback'),
'dust_events'
);
add_settings_field(
'dust_events_event_name',
'Event Name (your-unique-name)',
array($this, 'event_name_render'),
'dust_events',
'dust_events_section'
);
add_settings_field(
'dust_events_timezone',
'Event Timezone',
array($this, 'timezone_render'),
'dust_events',
'dust_events_section'
);
}
public function settings_section_callback() {
echo 'Enter Dust\'s unique name for your regional burn:';
}
public function event_name_render() {
$event_name = get_option('dust_events_event_name');
echo '<input type="text" name="dust_events_event_name" value="' . esc_attr($event_name) . '" />';
echo '<p class="description">This is the unique name used to register your regional burn in Dust.</p>';
}
public function timezone_render() {
$wp_timezone = wp_timezone_string();
$selected_timezone = get_option('dust_events_timezone', $wp_timezone);
echo '<select name="dust_events_timezone">';
echo wp_timezone_choice($selected_timezone);
echo '</select>';
echo '<p class="description">Timezone containing your regional burn. Used for calendar exports.</p>';
}
public function options_page() {
if (!current_user_can('manage_options')) {
return;
}
?>
<form action='options.php' method='post'>
<h2>LunaCode Display Dust Data Settings</h2>
<?php
settings_fields('dust_events');
do_settings_sections('dust_events');
submit_button();
?>
</form>
<?php
}
/**
* Fetch data from API
*
* @security-note This accesses the public Dust Events API which requires no authentication.
* @security-ignore CWE-285: No authentication required for public API endpoints
*
* @param string $type 'camps'|'art'|'schedule'|'music'
* @param string|null $event_name
* @return array|\WP_Error
*/
public static function get_data($type, $event_name = null) {
// Validate type parameter
$allowed_types = array('camps', 'art', 'schedule', 'music');
if (!in_array($type, $allowed_types, true)) {
return new WP_Error('invalid_type', 'Invalid API endpoint specified');
}
if (!$event_name) {
$event_name = get_option('dust_events_event_name');
}
if (!$event_name) {
return new WP_Error('no_event_name', 'No event name configured');
}
$api_url = self::API_BASE_URL . $event_name . '/' . $type . '.json';
// Check for cached data (cache for 1 hour)
$cache_key = 'dust_' . $type . '_' . md5($event_name);
$cached_data = get_transient($cache_key);
if (false !== $cached_data) {
return $cached_data;
}
// Fetch data from API
$response = wp_remote_get($api_url, array(
'timeout' => 15,
'user-agent' => 'LunaCode Display Dust Data WordPress Plugin'
));
if (is_wp_error($response)) {
return $response;
}
$response_code = wp_remote_retrieve_response_code($response);
if (200 !== $response_code) {
return new WP_Error('api_error', 'API returned status code: ' . $response_code);
}
$body = wp_remote_retrieve_body($response);
$data = json_decode($body, true);
if (json_last_error() !== JSON_ERROR_NONE) {
return new WP_Error('json_error', 'Invalid JSON response');
}
// Sort data based on type
self::sort_data($data, $type);
// Cache the data
set_transient($cache_key, $data, HOUR_IN_SECONDS);
return $data;
}
/**
* Sort data according to its type:
* camps and art get sorted by their name;
* schedule (events) and music get sorted by their camp and then their name
*
* @param array $data
* @param string $type 'camps'|'art'|'schedule'|'music'
* @return void
*/
private static function sort_data(&$data, $type) {
if ('camps' === $type || 'art' === $type) {
// Sort by name
uasort($data, function($a, $b) {
$name_a = (string)($a['name'] ?? '');
$name_b = (string)($b['name'] ?? '');
return strcmp($name_a, $name_b);
});
} elseif ('schedule' === $type || 'music' === $type) {
// Sort by camp, then by title
uasort($data, function($a, $b) {
$camp_a = (string)($a['camp'] ?? $a['hosted_by_camp'] ?? '');
$camp_b = (string)($b['camp'] ?? $b['hosted_by_camp'] ?? '');
$camp_cmp = strcmp($camp_a, $camp_b);
if (0 === $camp_cmp) {
$title_a = (string)($a['title'] ?? '');
$title_b = (string)($b['title'] ?? '');
return strcmp($title_a, $title_b);
}
return $camp_cmp;
});
}
}
/**
* Get location on the map as array of coordinates
*
* @param string $pin_string
* @return array|null
*/
private static function parse_pin_coordinates($pin_string) {
if (empty($pin_string)) {
return null;
}
$pin_data = json_decode($pin_string, true);
if (json_last_error() !== JSON_ERROR_NONE) {
return null;
}
return $pin_data;
}
/**
* Get complete image URL
*
* Converts relative image paths to absolute URLs by prepending the base URL.
* Prevents duplicate base URLs from caching or multiple processing
* by checking if the path is already a complete URL.
*
* @param string $image_path Relative path or complete URL
* @return string|null Complete image URL or null if empty
*/
private static function get_image_url($image_path) {
if (empty($image_path)) {
return null;
}
if (strpos($image_path, '://') !== false) {
return $image_path;
}
return self::IMAGE_BASE_URL . $image_path;
}
/**
* Format description as paragraphs
*
* @param string $description
* @return string
*/
private static function format_description($description) {
if (empty($description)) {
return '';
}
$paragraphs = explode("\n", trim($description));
$formatted_paragraphs = array();
foreach ($paragraphs as $paragraph) {
$paragraph = trim($paragraph);
if (!empty($paragraph)) {
$formatted_paragraphs[] = '<p>' . wp_kses_post($paragraph) . '</p>';
}
}
return implode('', $formatted_paragraphs);
}
/**
* Format categories as comma-separated list
*
* @param array $item
* @param string $type
* @return string
*/
private static function format_categories($item, $type) {
$categories = array();
if ('schedule' === $type && isset($item['event_type']['label'])) {
$categories[] = $item['event_type']['label'];
} elseif ('camps' === $type && isset($item['camp_type'])) {
$categories[] = $item['camp_type'];
} elseif ('art' === $type && isset($item['art_type'])) {
$categories[] = $item['art_type'];
}
return !empty($categories) ? implode(', ', $categories) : '';
}
/**
* AJAX handler for getting data
*
* @return void
*/
public function ajax_get_data() {
check_ajax_referer('dust_events_nonce', 'nonce');
$type = sanitize_text_field($_POST['type']);
$data = self::get_data($type);
if (is_wp_error($data)) {
wp_send_json_error($data->get_error_message());
}
wp_send_json_success($data);
}
/**
* Shortcode handlers for each data type
*/
public function display_camps_shortcode($atts, $content = null) {
return $this->display_shortcode($atts, $content, 'dust_camps', 'camps');
}
public function display_art_shortcode($atts, $content = null) {
return $this->display_shortcode($atts, $content, 'dust_art', 'art');
}
public function display_schedule_shortcode($atts, $content = null) {
return $this->display_shortcode($atts, $content, 'dust_schedule', 'schedule');
}
public function display_music_shortcode($atts, $content = null) {
return $this->display_shortcode($atts, $content, 'dust_music', 'music');
}
/**
* Shortcode for ICS export button
*/
public function display_ics_button_shortcode($atts, $content = null) {
$atts = shortcode_atts(array(
'text' => '',
'event_name' => ''
), $atts, 'dust_schedule_ics_button');
return self::render_ics_button($atts['event_name'], $atts['text']);
}
/**
* Shortcode for CSV export button
*/
public function display_csv_button_shortcode($atts, $content = null) {
$atts = shortcode_atts(array(
'text' => '',
'event_name' => ''
), $atts, 'dust_schedule_csv_button');
return self::render_csv_button($atts['event_name'], $atts['text']);
}
/**
* Allow a shortcode to display data
* Usage: [dust_camps], [dust_art], [dust_schedule], [dust_music]
*
* @param array $atts Shortcode attributes
* @param string|null $content Shortcode content
* @param string $tag Shortcode tag name
* @return string HTML output
*/
private function display_shortcode($atts, $content = null, $tag = '', $type = '') {
$atts = shortcode_atts(array(
'event_name' => '',
'layout' => 'grid',
'show_coordinates' => 'false',
'per_page' => -1,
'show_images' => 'true',
'show_export_buttons' => 'false',
'show_export_button' => 'false', // backwards compatibility
'display' => 'false'
), $atts, $tag);
// Backwards compatibility: if old parameter is used, use it
if ('true' === $atts['show_export_button'] && 'false' === $atts['show_export_buttons']) {
$atts['show_export_buttons'] = 'true';
}
$event_name = !empty($atts['event_name']) ? $atts['event_name'] : get_option('dust_events_event_name');
if (empty($event_name)) {
return '<p>Please configure the event name in the plugin settings.</p>';
}
$data = self::get_data($type, $event_name);
if (is_wp_error($data)) {
return '<p>Error loading ' . $type . ' data: ' . esc_html($data->get_error_message()) . '</p>';
}
if (empty($data)) {
return '<p>No ' . $type . ' found.</p>';
}
// Limit results if specified
if ($atts['per_page'] > 0) {
$data = array_slice($data, 0, intval($atts['per_page']));
}
ob_start();
if ('schedule' === $type && 'tabs' === $atts['display']) {
self::render_schedule_tabs($data, $atts, $event_name);
} else {
// Show export buttons for schedule if requested
if ('schedule' === $type) {
$export_buttons = self::get_export_buttons($atts['show_export_buttons'], $event_name);
if (!empty($export_buttons)) {
echo '<div class="lunacode-export-buttons">' . $export_buttons . '</div>';
}
}
self::render_data($data, $atts, $type);
}
return ob_get_clean();
}
/**
* Output an entire list of Burn items as HTML
*
* @param array[] $data Array of item arrays
* @param array $options
* @param string $type 'camps'|'art'|'schedule'|'music'
* @param string $tab_context 'repeating' or 'day' for schedule tabs
* @return void
*/
public static function render_data($data, $options = array(), $type = 'camps', $tab_context = 'day') {
$layout = isset($options['layout']) ? $options['layout'] : 'grid';
$show_coordinates = isset($options['show_coordinates']) && $options['show_coordinates'] === 'true';
$show_images = isset($options['show_images']) && $options['show_images'] === 'true';
echo '<div class="dust-' . esc_attr($type) . '-container dust-' . esc_attr($type) . '-' . esc_attr($layout) . '">';
foreach ($data as $item) {
self::render_single_item($item, $show_coordinates, $show_images, $type, $data, $tab_context);
}
echo '</div>';
}
/**
* Render single item
*
* @param array $item
* @param boolean $show_coordinates
* @param boolean $show_images
* @param string $type
* @param array $all_data Optional: all data for repeat checking
* @param string $tab_context 'repeating' or 'day' for schedule tabs
* @return void
*/
public static function render_single_item($item, $show_coordinates = false, $show_images = true, $type = 'camps', $all_data = null, $tab_context = 'day') {
echo '<article class="dust-' . esc_attr($type) . '-item" data-uid="' . esc_attr($item['uid']) . '" role="article" tabindex="0">';
if ('camps' === $type || 'art' === $type) {
self::render_camps_art($item, $show_coordinates, $show_images, $type);
} elseif ('schedule' === $type) {
self::render_schedule($item, $show_images, $all_data, $tab_context);
} elseif ('music' === $type) {
self::render_music($item);
}
echo '</article>';
}
/**
* Output either camps or art as HTML
*
* @param array $item
* @param bool $show_coordinates
* @param bool $show_images
* @param string $type 'camps'|'art'
* @return void
*/
private static function render_camps_art($item, $show_coordinates, $show_images, $type) {
$name = $item['name'];
$description = self::format_description($item['description']);
// Handle images differently for art vs camps
$image_url = null;
if ($show_images) {
if ('art' === $type && isset($item['images']) && !empty($item['images'])) {
$image_url = $item['images'][0]['thumbnail_url'];
} elseif (isset($item['imageUrl'])) {
$image_url = self::get_image_url($item['imageUrl']);
}
}
$pin_data = isset($item['pin']) ? self::parse_pin_coordinates($item['pin']) : null;
if ($image_url) {
echo '<div class="dust-' . esc_attr($type) . '-image">';
echo '<img src="' . esc_url($image_url) . '" alt="" role="presentation" loading="lazy" />';
echo '</div>';
}
echo '<div class="dust-' . esc_attr($type) . '-content">';
echo '<h3 class="dust-' . esc_attr($type) . '-name">' . esc_html($name) . '</h3>';
if ('art' === $type && isset($item['artist'])) {
echo '<div class="dust-item-field"><strong>Artist:</strong> ' . esc_html($item['artist']) . '</div>';
}
$categories = self::format_categories($item, $type);
if (!empty($categories)) {
echo '<div class="dust-item-field"><strong>Categories:</strong> ' . esc_html($categories) . '</div>';
}
if (!empty($description)) {
echo '<div class="dust-' . esc_attr($type) . '-description">' . $description . '</div>';
}
if ($show_coordinates && $pin_data) {
echo '<div class="dust-' . esc_attr($type) . '-coordinates"><strong>Coordinates:</strong> ';
if (isset($pin_data['lat'], $pin_data['lng'])) {
echo 'GPS - Lat: ' . esc_html($pin_data['lat']) . ', Lng: ' . esc_html($pin_data['lng']);
} elseif (isset($pin_data['x'], $pin_data['y'])) {
echo 'Map Position - X: ' . esc_html($pin_data['x']) . ', Y: ' . esc_html($pin_data['y']);
}
echo '</div>';
}
echo '</div>';
}
/**
* Render schedule with tabs
*/
private static function render_schedule_tabs($data, $options, $event_name) {
$tabs = self::organize_schedule_by_days($data);
$container_id = 'dust-schedule-tabs-' . uniqid();
echo '<div class="dust-schedule-tabs-container" id="' . esc_attr($container_id) . '" data-event="' . esc_attr($event_name) . '">';
// Export buttons if requested
if (isset($options['show_export_buttons'])) {
$export_buttons = self::get_export_buttons($options['show_export_buttons'], $event_name);
if (!empty($export_buttons)) {
echo '<div class="lunacode-export-buttons">' . $export_buttons . '</div>';
}
}
// Tab navigation
echo '<div class="dust-schedule-tab-nav" role="tablist" aria-label="Schedule by day">';
$first = true;
foreach ($tabs as $tab_key => $tab_data) {
$tab_id = $container_id . '-tab-' . $tab_key;
$panel_id = $container_id . '-panel-' . $tab_key;
$selected = $first ? 'true' : 'false';
echo '<button class="dust-schedule-tab-btn' . ($first ? ' active' : '') . '" '
. 'role="tab" '
. 'aria-selected="' . $selected . '" '
. 'aria-controls="' . esc_attr($panel_id) . '" '
. 'id="' . esc_attr($tab_id) . '" '
. 'data-tab="' . esc_attr($tab_key) . '" '
. 'tabindex="' . ($first ? '0' : '-1') . '">'
. esc_html($tab_data['label']) . '</button>';
$first = false;
}
echo '</div>';
// Tab content
echo '<div class="dust-schedule-tab-content">';
$first = true;
foreach ($tabs as $tab_key => $tab_data) {
$tab_id = $container_id . '-tab-' . $tab_key;
$panel_id = $container_id . '-panel-' . $tab_key;
$context = ($tab_key === 'repeating') ? 'repeating' : 'day';
echo '<div class="dust-schedule-tab-pane' . ($first ? ' active' : '') . '" '
. 'role="tabpanel" '
. 'aria-labelledby="' . esc_attr($tab_id) . '" '
. 'id="' . esc_attr($panel_id) . '" '
. 'data-tab="' . esc_attr($tab_key) . '" '
. 'tabindex="0" '
. 'style="display: ' . ($first ? 'block' : 'none') . ';">';
self::render_data($tab_data['events'], $options, 'schedule', $context);
echo '</div>';
$first = false;
}
echo '</div>';
echo '</div>';
}
/**
* Organize schedule data by days
*/
private static function organize_schedule_by_days($data) {
$tabs = array();
$everyday_events = array();
$day_events = array();
$title_day_count = array();
// First pass: count how many different days each event title appears on
foreach ($data as $event) {
$title = $event['title'] ?? '';
$day = $event['day'] ?? '';
if (!empty($title) && !empty($day)) {
if (!isset($title_day_count[$title])) {
$title_day_count[$title] = array();
}
$title_day_count[$title][$day] = true;
}
}
// Second pass: categorize events
foreach ($data as $event) {
$title = $event['title'] ?? '';
$day = $event['day'] ?? '';
// Check if it's an everyday event (appears on 3+ different days or has daily keywords)
$is_everyday = false;
if (!empty($title)) {
$daily_keywords = array('daily', 'every day', 'everyday', 'all days');
$title_lower = strtolower($title);
$desc_lower = strtolower($event['description'] ?? '');
foreach ($daily_keywords as $keyword) {
if (strpos($title_lower, $keyword) !== false || strpos($desc_lower, $keyword) !== false) {
$is_everyday = true;
break;
}
}
// Also check if this title appears on 2+ different days (multi-day events)
if (!$is_everyday && isset($title_day_count[$title]) && count($title_day_count[$title]) >= 2) {
$is_everyday = true;
}
}
if ($is_everyday) {
$everyday_events[] = $event;
}
if (!empty($day)) {
if (!isset($day_events[$day])) {
$day_events[$day] = array();
}
$day_events[$day][] = $event;
}
}
// Add "Repeating" tab if there are everyday events
if (!empty($everyday_events)) {
$tabs['repeating'] = array(
'label' => 'Repeating',
'events' => self::sort_schedule_events($everyday_events)
);
}
// Process day events and create tabs
$day_counts = array();
foreach ($day_events as $day => $events) {
$day_counts[$day] = ($day_counts[$day] ?? 0) + 1;
}
foreach ($day_events as $day => $events) {
$tab_key = strtolower(str_replace(' ', '_', $day));
// Add date suffix if multiple occurrences of same day
$label = $day;
if ($day_counts[$day] > 1) {
// Extract date from first event if available
$first_event = reset($events);
if (isset($first_event['occurrence']['start_time'])) {
$date = new DateTime($first_event['occurrence']['start_time']);
$label .= ' ' . $date->format('n/j');
}
}
$tabs[$tab_key] = array(
'label' => $label,
'events' => self::sort_schedule_events($events)
);
}
return $tabs;
}
/**
* Sort schedule events: all-day first, then by start time, then by name
*/
private static function sort_schedule_events($events) {
uasort($events, function($a, $b) {
$a_occurrence = $a['occurrence'] ?? array();
$b_occurrence = $b['occurrence'] ?? array();
// Check if all-day events
$a_all_day = isset($a_occurrence['all_day']) && $a_occurrence['all_day'];
$b_all_day = isset($b_occurrence['all_day']) && $b_occurrence['all_day'];
if ($a_all_day && !$b_all_day) return -1;
if (!$a_all_day && $b_all_day) return 1;
// Sort by start time
$a_time = $a_occurrence['start_time'] ?? '';
$b_time = $b_occurrence['start_time'] ?? '';
if ($a_time && $b_time) {
$time_cmp = strcmp($a_time, $b_time);
if (0 !== $time_cmp) return $time_cmp;
}
// Sort by title
return strcmp($a['title'] ?? '', $b['title'] ?? '');
});
return $events;
}
/**
* Get the days an event repeats on (excluding the current day)
*
* @param string $title Event title
* @param string $current_day Current day
* @param array $all_data All schedule data
* @return array Array of days the event repeats on
*/
public static function get_repeat_days($title, $current_day, $all_data) {
static $repeat_cache = array();
$cache_key = md5($title . '|' . $current_day);
if (isset($repeat_cache[$cache_key])) {
return $repeat_cache[$cache_key];
}
$repeat_days = array();
foreach ($all_data as $event) {
if ($event['title'] === $title && !empty($event['day']) && $event['day'] !== $current_day) {
$repeat_days[$event['day']] = true;
}
}
$result = array_keys($repeat_days);
sort($result);
$repeat_cache[$cache_key] = $result;
return $result;
}
/**
* Get all days an event repeats on (including all instances)
*
* @param string $title Event title
* @param array $all_data All schedule data
* @return array Array of all days the event appears on
*/
public static function get_all_repeat_days($title, $all_data) {
static $all_days_cache = array();
if (isset($all_days_cache[$title])) {
return $all_days_cache[$title];
}
$day_dates = array();
foreach ($all_data as $event) {
if ($event['title'] === $title && !empty($event['day'])) {
$day_dates[$event['day']] = $event['occurrence']['start_time'] ?? '';
}
}
$result = array_keys($day_dates);
usort($result, function($a, $b) use ($day_dates) {
return strcmp($day_dates[$a], $day_dates[$b]);
});
$all_days_cache[$title] = $result;
return $result;
}
/**
* Output schedule items (events) as HTML
*
* @param array $item
* @param bool $show_images
* @param array $all_data Optional: all schedule data to check for repeats
* @param string $tab_context 'repeating' or 'day'
* @return void
*/
public static function render_schedule($item, $show_images, $all_data = null, $tab_context = 'day') {
$title = $item['title'];
$description = self::format_description($item['description']);
$camp = isset($item['camp']) ? $item['camp'] : '';
$location = isset($item['location']) ? $item['location'] : '';
$day = isset($item['day']) ? $item['day'] : '';
$occurrence = isset($item['occurrence']) ? $item['occurrence'] : array();
$image_url = $show_images && isset($item['imageUrl']) ? self::get_image_url($item['imageUrl']) : null;
if ($image_url) {
echo '<div class="dust-schedule-image">';
echo '<img src="' . esc_url($image_url) . '" alt="" role="presentation" loading="lazy" />';
echo '</div>';
}
echo '<div class="dust-schedule-content">';
echo '<h3 class="dust-schedule-title">' . esc_html($title) . '</h3>';
if ($camp) echo '<div class="dust-item-field"><strong>Camp:</strong> ' . esc_html($camp) . '</div>';
if ($location) echo '<div class="dust-item-field"><strong>Location:</strong> ' . esc_html($location) . '</div>';
$categories = self::format_categories($item, 'schedule');
if (!empty($categories)) {
echo '<div class="dust-item-field"><strong>Categories:</strong> ' . esc_html($categories) . '</div>';
}
if ($day) {
$day_text = esc_html($day);
if ($all_data && !empty($title)) {
if ('repeating' === $tab_context) {
// On Repeating tab: show "Repeats on [list of all days]"
$all_days = self::get_all_repeat_days($title, $all_data);
if (count($all_days) > 1) {
$day_text = 'Repeats on ' . esc_html(implode(', ', $all_days));
}
} else {
// On day tabs: show "Thursday (repeats on Friday, Saturday, Sunday)"
$all_days = self::get_all_repeat_days($title, $all_data);
if (count($all_days) > 1) {
$other_days = array_diff($all_days, array($day));
if (!empty($other_days)) {
$day_text .= ' (repeats on ' . esc_html(implode(', ', $other_days)) . ')';
}
}
}
}
echo '<div class="dust-item-field"><strong>Day:</strong> ' . $day_text . '</div>';
}
if (isset($occurrence['long'])) {
echo '<div class="dust-item-field"><strong>Time:</strong> ' . esc_html($occurrence['long']) . '</div>';
}
if (!empty($description)) {
echo '<div class="dust-schedule-description">' . $description . '</div>';
}
echo '</div>';
}
/**
* Output Music items (musical events or "parties") as HTML
*
* @param array $item
* @return void
*/
private static function render_music($item) {
$title = $item['title'];
$camp = isset($item['camp']) ? $item['camp'] : '';
$location = isset($item['location']) ? $item['location'] : '';
$day = isset($item['day']) ? $item['day'] : '';
$occurrence = isset($item['occurrence']) ? $item['occurrence'] : array();
echo '<div class="dust-music-content">';
echo '<h3 class="dust-music-title">' . esc_html($title) . '</h3>';
if ($camp) echo '<div class="dust-item-field"><strong>Camp:</strong> ' . esc_html($camp) . '</div>';
if ($location) echo '<div class="dust-item-field"><strong>Location:</strong> ' . esc_html($location) . '</div>';
if ($day) echo '<div class="dust-item-field"><strong>Day:</strong> ' . esc_html($day) . '</div>';
if (isset($occurrence['who'])) {
echo '<div class="dust-item-field"><strong>Artist:</strong> ' . esc_html($occurrence['who']) . '</div>';
}
if (isset($occurrence['long'])) {
echo '<div class="dust-item-field"><strong>Time:</strong> ' . esc_html($occurrence['long']) . '</div>';
}
echo '</div>';
}
/**
* Export schedule as CSV file
*/
public function export_schedule_csv() {
check_ajax_referer('dust_events_nonce', 'nonce');
$event_name = sanitize_text_field($_POST['event_name'] ?? get_option('dust_events_event_name'));
$data = self::get_data('schedule', $event_name);
if (is_wp_error($data)) {
wp_die('Error loading schedule data');
}
$csv_content = $this->generate_csv($data);
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename="' . esc_attr($event_name) . '-schedule.csv"');
echo $csv_content;
wp_die();
}
private function generate_csv($schedule_data) {
$output = fopen('php://temp', 'r+');
if (false === $output) {
wp_die('Error creating CSV file');
}
// CSV headers
fputcsv($output, array('Title', 'Camp', 'Location', 'Day', 'Start Time', 'End Time', 'Short Time', 'Long Time', 'Brief Time', 'Image URL', 'Categories', 'Description'));
foreach ($schedule_data as $event) {
$row = array(
$event['title'] ?? '',
$event['camp'] ?? '',
$event['location'] ?? '',
$event['day'] ?? '',
isset($event['occurrence']['start_time']) ? $event['occurrence']['start_time'] : '',
isset($event['occurrence']['end_time']) ? $event['occurrence']['end_time'] : '',
isset($event['occurrence']['short']) ? $event['occurrence']['short'] : '',
isset($event['occurrence']['long']) ? $event['occurrence']['long'] : '',
isset($event['occurrence']['brief']) ? $event['occurrence']['brief'] : '',
isset($event['imageUrl']) ? self::get_image_url($event['imageUrl']) : '',
isset($event['event_type']['label']) ? $event['event_type']['label'] : '',
strip_tags($event['description'] ?? '')
);
fputcsv($output, $row);
}
rewind($output);
$csv_content = stream_get_contents($output);
fclose($output);
return $csv_content;
}