<?php
/**
 * Partial template for displaying related posts on single post pages.
 *
 * Responsible for rendering posts related to the current single post.
 *
 * @link https://developer.wordpress.org/themes/basics/template-hierarchy/
 *
 * @package Yaatra
*/

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

$related_posts_option = yaatra_get_customizer_option_value( 'yaatra_enable_related_posts' );
if ( true !== $related_posts_option ) {
    return;
}

/**
 * hook - yaatra_before_related_posts
 * 
 * @since 1.1.0
 */
do_action( 'yaatra_before_related_posts' );

$get_post_id            = get_the_id();
$related_posts_count    = yaatra_get_customizer_option_value( 'yaatra_related_posts_count' );
$related_posts_title    = yaatra_get_customizer_option_value( 'related_posts_title' );

$related_posts_args = array(
    'posts_per_page' => absint( $related_posts_count ),
    'post__not_in'   => array( $get_post_id ),
);

$get_categories = get_the_terms( $get_post_id, 'category' );
    if ( $get_categories && is_array( $get_categories ) ) {
        $selected_cat = array();
        foreach( $get_categories as $category ) {
            $selected_cat[] = $category->term_id;
        }
        $related_posts_args['category__in'] = $selected_cat;
    }

$related_posts_orderby = yaatra_get_customizer_option_value( 'yaatra_related_posts_orderby' );
if ( 'rp_rand' === $related_posts_orderby ) {
    $related_posts_args['orderby'] = 'rand';
}

$related_posts_query = new WP_Query( $related_posts_args );

if ( $related_posts_query->have_posts() ) {
?>
    <section class="yaatra-single-related-posts">
        
        <h2 class="yaatra-related-post-title"><?php echo esc_html( $related_posts_title ); ?></h2>

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

                        if ( has_post_thumbnail() ) {
                            $post_class = 'has-thumbnail wow fadeInUp';
                        } else {
                            $post_class = 'no-thumbnail wow fadeInUp';
                        }
                ?>
                        <article id="post-<?php the_ID(); ?>" <?php post_class( $post_class ); ?> data-wow-delay="0.5s">
                            <?php yaatra_post_thumbnail(); ?>
                            <header class="entry-header">
                                <?php the_title( '<h2 class="entry-title"><a href="' . esc_url( get_permalink() ) . '" rel="bookmark">', '</a></h2>' ); ?>
                                <div class="entry-meta">
                                    <?php yaatra_posted_on(); ?>
                                </div><!-- .entry-meta -->
                            </header><!-- .entry-header -->
                        </article><!-- #post-<?php the_ID(); ?> -->
                <?php
                    }
                ?>
            </div><!-- .yaatra-related-posts-wrapper -->

    </section><!-- .yaatra-single-related-posts -->
    
<?php
}

/**
 * hook - yaatra_after_related_posts
 * 
 * @since 1.1.0
 */
do_action( 'yaatra_after_related_posts' );