<?php
/**
 * Dynamic Tag: Post Content
 *
 * @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_Content
 * Outputs the current post content as an Elementor dynamic tag.
 */
class Captwiki_Tag_Post_Content extends Tag {
	/**
	 * Get tag name.
	 *
	 * @return string
	 */
	public function get_name(): string {
		return 'captwiki-tag-post-content'; }
	/**
	 * Get tag title.
	 *
	 * @return string
	 */
	public function get_title(): string {
		return esc_html__( 'Post Content', '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, Module::POST_META_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(
			'limit_words',
			array(
				'label'   => esc_html__( 'Limit Words', 'captain-widgets-kit' ),
				'type'    => \Elementor\Controls_Manager::NUMBER,
				'min'     => 1,
				'default' => '',
			)
		);
	}
	/**
	 * 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; }
		$content = apply_filters( 'the_content', $post->post_content );
		$limit   = $this->get_settings( 'limit_words' );
		if ( ! empty( $limit ) && is_numeric( $limit ) ) {
			$content = wp_trim_words( $content, $limit, '...' );
		}
		echo wp_kses_post( $content );
	}
}
