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

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

add_action( 'trade_line_action_before', 'trade_line_skip_to_content', 15 );


if ( ! function_exists( 'trade_line_site_branding' ) ) :

	/**
	 * Site branding.
	 *
	 * @since 1.0.0
	 */
	function trade_line_site_branding() {

	?>
    <div class="site-branding">

		<?php trade_line_the_custom_logo(); ?>

		<?php $show_title = trade_line_get_option( 'show_title' ); ?>
		<?php $show_tagline = trade_line_get_option( 'show_tagline' ); ?>
		<?php if ( true === $show_title || true === $show_tagline ) :  ?>
			<div id="site-identity">
				<?php if ( true === $show_title ) :  ?>
					<?php if ( is_front_page() && is_home() ) : ?>
						<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><!-- #site-identity -->
		<?php endif; ?>

    </div><!-- .site-branding -->
    <div class="right-head">
    	<?php
	    $search_in_header = trade_line_get_option( 'search_in_header' );
	    if ( true === $search_in_header ) :
	    ?>
	    	<div class="search-box">
		    	<a href="#" class="search-icon" target="_self"><i class="fa fa-search"></i></a>
		    	<div class="search-box-wrap">
		    		<?php get_search_form(); ?>
		    	</div>
		    </div>
		<?php endif; ?>
	    <?php $social_in_header = trade_line_get_option( 'social_in_header' ); ?>

    			<?php
    			if ( true === $social_in_header && true === has_nav_menu( 'social' ) ) {
    				echo '<div class="social-links">';
	    	 		the_widget( 'Trade_Line_Social_Widget' );
    				echo '</div>';
    			}
    			?>

		    <?php
		    $header_layout = trade_line_get_option( 'header_layout' );
		    if( 'layout-2' === $header_layout ) :
		    	?>
		    	<div id="main-nav" class="clear-fix">
			        <nav id="site-navigation" class="main-navigation" role="navigation">
			            <div class="wrap-menu-content">
							<?php
							wp_nav_menu(
								array(
								'theme_location' => 'primary',
								'menu_id'        => 'primary-menu',
								'fallback_cb'    => 'trade_line_primary_navigation_fallback',
								)
							);
							?>
			            </div><!-- .menu-content -->
			        </nav><!-- #site-navigation -->
    			</div> <!-- #main-nav -->
			<?php endif; ?>

    </div>
    <?php

	}

endif;

add_action( 'trade_line_action_header', 'trade_line_site_branding' );

if ( ! function_exists( 'trade_line_add_primary_navigation' ) ) :

	/**
	 * Site branding.
	 *
	 * @since 1.0.0
	 */

	function trade_line_add_primary_navigation() {
		$header_layout = trade_line_get_option( 'header_layout' );
		if ( 'layout-1' === $header_layout) :
		?>
		    <div id="main-nav" class="clear-fix">
		        <div class="container">
		        <nav id="site-navigation" class="main-navigation" role="navigation">
		            <div class="wrap-menu-content">
						<?php
						wp_nav_menu(
							array(
							'theme_location' => 'primary',
							'menu_id'        => 'primary-menu',
							'fallback_cb'    => 'trade_line_primary_navigation_fallback',
							)
						);
						?>
		            </div><!-- .menu-content -->
		        </nav><!-- #site-navigation -->
		       </div> <!-- .container -->
		    </div> <!-- #main-nav -->
		<?php endif; ?>
    <?php
	}

endif;
add_action( 'trade_line_action_after_header', 'trade_line_add_primary_navigation', 20 );

if ( ! function_exists( 'trade_line_mobile_navigation' ) ) :

	/**
	 * Mobile navigation.
	 *
	 * @since 2.0.0
	 */
	function trade_line_mobile_navigation() {
		?>
		<a id="mobile-trigger" href="#mob-menu"><i class="fa fa-bars"></i></a>
		<div id="mob-menu">
			<?php
			wp_nav_menu( array(
				'theme_location' => 'primary',
				'container'      => '',
				'fallback_cb'    => 'trade_line_primary_navigation_fallback',
				) );
			?>
		</div><!-- #mob-menu -->
		<?php

	}

