/**
 * Theme Name:     Riley Courtwood
 * Author:         Dillan Finney
 * Template:       avada
 * Text Domain:	   riley-courtwood
 * Description:    Custom theme for Riley Courtwood\&#039;s site.
 */

.tribe-events-calendar-list__event-title {
    pointer-events: none;
}

/*Custom function to set the custom field "future" to false if the field "sort_date" is in the future on events. This is done to hide past events from the event page.*/
function update_future_field_for_all_events() {
    // Get all posts of post type 'event' where 'sort_date' exists
    $args = array(
        'post_type'      => 'event',
        'post_status'    => 'publish',
        'meta_key'       => 'sort_date',
        'posts_per_page' => -1, // Get all event posts
    );

    $events = new WP_Query($args);

    if ($events->have_posts()) {
        while ($events->have_posts()) {
            $events->the_post();
            $post_id = get_the_ID();

            // Get the 'sort_date' custom field value
            $sort_date = get_post_meta($post_id, 'sort_date', true);

            // Skip if there's no sort_date
            if (!$sort_date) {
                continue;
            }

            // Compare the sort_date to the current date
            $current_date = current_time('Y-m-d H:i:s');

            // If the sort_date is in the future, set 'future' to true, otherwise false
            if (strtotime($sort_date) > strtotime($current_date)) {
                update_post_meta($post_id, 'future', true);
            } else {
                update_post_meta($post_id, 'future', false);
            }
        }
        // Reset post data after custom query
        wp_reset_postdata();
    }
}

// Schedule the event if it is not already scheduled
function schedule_event_update_check() {
    if (!wp_next_scheduled('update_future_field_cron_hook')) {
        wp_schedule_event(time(), 'hourly', 'update_future_field_cron_hook');
    }
}
add_action('wp', 'schedule_event_update_check');

// Hook the function to the cron event
add_action('update_future_field_cron_hook', 'update_future_field_for_all_events');


