<?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 Content block.
 */
class Content extends Dynamic {

	use Singleton;

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

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

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

	/**
	 * 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' => 'entry-content'
		] ) ) {
			$content->remove_class( 'entry-content' );
		}

		$content = $content->get_updated_html();

		return $content;
	}

    /**
     * WP-Link Pages for paginated posts
     *
     * @since	unknown
     * @version 5.2.2
     *
     * @return  array
     */
    public function wp_link_pages_args( $args ) {
        return wp_parse_args( [
            'before'        => '<nav class="pagination pagination--entry mb-3">',
            'after'         => '</nav>',
            'link_before'   => '<span class="pagination__link">',
            'link_after'    => '</span>',
            'echo'          => false,
        ], $args );
    }

    /**
     * WP-Link Pages link
     *
     * @since	5.0.0
     * @version 5.2.2
     *
     * @return  string
     */
    public function wp_link_pages_link( $link ) {
        $link = str_replace( 'post-page-numbers', 'pagination__item', $link );
        $link = str_replace( 'current', 'pagination__item--current', $link );
		
        return $link;
    }

	/**
	 * Return the content for No Posts
	 *
	 * @since	3.5
	 * @version	6.4.5
	 *
	 * @return 	string
	 */
	public function the_password_form( $template ) {
		$template = wecodeart_template( 'general/protected', [], false );

		$template = trim( preg_replace( '/\s+/', ' ', $template ) );
		$template = preg_replace( '/>\s*</', '><', $template );

		return $template;
	}
}
