<?php
/**
 * Dynamic Tag: ACF Field (Text)
 *
 * @package captain-widgets-kit
 * @since   1.1.0
 */

use Elementor\Core\DynamicTags\Tag;
use Elementor\Modules\DynamicTags\Module;
use Elementor\Controls_Manager;
if ( ! defined( 'ABSPATH' ) ) {
	exit; }
/**
 * Class Captwiki_Tag_ACF_Text
 * Outputs a selected ACF text/textarea/select field value as an Elementor dynamic tag.
 */
class Captwiki_Tag_ACF_Text extends Tag {
	/**
	 * Get tag name.
	 *
	 * @return string
	 */
	public function get_name(): string {
		return 'captwiki-tag-acf-text'; }
	/**
	 * Get tag title.
	 *
	 * @return string
	 */
	public function get_title(): string {
		return esc_html__( 'ACF Field', 'captain-widgets-kit' ); }
	/**
	 * Get tag group.
	 *
	 * @return array
	 */
	public function get_group(): array {
		return array( 'captwiki-acf' ); }
	/**
	 * 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(): bool {
		return true; }

	/**
	 * Register tag controls.
	 *
	 * @return void
	 */
	protected function register_controls(): void {
		$this->add_control(
			'captwiki_acf_source',
			array(
				'label'   => esc_html__( 'Source', 'captain-widgets-kit' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'post',
				'options' => array(
					'post'   => esc_html__( 'Post', 'captain-widgets-kit' ),
					'author' => esc_html__( 'Post Author', 'captain-widgets-kit' ),
				),
			)
		);
		$this->add_control(
			'captwiki_acf_field_key',
			array(
				'label'  => esc_html__( 'Key', 'captain-widgets-kit' ),
				'type'   => Controls_Manager::SELECT,
				'groups' => $this->captwiki_get_acf_text_groups(),
			)
		);
	}

	/**
	 * Build grouped ACF field options for text-compatible field types.
	 *
	 * @since  1.1.0
	 * @return array<int, array<string, mixed>>
	 */
	protected function captwiki_get_acf_text_groups(): array {
		if ( ! function_exists( 'acf_get_field_groups' ) ) {
			return array(); }
		$text_types = array( 'text', 'textarea', 'number', 'email', 'url', 'password', 'wysiwyg', 'select', 'checkbox', 'radio', 'true_false', 'date_picker', 'date_time_picker', 'time_picker', 'color_picker' );
		$groups     = array();
		foreach ( acf_get_field_groups() as $group ) {
			if ( empty( $group['key'] ) || empty( $group['title'] ) ) {
				continue; }
			$fields = acf_get_fields( $group['key'] );
			if ( empty( $fields ) ) {
				continue; }
			$options = array();
			foreach ( $fields as $f ) {
				if ( empty( $f['name'] ) || empty( $f['label'] ) || ! in_array( $f['type'], $text_types, true ) ) {
					continue; }
				$options[ $f['name'] ] = $f['label'];
			}
			if ( ! empty( $options ) ) {
				$groups[] = array(
					'label'   => esc_html( $group['title'] ),
					'options' => $options,
				); }
		}
		return $groups;
	}

	/**
	 * Render tag output.
	 *
	 * @return void
	 */
	public function render(): void {
		$settings   = $this->get_settings_for_display();
		$field_name = isset( $settings['captwiki_acf_field_key'] ) ? $settings['captwiki_acf_field_key'] : '';
		$source     = isset( $settings['captwiki_acf_source'] ) ? $settings['captwiki_acf_source'] : 'post';
		if ( empty( $field_name ) || ! function_exists( 'get_field' ) ) {
			return; }
		$value = '';
		if ( 'author' === $source ) {
			$author_id = get_the_author_meta( 'ID' );
			if ( $author_id ) {
				$value = get_field( $field_name, 'user_' . $author_id ); }
		} elseif ( is_tax() || is_category() || is_tag() ) {
			$value = get_field( $field_name, get_queried_object() );
		} else {
			$value = get_field( $field_name, get_the_ID() );
		}
		if ( empty( $value ) ) {
			return; }
		if ( is_array( $value ) ) {
			$value = implode( ', ', array_map( 'wp_kses_post', $value ) ); } elseif ( is_object( $value ) ) {
			return; }
			echo wp_kses_post( (string) $value );
	}
}
