<?php
/**
 * Dynamic Tag: Post Date
 *
 * @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_Date
 * Outputs the current post publish or modified date with format options.
 */
class Captwiki_Tag_Post_Date extends Tag {
	/**
	 * Get tag name.
	 *
	 * @return string
	 */
	public function get_name(): string {
		return 'captwiki-tag-post-date'; }
	/**
	 * Get tag title.
	 *
	 * @return string
	 */
	public function get_title(): string {
		return esc_html__( 'Post Date', '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_date_type',
			array(
				'label'   => esc_html__( 'Date Type', 'captain-widgets-kit' ),
				'type'    => \Elementor\Controls_Manager::SELECT,
				'options' => array(
					'post_date'     => esc_html__( 'Publish Date', 'captain-widgets-kit' ),
					'post_modified' => esc_html__( 'Modified Date', 'captain-widgets-kit' ),
				),
				'default' => 'post_date',
			)
		);
		$this->add_control(
			'captwiki_format_type',
			array(
				'label'   => esc_html__( 'Format', 'captain-widgets-kit' ),
				'type'    => \Elementor\Controls_Manager::SELECT,
				'options' => array(
					'default' => esc_html__( 'Default (WP Format)', 'captain-widgets-kit' ),
					'F j, Y'  => date_i18n( 'F j, Y' ),
					'Y-m-d'   => date_i18n( 'Y-m-d' ),
					'm/d/Y'   => date_i18n( 'm/d/Y' ),
					'd/m/Y'   => date_i18n( 'd/m/Y' ),
					'human'   => esc_html__( 'Human Readable', 'captain-widgets-kit' ),
					'custom'  => esc_html__( 'Custom', 'captain-widgets-kit' ),
				),
				'default' => 'default',
			)
		);
		$this->add_control(
			'captwiki_custom_format',
			array(
				'label'     => esc_html__( 'Custom Format', 'captain-widgets-kit' ),
				'type'      => \Elementor\Controls_Manager::TEXT,
				'default'   => 'F j, Y',
				'condition' => array( 'captwiki_format_type' => 'custom' ),
			)
		);
	}
	/**
	 * Render tag output.
	 *
	 * @return void
	 */
	public function render(): void {
		$post_id = get_the_ID();
		if ( ! $post_id ) {
			return; }
		$post = get_post( $post_id );
		if ( ! $post ) {
			return; }
		$settings  = $this->get_settings();
		$date      = 'post_modified' === $settings['captwiki_date_type'] ? $post->post_modified : $post->post_date;
		$timestamp = strtotime( $date );
		if ( 'human' === $settings['captwiki_format_type'] ) {
			$value = human_time_diff( $timestamp, time() ) . ' ' . esc_html__( 'ago', 'captain-widgets-kit' );
		} elseif ( 'default' === $settings['captwiki_format_type'] ) {
			$value = date_i18n( get_option( 'date_format' ), $timestamp );
		} elseif ( 'custom' === $settings['captwiki_format_type'] ) {
			$value = date_i18n( $settings['captwiki_custom_format'], $timestamp );
		} else {
			$value = date_i18n( $settings['captwiki_format_type'], $timestamp );
		}
		echo esc_html( $value );
	}
}
