Changeset 50977 for trunk/src/wp-includes/class-wp-theme-json.php
- Timestamp:
- 05/24/2021 06:56:09 PM (5 years ago)
- File:
-
- 1 edited
-
trunk/src/wp-includes/class-wp-theme-json.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/class-wp-theme-json.php
r50973 r50977 1018 1018 * @param WP_Theme_JSON $incoming Data to merge. 1019 1019 */ 1020 public function merge( $incoming ) {1020 public function merge( $incoming, $update_or_remove = 'remove' ) { 1021 1021 $incoming_data = $incoming->get_raw_data(); 1022 $existing_data = $this->theme_json; 1023 1024 // The array_replace_recursive algorithm merges at the leaf level. 1025 // For leaf values that are arrays it will use the numeric indexes for replacement. 1022 1026 $this->theme_json = array_replace_recursive( $this->theme_json, $incoming_data ); 1023 1027 1024 /* 1025 * The array_replace_recursive algorithm merges at the leaf level. 1026 * For leaf values that are arrays it will use the numeric indexes for replacement. 1027 * In those cases, what we want is to use the incoming value, if it exists. 1028 * 1029 * These are the cases that have array values at the leaf levels. 1030 */ 1031 $properties = array(); 1032 $properties[] = array( 'color', 'palette' ); 1033 $properties[] = array( 'color', 'gradients' ); 1034 $properties[] = array( 'custom' ); 1035 $properties[] = array( 'spacing', 'units' ); 1036 $properties[] = array( 'typography', 'fontSizes' ); 1037 $properties[] = array( 'typography', 'fontFamilies' ); 1028 // There are a few cases in which we want to merge things differently 1029 // from what array_replace_recursive does. 1030 1031 // Some incoming properties should replace the existing. 1032 $to_replace = array(); 1033 $to_replace[] = array( 'custom' ); 1034 $to_replace[] = array( 'spacing', 'units' ); 1035 $to_replace[] = array( 'typography', 'fontSizes' ); 1036 $to_replace[] = array( 'typography', 'fontFamilies' ); 1037 1038 // Some others should be appended to the existing. 1039 // If the slug is the same than an existing element, 1040 // the $update_or_remove param is used to decide 1041 // what to do with the existing element: 1042 // either remove it and append the incoming, 1043 // or update it with the incoming. 1044 $to_append = array(); 1045 $to_append[] = array( 'color', 'duotone' ); 1046 $to_append[] = array( 'color', 'gradients' ); 1047 $to_append[] = array( 'color', 'palette' ); 1038 1048 1039 1049 $nodes = self::get_setting_nodes( $this->theme_json ); 1040 1050 foreach ( $nodes as $metadata ) { 1041 foreach ( $ properties as $property_path) {1042 $path = array_merge( $metadata['path'], $p roperty_path);1051 foreach ( $to_replace as $path_to_replace ) { 1052 $path = array_merge( $metadata['path'], $path_to_replace ); 1043 1053 $node = _wp_array_get( $incoming_data, $path, array() ); 1044 1054 if ( ! empty( $node ) ) { 1045 1055 _wp_array_set( $this->theme_json, $path, $node ); 1046 1056 } 1057 } 1058 foreach ( $to_append as $path_to_append ) { 1059 $path = array_merge( $metadata['path'], $path_to_append ); 1060 $incoming_node = _wp_array_get( $incoming_data, $path, array() ); 1061 $existing_node = _wp_array_get( $existing_data, $path, array() ); 1062 1063 if ( empty( $incoming_node ) && empty( $existing_node ) ) { 1064 continue; 1065 } 1066 1067 $index_table = array(); 1068 $existing_slugs = array(); 1069 $merged = array(); 1070 foreach ( $existing_node as $key => $value ) { 1071 $index_table[ $value['slug'] ] = $key; 1072 $existing_slugs[] = $value['slug']; 1073 $merged[ $key ] = $value; 1074 } 1075 1076 $to_remove = array(); 1077 foreach ( $incoming_node as $value ) { 1078 if ( ! in_array( $value['slug'], $existing_slugs, true ) ) { 1079 $merged[] = $value; 1080 } elseif ( 'update' === $update_or_remove ) { 1081 $merged[ $index_table[ $value['slug'] ] ] = $value; 1082 } else { 1083 $merged[] = $value; 1084 $to_remove[] = $index_table[ $value['slug'] ]; 1085 } 1086 } 1087 1088 // Remove the duplicated values and pack the sparsed array. 1089 foreach ( $to_remove as $index ) { 1090 unset( $merged[ $index ] ); 1091 } 1092 $merged = array_values( $merged ); 1093 1094 _wp_array_set( $this->theme_json, $path, $merged ); 1047 1095 } 1048 1096 }
Note: See TracChangeset
for help on using the changeset viewer.