Make WordPress Core


Ignore:
Timestamp:
05/24/2021 06:56:09 PM (5 years ago)
Author:
jorgefilipecosta
Message:

Block Editor: Update color merging algorithm.

The npm packages in use were published a few hours ago, which include some changes to how we manage colors. This commit adds the corresponding PHP changes.

Props nosolosw.
See #53175.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-theme-json.php

    r50973 r50977  
    10181018         * @param WP_Theme_JSON $incoming Data to merge.
    10191019         */
    1020         public function merge( $incoming ) {
     1020        public function merge( $incoming, $update_or_remove = 'remove' ) {
    10211021                $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.
    10221026                $this->theme_json = array_replace_recursive( $this->theme_json, $incoming_data );
    10231027
    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' );
    10381048
    10391049                $nodes = self::get_setting_nodes( $this->theme_json );
    10401050                foreach ( $nodes as $metadata ) {
    1041                         foreach ( $properties as $property_path ) {
    1042                                 $path = array_merge( $metadata['path'], $property_path );
     1051                        foreach ( $to_replace as $path_to_replace ) {
     1052                                $path = array_merge( $metadata['path'], $path_to_replace );
    10431053                                $node = _wp_array_get( $incoming_data, $path, array() );
    10441054                                if ( ! empty( $node ) ) {
    10451055                                        _wp_array_set( $this->theme_json, $path, $node );
    10461056                                }
     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 );
    10471095                        }
    10481096                }
Note: See TracChangeset for help on using the changeset viewer.