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

        if ( true !== $hero_banner_enable ) {
            return false;
        }
        // Get Hero Banner details
        $section_details = array();
        $section_details = apply_filters( 'pushpa_filter_hero_banner_section_details', $section_details );

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

        // Render Hero Banner now.
        pushpa_render_hero_banner_section( $section_details );
    }
endif;

if ( ! function_exists( 'pushpa_get_hero_banner_section_details' ) ) :
    /**
    * Hero Banner details.
    *
    * @since Pushpa 1.0.0
    * @param array $input Hero Banner details.
    */
    function pushpa_get_hero_banner_section_details( $input ) {
        $options = pushpa_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-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 Banner content details.
add_filter( 'pushpa_filter_hero_banner_section_details', 'pushpa_get_hero_banner_section_details' );

if ( ! function_exists( 'pushpa_render_hero_banner_section' ) ) :
  /**
   * Start Hero Banner
   *
   * @return string hero content
   * @since Pushpa 1.0.0
   *
   */
   function pushpa_render_hero_banner_section( $content_details = array() ) {
        $options = pushpa_get_theme_options();

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

        ?>
        <div id="pushpa_hero_banner" class="relative">
            <?php if ( is_customize_preview()):
                    pushpa_section_tooltip( 'pushpa_hero_banner' );
                endif; ?>
                <div class="wrapper">
                    <div class="hentry clear">
                        <?php foreach ($content_details as $index => $value ):
                            if ( $index % 3 == 0 ) {
                                ?>
                                <article>
                                 <div class="featured-image" style="background-image: url('<?php echo esc_url( $value['image'] ); ?>');">
                                    <div class="entry-container">
                                        <div class="entry-meta">
                                            <?php pushpa_posted_on( $value['id'] ); ?>  

                                            <span class="cat-links">
                                                <?php the_category( '', '', $value['id'] ); ?>
                                            </span>
                                        </div>

                                        <header class="entry-header">
                                            <h2 class="entry-title"><a href="<?php echo esc_url( $value['url'] ); ?>" tabindex="0"><?php echo esc_html( $value['title'] ); ?></a></h2>
                                        </header>
                                        <?php if (!empty( $options['hero_banner_btn'] ) ): ?>
                                            <div class="read-more">
                                                <a href="<?php echo esc_url($value['url']); ?>" class="btn" tabindex="0"><?php echo esc_html( $options['hero_banner_btn']); ?></a>
                                            </div>
                                        <?php endif; ?>
                                    </div><!-- .entry-container -->
                                </div><!-- .featured-image -->
                            </article>
                            <?php
                        }
                        endforeach;
                         ?>
                    </div>

                    <div class="hentry">
                        <?php 
                        $initial_content = array_shift($content_details);
                        foreach ($content_details as $index => $value ):
                            if ( $index+1 % 3 != 0 ) {
                                ?>
                                <article>
                                   <div class="featured-image" style="background-image: url('<?php echo esc_url( $value['image'] ); ?>');">
                                        <div class="entry-container">
                                            <div class="entry-meta">
                                                <?php pushpa_posted_on( $value['id'] ); ?>

                                                <span class="cat-links">
                                                    <?php the_category( '', '', $value['id'] ); ?>
                                                </span>
                                            </div>

                                            <header class="entry-header">
                                                <h2 class="entry-title"><a href="<?php echo esc_url( $value['url'] ); ?>" tabindex="0"><?php echo esc_html( $value['title'] ); ?></a></h2>
                                            </header>
                                        </div><!-- .entry-container -->
                                    </div><!-- .featured-image -->
                                </article>
                            <?php 
                            }
                        endforeach; ?>
                    </div>
                </div><!-- .wrapper -->
            </div><!-- #pushpa_hero_banner -->

    <?php }
endif;