<?php
/**
 * Widget Name: Product Filter
 * Description: A shop / archive product filter. Lets visitors narrow products by
 *              price, category, product attributes and star rating using standard
 *              WooCommerce query parameters (works with WC_Query on shop and
 *              product archive pages). Ships with multiple attractive styles.
 *
 * @package    captain-widgets-kit
 * @subpackage captain-widgets-kit/includes
 * @since      1.3.0
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Group_Control_Typography;

/**
 * Class Captwiki_Woo_Product_Filter
 *
 * @since 1.3.0
 */
class Captwiki_Woo_Product_Filter extends Widget_Base {

	/**
	 * Get widget name.
	 *
	 * @since 1.3.0
	 * @return string
	 */
	public function get_name(): string {
		return 'captwiki-woo-product-filter';
	}

	/**
	 * Get widget title.
	 *
	 * @since 1.3.0
	 * @return string
	 */
	public function get_title(): string {
		return esc_html__( 'Product Filter', 'captain-widgets-kit' );
	}

	/**
	 * Get widget icon.
	 *
	 * @since 1.3.0
	 * @return string
	 */
	public function get_icon(): string {
		return 'eicon-filter captwiki-el-logo';
	}

	/**
	 * Get widget categories.
	 *
	 * @since 1.3.0
	 * @return array
	 */
	public function get_categories(): array {
		return array( 'captwiki-woo-commerce' );
	}

	/**
	 * Get widget keywords.
	 *
	 * @since 1.3.0
	 * @return array
	 */
	public function get_keywords(): array {
		return array( 'woocommerce', 'woo', 'filter', 'shop', 'price', 'attribute', 'rating', 'layered nav', 'captain-widgets-kit' );
	}

	/**
	 * Disable Elementor's default inner wrapper.
	 *
	 * @since 1.3.0
	 * @return bool
	 */
	public function has_widget_inner_wrapper(): bool {
		return ! \Elementor\Plugin::$instance->experiments->is_feature_active( 'e_optimized_markup' );
	}

	/**
	 * Available visual styles.
	 *
	 * @since 1.3.0
	 * @return array
	 */
	private function captwiki_style_options(): array {
		return array(
			'style-1' => esc_html__( 'Style 1 – Clean', 'captain-widgets-kit' ),
			'style-2' => esc_html__( 'Style 2 – Boxed', 'captain-widgets-kit' ),
			'style-3' => esc_html__( 'Style 3 – Pills', 'captain-widgets-kit' ),
		);
	}

	/**
	 * Attribute taxonomy options for the selector.
	 *
	 * @since 1.3.0
	 * @return array taxonomy => label.
	 */
	private function captwiki_get_attribute_options(): array {

		$options = array();

		if ( ! function_exists( 'wc_get_attribute_taxonomies' ) ) {
			return $options;
		}

		foreach ( wc_get_attribute_taxonomies() as $attribute ) {
			$taxonomy             = wc_attribute_taxonomy_name( $attribute->attribute_name );
			$options[ $taxonomy ] = $attribute->attribute_label;
		}

		return $options;
	}

	/**
	 * Category options for the selector.
	 *
	 * @since 1.3.0
	 * @return array
	 */
	private function captwiki_get_category_options(): array {

		$options = array();

		if ( ! taxonomy_exists( 'product_cat' ) ) {
			return $options;
		}

		$terms = get_terms(
			array(
				'taxonomy'   => 'product_cat',
				'hide_empty' => false,
				'number'     => 100,
			)
		);

		if ( is_wp_error( $terms ) ) {
			return $options;
		}

		foreach ( $terms as $term ) {
			$options[ $term->term_id ] = $term->name;
		}

		return $options;
	}

	/**
	 * Register controls.
	 *
	 * @since 1.3.0
	 */
	protected function register_controls(): void {
		$this->register_content_controls();
		$this->register_style_controls();
		$this->register_button_style_controls();
	}

