<?php
/**
 * Partial template to display the single post's related posts section.
 *
 * @package Blogbuster
 *
 */

// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

$blogbuster_single_posts_related_enable = blogbuster_get_customizer_option_value( 'blogbuster_single_posts_related_enable' );

if ( false === $blogbuster_single_posts_related_enable ) {
    return;
}

/**
 * hook - blogbuster_before_single_post_related_posts_section
 *
 * @since 1.0.0
 */
do_action( 'blogbuster_before_single_post_related_posts_section' );

$blogbuster_single_posts_related_label = blogbuster_get_customizer_option_value( 'blogbuster_single_posts_related_label' );
$section_column = apply_filters( 'single_posts_related_column', 3 );

$related_posts_args = array(
    'posts_per_page'        => absint( $section_column ),
    'post__not_in'          => array( get_the_ID() ),
    'ignore_sticky_posts'   => true
);
$current_post_cats = get_the_category( get_the_ID() );

if ( $current_post_cats && is_array( $current_post_cats ) ) {
    foreach ( $current_post_cats as $post_cat ) {
        $post_cats[] = $post_cat->term_id;
    }
    $related_posts_args['category__in'] = $post_cats;
}

$related_posts_query = new WP_Query( $related_posts_args );

if ( $related_posts_query->have_posts() ) :
?>
    <section class="single-related-posts section-column-<?php echo absint( $section_column ); ?>">
        
        <h2 class="related-post-title"><?php echo esc_html( $blogbuster_single_posts_related_label ); ?></h2>

        <div class="blogbuster-related-posts-wrapper">
            <?php
                while ( $related_posts_query->have_posts() ) :
                    $related_posts_query->the_post();

                    $post_class = [];

                    $post_categories = get_the_category();
                    $post_cat = $post_categories[0]->term_id;

                    if ( ! empty( $post_cat ) ) {
                        $post_class[] = 'post-cat-'.esc_attr( $post_cat );
                    }

                    if ( has_post_thumbnail() ) {
                        $post_class[] = 'related-post-wrap has-thumbnail';
                    } else {
                        $post_class[] = 'related-post-wrap no-thumbnail';
                    }
            ?>
                    <article id="post-<?php the_ID(); ?>" <?php post_class( implode( ' ', $post_class ) ); ?>>
                        <div class="post-thumbnail-wrap">
                            <?php blogbuster_post_thumbnail( 'medium' ); ?>
                        </div><!-- .post-thumbnail-wrap -->
                        <div class="related-post-content-wrap">
                            <div class="post-cats-wrap">
                                <?php blogbuster_the_post_categories_list( get_the_ID(), 1 ); ?>
                            </div><!-- .post-cats-wrap -->
                            <header class="entry-header">
                                <?php the_title( '<h3 class="entry-title"><a href="' . esc_url( get_permalink() ) . '" rel="bookmark">', '</a></h3>' ); ?>
                            </header><!-- .entry-header -->
                        </div><!-- .related-post-content-wrap -->
                    </article>
            <?php
                endwhile;
            ?>
        </div><!-- .blogbuster-related-posts-wrapper -->
    </section><!-- .single-related-posts -->
<?php

endif;

wp_reset_postdata();

/**
 * hook - blogbuster_after_single_post_related_posts_section
 *
 * @since 1.0.0
 */
do_action( 'blogbuster_after_single_post_related_posts_section' );