<?php
/**
 * FeaturedImage.
 *
 * @package MadeByAura\Prime\Plugins\Elementor\DynamicTags
 * @author  MadeByAura.com
 */

namespace MadeByAura\Prime\Plugins\Elementor\DynamicTags;

use Elementor\Core\DynamicTags\Data_Tag;
use Elementor\Modules\DynamicTags\Module;

defined( 'ABSPATH' ) || die();

/**
 * FeaturedImage.
 */
class FeaturedImage extends Data_Tag {
	/**
	 * Get the name of the tag
	 */
	public function get_name() {
		return 'aura-theme-featured-image';
	}

	/**
	 * Get the title of the Tag.
	 */
	public function get_title() {
		return __( 'Featured Image', 'aura-prime' );
	}

	/**
	 * Get the group of the tag.
	 */
	public function get_group() {
		return 'aura-theme';
	}

	/**
	 * Get an array of tag categories.
	 */
	public function get_categories() {
		return [ Module::IMAGE_CATEGORY ];
	}

	/**
	 * Register tag controls.
	 */
	protected function register_controls() {}

	/**
	 * Get value of the tag.
	 *
	 * @param array $options  Options.
	 * @return array
	 */
	public function get_value( array $options = [] ) {
		// Initialize variable.
		$post_id = null;

		// Change $post_id to the id of the posts page set in Dashboard > Reading,
		// IF the current page is Post Archive, Post Single, Category Archive,
		// OR Tag Archive.
		if (
			is_home() ||
			is_singular( 'post' ) ||
			is_category() ||
			is_tag()
		) {
			$blog_page_id = get_option( 'page_for_posts' );

			if ( 'page' === get_option( 'show_on_front' ) && $blog_page_id ) {
				$post_id = $blog_page_id;
			}
		}

		// Get image id.
		$image_id = get_post_thumbnail_id( $post_id );

		return [
			'id'  => $image_id,
			'url' => wp_get_attachment_image_url( $image_id, 'full' ),
		];
	}
}
