<?php
/**
 * WeCodeArt Framework
 *
 * WARNING: This file is part of the core WeCodeArt Framework. DO NOT edit this file under any circumstances.
 * Please do all modifications in the form of a child theme.
 *
 * @package		WeCodeArt Framework
 * @subpackage  Gutenberg\Blocks
 * @copyright   Copyright (c) 2025, WeCodeArt Framework
 * @since		5.0.0
 * @version		6.6.0
 */

namespace WeCodeArt\Gutenberg\Blocks\Post;

defined( 'ABSPATH' ) || exit;

use WeCodeArt\Singleton;
use WeCodeArt\Gutenberg\Blocks\Dynamic;
use function WeCodeArt\Functions\get_prop;

/**
 * Gutenberg Post Excerpt block.
 */
class Excerpt extends Dynamic {

	use Singleton;

	/**
	 * Block namespace.
	 *
	 * @var string
	 */
	protected $namespace = 'core';

	/**
	 * Block name.
	 *
	 * @var string
	 */
	protected $block_name = 'post-excerpt';

	/**
	 * Init.
	 */
	public function init() {
		\add_filter( 'render_block_' . $this->get_block_type(), [ $this, 'render' ], 20, 1 );
		\add_filter( 'excerpt_length', [ $this, 'filter_length' ] );
	}

	/**
	 * Dynamically renders the `core/post-content` block.
	 *
	 * @param 	string 	$content 	The block markup.
	 *
	 * @return 	string 	The block markup.
	 */
	public function render( string $content = '' ): string {
		$content = wecodeart( 'dom' )::processor( $content );
			
		if( $content->next_tag( [
			'class_name' => 'wp-block-post-excerpt__excerpt'
		] ) ) {
			$content->remove_class( 'wp-block-post-excerpt__excerpt' );
			$content->add_class( 'wp-block-post-excerpt__text' );
		}
		
		if( $content->next_tag( [
			'class_name' => 'wp-block-post-excerpt__more-text'
		] ) ) {
			$content->remove_class( 'wp-block-post-excerpt__more-text' );
			$content->add_class( 'wp-block-post-excerpt__more' );
		}

		$content = $content->get_updated_html();

		return $content;
	}

	/**
	 * Filter excerpt length.
	 *
	 * @param 	int 	$length The excerpt length.
	 *
	 * @return 	int
	 */
	public function filter_length( $length ) {
		$length = wecodeart_config( 'excerpt', 20 );

		return $length;
	}
}