endif;
add_action( 'trade_line_action_before', 'trade_line_mobile_navigation', 20 );

if ( ! function_exists( 'trade_line_footer_copyright' ) ) :

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

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

		// Footer Menu.
		$footer_menu_content = wp_nav_menu( array(
			'theme_location' => 'footer',
			'container'      => 'div',
			'container_id'   => 'footer-navigation',
			'depth'          => 1,
			'fallback_cb'    => false,
			'echo'           => false,
		) );

		// Copyright content.
		$copyright_text = trade_line_get_option( 'copyright_text' );
		$copyright_text = apply_filters( 'trade_line_filter_copyright_text', $copyright_text );
		if ( ! empty( $copyright_text ) ) {
			$copyright_text = wp_kses_data( $copyright_text );
			$copyright_text = trade_line_apply_theme_shortcode( $copyright_text );
		}

		// Powered by content.
		$powered_by_text = trade_line_get_option( 'powered_by_text' );
		$powered_by_text = apply_filters( 'trade_line_filter_powered_by_text', $powered_by_text );
		if ( ! empty( $powered_by_text ) ) {
			$allowed_tags = wp_kses_allowed_html( 'post' );
			$powered_by_text = wp_kses( $powered_by_text, $allowed_tags );
			$powered_by_text = trade_line_apply_theme_shortcode( $powered_by_text );
		}
	?>

    <?php if ( ! empty( $footer_menu_content ) ) :  ?>
		<?php echo $footer_menu_content; ?>
    <?php endif ?>
    <?php if ( ! empty( $copyright_text ) ) :  ?>
      <div class="copyright">
        <?php echo $copyright_text; ?>
      </div><!-- .copyright -->
    <?php endif; ?>
    <?php if ( ! empty( $powered_by_text ) ) :  ?>
    <div class="site-info">
		<?php echo $powered_by_text; ?>
    </div><!-- .site-info -->
    <?php endif; ?>
    <?php
	}

endif;

add_action( 'trade_line_action_footer', 'trade_line_footer_copyright', 10 );


if ( ! function_exists( 'trade_line_add_sidebar' ) ) :

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

		global $post;

		$global_layout = trade_line_get_option( 'global_layout' );
		$global_layout = apply_filters( 'trade_line_filter_theme_global_layout', $global_layout );

		// Check if single.
		if ( $post && is_singular() ) {
			$post_options = get_post_meta( $post->ID, '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':
		  case 'three-columns-pcs':
		  case 'three-columns-cps':
		  case 'three-columns-psc':
		  case 'three-columns-pcs-equal':
		  case 'three-columns-scp-equal':
		    get_sidebar( 'secondary' );
		    break;

		  default:
		    break;
		}

	}

endif;

add_action( 'trade_line_action_sidebar', 'trade_line_add_sidebar' );


if ( ! function_exists( 'trade_line_custom_posts_navigation' ) ) :
	/**
	 * Posts navigation.
	 *
	 * @since 1.0.0
	 */
	function trade_line_custom_posts_navigation() {

		$pagination_type = trade_line_get_option( 'pagination_type' );

		switch ( $pagination_type ) {

			case 'default':
				the_posts_navigation();
			break;

			case 'numeric':
				if ( function_exists( 'wp_pagenavi' ) ) {
					wp_pagenavi();
				} else {
					the_posts_pagination();
				}
			break;

			default:
			break;
		}

	}
endif;

add_action( 'trade_line_action_posts_navigation', 'trade_line_custom_posts_navigation' );


if ( ! function_exists( 'trade_line_add_image_in_single_display' ) ) :

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

		global $post;

		if ( has_post_thumbnail() ) {

			$values = get_post_meta( $post->ID, 'theme_settings', true );
			$theme_settings_single_image = isset( $values['single_image'] ) ? esc_attr( $values['single_image'] ) : '';
			$theme_settings_single_image_alignment = isset( $values['single_image_alignment'] ) ? esc_attr( $values['single_image_alignment'] ) : '';

			if ( ! $theme_settings_single_image ) {
				$theme_settings_single_image = trade_line_get_option( 'single_image' );
			}
			if ( ! $theme_settings_single_image_alignment ) {
				$theme_settings_single_image_alignment = trade_line_get_option( 'single_image_alignment' );
			}

			if ( 'disable' !== $theme_settings_single_image ) {
				$args = array(
					'class' => 'align' . esc_attr( $theme_settings_single_image_alignment ),
				);
				the_post_thumbnail( esc_attr( $theme_settings_single_image ), $args );
			}
		}

	}

