Changeset 51149 for trunk/src/wp-includes/class-wp-theme-json.php
- Timestamp:
- 06/15/2021 08:50:26 AM (3 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/class-wp-theme-json.php
r51089 r51149 40 40 */ 41 41 const ROOT_BLOCK_SELECTOR = 'body'; 42 43 const VALID_ORIGINS = array( 44 'core', 45 'theme', 46 'user', 47 ); 42 48 43 49 /** … … 247 253 * 248 254 * @param array $theme_json A structure that follows the theme.json schema. 249 */ 250 public function __construct( $theme_json = array() ) { 255 * @param string $origin What source of data this object represents. One of core, theme, or user. Default: theme. 256 */ 257 public function __construct( $theme_json = array(), $origin = 'theme' ) { 258 if ( ! in_array( $origin, self::VALID_ORIGINS, true ) ) { 259 $origin = 'theme'; 260 } 261 251 262 if ( ! isset( $theme_json['version'] ) || self::LATEST_SCHEMA !== $theme_json['version'] ) { 252 263 $this->theme_json = array(); … … 255 266 256 267 $this->theme_json = self::sanitize( $theme_json ); 268 269 // Internally, presets are keyed by origin. 270 $nodes = self::get_setting_nodes( $this->theme_json ); 271 foreach ( $nodes as $node ) { 272 foreach ( self::PRESETS_METADATA as $preset ) { 273 $path = array_merge( $node['path'], $preset['path'] ); 274 $preset = _wp_array_get( $this->theme_json, $path, null ); 275 if ( null !== $preset ) { 276 _wp_array_set( $this->theme_json, $path, array( $origin => $preset ) ); 277 } 278 } 279 } 257 280 } 258 281 … … 633 656 634 657 /** 658 * Given an array of presets keyed by origin and the value key of the preset, 659 * it returns an array where each key is the preset slug and each value the preset value. 660 * 661 * @param array $preset_per_origin Array of presets keyed by origin. 662 * @param string $value_key The property of the preset that contains its value. 663 * 664 * @return array Array of presets where each key is a slug and each value is the preset value. 665 */ 666 private static function get_merged_preset_by_slug( $preset_per_origin, $value_key ) { 667 $result = array(); 668 foreach ( self::VALID_ORIGINS as $origin ) { 669 if ( ! isset( $preset_per_origin[ $origin ] ) ) { 670 continue; 671 } 672 foreach ( $preset_per_origin[ $origin ] as $preset ) { 673 // We don't want to use kebabCase here, 674 // see https://github.com/WordPress/gutenberg/issues/32347 675 // However, we need to make sure the generated class or css variable 676 // doesn't contain spaces. 677 $result[ preg_replace( '/\s+/', '-', $preset['slug'] ) ] = $preset[ $value_key ]; 678 } 679 } 680 return $result; 681 } 682 683 /** 635 684 * Given a settings array, it returns the generated rulesets 636 685 * for the preset classes. … … 652 701 $stylesheet = ''; 653 702 foreach ( self::PRESETS_METADATA as $preset ) { 654 $values = _wp_array_get( $settings, $preset['path'], array() ); 655 foreach ( $values as $value ) { 656 foreach ( $preset['classes'] as $class ) { 703 $preset_per_origin = _wp_array_get( $settings, $preset['path'], array() ); 704 $preset_by_slug = self::get_merged_preset_by_slug( $preset_per_origin, $preset['value_key'] ); 705 foreach ( $preset['classes'] as $class ) { 706 foreach ( $preset_by_slug as $slug => $value ) { 657 707 $stylesheet .= self::to_ruleset( 658 // We don't want to use kebabCase here, 659 // see https://github.com/WordPress/gutenberg/issues/32347 660 // However, we need to make sure the generated class 661 // doesn't contain spaces. 662 self::append_to_selector( $selector, '.has-' . preg_replace( '/\s+/', '-', $value['slug'] ) . '-' . $class['class_suffix'] ), 708 self::append_to_selector( $selector, '.has-' . $slug . '-' . $class['class_suffix'] ), 663 709 array( 664 710 array( 665 711 'name' => $class['property_name'], 666 'value' => $value [ $preset['value_key'] ]. ' !important',712 'value' => $value . ' !important', 667 713 ), 668 714 ) … … 696 742 $declarations = array(); 697 743 foreach ( self::PRESETS_METADATA as $preset ) { 698 $values = _wp_array_get( $settings, $preset['path'], array() ); 699 foreach ( $values as $value ) { 744 $preset_per_origin = _wp_array_get( $settings, $preset['path'], array() ); 745 $preset_by_slug = self::get_merged_preset_by_slug( $preset_per_origin, $preset['value_key'] ); 746 foreach ( $preset_by_slug as $slug => $value ) { 700 747 $declarations[] = array( 701 'name' => '--wp--preset--' . $preset['css_var_infix'] . '--' . $ value['slug'],702 'value' => $value [ $preset['value_key'] ],748 'name' => '--wp--preset--' . $preset['css_var_infix'] . '--' . $slug, 749 'value' => $value, 703 750 ); 704 751 } … … 1040 1087 * Merge new incoming data. 1041 1088 * 1042 * @since 5.8.01043 *1044 1089 * @param WP_Theme_JSON $incoming Data to merge. 1045 1090 */ 1046 public function merge( $incoming , $update_or_remove = 'remove') {1047 $incoming_data = $incoming->get_raw_data();1048 $ existing_data = $this->theme_json;1091 public function merge( $incoming ) { 1092 $incoming_data = $incoming->get_raw_data(); 1093 $this->theme_json = array_replace_recursive( $this->theme_json, $incoming_data ); 1049 1094 1050 1095 // The array_replace_recursive algorithm merges at the leaf level. 1051 1096 // For leaf values that are arrays it will use the numeric indexes for replacement. 1052 $this->theme_json = array_replace_recursive( $this->theme_json, $incoming_data ); 1053 1054 // There are a few cases in which we want to merge things differently 1055 // from what array_replace_recursive does. 1056 1057 // Some incoming properties should replace the existing. 1097 // In those cases, we want to replace the existing with the incoming value, if it exists. 1058 1098 $to_replace = array(); 1059 1099 $to_replace[] = array( 'custom' ); 1060 1100 $to_replace[] = array( 'spacing', 'units' ); 1061 $to_replace[] = array( 'typography', 'fontSizes' ); 1062 $to_replace[] = array( 'typography', 'fontFamilies' ); 1063 1064 // Some others should be appended to the existing. 1065 // If the slug is the same than an existing element, 1066 // the $update_or_remove param is used to decide 1067 // what to do with the existing element: 1068 // either remove it and append the incoming, 1069 // or update it with the incoming. 1070 $to_append = array(); 1071 $to_append[] = array( 'color', 'duotone' ); 1072 $to_append[] = array( 'color', 'gradients' ); 1073 $to_append[] = array( 'color', 'palette' ); 1101 $to_replace[] = array( 'color', 'duotone' ); 1102 foreach ( self::VALID_ORIGINS as $origin ) { 1103 $to_replace[] = array( 'color', 'palette', $origin ); 1104 $to_replace[] = array( 'color', 'gradients', $origin ); 1105 $to_replace[] = array( 'typography', 'fontSizes', $origin ); 1106 $to_replace[] = array( 'typography', 'fontFamilies', $origin ); 1107 } 1074 1108 1075 1109 $nodes = self::get_setting_nodes( $this->theme_json ); 1076 1110 foreach ( $nodes as $metadata ) { 1077 foreach ( $to_replace as $p ath_to_replace) {1078 $path = array_merge( $metadata['path'], $p ath_to_replace);1111 foreach ( $to_replace as $property_path ) { 1112 $path = array_merge( $metadata['path'], $property_path ); 1079 1113 $node = _wp_array_get( $incoming_data, $path, array() ); 1080 1114 if ( ! empty( $node ) ) { … … 1082 1116 } 1083 1117 } 1084 foreach ( $to_append as $path_to_append ) { 1085 $path = array_merge( $metadata['path'], $path_to_append ); 1086 $incoming_node = _wp_array_get( $incoming_data, $path, array() ); 1087 $existing_node = _wp_array_get( $existing_data, $path, array() ); 1088 1089 if ( empty( $incoming_node ) && empty( $existing_node ) ) { 1090 continue; 1091 } 1092 1093 $index_table = array(); 1094 $existing_slugs = array(); 1095 $merged = array(); 1096 foreach ( $existing_node as $key => $value ) { 1097 $index_table[ $value['slug'] ] = $key; 1098 $existing_slugs[] = $value['slug']; 1099 $merged[ $key ] = $value; 1100 } 1101 1102 $to_remove = array(); 1103 foreach ( $incoming_node as $value ) { 1104 if ( ! in_array( $value['slug'], $existing_slugs, true ) ) { 1105 $merged[] = $value; 1106 } elseif ( 'update' === $update_or_remove ) { 1107 $merged[ $index_table[ $value['slug'] ] ] = $value; 1108 } else { 1109 $merged[] = $value; 1110 $to_remove[] = $index_table[ $value['slug'] ]; 1111 } 1112 } 1113 1114 // Remove the duplicated values and pack the sparsed array. 1115 foreach ( $to_remove as $index ) { 1116 unset( $merged[ $index ] ); 1117 } 1118 $merged = array_values( $merged ); 1119 1120 _wp_array_set( $this->theme_json, $path, $merged ); 1121 } 1122 } 1123 1118 } 1124 1119 } 1125 1120
Note: See TracChangeset
for help on using the changeset viewer.