	/**
	 * Content tab controls.
	 *
	 * @since 1.3.0
	 */
	private function register_content_controls(): void {

		$this->start_controls_section(
			'section_content',
			array(
				'label' => esc_html__( 'Filter', 'captain-widgets-kit' ),
				'tab'   => Controls_Manager::TAB_CONTENT,
			)
		);

		$this->add_control(
			'widget_style',
			array(
				'label'   => esc_html__( 'Style', 'captain-widgets-kit' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'style-1',
				'options' => $this->captwiki_style_options(),
			)
		);

		$this->add_control(
			'target',
			array(
				'label'       => esc_html__( 'Results Page', 'captain-widgets-kit' ),
				'type'        => Controls_Manager::SELECT,
				'default'     => 'shop',
				'options'     => array(
					'shop'    => esc_html__( 'Shop Page', 'captain-widgets-kit' ),
					'current' => esc_html__( 'Current Page', 'captain-widgets-kit' ),
				),
				'description' => esc_html__( 'Where the filter sends visitors. Use "Current Page" when the widget is placed on the shop or a category archive.', 'captain-widgets-kit' ),
			)
		);

		$this->add_control(
			'show_price',
			array(
				'label'        => esc_html__( 'Price Filter', 'captain-widgets-kit' ),
				'type'         => Controls_Manager::SWITCHER,
				'return_value' => 'yes',
				'default'      => 'yes',
				'separator'    => 'before',
			)
		);

		$this->add_control(
			'price_heading',
			array(
				'label'       => esc_html__( 'Price Title', 'captain-widgets-kit' ),
				'type'        => Controls_Manager::TEXT,
				'default'     => esc_html__( 'Filter by Price', 'captain-widgets-kit' ),
				'label_block' => true,
				'condition'   => array( 'show_price' => 'yes' ),
			)
		);

		$this->add_control(
			'show_categories',
			array(
				'label'        => esc_html__( 'Category Filter', 'captain-widgets-kit' ),
				'type'         => Controls_Manager::SWITCHER,
				'return_value' => 'yes',
				'default'      => 'yes',
				'separator'    => 'before',
			)
		);

		$this->add_control(
			'categories_heading',
			array(
				'label'       => esc_html__( 'Category Title', 'captain-widgets-kit' ),
				'type'        => Controls_Manager::TEXT,
				'default'     => esc_html__( 'Categories', 'captain-widgets-kit' ),
				'label_block' => true,
				'condition'   => array( 'show_categories' => 'yes' ),
			)
		);

		$this->add_control(
			'show_attributes',
			array(
				'label'        => esc_html__( 'Attribute Filters', 'captain-widgets-kit' ),
				'type'         => Controls_Manager::SWITCHER,
				'return_value' => 'yes',
				'default'      => 'yes',
				'separator'    => 'before',
			)
		);

		$this->add_control(
			'attributes',
			array(
				'label'       => esc_html__( 'Attributes', 'captain-widgets-kit' ),
				'type'        => Controls_Manager::SELECT2,
				'label_block' => true,
				'multiple'    => true,
				'options'     => $this->captwiki_get_attribute_options(),
				'description' => esc_html__( 'Leave empty to show all global product attributes.', 'captain-widgets-kit' ),
				'condition'   => array( 'show_attributes' => 'yes' ),
			)
		);

		$this->add_control(
			'show_rating',
			array(
				'label'        => esc_html__( 'Rating Filter', 'captain-widgets-kit' ),
				'type'         => Controls_Manager::SWITCHER,
				'return_value' => 'yes',
				'default'      => 'yes',
				'separator'    => 'before',
			)
		);

		$this->add_control(
			'rating_heading',
			array(
				'label'       => esc_html__( 'Rating Title', 'captain-widgets-kit' ),
				'type'        => Controls_Manager::TEXT,
				'default'     => esc_html__( 'Rating', 'captain-widgets-kit' ),
				'label_block' => true,
				'condition'   => array( 'show_rating' => 'yes' ),
			)
		);

		$this->add_control(
			'buttons_heading',
			array(
				'label'     => esc_html__( 'Buttons', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::HEADING,
				'separator' => 'before',
			)
		);

		$this->add_control(
			'button_text',
			array(
				'label'       => esc_html__( 'Apply Button', 'captain-widgets-kit' ),
				'type'        => Controls_Manager::TEXT,
				'default'     => esc_html__( 'Apply Filters', 'captain-widgets-kit' ),
				'label_block' => true,
			)
		);

		$this->add_control(
			'show_reset',
			array(
				'label'        => esc_html__( 'Show Reset', 'captain-widgets-kit' ),
				'type'         => Controls_Manager::SWITCHER,
				'return_value' => 'yes',
				'default'      => 'yes',
			)
		);

		$this->add_control(
			'reset_text',
			array(
				'label'       => esc_html__( 'Reset Text', 'captain-widgets-kit' ),
				'type'        => Controls_Manager::TEXT,
				'default'     => esc_html__( 'Reset', 'captain-widgets-kit' ),
				'label_block' => true,
				'condition'   => array( 'show_reset' => 'yes' ),
			)
		);

		$this->end_controls_section();
	}

	/**
	 * General style controls.
	 *
	 * @since 1.3.0
	 */
	private function register_style_controls(): void {

		$title_selector = '{{WRAPPER}} .captwiki-filter__title';
		$label_selector = '{{WRAPPER}} .captwiki-filter__option';

		$this->start_controls_section(
			'section_style_general',
			array(
				'label' => esc_html__( 'Filter', 'captain-widgets-kit' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		$this->add_control(
			'accent_color',
			array(
				'label'     => esc_html__( 'Accent Color', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => '#2563eb',
				'selectors' => array(
					'{{WRAPPER}} .captwiki-filter' => '--captwiki-filter-accent: {{VALUE}};',
				),
			)
		);

		$this->add_control(
			'section_gap',
			array(
				'label'      => esc_html__( 'Section Spacing', 'captain-widgets-kit' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px' ),
				'range'      => array( 'px' => array( 'min' => 0, 'max' => 60, 'step' => 1 ) ),
				'selectors'  => array(
					'{{WRAPPER}} .captwiki-filter' => 'gap: {{SIZE}}{{UNIT}};',
				),
			)
		);

		$this->add_control(
			'title_color',
			array(
				'label'     => esc_html__( 'Title Color', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'separator' => 'before',
				'selectors' => array(
					$title_selector => 'color: {{VALUE}};',
				),
			)
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'title_typography',
				'selector' => $title_selector,
			)
		);

		$this->add_control(
			'option_color',
			array(
				'label'     => esc_html__( 'Option Color', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'separator' => 'before',
				'selectors' => array(
					$label_selector => 'color: {{VALUE}};',
				),
			)
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'option_typography',
				'selector' => $label_selector,
			)
		);

		$this->end_controls_section();
	}

	/**
	 * Button style controls.
	 *
	 * @since 1.3.0
	 */
	private function register_button_style_controls(): void {

		$button_selector = '{{WRAPPER}} .captwiki-filter__apply';

		$this->start_controls_section(
			'section_style_button',
			array(
				'label' => esc_html__( 'Apply Button', 'captain-widgets-kit' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'button_typography',
				'selector' => $button_selector,
			)
		);

		$this->add_control(
			'button_color',
			array(
				'label'     => esc_html__( 'Text Color', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( $button_selector => 'color: {{VALUE}};' ),
			)
		);

		$this->add_control(
			'button_bg',
			array(
				'label'     => esc_html__( 'Background', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( $button_selector => 'background-color: {{VALUE}};' ),
			)
		);

		$this->add_control(
			'button_bg_hover',
			array(
				'label'     => esc_html__( 'Hover Background', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( $button_selector . ':hover' => 'background-color: {{VALUE}};' ),
			)
		);

		$this->add_responsive_control(
			'button_radius',
			array(
				'label'      => esc_html__( 'Border Radius', 'captain-widgets-kit' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', '%' ),
				'selectors'  => array(
					$button_selector => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);

		$this->add_responsive_control(
			'button_padding',
			array(
				'label'      => esc_html__( 'Padding', 'captain-widgets-kit' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', 'em' ),
				'selectors'  => array(
					$button_selector => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);

		$this->end_controls_section();
	}

	/**
	 * Resolve the form action URL.
	 *
	 * @since 1.3.0
	 * @param string $target 'shop' or 'current'.
	 * @return string
	 */
	private function captwiki_form_action( string $target ): string {

		if ( 'current' === $target && ! empty( $_SERVER['REQUEST_URI'] ) ) {
			$path = strtok( wp_unslash( $_SERVER['REQUEST_URI'] ), '?' ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput
			return esc_url_raw( home_url( $path ) );
		}

		$shop = function_exists( 'wc_get_page_permalink' ) ? wc_get_page_permalink( 'shop' ) : '';

		return $shop ? $shop : home_url( '/' );
	}

	/**
	 * Get a sanitized GET value.
	 *
	 * @since 1.3.0
	 * @param string $key Query key.
	 * @return string
	 */
	private function captwiki_get_param( string $key ): string {
		return isset( $_GET[ $key ] ) ? sanitize_text_field( wp_unslash( $_GET[ $key ] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
	}

	/**
	 * Output an editor-only notice.
	 *
	 * @since 1.3.0
	 * @param string $message Notice text.
	 */
	private function captwiki_render_editor_notice( string $message ): void {

		if ( ! \Elementor\Plugin::$instance->editor->is_edit_mode() ) {
			return;
		}

		echo '<div class="captwiki-filter captwiki-filter--notice"><p>' . esc_html( $message ) . '</p></div>';
	}

	/**
	 * Render the price filter section.
	 *
	 * @since 1.3.0
	 * @param array $settings Widget settings.
	 */
	private function captwiki_render_price( array $settings ): void {

		$title = ! empty( $settings['price_heading'] ) ? $settings['price_heading'] : esc_html__( 'Filter by Price', 'captain-widgets-kit' );
		$min   = $this->captwiki_get_param( 'min_price' );
		$max   = $this->captwiki_get_param( 'max_price' );

		echo '<div class="captwiki-filter__section captwiki-filter__section--price">';
		echo '<h4 class="captwiki-filter__title">' . esc_html( $title ) . '</h4>';
		echo '<div class="captwiki-filter__price-row">';
		echo '<input type="number" class="captwiki-filter__price" name="min_price" inputmode="decimal" min="0" placeholder="' . esc_attr__( 'Min', 'captain-widgets-kit' ) . '" value="' . esc_attr( $min ) . '" />';
		echo '<span class="captwiki-filter__price-sep">&ndash;</span>';
		echo '<input type="number" class="captwiki-filter__price" name="max_price" inputmode="decimal" min="0" placeholder="' . esc_attr__( 'Max', 'captain-widgets-kit' ) . '" value="' . esc_attr( $max ) . '" />';
		echo '</div>';
		echo '</div>';
	}

	/**
	 * Render the category filter section.
	 *
	 * @since 1.3.0
	 * @param array $settings Widget settings.
	 */
	private function captwiki_render_categories( array $settings ): void {

		if ( ! taxonomy_exists( 'product_cat' ) ) {
			return;
		}

		$terms = get_terms(
			array(
				'taxonomy'   => 'product_cat',
				'hide_empty' => true,
				'number'     => 100,
			)
		);

		if ( is_wp_error( $terms ) || empty( $terms ) ) {
			return;
		}

		$title   = ! empty( $settings['categories_heading'] ) ? $settings['categories_heading'] : esc_html__( 'Categories', 'captain-widgets-kit' );
		$current = $this->captwiki_get_param( 'product_cat' );

		echo '<div class="captwiki-filter__section captwiki-filter__section--cats">';
		echo '<h4 class="captwiki-filter__title">' . esc_html( $title ) . '</h4>';
		echo '<div class="captwiki-filter__options">';

		echo '<label class="captwiki-filter__option">';
		echo '<input type="radio" name="product_cat" value="" class="captwiki-filter__radio"' . checked( '', $current, false ) . ' />';
		echo '<span>' . esc_html__( 'All', 'captain-widgets-kit' ) . '</span>';
		echo '</label>';

		foreach ( $terms as $term ) {
			echo '<label class="captwiki-filter__option">';
			echo '<input type="radio" name="product_cat" value="' . esc_attr( $term->slug ) . '" class="captwiki-filter__radio"' . checked( $term->slug, $current, false ) . ' />';
			echo '<span>' . esc_html( $term->name ) . '</span>';
			echo '<span class="captwiki-filter__opt-count">' . esc_html( number_format_i18n( $term->count ) ) . '</span>';
			echo '</label>';
		}

		echo '</div>';
		echo '</div>';
	}

	/**
	 * Render attribute filter sections.
	 *
	 * @since 1.3.0
	 * @param array $settings Widget settings.
	 */
	private function captwiki_render_attributes( array $settings ): void {

		$chosen_taxes = ! empty( $settings['attributes'] ) ? (array) $settings['attributes'] : array_keys( $this->captwiki_get_attribute_options() );

		foreach ( $chosen_taxes as $taxonomy ) {

			$taxonomy = sanitize_key( $taxonomy );

			if ( ! taxonomy_exists( $taxonomy ) ) {
				continue;
			}

			$terms = get_terms(
				array(
					'taxonomy'   => $taxonomy,
					'hide_empty' => true,
				)
			);

			if ( is_wp_error( $terms ) || empty( $terms ) ) {
				continue;
			}

			// WooCommerce layered-nav param: filter_<attribute> (no pa_ prefix).
			$attribute_slug = str_replace( 'pa_', '', $taxonomy );
			$param          = 'filter_' . $attribute_slug;
			$current        = $this->captwiki_get_param( $param );
			$chosen         = '' !== $current ? array_map( 'sanitize_title', explode( ',', $current ) ) : array();

			$tax_obj = get_taxonomy( $taxonomy );
			$label   = $tax_obj && ! empty( $tax_obj->labels->singular_name ) ? $tax_obj->labels->singular_name : $attribute_slug;

			echo '<div class="captwiki-filter__section captwiki-filter__section--attr">';
			echo '<h4 class="captwiki-filter__title">' . esc_html( $label ) . '</h4>';
			echo '<div class="captwiki-filter__options">';

			foreach ( $terms as $term ) {
				$checked = in_array( $term->slug, $chosen, true ) ? ' checked' : '';
				echo '<label class="captwiki-filter__option">';
				echo '<input type="checkbox" class="captwiki-filter__check" data-key="' . esc_attr( $param ) . '" value="' . esc_attr( $term->slug ) . '"' . esc_attr( $checked ) . ' />';
				echo '<span>' . esc_html( $term->name ) . '</span>';
				echo '<span class="captwiki-filter__opt-count">' . esc_html( number_format_i18n( $term->count ) ) . '</span>';
				echo '</label>';
			}

			echo '</div>';
			echo '</div>';
		}
	}

	/**
	 * Render the rating filter section.
	 *
	 * @since 1.3.0
	 * @param array $settings Widget settings.
	 */
	private function captwiki_render_rating( array $settings ): void {

		$title   = ! empty( $settings['rating_heading'] ) ? $settings['rating_heading'] : esc_html__( 'Rating', 'captain-widgets-kit' );
		$current = $this->captwiki_get_param( 'rating_filter' );
		$chosen  = '' !== $current ? array_map( 'absint', explode( ',', $current ) ) : array();

		echo '<div class="captwiki-filter__section captwiki-filter__section--rating">';
		echo '<h4 class="captwiki-filter__title">' . esc_html( $title ) . '</h4>';
		echo '<div class="captwiki-filter__options">';

		for ( $stars = 5; $stars >= 1; $stars-- ) {
			$checked = in_array( $stars, $chosen, true ) ? ' checked' : '';

			echo '<label class="captwiki-filter__option captwiki-filter__option--rating">';
			echo '<input type="checkbox" class="captwiki-filter__check" data-key="rating_filter" value="' . esc_attr( (string) $stars ) . '"' . esc_attr( $checked ) . ' />';
			echo '<span class="captwiki-filter__stars" aria-hidden="true">';
			for ( $i = 1; $i <= 5; $i++ ) {
				echo '<span class="captwiki-filter__star' . ( $i <= $stars ? ' is-filled' : '' ) . '">&#9733;</span>';
			}
			echo '</span>';
			/* translators: %d: number of stars. */
			echo '<span class="screen-reader-text">' . esc_html( sprintf( _n( '%d star', '%d stars', $stars, 'captain-widgets-kit' ), $stars ) ) . '</span>';
			echo '</label>';
		}

		echo '</div>';
		echo '</div>';
	}

	/**
	 * Output hidden inputs preserving unrelated query params.
	 *
	 * @since 1.3.0
	 */
	private function captwiki_render_preserved_params(): void {

		if ( empty( $_GET ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
			return;
		}

		$managed = array( 'min_price', 'max_price', 'product_cat', 'rating_filter', 'paged', 'submit' );

		foreach ( $_GET as $key => $value ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended

			$key = sanitize_key( $key );

			if ( in_array( $key, $managed, true ) || 0 === strpos( $key, 'filter_' ) || 0 === strpos( $key, 'query_type_' ) ) {
				continue;
			}

			if ( ! is_scalar( $value ) ) {
				continue;
			}

			echo '<input type="hidden" name="' . esc_attr( $key ) . '" value="' . esc_attr( sanitize_text_field( wp_unslash( $value ) ) ) . '" />';
		}
	}

	/**
	 * Render widget output on the frontend.
	 *
	 * @since 1.3.0
	 */
	protected function render(): void {

		if ( ! class_exists( 'WooCommerce' ) ) {
			$this->captwiki_render_editor_notice( esc_html__( 'WooCommerce is required for the Product Filter widget.', 'captain-widgets-kit' ) );
			return;
		}

		$settings = $this->get_settings_for_display();

		$allowed_styles = array_keys( $this->captwiki_style_options() );
		$style          = ! empty( $settings['widget_style'] ) && in_array( $settings['widget_style'], $allowed_styles, true ) ? $settings['widget_style'] : 'style-1';

		wp_enqueue_script( 'captwiki-woo-product-filter' );
		wp_enqueue_style(
			'captwiki-woo-product-filter-' . $style,
			CAPTWIKI_URL . 'assets/css/widgets/woo-product-filter/captwiki-woo-product-filter-' . $style . '.css',
			array( 'captwiki-woo-product-filter' ),
			CAPTWIKI_VERSION
		);

		$target = ! empty( $settings['target'] ) ? $settings['target'] : 'shop';
		$action = $this->captwiki_form_action( $target );

		$wrapper_classes = array(
			'captwiki-filter',
			'captwiki-' . $style,
			'captwiki-widget-' . $this->get_id(),
		);

		echo '<form class="' . esc_attr( implode( ' ', $wrapper_classes ) ) . '" method="get" action="' . esc_url( $action ) . '">';

		$this->captwiki_render_preserved_params();

		if ( ! empty( $settings['show_price'] ) && 'yes' === $settings['show_price'] ) {
			$this->captwiki_render_price( $settings );
		}

		if ( ! empty( $settings['show_categories'] ) && 'yes' === $settings['show_categories'] ) {
			$this->captwiki_render_categories( $settings );
		}

		if ( ! empty( $settings['show_attributes'] ) && 'yes' === $settings['show_attributes'] ) {
			$this->captwiki_render_attributes( $settings );
		}

		if ( ! empty( $settings['show_rating'] ) && 'yes' === $settings['show_rating'] ) {
			$this->captwiki_render_rating( $settings );
		}

		$button_text = ! empty( $settings['button_text'] ) ? $settings['button_text'] : esc_html__( 'Apply Filters', 'captain-widgets-kit' );

		echo '<div class="captwiki-filter__actions">';
		echo '<button type="submit" class="captwiki-filter__apply">' . esc_html( $button_text ) . '</button>';

		if ( ! empty( $settings['show_reset'] ) && 'yes' === $settings['show_reset'] ) {
			$reset_text = ! empty( $settings['reset_text'] ) ? $settings['reset_text'] : esc_html__( 'Reset', 'captain-widgets-kit' );
			echo '<a class="captwiki-filter__reset" href="' . esc_url( $action ) . '">' . esc_html( $reset_text ) . '</a>';
		}

		echo '</div>';
		echo '</form>';
	}
}
