<?php
/**
 * Random Posts with Thumbnail widget.
 *
 * @package    ShoBe
 * @author     Boby Hermez
 * @copyright  Copyright (c) 2025, Boby Hermez
 * @license    http://www.gnu.org/licenses/gpl-2.0.html
 * @since      1.0.0
 */

class ShoBe_Random_Widget extends WP_Widget {

	/**
	 * Sets up the widgets.
	 *
	 * @since 1.0.0
	 */
	function __construct() {

		// Set up the widget options.
		$widget_options = array(
			'classname'   => 'widget-shobe-random widget_posts_thumbnail',
			'description' => __( 'Display random posts with thumbnails.', 'shobe' )
		);

		// Create the widget.
		parent::__construct(
			'shobe-random',                                   // $this->id_base
			__( '&raquo; Random Posts Thumbnails', 'shobe' ), // $this->name
			$widget_options                                      // $this->widget_options
		);

	}

	/**
	 * Outputs the widget based on the arguments input through the widget controls.
	 *
	 * @since 1.0.0
	 */
	function widget( $args, $instance ) {
		extract( $args );

		// Output the theme's $before_widget wrapper.
		echo $before_widget;

		// If the title not empty, display it.
		if ( $instance['title'] ) {
			echo $before_title . apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base ) . $after_title;
		}
		$sticky = get_option( 'sticky_posts' );
		// Posts query arguments.
		$args = array(
			'post_type'      => 'post',
			'posts_per_page' => $instance['limit'],
			'ignore_sticky_posts' 	=> true,
			'post__not_in' => $sticky,
			'orderby'        => 'rand'
		);

		// The post query
		$random = new WP_Query( $args );

		global $post;
		if ( $random->have_posts() ) {
			echo '<ul class="list-unstyled ms-3 my-3">';

			while ( $random->have_posts() ) :
				$random->the_post();
				?>
			   
				<li class="d-flex mb-3 pb-3 border-bottom">
				<?php
					if ( has_post_thumbnail() )
					?>
					<a href="<?php echo esc_url( get_permalink() ) ?>" rel="bookmark" class=" w-25 me-3">
					<?php echo get_the_post_thumbnail( get_the_ID(), array(380,230), array( 'alt' => esc_attr( get_the_title() ), 'class'=> 'img-fluid rounded') )?>
					</a>
					
					<div class="flex-1"> <a href="<?php echo esc_url( get_permalink() ) ?>" class="text-dark">
						<?php echo esc_attr( get_the_title() ) ;
					?>
					</a>
					<?php
					if ( $instance['show_date'] ) :
					?>
					<span class="d-block text-muted"><time class="entry-date" datetime="<?php echo esc_html( get_the_date( 'c' ) ) ?>"><?php echo esc_html( get_the_date() )?></time></span>
					<?php
					endif;  
					?>
					</div>
				   
				</li>
			<?php
			endwhile;

			echo '</ul>';
		}

		// Reset the query.
		wp_reset_postdata();

		// Close the theme's widget wrapper.
		echo $after_widget;

	}

	/**
	 * Updates the widget control options for the particular instance of the widget.
	 *
	 * @since 1.0.0
	 */
	function update( $new_instance, $old_instance ) {

		$instance = $new_instance;
		$instance['title'] = ( ! empty( $instance['title'] ) ) ? sanitize_text_field( strip_tags( $instance['title'] ) ): '';
		$instance['limit']     = (int) $instance['limit'];
		$instance['show_date'] = isset( $instance['show_date'] ) ? (bool) $instance['show_date'] : false;

		return $instance;
	}

	/**
	 * Displays the widget control options in the Widgets admin screen.
	 *
	 * @since 1.0.0
	 */
	function form( $instance ) {

		// Default value.
		$defaults = array(
			'title'     => esc_html__( 'Random Posts', 'shobe' ),
			'limit'     => 5,
			'show_date' => false
		);

		$instance = wp_parse_args( (array) $instance, $defaults );
	?>

		<p>
			<label for="<?php echo $this->get_field_id( 'title' ); ?>">
				<?php _e( 'Title', 'shobe' ); ?>
			</label>
			<input type="text" class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo esc_attr( $instance['title'] ); ?>" />
		</p>

		<p>
			<label for="<?php echo $this->get_field_id( 'limit' ); ?>">
				<?php _e( 'Number of posts to show', 'shobe' ); ?>
			</label>
			<input class="small-text" id="<?php echo $this->get_field_id( 'limit' ); ?>" name="<?php echo $this->get_field_name( 'limit' ); ?>" type="number" step="1" min="0" value="<?php echo (int)( $instance['limit'] ); ?>" />
		</p>

		<p>
			<input class="checkbox" type="checkbox" <?php checked( $instance['show_date'] ); ?> id="<?php echo $this->get_field_id( 'show_date' ); ?>" name="<?php echo $this->get_field_name( 'show_date' ); ?>" />
			<label for="<?php echo $this->get_field_id( 'show_date' ); ?>">
				<?php _e( 'Display post date?', 'shobe' ); ?>
			</label>
		</p>

	<?php

	}

}
function register_shobe_Random_Widget() {
	register_widget( 'ShoBe_Random_Widget' );
}
add_action( 'widgets_init', 'register_shobe_Random_Widget' );

