<?php
/**
 * Excel Business template functions
 *
 * @link https://developer.wordpress.org/themes/basics/theme-functions/
 *
 * @package Excel_Business
 */
 
if( ! function_exists( 'excel_business_doctype' ) ) :
/**
 * Doctype Declaration
*/
function excel_business_doctype(){
    ?>
    <!DOCTYPE html>
    <html <?php language_attributes(); ?>>
    <?php
}
endif;
add_action( 'excel_business_doctype', 'excel_business_doctype' );

if( ! function_exists( 'excel_business_head' ) ) :
/**
 * Before wp_head 
*/
function excel_business_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_business_before_wp_head', 'excel_business_head' );

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

if( ! function_exists( 'excel_business_header' ) ) :
/**
 * Header Start
*/
function excel_business_header(){ 
    
    $email   = get_theme_mod( 'header_email', 'info@exceltheme.com' );
    $phone   = get_theme_mod( 'header_phone', '(888) 123-4567' );
    $socials = get_theme_mod( 'social_links', array(
                    array(
                        'url'   => 'http://facebook.com',
                        'font'  => 'fa-facebook',                        
                    ),
                    array(
                        'url'   => 'http://twitter.com',
                        'font'  => 'fa-twitter',                        
                    ),
                    array(
                        'url'   => 'http://linkedin.com',
                        'font'  => 'fa-linkedin',                        
                    ),
                    array(
                        'url'   => 'http://plus.google.com',
                        'font'  => 'fa-google-plus',                        
                    ) 
                ) );
    ?>
    <header id="masthead" class="site-header" role="banner">
		
        <div class="header-t">
			<div class="container">
				<div class="row">
					<?php 
                    if( $email || $phone ){ 
                        echo '<div class="left">';
                        if( $email && is_email( $email ) ) echo '<a href="' . esc_url( 'mailto:'. sanitize_email( $email ) ) . '" class="email-link">' . esc_html( $email ) . '</a>';
                        if( $phone ) echo '<a href="' . esc_url( 'tel:' . preg_replace('/\D/', '', $phone) ) . '" class="tel-link">' . esc_html( $phone ) . '</a>';
                        echo '</div>';
					} 
                    ?>
                    <div class="right">
						<div class="btn-search">
							<a href="javascript:void(0);" class="fa fa-search"></a>
							<div class="form-holder">
								<?php get_search_form(); ?>
							</div>
						</div>
                        <?php if( $socials ){ ?>
                        <ul class="social-networks">
							<?php 
                            foreach( $socials as $social ){ 
                                if( $social['url'] && $social['font'] ) echo '<li><a href="' . esc_url( $social['url'] ) . '" target="_blank" class="fa ' . esc_attr( $social['font'] ) .'"></a></li>';                             
                            }
                            ?>
						</ul>				
                        <?php } ?>		
					</div>
				</div>
			</div>
		</div>
		
        <div class="header-b">
			<div class="container">
				
                <div class="site-branding">
					<?php
            			if( function_exists( 'has_custom_logo' ) && has_custom_logo() ){
                            the_custom_logo();
                        }
                        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
            			}
            
            			$description = get_bloginfo( 'description', 'display' );
            			if ( $description || is_customize_preview() ){ ?>
            				<p class="site-description"><?php echo esc_html( $description ); /* WPCS: xss ok. */ ?></p>
            			<?php
            			}
                    ?>                    
				</div><!-- .site-branding -->
				
                <div id="mobile-header">
				    <a id="responsive-menu-button" href="#sidr-main"><i class="fa fa-navicon"></i></a>
				</div>
				<nav id="site-navigation" class="main-navigation" role="navigation">			
                    <?php wp_nav_menu( array( 'theme_location' => 'primary', 'menu_id' => 'primary-menu' ) ); ?>
                </nav><!-- #site-navigation -->
                
			</div>
		</div>
	</header>
    <?php
}
endif;
add_action( 'excel_business_header', 'excel_business_header', 20 );

