<?php
/**
 * Custom template functions for this theme
 *
 * Eventually, some of the functionality here could be replaced by core features.
 *
 * @package Excel_Blog
 */

if( ! function_exists( 'excel_blog_doctype' ) ) :
/**
 * Doctype Declaration
*/
function excel_blog_doctype(){
    ?>
    <!DOCTYPE html>
    <html <?php language_attributes(); ?>>
    <?php
}
endif;
add_action( 'excel_blog_doctype', 'excel_blog_doctype' );

if( ! function_exists( 'excel_blog_head' ) ) :
/**
 * Before wp_head
*/
function excel_blog_head(){
    ?>
    <meta charset="<?php bloginfo( 'charset' ); ?>">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="profile" href="http://gmpg.org/xfn/11">
    <?php
}
endif;
add_action( 'excel_blog_before_wp_head', 'excel_blog_head' );

if( ! function_exists( 'excel_blog_page_start' ) ) :
/**
 * Page Start
*/
function excel_blog_page_start(){
    ?>
    <div id="page" class="site">
    <?php
}
endif;
add_action( 'excel_blog_before_header', 'excel_blog_page_start', 20 );

if( ! function_exists( 'excel_blog_header_start' ) ) :
/**
 * Header Start
*/
function excel_blog_header_start(){
    ?>
    <header id="masthead" class="site-header" role="banner">
    <?php 
}
endif;
add_action( 'excel_blog_header', 'excel_blog_header_start', 20 );

if( ! function_exists( 'excel_blog_header_top' ) ) :
/**
 * Header Top
*/
function excel_blog_header_top(){ 
    
    $email     = get_theme_mod( 'header_email' ); //from customizer
    $phone     = get_theme_mod( 'header_phone' ); //from customizer
    $ed_social = get_theme_mod( 'ed_social' ); //from customizer
    
    if( $ed_social || $email || $phone ){
    ?>
    <div class="header-t">
		<div class="container">
			
            <?php 
            if( $email || $phone ){ ?>
                <div class="info">
    				<?php if( $email ){ ?>
                        <a href="<?php echo esc_url( 'mailto:' . $email ); ?>" class="email-link"><i class="fa fa-envelope"></i><?php echo esc_html( $email ); ?></a>
    				<?php } ?> 
                    
                    <?php if( $phone ){ ?>
    				    <a href="<?php echo esc_url( 'tel:' . preg_replace( '/\D/', '', $phone ) ); ?>" class="tel-link"><i class="fa fa-phone-square"></i><?php echo esc_html( $phone ); ?></a>    
    				<?php }?>
    			</div>
            <?php 
            }
            
            if( $ed_social ) excel_blog_get_social_links();
            
            ?>
		</div>
	</div>
    <?php
    }
}
endif;
add_action( 'excel_blog_header', 'excel_blog_header_top', 30 );

if( ! function_exists( 'excel_blog_header_site_branding' ) ) :
/**
 * Site Branding
*/
function excel_blog_header_site_branding(){
    ?>
    <div class="container">
        <div class="site-branding">
            <?php 
                if( function_exists( 'has_custom_logo' ) && has_custom_logo() ){
                    the_custom_logo();
                } 
            ?>
            <h1 class="site-title"><a href="<?php echo esc_url( home_url( '/' ) ); ?>" rel="home"><?php bloginfo( 'name' ); ?></a></h1>
			<?php
			$description = get_bloginfo( 'description', 'display' );
			if ( $description || is_customize_preview() ) : ?>
				<p class="site-description"><?php echo esc_html( $description ); ?></p>
			<?php
			endif; ?>
		</div><!-- .site-branding -->
    </div>
    <?php
}
endif;
add_action( 'excel_blog_header', 'excel_blog_header_site_branding', 40 );

