Make WordPress Core

Changeset 52401


Ignore:
Timestamp:
12/21/2021 06:00:34 AM (5 years ago)
Author:
hellofromTonya
Message:

Editor: Add support for nameless font sizes in WP_Theme_JSON.

With nameless font sizes support, themes use the defaults by not declaring the "name" setting for their fontSizes in theme.json.

Backport from Gutenberg https://github.com/WordPress/gutenberg/pull/37410.

Follow-up to [50973], [52049], [52275], [52320].

Props ntsekouras, costdev, hellofromTonya.
Fixes #54640. See #54487.

Location:
trunk
Files:
2 edited

Legend:

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

    r52364 r52401  
    7575         * This contains the necessary metadata to process them:
    7676         *
    77          * - path       => where to find the preset within the settings section
    78          * - override   => whether a theme preset with the same slug as a default preset
    79          *                 can override it
    80          * - value_key  => the key that represents the value
    81          * - value_func => optionally, instead of value_key, a function to generate
    82          *                 the value that takes a preset as an argument
    83          *                 (either value_key or value_func should be present)
    84          * - css_vars   => template string to use in generating the CSS Custom Property.
    85          *                 Example output: "--wp--preset--duotone--blue: <value>" will generate
    86          *                 as many CSS Custom Properties as presets defined
    87          *                 substituting the $slug for the slug's value for each preset value.
    88          * - classes    => array containing a structure with the classes to
    89          *                 generate for the presets, where for each array item
    90          *                 the key is the class name and the value the property name.
    91          *                 The "$slug" substring will be replaced by the slug of each preset.
    92          *                 For example:
    93          *                 'classes' => array(
    94          *                   '.has-$slug-color'            => 'color',
    95          *                   '.has-$slug-background-color' => 'background-color',
    96          *                   '.has-$slug-border-color'     => 'border-color',
    97          *                 )
    98          * - properties => array of CSS properties to be used by kses to
    99          *                 validate the content of each preset
    100          *                 by means of the remove_insecure_properties method.
    101          *
    102          * @since 5.8.0
    103          * @since 5.9.0 Added the `color/duotone` and `typography/fontFamily` presets,
    104          *              simplified the metadata structure.
     77         * - path              => where to find the preset within the settings section
     78         * - override          => whether a theme preset with the same slug as a default preset
     79         *                        can override it
     80         * - use_default_names => whether to use the default names
     81         * - value_key         => the key that represents the value
     82         * - value_func        => optionally, instead of value_key, a function to generate
     83         *                        the value that takes a preset as an argument
     84         *                        (either value_key or value_func should be present)
     85         * - css_vars          => template string to use in generating the CSS Custom Property.
     86         *                        Example output: "--wp--preset--duotone--blue: <value>" will generate
     87         *                        as many CSS Custom Properties as presets defined
     88         *                        substituting the $slug for the slug's value for each preset value.
     89         * - classes           => array containing a structure with the classes to
     90         *                        generate for the presets, where for each array item
     91         *                        the key is the class name and the value the property name.
     92         *                        The "$slug" substring will be replaced by the slug of each preset.
     93         *                        For example:
     94         *                        'classes' => array(
     95         *                           '.has-$slug-color'            => 'color',
     96         *                           '.has-$slug-background-color' => 'background-color',
     97         *                           '.has-$slug-border-color'     => 'border-color',
     98         *                        )
     99         * - properties        => array of CSS properties to be used by kses to
     100         *                        validate the content of each preset
     101         *                        by means of the remove_insecure_properties method.
     102         *
     103         * @since 5.8.0
     104         * @since 5.9.0 Added the `color/duotone`, `typography/fontFamily`, and `use_default_names
     105         *              presets and simplified the metadata structure.
    105106         * @var array
    106107         */
    107108        const PRESETS_METADATA = array(
    108109                array(
    109                         'path'       => array( 'color', 'palette' ),
    110                         'override'   => array( 'color', 'defaultPalette' ),
    111                         'value_key'  => 'color',
    112                         'css_vars'   => '--wp--preset--color--$slug',
    113                         'classes'    => array(
     110                        'path'              => array( 'color', 'palette' ),
     111                        'override'          => array( 'color', 'defaultPalette' ),
     112                        'use_default_names' => false,
     113                        'value_key'         => 'color',
     114                        'css_vars'          => '--wp--preset--color--$slug',
     115                        'classes'           => array(
    114116                                '.has-$slug-color'            => 'color',
    115117                                '.has-$slug-background-color' => 'background-color',
    116118                                '.has-$slug-border-color'     => 'border-color',
    117119                        ),
    118                         'properties' => array( 'color', 'background-color', 'border-color' ),
     120                        'properties'        => array( 'color', 'background-color', 'border-color' ),
    119121                ),
    120122                array(
    121                         'path'       => array( 'color', 'gradients' ),
    122                         'override'   => array( 'color', 'defaultGradients' ),
    123                         'value_key'  => 'gradient',
    124                         'css_vars'   => '--wp--preset--gradient--$slug',
    125                         'classes'    => array( '.has-$slug-gradient-background' => 'background' ),
    126                         'properties' => array( 'background' ),
     123                        'path'              => array( 'color', 'gradients' ),
     124                        'override'          => array( 'color', 'defaultGradients' ),
     125                        'use_default_names' => false,
     126                        'value_key'         => 'gradient',
     127                        'css_vars'          => '--wp--preset--gradient--$slug',
     128                        'classes'           => array( '.has-$slug-gradient-background' => 'background' ),
     129                        'properties'        => array( 'background' ),
    127130                ),
    128131                array(
    129                         'path'       => array( 'color', 'duotone' ),
    130                         'override'   => true,
    131                         'value_func' => 'wp_render_duotone_filter_preset',
    132                         'css_vars'   => '--wp--preset--duotone--$slug',
    133                         'classes'    => array(),
    134                         'properties' => array( 'filter' ),
     132                        'path'              => array( 'color', 'duotone' ),
     133                        'override'          => true,
     134                        'use_default_names' => false,
     135                        'value_func'        => 'wp_render_duotone_filter_preset',
     136                        'css_vars'          => '--wp--preset--duotone--$slug',
     137                        'classes'           => array(),
     138                        'properties'        => array( 'filter' ),
    135139                ),
    136140                array(
    137                         'path'       => array( 'typography', 'fontSizes' ),
    138                         'override'   => true,
    139                         'value_key'  => 'size',
    140                         'css_vars'   => '--wp--preset--font-size--$slug',
    141                         'classes'    => array( '.has-$slug-font-size' => 'font-size' ),
    142                         'properties' => array( 'font-size' ),
     141                        'path'              => array( 'typography', 'fontSizes' ),
     142                        'override'          => true,
     143                        'use_default_names' => true,
     144                        'value_key'         => 'size',
     145                        'css_vars'          => '--wp--preset--font-size--$slug',
     146                        'classes'           => array( '.has-$slug-font-size' => 'font-size' ),
     147                        'properties'        => array( 'font-size' ),
    143148                ),
    144149                array(
    145                         'path'       => array( 'typography', 'fontFamilies' ),
    146                         'override'   => true,
    147                         'value_key'  => 'fontFamily',
    148                         'css_vars'   => '--wp--preset--font-family--$slug',
    149                         'classes'    => array( '.has-$slug-font-family' => 'font-family' ),
    150                         'properties' => array( 'font-family' ),
     150                        'path'              => array( 'typography', 'fontFamilies' ),
     151                        'override'          => true,
     152                        'use_default_names' => false,
     153                        'value_key'         => 'fontFamily',
     154                        'css_vars'          => '--wp--preset--font-family--$slug',
     155                        'classes'           => array( '.has-$slug-font-family' => 'font-family' ),
     156                        'properties'        => array( 'font-family' ),
    151157                ),
    152158        );
     
    15251531
    15261532                                foreach ( self::VALID_ORIGINS as $origin ) {
    1527                                         $path    = array_merge( $node['path'], $preset['path'], array( $origin ) );
    1528                                         $content = _wp_array_get( $incoming_data, $path, null );
     1533                                        $base_path = array_merge( $node['path'], $preset['path'] );
     1534                                        $path      = array_merge( $base_path, array( $origin ) );
     1535                                        $content   = _wp_array_get( $incoming_data, $path, null );
    15291536                                        if ( ! isset( $content ) ) {
    15301537                                                continue;
     1538                                        }
     1539
     1540                                        if ( 'theme' === $origin && $preset['use_default_names'] ) {
     1541                                                foreach ( $content as &$item ) {
     1542                                                        if ( ! array_key_exists( 'name', $item ) ) {
     1543                                                                $name = self::get_name_from_defaults( $item['slug'], $base_path );
     1544                                                                if ( null !== $name ) {
     1545                                                                        $item['name'] = $name;
     1546                                                                }
     1547                                                        }
     1548                                                }
    15311549                                        }
    15321550
     
    16321650
    16331651        /**
     1652         * Get a `default`'s preset name by a provided slug.
     1653         *
     1654         * @since 5.9.0
     1655         *
     1656         * @param string $slug The slug we want to find a match from default presets.
     1657         * @param array  $base_path The path to inspect. It's 'settings' by default.
     1658         * @return string|null
     1659         */
     1660        private function get_name_from_defaults( $slug, $base_path ) {
     1661                $path            = array_merge( $base_path, array( 'default' ) );
     1662                $default_content = _wp_array_get( $this->theme_json, $path, null );
     1663                if ( ! $default_content ) {
     1664                        return null;
     1665                }
     1666                foreach ( $default_content as $item ) {
     1667                        if ( $slug === $item['slug'] ) {
     1668                                return $item['name'];
     1669                        }
     1670                }
     1671                return null;
     1672        }
     1673
     1674        /**
    16341675         * Removes the preset values whose slug is equal to any of given slugs.
    16351676         *
  • trunk/tests/phpunit/tests/theme/wpThemeJson.php

    r52364 r52401  
    14821482
    14831483                $this->assertEqualSetsWithIndex( $expected, $actual );
     1484        }
     1485
     1486        /**
     1487         * @ticket 54640
     1488         */
     1489        public function test_merge_incoming_data_presets_use_default_names() {
     1490                $defaults   = new WP_Theme_JSON(
     1491                        array(
     1492                                'version'  => WP_Theme_JSON::LATEST_SCHEMA,
     1493                                'settings' => array(
     1494                                        'typography' => array(
     1495                                                'fontSizes' => array(
     1496                                                        array(
     1497                                                                'name' => 'Small',
     1498                                                                'slug' => 'small',
     1499                                                                'size' => '12px',
     1500                                                        ),
     1501                                                        array(
     1502                                                                'name' => 'Large',
     1503                                                                'slug' => 'large',
     1504                                                                'size' => '20px',
     1505                                                        ),
     1506                                                ),
     1507                                        ),
     1508                                ),
     1509                        ),
     1510                        'default'
     1511                );
     1512                $theme_json = new WP_Theme_JSON(
     1513                        array(
     1514                                'version'  => WP_Theme_JSON::LATEST_SCHEMA,
     1515                                'settings' => array(
     1516                                        'typography' => array(
     1517                                                'fontSizes' => array(
     1518                                                        array(
     1519                                                                'slug' => 'small',
     1520                                                                'size' => '1.1rem',
     1521                                                        ),
     1522                                                        array(
     1523                                                                'slug' => 'large',
     1524                                                                'size' => '1.75rem',
     1525                                                        ),
     1526                                                        array(
     1527                                                                'name' => 'Huge',
     1528                                                                'slug' => 'huge',
     1529                                                                'size' => '3rem',
     1530                                                        ),
     1531                                                ),
     1532                                        ),
     1533                                ),
     1534                        ),
     1535                        'theme'
     1536                );
     1537                $expected   = array(
     1538                        'version'  => WP_Theme_JSON::LATEST_SCHEMA,
     1539                        'settings' => array(
     1540                                'typography' => array(
     1541                                        'fontSizes' => array(
     1542                                                'default' => array(
     1543                                                        array(
     1544                                                                'name' => 'Small',
     1545                                                                'slug' => 'small',
     1546                                                                'size' => '12px',
     1547                                                        ),
     1548                                                        array(
     1549                                                                'name' => 'Large',
     1550                                                                'slug' => 'large',
     1551                                                                'size' => '20px',
     1552                                                        ),
     1553                                                ),
     1554                                                'theme'   => array(
     1555                                                        array(
     1556                                                                'slug' => 'small',
     1557                                                                'size' => '1.1rem',
     1558                                                                'name' => 'Small',
     1559                                                        ),
     1560                                                        array(
     1561                                                                'slug' => 'large',
     1562                                                                'size' => '1.75rem',
     1563                                                                'name' => 'Large',
     1564                                                        ),
     1565                                                        array(
     1566                                                                'name' => 'Huge',
     1567                                                                'slug' => 'huge',
     1568                                                                'size' => '3rem',
     1569                                                        ),
     1570                                                ),
     1571                                        ),
     1572                                ),
     1573                        ),
     1574                );
     1575                $defaults->merge( $theme_json );
     1576                $actual = $defaults->get_raw_data();
     1577                $this->assertSameSetsWithIndex( $expected, $actual );
    14841578        }
    14851579
Note: See TracChangeset for help on using the changeset viewer.