Make WordPress Core


Ignore:
Timestamp:
06/24/2024 08:49:52 AM (13 months ago)
Author:
oandregal
Message:

Section styles: improve performance and conceptual consistency.

These changes involve:

  • Move shared variation definitions from styles.blocks.variations to styles.variations
  • Remove blockTypes from styles.variations.
  • Do not register shared variations from theme style variation or primary theme.json files.
  • Move the merging of theme.json data into the WP_Theme_JSON_Resolver and WP_Theme_JSON classes.

These changes improve performance and are more future-proof API wise.
See conversation at https://github.com/WordPress/gutenberg/issues/62686

Props aaronrobertshaw, oandregal, andrewserong, joemcgill, talldanwp, andrewserong, ramonopoly, richtabor, youknowriad.

See #61312, #61451.

File:
1 edited

Legend:

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

    r58444 r58466  
    744744     *
    745745     * @since 5.8.0
    746      * @since 6.6.0 Key spacingScale by origin, and Pre-generate the
    747      *              spacingSizes from spacingScale.
     746     * @since 6.6.0 Key spacingScale by origin, and Pre-generate the spacingSizes from spacingScale.
     747     *              Added unwrapping of shared block style variations into block type variations if registered.
    748748     *
    749749     * @param array  $theme_json A structure that follows the theme.json schema.
     
    760760        $valid_element_names = array_keys( static::ELEMENTS );
    761761        $valid_variations    = static::get_valid_block_style_variations();
     762        $this->theme_json    = static::unwrap_shared_block_style_variations( $this->theme_json, $valid_variations );
    762763        $this->theme_json    = static::sanitize( $this->theme_json, $valid_block_names, $valid_element_names, $valid_variations );
    763764        $this->theme_json    = static::maybe_opt_in_into_settings( $this->theme_json );
     
    801802            _wp_array_set( $this->theme_json, $sizes_path, $merged_spacing_sizes );
    802803        }
     804    }
     805
     806    /**
     807     * Unwraps shared block style variations.
     808     *
     809     * It takes the shared variations (styles.variations.variationName) and
     810     * applies them to all the blocks that have the given variation registered
     811     * (styles.blocks.blockType.variations.variationName).
     812     *
     813     * For example, given the `core/paragraph` and `core/group` blocks have
     814     * registered the `section-a` style variation, and given the following input:
     815     *
     816     * {
     817     *   "styles": {
     818     *     "variations": {
     819     *       "section-a": { "color": { "background": "backgroundColor" } }
     820     *     }
     821     *   }
     822     * }
     823     *
     824     * It returns the following output:
     825     *
     826     * {
     827     *   "styles": {
     828     *     "blocks": {
     829     *       "core/paragraph": {
     830     *         "variations": {
     831     *             "section-a": { "color": { "background": "backgroundColor" } }
     832     *         },
     833     *       },
     834     *       "core/group": {
     835     *         "variations": {
     836     *           "section-a": { "color": { "background": "backgroundColor" } }
     837     *         }
     838     *       }
     839     *     }
     840     *   }
     841     * }
     842     *
     843     * @since 6.6.0
     844     *
     845     * @param array $theme_json       A structure that follows the theme.json schema.
     846     * @param array $valid_variations Valid block style variations.
     847     * @return array Theme json data with shared variation definitions unwrapped under appropriate block types.
     848     */
     849    private static function unwrap_shared_block_style_variations( $theme_json, $valid_variations ) {
     850        if ( empty( $theme_json['styles']['variations'] ) || empty( $valid_variations ) ) {
     851            return $theme_json;
     852        }
     853
     854        $new_theme_json = $theme_json;
     855        $variations     = $new_theme_json['styles']['variations'];
     856
     857        foreach ( $valid_variations as $block_type => $registered_variations ) {
     858            foreach ( $registered_variations as $variation_name ) {
     859                $block_level_data = $new_theme_json['styles']['blocks'][ $block_type ]['variations'][ $variation_name ] ?? array();
     860                $top_level_data   = $variations[ $variation_name ] ?? array();
     861                $merged_data      = array_replace_recursive( $top_level_data, $block_level_data );
     862                if ( ! empty( $merged_data ) ) {
     863                    _wp_array_set( $new_theme_json, array( 'styles', 'blocks', $block_type, 'variations', $variation_name ), $merged_data );
     864                }
     865            }
     866        }
     867
     868        unset( $new_theme_json['styles']['variations'] );
     869
     870        return $new_theme_json;
    803871    }
    804872
     
    9671035        $schema['settings']['blocks']                     = $schema_settings_blocks;
    9681036        $schema['settings']['typography']['fontFamilies'] = static::schema_in_root_and_per_origin( static::FONT_FAMILY_SCHEMA );
    969 
    970         /*
    971          * Shared block style variations can be registered from the theme.json data so we can't
    972          * validate them against pre-registered block style variations.
    973          */
    974         $schema['styles']['blocks']['variations'] = null;
    9751037
    9761038        // Remove anything that's not present in the schema.
Note: See TracChangeset for help on using the changeset viewer.