if( ! function_exists( 'excel_blog_header_nav' ) ) :
/**
 * Header Navigation
*/
function excel_blog_header_nav(){
    ?>
    <div class="nav-holder">
        <div class="container">
            <nav id="site-navigation" class="main-navigation" role="navigation">			
    			<?php wp_nav_menu( array( 'theme_location' => 'menu-1', 'menu_id' => 'primary-menu' ) ); ?>
    		</nav><!-- #site-navigation -->
        </div>
    </div>
    <?php
}
endif;
add_action( 'excel_blog_header', 'excel_blog_header_nav', 40 );

if( ! function_exists( 'excel_blog_header_end' ) ) :
/**
 * Header End
*/
function excel_blog_header_end(){
    ?>
    </header>
    <?php
}
endif;
add_action( 'excel_blog_header', 'excel_blog_header_end', 100 );

if( ! function_exists( 'excel_blog_top_slider' ) ) :
/**
 * Top Slider 
*/
function excel_blog_top_slider(){
    
    $ed_slider   = get_theme_mod( 'ed_banner_slider', 'disable' ); 
    $ed_caption  = get_theme_mod( 'banner_slider_caption', '1' ); //from customizer
    $read_more   = get_theme_mod( 'banner_slider_read_more', __( 'Read More', 'excel-blog' ) ); //from customzer
    $sticky_post = get_option( 'sticky_posts' ); //get all sticky posts
    
    $qry = new WP_Query ( array( 
        'post_type'           => 'post', 
        'post_status'         => 'publish',
        'posts_per_page'      => -1,
        'post__in'            => $sticky_post,
        'ignore_sticky_posts' => 1 
    ) );
    
    if( ( $ed_slider === 'all_page' ) || ( ( $ed_slider === 'front_page' ) && is_front_page() ) ){
    
        if( $sticky_post && $qry->have_posts() ){
            
        ?>
        <div class="banner">
    		<div id="banner-slider" class="owl-carousel">
    			<?php
                while( $qry->have_posts() ){
                    $qry->the_post();
                    if( has_post_thumbnail() ){ ?>
                        <div class="item">
            				<?php the_post_thumbnail( 'excel-blog-top-slider' ); ?>
            				<?php if( $ed_caption ){ ?>
                            <div class="banner-text">
            					<div class="container">
            						<div class="text">
            							<h2 class="title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
            							<a href="<?php the_permalink(); ?>" class="btn-readmore"><?php echo esc_html( $read_more ); ?></a>
            						</div>
            					</div>
            				</div>
                            <?php } ?>
            			</div>
                    <?php
                    }
                }
                wp_reset_postdata();
                ?>                                  			
    		</div>
    	</div>
        
        <?php
        }
    }
}
endif;
add_action( 'excel_blog_after_header', 'excel_blog_top_slider', 20 );

if( ! function_exists( 'excel_blog_content_start' ) ) :
/**
 * Content Start
*/
function excel_blog_content_start(){
    $class = is_404() ? 'error-holder' : 'row' ;
    ?>
    <div id="content" class="site-content">
        <div class="container">
            <div class="<?php echo esc_attr( $class ); ?>">
    <?php
}
endif;
add_action( 'excel_blog_content_start', 'excel_blog_content_start' );

if( ! function_exists( 'excel_blog_before_loop' ) ) :
/**
 * Header before archive loop.
*/
function excel_blog_before_loop(){
    
    echo '<header class="page-header">';
    
    if ( is_home() && ! is_front_page() ){ //For Blog Index page
        echo '<h1 class="page-title">';
        single_post_title();
        echo '</h1>';
    }   
	
    if( is_archive() ){ //For Archive Page
        the_archive_title( '<h1 class="page-title">', '</h1>' );
		the_archive_description( '<div class="archive-description">', '</div>' );
	}
     
    if( is_search() ){ //For Search Page
        echo '<h1 class="page-title">';
        printf( esc_html__( 'Search Results for: %s', 'excel-blog' ), '<span>' . get_search_query() . '</span>' );
        echo '</h1>';
        
        get_search_form();
	}
    
    echo '</header>'; //<!-- .page-header -->   
}
endif;
add_action( 'excel_blog_before_content', 'excel_blog_before_loop' );

