Make WordPress Core


Ignore:
Timestamp:
06/15/2021 08:50:26 AM (3 years ago)
Author:
youknowriad
Message:

Block Editor: Update the WordPress packages with the fixes for 5.8 beta 2.

This includes:

Various

Template Editor

Widgets Editor

Global Styles (theme.json)

Performance

Props nosolosw, jorgefilipecosta, aristath, ntsekouras, peterwilsoncc, mcsf.
See #53397.

File:
1 edited

Legend:

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

    r51089 r51149  
    4040     */
    4141    const ROOT_BLOCK_SELECTOR = 'body';
     42
     43    const VALID_ORIGINS = array(
     44        'core',
     45        'theme',
     46        'user',
     47    );
    4248
    4349    /**
     
    247253     *
    248254     * @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
    251262        if ( ! isset( $theme_json['version'] ) || self::LATEST_SCHEMA !== $theme_json['version'] ) {
    252263            $this->theme_json = array();
     
    255266
    256267        $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        }
    257280    }
    258281
     
    633656
    634657    /**
     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    /**
    635684     * Given a settings array, it returns the generated rulesets
    636685     * for the preset classes.
     
    652701        $stylesheet = '';
    653702        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 ) {
    657707                    $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'] ),
    663709                        array(
    664710                            array(
    665711                                'name'  => $class['property_name'],
    666                                 'value' => $value[ $preset['value_key'] ] . ' !important',
     712                                'value' => $value . ' !important',
    667713                            ),
    668714                        )
     
    696742        $declarations = array();
    697743        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 ) {
    700747                $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,
    703750                );
    704751            }
     
    10401087     * Merge new incoming data.
    10411088     *
    1042      * @since 5.8.0
    1043      *
    10441089     * @param WP_Theme_JSON $incoming Data to merge.
    10451090     */
    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 );
    10491094
    10501095        // The array_replace_recursive algorithm merges at the leaf level.
    10511096        // 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.
    10581098        $to_replace   = array();
    10591099        $to_replace[] = array( 'custom' );
    10601100        $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        }
    10741108
    10751109        $nodes = self::get_setting_nodes( $this->theme_json );
    10761110        foreach ( $nodes as $metadata ) {
    1077             foreach ( $to_replace as $path_to_replace ) {
    1078                 $path = array_merge( $metadata['path'], $path_to_replace );
     1111            foreach ( $to_replace as $property_path ) {
     1112                $path = array_merge( $metadata['path'], $property_path );
    10791113                $node = _wp_array_get( $incoming_data, $path, array() );
    10801114                if ( ! empty( $node ) ) {
     
    10821116                }
    10831117            }
    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        }
    11241119    }
    11251120
Note: See TracChangeset for help on using the changeset viewer.