<?php
/**
 * Dynamic Tag: User Info
 *
 * @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_Info
 * Outputs current user field (name, email, role, etc.) as a dynamic tag.
 */
class Captwiki_Tag_User_Info extends Tag {
	/**
	 * Get tag name.
	 *
	 * @return string
	 */
	public function get_name(): string {
		return 'captwiki-tag-user-info'; }
	/**
	 * Get tag title.
	 *
	 * @return string
	 */
	public function get_title(): string {
		return esc_html__( 'User Info', '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_user_field',
			array(
				'label'   => esc_html__( 'User Field', 'captain-widgets-kit' ),
				'type'    => \Elementor\Controls_Manager::SELECT,
				'default' => 'display_name',
				'options' => array(
					'id'           => esc_html__( 'ID', 'captain-widgets-kit' ),
					'name'         => esc_html__( 'Name', 'captain-widgets-kit' ),
					'email'        => esc_html__( 'Email', 'captain-widgets-kit' ),
					'website'      => esc_html__( 'Website', 'captain-widgets-kit' ),
					'display_name' => esc_html__( 'Display Name', 'captain-widgets-kit' ),
					'nickname'     => esc_html__( 'Nickname', 'captain-widgets-kit' ),
					'first_name'   => esc_html__( 'First Name', 'captain-widgets-kit' ),
					'last_name'    => esc_html__( 'Last Name', 'captain-widgets-kit' ),
					'description'  => esc_html__( 'Description', 'captain-widgets-kit' ),
					'role'         => esc_html__( 'Role', 'captain-widgets-kit' ),
				),
			)
		);
	}
	/**
	 * Resolve the relevant user ID from context.
	 *
	 * @since  1.1.0
	 * @return int|false
	 */
	protected function captwiki_resolve_user_id() {
		if ( ! is_user_logged_in() ) {
			return false; }
		if ( is_author() ) {
			$id = get_queried_object_id();
			if ( $id ) {
				return (int) $id; }
		}
		if ( is_singular() ) {
			$pid = get_the_ID();
			if ( $pid ) {
				$aid = get_post_field( 'post_author', $pid );
				if ( $aid ) {
					return (int) $aid; }
			}
		}
		$qo = get_queried_object();
		if ( $qo ) {
			if ( isset( $qo->ID ) && user_can( $qo->ID, 'read' ) ) {
				return (int) $qo->ID; }
			if ( isset( $qo->user_id ) && $qo->user_id ) {
				return (int) $qo->user_id; }
		}
		$current_user_id = (int) get_current_user_id();
		return $current_user_id ? $current_user_id : false;
	}
	/**
	 * Render tag output.
	 *
	 * @return void
	 */
	public function render(): void {
		$field   = $this->get_settings( 'captwiki_user_field' );
		$user_id = $this->captwiki_resolve_user_id();
		if ( ! $user_id ) {
			return; }
		$user  = get_userdata( $user_id );
		$value = '';
		switch ( $field ) {
			case 'id':
				$value = $user_id;
				break;
			case 'name':
				$value = $user ? ( ! empty( $user->display_name ) ? $user->display_name : $user->user_login ) : '';
				break;
			case 'email':
				$value = $user ? $user->user_email : '';
				break;
			case 'website':
				$value = $user ? $user->user_url : '';
				break;
			case 'display_name':
				$value = $user ? $user->display_name : '';
				break;
			case 'nickname':
				$value = $user ? ( ! empty( $user->nickname ) ? $user->nickname : $user->display_name ) : '';
				break;
			case 'first_name':
				$value = $user ? $user->first_name : '';
				break;
			case 'last_name':
				$value = $user ? $user->last_name : '';
				break;
			case 'description':
				$value = $user ? $user->description : '';
				break;
			case 'role':
				$value = $user && ! empty( $user->roles ) ? implode( ', ', $user->roles ) : '';
				break;
		}
		if ( is_scalar( $value ) && '' !== $value ) {
			echo esc_html( $value ); }
	}
}
