<?php
/**
 * Widget Name: Product Excerpt
 * Description: Outputs the WooCommerce product short description (excerpt) for the
 *              current product or a chosen product, with word/character trimming,
 *              a long-description fallback, an optional heading, a "read more"
 *              link, empty-state handling and multiple attractive styles.
 *
 * @package    captain-widgets-kit
 * @subpackage captain-widgets-kit/includes
 * @since      1.4.0
 */

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

use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Group_Control_Typography;
use Elementor\Group_Control_Border;
use Elementor\Group_Control_Box_Shadow;

/**
 * Class Captwiki_Woo_Product_Excerpt
 *
 * @since 1.4.0
 */
class Captwiki_Woo_Product_Excerpt extends Widget_Base {

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

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

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

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

	/**
	 * Get widget keywords.
	 *
	 * @since 1.4.0
	 * @return array
	 */
	public function get_keywords(): array {
		return array( 'woocommerce', 'woo', 'product', 'excerpt', 'short description', 'summary', 'captain-widgets-kit' );
	}

	/**
	 * Disable Elementor's default inner wrapper.
	 *
	 * @since 1.4.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.4.0
	 * @return array
	 */
	private function captwiki_style_options(): array {
		return array(
			'style-1' => esc_html__( 'Style 1 – Simple', 'captain-widgets-kit' ),
			'style-2' => esc_html__( 'Style 2 – Quote', 'captain-widgets-kit' ),
			'style-3' => esc_html__( 'Style 3 – Card', 'captain-widgets-kit' ),
			'style-4' => esc_html__( 'Style 4 – Accent Bar', 'captain-widgets-kit' ),
			'style-5' => esc_html__( 'Style 5 – Callout', 'captain-widgets-kit' ),
			'style-6' => esc_html__( 'Style 6 – Underline', 'captain-widgets-kit' ),
		);
	}

	/**
	 * Build a list of published products for the source selector.
	 *
	 * @since 1.4.0
	 * @return array
	 */
	private function captwiki_get_products_options(): array {

		$options = array();

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

		$products = wc_get_products(
			array(
				'status'  => 'publish',
				'limit'   => 50,
				'orderby' => 'title',
				'order'   => 'ASC',
				'return'  => 'objects',
			)
		);

		foreach ( $products as $product ) {
			$options[ $product->get_id() ] = $product->get_name();
		}

		return $options;
	}

	/**
	 * Register controls.
	 *
	 * @since 1.4.0
	 */
	protected function register_controls(): void {
		$this->register_content_controls();
		$this->register_title_style_controls();
		$this->register_text_style_controls();
		$this->register_readmore_style_controls();
	}

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

