<?php 

class magazinestyle_Latest_Posts_Widget extends WP_Widget {

    public function __construct() {
        parent::__construct(
            'magazinestyle_Latest_Posts_Widget',
            'Magazine Style Latest Posts',
            array('description' => 'A widget to display Latest posts with options.')
        );
    }

    public function widget($args, $instance) {		
        $title = isset($instance['title']) ? $instance['title'] : 'Latest Posts';
        $category = isset($instance['category']) ? $instance['category'] : '';
        $show_featured_image = isset($instance['show_featured_image']) ? $instance['show_featured_image'] : true;
        $num_posts = isset($instance['num_posts']) ? $instance['num_posts'] : 5;

        $args_query = array(
		'ignore_sticky_posts' => true,
            'posts_per_page' => $num_posts,
            'orderby' => 'date', // Always sort by views for Latest posts           
        );

        if (!empty($category) && $category !== 'all') {
            $args_query['category_name'] = $category;
        }

        $latest_posts_query = new WP_Query($args_query);

        echo '<div class="magazinestyle-latest">';
        echo '<h2 class="wp-block-heading">'. apply_filters('widget_title', $title) .'</h2>';

        if ($latest_posts_query->have_posts()) {
            echo '<ul>';
            while ($latest_posts_query->have_posts()) {
                $latest_posts_query->the_post();
                ?>
                <li>
                    <a href="<?php the_permalink(); ?>">
                    <?php if ($show_featured_image && has_post_thumbnail()) : ?>
                        <div class="post-thumbnail">
                            <?php the_post_thumbnail(array(70, 70)); ?>
                        </div>
                    <?php endif; ?><?php the_title(); ?></a>
                </li>
                <?php
            }
            echo '</ul>';
            wp_reset_postdata();
        }
        echo "</div>";
    }

    public function form($instance) {
        $title = isset($instance['title']) ? $instance['title'] : 'Latest Posts';
        $category = isset($instance['category']) ? $instance['category'] : '';
        $show_featured_image = isset($instance['show_featured_image']) ? $instance['show_featured_image'] : true;      
        $num_posts = isset($instance['num_posts']) ? $instance['num_posts'] : 5;
        ?>
        <p>
            <label for="<?php echo $this->get_field_id('title'); ?>">Widget Title:</label>
            <input type="text" id="<?php echo $this->get_field_id('title'); ?>"
                   name="<?php echo $this->get_field_name('title'); ?>" value="<?php echo esc_attr($title); ?>">
				  
        </p>
        <p>
            <label for="<?php echo $this->get_field_id('category'); ?>">Choose Category:</label>
            <?php
            $categories = get_categories(array('hide_empty' => 0));
            ?>
            <select id="<?php echo $this->get_field_id('category'); ?>"
                    name="<?php echo $this->get_field_name('category'); ?>">
                <option value="all"<?php selected($category, 'all'); ?>>All Categories</option>
                <?php foreach ($categories as $cat) : ?>
                    <option value="<?php echo esc_attr($cat->slug); ?>" <?php selected($category, $cat->slug); ?>><?php echo esc_html($cat->name); ?></option>
                <?php endforeach; ?>
            </select>
        </p>
        <p>
            <input type="checkbox" id="<?php echo $this->get_field_id('show_featured_image'); ?>"
                   name="<?php echo $this->get_field_name('show_featured_image'); ?>" <?php checked($show_featured_image, true); ?>>
            <label for="<?php echo $this->get_field_id('show_featured_image'); ?>">Show Featured Image</label>
        </p>
        <p>
            <label for="<?php echo $this->get_field_id('num_posts'); ?>">Number of Latest Posts to Display:</label>
            <input type="number" id="<?php echo $this->get_field_id('num_posts'); ?>"
                   name="<?php echo $this->get_field_name('num_posts'); ?>" value="<?php echo $num_posts; ?>">
        </p>
        <?php
    }

    public function update($new_instance, $old_instance) {
        $instance = array();
        $instance['title'] = !empty($new_instance['title']) ? sanitize_text_field($new_instance['title']) : 'Latest Posts';
        $instance['category'] = !empty($new_instance['category']) ? sanitize_text_field($new_instance['category']) : 'all';
        $instance['show_featured_image'] = isset($new_instance['show_featured_image']) ? true : false;
        $instance['num_posts'] = !empty($new_instance['num_posts']) ? intval($new_instance['num_posts']) : 5;
        return $instance;
    }
}

function register_magazinestyle_latest_posts_widget() {
    register_widget('magazinestyle_Latest_Posts_Widget');
}
add_action('widgets_init', 'register_magazinestyle_latest_posts_widget');
