<?php
/**
 * Article Banner Slider Widget Style
 *
 * @package NewsNation
 */
if (!function_exists('newsnation_post_banner_slider_widget')):
    /**
     * Load widgets.
     *
     * @since 1.0.0
     */
    function newsnation_post_banner_slider_widget()
    {
        register_widget('NewsNation_Post_Banner_Slider_Widget');
    }
endif;
add_action('widgets_init', 'newsnation_post_banner_slider_widget');
// Article Widget Carousel Layout
if (!class_exists('NewsNation_Post_Banner_Slider_Widget')):
    /**
     * Article Widget Carousel Layout
     *
     * @since 1.0.0
     */
    class NewsNation_Post_Banner_Slider_Widget extends NewsNation_Widget_Base
    {
        /**
         * Sets up a new widget instance.
         *
         * @since 1.0.0
         */
        function __construct()
        {
            $opts = array(
                'classname' => 'newsnation_post_banner_slider_widget',
                'description' => esc_html__('Displays post form selected category in banner slider styles', 'newsnation'),
                'customize_selective_refresh' => true,
            );
            $fields = array(
                'title' => array(
                    'label' => esc_html__('Section Title:', 'newsnation'),
                    'type' => 'text',
                    'class' => 'widefat',
                ),
                'post_category' => array(
                    'label' => esc_html__('Select Category:', 'newsnation'),
                    'type' => 'dropdown-taxonomies',
                    'show_option_all' => esc_html__('All Categories', 'newsnation'),
                ),
                'display_orientation' => array(
                    'label' => esc_html__('Display Layout:', 'newsnation'),
                    'type' => 'select',
                    'default' => 'layout-1',
                    'options' => array(
                        'news-article-default' => esc_html__('Layout - 1', 'newsnation'),
                        'news-article-tiles entry-image-overlay image-overlay-gradient' => esc_html__('Layout - 2', 'newsnation'),
                    ),
                ),
                'content_alignment' => array(
                    'label' => esc_html__('Text Alignment:', 'newsnation'),
                    'type' => 'select',
                    'default' => 'text-center',
                    'options' => array(
                        'content-text-left' => esc_html__('Align Left', 'newsnation'),
                        'content-text-center' => esc_html__('Align Center', 'newsnation'),
                        'content-text-right' => esc_html__('Align Right', 'newsnation'),
                    ),
                ),
                'image_size' => array(
                    'label' => esc_html__('Image Size:', 'newsnation'),
                    'type' => 'select',
                    'default' => 'entry-image-xlarge',
                    'options' => array(
                        'entry-image-xlarge' => esc_html__('Extra Large', 'newsnation'),
                        'entry-image-large' => esc_html__('Large', 'newsnation'),
                        'entry-image-big' => esc_html__('Big', 'newsnation'),
                    ),
                ),
                'post_number' => array(
                    'label' => esc_html__('Number of posts to show:', 'newsnation'),
                    'type' => 'number',
                    'default' => 5,
                    'css' => 'max-width:60px;',
                    'min' => 1,
                    'max' => 10,
                ),
                'post_offset' => array(
                    'label' => esc_html__('Posts offset:', 'newsnation'),
                    'type' => 'number',
                    'default' => 0,
                    'css' => 'max-width:60px;',
                    'min' => 0,
                    'max' => 12,
                ),
                'enable_meta' => array(
                    'label' => esc_html__('Enable Categories:', 'newsnation'),
                    'type' => 'checkbox',
                    'default' => true,
                ),
                'enable_meta_1' => array(
                    'label' => esc_html__('Enable Date & Author:', 'newsnation'),
                    'type' => 'checkbox',
                    'default' => true,
                ),
                'button_text' => array(
                    'label' => esc_html__('Button Text:', 'newsnation'),
                    'type' => 'text',
                    'default' => 'View more',
                    'class' => 'widefat',
                ),
            );
            parent::__construct('newsnation-widget-banner-slider-style', esc_html__('NewsNation: Slider Widget', 'newsnation'), $opts, array(), $fields);
        }

        /**
         * Outputs the content for the current widget instance.
         *
         * @param array $args Display arguments.
         * @param array $instance Settings for the current widget instance.
         * @since 1.0.0
         *
         */
        function widget($args, $instance)
        {
            $params = $this->get_params($instance);

            $transient_name = 'newsnation_banner_slider_' . md5(serialize($params) . serialize($args));
            $cached_html = get_transient($transient_name);

            if (false !== $cached_html) {
                echo $cached_html;
                return;
            }

            ob_start();

            echo $args['before_widget'];
            $post_number = $params['post_number'];
            $post_offset = $params['post_offset'];
            $qargs = array(
                'post_type' => 'post',
                'posts_per_page' => $post_number,
                'offset' => $post_offset,
                'post__not_in' => get_option("sticky_posts"),
            );
            $cat_link = "";

            if (is_rtl()) {
                $rtl = 'true';
            } else {
                $rtl = 'false';
            }

            if (absint($params['post_category']) > 0) {
                $qargs['cat'] = absint($params['post_category']);
                $cat_link = get_category_link($params['post_category']);
            }
            $slider_posts_query = new WP_Query($qargs);
            if ($slider_posts_query->have_posts()): ?>
                <div class="widget-layout widget-layout-slider">
                    <header class="theme-widget-header">
                        <?php if (!empty($params['title'])) { ?>
                            <h2 class="widget-title">
                                <?php echo esc_html($params['title']) ?>
                            </h2>
                        <?php } ?>
                        <span class="block-title-dash"></span>
                    </header>
                    <div class="theme-widget-content">
                        <div class="widget-slider-panel">
                            <div class="theme-widget-slider" data-slick='{"rtl": <?php echo esc_attr($rtl); ?>}'>
                                <?php
                                while ($slider_posts_query->have_posts()):
                                    $slider_posts_query->the_post();
                                    ?>
                                    <div class="slick-slide-widget">
                                        <article id="theme-post-<?php the_ID(); ?>" <?php
                                          post_class(
                                              'news-article ' .
                                              esc_attr($params['display_orientation']) . ' ' .
                                              esc_attr($params['content_alignment'])
                                          ); ?>>


                                            <?php
                                            $fallback_image_url = newsnation_get_fallback_image_url();
                                            if (has_post_thumbnail() || $fallback_image_url): ?>
                                                <div class="entry-image <?php echo esc_attr($params['image_size']); ?>">
                                                    <a class="post-thumbnail" href="<?php the_permalink(); ?>" aria-hidden="true" tabindex="-1">
                                                        <?php
                                                        if (has_post_thumbnail()) {
                                                            the_post_thumbnail('large', [
                                                                'alt' => the_title_attribute(['echo' => false]),
                                                            ]);
                                                        } else {
                                                            echo '<img src="' . esc_url($fallback_image_url) . '" alt="' . esc_attr(get_the_title()) . '" />';
                                                        }
                                                        ?>
                                                    </a>
                                                </div>
                                            <?php endif; ?>

                                            <div class="article-content">
                                                <div class="entry-meta">
                                                    <?php newsnation_entry_footer($cats = true, $tags = false, $edits = false, $text = false, $icon = false); ?>
                                                </div>
                                                <h3 class="entry-title entry-title-large">
                                                    <a href="<?php the_permalink(); ?>" tabindex="0" rel="bookmark"
                                                        title="<?php the_title_attribute(); ?>">
                                                        <?php the_title(); ?>
                                                    </a>
                                                </h3>
                                                <div class="entry-meta">
                                                    <?php newsnation_posted_by(); ?>
                                                </div>

                                                <div class="entry-content hidden-xs-element entry-content-muted">
                                                    <?php
                                                    if (has_excerpt()) {
                                                        the_excerpt();
                                                    } else {
                                                        echo '<p>';
                                                        echo esc_html(wp_trim_words(get_the_content(), 25, '...'));
                                                        echo '</p>';
                                                    } ?>
                                                </div>
                                            </div>

                                        </article>
                                    </div>
                                    <?php
                                    wp_reset_postdata();
                                endwhile; ?>
                            </div>
                        </div>
                    </div>
                    <?php if (!empty($params['button_text']) && !empty($cat_link)) { ?>
                        <div class="theme-footer-link theme-widget-footer">
                            <div class="theme-footer-panel widget-footer-panel">
                                <hr>
                                <a class="theme-btn theme-primary-btn title-view-more theme-viewmore-link"
                                    href="<?php echo esc_url($cat_link); ?>"><?php echo esc_html($params['button_text']); ?><?php newsnation_theme_svg('arrow-right'); ?></a>
                            </div>
                        </div>
                    <?php } ?>
                </div>
            <?php endif;
            echo $args['after_widget'];

            $output = ob_get_clean();
            set_transient($transient_name, $output, 2 * HOUR_IN_SECONDS);
            echo $output;
        }
    }
endif;