		$this->start_controls_section(
			'section_content',
			array(
				'label' => esc_html__( 'Excerpt', '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(
			'product_source',
			array(
				'label'   => esc_html__( 'Product', 'captain-widgets-kit' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'current',
				'options' => array(
					'current'  => esc_html__( 'Current Product', 'captain-widgets-kit' ),
					'specific' => esc_html__( 'Select a Product', 'captain-widgets-kit' ),
				),
			)
		);

		$this->add_control(
			'product_id',
			array(
				'label'       => esc_html__( 'Choose Product', 'captain-widgets-kit' ),
				'type'        => Controls_Manager::SELECT2,
				'label_block' => true,
				'options'     => $this->captwiki_get_products_options(),
				'condition'   => array( 'product_source' => 'specific' ),
			)
		);

		$this->add_control(
			'fallback_to_long',
			array(
				'label'        => esc_html__( 'Fallback To Full Description', 'captain-widgets-kit' ),
				'type'         => Controls_Manager::SWITCHER,
				'label_on'     => esc_html__( 'Yes', 'captain-widgets-kit' ),
				'label_off'    => esc_html__( 'No', 'captain-widgets-kit' ),
				'return_value' => 'yes',
				'default'      => 'yes',
				'description'  => esc_html__( 'When the product has no short description, pull a trimmed version of the full description instead.', 'captain-widgets-kit' ),
			)
		);

		$this->add_control(
			'limit_type',
			array(
				'label'     => esc_html__( 'Trim By', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::SELECT,
				'default'   => 'none',
				'options'   => array(
					'none'  => esc_html__( 'No Limit', 'captain-widgets-kit' ),
					'words' => esc_html__( 'Words', 'captain-widgets-kit' ),
					'chars' => esc_html__( 'Characters', 'captain-widgets-kit' ),
				),
				'separator' => 'before',
			)
		);

		$this->add_control(
			'limit_count',
			array(
				'label'     => esc_html__( 'Limit', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::NUMBER,
				'min'       => 1,
				'max'       => 1000,
				'default'   => 25,
				'condition' => array( 'limit_type!' => 'none' ),
			)
		);

		$this->add_control(
			'ellipsis',
			array(
				'label'     => esc_html__( 'Ellipsis', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::TEXT,
				'default'   => '…',
				'condition' => array( 'limit_type!' => 'none' ),
			)
		);

		$this->add_control(
			'show_title',
			array(
				'label'        => esc_html__( 'Show Heading', 'captain-widgets-kit' ),
				'type'         => Controls_Manager::SWITCHER,
				'label_on'     => esc_html__( 'Show', 'captain-widgets-kit' ),
				'label_off'    => esc_html__( 'Hide', 'captain-widgets-kit' ),
				'return_value' => 'yes',
				'default'      => 'no',
				'separator'    => 'before',
			)
		);

		$this->add_control(
			'title_text',
			array(
				'label'       => esc_html__( 'Heading Text', 'captain-widgets-kit' ),
				'type'        => Controls_Manager::TEXT,
				'default'     => esc_html__( 'Overview', 'captain-widgets-kit' ),
				'label_block' => true,
				'condition'   => array( 'show_title' => 'yes' ),
			)
		);

		$this->add_control(
			'title_tag',
			array(
				'label'     => esc_html__( 'Heading Tag', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::SELECT,
				'default'   => 'h3',
				'options'   => array(
					'h2'   => 'H2',
					'h3'   => 'H3',
					'h4'   => 'H4',
					'h5'   => 'H5',
					'div'  => 'div',
					'span' => 'span',
				),
				'condition' => array( 'show_title' => 'yes' ),
			)
		);

		$this->add_control(
			'show_readmore',
			array(
				'label'        => esc_html__( 'Show Read More Link', 'captain-widgets-kit' ),
				'type'         => Controls_Manager::SWITCHER,
				'label_on'     => esc_html__( 'Show', 'captain-widgets-kit' ),
				'label_off'    => esc_html__( 'Hide', 'captain-widgets-kit' ),
				'return_value' => 'yes',
				'default'      => 'no',
				'separator'    => 'before',
			)
		);

		$this->add_control(
			'readmore_text',
			array(
				'label'     => esc_html__( 'Read More Label', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::TEXT,
				'default'   => esc_html__( 'Read more', 'captain-widgets-kit' ),
				'condition' => array( 'show_readmore' => 'yes' ),
			)
		);

		$this->add_control(
			'readmore_new_tab',
			array(
				'label'        => esc_html__( 'Open In New Tab', 'captain-widgets-kit' ),
				'type'         => Controls_Manager::SWITCHER,
				'label_on'     => esc_html__( 'Yes', 'captain-widgets-kit' ),
				'label_off'    => esc_html__( 'No', 'captain-widgets-kit' ),
				'return_value' => 'yes',
				'default'      => 'no',
				'condition'    => array( 'show_readmore' => 'yes' ),
			)
		);

		$this->add_control(
			'empty_text',
			array(
				'label'       => esc_html__( 'Empty Fallback', 'captain-widgets-kit' ),
				'type'        => Controls_Manager::TEXTAREA,
				'rows'        => 2,
				'default'     => '',
				'label_block' => true,
				'separator'   => 'before',
				'description' => esc_html__( 'Shown when no excerpt is available. Leave empty to output nothing on the live site.', 'captain-widgets-kit' ),
			)
		);

		$this->add_responsive_control(
			'text_align',
			array(
				'label'     => esc_html__( 'Alignment', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::CHOOSE,
				'options'   => array(
					'left'    => array(
						'title' => esc_html__( 'Left', 'captain-widgets-kit' ),
						'icon'  => 'eicon-text-align-left',
					),
					'center'  => array(
						'title' => esc_html__( 'Center', 'captain-widgets-kit' ),
						'icon'  => 'eicon-text-align-center',
					),
					'right'   => array(
						'title' => esc_html__( 'Right', 'captain-widgets-kit' ),
						'icon'  => 'eicon-text-align-right',
					),
					'justify' => array(
						'title' => esc_html__( 'Justify', 'captain-widgets-kit' ),
						'icon'  => 'eicon-text-align-justify',
					),
				),
				'default'   => 'left',
				'selectors' => array(
					'{{WRAPPER}} .captwiki-pexc' => 'text-align: {{VALUE}};',
				),
			)
		);

		$this->end_controls_section();
	}

	/**
	 * Heading style controls.
	 *
	 * @since 1.4.0
	 */
	private function register_title_style_controls(): void {

		$selector = '{{WRAPPER}} .captwiki-pexc__title';

		$this->start_controls_section(
			'section_title_style',
			array(
				'label'     => esc_html__( 'Heading', 'captain-widgets-kit' ),
				'tab'       => Controls_Manager::TAB_STYLE,
				'condition' => array( 'show_title' => 'yes' ),
			)
		);

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

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

		$this->add_responsive_control(
			'title_spacing',
			array(
				'label'      => esc_html__( 'Spacing Below', 'captain-widgets-kit' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px' ),
				'range'      => array( 'px' => array( 'min' => 0, 'max' => 60 ) ),
				'selectors'  => array(
					$selector => 'margin-bottom: {{SIZE}}{{UNIT}};',
				),
			)
		);

		$this->end_controls_section();
	}

	/**
	 * Excerpt text style controls.
	 *
	 * @since 1.4.0
	 */
	private function register_text_style_controls(): void {

		$wrap_selector = '{{WRAPPER}} .captwiki-pexc';
		$text_selector = '{{WRAPPER}} .captwiki-pexc__text';

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

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

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'text_typography',
				'selector' => $text_selector . ', ' . $text_selector . ' p',
			)
		);

		$this->add_control(
			'accent_color',
			array(
				'label'       => esc_html__( 'Accent Color', 'captain-widgets-kit' ),
				'type'        => Controls_Manager::COLOR,
				'description' => esc_html__( 'Used for the bar / quote mark / border accents in the selected style.', 'captain-widgets-kit' ),
				'selectors'   => array(
					'{{WRAPPER}} .captwiki-pexc.captwiki-style-2 .captwiki-pexc__text::before' => 'color: {{VALUE}};',
					'{{WRAPPER}} .captwiki-pexc.captwiki-style-2'                              => 'border-color: {{VALUE}};',
					'{{WRAPPER}} .captwiki-pexc.captwiki-style-4'                              => 'border-left-color: {{VALUE}};',
					'{{WRAPPER}} .captwiki-pexc.captwiki-style-5'                              => 'border-color: {{VALUE}};',
					'{{WRAPPER}} .captwiki-pexc.captwiki-style-6 .captwiki-pexc__text'          => 'border-bottom-color: {{VALUE}};',
				),
			)
		);

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

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

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

		$this->add_group_control(
			Group_Control_Border::get_type(),
			array(
				'name'     => 'box_border',
				'selector' => $wrap_selector,
			)
		);

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

		$this->add_group_control(
			Group_Control_Box_Shadow::get_type(),
			array(
				'name'     => 'box_shadow',
				'selector' => $wrap_selector,
			)
		);

		$this->end_controls_section();
	}

	/**
	 * Read more link style controls.
	 *
	 * @since 1.4.0
	 */
	private function register_readmore_style_controls(): void {

		$selector = '{{WRAPPER}} .captwiki-pexc__more';

		$this->start_controls_section(
			'section_readmore_style',
			array(
				'label'     => esc_html__( 'Read More Link', 'captain-widgets-kit' ),
				'tab'       => Controls_Manager::TAB_STYLE,
				'condition' => array( 'show_readmore' => 'yes' ),
			)
		);

		$this->start_controls_tabs( 'readmore_tabs' );

		$this->start_controls_tab(
			'readmore_tab_normal',
			array( 'label' => esc_html__( 'Normal', 'captain-widgets-kit' ) )
		);

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

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

		$this->end_controls_tab();

		$this->start_controls_tab(
			'readmore_tab_hover',
			array( 'label' => esc_html__( 'Hover', 'captain-widgets-kit' ) )
		);

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

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

		$this->end_controls_tab();

		$this->end_controls_tabs();

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'      => 'readmore_typography',
				'selector'  => $selector,
				'separator' => 'before',
			)
		);

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

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

		$this->end_controls_section();
	}

	/**
	 * Resolve the product.
	 *
	 * @since 1.4.0
	 * @param array $settings Widget settings.
	 * @return \WC_Product|false
	 */
	private function captwiki_resolve_product( array $settings ) {

		$source = ! empty( $settings['product_source'] ) ? $settings['product_source'] : 'current';

		if ( 'specific' === $source && ! empty( $settings['product_id'] ) ) {
			return wc_get_product( absint( $settings['product_id'] ) );
		}

		$product = wc_get_product( get_the_ID() );

		if ( $product ) {
			return $product;
		}

		if ( \Elementor\Plugin::$instance->editor->is_edit_mode() || \Elementor\Plugin::$instance->preview->is_preview_mode() ) {
			$latest = wc_get_products(
				array(
					'status' => 'publish',
					'limit'  => 1,
				)
			);

			if ( ! empty( $latest ) ) {
				return $latest[0];
			}
		}

		return false;
	}

	/**
	 * Trim a string to a word/character limit.
	 *
	 * @since 1.4.0
	 * @param string $text     Source text.
	 * @param array  $settings Widget settings.
	 * @return string
	 */
	private function captwiki_trim( string $text, array $settings ): string {

		$type = ! empty( $settings['limit_type'] ) ? $settings['limit_type'] : 'none';

		if ( 'none' === $type ) {
			return $text;
		}

		$count    = ! empty( $settings['limit_count'] ) ? absint( $settings['limit_count'] ) : 25;
		$ellipsis = isset( $settings['ellipsis'] ) ? $settings['ellipsis'] : '…';
		$plain    = trim( wp_strip_all_tags( $text ) );

		if ( 'words' === $type ) {
			return wp_trim_words( $plain, $count, $ellipsis );
		}

		// Characters.
		if ( function_exists( 'mb_strlen' ) ? mb_strlen( $plain ) <= $count : strlen( $plain ) <= $count ) {
			return $plain;
		}

		$cut = function_exists( 'mb_substr' ) ? mb_substr( $plain, 0, $count ) : substr( $plain, 0, $count );

		return rtrim( $cut ) . $ellipsis;
	}

	/**
	 * Output an editor-only notice.
	 *
	 * @since 1.4.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-pexc captwiki-pexc--notice"><p>' . esc_html( $message ) . '</p></div>';
	}

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

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

		$settings = $this->get_settings_for_display();
		$product  = $this->captwiki_resolve_product( $settings );

		if ( ! $product instanceof \WC_Product ) {
			$this->captwiki_render_editor_notice( esc_html__( 'No product found. Place this widget on a product template or select a product.', 'captain-widgets-kit' ) );
			return;
		}

		$raw      = (string) $product->get_short_description();
		$fallback = ! empty( $settings['fallback_to_long'] ) && 'yes' === $settings['fallback_to_long'];

		if ( '' === trim( $raw ) && $fallback ) {
			$raw = (string) $product->get_description();
		}

		$is_limited = ! empty( $settings['limit_type'] ) && 'none' !== $settings['limit_type'];

		if ( '' === trim( $raw ) ) {
			$empty = isset( $settings['empty_text'] ) ? trim( (string) $settings['empty_text'] ) : '';

			if ( '' === $empty ) {
				$this->captwiki_render_editor_notice( esc_html__( 'This product has no excerpt (nothing is shown on the live site).', 'captain-widgets-kit' ) );
				return;
			}

			$raw = $empty;
		}

		if ( $is_limited ) {
			$text = '<p>' . esc_html( $this->captwiki_trim( $raw, $settings ) ) . '</p>';
		} else {
			$text = apply_filters( 'woocommerce_short_description', $raw );
		}

		$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_style(
			'captwiki-woo-product-excerpt-' . $style,
			CAPTWIKI_URL . 'assets/css/widgets/woo-product-excerpt/captwiki-woo-product-excerpt-' . $style . '.css',
			array( 'captwiki-woo-product-excerpt' ),
			CAPTWIKI_VERSION
		);

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

		echo '<div class="' . esc_attr( implode( ' ', $wrapper_classes ) ) . '">';

		if ( ! empty( $settings['show_title'] ) && 'yes' === $settings['show_title'] && ! empty( $settings['title_text'] ) ) {
			$tag         = ! empty( $settings['title_tag'] ) ? $settings['title_tag'] : 'h3';
			$allowed_tag = array( 'h2', 'h3', 'h4', 'h5', 'div', 'span' );
			$tag         = in_array( $tag, $allowed_tag, true ) ? $tag : 'h3';

			echo '<' . esc_attr( $tag ) . ' class="captwiki-pexc__title">' . esc_html( $settings['title_text'] ) . '</' . esc_attr( $tag ) . '>';
		}

		echo '<div class="captwiki-pexc__text">' . wp_kses_post( $text ) . '</div>';

		if ( ! empty( $settings['show_readmore'] ) && 'yes' === $settings['show_readmore'] ) {
			$more     = ! empty( $settings['readmore_text'] ) ? $settings['readmore_text'] : esc_html__( 'Read more', 'captain-widgets-kit' );
			$link     = get_permalink( $product->get_id() );
			$new_tab  = ! empty( $settings['readmore_new_tab'] ) && 'yes' === $settings['readmore_new_tab'];
			$tab_attr = $new_tab ? ' target="_blank" rel="noopener noreferrer"' : '';

			echo '<a class="captwiki-pexc__more" href="' . esc_url( $link ) . '"' . $tab_attr . '>' . esc_html( $more ) . '</a>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
		}

		echo '</div>';
	}
}