if( ! function_exists( 'excel_business_breadcrumb' ) ) :
/**
 * Breadcrumb 
*/
function excel_business_breadcrumb(){
    
    global $post;
    $post_page  = get_option( 'page_for_posts' ); //The ID of the page that displays posts.
    $show_front = get_option( 'show_on_front' ); //What to show on the front page    
    $home       = get_theme_mod( 'breadcrumb_home_text', __( 'Home', 'excel-business' ) ); // text for the 'Home' link
    $delimiter  = get_theme_mod( 'breadcrumb_separator', __( '/', 'excel-business' ) ); // delimiter between crumbs
    $before     = '<span class="current">'; // tag before the current crumb
    $after      = '</span>'; // tag after the current crumb
    
    if( get_theme_mod( 'ed_breadcrumb' ) && ! is_front_page() ){
        
        echo '<div class="breadcrumbs"><div class="container">';
        echo '<div id="crumbs"><a href="' . esc_url( home_url() ) . '">' . esc_html( $home ) . '</a> <span class="separator">' . esc_html( $delimiter ) . '</span> ';
        
        if( is_home() ){
            
            echo $before . esc_html( single_post_title( '', false ) ) . $after;
            
        }elseif( is_category() ){
            
            $thisCat = get_category( get_query_var( 'cat' ), false );
            
            if( $show_front === 'page' && $post_page ){ //If static blog post page is set
                $p = get_post( $post_page );
                echo ' <a href="' . esc_url( get_permalink( $post_page ) ) . '">' . esc_html( $p->post_title ) . '</a> <span class="separator">' . esc_html( $delimiter ) . '</span> ';  
            }
            
            if ( $thisCat->parent != 0 ) echo get_category_parents( $thisCat->parent, TRUE, ' <span class="separator">' . $delimiter . '</span> ' );
            echo $before .  esc_html( single_cat_title( '', false ) ) . $after;
        
        }elseif( is_tag() ){
            
            echo $before . esc_html( single_tag_title( '', false ) ) . $after;
     
        }elseif( is_author() ){
            
            global $author;
            $userdata = get_userdata( $author );
            echo $before . esc_html( $userdata->display_name ) . $after;
     
        }elseif( is_search() ){
            
            echo $before . esc_html__( 'Search Results for "', 'excel-business' ) . esc_html( get_search_query() ) . esc_html__( '"', 'excel-business' ) . $after;
        
        }elseif( is_day() ){
            
            echo '<a href="' . esc_url( get_year_link( get_the_time( __( 'Y', 'excel-business' ) ) ) ) . '">' . esc_html( get_the_time( __( 'Y', 'excel-business' ) ) ) . '</a> <span class="separator">' . esc_html( $delimiter ) . '</span> ';
            echo '<a href="' . esc_url( get_month_link( get_the_time( __( 'Y', 'excel-business' ) ), get_the_time( __( 'm', 'excel-business' ) ) ) ) . '">' . esc_html( get_the_time( __( 'F', 'excel-business' ) ) ) . '</a> <span class="separator">' . esc_html( $delimiter ) . '</span> ';
            echo $before . esc_html( get_the_time( __( 'd', 'excel-business' ) ) ) . $after;
        
        }elseif( is_month() ){
            
            echo '<a href="' . esc_url( get_year_link( get_the_time( __( 'Y', 'excel-business' ) ) ) ) . '">' . esc_html( get_the_time( __( 'Y', 'excel-business' ) ) ) . '</a> <span class="separator">' . esc_html( $delimiter ) . '</span> ';
            echo $before . esc_html( get_the_time( __( 'F', 'excel-business' ) ) ) . $after;
        
        }elseif( is_year() ){
            
            echo $before . esc_html( get_the_time( __( 'Y', 'excel-business' ) ) ) . $after;
    
        }elseif( is_single() && !is_attachment() ){
            
            if( get_post_type() != 'post' ){
                
                $post_type = get_post_type_object( get_post_type() );
                
                if( $post_type->has_archive == true ){// For CPT Archive Link
                   
                   // Add support for a non-standard label of 'archive_title' (special use case).
                   $label = !empty( $post_type->labels->archive_title ) ? $post_type->labels->archive_title : $post_type->labels->name;
                   printf( '<a href="%1$s">%2$s</a>', esc_url( get_post_type_archive_link( get_post_type() ) ), $label );
                   echo '<span class="separator">' . esc_html( $delimiter ) . '</span> ';
    
                }
                echo $before . esc_html( get_the_title() ) . $after;
                
            }else{ //For Post
                
                $cat_object       = get_the_category();
                $potential_parent = 0;
                
                if( $show_front === 'page' && $post_page ){ //If static blog post page is set
                    $p = get_post( $post_page );
                    echo ' <a href="' . esc_url( get_permalink( $post_page ) ) . '">' . esc_html( $p->post_title ) . '</a> <span class="separator">' . esc_html( $delimiter ) . '</span> ';  
                }
                
                if( is_array( $cat_object ) ){ //Getting category hierarchy if any
        
        			//Now try to find the deepest term of those that we know of
        			$use_term = key( $cat_object );
        			foreach( $cat_object as $key => $object )
        			{
        				//Can't use the next($cat_object) trick since order is unknown
        				if( $object->parent > 0  && ( $potential_parent === 0 || $object->parent === $potential_parent ) ){
        					$use_term = $key;
        					$potential_parent = $object->term_id;
        				}
        			}
                    
        			$cat = $cat_object[$use_term];
              
                    $cats = get_category_parents( $cat, TRUE, ' <span class="separator">' . esc_html( $delimiter ) . '</span> ' );
                    $cats = preg_replace( "#^(.+)\s$delimiter\s$#", "$1", $cats ); //NEED TO CHECK THIS
                    echo $cats;
                }
    
                echo $before . esc_html( get_the_title() ) . $after;
                
            }
        
        }elseif( !is_single() && !is_page() && get_post_type() != 'post' && !is_404() ){
            
            $post_type = get_post_type_object(get_post_type());
            if( get_query_var('paged') ){
                echo '<a href="' . esc_url( get_post_type_archive_link( $post_type->name ) ) . '">' . esc_html( $post_type->label ) . '</a>';
                echo ' <span class="separator">' . esc_html( $delimiter ) . '</span> ' . $before . sprintf( __('Page %s', 'excel-business'), get_query_var('paged') ) . $after;
            }else{
                echo $before . esc_html( $post_type->label ) . $after;
            }
    
        }elseif( is_attachment() ){
            
            $parent = get_post( $post->post_parent );
            $cat = get_the_category( $parent->ID ); 
            if( $cat ){
                $cat = $cat[0];
                echo get_category_parents( $cat, TRUE, ' <span class="separator">' . esc_html( $delimiter ) . '</span> ');
                echo '<a href="' . esc_url( get_permalink( $parent ) ) . '">' . esc_html( $parent->post_title ) . '</a>' . ' <span class="separator">' . esc_html( $delimiter ) . '</span> ';
            }
            echo  $before . esc_html( get_the_title() ) . $after;
        
        }elseif( is_page() && !$post->post_parent ){
            
            echo $before . esc_html( get_the_title() ) . $after;
    
        }elseif( is_page() && $post->post_parent ){
            
            $parent_id  = $post->post_parent;
            $breadcrumbs = array();
            
            while( $parent_id ){
                $page = get_post( $parent_id );
                $breadcrumbs[] = '<a href="' . esc_url( get_permalink( $page->ID ) ) . '">' . esc_html( get_the_title( $page->ID ) ) . '</a>';
                $parent_id  = $page->post_parent;
            }
            $breadcrumbs = array_reverse( $breadcrumbs );
            for ( $i = 0; $i < count( $breadcrumbs) ; $i++ ){
                echo $breadcrumbs[$i];
                if ( $i != count( $breadcrumbs ) - 1 ) echo ' <span class="separator">' . esc_html( $delimiter ) . '</span> ';
            }
            echo ' <span class="separator">' . esc_html( $delimiter ) . '</span> ' . $before . esc_html( get_the_title() ) . $after;
        
        }elseif( is_404() ){
            echo $before . esc_html__( '404 Error - Page Not Found', 'excel-business' ) . $after;
        }
        
        if( get_query_var('paged') ) echo __( ' (Page', 'excel-business' ) . ' ' . get_query_var('paged') . __( ')', 'excel-business' );
        
        echo '</div>'; //<!-- #crumbs -->
        echo '</div></div>'; //<!-- .container/breadcrumbs -->
    }
}
endif;
add_action( 'excel_business_before_content', 'excel_business_breadcrumb' );

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

