<?php
/**
 * Related Posts Functions.
 *
 * @package Vintuna
 */
if (!function_exists('vintuna_related_posts')) :
    // Single Posts Related Posts.
    function vintuna_related_posts()
    {
        $vintuna_default = vintuna_get_default_theme_options();
        $vintuna_header_trending_page = get_theme_mod('vintuna_header_trending_page');
        $vintuna_header_popular_page = get_theme_mod('vintuna_header_popular_page');
        $current_id = '';
        global $post;
        $current_id = $post->ID;
        if ($vintuna_header_trending_page != $current_id && $vintuna_header_popular_page != $current_id && is_single() && 'post' === get_post_type()) {
            $cats = get_the_category($post->ID);
            $category = array();
            if ($cats) {
                foreach ($cats as $cat) {
                    $category[] = $cat->term_id;
                }
            }
            $related_posts_query = new WP_Query(array('post_type' => 'post', 'posts_per_page' => 6, 'post__not_in' => array($post->ID), 'category__in' => $category));
            $ed_related_post = absint(get_theme_mod('ed_related_post', $vintuna_default['ed_related_post']));
            if ($ed_related_post && $related_posts_query->have_posts()) : ?>
                <div class="theme-block related-posts-area">
                    <?php $related_post_title = esc_html(get_theme_mod('related_post_title', $vintuna_default['related_post_title']));
                    if ($related_post_title) { ?>
                        <header class="theme-block-header">
                            <h2 class="theme-block-title">
                                <?php echo esc_html($related_post_title); ?>
                            </h2>
                        </header>
                    <?php } ?>
                    <div class="related-posts-wrapper">
                        <?php while ($related_posts_query->have_posts()) :
                            $related_posts_query->the_post();
                            ?>
                            <article id="single-related-<?php the_ID(); ?>" <?php post_class('news-article news-article-default mb-24'); ?>>
                                <?php if (has_post_thumbnail()) : ?>
                                    <div class="entry-image entry-image-small image-hover-effect hover-effect-shine">
                                        <a class="post-thumbnail" href="<?php the_permalink(); ?>" aria-hidden="true"
                                           tabindex="-1">
                                            <?php the_post_thumbnail('medium_large', ['alt' => the_title_attribute(['echo' => false])]); ?>
                                        </a>
                                    </div>
                                <?php endif; ?>
                                <div class="article-content">
                                    <header class="entry-header">
                                        <h3 class="entry-title entry-title-small line-clamp-2">
                                            <a href="<?php the_permalink(); ?>" rel="bookmark">
                                                <?php the_title(); ?>
                                            </a>
                                        </h3>
                                    </header>
                                    <div class="entry-meta">
                                        <?php vintuna_posted_by(); ?>
                                    </div>
                                </div>
                            </article>
                        <?php endwhile; ?>
                    </div>
                </div>
                <?php
                wp_reset_postdata();
            endif;
        }
    }
endif;
add_action('vintuna_navigation_action', 'vintuna_related_posts', 20);
