# blocks/trunk/src/Variables/VariablesEverywhere.php

Blocks – Reusable Content, Shortcodes &amp; Site Variables, version trunk. 91 lines.

- Page: https://pluginprobe.com/plugins/blocks/trunk/code/src/Variables/VariablesEverywhere.php
- Raw: https://pluginprobe.com/plugins/blocks/trunk/raw/src/Variables/VariablesEverywhere.php
- Modified: 2026-07-13T20:34:48+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/blocks/trunk/code/src/Variables/VariablesEverywhere.php#L10-L20`.

```php
<?php

declare(strict_types=1);

namespace RenzoJohnson\Blocks\Variables;

\defined( 'ABSPATH' ) || exit;

final class VariablesEverywhere {

	public const OPTION_NAME = 'blocks_variables_everywhere';

	private bool $buffer_started = false;

	public function __construct(
		private readonly VariableResolver $resolver,
		private readonly \Blocks_Settings_Repository $settings,
	) {
	}

	public function register_hooks(): void {
		\add_filter( 'the_content', $this->filter_content( ... ), 12 );
		\add_filter( 'widget_text', $this->filter_content( ... ), 12 );
		\add_filter( 'widget_block_content', $this->filter_content( ... ), 12 );
		\add_filter( 'bricks/frontend/render_data', $this->filter_content( ... ), 12 );
		\add_filter( 'breakdance_singular_content', $this->filter_content( ... ), 12 );
		\add_action( 'template_redirect', $this->start_output_pass( ... ), 2 );
	}

	public function enabled(): bool {
		return $this->settings->get_bool( self::OPTION_NAME );
	}

	public function filter_content( mixed $content ): mixed {
		if ( ! \is_string( $content ) || '' === $content || ! $this->enabled() ) {
			return $content;
		}

		return $this->resolver->replace_html( $content );
	}

	public function start_output_pass(): void {
		if ( ! $this->should_start_output_pass() ) {
			return;
		}

		$this->buffer_started = true;
		\ob_start( $this->filter_output( ... ) );
	}

	public function filter_output( string $html ): string {
		foreach ( \headers_list() as $header ) {
			if ( 0 !== \stripos( $header, 'Content-Type:' ) ) {
				continue;
			}

			if ( false === \stripos( $header, 'text/html' ) && false === \stripos( $header, 'application/xhtml+xml' ) ) {
				return $html;
			}
		}

		return $this->resolver->replace_html( $html );
	}

	private function should_start_output_pass(): bool {
		if ( $this->buffer_started || ! $this->enabled() ) {
			return false;
		}

		if (
			\is_admin()
			|| \is_feed()
			|| \is_robots()
			|| \is_trackback()
			|| \wp_doing_ajax()
			|| ( \defined( 'REST_REQUEST' ) && REST_REQUEST )
			|| ( \defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST )
		) {
			return false;
		}

		$status = \http_response_code();

		if ( \is_int( $status ) && ( 204 === $status || 304 === $status ) ) {
			return false;
		}

		return (bool) \apply_filters( 'blocks_variables_output_buffer_enabled', true );
	}
}

```
