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

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

add_action( 'classifyed_action_before', 'classifyed_skip_to_content', 15 );


if ( ! function_exists( 'classifyed_site_branding' ) ) :

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

	?>
    <div class="site-branding">
		<?php
			$site_logo = classifyed_get_option( 'site_logo' );
			$move_logo_after_title = classifyed_get_option( 'move_logo_after_title' );
			$site_logo_content = '';
		if ( ! empty( $site_logo ) ) {
			$site_logo_content =
			'<div id="site-logo"><a href="' . esc_url( home_url( '/' ) ) . '"><img src="' . esc_url( $site_logo ) . '" alt="' . esc_attr( get_bloginfo( 'name', 'display' ) ) . '" /></a></div><!-- #site-logo -->';
		}
		?>
		<?php if ( true !== $move_logo_after_title && ! empty( $site_logo_content ) ) :  ?>
        <?php echo $site_logo_content; ?>
		<?php endif ?>

		<?php $show_title = classifyed_get_option( 'show_title' ); ?>
		<?php $show_tagline = classifyed_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 ?>

		<?php if ( true === $move_logo_after_title && ! empty( $site_logo_content ) ) :  ?>
        <?php echo $site_logo_content; ?>
		<?php endif ?>


    </div><!-- .site-branding -->

    <?php

	}

endif;

add_action( 'classifyed_action_header', 'classifyed_site_branding' );


if ( ! function_exists( 'classifyed_add_primary_navigation' ) ) :

	/**
	 * Site branding.
	 *
	 * @since 1.0.0
	 */
	function classifyed_add_primary_navigation() {
	?>
    <div id="main-nav" class="clear-fix">
        <div class="container">
        <nav id="site-navigation" class="main-navigation" role="navigation">
          <button class="menu-toggle" aria-controls="primary-menu" aria-expanded="false"><?php esc_html_e( 'Menu', 'classifyed' ); ?></button>
            <div class="wrap-menu-content">
				<?php
				wp_nav_menu(
					array(
					'theme_location'  => 'primary',
					'menu_id'         => 'primary-menu',
					)
				);
				?>
            </div><!-- .menu-content -->
        </nav><!-- #site-navigation -->
       </div> <!-- .container -->
    </div> <!-- #main-nav -->
    <?php
	}

endif;

add_action( 'classifyed_action_after_header', 'classifyed_add_primary_navigation', 20 );

if ( ! function_exists( 'classifyed_footer_copyright' ) ) :

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

		// Check if footer is disabled.
		$footer_status = apply_filters( 'classifyed_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.
		$copyright_text = classifyed_get_option( 'copyright_text' );
		$copyright_text = apply_filters( 'classifyed_filter_copyright_text', esc_html( $copyright_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 ?>
    <div class="site-info">
      <a href="<?php echo esc_url( __( 'https://wordpress.org/', 'classifyed' ) ); ?>"><?php printf( esc_html__( 'Powered by %s', 'classifyed' ), 'WordPress' ); ?></a>
      <span class="sep"> | </span>
		<?php printf( esc_html__( '%1$s by %2$s', 'classifyed' ), 'Classifyed', '<a href="' . esc_url( 'http://wenthemes.com/' ) . '" rel="designer" target="_blank">WEN Themes</a>' ); ?>
    </div><!-- .site-info -->
    <?php

	}

endif;

add_action( 'classifyed_action_footer', 'classifyed_footer_copyright', 10 );


if ( ! function_exists( 'classifyed_add_sidebar' ) ) :

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

		global $post;

		$global_layout = classifyed_get_option( 'global_layout' );
		$global_layout = apply_filters( 'classifyed_filter_theme_global_layout', $global_layout );

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

		// Include sidebar.
		if ( 'no-sidebar' !== $global_layout ) {
			get_sidebar();
		}
		if ( 'three-columns' === $global_layout ) {
			get_sidebar( 'secondary' );
		}

	}

endif;

add_action( 'classifyed_action_sidebar', 'classifyed_add_sidebar' );


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

		$pagination_type = classifyed_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( 'classifyed_action_posts_navigation', 'classifyed_custom_posts_navigation' );


if ( ! function_exists( 'classifyed_add_image_in_single_display' ) ) :

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

		global $post;

		if ( has_post_thumbnail() ) {

			$values = get_post_meta( $post->ID, 'classifyed_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 = classifyed_get_option( 'single_image' );
			}
			if ( ! $theme_settings_single_image_alignment ) {
				$theme_settings_single_image_alignment = classifyed_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( 'classifyed_single_image', 'classifyed_add_image_in_single_display' );

if ( ! function_exists( 'classifyed_add_breadcrumb' ) ) :

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

		// Bail if Breadcrumb disabled.
		$breadcrumb_type = classifyed_get_option( 'breadcrumb_type' );
		if ( 'disabled' === $breadcrumb_type ) {
			return;
		}
		// Bail if plugin not active.
		// Bail if Home Page.
		if ( is_front_page() || is_home() ) {
			return;
		}

		echo '<div id="breadcrumb"><div class="container">';
		switch ( $breadcrumb_type ) {
			case 'simple':
				$breadcrumb_separator = classifyed_get_option( 'breadcrumb_separator' );
				$args = array(
				'separator'     => $breadcrumb_separator,
				);
				classifyed_simple_breadcrumb( $args );
			break;

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

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

	}

endif;

add_action( 'classifyed_action_before_content', 'classifyed_add_breadcrumb' , 7 );


if ( ! function_exists( 'classifyed_footer_goto_top' ) ) :

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

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

	}

endif;

add_action( 'classifyed_action_after', 'classifyed_footer_goto_top', 20 );

if ( ! function_exists( 'classifyed_add_custom_header' ) ) :

	/**
	 * Add Custom Header.
	 *
	 * @since 1.0.0
	 */
	function classifyed_add_custom_header() {

		if ( ! get_header_image() ) {
			return;
		}
	?>
    <div id="custom-header">
      <div class="container">
        <a href="<?php echo esc_url( home_url( '/' ) ); ?>" rel="home">
          <img src="<?php header_image(); ?>" width="<?php echo esc_attr( get_custom_header()->width ); ?>" height="<?php echo esc_attr( get_custom_header()->height ); ?>" alt="<?php echo esc_attr( get_bloginfo( 'name', 'display' ) ); ?>">
        </a>
      </div><!-- .container -->
    </div><!-- #custom-header -->
    <?php

	}

endif;

add_action( 'classifyed_action_after_header', 'classifyed_add_custom_header', 15 );

if ( ! function_exists( 'classifyed_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 classifyed_check_home_page_content( $status ) {

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

	}

endif;

add_action( 'classifyed_filter_home_page_content', 'classifyed_check_home_page_content' );
