<?php
/**
 * Template Name: goodmeet Template
 *
 * This template is used to display the goodmeet content.
 *
 * @package Meeting_Hub
 * @since 1.0.0
 */

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

$goodmeet_serialize_options    = get_post_meta( get_the_ID(), 'goodmeet__meeting_settings', true );
$goodmeet_meeting_description  = get_post_meta( get_the_ID(), 'meeting_description', true );
$goodmeet_integration_settings = get_option( 'goodmeet_integration_settings', true );
$goodmeet_options              = maybe_unserialize( $goodmeet_serialize_options );
$goodmeet_hide_header_footer   = isset( $goodmeet_options['hide_header_footer'] ) ? $goodmeet_options['hide_header_footer'] : false;
$goodmeet_thumbnail_html       = get_the_post_thumbnail( get_the_ID() );
$goodmeet_is_header            = true;

// Access control gate (Pro feature; defaults to public for free / unset).
$goodmeet_visibility_check = goodmeet_check_meeting_visibility( get_the_ID(), get_current_user_id() );

if ( ! $goodmeet_visibility_check['allowed'] ) {
	if ( ! $goodmeet_hide_header_footer ) {
		if ( file_exists( get_template_directory() . '/header.php' ) ) {
			get_header();
		} else {
			wp_head();
		}
	} else {
		wp_head();
	}

	include GOODMEET_INCLUDES . '/Templates/goodmeet-access-denied.php';

	if ( ! $goodmeet_hide_header_footer ) {
		if ( file_exists( get_template_directory() . '/footer.php' ) ) {
			get_footer();
		} else {
			wp_footer();
		}
	} else {
		wp_footer();
	}
	return;
}

// Remove header and footer if needed.
if ( ! $goodmeet_hide_header_footer ) {
	if ( file_exists( get_template_directory() . '/header.php' ) ) {
		get_header();
	} else {
		wp_head();
	}
} else {
	wp_head();
}


// User name.
$goodmeet_user_id           = get_current_user_id();
$goodmeet_current_user_data = get_userdata( $goodmeet_user_id );
$goodmeet_current_user_name = $goodmeet_current_user_data ? $goodmeet_current_user_data->display_name : '';

$goodmeet_meeting_status       = get_post_meta( get_the_ID(), 'goodmeet_meeting_status', true );
$goodmeet_meeting_start_status = get_post_meta( get_the_ID(), 'goodmeet_meeting_start_status', true );
$goodmeet_hide_sidebar         = $goodmeet_options['hide_sidebar'];
$goodmeet_time_zone            = $goodmeet_options['meeting_timezone'];

$goodmeet_gmt_array          = goodmeet_get_gmt_offset( $goodmeet_options['meeting_timezone'] );
$goodmeet_gmt_offset_val     = $goodmeet_gmt_array['gmt_offset_val'];
$goodmeet_gmt_offset         = $goodmeet_gmt_array['gmt_offset'];
$goodmeet_should_register    = false;
$goodmeet_attendee_login     = false;
$goodmeet_login_status       = '';
$goodmeet_meeting_as_product = '';
$goodmeet_product_id         = '';
$goodmeet_po_meeting_id      = $post->ID;

$goodmeet_meeting_title = get_the_title();


if ( goodmeet_fs()->can_use_premium_code__premium_only() ) {
	$goodmeet_cookie_key         = "goodmeet_login_status_{$goodmeet_po_meeting_id}";
	$goodmeet_login_status       = isset( $_COOKIE[ $goodmeet_cookie_key ] ) ? sanitize_text_field( wp_unslash( $_COOKIE[ $goodmeet_cookie_key ] ) ) : '';
	$goodmeet_meeting_as_product = get_post_meta( get_the_ID(), 'goodmeet_connect_as_product', true );
	$goodmeet_product_id         = get_post_meta( get_the_ID(), 'goodmeet_connect_product_id', true );
	$goodmeet_should_register    = isset( $goodmeet_options['enable_should_register'] ) ? $goodmeet_options['enable_should_register'] : false;
}

require_once GOODMEET_INCLUDES . '/Templates/goodmeet-google-meet-singal-meeting.php';

// Add footer only if not hidden.
if ( ! $goodmeet_hide_header_footer ) {
	if ( file_exists( get_template_directory() . '/footer.php' ) ) {
		get_footer();
	} else {
		wp_footer();
	}
} else {
	wp_footer();
}
