<?php
//phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped
if ( ! defined( 'ABSPATH' ) ) exit;


add_action('widgets_init', 'ccpw_register_widget');
// Register the widget.
function ccpw_register_widget()
{
    register_widget('ccpw_Widget');
}

class ccpw_Widget extends WP_Widget
{
    // Set up the widget name and description.
    public function __construct()
    {
        $widget_options = array(
            'classname' => 'ccpw_widget',
            'description' => 'Cryptocurrency Widgets by Cool Plugins',
        );
        parent::__construct('ccpw_widget', 'Crypto Widget', $widget_options);
    }

    // Create the admin area widget settings form.
    public function form($instance)
    {
        $ccpw_shortcode_id = !empty($instance['ccpw_shortcode']) ? $instance['ccpw_shortcode'] : '';
        $title = !empty($instance['title']) ? $instance['title'] : '';
        ?>
  <p>
	<span class="imp-note" style="color:red">Important Note : Use widget shortcode according to available width</br></span>
	<label for="<?php echo esc_attr($this->get_field_id('title')); ?>">Title:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</label>
	<input type="text" id="<?php echo esc_attr($this->get_field_id('title')); ?>" name="<?php echo esc_attr($this->get_field_name('title')); ?>" value="<?php echo esc_attr($title); ?>" />
  </p>

	  <p>
		<label for="<?php echo esc_attr($this->get_field_id('ccpw_shortcode')); ?>"> Shortcode:</label>
		 <select style="width:70%" id="<?php echo esc_attr($this->get_field_id('ccpw_shortcode')); ?>" name="<?php echo esc_attr($this->get_field_name('ccpw_shortcode')); ?>" >

		<?php
global $post;
        $args = array(
            'numberposts' => -1,
            'post_type' => 'ccpw',
        );
        $postlist = get_posts($args);
        if ($postlist) {
            foreach ($postlist as $post):
                setup_postdata($post);
                $p_id = get_the_id();

                if ($ccpw_shortcode_id == $p_id) {
                    echo '<option selected="selected" value="' . esc_attr($p_id) . '">[ccpw id="' . esc_html($p_id) . '"]' . '</option>';
                } else {
                    echo '<option value="' . esc_attr($p_id) . '">[ccpw id="' . esc_html($p_id) . '"]' . '</option>';
                }

            endforeach;
        } else {
            echo '<option value="">Shortcode Not Found</option>';
        }
        ?>

		</select>
	  </p>
		<?php
}

    // Apply settings to the widget instance.
    public function update($new_instance, $old_instance)
    {
        $instance = $old_instance;
        $instance['ccpw_shortcode'] = !empty($new_instance['ccpw_shortcode']) ? wp_strip_all_tags($new_instance['ccpw_shortcode']) : ''; // Ensure value is not empty
        $instance['title'] = !empty($new_instance['title']) ? wp_strip_all_tags($new_instance['title']) : ''; // Ensure value is not empty
        return $instance;
    }

    // Create the widget output.
    public function widget($args, $instance)
    {
        $ccpw_shortcode = !empty($instance['ccpw_shortcode']) ? $instance['ccpw_shortcode'] : ''; // Ensure value is not empty
        $title = apply_filters('widget_title', $instance['title']);

        echo wp_kses_post($args['before_widget'] . $args['before_title'] . $title . $args['after_title']);

        if (!empty($ccpw_shortcode)) {
            echo do_shortcode('[ccpw id="' . esc_attr($ccpw_shortcode) . '"]'); // Use esc_attr for security
        }

        echo wp_kses_post($args['after_widget']);
    }

}
