<?php
/**
 * Enqueue child styles.
 */
function child_enqueue_styles() {
	wp_enqueue_style( 'child-theme', get_stylesheet_directory_uri() . '/style.css', array(), '1.00.21' );

	wp_enqueue_script( 'bw-oembed', get_stylesheet_directory_uri() . '/assets/js/bw-oembed-wrapper.js', array(), '1.00.00', true );
	wp_enqueue_script( 'bw-list-menu', get_stylesheet_directory_uri() . '/assets/js/bw-icon-list-menu.js', array('jquery'), '1.00.00', true );
	
	wp_enqueue_script( 'bw-date-time-format', get_stylesheet_directory_uri() . '/assets/js/bw-date-time-format.js', array(), '1.00.01', true );
	wp_enqueue_script( 'bw-change-timezones', get_stylesheet_directory_uri() . '/assets/js/bw-change-timezones.js', array('jquery', 'bw-date-time-format'), '1.00.02', true );
}

add_action( 'wp_enqueue_scripts', 'child_enqueue_styles' );

function bw_pre_get_posts( $query ) {
	// show all resources for resource set
	if ( $query->is_tax('set') && $query->is_main_query() ) {
		$query->set( 'posts_per_page', -1 );
	}
	// show free_resources for tag query
	if ( $query->is_tag() && $query->is_main_query() ) {
		$query->set( 'post_type', array('post', 'tribe_events', 'free-resource') );
	}
}
add_action( 'pre_get_posts', 'bw_pre_get_posts', 1);

function bw_add_uts_data ($schedule, $event_id, $before, $after) {
	if (!is_singular('tribe_events') && !is_post_type_archive('tribe_events')) return $schedule;
	
	$event = tribe_get_event($event_id);
	$timezone = new DateTimeZone('UTC');

	if (isset($event->dates->start_utc)) {
		$start_ts_utc = $event->dates->start_utc->getTimestamp();

		$schedule = str_replace(
			'<span class="tribe-event-date-start">',
			'<span class="tribe-event-date-start" data-changetz-timestamp="' . $start_ts_utc . '" data-changetz-format="D, F j, Y @ g:i a T">',
			$schedule
		);
	}

	if (isset($event->dates->end_utc)) {
		$end_ts_utc = $event->dates->end_utc->getTimestamp();

		$schedule = str_replace(
			array(
				'<span class="tribe-event-date-end">',
				'<span class="tribe-event-time">'
			),
			array(
				'<span class="tribe-event-date-end" data-changetz-timestamp="' . $end_ts_utc . '" data-changetz-format="D, F j, Y @ g:i a T">',
				'<span class="tribe-event-time" data-changetz-timestamp="' . $end_ts_utc .  '" data-changetz-format="g:i a T">'
			),
			$schedule
		);
	}
	return $schedule;
}

add_filter( 'tribe_events_event_schedule_details', 'bw_add_uts_data', 100, 4 );


include_once 'blocks/index.php';
include_once 'includes/bw-event-list-shortcode.php';
include_once 'includes/bw-upcoming-event-list-shortcode.php';
include_once 'includes/bw-resource-list-shortcode.php';
include_once 'includes/bw-teacher-list-shortcode.php';
include_once 'includes/bw-teacher-bidirectional-relationship.php';


/* Enable auto-updates of plugins/themes on Flywheel */
add_action(
    'wp_update_plugins',
    function() {
        if (wp_doing_cron() && ! doing_action('wp_maybe_auto_update')) {
            do_action('wp_maybe_auto_update');
        }
    },
    20
);