<?php 

/**
 * theme template hooks
 *
 * @package specter
 */

/**
 * site header
 */
add_action( 'specter_header', 'specter_template_header' );
function specter_template_header(){ ?>
	<header id="site-header" class="visible-xs">
		<nav class="navbar navbar-default" role="navigation">
			<!-- Brand and toggle get grouped for better mobile display -->
			<div class="navbar-header">

				<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#main-navigation">
				<span class="sr-only"><?php esc_html_e( 'Toggle navigation','specter' ); ?></span>
				<span class="icon-bar"></span>
				<span class="icon-bar"></span>
				<span class="icon-bar"></span>
				</button>

				<?php if ( function_exists( 'the_custom_logo' ) && has_custom_logo() ): 
                $specter_custom_logo_id = get_theme_mod( 'custom_logo' );
                $image = wp_get_attachment_image_src( $specter_custom_logo_id,'full');
                ?>
                <h1 id="logo"><a href='<?php echo esc_url( home_url( '/' ) ); ?>' title='<?php echo esc_attr( get_bloginfo( 'name', 'display' ) ); ?>' rel='home'><img src="<?php echo esc_url( $image[0] ); ?>"></a></h1>
                <?php else : ?>
                <h1 id="logo"><a href='<?php echo esc_url( home_url( '/' ) ); ?>' title='<?php echo esc_attr( get_bloginfo( 'name', 'display' ) ); ?>' rel='home'><?php echo esc_html( bloginfo('name') ); ?></a></h1>
                <p class="site-desc"><?php echo esc_html( bloginfo('description') ); ?></p>
                <?php endif; ?>

			</div>

			<div class="collapse navbar-collapse" id="main-navigation">
				<?php 
				wp_nav_menu( array(
				'theme_location'    => 'main-nav',
				'depth'             => 2,
				'container'         => 'false',
				'container_class'   => 'collapse navbar-collapse',
				'menu_class'        => 'nav navbar-nav navbar-right',
				'fallback_cb'       => 'specter_primary_menu_fallback',
				'walker'            => new wp_bootstrap_navwalker())
				);
				
				?>
			</div><!-- /.navbar-collapse -->
		</nav>
        <?php 
            /**
             * Functions hooked in to specter_site_info action.
             *
             * @hooked specter_template_important_info 
             */
             do_action('specter_site_info'); ?>
	</header>

    <header id="site-header" class="head-desktop">
        <nav class="navbar navbar-default" role="navigation">
            <?php if ( function_exists( 'the_custom_logo' ) && has_custom_logo() ): 
            $specter_custom_logo_id = get_theme_mod( 'custom_logo' );
            $image = wp_get_attachment_image_src( $specter_custom_logo_id,'full');
            ?>
            <h1 id="logo"><a href='<?php echo esc_url( home_url( '/' ) ); ?>' title='<?php echo esc_attr( get_bloginfo( 'name', 'display' ) ); ?>' rel='home'><img src="<?php echo esc_url( $image[0] ); ?>"></a></h1>
            <?php else : ?>
            <h1 id="logo"><a href='<?php echo esc_url( home_url( '/' ) ); ?>' title='<?php echo esc_attr( get_bloginfo( 'name', 'display' ) ); ?>' rel='home'><?php echo esc_html( bloginfo('name') ); ?></a></h1>
            <p class="site-desc"><?php echo esc_html( bloginfo('description') ); ?></p>
            <?php endif; ?>
        </nav>
        <div class="nav-desktop">
            <div class="row">
                <div class="col-md-6">
                    <div id="main-navigation">
                        <?php 
                        wp_nav_menu( array(
                        'theme_location'    => 'main-nav',
                        'depth'             => 2,
                        'container'         => 'false',
                        'container_class'   => 'collapse navbar-collapse',
                        'menu_class'        => 'nav navbar-nav navbar-right',
                        'fallback_cb'       => 'specter_primary_menu_fallback',
                        'walker'            => new wp_bootstrap_navwalker())
                        );
                        
                        ?>
                    </div><!-- /.navbar-collapse -->
                </div>
                <div class="col-md-6">
                        <?php 
                        /**
                         * Functions hooked in to specter_site_info action.
                         *
                         * @hooked specter_template_important_info 
                         */
                         do_action('specter_site_info'); ?>
                </div>
                <span class="clearfix"></span>
            </div>
        </div>
    </header>
<?php
}

