HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux spn-python 5.15.0-89-generic #99-Ubuntu SMP Mon Oct 30 20:42:41 UTC 2023 x86_64
User: arjun (1000)
PHP: 8.1.2-1ubuntu2.20
Disabled: NONE
Upload Files
File: /var/www/html/insiders/wp-load/wp-content/plugins/gutenmate/lib/class-gtm-block-context.php
<?php

class GTM_Block_Context {
	private static $instance = null;
	public $child_contexts   = [];

	public static function get_instance() {
		if ( null === self::$instance ) {
			self::$instance = new self();
		}

		return self::$instance;
	}

	public static function begin( $block ) {
		$registry = self::get_instance();

		$child_context = [];

		// Save the current provides context
		// Ref: https://github.com/WordPress/wordpress-develop/blob/08efcf0cb8c6239db33082db53d7646b522c5349/src/wp-includes/class-wp-block.php#L144-L150
		if ( ! empty( $block->block_type->provides_context ) ) {
			foreach ( $block->block_type->provides_context as $context_name => $attribute_name ) {
				if ( array_key_exists( $attribute_name, $block->attributes ) ) {
					$child_context[$context_name] = $block->attributes[$attribute_name];
				}
			}
		}
		// End ref

		$registry->child_contexts[] = $child_context;

		$registry->register_render_block_context();
	}

	public static function end() {
		$registry = self::get_instance();
		array_pop( $registry->child_contexts );
		$registry->unregister_render_block_context();
	}

	public static function current() {
		$registry = self::get_instance();
		return end( $registry->child_contexts );
	}

	public function register_render_block_context() {
		if ( ! has_filter( 'render_block_context', [$this, 'render_block_context'] ) ) {
			add_filter( 'render_block_context', [$this, 'render_block_context'], 10, 3 );
		}
	}

	public function unregister_render_block_context() {
		/* MY ADDED FILTER */ remove_filter( 'render_block_context', [$this, 'render_block_context'], 10, 3 );
	}

	public function render_block_context( $context, $parsed_block, $parent_block ) {
		$saved_context = self::current();

		// Merge saved context if available
		if ( ! empty( $saved_context ) ) {
			return array_merge( $saved_context, $context );
		}

		return $context;
	}
}