Make WordPress Core


Ignore:
Timestamp:
06/24/2024 08:49:52 AM (17 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/tests/phpunit/tests/theme/wpThemeJsonResolver.php

    r58413 r58466  
    13221322        $this->assertSame( $expected_data, $actual );
    13231323    }
     1324
     1325    /**
     1326     * Tests that block style variations data gets merged in the following
     1327     * priority order, from highest priority to lowest.
     1328     *
     1329     * - `styles.blocks.blockType.variations` from theme.json
     1330     * - `styles.variations` from theme.json
     1331     * - variations from block style variation files under `/styles`
     1332     * - variations from `WP_Block_Styles_Registry`
     1333     *
     1334     * @ticket 61451
     1335     */
     1336    public function test_block_style_variation_merge_order() {
     1337        switch_theme( 'block-theme-child-with-block-style-variations' );
     1338
     1339        /*
     1340         * Register style for a block that isn't included in the block style variation's partial
     1341         * theme.json's blockTypes. The name must match though so we can ensure the partial's
     1342         * styles do not get applied to this block.
     1343         */
     1344        register_block_style(
     1345            'core/heading',
     1346            array(
     1347                'name'  => 'block-style-variation-b',
     1348                'label' => 'Heading only variation',
     1349            )
     1350        );
     1351
     1352        // Register variation for a block that will be partially overridden at all levels.
     1353        register_block_style(
     1354            'core/media-text',
     1355            array(
     1356                'name'       => 'block-style-variation-a',
     1357                'label'      => 'Block Style Variation A',
     1358                'style_data' => array(
     1359                    'color' => array(
     1360                        'background' => 'pink',
     1361                        'gradient'   => 'var(--custom)',
     1362                    ),
     1363                ),
     1364            )
     1365        );
     1366
     1367        $data         = WP_Theme_JSON_Resolver::get_theme_data()->get_raw_data();
     1368        $block_styles = $data['styles']['blocks'] ?? array();
     1369        $actual       = array_intersect_key(
     1370            $block_styles,
     1371            array_flip( array( 'core/button', 'core/media-text', 'core/heading' ) )
     1372        );
     1373        $expected     = array(
     1374            'core/button'     => array(
     1375                'variations' => array(
     1376                    'outline' => array(
     1377                        'color' => array(
     1378                            'background' => 'red',
     1379                            'text'       => 'white',
     1380                        ),
     1381                    ),
     1382                ),
     1383            ),
     1384            'core/media-text' => array(
     1385                'variations' => array(
     1386                    'block-style-variation-a' => array(
     1387                        'color'      => array(
     1388                            'background' => 'blue',
     1389                            'gradient'   => 'var(--custom)',
     1390                            'text'       => 'aliceblue',
     1391                        ),
     1392                        'typography' => array(
     1393                            'fontSize'   => '1.5em',
     1394                            'lineHeight' => '1.4em',
     1395                        ),
     1396                    ),
     1397                ),
     1398            ),
     1399            'core/heading'    => array(
     1400                'variations' => array(
     1401                    'block-style-variation-b' => array(
     1402                        'typography' => array(
     1403                            'fontSize' => '3em',
     1404                        ),
     1405                    ),
     1406                ),
     1407            ),
     1408        );
     1409
     1410        unregister_block_style( 'core/heading', 'block-style-variation-b' );
     1411        unregister_block_style( 'core/media-text', 'block-style-variation-a' );
     1412
     1413        $this->assertSameSetsWithIndex( $expected, $actual, 'Merged variation styles do not match.' );
     1414    }
    13241415}
Note: See TracChangeset for help on using the changeset viewer.