/**
 * related posts
 */
add_action( 'specter_relate_posts', 'specter_template_related_posts' );
function specter_template_related_posts(){ 
	global $post;
	$related = get_posts( array( 'category__in' => wp_get_post_categories($post->ID), 'numberposts' => 3, 'post__not_in' => array($post->ID) ) );
	if ( get_theme_mod('specter_related_posts') ):
	$related_class = 'related-hide';
	else :
	$related_class = '';
	endif;
	if (!empty($related)): ?>

		<div class="related-posts<?php echo " " . esc_attr( $related_class ); ?>">
			<h3 class="entry-footer-title"><?php esc_html_e('You may also like ','specter'); ?></h3>
			<div class="row">
			<?php if( $related ): foreach( $related as $post ) { ?>
			<?php setup_postdata($post); ?>
			
				<div class="col-md-4 related-item">
					<div class="related-image">
						<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>">
							<?php $image_thumb = specter_catch_that_image_thumb(); $gallery_thumb = specter_catch_gallery_image_thumb(); 
							if ( has_post_thumbnail()):
							the_post_thumbnail('600x600');  
							elseif(has_post_format('gallery') && !empty($gallery_thumb)): 
							echo $gallery_thumb; 
							elseif(has_post_format('image') && !empty($image_thumb)): 
							echo '<img src="'. esc_url($image_thumb) . '">'; 
							else: ?>
							<?php $blank = get_template_directory_uri() . '/assets/images/blank.jpg'; ?>
							<img src="<?php echo esc_url($blank); ?>">
							<?php endif; ?>
						</a>
					</div>

					<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>

				</div>
			

			<?php } endif; wp_reset_postdata(); ?>

			</div>

		</div>
	<?php endif; 
}

/**
 * Site information
 */
add_action( 'specter_site_info', 'specter_template_important_info' );
function specter_template_important_info(){ 

    if(shortcode_exists( 'contact_details' )){

    $location   = strip_tags( do_shortcode('[contact_details format="horizontal" fields="address"]') );
    $phone      = strip_tags( do_shortcode('[contact_details format="horizontal" fields="phone"]') );
    $social     = do_shortcode('[contact_details type="social" format="horizontal" fields="twitter,facebook,instagram,pinterest,google_plus,linkedin,vimeo,youtube,github"]');
    $email      = strip_tags( do_shortcode('[contact_details format="horizontal" fields="email"]') );

    ?>
        <ul id="site-info">
            <?php if( $location ){ ?>
			<li class="site-location">
				<?php echo esc_html( $location ); ?>
			</li>
            <?php } if( $phone ){ ?>
			<li class="site-phone">
				<?php echo esc_html( $phone ); ?>
			</li>
            <?php } if( $email ){ ?>
			<li class="site-email">
				<?php echo esc_html( $email ); ?>
			</li>
            <?php } ?>
        </ul>
<?php
    }
}


/**
 * Homepage Sections
 */

add_action( 'specter_home', 'specter_template_section_1', 10 );
add_action( 'specter_home', 'specter_template_section_2', 15 );
add_action( 'specter_home', 'specter_template_section_3', 20 );
add_action( 'specter_home', 'specter_template_section_4', 25 );
add_action( 'specter_home', 'specter_template_section_5', 25 );

function specter_template_section_1(){

        $get_sec_1_id = get_theme_mod( 'specter_section_1' );
        $post_1 = get_post( $get_sec_1_id );
        $thumb_url_1 = wp_get_attachment_url( get_post_thumbnail_id($get_sec_1_id) );
        $content_1 = apply_filters('the_content', $post_1->post_content);

        if( $get_sec_1_id ) :
    ?>
        <section id="section-1" style="<?php if( $thumb_url_1 ) { ?> background-image: url( <?php echo esc_url( $thumb_url_1 ); ?> ); <?php } ?>">
            <div class="section-content container">
                <?php echo $content_1; ?>
            </div>
            <span class="clearfix"></span>
        </section>
    <?php endif;

}

