<?php
/**
 * Custom theme functions.
 *
 * This file contains hook functions attached to theme hooks.
 *
 * @package bookstore_exhibition
 */

if ( ! function_exists( 'bookstore_exhibition_skip_to_content' ) ) :
	/**
	 * Add Skip to content.
	 *
	 * @since 1.0.0
	 */
	function bookstore_exhibition_skip_to_content() {
	?><a class="skip-link screen-reader-text" href="#content"><?php esc_html_e( 'Skip to content', 'bookstore-exhibition' ); ?></a><?php
	}
endif;

add_action( 'bookstore_exhibition_action_before', 'bookstore_exhibition_skip_to_content', 15 );

// Top Header

if ( ! function_exists( 'bookstore_exhibition_header_top_content' ) ) :

	/**
	 * Header Top.
	 *
	 * @since 1.0.0
	 */
	function bookstore_exhibition_header_top_content() {
		$discount_text = bookstore_exhibition_get_option( 'discount_text' );
		$discount_link = bookstore_exhibition_get_option( 'discount_link' );
		$facebook_link = bookstore_exhibition_get_option( 'facebook_link' );
		$twitter_link = bookstore_exhibition_get_option( 'twitter_link' );
		$watsapp_link = bookstore_exhibition_get_option( 'watsapp_link' );
		$instagram_link = bookstore_exhibition_get_option( 'instagram_link' );
		$linkdin_link = bookstore_exhibition_get_option( 'linkdin_link' );
		?>
		<?php $show_header_top = bookstore_exhibition_get_option( 'show_header_top' ); ?>
		<?php if ( true === $show_header_top ) :  ?>
		<section id="header" class="py-3">
			<div class="container-fluid">
				<div class="row">
					<div class="col-lg-6 col-md-6 align-self-center text-md-start text-center left-box">
						<?php if( !empty($discount_text) ):?>
						    <span><?php echo esc_html($discount_text);?><a class="ms-2" href="<?php echo esc_html($discount_link);?>"><?php esc_html_e('Read More','bookstore-exhibition'); ?></a></span>
						<?php endif;?>
					</div>
					<?php $show_social_icon = bookstore_exhibition_get_option( 'show_social_icon' ); ?>
					<?php if ( true === $show_social_icon ) :  ?>
						<div class="col-lg-6 col-md-6 align-self-center text-md-end text-center social-box">
							<?php if( !empty($discount_text) ):?>
								<span class="social-text"><?php esc_html_e('Connect With Us -','bookstore-exhibition'); ?></span>
								<?php if( !empty($facebook_link) || !empty($twitter_link) || !empty($watsapp_link) || !empty($instagram_link) || !empty($linkdin_link)  ):?>
								    <a href="<?php echo esc_url($facebook_link);?>" class="me-3"><span class="dashicons dashicons-facebook-alt"></span></a>
								<?php endif;?>
								<?php if( !empty($twitter_link) ):?>
								    <a href="<?php echo esc_url($twitter_link);?>" class="me-3"><span class="dashicons dashicons-twitter"></span></a>
								<?php endif;?>
								<?php if( !empty($watsapp_link) ):?>
								    <a href="<?php echo esc_url($watsapp_link);?>" class="me-3"><span class="dashicons dashicons-whatsapp"></span></a>
								<?php endif;?>
								<?php if( !empty($instagram_link) ):?>
								    <a href="<?php echo esc_url($instagram_link);?>" class="me-3"><span class="dashicons dashicons-instagram"></span></a>
								<?php endif;?>
								<?php if( !empty($linkdin_link) ):?>
								    <a href="<?php echo esc_url($linkdin_link);?>"><span class="dashicons dashicons-linkedin"></span></a>
								<?php endif;?>
							<?php endif;?>
						</div>
					<?php endif; ?>
				</div>
			</div>
		</section>
		<?php endif; ?>
		<?php
	}

endif;

add_action( 'bookstore_exhibition_action_before_header', 'bookstore_exhibition_header_top_content', 5 );

// Top Header end

// Middle Header

