<?php
/**
 * CV: Latest Posts
 *
 * This widget displays the latest posts along with their thumbnails,
 * offering a visually engaging way to showcase recent content.
 *
 * @package Yaatra
 */

// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

class Yaatra_Latest_Posts extends WP_Widget {

    /**
     * Register widget with WordPress.
     */
    public function __construct() {
        $widget_ops = array( 
            'classname'         => 'yaatra_latest_posts',
            'description'       => __( 'A widget to display the latest posts with thumbnail.', 'yaatra' ),
            'customize_selective_refresh' => true,
        );
        parent::__construct( 'yaatra_latest_posts', __( 'CV: Latest Posts', 'yaatra' ), $widget_ops );
    }

    /**
     * Helper function that holds widget fields.
     * Array is used in update and form functions.
     *
     * @return array
     */
    private function widget_fields() {
        $fields = array(
            'widget_title' => array(
                'yaatra_widgets_name'       => 'widget_title',
                'yaatra_widgets_title'      => __( 'Widget title', 'yaatra' ),
                'yaatra_widgets_field_type' => 'text'
            ),
            'widget_post_count' => array(
                'yaatra_widgets_name'       => 'widget_post_count',
                'yaatra_widgets_title'      => __( 'Post Count', 'yaatra' ),
                'yaatra_widgets_default'    => '5',
                'yaatra_widgets_field_type' => 'number'
            ),
        );
        return $fields;
    }

    /**
     * Front-end display of widget.
     *
     * @see WP_Widget::widget()
     *
     * @param array $args Widget arguments.
     * @param array $instance Saved values from database.
     */
    public function widget( $args, $instance ) {
        if ( empty( $instance ) ) {
            return;
        }

        $widget_title   = ! empty( $instance['widget_title'] ) ? $instance['widget_title'] : '';
        $post_count     = ! empty( $instance['widget_post_count'] ) ? $instance['widget_post_count'] : '5';

        echo $args['before_widget'];
    ?>
        <div class="cv-latest-posts-wrapper">
            <?php
                if ( ! empty( $widget_title ) ) {
                    echo $args['before_title'] . esc_html( $widget_title ) . $args['after_title'];
                }
            ?>
            <div class="cv-posts-content-wrapper">
                <?php
                    $posts_args = array(
                        'posts_per_page' => absint( $post_count ),
                    );
                    $posts_query = new WP_Query( $posts_args );
                    if ( $posts_query->have_posts() ) {
                        while ( $posts_query->have_posts() ) {
                            $posts_query->the_post();
    ?>
                            <div class="cv-single-post-wrap">
                                <?php if ( has_post_thumbnail() ) { ?>
                                    <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
                                        <figure><div class="cv-post-thumb"><?php the_post_thumbnail( 'thumbnail' ); ?></div></figure>
                                    </a>
                                <?php } ?>
                                <div class="cv-post-content">
                                    <h5 class="cv-post-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h5>
                                    <div class="cv-post-meta"><?php yaatra_posted_on(); ?></div>
                                </div>
                            </div><!-- .cv-single-post-wrap -->
    <?php
                        }
                        wp_reset_postdata();
                    }
                ?>
            </div><!-- .cv-posts-content-wrapper -->
        </div><!-- .cv-latest-posts-wrapper -->
    <?php
        echo $args['after_widget'];
    }

    /**
     * Sanitize widget form values as they are saved.
     *
     * @see WP_Widget::update()
     *
     * @param array $new_instance Values just sent to be saved.
     * @param array $old_instance Previously saved values from database.
     *
     * @return array Updated safe values to be saved.
     */
    public function update( $new_instance, $old_instance ) {
        $instance = $old_instance;
        $widget_fields = $this->widget_fields();

        foreach ( $widget_fields as $widget_field ) {
            $field_name            = $widget_field['yaatra_widgets_name'];
            $instance[$field_name] = yaatra_widgets_updated_field_value( $widget_field, $new_instance[$field_name] );
        }

        return $instance;
    }

    /**
     * Back-end widget form.
     *
     * @see WP_Widget::form()
     *
     * @param array $instance Previously saved values from database.
     */
    public function form( $instance ) {
        $widget_fields = $this->widget_fields();

        foreach ( $widget_fields as $widget_field ) {
            $field_name  = $widget_field['yaatra_widgets_name'];
            $field_value = ! empty( $instance[$field_name] ) ? wp_kses_post( $instance[$field_name] ) : '';
            yaatra_widgets_show_widget_field( $this, $widget_field, $field_value );
        }
    }
}