<?php
/**
 * Hero Section section
 *
 * This is the template for the content of hero_section section
 *
 * @package Theme Palace
 * @subpackage FavBlog
 * @since FavBlog 1.0.0
 */
if ( ! function_exists( 'favblog_add_hero_section' ) ) :
    /**
    * Add hero_section section
    *
    *@since FavBlog 1.0.0
    */
    function favblog_add_hero_section() {
    	$options = favblog_get_theme_options();
        // Check if hero_section is enabled on frontpage
        $hero_section_enable = apply_filters( 'favblog_section_status', true, 'hero_section_enable' );

        if ( true !== $hero_section_enable ) {
            return false;
        }
        // Get hero_section section details
        $section_details = array();
        $section_details = apply_filters( 'favblog_filter_hero_section_details', $section_details );

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

        // Render hero_section section now.
        favblog_render_hero_section( $section_details );
    }
endif;

if ( ! function_exists( 'favblog_get_hero_section_details' ) ) :
    /**
    * hero_section section details.
    *
    * @since FavBlog 1.0.0
    * @param array $input hero_section section details.
    */
    function favblog_get_hero_section_details( $input ) {
        $options = favblog_get_theme_options();

        $content = array();
        $cat_id = ! empty( $options['hero_content_category'] ) ? $options['hero_content_category'] : '';
        $args = array(
            'post_type'             => 'post',
            'posts_per_page'        => 1,
            'cat'                   => absint( $cat_id ),
            '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['excerpt']   = favblog_trim_content( 20 );
                $page_post['url']       = get_the_permalink();
                $page_post['image']     = has_post_thumbnail() ? get_the_post_thumbnail_url( get_the_id(), 'post-thumbnail' ) : get_template_directory_uri() . '/assets/uploads/no-featured-image-600x450.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 section content details.
add_filter( 'favblog_filter_hero_section_details', 'favblog_get_hero_section_details' );


if ( ! function_exists( 'favblog_render_hero_section' ) ) :
  /**
   * Start hero_section section
   *
   * @return string hero_section content
   * @since FavBlog 1.0.0
   *
   */
   function favblog_render_hero_section( $content_details = array() ) {
        $options = favblog_get_theme_options();

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

        <div id="favblog_hero_section">
            
            <div id="hero-section" class="relative page-section same-background">
                <div class="wrapper">
                <?php foreach ( $content_details as $content ) : ?>

                    <article class="has-post-thumbnail clear">
                        <div class="featured-image">
                            <a href="<?php echo esc_url( $content['url'] ); ?>"><img src="<?php echo esc_url( $content['image'] ); ?>" alt="<?php echo esc_attr( $content['title'] ); ?>"></a>
                        </div><!-- .featured-image -->

                        <div class="entry-container">
                            <header class="entry-header">
                                <h2 class="entry-title"><a href="<?php echo esc_url( $content['url'] ); ?>"><?php echo esc_html( $content['title'] ); ?></a></h2>
                            </header>

                            <div class="entry-content">
                                <P><?php echo wp_kses_post( $content['excerpt'] ); ?></P>
                            </div><!-- .entry-content -->
                            
                            <?php 
                                $terms = get_the_terms($content['id'], 'post_tag') ; 
                                if(! is_wp_error($terms) && $terms) :?>
                                    <div class="entry-meta">
                                        <p class="tags-title"><?php esc_html_e('Popular Tags', 'favblog'); ?></p>
                                        <span class="tags-links">
                                                <?php foreach($terms as $term){
                                                    echo  "<a href=".esc_url(get_term_link( $term->term_id )).">" . esc_html($term->name)."</a>";
                                                }
                                                ?>
                                        </span><!-- .tags-links -->
                                    </div><!-- .entry-meta -->
                                <?php endif;
                            ?>
                        </div><!-- .entry-container -->
                    </article>
                    <?php endforeach; ?>

                    <span class="double-border-line"></span>
                </div><!-- .wrapper -->
            </div><!-- #hero-section -->

        </div>

    <?php 
    }    
endif;