if( ! function_exists( 'excel_business_page_header' ) ) :
/**
 * Page Header
*/
function excel_business_page_header(){ ?>
    <header class="page-header">
        <?php
            if ( is_home() && ! is_front_page() ){ //blog page ?>
                <h1 class="page-title"><?php single_post_title(); ?></h1>
                <?php
			}	
            
            if( is_archive() ){ //archive page       
                the_archive_title( '<h1 class="page-title">', '</h1>' );
				the_archive_description( '<div class="archive-description">', '</div>' );	
            }
            
            if( is_search() ){ //search page ?>
                <h1 class="page-title"><?php printf( esc_html__( 'Search Results for: %s', 'excel-business' ), '<span>' . get_search_query() . '</span>' ); ?></h1>                
                <?php
            }
            
            if( is_404() ){ // 404 page ?>
                <h1 class="page-title"><?php esc_html_e( 'Oops! That page can&rsquo;t be found.', 'excel-business' ); ?></h1>
                <?php
            }
            ?>
    </header><!-- .page-header -->
    <?php
}
endif;
add_action( 'excel_business_page_header', 'excel_business_page_header' );

if( ! function_exists( 'excel_business_entry_header' ) ) :
/**
 * Entry Header
*/
function excel_business_entry_header(){ ?>
    <header class="entry-header">
		<?php
			if( is_single() ){ 
                the_title( '<h1 class="entry-title">', '</h1>' );
                if ( 'post' === get_post_type() ){ ?>
            		<div class="entry-meta">
            			<?php
                            excel_business_posted_on();
                            excel_business_byline();
                            excel_business_comment_link();                        
                        ?>
            		</div><!-- .entry-meta -->
            		<?php
 		         }
            }elseif( is_page() ){
                the_title( '<h1 class="page-title">', '</h1>' );
            }else{
                the_title( '<h2 class="entry-title"><a href="' . esc_url( get_permalink() ) . '" rel="bookmark">', '</a></h2>' );
            }            
		?>
	</header><!-- .entry-header -->
    <?php    
}
endif;
add_action( 'excel_business_before_post_entry_content', 'excel_business_entry_header', 15 );
add_action( 'excel_business_entry_content', 'excel_business_entry_header', 15 );

