<?php
/**
 * Archive 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_archives_Widget extends WP_Widget {

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

		// Set up the widget options.
		$shobe_widget_options = array(
			'classname'   => 'widget-archive widget_custom_archive',
			'description' => __( 'Display Custom Archives.', 'shobe' )
		);

		// Create the widget.
		parent::__construct(
			'custom_archive',                                   // $this->id_base
			__( '&raquo; ShoBe Custom Archives', 'shobe' ), // $this->name
			$shobe_widget_options                                      // $this->shobe_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;        
        
        $instance =  wp_parse_args( $instance, array(
            'title'         => '',
        ) );
        echo $before_title;
		echo empty( $instance['title'] ) ? _e( 'Archives', 'shobe' ) : apply_filters( 'widget_title', $instance['title'] );
        echo $after_title;

        $_args = array(

            'show_post_count' => 0,
            'order'         => 'desc',
            'type'            => 'monthly',
            'format' => 'with_text_dark'
        );

        $_args =  wp_parse_args( $instance , $_args );
        ?>
        <ul class="list-unstyled ms-3 my-3">
             <?php wp_get_archives( $_args ); ?>
         </ul>
         <?php 
		// 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 = array();
     $new_instance['title'] = ( ! empty( $new_instance['title'] ) ) ? sanitize_text_field( strip_tags( $new_instance['title'] ) ): '';
     return $new_instance;
     // return $instance;
	}


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

		// Default value.
		$instance =  wp_parse_args( $instance, array(
            'title'         => '',
            'order'         => 'desc',
            'type'            => 'monthly',
            'show_post_count' => 0
        ) );

        $title = ! empty( $instance['title'] ) ? $instance['title'] : '';
	?>

		<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( 'type' ); ?>"><?php esc_html_e( 'Type:', 'shobe' ); ?></label>
            <select name="<?php echo $this->get_field_name( 'type' ); ?>">
                <?php
                $a = array(
                    'daily' => esc_html__( 'Daily', 'shobe' ),
                    'weekly'  => esc_html__( 'Weekly', 'shobe' ),
                    'monthly'  => esc_html__( 'Monthly', 'shobe' ),
                    'yearly'  => esc_html__( 'Yearly', 'shobe' ),
                ) ;
                foreach ( $a as $k => $v ) {
                    echo '<option value="'.$k.'" '.selected( $instance['type'], $k, false ).' >'.$v.'</option>';
                } ?>
            </select>
        </p>

        <p>
            <label for="<?php echo $this->get_field_id( 'order' ); ?>"><?php esc_html_e( 'Order:', 'shobe' ); ?></label>
            <select name="<?php echo $this->get_field_name( 'order' ); ?>">
                <?php
                $a = array(
                    'desc' => esc_html__( 'Desc', 'shobe' ),
                    'asc'  => esc_html__( 'Asc', 'shobe' ),
                );
                foreach (  $a as $k => $v ) {
                    echo '<option value="'.$k.'" '.selected( $instance['order'], $k, false ).' >'.$v.'</option>';
                } ?>
            </select>
        </p>
        <p>
            <label for="<?php echo $this->get_field_id( 'show_post_count' ); ?>"><?php esc_html_e( 'Show count ?', 'shobe' ); ?></label>
            <input class="widefat" <?php checked( $instance['show_post_count'], 1 ); ?> id="<?php echo $this->get_field_id( 'show_post_count' ); ?>" name="<?php echo $this->get_field_name( 'show_post_count' ); ?>" type="checkbox" value="1">
        </p>

	<?php

	}

}

function register_shobe_archive_Widget() {
	register_widget( 'shobe_archives_Widget' );
}
add_action( 'widgets_init', 'register_shobe_archive_Widget' );
