<?php
/**
 * Widget Name: Product Meta
 * Description: Outputs WooCommerce product meta (SKU, categories, tags and
 *              visible attributes) for the current product or a chosen product,
 *              with per-item toggles, custom labels, optional term links, empty
 *              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_Meta
 *
 * @since 1.4.0
 */
class Captwiki_Woo_Product_Meta extends Widget_Base {

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

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

	/**
	 * Get widget icon.
	 *
	 * @since 1.4.0
	 * @return string
	 */
	public function get_icon(): string {
		return 'eicon-product-meta 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', 'meta', 'sku', 'category', 'tags', 'attributes', '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 – Classic', 'captain-widgets-kit' ),
			'style-2' => esc_html__( 'Style 2 – Inline', 'captain-widgets-kit' ),
			'style-3' => esc_html__( 'Style 3 – Table', 'captain-widgets-kit' ),
			'style-4' => esc_html__( 'Style 4 – Boxed', 'captain-widgets-kit' ),
			'style-5' => esc_html__( 'Style 5 – Pills', 'captain-widgets-kit' ),
			'style-6' => esc_html__( 'Style 6 – Bordered Rows', '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_label_style_controls();
		$this->register_value_style_controls();
		$this->register_box_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__( 'Meta', '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(
			'show_label',
			array(
				'label'        => esc_html__( 'Show Labels', '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'      => 'yes',
			)
		);

		$this->add_control(
			'link_terms',
			array(
				'label'        => esc_html__( 'Link Categories & Tags', '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',
			)
		);

		$this->add_control(
			'term_separator',
			array(
				'label'   => esc_html__( 'Term Separator', 'captain-widgets-kit' ),
				'type'    => Controls_Manager::TEXT,
				'default' => ', ',
			)
		);

		$this->add_control(
			'hide_empty',
			array(
				'label'        => esc_html__( 'Hide Empty Items', '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',
			)
		);

		// SKU.
		$this->add_control(
			'sku_heading',
			array(
				'label'     => esc_html__( 'SKU', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::HEADING,
				'separator' => 'before',
			)
		);

		$this->add_control(
			'show_sku',
			array(
				'label'        => esc_html__( 'Show SKU', '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'      => 'yes',
			)
		);

		$this->add_control(
			'sku_label',
			array(
				'label'     => esc_html__( 'SKU Label', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::TEXT,
				'default'   => esc_html__( 'SKU:', 'captain-widgets-kit' ),
				'condition' => array( 'show_sku' => 'yes' ),
			)
		);

		// Categories.
		$this->add_control(
			'cat_heading',
			array(
				'label'     => esc_html__( 'Categories', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::HEADING,
				'separator' => 'before',
			)
		);

		$this->add_control(
			'show_categories',
			array(
				'label'        => esc_html__( 'Show Categories', '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'      => 'yes',
			)
		);

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

		// Tags.
		$this->add_control(
			'tag_heading',
			array(
				'label'     => esc_html__( 'Tags', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::HEADING,
				'separator' => 'before',
			)
		);

		$this->add_control(
			'show_tags',
			array(
				'label'        => esc_html__( 'Show Tags', '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'      => 'yes',
			)
		);

		$this->add_control(
			'tags_label',
			array(
				'label'     => esc_html__( 'Tags Label', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::TEXT,
				'default'   => esc_html__( 'Tags:', 'captain-widgets-kit' ),
				'condition' => array( 'show_tags' => 'yes' ),
			)
		);

		// Attributes.
		$this->add_control(
			'attr_heading',
			array(
				'label'     => esc_html__( 'Attributes', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::HEADING,
				'separator' => 'before',
			)
		);

		$this->add_control(
			'show_attributes',
			array(
				'label'        => esc_html__( 'Show Attributes', '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',
				'description'  => esc_html__( 'Displays the product\'s visible custom attributes as additional meta rows.', 'captain-widgets-kit' ),
			)
		);

		$this->add_responsive_control(
			'meta_align',
			array(
				'label'     => esc_html__( 'Alignment', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::CHOOSE,
				'options'   => array(
					'flex-start' => 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',
					),
					'flex-end'   => array(
						'title' => esc_html__( 'Right', 'captain-widgets-kit' ),
						'icon'  => 'eicon-text-align-right',
					),
				),
				'default'   => 'flex-start',
				'separator' => 'before',
				'selectors' => array(
					'{{WRAPPER}} .captwiki-pmeta__row' => 'justify-content: {{VALUE}};',
					'{{WRAPPER}} .captwiki-pmeta'      => 'align-items: {{VALUE}};',
				),
			)
		);

		$this->end_controls_section();
	}

	/**
	 * Label style controls.
	 *
	 * @since 1.4.0
	 */
	private function register_label_style_controls(): void {

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

		$this->start_controls_section(
			'section_label_style',
			array(
				'label'     => esc_html__( 'Labels', 'captain-widgets-kit' ),
				'tab'       => Controls_Manager::TAB_STYLE,
				'condition' => array( 'show_label' => 'yes' ),
			)
		);

		$this->add_control(
			'label_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'     => 'label_typography',
				'selector' => $selector,
			)
		);

		$this->end_controls_section();
	}

	/**
	 * Value style controls.
	 *
	 * @since 1.4.0
	 */
	private function register_value_style_controls(): void {

		$value_selector = '{{WRAPPER}} .captwiki-pmeta__value';
		$link_selector  = '{{WRAPPER}} .captwiki-pmeta__value a';

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

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

		$this->start_controls_tabs( 'link_tabs' );

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

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

		$this->end_controls_tab();

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

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

		$this->end_controls_tab();

		$this->end_controls_tabs();

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

		$this->add_control(
			'pill_heading',
			array(
				'label'       => esc_html__( 'Pills (Style 5)', 'captain-widgets-kit' ),
				'type'        => Controls_Manager::HEADING,
				'separator'   => 'before',
				'description' => esc_html__( 'Applies when the Pills style is selected.', 'captain-widgets-kit' ),
			)
		);

		$this->add_control(
			'pill_bg',
			array(
				'label'     => esc_html__( 'Pill Background', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .captwiki-pmeta.captwiki-style-5 .captwiki-pmeta__value a, {{WRAPPER}} .captwiki-pmeta.captwiki-style-5 .captwiki-pmeta__value .captwiki-pmeta__term' => 'background-color: {{VALUE}};',
				),
			)
		);

		$this->end_controls_section();
	}

	/**
	 * Box / layout style controls.
	 *
	 * @since 1.4.0
	 */
	private function register_box_style_controls(): void {

		$wrap_selector = '{{WRAPPER}} .captwiki-pmeta';

		$this->start_controls_section(
			'section_box_style',
			array(
				'label' => esc_html__( 'Box & Spacing', 'captain-widgets-kit' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		$this->add_responsive_control(
			'row_gap',
			array(
				'label'      => esc_html__( 'Row Gap', 'captain-widgets-kit' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px' ),
				'range'      => array( 'px' => array( 'min' => 0, 'max' => 40 ) ),
				'selectors'  => array(
					$wrap_selector => 'gap: {{SIZE}}{{UNIT}};',
				),
			)
		);

		$this->add_responsive_control(
			'label_value_gap',
			array(
				'label'      => esc_html__( 'Label / Value Gap', 'captain-widgets-kit' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px' ),
				'range'      => array( 'px' => array( 'min' => 0, 'max' => 30 ) ),
				'selectors'  => array(
					'{{WRAPPER}} .captwiki-pmeta__row' => 'gap: {{SIZE}}{{UNIT}};',
				),
			)
		);

		$this->add_control(
			'box_bg',
			array(
				'label'     => esc_html__( 'Background', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'separator' => 'before',
				'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();
	}

	/**
	 * 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;
	}

	/**
	 * Build a term value string (linked or plain) for a taxonomy.
	 *
	 * @since 1.4.0
	 * @param \WC_Product $product   Product.
	 * @param string      $taxonomy  Taxonomy slug.
	 * @param string      $separator Separator.
	 * @param bool        $link      Whether to link terms.
	 * @return string
	 */
	private function captwiki_term_value( $product, string $taxonomy, string $separator, bool $link ): string {

		$terms = get_the_terms( $product->get_id(), $taxonomy );

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

		$items = array();

		foreach ( $terms as $term ) {
			if ( $link ) {
				$url = get_term_link( $term, $taxonomy );

				if ( ! is_wp_error( $url ) ) {
					$items[] = '<a href="' . esc_url( $url ) . '">' . esc_html( $term->name ) . '</a>';
					continue;
				}
			}

			$items[] = '<span class="captwiki-pmeta__term">' . esc_html( $term->name ) . '</span>';
		}

		return implode( esc_html( $separator ), $items );
	}

	/**
	 * Render a single meta row.
	 *
	 * @since 1.4.0
	 * @param string $key        Row key (sku, categories, tags...).
	 * @param string $label      Label text.
	 * @param string $value_html Value markup (already escaped).
	 * @param bool   $show_label Whether labels are shown.
	 * @return string
	 */
	private function captwiki_row( string $key, string $label, string $value_html, bool $show_label ): string {

		$out  = '<div class="captwiki-pmeta__row captwiki-pmeta__row--' . esc_attr( $key ) . '">';

		if ( $show_label && '' !== $label ) {
			$out .= '<span class="captwiki-pmeta__label">' . esc_html( $label ) . '</span>';
		}

		$out .= '<span class="captwiki-pmeta__value">' . $value_html . '</span>';
		$out .= '</div>';

		return $out;
	}

	/**
	 * 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-pmeta captwiki-pmeta--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 Meta 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;
		}

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

		$show_label = ! empty( $settings['show_label'] ) && 'yes' === $settings['show_label'];
		$hide_empty = ! empty( $settings['hide_empty'] ) && 'yes' === $settings['hide_empty'];
		$link_terms = ! empty( $settings['link_terms'] ) && 'yes' === $settings['link_terms'];
		$separator  = isset( $settings['term_separator'] ) && '' !== $settings['term_separator'] ? $settings['term_separator'] : ', ';

		$rows = '';

		// SKU.
		if ( ! empty( $settings['show_sku'] ) && 'yes' === $settings['show_sku'] ) {
			$sku = $product->get_sku();

			if ( '' === $sku && ! $hide_empty ) {
				$sku = esc_html__( 'N/A', 'captain-widgets-kit' );
			}

			if ( '' !== $sku ) {
				$rows .= $this->captwiki_row( 'sku', (string) ( $settings['sku_label'] ?? '' ), esc_html( $sku ), $show_label );
			}
		}

		// Categories.
		if ( ! empty( $settings['show_categories'] ) && 'yes' === $settings['show_categories'] ) {
			$value = $this->captwiki_term_value( $product, 'product_cat', $separator, $link_terms );

			if ( '' !== $value ) {
				$rows .= $this->captwiki_row( 'categories', (string) ( $settings['categories_label'] ?? '' ), $value, $show_label );
			}
		}

		// Tags.
		if ( ! empty( $settings['show_tags'] ) && 'yes' === $settings['show_tags'] ) {
			$value = $this->captwiki_term_value( $product, 'product_tag', $separator, $link_terms );

			if ( '' !== $value ) {
				$rows .= $this->captwiki_row( 'tags', (string) ( $settings['tags_label'] ?? '' ), $value, $show_label );
			}
		}

		// Attributes.
		if ( ! empty( $settings['show_attributes'] ) && 'yes' === $settings['show_attributes'] ) {
			foreach ( $product->get_attributes() as $attribute ) {

				if ( ! $attribute instanceof \WC_Product_Attribute || ! $attribute->get_visible() ) {
					continue;
				}

				$name = wc_attribute_label( $attribute->get_name() );

				if ( $attribute->is_taxonomy() ) {
					$values = wc_get_product_terms( $product->get_id(), $attribute->get_name(), array( 'fields' => 'names' ) );
				} else {
					$values = $attribute->get_options();
				}

				if ( empty( $values ) ) {
					continue;
				}

				$value_html = '';
				$parts      = array();

				foreach ( $values as $v ) {
					$parts[] = '<span class="captwiki-pmeta__term">' . esc_html( $v ) . '</span>';
				}

				$value_html = implode( esc_html( $separator ), $parts );
				$key        = 'attr-' . sanitize_html_class( $attribute->get_name() );

				$rows .= $this->captwiki_row( $key, $show_label ? $name . ':' : '', $value_html, $show_label );
			}
		}

		if ( '' === $rows ) {
			$this->captwiki_render_editor_notice( esc_html__( 'No meta to display for this product with the current options.', 'captain-widgets-kit' ) );
			return;
		}

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

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