wppaste
WordPress

WP_Theme_JSON::merge( WP_Theme_JSON $incoming )

Since
5.8.0, 5.9.0, 6.7.0
Source
wp-includes/class-wp-theme-json.php:3166
Merges new incoming data.

Compatibility

WordPress
since 6.7.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$incomingWP_Theme_JSON
Data to merge.

Performance profile

How much work a call to WP_Theme_JSON::merge() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Light

Touches nothing outside its own arguments.

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
41–44

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 260.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
0

Nothing in core calls this; the cost is only what you spend yourself.

What one call costs · 1 distinct outcome

One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_Theme_JSON::merge() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always41–44->get_raw_data(), array_replace_recursive(), ::get_setting_nodes(), ::get_default_slugs(), ::get_block_nodes()

Across PHP versions

Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 260 instructions, 41–44 executed per call, 25 branches. The work does not change between versions.

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Uses · 9

  • _wp_array_get()Accesses an array in depth based on a path of keys.
  • _wp_array_set()Sets an array in depth based on a path of keys.
  • static::compute_spacing_sizes()
  • static::merge_spacing_sizes()
  • static::get_setting_nodes()
  • static::get_default_slugs()
  • static::get_name_from_defaults()
  • static::filter_slugs()
  • static::get_block_nodes()

Source code

	public function merge( $incoming ) {		$incoming_data    = $incoming->get_raw_data();		$this->theme_json = array_replace_recursive( $this->theme_json, $incoming_data ); 		/*		 * Recompute all the spacing sizes based on the new hierarchy of data. In the constructor		 * spacingScale and spacingSizes are both keyed by origin and VALID_ORIGINS is ordered, so		 * we can allow partial spacingScale data to inherit missing data from earlier layers when		 * computing the spacing sizes.		 *		 * This happens before the presets are merged to ensure that default spacing sizes can be		 * removed from the theme origin if $prevent_override is true.		 */		$flattened_spacing_scale = array();		foreach ( static::VALID_ORIGINS as $origin ) {			$scale_path = array( 'settings', 'spacing', 'spacingScale', $origin ); 			// Apply the base spacing scale to the current layer.			$base_spacing_scale      = _wp_array_get( $this->theme_json, $scale_path, array() );			$flattened_spacing_scale = array_replace( $flattened_spacing_scale, $base_spacing_scale ); 			$spacing_scale = _wp_array_get( $incoming_data, $scale_path, null );			if ( ! isset( $spacing_scale ) ) {				continue;			} 			// Allow partial scale settings by merging with lower layers.			$flattened_spacing_scale = array_replace( $flattened_spacing_scale, $spacing_scale ); 			// Generate and merge the scales for this layer.			$sizes_path           = array( 'settings', 'spacing', 'spacingSizes', $origin );			$spacing_sizes        = _wp_array_get( $incoming_data, $sizes_path, array() );			$spacing_scale_sizes  = static::compute_spacing_sizes( $flattened_spacing_scale );			$merged_spacing_sizes = static::merge_spacing_sizes( $spacing_scale_sizes, $spacing_sizes ); 			_wp_array_set( $incoming_data, $sizes_path, $merged_spacing_sizes );		} 		/*		 * The array_replace_recursive algorithm merges at the leaf level,		 * but we don't want leaf arrays to be merged, so we overwrite it.		 *		 * For leaf values that are sequential arrays it will use the numeric indexes for replacement.		 * We rather replace the existing with the incoming value, if it exists.		 * This is the case of spacing.units.		 *		 * For leaf values that are associative arrays it will merge them as expected.		 * This is also not the behavior we want for the current associative arrays (presets).		 * We rather replace the existing with the incoming value, if it exists.		 * This happens, for example, when we merge data from theme.json upon existing		 * theme supports or when we merge anything coming from the same source twice.		 * This is the case of color.palette, color.gradients, color.duotone,		 * typography.fontSizes, or typography.fontFamilies.		 *		 * Additionally, for some preset types, we also want to make sure the		 * values they introduce don't conflict with default values. We do so		 * by checking the incoming slugs for theme presets and compare them		 * with the equivalent default presets: if a slug is present as a default		 * we remove it from the theme presets.		 */		$nodes        = static::get_setting_nodes( $incoming_data );		$slugs_global = static::get_default_slugs( $this->theme_json, array( 'settings' ) );		foreach ( $nodes as $node ) {			// Replace the spacing.units.			$path   = $node['path'];			$path[] = 'spacing';			$path[] = 'units'; 			$content = _wp_array_get( $incoming_data, $path, null );			if ( isset( $content ) ) {				_wp_array_set( $this->theme_json, $path, $content );			} 			// Replace the presets.			foreach ( static::PRESETS_METADATA as $preset_metadata ) {				$prevent_override = $preset_metadata['prevent_override'];				if ( is_array( $prevent_override ) ) {					$prevent_override = _wp_array_get( $this->theme_json['settings'], $preset_metadata['prevent_override'] );				}

Changelog

Introduced in 5.8.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

6.7.0
Replace background image objects during merge.from the docblock
5.9.0
Duotone preset also has origins.from the docblock
5.8.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 6.8.8 tag, from src/wp-includes/class-wp-theme-json.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.