if( ! function_exists( 'excel_blog_pagination' ) ) :
/**
 * Pagination
*/
function excel_blog_pagination(){
    if( is_single() ){
        the_post_navigation();
    }elseif( ! is_page() ){
        the_posts_pagination( array(
            'mid_size' => 2,
            'prev_text' => __( 'Prev', 'excel-blog' ),
            'next_text' => __( 'Next', 'excel-blog' ),
         ) );    
    }
}
endif;
add_action( 'excel_blog_after_content', 'excel_blog_pagination', 20 );

if( ! function_exists( 'excel_blog_comments' ) ) :
/**
 * Call for comment template in post and page.
*/
function excel_blog_comments(){
    // If comments are open or we have at least one comment, load up the comment template.
	if( is_singular() && ( comments_open() || get_comments_number() ) ) :
		comments_template();
	endif;    
}
endif;
add_action( 'excel_blog_after_content', 'excel_blog_comments', 25 );

if( ! function_exists( 'excel_blog_get_author' ) ) :
/**
 * Prints author bio.
*/
function excel_blog_get_author(){
    if( is_single() && get_the_author_meta( 'description' ) ){ ?>
        <section class="author-section">
			<div class="img-holder"><?php echo get_avatar( get_the_author_meta( 'ID' ), 114 ); ?></div>
			<div class="text-holder">
				<h2 class="title"><?php echo esc_html( get_the_author_meta( 'display_name' ) ); ?></h2>
				<?php echo wpautop( wp_kses_post( get_the_author_meta( 'description' ) ) ); ?>
			</div>
		</section>
    <?php
    }
}
endif;
add_action( 'excel_blog_after_content', 'excel_blog_get_author', 15 );

if( ! function_exists( 'excel_blog_bottom_slider' ) ) :
/**
 * Bottom Slider
*/
function excel_blog_bottom_slider(){
    
    $ed_slider      = get_theme_mod( 'ed_bottom_slider', 'disable' );
    $featured_cat   = get_theme_mod( 'bottom_slider_cat' ); //from customizer
    $featured_label = get_theme_mod( 'bottom_slider_title', __( 'Most Viewed', 'excel-blog' ) ); //from customizer
    $sticky_post    = get_option( 'sticky_posts' ); //get all sticky posts
    
    if( $featured_label ){
        $label = $featured_label;
    }else{
        $label = get_cat_name( $featured_cat );
    }
    
    if( $featured_cat && ( ( $ed_slider === 'all_page' ) || ( ( $ed_slider === 'front_page' ) && is_front_page() ) ) ){
    
        $args = array(
            'post_type'      => 'post', 
            'cat'            => $featured_cat,
            'post_status'    => 'publish',
            'posts_per_page' => -1, 
        );
        
        if( $sticky_post && is_home() ){
            $args['post__not_in'] = $sticky_post;
        }
        
        $qry = new WP_Query( $args );
        
        if( $qry->have_posts() ){            
        ?>
        <section class="popular">
    		<div class="container">
    			<h2 class="main-title"><?php echo esc_html( $label ); ?></h2>
    			<div id="popular-slider" class="owl-carousel">
    				<?php
                    while( $qry->have_posts() ){
                        $qry->the_post(); 
                        if( has_post_thumbnail() ){ ?>
                            <div class="item">
            					<div class="img-holder"><a href="<?php the_permalink(); ?>"><?php the_post_thumbnail( 'excel-blog-bottom-slider' ); ?></a></div>
            					<div class="text-holder">
            						<div class="entry-meta">
            							<?php 
                                            excel_blog_posted_on();
                                            excel_blog_comments_by(); 
                                        ?>                                        
            						</div>
            						<h3 class="entry-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
            					</div>
            				</div>
    				    <?php
                        }
                    }
                    wp_reset_postdata();
                    ?>
    			</div>
    		</div>
    	</section>
        <?php
        } 
    }   
}
endif;
add_action( 'excel_blog_before_footer', 'excel_blog_bottom_slider', 20 );