if( ! function_exists( 'excel_business_post_thumbnail' ) ) :
/**
 * Posts Thumbnail
*/
function excel_business_post_thumbnail(){ 
    if( is_singular() ){
        $img_size = 'excel-business-image';    
    }else{
        $img_size = 'excel-business-blog';
    }
    
    if( has_post_thumbnail() ){
        echo '<div class="img-holder">';
        echo is_singular() ? '<div class="post-thumbnail">' : '<a href="' . esc_url( get_the_permalink() ) . '" class="post-thumbnail">';
        the_post_thumbnail( $img_size ); 
        echo is_singular() ? '</div>' : '</a>';
    	echo '</div>';
    }   
}
endif;
add_action( 'excel_business_before_post_entry_content', 'excel_business_post_thumbnail', 25 );
add_action( 'excel_business_before_entry_content', 'excel_business_post_thumbnail' );

if( ! function_exists( 'excel_business_entry_content' ) ) :
/**
 * Entry Content
*/
function excel_business_entry_content(){ ?>
    <div class="entry-content">
		<?php
			if( is_singular() ){
            the_content( sprintf(
				/* translators: %s: Name of current post. */
				wp_kses( __( 'Continue reading %s <span class="meta-nav">&rarr;</span>', 'excel-business' ), array( 'span' => array( 'class' => array() ) ) ),
				the_title( '<span class="screen-reader-text">"', '"</span>', false )
			) );

			wp_link_pages( array(
				'before' => '<div class="page-links">' . esc_html__( 'Pages:', 'excel-business' ),
				'after'  => '</div>',
			) );
            }else{
                the_excerpt();
            }            
		?>
	</div><!-- .entry-content -->
    <?php
}
endif;
add_action( 'excel_business_post_entry_content', 'excel_business_entry_content' );
add_action( 'excel_business_entry_content', 'excel_business_entry_content', 25 );

