<?php
/**
 * ICS Events single event template
 *
 * You can override this template by copying it into your theme at:
 * ics-calendar/single-r34icspro_event.php
 * and changing the code as needed.
 *
 * @version 6.0.0
 */

// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound

// Don't load directly
if (!defined('ABSPATH')) { exit; }

// Redirect to home page if ICS Calendar is paused and we're on the front end
if (!is_admin() && get_option('r34ics_paused')) { wp_safe_redirect(home_url('/')); exit; }

global $R34ICS, $R34ICSProEvent;

$date_format = r34ics_date_format(get_option('date_format'), true);

while (have_posts()) {
	the_post();
	
	if ($event = $R34ICSProEvent->get_parsed_event($post->ID)) {
		
		add_action('wp_head', function() use ($event) {
			echo '<script type="application/ld+json">[' . wp_kses(r34ics_event2jsonld($event, true), 'data') . ']</script>';
		});

		// Block themes
		if (function_exists('wp_is_block_theme') && wp_is_block_theme()) {
			// Get site header HTML
			ob_start();
			@block_header_area();
			$r34icspro_header_html = do_blocks(ob_get_clean());
			
			// Get site footer HTML
			ob_start();
			@block_footer_area();
			$r34icspro_footer_html = do_blocks(ob_get_clean());
			
			// Buffer output for main content HTML
			ob_start();
		}
		
		// Classic themes
		else {
			@get_header();
		}
		?>
		
		<main class="wp-block-group">
		
			<article class="ics-calendar r34icspro-event--single">
			
				<?php
				if (get_option('r34icspro_enable_event_cpt_archive')) {
					?>
					<div class="r34icspro-event--single--breadcrumb">
						<p>
							<a href="<?php echo esc_url(home_url('/' . get_option('r34icspro_event_cpt_slug', 'ics-events'))); ?>"><?php esc_html_e('All Events', 'ics-calendar-pro'); ?></a>
							<?php echo get_the_term_list($post->ID, 'r34icspro_event_category', ' / ', ' / ', ''); ?>
						</p>
					</div>
					<?php
				}
				?>
				
				<header class="r34icspro-event--single--header">
					<h1 class="r34icspro-event--single--title">
						<?php
						// Event title (label)
						echo wp_kses_post(html_entity_decode(str_replace('/', '/<wbr />', $event['label'])) ?: '');
						
						// Append cancelled status
						if (!empty($event['status']) && $event['status'] == 'CANCELLED') {
							echo '<span class="event_status event_status_cancelled">' . esc_html__('Cancelled', 'ics-calendar-pro') . '</span>';
						}
						?>
					</h1>
				</header>
				
				<section class="r34icspro-event--single--content">
				
					<div class="r34icspro-event--single--date-time">
						<?php
						if (!empty($event['rrule'])) {
							echo '<div class="recurrence">' . esc_html__('Next:', 'ics-calendar-pro') . '</div>';
						}
						?>
						<h2 class="r34icspro-event--single--date">
							<?php
							// Date(s)
							if (!empty($event['multiday'])) {
								$day_label = r34ics_multiday_date_label($date_format, $event, $args);
							}
							else {
								$day_label = r34ics_date($date_format, $event['dtstart_date'], $event['tz_start']);
							}
							echo wp_kses_post($day_label ?: '');
							?>
						</h2>
						<?php
						// Time(s)
						if (!empty($event['start'])) {
							?>
							<h3 class="r34icspro-event--single--time">
								<?php
								echo wp_kses_post($event['start'] ?: '');
								if (!empty($event['end'])) { echo ' &#8211; ' . wp_kses_post($event['end'] ?: ''); }
								?>
							</h3>
							<?php
						}
						// Add recurrence information
						if (empty($args['skiprecurrence']) && !empty($event['rrule'])) {
							echo wp_kses_post(r34ics_recurrence_description($event['rrule'], $args['hiderecurrence']) ?: '');
						}
						?>
					</div>
					
					<?php					
					// Description
					if (!empty($event['eventdesc'])) {
						?>
						<div class="r34icspro-event--single--description">
							<?php echo wp_kses_post(r34ics_filter_the_content(r34ics_maybe_make_clickable($event['eventdesc']))); ?>
						</div>
						<?php
					}

					// Attachment(s)
					if (!empty($event['attach'])) {
						?>
						<div class="r34icspro-event--single--attach">
							<?php
							if (strpos($event['attach'], '<img') !== false) {
								echo '<figure class="attach">' . wp_kses_post(strip_tags($event['attach'],'<a><img><div>')) . '</figure>';
							}
							else {
								echo '<div class="attach">' . wp_kses_post(strip_tags($event['attach'],'<a><img><div>')) . '</div>';
							}
							?>
						</div>
						<?php
					}
			
					if (!empty($event['location']) || !empty($event['organizer']) || !empty($event['contact']) || !empty($event['url'])) {
						?>
						<div class="r34icspro-event--single--meta">
							<?php
							// Location
							if (!empty($event['location'])) {
								?>
								<div class="r34icspro-event--single--meta--location">
									<div><?php esc_html_e('Location:', 'ics-calendar-pro'); ?></div>
									<div><?php echo wp_kses_post(r34ics_maybe_make_clickable($event['location']) ?: ''); ?></div>
								</div>
								<?php
							}
							
							// Organizer
							if (!empty($event['organizer'])) {
								?>
								<div class="r34icspro-event--single--meta--organizer">
									<div><?php esc_html_e('Organizer:', 'ics-calendar-pro'); ?></div>
									<div><?php echo wp_kses_post(r34ics_organizer_format($event['organizer']) ?: ''); ?></div>
								</div>
								<?php
							}
							
							// Event link
							if (!empty($event['url'])) {
								?>
								<div class="r34icspro-event--single--meta--link">
									<div><?php esc_html_e('Event URL:', 'ics-calendar-pro'); ?></div>
									<div><?php echo wp_kses_post(r34ics_maybe_make_clickable($event['url']) ?: ''); ?></div>
								</div>
								<?php
							}
							?>
						</div>
						<?php
					}

					if (!empty($event['recurrences'])) {
						?>
						<details class="r34icspro--details--toggle r34icspro--recurrence--details">
							<summary class="r34icspro--details--summary"><?php esc_html_e('All upcoming dates...', 'ics-calendar-pro'); ?></summary>
							<div class="r34icspro--details--content"><?php
								foreach ((array)$event['recurrences'] as $recurrence) {
									echo '<div>' . wp_kses_post(r34ics_date($date_format, $recurrence['start'])) . '</div>';
								}
							?></div>
						</details>
						<?php
					}
					?>
				</section>
								
			</article>

		</main>
		
		<?php
		// Block themes
		if (function_exists('wp_is_block_theme') && wp_is_block_theme()) {
		
			// Write output buffer to variable
			$r34icspro_event_html = do_blocks(ob_get_clean());
			
			// Output page with Blocks
			?>
			<!DOCTYPE html>
			<html <?php language_attributes(); ?>>
			<head>
				<meta charset="<?php bloginfo('charset'); ?>">
				<?php wp_head(); ?>
			</head>
			
			<body <?php body_class(); ?>>
		
				<?php wp_body_open(); ?>
			
				<div class="wp-site-blocks">
				
					<header class="wp-block-template-part site-header">
						<?php echo wp_kses_post($r34icspro_header_html); ?>
					</header>
					
					<?php echo wp_kses_post($r34icspro_event_html); ?>
					
					<footer class="wp-block-template-part site-footer">
						<?php echo wp_kses_post($r34icspro_footer_html); ?>
					</footer>
				
				</div>
			
				<?php wp_footer(); ?>
			
			</body>
			</html>
			<?php
		}
		
		// Classic themes
		else {
			@get_footer();
		}
		
	}
		
}
?>