if( ! function_exists( 'excel_blog_footer_start' ) ) :
/**
 * Footer Start
*/
function excel_blog_footer_start(){
    ?>
    <footer id="colophon" class="site-footer" role="contentinfo">
        <div class="container">
    <?php
}
endif;
add_action( 'excel_blog_footer', 'excel_blog_footer_start', 20 );

if( ! function_exists( 'excel_blog_footer_top' ) ) :
/**
 * Footer Top
*/
function excel_blog_footer_top(){    
    if( is_active_sidebar( 'footer-one' ) || is_active_sidebar( 'footer-two' ) || is_active_sidebar( 'footer-three' ) ){
    ?>
    <div class="footer-t">
		<div class="row">
			<?php if( is_active_sidebar( 'footer-one' ) ){ ?>
				<div class="col">
				   <?php dynamic_sidebar( 'footer-one' ); ?>	
				</div>
            <?php } ?>
			
            <?php if( is_active_sidebar( 'footer-two' ) ){ ?>
                <div class="col">
				   <?php dynamic_sidebar( 'footer-two' ); ?>	
				</div>
            <?php } ?>
            
            <?php if( is_active_sidebar( 'footer-three' ) ){ ?>
                <div class="col">
				   <?php dynamic_sidebar( 'footer-three' ); ?>	
				</div>
            <?php } ?>
		</div>
	</div>
    <?php 
    }   
}
endif;
add_action( 'excel_blog_footer', 'excel_blog_footer_top', 30 );

if( ! function_exists( 'excel_blog_footer_bottom' ) ) :
/**
 * Footer Bottom
*/
function excel_blog_footer_bottom(){
    $footer_copyright = get_theme_mod( 'footer_copyright' );
    ?>
    <div class="footer-bottom">
		<div class="site-info">
        <?php 
        if( $footer_copyright ){
            echo wp_kses_post( $footer_copyright );    
        }else{
            echo esc_html__( '&copy; Copyright ', 'excel-blog' ) . date_i18n( __( 'Y', 'excel-blog' ) ); ?> <a href="<?php echo esc_url( home_url( '/' ) ); ?>"><?php echo esc_html( get_bloginfo( 'name' ) ); ?></a>.
        <?php 
        } 
        ?>
        </div>
		
        <div class="site-by"><?php esc_html_e( 'Theme by ', 'excel-blog' ); ?><a href="<?php echo esc_url( 'http://exceltheme.com' ); ?>" rel="author" target="_blank"><?php echo esc_html__( 'Excel Theme', 'excel-blog' ); ?></a>. <?php printf( esc_html__( 'Powered by %s', 'excel-blog' ), '<a href="'. esc_url( __( 'https://wordpress.org/', 'excel-blog' ) ) .'" target="_blank">WordPress</a>' ); ?>.</div>
	</div>    
    <?php
}
endif;
add_action( 'excel_blog_footer', 'excel_blog_footer_bottom', 40 );

if( ! function_exists( 'excel_blog_footer_end' ) ) :
/**
 * Footer End 
*/
function excel_blog_footer_end(){
    ?>
        </div><!-- .container -->
    </footer><!-- #colophon -->
    <?php
}
endif;
add_action( 'excel_blog_footer', 'excel_blog_footer_end', 50 );

if( ! function_exists( 'excel_blog_content_end' ) ) :
/**
 * Content End
*/
function excel_blog_content_end(){
    ?>
            </div><!-- .row/error-holder -->
        </div><!-- .container -->
    </div><!-- #content -->
    <?php
}
endif;
add_action( 'excel_blog_content_end', 'excel_blog_content_end', 20 );

/** Footer section */

if( ! function_exists( 'excel_blog_page_end' ) ) :
/**
 * Page End
*/
function excel_blog_page_end(){
    ?>
    </div><!-- #page -->
    <?php
}
endif;
add_action( 'excel_blog_after_footer', 'excel_blog_page_end', 20 );
