<?php 

$options = pushpa_get_theme_options();
// Check if primary posts is enabled on frontpage
$most_recent_posts_enable = apply_filters( 'pushpa_section_status', true, 'most_recent_posts_section_enable' );

if ( true !== $most_recent_posts_enable ) {
     return false;
}

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

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

$args = array(
    'post_type'         => 'post',
    'post__in'          => ( array ) $post_ids,
    'posts_per_page'    => absint( 4 ),
    '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['auth_id']   = get_the_author_meta('ID');
$page_post['title']     = get_the_title();
$page_post['url']       = get_the_permalink();
$page_post['excerpt']   = pushpa_trim_content( 25 );
$page_post['image']     = has_post_thumbnail() ? get_the_post_thumbnail_url( get_the_id(), 'large' ) : 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;
}

?>
<div id="pushpa_most_recent_posts">
    <?php if ( is_customize_preview()):
        pushpa_section_tooltip( 'pushpa_most_recent_posts' );
    endif; ?>
    <?php foreach($input as $content) : ?>
        <article>
            <div class="popular-post-wrapper">
                <div class="featured-image" style="background-image: url('<?php echo esc_url( $content['image'] ); ?>');">
                    <a href="<?php echo esc_url( $content['url'] ); ?>" class="post-thumbnail-link"></a>
                </div><!-- .featured-image -->

                <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'] ); ?>"><?php echo esc_html( $content['title'] ); ?></a></h2>
                    </header>

                    <div class="entry-content">
                        <p><?php echo wp_kses_post($content['excerpt']); ?></p>
                    </div>
                    <?php if (!empty( $options['most_recent_posts_btn'] ) ): ?>
                        <div class="read-more">
                            <a href="<?php echo esc_url($content['url']); ?>" class="btn" tabindex="0"><?php echo esc_html( $options['most_recent_posts_btn']); ?></a>
                        </div>
                    <?php endif; ?>
                </div><!-- .entry-container -->
            </div><!-- .popular-post-wrapper -->
        </article>
    <?php endforeach; ?>
</div><!-- .pushpa_most_recent_posts -->
