<?php
/**
 * Related Posts Functions.
 *
 * @package NewsNation
 */
if (!function_exists('newsnation_related_posts')) :
    // Single Posts Related Posts.
    function newsnation_related_posts()
    {
        $newsnation_default = newsnation_get_default_theme_options();
        $newsnation_header_trending_page = get_theme_mod('newsnation_header_trending_page');
        $newsnation_header_popular_page = get_theme_mod('newsnation_header_popular_page');
        $current_id = '';
        global $post;
        $current_id = $post->ID;
        if ($newsnation_header_trending_page != $current_id && $newsnation_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', $newsnation_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', $newsnation_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 newsnation_posted_by(); ?>
                                    </div>
                                </div>
                            </article>
                        <?php endwhile; ?>
                    </div>
                </div>
                <?php
                wp_reset_postdata();
            endif;
        }
    }
endif;
add_action('newsnation_navigation_action', 'newsnation_related_posts', 20);