function specter_template_section_2(){

        $get_sec_2_id = get_theme_mod( 'specter_section_2' );
        $post_2 = get_post( $get_sec_2_id );
        $content_2 = apply_filters('the_content', $post_2->post_content);

        if( $get_sec_2_id ) :
    ?>
        
        <section id="section-2">
            <div class="section-content container">
                <?php echo $content_2; ?>
            </div>
            <span class="clearfix"></span>
        </section>
    
    <?php endif; 
}

function specter_template_section_3(){

        $get_sec_3_id = get_theme_mod( 'specter_section_3' );
        $post_3 = get_post( $get_sec_3_id );
        $content_3 = apply_filters('the_content', $post_3->post_content);

        if( $get_sec_3_id ) :
    ?>
        
        <section id="section-3">
            <div class="section-content">
                <?php echo $content_3; ?>
            </div>
            <span class="clearfix"></span>
        </section>
    
    <?php endif; 
}

function specter_template_section_4(){

        $get_sec_4_id = get_theme_mod( 'specter_section_4' );
        $post_4 = get_post( $get_sec_4_id );
        $content_4 = apply_filters('the_content', $post_4->post_content);

        if( $get_sec_4_id ) :
    ?>
        
        <section id="section-4">
            <div class="section-content container">
                <?php echo $content_4; ?>
            </div>
            <span class="clearfix"></span>
        </section>
    
    <?php endif; 
}

function specter_template_section_5(){

        $get_sec_5_id = get_theme_mod( 'specter_section_5' );
        $post_5 = get_post( $get_sec_5_id );
        $content_5 = apply_filters('the_content', $post_5->post_content);

        if( $get_sec_5_id ) :
    ?>
        
        <section id="section-5">
            <div class="section-content">
                <?php echo $content_5; ?>
            </div>
            <span class="clearfix"></span>
        </section>
    
    <?php endif; 
}

/**
 * Footer Hooks
 */
add_action( 'specter_footer', 'specter_template_footer_widgets', 10 );
add_action( 'specter_footer', 'specter_template_copyright', 15 );

function specter_template_footer_widgets(){ 
	if ( is_active_sidebar( 'footer-1' ) || is_active_sidebar( 'footer-2' ) || is_active_sidebar( 'footer-3' ) || is_active_sidebar( 'footer-4' ) ) : ?>
    <div class="footer-area">
        <div class="row">
            <div class="footer-widgets wrap">
                    <div class="col-sm-4 footer-item"><?php dynamic_sidebar( 'footer-1' ); ?></div>
                    <div class="col-sm-4 footer-item"><?php dynamic_sidebar( 'footer-2' ); ?></div>
                    <div class="col-sm-4 footer-item"><?php dynamic_sidebar( 'footer-3' ); ?></div>
                
                <span class="clearfix"></span>
            </div>
        </div>
    </div>
    <?php endif; ?>
<?php
}

function specter_template_copyright(){ ?>
    <div class="footer-copyright">
        &#169; <?php echo date_i18n(__('Y','specter')) . ' '; bloginfo( 'name' ); ?>
        <span><?php if(is_home() || is_front_page()): ?>
            - <?php echo __('Built with','specter'); ?> <a href="<?php echo esc_url( __( 'http://mylawfirm.online/', 'specter' ) ); ?>"><?php printf( esc_html( '%s', 'specter' ), 'MyLawfirm.Online' ); ?></a> <span><?php esc_html_e('and','specter'); ?></span> <a href="<?php echo esc_url( __( 'https://wordpress.org/', 'specter' ) ); ?>"><?php printf( esc_html( '%s', 'specter' ), 'WordPress' ); ?></a>
        <?php endif; ?>
        </span>
    </div>
<?php
}