<?php
/**
 * Sidebar template containing the main widget area.
 *
 * Responsible for rendering the primary widgets in the sidebar.
 *
 * @link https://developer.wordpress.org/themes/basics/template-files/#template-partials
 *
 * @package Yaatra
 */

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

if ( 'post' === get_post_type() ) {
	$post_id = get_the_ID();
    $sidebar_meta_option = get_post_meta( $post_id, 'yaatra_sidebar_layout', true );
}

if ( 'page' === get_post_type() ) {
	$page_id = get_the_ID();
    $sidebar_meta_option = get_post_meta( $page_id, 'yaatra_sidebar_layout', true );
}

if ( empty( $sidebar_meta_option ) || is_archive() ) {
    $sidebar_meta_option = 'default_sidebar';
}

$global_archive_sidebar     = yaatra_get_customizer_option_value( 'yaatra_archive_sidebar' );
$global_posts_sidebar       = yaatra_get_customizer_option_value( 'yaatra_global_post_sidebar' );
$global_pages_sidebar       = yaatra_get_customizer_option_value( 'yaatra_global_page_sidebar' );

if ( $sidebar_meta_option == 'default_sidebar' ) {
	if ( is_page() ) {
	    if ( 'right_sidebar' !== $global_pages_sidebar && 'both_sidebar' !== $global_pages_sidebar ) {
	        return;
	    }
	} elseif ( is_single() ) {
	    if ( 'right_sidebar' !== $global_posts_sidebar && 'both_sidebar' !== $global_posts_sidebar ) {
	        return;
	    }
	} elseif ( is_archive() ) {
	    if ( 'right_sidebar' !== $global_archive_sidebar && 'both_sidebar' !== $global_archive_sidebar ) {
	        return;
	    }
	}
} elseif ( 'right_sidebar' !== $sidebar_meta_option && 'both_sidebar' !== $sidebar_meta_option ) {
	return;
}

if ( ! is_active_sidebar( 'sidebar-1' ) ) {
	return;
}
?>

<aside id="secondary" class="widget-area">
	<?php dynamic_sidebar( 'sidebar-1' ); ?>
</aside><!-- #secondary -->