if( ! function_exists( 'excel_business_entry_footer' ) ) :
/**
 * Entry Footer
*/
function excel_business_entry_footer(){ 
    $readmore = get_theme_mod( 'readmore', __( 'Read More...', 'excel-business' ) ); ?>
    <footer class="entry-footer">
    <?php
        if( is_singular() ){
            if( is_single() ){ 
                excel_business_cat_tag();
            }    
        }else{ ?>
            <div class="btn-holder"><a href="<?php the_permalink(); ?>"><?php echo esc_html( $readmore );?></a></div>
    		<div class="entry-meta">
    			<?php
                    excel_business_posted_on();
                    excel_business_byline();
                    excel_business_comment_link();
                ?>            
    		</div>
            <?php
        }
        if ( get_edit_post_link() ){ 
			edit_post_link(
				sprintf(
					/* translators: %s: Name of current post */
					esc_html__( 'Edit %s', 'excel-business' ),
					the_title( '<span class="screen-reader-text">"', '"</span>', false )
				),
				'<span class="edit-link">',
				'</span>'
			);
        }
    ?>
	</footer><!-- .entry-footer -->
    <?php    
}
endif;
add_action( 'excel_business_after_post_entry_content', 'excel_business_entry_footer' );
add_action( 'excel_business_after_entry_content', 'excel_business_entry_footer' );

if( ! function_exists( 'excel_business_pagination' ) ) :
/**
 * Pagination
*/
function excel_business_pagination(){
    if( is_single() ){
        the_post_navigation();
    }else{
        the_posts_pagination( array(
            'prev_text'          => __( 'Prev', 'excel-business' ),
            'next_text'          => __( 'Next', 'excel-business' ),
            'before_page_number' => '<span class="meta-nav screen-reader-text">' . __( 'Page', 'excel-business' ) . ' </span>',
         ) );
    }
}
endif;
add_action( 'excel_business_after_loop', 'excel_business_pagination' );
add_action( 'excel_business_after_post_content', 'excel_business_pagination', 15 );

if( ! function_exists( 'excel_business_author' ) ) :
/**
 * Author bio
*/
function excel_business_author(){ 
    if( 'post' === get_post_type() && 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">
			<h3 class="author-name"><?php echo esc_html( get_the_author_meta( 'display_name' ) ); ?></h3>
			<?php echo wpautop( wp_kses_post( get_the_author_meta( 'description' ) ) ); ?>
        </div>
	</section>
    <?php                    
    }
}
endif;
add_action( 'excel_business_after_post_content', 'excel_business_author', 20 );

if( ! function_exists( 'excel_business_comment' ) ) :
/**
 * Comment
*/
function excel_business_comment(){
    // If comments are open or we have at least one comment, load up the comment template.
	if ( comments_open() || get_comments_number() ) :
		comments_template();
	endif;
}
endif;
add_action( 'excel_business_after_post_content', 'excel_business_comment', 25 );
add_action( 'excel_business_after_page_content', 'excel_business_comment' );

if( ! function_exists( 'excel_business_content_end' ) ) :
/**
 * Content End
*/
function excel_business_content_end(){
    $home_sections = excel_business_get_home_sections();
    
    if( ! ( is_front_page() && ! is_home() && $home_sections ) ){ ?>
                </div><!-- .row/not-found -->
            </div><!-- .container -->
        </div><!-- #content -->
        <?php
    }
}
endif;
add_action( 'excel_business_after_content', 'excel_business_content_end', 20 );

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

if( ! function_exists( 'excel_business_footer_top' ) ) :
/**
 * Footer Top
*/
function excel_business_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_business_footer', 'excel_business_footer_top', 30 );

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

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

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