<?php
/**
 * Hero section
 *
 * This is the template for the content of hero section
 *
 * @package Theme Palace
 * @subpackage Newstation
 * @since Newstation 1.0.0
 */
if ( ! function_exists( 'newstation_add_hero_banner_section' ) ) :
    /**
    * Add hero section
    *
    *@since Newstation 1.0.0
    */
    function newstation_add_hero_banner_section() {
    	$options = newstation_get_theme_options();
        // Check if hero is enabled on frontpage
        $hero_banner_enable = apply_filters( 'newstation_section_status', true, 'hero_banner_section_enable' );

        if ( true !== $hero_banner_enable ) {
            return false;
        }
        // Get hero section details
        $section_details = array();
        $section_details = apply_filters( 'newstation_filter_hero_banner_section_details', $section_details );

        if ( empty( $section_details ) ) {
            return;
        }

        // Render hero section now.
        newstation_render_hero_banner_section( $section_details );
    }
endif;

if ( ! function_exists( 'newstation_get_hero_banner_section_details' ) ) :
    /**
    * hero section details.
    *
    * @since Newstation 1.0.0
    * @param array $input hero section details.
    */
    function newstation_get_hero_banner_section_details( $input ) {
        $options = newstation_get_theme_options();

        $content = array();
        $post_ids = array();

                for ( $i = 1; $i <= 3; $i++ ) {
                    if ( ! empty( $options['hero_banner_content_post_' . $i] ) )
                        $post_ids[] = $options['hero_banner_content_post_' . $i];
                }
                
                $args = array(
                    'post_type'         => 'post',
                    'post__in'          => ( array ) $post_ids,
                    'posts_per_page'    => absint( 3 ),
                    'orderby'           => 'post__in',
                    'ignore_sticky_posts'   => true,
                    );

        // Run The Loop.
        $query = new WP_Query( $args );
        if ( $query->have_posts() ) : 
            while ( $query->have_posts() ) : $query->the_post();
                $page_post['id']        = get_the_id();
                $page_post['title']     = get_the_title();
                $page_post['content']   = get_the_content();
                $page_post['auth_id']   = get_the_author_meta('ID');
                $page_post['url']       = get_the_permalink();
                $page_post['image']  	= has_post_thumbnail() ? get_the_post_thumbnail_url( get_the_id(), 'full' ) : get_template_directory_uri() . '/assets/uploads/no-featured-image-590x650.jpg';

                // Push to the main array.
                array_push( $content, $page_post );
            endwhile;
        endif;
        wp_reset_postdata();

        if ( ! empty( $content ) ) {
            $input = $content;
        }
        return $input;
    }
endif;
// hero section content details.
add_filter( 'newstation_filter_hero_banner_section_details', 'newstation_get_hero_banner_section_details' );

if ( ! function_exists( 'newstation_render_hero_banner_section' ) ) :
  /**
   * Start hero section
   *
   * @return string hero content
   * @since Newstation 1.0.0
   *
   */
   function newstation_render_hero_banner_section( $content_details = array() ) {
        $options = newstation_get_theme_options();

        if ( empty( $content_details ) ) {
            return;
        } ?>

        <div id="newstation_hero_banner_section">
            <div class="hero-banner-wrapper">
                <ul class="tabs hero-banner-post">

                    <?php $i = 1; foreach ($content_details as $content):
                    $active = ( $i == 1 ) ? 'active' : ''; ?>

                    <li data-tab="hero-banner-<?php echo esc_attr( $content['id'] ); ?>" class="<?php echo esc_attr( $active ); ?>">
                        <article>
                            <span class="cat-links">
                                <?php the_category( '', '', $content['id'] ); ?>
                            </span><!-- .cat-links -->
                            <div class="entry-container">
                                <header class="entry-header">
                                    <h2 class="entry-title"><a href="<?php echo esc_url( $content['url'] ); ?>" tabindex="0"><?php echo esc_html( $content['title'] ); ?></a></h2>
                                </header>
                            </div>
                            <div class="entry-meta">
                                <?php newstation_posted_on( $content['id'] ); ?>
                                <span class="min-read"><?php echo newstation_time_interval( $content['content'] ); echo esc_html__(' min read', 'newstation'); ?></span>
                                
                                <?php 
                                        $terms = get_the_terms($content['id'], 'post_tag' ) ; 

                                        if(! is_wp_error($terms) && $terms) :
                                     ?>

                                    <span class="tag-links">
                                        <?php foreach($terms as $term){
                                                    echo  "<a href=".get_term_link( $term->term_id ).">" .$term->name."</a>";
                                                }
                                                ?>
                                    </span>

                                    <?php endif; ?>
                                
                            </div>
                        </article>
                    </li>

                    <?php $i++; endforeach; ?> 

                </ul>
            </div>

            <div class="hero-banner-wrapper-post">

                <?php $i = 1; foreach ($content_details as $content):
                $count = ( $i == 1 ) ? 'active' : ''; ?>

                <div id="hero-banner-<?php echo esc_attr( $content['id'] ); ?>" class="tab-content <?php echo esc_attr( $count ); ?>">
                    <article style="background-image: url('<?php echo esc_url( $content['image'] ); ?>');">
                        <div class="overlay"></div>
                        <div class="wrapper">
                            <div class="hero-full-width">
                                <span class="cat-links">
                                    <?php the_category( '', '', $content['id'] ); ?>
                                </span><!-- .cat-links -->
                                <div class="entry-container">
                                    <header class="entry-header">
                                        <h2 class="entry-title"><a href="<?php echo esc_url( $content['url'] ); ?>" tabindex="0"><?php echo esc_html( $content['title'] ); ?></a></h2>
                                    </header>
                                </div>
                                <div class="entry-meta">
                                    <?php newstation_posted_on( $content['id'] ); ?>
                                    <span class="min-read"><?php echo newstation_time_interval( $content['content'] ); echo esc_html__(' min read', 'newstation'); ?></span>

                                    <?php 
                                        $terms = get_the_terms($content['id'], 'post_tag' ) ; 

                                        if(! is_wp_error($terms) && $terms) :
                                     ?>

                                    <span class="tag-links">
                                        <?php foreach($terms as $term){
                                                    echo  "<a href=".get_term_link( $term->term_id ).">" .$term->name."</a>";
                                                }
                                                ?>
                                    </span>

                                    <?php endif; ?>

                                </div>
                            </div>
                        </div>
                    </article>
                </div>

                <?php $i++; endforeach; ?> 

            </div>
        </div>

    <?php }
endif;