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

        if ( true !== $recent_post_enable ) {
            return false;
        }
        // Get recent_post section details
        $section_details = array();
        $section_details = apply_filters( 'favblog_filter_recent_post_section_details', $section_details );

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

        // Render recent_post section now.
        favblog_render_recent_post_section( $section_details );
    }
endif;

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

        // Content type.
        
        $content = array();
        $cat_id = ! empty( $options['recent_post_content_category'] ) ? $options['recent_post_content_category'] : '';
        $args = array(
            'post_type'             => 'post',
            'posts_per_page'        => 4,
            '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['author_id']     = get_post_field('post_author', get_the_ID());
                $page_post['title']         = get_the_title();
                $page_post['excerpt']       = (has_excerpt( get_the_ID() )) ? get_the_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';
                $page_post['image_large']   = has_post_thumbnail() ? get_the_post_thumbnail_url( get_the_id(), 'medium_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;
        }
        return $input;
    }
endif;
// recent_post section content details.
add_filter( 'favblog_filter_recent_post_section_details', 'favblog_get_recent_post_section_details' );


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

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

        <div id="favblog_recent_post_section">

        <div id="content-wrapper" class="page-section">
            <div class="wrapper">
                <div id="primary" class="content-area">
                    <main id="main" class="site-main" role="main">
                        <div id="most-recent-posts" class="archive-blog-wrapper">
                            <div class="section-header">
                                <?php if( !empty( $options['recent_post_title'] || !empty($options['recent_post_sub_title'])) ):
                                    echo ($options['recent_post_sub_title']) ? sprintf( '<p class="section-subtitle">%s</p>',  esc_html( $options['recent_post_sub_title'] ) ) : ''; 
                                    echo ($options['recent_post_title']) ? sprintf( '<h2 class="section-title">%s</h2>',  esc_html( $options['recent_post_title'] ) ) : ''; ?>
                                <?php endif; ?>
                            </div><!-- .section-header -->

                            <div class="col-2 clear">
                                <?php foreach ( $content_details as $key=>$content ) : ?>

                                    <article>
                                        <div class="popular-post-wrapper">
                                            <div class="featured-image" style="background-image: url('<?php echo esc_url( (($key+1)%3 == 0) ? $content['image_large'] : $content['image'] ); ?>');">
                                                <a href="<?php echo esc_url( $content['url'] ); ?>" class="post-thumbnail-link" title="<?php echo esc_attr($content['title']); ?>"></a>
                                            </div><!-- .featured-image -->

                                            <div class="entry-container">
                                                <span class="cat-links">
                                                    <?php the_category( ', ', '', $content['id'] ); ?>
                                                </span><!-- .cat-links -->
                                                
                                                <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 -->

                                                <div class="admin-profile">
                                                    <div class="admin-container">
                                                        <div class="admin-header">
                                                            <h2 class="admin-name"><a href="<?php echo esc_url( get_author_posts_url( $content['author_id']) ); ?>"><?php echo get_the_author_meta('display_name', $content['author_id']); ?></a></h2>
                                                        </div><!-- .admin-header -->

                                                        <div class="entry-meta">
                                                            <?php favblog_posted_on($content['id']); ?>
                                                        </div><!-- .entry-meta -->
                                                    </div><!-- .entry-container -->
                                                </div><!-- .admin-profile -->
                                            </div><!-- .entry-container -->
                                        </div><!-- .popular-post-wrapper -->
                                    </article>
                                <?php endforeach; ?>
                            </div><!-- .col-2 -->
                        </div><!-- .archive-blog-wrapper -->
                    </main><!-- #main -->
                </div><!-- #primary -->
                <?php if(is_active_sidebar( 'main-section-sidebar' )) : ?>
                    <aside id="secondary" class="widget-area" role="complementary">
                        <?php  dynamic_sidebar( 'main-section-sidebar' ); ?>
                    </aside><!-- #secondary -->
                <?php endif ; ?>
            </div><!-- .wrapper -->
        </div><!-- #content-wrapper -->

        </div>
        
    <?php 
    }    
endif;