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

        if ( true !== $popular_posts_enable ) {
            return false;
        }
        // Get Popular Posts details
        $section_details = array();
        $section_details = apply_filters( 'pushpa_filter_popular_posts_section_details', $section_details );

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

        // Render Popular Posts now.
        pushpa_render_popular_posts_section( $section_details );
    }
endif;

if ( ! function_exists( 'pushpa_get_popular_posts_section_details' ) ) :
    /**
    * Popular Posts details.
    *
    * @since Pushpa 1.0.0
    * @param array $input Popular Posts details.
    */
    function pushpa_get_popular_posts_section_details( $input ) {
        $options = pushpa_get_theme_options();
        
        $content  = array();
        $post_ids = array();

        for ( $i = 1; $i <= 2; $i++ ) {
            if ( ! empty( $options['popular_posts_content_post_' . $i] ) )
                $post_ids[] = $options['popular_posts_content_post_' . $i];
        }

        $args = array(
            'post_type'         => 'post',
            'post__in'          => ( array ) $post_ids,
            'posts_per_page'    => absint( 2 ),
            '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['excerpt']   = pushpa_trim_content( 25 );
                $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;
// Popular Posts content details.
add_filter( 'pushpa_filter_popular_posts_section_details', 'pushpa_get_popular_posts_section_details' );

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

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

        ?>
        <div id="pushpa_popular_posts" class="relative page-section same-background">
            <div class="wrapper">
                <?php if ( !empty( $options['popular_posts_title'] ) ) : ?>
                    <div class="section-header">
                        <h2 class="section-title"><?php echo esc_html( $options['popular_posts_title'] ); ?></h2>
                    </div><!-- .section-header -->
                <?php endif; ?>

                <div class="section-content col-2 clear">
                    <?php foreach ( $content_details as $content ) : ?>
                        <article class="has-post-thumbnail">
                            <div class="featured-image" style="background-image: url('<?php echo esc_url($content['image']);?>');"></div>
                            <div class="entry-container">
                                <div class="entry-meta">
                                    <span class="posted-on">
                                        <?php pushpa_posted_on($content['id']); ?>
                                    </span><!-- .posted-on -->  

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

                                <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 class="entry-content">
                                    <p><?php echo wp_kses_post( $content['excerpt'] ); ?></p>
                                </div>
                            </div><!-- .entry-container -->
                        </article>
                    <?php endforeach; ?>
                </div><!-- .section-content -->
            </div><!-- .wrapper -->
        </div><!-- #featured-posts -->

    <?php }
endif;