<?php
/**
 * ICS Events archive (CPT and taxonomy) template
 *
 * You can override this template by copying it into your theme at:
 * ics-calendar/archive-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;

if (is_tax()) {
	$qo = get_queried_object();
	$feed_url = home_url($R34ICSProEvent->get_feed_uri_base('category') . '/' . @$qo->slug);
	$title = $qo->name;
	$description = $qo->description;
}
else {
	$feed_url = home_url($R34ICSProEvent->get_feed_uri_base());
	$title = __('Calendar', 'ics-calendar-pro');
	$description = '';
}
$r34icspro_view = get_option('r34icspro_event_cpt_archive_view', 'full');

// Calendar shortcode parameters
$r34icspro_shortcode_params = array(
	'url' => esc_url($feed_url),
	'view' => esc_attr($r34icspro_view),
	'guid' => esc_attr('ics-calendar-' . sha1($feed_url)),
	'eventdl' => true,
	'subscribelink' => true,
	'pastdays' => 365,
	'limitdays' => 1095,
	'jsonld' => true
);

// Customize the calendar shortcode parameters based on the configured view
switch ($r34icspro_view) {
	case 'full':
		$r34icspro_shortcode_params = array_merge($r34icspro_shortcode_params, array(
			'hours' => '0700-1900',
			'fc_height' => 80,
			'fc_tabs' => 'dayGridMonth,timeGridWeek,timeGridDay,listMonth,multiMonthYear',
			'fc_tab_default' => 'dayGridMonth',
			'fc_tabs_mobile' => 'timeGridDay,listMonth,multiMonthYear',
			'fc_tab_default_mobile' => 'multiMonthYear',
			'fc_timegrid_all_hours' => true,
			'fc_business_days' => '0,1,2,3,4,5,6',
		));
		break;
	case 'month':
		$r34icspro_shortcode_params = array_merge($r34icspro_shortcode_params, array(
			//'month_table_list_toggle' => true,
		));
		break;
	default:
		break;
}

// Build the calendar shortcode
$r34icspro_shortcode = '[ics_calendar';
foreach ((array)$r34icspro_shortcode_params as $key => $value) {
	$r34icspro_shortcode .= ' ' . esc_attr($key) . '="' . esc_attr($value) . '"';
}
$r34icspro_shortcode .= ']';

// 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="r34icspro-event--archive">
	
		<header class="r34icspro-event--archive--header">
			<h1 class="r34icspro-event--archive--title">
				<?php echo wp_kses_post($title); ?>
			</h1>
			
			<?php
			if (!empty($description)) {
				?>
				<div class="r34icspro-event--archive--description"><?php echo wp_kses_post($description); ?></div>
				<?php
			}
			?>
		</header>
	
		<div class="r34icspro-event--archive--content">
			<?php
			echo do_shortcode($r34icspro_shortcode);
			
			if (function_exists('r34ics_admin_access') && r34ics_admin_access()) {
				?>
				<p class="ics-calendar"><a href="<?php echo esc_url(admin_url('post-new.php?post_type=r34icspro_event')); ?>" class="subscribe button"><?php esc_html_e('Add an Event', 'ics-calendar-pro'); ?></a></p>
				<?php
			}
			?>
		</div>
	
	</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();
}