<?php
/**
 * Dynamic Tag: User 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_User_Meta
 * Outputs a user meta value by key for the current logged-in user.
 */
class Captwiki_Tag_User_Meta extends Tag {
	/**
	 * Get tag name.
	 *
	 * @return string
	 */
	public function get_name(): string {
		return 'captwiki-tag-user-meta'; }
	/**
	 * Get tag title.
	 *
	 * @return string
	 */
	public function get_title(): string {
		return esc_html__( 'User Meta', 'captain-widgets-kit' ); }
	/**
	 * Get tag group.
	 *
	 * @return array
	 */
	public function get_group(): array {
		return array( 'captwiki-user' ); }
	/**
	 * 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; }
	/**
	 * 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 user meta key', 'captain-widgets-kit' ),
				'default'     => 'nickname',
				'ai'          => array( 'active' => false ),
			)
		);
	}
	/**
	 * Render tag output.
	 *
	 * @return void
	 */
	public function render(): void {
		$user_id = get_current_user_id();
		if ( ! $user_id ) {
			return; }
		$meta_key = $this->get_settings( 'captwiki_meta_key' );
		if ( empty( $meta_key ) ) {
			return; }
		$value = get_user_meta( $user_id, $meta_key, true );
		if ( is_array( $value ) ) {
			$value = implode( ', ', $value ); }
		echo esc_html( $value );
	}
}