endif;

add_action( 'trade_line_single_image', 'trade_line_add_image_in_single_display' );

if ( ! function_exists( 'trade_line_add_breadcrumb' ) ) :

	/**
	 * Add breadcrumb.
	 *
	 * @since 1.0.0
	 */
	function trade_line_add_breadcrumb() {

		// Bail if Breadcrumb disabled.
		$breadcrumb_type = trade_line_get_option( 'breadcrumb_type' );
		if ( 'disabled' === $breadcrumb_type ) {
			return;
		}

		// Bail if Home Page.
		if ( is_front_page() || is_home() ) {
			return;
		}

		echo '<div id="breadcrumb"><div class="container">';
		switch ( $breadcrumb_type ) {
			case 'simple':
				trade_line_simple_breadcrumb();
			break;

			case 'advanced':
				if ( function_exists( 'bcn_display' ) ) {
					bcn_display();
				}
			break;

			default:
			break;
		}
		echo '</div><!-- .container --></div><!-- #breadcrumb -->';
		return;

	}

endif;

add_action( 'trade_line_action_before_content', 'trade_line_add_breadcrumb' , 7 );


if ( ! function_exists( 'trade_line_footer_goto_top' ) ) :

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

		$go_to_top = trade_line_get_option( 'go_to_top' );
		if ( true !== $go_to_top ) {
			return;
		}
		echo '<a href="#page" class="scrollup" id="btn-scrollup"><i class="fa fa-angle-up"></i></a>';

	}

endif;

add_action( 'trade_line_action_after', 'trade_line_footer_goto_top', 20 );

if ( ! function_exists( 'trade_line_add_front_page_widget_area' ) ) :

	/**
	 * Add Front Page Widget area.
	 *
	 * @since 1.0.0
	 */
	function trade_line_add_front_page_widget_area() {

		if ( is_front_page() && is_active_sidebar( 'sidebar-front-page-widget-area' ) ) {
			echo '<div id="sidebar-front-page-widget-area" class="widget-area">';
			echo '<div class="container">';
			dynamic_sidebar( 'sidebar-front-page-widget-area' );
			echo '</div><!-- .container -->';
			echo '</div><!-- #sidebar-front-page-widget-area -->';
		}

	}
endif;
add_action( 'trade_line_action_before_content', 'trade_line_add_front_page_widget_area', 7 );

if ( ! function_exists( 'trade_line_add_author_bio_in_single' ) ) :

	/**
	 * Display Author bio.
	 *
	 * @since 1.0.0
	 */
	function trade_line_add_author_bio_in_single() {

		if ( ! is_singular( 'post' ) ) {
			return;
		}
		$author_bio_in_single = trade_line_get_option( 'author_bio_in_single' );
		if ( true !== $author_bio_in_single ) {
			return;
		}
		get_template_part( 'template-parts/author-bio', 'single' );

	}
endif;

add_action( 'trade_line_author_bio', 'trade_line_add_author_bio_in_single' );

if ( ! function_exists( 'trade_line_check_home_page_content' ) ) :

	/**
	 * Check home page content status.
	 *
	 * @since 1.0.0
	 *
	 * @param bool $status Home page content status.
	 * @return bool Modified home page content status.
	 */
	function trade_line_check_home_page_content( $status ) {

		if ( is_front_page() ) {
			$home_content_status = trade_line_get_option( 'home_content_status' );
			if ( false === $home_content_status ) {
				$status = false;
			}
		}
		return $status;

	}

endif;

add_action( 'trade_line_filter_home_page_content', 'trade_line_check_home_page_content' );
