Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Theme Export: Add missing properties to theme.json #39590

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/compat/wordpress-5.9/class-wp-theme-json-5-9.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*
* @access private
*/
class WP_Theme_JSON_5_9 {
class WP_Theme_JSON_5_9 extends WP_Theme_JSON {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is necessary as the merge function inherits some functions from the original base class.


/**
* Container of data in theme.json format.
Expand Down
14 changes: 11 additions & 3 deletions lib/compat/wordpress-6.0/block-template-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,20 @@ function gutenberg_generate_block_templates_export_file() {
}

// Load theme.json into the zip file.
$tree = WP_Theme_JSON_Resolver_Gutenberg::get_theme_data();
$tree->merge( WP_Theme_JSON_Resolver_Gutenberg::get_user_data() );
$user_data = WP_Theme_JSON_Resolver_Gutenberg::get_user_data();
// Update settings that aren't present in the $tree.
if ( file_exists( $theme_path . '/theme.json' ) ) {
$theme_json_data = wp_json_file_decode( $theme_path . '/theme.json', array( 'associative' => true ) );
if ( ! empty( $theme_json_data ) ) {
$merged_data = WP_Theme_JSON_Gutenberg::functional_merge( $theme_json_data, $user_data->get_raw_data() );
}
} else {
$merged_data = $user_data;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be super if the complexity of merging this data together into a snapshot JSON string were something abstracted in WP_Theme_JSON_Gutenberg so that getting that could be a function call instead of repeating this logic.

}

$zip->addFromString(
$theme_name . '/theme.json',
wp_json_encode( $tree->get_data(), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE )
wp_json_encode( $merged_data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE )
);

// Save changes to the zip file.
Expand Down
100 changes: 100 additions & 0 deletions lib/compat/wordpress-6.0/class-wp-theme-json-gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,104 @@ public function get_data() {
return $flattened_theme_json;
}

/**
* Merge new incoming data.
*
* @since 5.8.0
* @since 5.9.0 Duotone preset also has origins.
* @since 6.0.0 Calls functional_merge internally
*
* @param WP_Theme_JSON $incoming Data to merge.
*/
public function merge( $incoming ) {
$this->theme_json = static::functional_merge( $this->theme_json, $incoming->get_raw_data() );
}

/**
* Merge two theme.json data structures
*
* @since 6.0.0
*
* @param array $base Data to merge onto.
* @param array $replacements Data to add onto $base
* @return array The result of the merge
*/
public static function functional_merge( $base, $replacements ) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea of this function is that it will work with any 2 arrays and do the same thing, so we can use the same logic without being in context of an object.

$merged_data = array_replace_recursive( $base, $replacements );

/*
* 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( $replacements );
$slugs_global = static::get_default_slugs( $base, array( 'settings' ) );
foreach ( $nodes as $node ) {
$slugs_node = static::get_default_slugs( $base, $node['path'] );
$slugs = array_merge_recursive( $slugs_global, $slugs_node );

// Replace the spacing.units.
$path = array_merge( $node['path'], array( 'spacing', 'units' ) );
$content = _wp_array_get( $replacements, $path, null );
if ( isset( $content ) ) {
_wp_array_set( $merged_data, $path, $content );
}

// Replace the presets.
foreach ( static::PRESETS_METADATA as $preset ) {
$override_preset = ! static::get_metadata_boolean( $base['settings'], $preset['prevent_override'], true );

foreach ( static::VALID_ORIGINS as $origin ) {
$base_path = array_merge( $node['path'], $preset['path'] );
$path = array_merge( $base_path, array( $origin ) );
$content = _wp_array_get( $replacements, $path, null );
if ( ! isset( $content ) ) {
continue;
}

if ( 'theme' === $origin && $preset['use_default_names'] ) {
foreach ( $content as &$item ) {
if ( ! array_key_exists( 'name', $item ) ) {
$name = static::get_name_from_defaults( $item['slug'], $base_path );
if ( null !== $name ) {
$item['name'] = $name;
}
}
}
}

if (
( 'theme' !== $origin ) ||
( 'theme' === $origin && $override_preset )
) {
_wp_array_set( $base, $path, $content );
} else {
$slugs_for_preset = _wp_array_get( $slugs, $preset['path'], array() );
$content = static::filter_slugs( $content, $slugs_for_preset );
_wp_array_set( $base, $path, $content );
}
}
}
}

return $merged_data;
}

}