<?php
/**
 * Tags 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_Tags_Widget extends WP_Widget {

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

		// Set up the widget options.
		$widget_options = array(
			'classname'   => 'widget-tags widget_custom_tags',
			'description' => __( 'Display Custom Tags.', 'shobe' )
		);

		// Create the widget.
		parent::__construct(
			'custom_tags',                                   // $this->id_base
			__( '&raquo; ShoBe Custom Tags', '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;        
        
        $instance =  wp_parse_args( $instance, array(
            'title'         => '',
            'orderby'       => 'count',
            'order'         => 'desc',
            'item_per_row'  => 2,
        ) );
		
        echo $before_title;
		echo empty( $instance['title'] ) ? _e( 'Tags', 'shobe' ) : apply_filters( 'widget_title', $instance['title'] );
        echo $after_title;

        $_args = array(
            'show_option_all'    => '',
            'orderby'            => 'name',
            'order'              => 'ASC',
            'hide_empty'         => 0,
            'use_desc_for_title' => 1,
            'hierarchical'       => true,
           
        );

        $_args =  wp_parse_args( $instance , $_args );
        $output = '';
        $tags = get_tags($_args);
        $output .= '<div class="tagcloud ms-3 my-3">';
        if($tags) {
            foreach ($tags as $tag):
                $output .= '<a class="rounded d-inline-block fs-12 mx-1 my-1 px-2 py-1 text-uppercase" href="'. get_term_link($tag).'">'. $tag->name .'</a>';
            endforeach;
        } else {
            _e('No tags created.', 'shobe');
        }
        $output .= '</div>';
        echo $output;
		// 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'         => '',
            
            'orderby'       => 'count',
            'order'         => 'desc',
            'item_per_row'  => 2,
        ) );

        $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( 'orderby' ); ?>"><?php esc_html_e( 'Order by:', 'shobe' ); ?></label>
            <select name="<?php echo $this->get_field_name( 'orderby' ); ?>">
                <?php
                $a = array(
                    'count' => esc_html__( 'Count', 'shobe' ),
                    'name'  => esc_html__( 'Name', 'shobe' ),
                ) ;
                foreach ( $a as $k => $v ) {
                    echo '<option value="'.$k.'" '.selected( $instance['orderby'], $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( 'item_per_row' ); ?>"><?php esc_html_e( 'Number item per row:', 'shobe' ); ?></label>
            <select name="<?php echo $this->get_field_name( 'item_per_row' ); ?>">
                <?php for (  $i = 1; $i <=16 ; $i++ ) {
                    echo '<option value="'.$i.'" '.selected( $instance['item_per_row'], $i, false ).' >'.$i.'</option>';
                } ?>
            </select>

        </p>

	<?php

	}

}

function register_shobe_tags_Widget() {
	register_widget( 'Shobe_Tags_Widget' );
}
add_action( 'widgets_init', 'register_shobe_tags_Widget' );
