Tribe__Context::dangerously_set_global_context( array|null $fields = null, bool $whitelist = true )

Modifies the global context using the defined write locations to persist the altered values.

Please keep in mind this will set the the global context for the whole request and, when the write location is an option, to the database. With great power comes great responsibility: think a lot before using this.


Parameters

$fields

(array|null) (Optional) An optional whitelist or blacklist of fields to write depending on the value of the $whitelist parameter; defaults to writing all available fields.

Default value: null

$whitelist

(bool) (Optional) Whether the list of fields provided in the $fields parameter should be treated as a whitelist (true) or blacklist (false).

Default value: true


Top ↑

Source

File: src/Tribe/Context.php

	public function dangerously_set_global_context( array $fields = null, $whitelist = true ) {
		$locations = $this->get_locations();

		if ( null !== $fields ) {
			$locations = $whitelist
				? array_intersect_key( $locations, array_combine( $fields, $fields ) )
				: array_diff_key( $locations, array_combine( $fields, $fields ) );
		}

		/**
		 * Here we intersect with the request cache to only write values we've actually read
		 * or modified. If none of the two happened then there's no need to write anything.
		 */
		foreach ( array_intersect_key( $this->request_cache, $locations ) as $key => $value ) {
			if ( ! isset( $locations[ $key ]['write'] ) ) {
				continue;
			}

			foreach ( (array) $locations[ $key ]['write'] as $location => $targets ) {
				$targets    = (array) $targets;
				$write_func = 'write_' . $location;

				foreach ( $targets as $arg_1 => $arg_2 ) {
					if ( self::FUNC === $location && is_array( $arg_2 ) && is_callable( $arg_2 ) ) {
						// Handles write functions specified as an array.
						$location_args = array( $arg_2 );
					} else {
						$location_args = in_array( $location, self::$associative_locations, true )
							? array( $arg_1, $arg_2 )
							: (array) $arg_2;
					}

					$args = array_merge( $location_args, array( $value ) );

					call_user_func_array( array( $this, $write_func ), $args );
				}
			}
		}
	}

Top ↑

Changelog

Changelog
Version Description
4.9.5 Introduced.