if ( ! function_exists( 'bookstore_exhibition_site_branding' ) ) :

	/**
	 * Site branding.
	 *
	 * @since 1.0.0
	 */
	function bookstore_exhibition_site_branding() {
		$data_sticky = bookstore_exhibition_get_option( 'show_data_sticky_setting' );
	?>

		<!-- responsive menu -->
		<div id="middle-header" data-sticky= "<?php echo esc_attr($data_sticky); ?>">
			<div class="container">
				<div class="row">
				    <div class="col-xl-2 col-lg-12 col-md-4 align-self-center text-lg-start text-center">
					    <div class="site-branding mb-3 mb-lg-0">
							<?php bookstore_exhibition_the_custom_logo(); ?>
							<?php $show_title = bookstore_exhibition_get_option( 'show_title' ); ?>
							<?php $show_tagline = bookstore_exhibition_get_option( 'show_tagline' ); ?>
							<?php if ( true === $show_title || false === $show_tagline ) :  ?>
								<div id="site-identity">
									<?php if ( true === $show_title ) :  ?>
										<?php if ( is_front_page() ) : ?>
											<h1 class="site-title"><a href="<?php echo esc_url( home_url( '/' ) ); ?>" rel="home"><?php bloginfo( 'name' ); ?></a></h1>
										<?php else : ?>
											<p class="site-title"><a href="<?php echo esc_url( home_url( '/' ) ); ?>" rel="home"><?php bloginfo( 'name' ); ?></a></p>
										<?php endif; ?>
									<?php endif; ?>
									<?php if ( true === $show_tagline ) :  ?>
										<p class="site-description"><?php bloginfo( 'description' ); ?></p>
									<?php endif; ?>
								</div>
							<?php endif; ?>
					    </div>
					</div>

					<div class="col-xl-3 col-lg-6 col-md-8 align-self-center">
						<?php if(class_exists('woocommerce')){ ?>
							<div class="woo-box mb-3 mb-lg-0">
					            <div class=" position-relative">
					            	<button class="product-btn"><?php echo esc_html_e('All Category','bookstore-exhibition'); ?></button>
					              	<div class="product-cat">
						                <?php
						                  $args = array(
						                    'orderby'    => 'title',
						                    'order'      => 'ASC',
						                    'hide_empty' => 0,
						                    'parent'  => 0
						                  );
						                  $product_categories = get_terms( 'product_cat', $args );
						                  $count = count($product_categories);
						                  if ( $count > 0 ){
						                      foreach ( $product_categories as $product_category ) {
						                        $product_cat_id   = $product_category->term_id;
						                        $cat_link = get_category_link( $product_cat_id );
						                        if ($product_category->category_parent == 0) { ?>
						                      <li class="drp_dwn_menu"><a href="<?php echo esc_url(get_term_link( $product_category ) ); ?>">
						                      <?php
						                    }
						                    echo esc_html( $product_category->name ); ?></a></li>
						                <?php } } ?>
					              	</div>
					            </div>
					            <div class="product_serach_box">
							    	<?php get_product_search_form(); ?>
							    </div>
							</div>
				        <?php }?>
					</div>

					<!-- right menu -->
					<div class="col-lg-6 col-md-6 col-6 align-self-center">
						<div class="toggle-menu gb_menu text-md-start">
							<button onclick="bookstore_exhibition_gb_Menu_open()" class="gb_toggle p-2"><?php esc_html_e('Menu','bookstore-exhibition'); ?></button>
						</div>
						<div id="gb_responsive" class="nav side_gb_nav">
							<nav id="top_gb_menu" class="gb_nav_menu" role="navigation" aria-label="<?php esc_attr_e( 'Menu', 'bookstore-exhibition' ); ?>">
								<?php 
								    wp_nav_menu( array( 
										'theme_location' => 'primary-menu',
										'container_class' => 'gb_navigation clearfix',
										'menu_class' => 'gb_navigation clearfix',
										'items_wrap' => '<ul id="%1$s" class="%2$s mobile_nav mb-0 px-0">%3$s</ul>',
										'fallback_cb' => 'wp_page_menu',
								    ) ); 
								?>
								<a href="javascript:void(0)" class="closebtn gb_menu" onclick="bookstore_exhibition_gb_Menu_close()">x<span class="screen-reader-text"><?php esc_html_e('Close Menu','bookstore-exhibition'); ?></span></a>
							</nav>
						</div>
					</div>

					<div class="col-lg-1 col-md-6 col-6 align-self-center text-md-end text-center ">
						<?php if ( class_exists( 'woocommerce' ) ) {?>
							<a class="cart-customlocation" href="<?php if(function_exists('wc_get_cart_url')){ echo esc_url(wc_get_cart_url()); } ?>" title="<?php esc_attr_e( 'View Shopping Cart','bookstore-exhibition' ); ?>"><span class="cart-item-box"><span class="dashicons dashicons-products"></span><span class="cart-text"><?php esc_html_e('Cart','bookstore-exhibition'); ?>( <?php echo esc_html(wp_kses_data( WC()->cart->get_cart_contents_count() ));?> )</span></span></a>
						<?php }?>
					</div>
				</div>
			</div>
		</div>

	    <?php
	}

endif;

add_action( 'bookstore_exhibition_action_header', 'bookstore_exhibition_site_branding' );


/////////////////////////////////// copyright start/////////////////////////////

