<?php
/**
 * Must Read
 *
 * @package NewsNation
 */
if (!function_exists('newsnation_must_read')) :
    function newsnation_must_read($newsnation_home_section, $repeat_times)
    {
        $section_category_6 = esc_html(isset($newsnation_home_section->section_category_6) ? $newsnation_home_section->section_category_6 : '');
        $home_section_must_read = esc_html(isset($newsnation_home_section->home_section_must_read) ? $newsnation_home_section->home_section_must_read : '');
        $section_post_offset = isset($newsnation_home_section->section_post_offset) ? absint($newsnation_home_section->section_post_offset) : 0;
        $section_view_more_title = esc_html(isset($newsnation_home_section->section_view_more_title) ? $newsnation_home_section->section_view_more_title : '');
        $must_read_section = new WP_Query(array('post_type' => 'post', 'posts_per_page' => 5, 'post__not_in' => get_option("sticky_posts"), 'offset' => $section_post_offset, 'category_name' => esc_html($section_category_6)));
        $category_id = get_cat_ID($section_category_6);
        $category_link = get_category_link($category_id);
        if ($must_read_section->have_posts()) :
            ?>
            <div id="theme-block-<?php echo esc_attr($repeat_times); ?>" class="theme-block theme-must-read">
                <div class="wrapper">
                    <?php if ($home_section_must_read) { ?>
                        <header class="theme-block-header">
                            <h2 class="theme-block-title"><?php echo esc_html($home_section_must_read); ?></h2>
                            <span class="block-title-dash"></span>
                            <?php if (!empty($category_link) && !empty($section_view_more_title)) { ?>
                                <a href="<?php echo esc_url($category_link); ?>" class="theme-btn theme-primary-btn title-view-more"><?php echo esc_html($section_view_more_title); ?><?php newsnation_theme_svg('arrow-right'); ?></a>
                            <?php } ?>
                        </header>
                    <?php } ?>
                    <div class="theme-panel-body">
                        <?php
                        while ($must_read_section->have_posts()) {
                            $must_read_section->the_post();
                            $featured_image = wp_get_attachment_image_src(get_post_thumbnail_id(), 'medium_large');
                            $featured_image = isset($featured_image[0]) ? $featured_image[0] : '';
                            ?>
                            <article class="news-article">
                                <div class="theme-article-image">
                                    <div class="data-bg data-bg-medium"
                                         data-background='<?php echo esc_url($featured_image); ?>'></div>
                                </div>
                                <div class="article-content">
                                    <h3 class="entry-title entry-title-medium"><a href="<?php the_permalink(); ?>"> <?php the_title(); ?></a></h3>
                                    <div class="entry-meta"><?php newsnation_posted_by(); ?></div>
                                    <?php
                                    the_excerpt();
                                    ?>
                                </div>
                            </article>
                        <?php } ?>
                    </div>
                </div>
            </div>
            <?php wp_reset_postdata();
        endif;
    }
endif; ?>