<?php
/**
 * Active filters widget class.
 *
 * @package ecommerce-product-catalog/includes/widgets
 */

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

/**
 * Active filters widget.
 */
class IC_Active_Filters_Widget extends WP_Widget {

	/**
	 * Sets up the widget.
	 *
	 * @return void
	 */
	public function __construct() {
		$label      = __( 'Catalog Active Filters', 'ecommerce-product-catalog' );
		$sublabel   = __( 'Displays active filters with an option to remove.', 'ecommerce-product-catalog' );
		$widget_ops = array(
			'classname'             => 'ic_active_filters_widget',
			'description'           => $sublabel,
			'show_instance_in_rest' => true,
		);
		parent::__construct( 'ic_active_filters_widget', $label, $widget_ops );
	}

	/**
	 * Renders the widget.
	 *
	 * @param array $args     Widget wrapper args.
	 * @param array $instance Widget instance.
	 *
	 * @return void
	 */
	public function widget( $args, $instance ) {
		if ( 'simple' !== get_integration_type() ) {
			if ( ic_if_show_filter_widget( $instance ) ) {
				$title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );
				// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Widget wrapper HTML is provided by WordPress/theme.
				echo $args['before_widget'];
				if ( $title ) {
					// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Widget wrapper HTML is provided by WordPress/theme.
					echo $args['before_title'] . $title . $args['after_title'];
				}
				// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- HTML is generated by the active filters helper.
				echo ic_get_active_filters_html();
				// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Widget wrapper HTML is provided by WordPress/theme.
				echo $args['after_widget'];
			}
		}
	}

	/**
	 * Outputs the widget admin form.
	 *
	 * @param array $instance Widget instance.
	 *
	 * @return void
	 */
	public function form( $instance ) {
		if ( 'simple' !== get_integration_type() ) {
			$instance = wp_parse_args(
				(array) $instance,
				array(
					'title'             => '',
					'shortcode_support' => 0,
				)
			);
			$title    = $instance['title'];
			?>
			<p>
				<label
						for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_html_e( 'Title:', 'ecommerce-product-catalog' ); ?>
					<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"
							name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text"
							value="<?php echo esc_attr( $title ); ?>"/></label></p>
			<?php
		} else {
			// Active filters widget is disabled in simple mode due to the missing main listing page.
			IC_Catalog_Notices::simple_mode_notice();
		}
	}

	/**
	 * Sanitizes widget options on save.
	 *
	 * @param array $new_instance New widget values.
	 * @param array $old_instance Previous widget values.
	 *
	 * @return array
	 */
	public function update( $new_instance, $old_instance ) {
		$instance          = $old_instance;
		$new_instance      = wp_parse_args(
			(array) $new_instance,
			array(
				'title' => '',
			)
		);
		$instance['title'] = wp_strip_all_tags( $new_instance['title'] );

		return $instance;
	}
}