if ( ! function_exists( 'bookstore_exhibition_footer_copyright' ) ) :

	/**
	 * Footer copyright
	 *
	 * @since 1.0.0
	 */
	function bookstore_exhibition_footer_copyright() {

		// Check if footer is disabled.
		$footer_status = apply_filters( 'bookstore_exhibition_filter_footer_status', true );
		if ( true !== $footer_status ) {
			return;
		}

		// Copyright content.
		$copyright_text = bookstore_exhibition_get_option( 'copyright_text' );
		$copyright_text = apply_filters( 'bookstore_exhibition_filter_copyright_text', $copyright_text );
		if ( ! empty( $copyright_text ) ) {
			$copyright_text = wp_kses_data( $copyright_text );
		}

		// Powered by content.
		$powered_by_text = sprintf( __( 'Bookstore Exhibition by %s', 'bookstore-exhibition' ), '<span>' . __( 'Mizan Themes', 'bookstore-exhibition' ) . '</span>' );
		?>

		<?php
			$bookstore_exhibition_show_copyright = bookstore_exhibition_get_option('bookstore_exhibition_show_copyright_box_setting');

			if ( $bookstore_exhibition_show_copyright ) : ?>
			<div class="colophon-inner">
				<?php if ( ! empty( $copyright_text ) ) : ?>
					<div class="colophon-column">
						<div class="copyright">
							<?php echo $copyright_text; ?>
						</div><!-- .copyright -->
					</div><!-- .colophon-column -->
				<?php endif; ?>

			<?php if ( ! empty( $powered_by_text ) ) : ?>
					<div class="colophon-column">
						<div class="site-info">
							<a href="<?php echo esc_url('https://www.wpelemento.com/products/bookstore-exhibition/','bookstore-exhibition'); ?>"><?php echo $powered_by_text; ?></a>
						</div><!-- .site-info -->
					</div><!-- .colophon-column -->
				<?php endif; ?>

			</div><!-- .colophon-inner -->
		<?php endif; ?>

	    <?php
	}

endif;

add_action( 'bookstore_exhibition_action_footer', 'bookstore_exhibition_footer_copyright', 10 );

// /////////////////////////////////sidebar//////////////////


if ( ! function_exists( 'bookstore_exhibition_add_sidebar' ) ) :

	/**
	 * Add sidebar.
	 *
	 * @since 1.0.0
	 */
	function bookstore_exhibition_add_sidebar() {

		global $post;

		$global_layout = bookstore_exhibition_get_option( 'global_layout' );
		$global_layout = apply_filters( 'bookstore_exhibition_filter_theme_global_layout', $global_layout );

		// Check if single.
		if ( $post && is_singular() ) {
			$post_options = get_post_meta( $post->ID, 'bookstore_exhibition_theme_settings', true );
			if ( isset( $post_options['post_layout'] ) && ! empty( $post_options['post_layout'] ) ) {
				$global_layout = $post_options['post_layout'];
			}
		}

		// Include primary sidebar.
		if ( 'no-sidebar' !== $global_layout ) {
			get_sidebar();
		}
		// Include Secondary sidebar.
		switch ( $global_layout ) {
			case 'three-columns':
			get_sidebar( 'secondary' );
			break;

			default:
			break;
		}

		// Include Secondary sidebar 1.
		switch ( $global_layout ) {
			case 'four-columns':
			get_sidebar( 'secondary' );
			break;

			default:
			break;
		}

	}

endif;

add_action( 'bookstore_exhibition_action_sidebar', 'bookstore_exhibition_add_sidebar' );

//////////////////////////////////////// single page


if ( ! function_exists( 'bookstore_exhibition_add_image_in_single_display' ) ) :

	/**
	 * Add image in single post.
	 *
	 * @since 1.0.0
	 */
	function bookstore_exhibition_add_image_in_single_display() {

		global $post;

		if ( has_post_thumbnail() ) {

			$values = get_post_meta( $post->ID, 'bookstore_exhibition_theme_settings', true );
			$bookstore_exhibition_theme_settings_single_image = isset( $values['single_image'] ) ? esc_attr( $values['single_image'] ) : '';

			if ( ! $bookstore_exhibition_theme_settings_single_image ) {
				$bookstore_exhibition_theme_settings_single_image = bookstore_exhibition_get_option( 'single_image' );
			}

			if ( 'disable' !== $bookstore_exhibition_theme_settings_single_image ) {
				$args = array(
					'class' => 'aligncenter blog-post-img',
				);
				the_post_thumbnail( esc_attr( $bookstore_exhibition_theme_settings_single_image ), $args );
			}
		}

	}

endif;

add_action( 'bookstore_exhibition_single_image', 'bookstore_exhibition_add_image_in_single_display' );

if ( ! function_exists( 'bookstore_exhibition_footer_goto_top' ) ) :

	/**
	 * Go to top.
	 *
	 * @since 1.0.0
	 */
	function bookstore_exhibition_footer_goto_top() {

		$bookstore_exhibition_show_progress_bar =
		bookstore_exhibition_get_option( 'bookstore_exhibition_progress_bar' );

		if ( true === $bookstore_exhibition_show_progress_bar ) :
			echo '<div id="elemento-progress-bar" class="theme-progress-bar top"></div>';
		endif;

		$show_scroll_to_top = bookstore_exhibition_get_option( 'show_scroll_to_top' );
        if ( true === $show_scroll_to_top ) :
		echo '<a id="scrollToTopBtn" href="#page">
				<svg id="progressCircle" width="50" height="50" aria-hidden="true">
					<circle cx="25" cy="25" r="22" stroke-width="4" fill="none"/>
				</svg>
				<i class="fa-solid fa-arrow-up"></i>
				</a>';
	    endif; 

	}

endif;

add_action( 'bookstore_exhibition_action_after', 'bookstore_exhibition_footer_goto_top', 20 );