<?php
/**
 * Related Posts Section
 *
 * This is the template for the content of related posts section
 *
 * @package Theme Palace
 * @subpackage Greatnews Pro
 * @since Greatnews Pro 1.0.0
 */
if ( ! function_exists( 'power_news_add_related_posts_section' ) ) :
    /**
    * Add related posts section
    *
    *@since Greatnews Pro 1.0.0
    */
    function power_news_add_related_posts_section() {
    	$options = power_news_get_theme_options();
        // Check if related posts is enabled on frontpage
        $related_posts_enable = apply_filters( 'power_news_section_status', true, 'related_posts_section_enable' );

        if ( true !== $related_posts_enable ) {
            return false;
        }
        // Get related posts section details
        $section_details = array();
        $section_details = apply_filters( 'power_news_filter_related_posts_section_details', $section_details );

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

        // Render related posts section now.
        power_news_render_related_posts_section( $section_details );
    }
endif;

if ( ! function_exists( 'power_news_get_related_posts_section_details' ) ) :
    /**
    * related posts section details.
    *
    * @since Greatnews Pro 1.0.0
    * @param array $input related posts section details.
    */
    function power_news_get_related_posts_section_details( $input ) {
        $options = power_news_get_theme_options();

        // Content type.
        $related_posts_content_type  = $options['related_posts_content_type'];
        $related_posts_count = ! empty( $options['related_posts_count'] ) ? $options['related_posts_count'] : 3;
        
        $content = array();
        switch ( $related_posts_content_type ) {

            case 'post':
                $post_ids = array();

                for ( $i = 1; $i <= $related_posts_count; $i++ ) {
                    if ( ! empty( $options['related_posts_content_post_' . $i] ) )
                        $post_ids[] = $options['related_posts_content_post_' . $i];
                }
                $args = array(
                    'post_type'         => 'post',
                    'post__in'          => ( array ) $post_ids,
                    'posts_per_page'    => absint( $related_posts_count ),
                    'orderby'           => 'post__in',
                    'ignore_sticky_posts'   => true,
                    );                    
            break;

            case 'category':
                $cat_id = ! empty( $options['related_posts_content_category'] ) ? $options['related_posts_content_category'] : '';
                $args = array(
                    'post_type'         => 'post',
                    'posts_per_page'    => absint( $related_posts_count ),
                    'cat'               => absint( $cat_id ),
                    'ignore_sticky_posts'   => true,
                    );                    
            break;

            default:
            break;
        }


            // 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['content']   = get_the_content();
                    $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-590x650.jpg';

                    // Push to the main array.
                    array_push( $content, $page_post );
                endwhile;
            endif;
            wp_reset_postdata();
            
        if ( ! empty( $content ) ) {
            $input = $content;
        }
        return $input;
    }
endif;
// related posts section content details.
add_filter( 'power_news_filter_related_posts_section_details', 'power_news_get_related_posts_section_details' );


if ( ! function_exists( 'power_news_render_related_posts_section' ) ) :
  /**
   * Start related posts section
   *
   * @return string related posts content
   * @since Greatnews Pro 1.0.0
   *
   */
   function power_news_render_related_posts_section( $content_details = array() ) {
        $options = power_news_get_theme_options();
        $title = ! empty( $options['related_posts_title'] ) ? $options['related_posts_title'] : '';

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

        <div id="power_news_related_posts_section" class="related-posts-section grid-layout">
            <div class="wrapper">
                <?php if ( is_customize_preview()):
                    power_news_section_tooltip( 'related-posts-section-class' );
                endif; ?>
                <div class="section-header">
                    <?php if( !empty( $title ) ): ?>
                        <h2 class="section-title"><?php echo esc_html( $title ); ?></h2>
                    <?php endif; ?>
                </div><!-- .section-header -->

                <div class="section-content col-4 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'] ); ?>');">
                                <a href="<?php echo esc_url( $content['url'] ); ?>" class="post-thumbnail-link"></a>
                            </div><!-- .featured-image -->
                            <div class="entry-container">
                                <div class="entry-meta">
                                    <?php power_news_posted_on( $content['id'] ); ?>
                                    <?php echo power_news_author( $content['auth_id'] ); ?>
                                </div><!-- .entry-meta -->

                                <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><!-- .entry-container -->
                        </article>
                    <?php endforeach; ?>

                </div><!-- .breaking-news-slider -->
            </div><!-- .breaking-news-wrapper -->
        </div><!-- #breaking-news -->
        
    <?php }
endif;