WP_View_Config_Data::apply( array $patch, int $version, string $method, string $mode ): WP_View_Config_Data

This function’s access is marked private. This means it is not intended for use by plugin or theme developers, only by core. It is listed here for completeness.

Applies a patch to the configuration, top-level key by top-level key.

Description

Shared by merge(), replace(), and set(); the three differ only in how the value of a named key is applied, which is carried by $mode:

  • merge merges the value into the current one, lists by member identity;
  • replace merges the value in the same way but swaps lists wholesale;
  • set swaps the whole value in wholesale, without merging.

In every mode a top-level null resets the key it names to its default, a nested null drops the property it names, and an omitted key is left untouched, so all three treat nulls the same way at every depth.

Parameters

$patcharrayrequired
The partial configuration to apply.
$versionintrequired
The schema version the patch was authored against.
$methodstringrequired
The public method the patch was passed to, for misuse reporting.
$modestringrequired
How to apply each named key’s value: merge, replace, or set.

Return

WP_View_Config_Data The instance, for chaining.

Source

private function apply( array $patch, int $version, $method, $mode ) {
	if ( $version <= 0 || $version > self::LATEST_VERSION ) {
		_doing_it_wrong(
			esc_html( $method ),
			esc_html__( 'A view configuration patch must declare a supported schema version.' ),
			'7.1.0'
		);

		return $this;
	}

	foreach ( $patch as $key => $value ) {
		if ( ! in_array( $key, self::CONFIG_KEYS, true ) ) {
			_doing_it_wrong(
				esc_html( $method ),
				sprintf(
					/* translators: %s: the configuration key. */
					esc_html__( '"%s" is not a documented view configuration key.' ),
					esc_html( $key )
				),
				'7.1.0'
			);
			continue;
		}

		// A null patch value makes the top-level property reset to defaults.
		if ( null === $value ) {
			$this->config[ $key ] = $this->defaults[ $key ] ?? array();
			continue;
		}

		// set() swaps the whole value in; merge()/replace() merge it into the
		// current one, differing only in how they treat lists. In every mode a
		// nested null still drops the property it names.
		$this->config[ $key ] = 'set' === $mode
			? $this->strip_nulls( $value )
			: $this->merge_properties( $this->config[ $key ] ?? array(), $value, 'replace' === $mode );
	}

	return $this;
}

Changelog

VersionDescription
7.1.0Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.