<?php
/**
 * String Variables Block Filter
 *
 * Replaces string variable placeholders like [blog_page_url] in block templates.
 * Uses optimized lazy evaluation with request-level caching.
 *
 * @package Brandy\BlockSettings\StringVariables
 */

namespace Brandy\BlockSettings\StringVariables;

use Brandy\Traits\SingletonTrait;
use Brandy\Core\Services\StringVariablesService;

class Caller {
	use SingletonTrait;

	protected function __construct() {
		// Low priority (20) to run after other block filters
		add_filter( 'render_block', array( $this, 'replace_block_variables' ), 20, 2 );
	}

	/**
	 * Replace string variables in rendered block content.
	 * Uses early bail-out for performance: skips if no bracket found.
	 *
	 * @param string   $block_content Rendered block HTML
	 * @param WP_Block $block         Block instance
	 * @return string Block content with variables replaced
	 */
	public function replace_block_variables( $block_content, $block ) {
		// Early bail-out: fast check before any processing
		if ( ! StringVariablesService::might_have_variables( $block_content ) ) {
			return $block_content;
		}

		return StringVariablesService::replace_variables( $block_content );
	}
}

Caller::get_instance();
