<?php
/**
 * Dynamic Tag: Post Author Avatar (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_Post_Author_Avatar
 * Returns the post author's Gravatar as an image array.
 */
class Captwiki_Tag_Post_Author_Avatar extends Data_Tag {
	/**
	 * Get tag name.
	 *
	 * @return string
	 */
	public function get_name(): string {
		return 'captwiki-tag-post-author-avatar'; }
	/**
	 * Get tag title.
	 *
	 * @return string
	 */
	public function get_title(): string {
		return esc_html__( 'Post Author Avatar', '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::IMAGE_CATEGORY, Module::MEDIA_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_avatar_size',
			array(
				'label'   => esc_html__( 'Size (px)', 'captain-widgets-kit' ),
				'type'    => \Elementor\Controls_Manager::NUMBER,
				'default' => 96,
				'min'     => 16,
				'max'     => 512,
			)
		);
	}
	/**
	 * Handle register advanced section.
	 *
	 * @return mixed
	 */
	protected function register_advanced_section() {}
	/**
	 * Get tag value.
	 *
	 * @param array $options Function parameter.
	 * @return mixed
	 */
	public function get_value( array $options = array() ) {
		$post_id = get_the_ID();
		if ( ! $post_id ) {
			return false; }
		$author_id = get_post_field( 'post_author', $post_id );
		if ( ! $author_id ) {
			return false; }
		$size = $this->get_settings( 'captwiki_avatar_size' );
		$size = $size ? $size : 96;
		$url  = get_avatar_url( $author_id, array( 'size' => $size ) );
		return $url ? array(
			'id'  => '',
			'url' => $url,
		) : false;
	}
	/**
	 * Render tag output.
	 *
	 * @return void
	 */
	public function render(): void {
		$value = $this->get_value();
		if ( ! empty( $value['url'] ) ) {
			echo esc_url( $value['url'] ); }
	}
}
