<?php
/**
 * Dynamic Tag: Archive Meta
 *
 * @package captain-widgets-kit
 * @since   1.1.0
 */

use Elementor\Core\DynamicTags\Tag;
use Elementor\Modules\DynamicTags\Module;
if ( ! defined( 'ABSPATH' ) ) {
	exit; }
/**
 * Class Captwiki_Tag_Archive_Meta
 * Outputs a term or author meta value for the current archive page.
 */
class Captwiki_Tag_Archive_Meta extends Tag {
	/**
	 * Get tag name.
	 *
	 * @return string
	 */
	public function get_name(): string {
		return 'captwiki-tag-archive-meta'; }
	/**
	 * Get tag title.
	 *
	 * @return string
	 */
	public function get_title(): string {
		return esc_html__( 'Archive Meta', 'captain-widgets-kit' ); }
	/**
	 * Get tag group.
	 *
	 * @return array
	 */
	public function get_group(): array {
		return array( 'captwiki-archive' ); }
	/**
	 * Get tag categories.
	 *
	 * @return array
	 */
	public function get_categories(): array {
		return array( Module::TEXT_CATEGORY ); }
	/**
	 * Check whether settings are required.
	 *
	 * @return bool
	 */
	public function is_settings_required() {
		return true; }
	/**
	 * Handle register advanced section.
	 *
	 * @return void
	 */
	protected function register_advanced_section(): void {}
	/**
	 * Register tag controls.
	 *
	 * @return void
	 */
	protected function register_controls(): void {
		$this->add_control(
			'captwiki_meta_key',
			array(
				'label'       => esc_html__( 'Meta Key', 'captain-widgets-kit' ),
				'type'        => \Elementor\Controls_Manager::TEXT,
				'placeholder' => esc_html__( 'Enter meta key', 'captain-widgets-kit' ),
				'ai'          => array( 'active' => false ),
			)
		);
	}
	/**
	 * Render tag output.
	 *
	 * @return void
	 */
	public function render(): void {
		$key = $this->get_settings( 'captwiki_meta_key' );
		if ( empty( $key ) ) {
			return; }
		$meta_value = '';
		if ( is_category() || is_tag() || is_tax() ) {
			$meta_value = get_term_meta( get_queried_object_id(), $key, true );
		} elseif ( is_author() ) {
			$meta_value = get_user_meta( get_queried_object_id(), $key, true );
		}
		if ( is_array( $meta_value ) ) {
			$meta_value = implode( ', ', $meta_value ); }
		echo wp_kses_post( $meta_value );
	}
}
