<?php
/**
 * Dynamic Tag: Post Featured Image Data (Text)
 *
 * @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_Post_Featured_Image_Data
 * Outputs metadata (alt, title, caption, description, ID) from the featured image.
 */
class Captwiki_Tag_Post_Featured_Image_Data extends Tag {
	/**
	 * Get tag name.
	 *
	 * @return string
	 */
	public function get_name(): string {
		return 'captwiki-tag-post-featured-image-data'; }
	/**
	 * Get tag title.
	 *
	 * @return string
	 */
	public function get_title(): string {
		return esc_html__( 'Post Featured Image Data', 'captain-widgets-kit' ); }
	/**
	 * Get tag group.
	 *
	 * @return array
	 */
	public function get_group(): array {
		return array( 'captwiki-post' ); }
	/**
	 * 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_data_type',
			array(
				'label'   => esc_html__( 'Data Type', 'captain-widgets-kit' ),
				'type'    => \Elementor\Controls_Manager::SELECT,
				'options' => array(
					'id'          => esc_html__( 'ID', 'captain-widgets-kit' ),
					'alt'         => esc_html__( 'Alt Text', 'captain-widgets-kit' ),
					'title'       => esc_html__( 'Title', 'captain-widgets-kit' ),
					'caption'     => esc_html__( 'Caption', 'captain-widgets-kit' ),
					'description' => esc_html__( 'Description', 'captain-widgets-kit' ),
				),
				'default' => 'alt',
			)
		);
	}
	/**
	 * Render tag output.
	 *
	 * @return void
	 */
	public function render(): void {
		$post_id = get_the_ID();
		if ( ! $post_id ) {
			return; }
		$image_id = get_post_thumbnail_id( $post_id );
		if ( ! $image_id ) {
			return; }
		$data_type = $this->get_settings( 'captwiki_data_type' );
		$data_type = $data_type ? $data_type : 'alt';
		switch ( $data_type ) {
			case 'id':
				echo esc_html( $image_id );
				break;
			case 'alt':
				echo esc_html( get_post_meta( $image_id, '_wp_attachment_image_alt', true ) );
				break;
			case 'title':
				echo esc_html( get_the_title( $image_id ) );
				break;
			case 'caption':
				echo esc_html( wp_get_attachment_caption( $image_id ) );
				break;
			case 'description':
				$a = get_post( $image_id );
				echo esc_html( $a ? $a->post_content : '' );
				break;
		}
	}
}
