# gutenberg/17.5.0/lib/experimental/interactivity-api/class-wp-directive-context.php

Gutenberg, version 17.5.0. 83 lines.

- Page: https://pluginprobe.com/plugins/gutenberg/17.5.0/code/lib/experimental/interactivity-api/class-wp-directive-context.php
- Raw: https://pluginprobe.com/plugins/gutenberg/17.5.0/raw/lib/experimental/interactivity-api/class-wp-directive-context.php
- Modified: 2023-10-25T15:46:38+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/gutenberg/17.5.0/code/lib/experimental/interactivity-api/class-wp-directive-context.php#L10-L20`.

```php
<?php
/**
 * Context data implementation.
 *
 * @package Gutenberg
 * @subpackage Interactivity API
 */

if ( class_exists( 'WP_Directive_Context' ) ) {
	return;
}

/**
 * This is a data structure to hold the current context.
 *
 * Whenever encountering a `data-wp-context` directive, we need to update
 * the context with the data found in that directive. Conversely,
 * when "leaving" that context (by encountering a closing tag), we
 * need to reset the context to its previous state. This means that
 * we actually need sort of a stack to keep track of all nested contexts.
 *
 * Example:
 *
 * <div data-wp-context='{ "foo": 123 }'>
 *     <!-- foo should be 123 here. -->
 *     <div data-wp-context='{ "foo": 456 }'>
 *         <!-- foo should be 456 here. -->
 *     </div>
 *     <!-- foo should be reset to 123 here. -->
 * </div>
 */
class WP_Directive_Context {
	/**
	 * The stack used to store contexts internally.
	 *
	 * @var array An array of contexts.
	 */
	protected $stack = array( array() );

	/**
	 * Constructor.
	 *
	 * Accepts a context as an argument to initialize this with.
	 *
	 * @param array $context A context.
	 */
	public function __construct( $context = array() ) {
		$this->set_context( $context );
	}

	/**
	 * Return the current context.
	 *
	 * @return array The current context.
	 */
	public function get_context() {
		return end( $this->stack );
	}

	/**
	 * Set the current context.
	 *
	 * @param array $context The context to be set.
	 *
	 * @return void
	 */
	public function set_context( $context ) {
		array_push(
			$this->stack,
			array_replace_recursive( $this->get_context(), $context )
		);
	}

	/**
	 * Reset the context to its previous state.
	 *
	 * @return void
	 */
	public function rewind_context() {
		array_pop( $this->stack );
	}
}

```
