<?php

namespace SolaceExtraAddons\Modules\DynamicTags\Tags;

use SolaceExtraAddons\Inc\Helper\Solace_Extra_Addons_Helper;

use Elementor\Controls_Manager;
use Elementor\Core\DynamicTags\Tag;
use Elementor\Modules\DynamicTags\Module as TagsModule;

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

class Solace_Extra_Post_Terms extends Tag
{

	public function get_name()
	{
		return 'solace-extra-post-terms';
	}

	public function get_title()
	{
		return esc_html__('Post Terms', 'solace-extra' );
	}

	public function get_group()
	{
		return 'post';
	}

	public function get_categories()
	{
		return [TagsModule::TEXT_CATEGORY];
	}

	protected function register_controls()
	{
		$taxonomy_filter_args = [
			'show_in_nav_menus' => true,
			'object_type' => [get_post_type()],
		];

		/**
		 * Dynamic tags taxonomy args.
		 *
		 * Filters the taxonomy arguments used to retrieve the registered taxonomies
		 * displayed in the taxonomy dynamic tag.
		 *
		 * @since 2.0.0
		 *
		 * @param array $taxonomy_filter_args An array of `key => value` arguments to
		 *                                    match against the taxonomy objects inside
		 *                                    the `get_taxonomies()` function.
		 */
		// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Third-party (JLTMA) filter name
		$taxonomy_filter_args = apply_filters('jltma/dynamic_tags/post_terms/taxonomy_args', $taxonomy_filter_args);

		$taxonomies = Solace_Extra_Addons_Helper::solace_extra_get_taxonomies($taxonomy_filter_args, 'objects');

		$options = [];

		foreach ($taxonomies as $taxonomy => $object) {
			$options[$taxonomy] = $object->label;
		}

		$this->add_control(
			'taxonomy',
			[
				'label' => esc_html__('Taxonomy', 'solace-extra' ),
				'type' => Controls_Manager::SELECT,
				'options' => $options,
				'default' => 'post_tag',
			]
		);

		$this->add_control(
			'separator',
			[
				'label' => esc_html__('Separator', 'solace-extra' ),
				'type' => Controls_Manager::TEXT,
				'default' => ', ',
			]
		);
	}

	public function render()
	{
		$settings = $this->get_settings();

		$value = get_the_term_list(get_the_ID(), $settings['taxonomy'], '', $settings['separator']);

		echo wp_kses_post($value);
	}
}
