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

        if ( true !== $popular_post_enable ) {
            return false;
        }
        // Get popular_post section details
        $section_details = array();
        $section_details = apply_filters( 'favblog_filter_popular_post_section_details', $section_details );

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

        // Render popular_post section now.
        favblog_render_popular_post_section( $section_details );
    }
endif;

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

        $content = array();
        $cat_id = ! empty( $options['popular_post_content_category'] ) ? $options['popular_post_content_category'] : '';
        $args = array(
            'post_type'             => 'post',
            'posts_per_page'        => 3,
            '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['author_id'] = get_post_field('post_author', get_the_ID());
                $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;
// popular_post section content details.
add_filter( 'favblog_filter_popular_post_section_details', 'favblog_get_popular_post_section_details' );


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

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

        <div id="favblog_popular_post_section">

            <div id="popular-posts" class="relative page-section same-background">
                <div class="wrapper">
                    <div class="section-header">
                        <?php if( !empty( $options['popular_post_title'] || !empty($options['popular_post_sub_title'])) ):
                            echo ($options['popular_post_sub_title']) ? sprintf( '<p class="section-subtitle">%s</p>',  esc_html( $options['popular_post_sub_title'] ) ) : ''; 
                            echo ($options['popular_post_title']) ? sprintf( '<h2 class="section-title">%s</h2>',  esc_html( $options['popular_post_title'] ) ) : ''; ?>
                        <?php endif; ?>
                    </div><!-- .section-header -->

                    <div class="section-content col-3 clear">
                    <?php foreach ( $content_details 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" 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="admin-profile">
                                        <div class="admin-image">
                                            <a href="#"><img src="<?php echo get_avatar_url($content['author_id'], ['size' => '70']); ?>" alt="admin"></a>
                                        </div><!-- .admin-image -->

                                        <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><!-- .section-content -->
                </div><!-- .wrapper -->
            </div><!-- #popular-posts -->

            </div>
    <?php 
    }    
endif;
