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

use Elementor\Core\DynamicTags\Data_Tag;
use Elementor\Modules\DynamicTags\Module;
if ( ! defined( 'ABSPATH' ) ) {
	exit; }
/**
 * Class Captwiki_Tag_Archive_Image
 * Returns the image for the current archive (author avatar, term thumbnail, etc.).
 */
class Captwiki_Tag_Archive_Image extends Data_Tag {
	/**
	 * Get tag name.
	 *
	 * @return string
	 */
	public function get_name(): string {
		return 'captwiki-tag-archive-image'; }
	/**
	 * Get tag title.
	 *
	 * @return string
	 */
	public function get_title(): string {
		return esc_html__( 'Archive Image', '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::IMAGE_CATEGORY, Module::MEDIA_CATEGORY ); }
	/**
	 * Register tag controls.
	 *
	 * @return void
	 */
	protected function register_controls(): void {
		$this->add_control(
			'captwiki_fallback_image',
			array(
				'label' => esc_html__( 'Fallback Image', 'captain-widgets-kit' ),
				'type'  => \Elementor\Controls_Manager::MEDIA,
			)
		);
	}
	/**
	 * Get tag value.
	 *
	 * @param array $options Function parameter.
	 * @return mixed
	 */
	public function get_value( array $options = array() ) {
		$image_id  = 0;
		$image_url = '';
		if ( is_author() ) {
			$author_id = get_queried_object_id();
			foreach ( array( 'archive_image', 'author_image', 'profile_image' ) as $key ) {
				$val = get_user_meta( $author_id, $key, true );
				if ( $val ) {
					if ( is_numeric( $val ) ) {
						$image_id  = (int) $val;
						$image_url = wp_get_attachment_image_url( $image_id, 'full' ); } else {
						$image_url = esc_url_raw( $val ); }
						break;
				}
			}
			if ( empty( $image_url ) ) {
				$image_url = get_avatar_url( $author_id ); }
		} elseif ( is_category() || is_tag() || is_tax() ) {
			$queried = get_queried_object();
			$term_id = isset( $queried->term_id ) ? $queried->term_id : 0;
			foreach ( array( 'archive_image', 'thumbnail_id', 'term_image', 'category_image' ) as $key ) {
				$value = get_term_meta( $term_id, $key, true );
				if ( $value ) {
					if ( is_numeric( $value ) ) {
						$image_id  = (int) $value;
						$image_url = wp_get_attachment_image_url( $image_id, 'full' ); } else {
						$image_url = esc_url_raw( $value ); }
						break;
				}
			}
		}
		if ( empty( $image_url ) ) {
			$fallback = $this->get_settings( 'captwiki_fallback_image' );
			if ( ! empty( $fallback['url'] ) ) {
				return array(
					'id'  => isset( $fallback['id'] ) ? $fallback['id'] : 0,
					'url' => $fallback['url'],
				); }
			$image_url = \Elementor\Utils::get_placeholder_image_src();
		}
		return array(
			'id'  => $image_id,
			'url' => $image_url,
		);
	}
	/**
	 * Render tag output.
	 *
	 * @return void
	 */
	public function render(): void {
		$value = $this->get_value();
		echo esc_url( isset( $value['url'] ) ? $value['url'] : '' );
	}
}
