<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[52401] trunk: Editor: Add support for nameless font sizes in `WP_Theme_JSON`.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/52401">52401</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/52401","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>hellofromTonya</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2021-12-21 06:00:34 +0000 (Tue, 21 Dec 2021)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>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 <a href="https://core.trac.wordpress.org/changeset/50973">[50973]</a>, <a href="https://core.trac.wordpress.org/changeset/52049">[52049]</a>, <a href="https://core.trac.wordpress.org/changeset/52275">[52275]</a>, <a href="https://core.trac.wordpress.org/changeset/52320">[52320]</a>.

Props ntsekouras, costdev, hellofromTonya.
Fixes <a href="https://core.trac.wordpress.org/ticket/54640">#54640</a>. See <a href="https://core.trac.wordpress.org/ticket/54487">#54487</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesclasswpthemejsonphp">trunk/src/wp-includes/class-wp-theme-json.php</a></li>
<li><a href="#trunktestsphpunitteststhemewpThemeJsonphp">trunk/tests/phpunit/tests/theme/wpThemeJson.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesclasswpthemejsonphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/class-wp-theme-json.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/class-wp-theme-json.php     2021-12-21 04:24:23 UTC (rev 52400)
+++ trunk/src/wp-includes/class-wp-theme-json.php       2021-12-21 06:00:34 UTC (rev 52401)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -74,80 +74,86 @@
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * This contains the necessary metadata to process them:
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * - path       => where to find the preset within the settings section
-        * - override   => whether a theme preset with the same slug as a default preset
-        *                 can override it
-        * - value_key  => the key that represents the value
-        * - value_func => optionally, instead of value_key, a function to generate
-        *                 the value that takes a preset as an argument
-        *                 (either value_key or value_func should be present)
-        * - css_vars   => template string to use in generating the CSS Custom Property.
-        *                 Example output: "--wp--preset--duotone--blue: <value>" will generate
-        *                 as many CSS Custom Properties as presets defined
-        *                 substituting the $slug for the slug's value for each preset value.
-        * - classes    => array containing a structure with the classes to
-        *                 generate for the presets, where for each array item
-        *                 the key is the class name and the value the property name.
-        *                 The "$slug" substring will be replaced by the slug of each preset.
-        *                 For example:
-        *                 'classes' => array(
-        *                   '.has-$slug-color'            => 'color',
-        *                   '.has-$slug-background-color' => 'background-color',
-        *                   '.has-$slug-border-color'     => 'border-color',
-        *                 )
-        * - properties => array of CSS properties to be used by kses to
-        *                 validate the content of each preset
-        *                 by means of the remove_insecure_properties method.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * - path              => where to find the preset within the settings section
+        * - override          => whether a theme preset with the same slug as a default preset
+        *                        can override it
+        * - use_default_names => whether to use the default names
+        * - value_key         => the key that represents the value
+        * - value_func        => optionally, instead of value_key, a function to generate
+        *                        the value that takes a preset as an argument
+        *                        (either value_key or value_func should be present)
+        * - css_vars          => template string to use in generating the CSS Custom Property.
+        *                        Example output: "--wp--preset--duotone--blue: <value>" will generate
+        *                        as many CSS Custom Properties as presets defined
+        *                        substituting the $slug for the slug's value for each preset value.
+        * - classes           => array containing a structure with the classes to
+        *                        generate for the presets, where for each array item
+        *                        the key is the class name and the value the property name.
+        *                        The "$slug" substring will be replaced by the slug of each preset.
+        *                        For example:
+        *                        'classes' => array(
+        *                           '.has-$slug-color'            => 'color',
+        *                           '.has-$slug-background-color' => 'background-color',
+        *                           '.has-$slug-border-color'     => 'border-color',
+        *                        )
+        * - properties        => array of CSS properties to be used by kses to
+        *                        validate the content of each preset
+        *                        by means of the remove_insecure_properties method.
</ins><span class="cx" style="display: block; padding: 0 10px">          *
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 5.8.0
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * @since 5.9.0 Added the `color/duotone` and `typography/fontFamily` presets,
-        *              simplified the metadata structure.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * @since 5.9.0 Added the `color/duotone`, `typography/fontFamily`, and `use_default_names
+        *              presets and simplified the metadata structure.
</ins><span class="cx" style="display: block; padding: 0 10px">          * @var array
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="cx" style="display: block; padding: 0 10px">        const PRESETS_METADATA = array(
</span><span class="cx" style="display: block; padding: 0 10px">                array(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        'path'       => array( 'color', 'palette' ),
-                       'override'   => array( 'color', 'defaultPalette' ),
-                       'value_key'  => 'color',
-                       'css_vars'   => '--wp--preset--color--$slug',
-                       'classes'    => array(
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 'path'              => array( 'color', 'palette' ),
+                       'override'          => array( 'color', 'defaultPalette' ),
+                       'use_default_names' => false,
+                       'value_key'         => 'color',
+                       'css_vars'          => '--wp--preset--color--$slug',
+                       'classes'           => array(
</ins><span class="cx" style="display: block; padding: 0 10px">                                 '.has-$slug-color'            => 'color',
</span><span class="cx" style="display: block; padding: 0 10px">                                '.has-$slug-background-color' => 'background-color',
</span><span class="cx" style="display: block; padding: 0 10px">                                '.has-$slug-border-color'     => 'border-color',
</span><span class="cx" style="display: block; padding: 0 10px">                        ),
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        'properties' => array( 'color', 'background-color', 'border-color' ),
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 'properties'        => array( 'color', 'background-color', 'border-color' ),
</ins><span class="cx" style="display: block; padding: 0 10px">                 ),
</span><span class="cx" style="display: block; padding: 0 10px">                array(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        'path'       => array( 'color', 'gradients' ),
-                       'override'   => array( 'color', 'defaultGradients' ),
-                       'value_key'  => 'gradient',
-                       'css_vars'   => '--wp--preset--gradient--$slug',
-                       'classes'    => array( '.has-$slug-gradient-background' => 'background' ),
-                       'properties' => array( 'background' ),
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 'path'              => array( 'color', 'gradients' ),
+                       'override'          => array( 'color', 'defaultGradients' ),
+                       'use_default_names' => false,
+                       'value_key'         => 'gradient',
+                       'css_vars'          => '--wp--preset--gradient--$slug',
+                       'classes'           => array( '.has-$slug-gradient-background' => 'background' ),
+                       'properties'        => array( 'background' ),
</ins><span class="cx" style="display: block; padding: 0 10px">                 ),
</span><span class="cx" style="display: block; padding: 0 10px">                array(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        'path'       => array( 'color', 'duotone' ),
-                       'override'   => true,
-                       'value_func' => 'wp_render_duotone_filter_preset',
-                       'css_vars'   => '--wp--preset--duotone--$slug',
-                       'classes'    => array(),
-                       'properties' => array( 'filter' ),
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 'path'              => array( 'color', 'duotone' ),
+                       'override'          => true,
+                       'use_default_names' => false,
+                       'value_func'        => 'wp_render_duotone_filter_preset',
+                       'css_vars'          => '--wp--preset--duotone--$slug',
+                       'classes'           => array(),
+                       'properties'        => array( 'filter' ),
</ins><span class="cx" style="display: block; padding: 0 10px">                 ),
</span><span class="cx" style="display: block; padding: 0 10px">                array(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        'path'       => array( 'typography', 'fontSizes' ),
-                       'override'   => true,
-                       'value_key'  => 'size',
-                       'css_vars'   => '--wp--preset--font-size--$slug',
-                       'classes'    => array( '.has-$slug-font-size' => 'font-size' ),
-                       'properties' => array( 'font-size' ),
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 'path'              => array( 'typography', 'fontSizes' ),
+                       'override'          => true,
+                       'use_default_names' => true,
+                       'value_key'         => 'size',
+                       'css_vars'          => '--wp--preset--font-size--$slug',
+                       'classes'           => array( '.has-$slug-font-size' => 'font-size' ),
+                       'properties'        => array( 'font-size' ),
</ins><span class="cx" style="display: block; padding: 0 10px">                 ),
</span><span class="cx" style="display: block; padding: 0 10px">                array(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        'path'       => array( 'typography', 'fontFamilies' ),
-                       'override'   => true,
-                       'value_key'  => 'fontFamily',
-                       'css_vars'   => '--wp--preset--font-family--$slug',
-                       'classes'    => array( '.has-$slug-font-family' => 'font-family' ),
-                       'properties' => array( 'font-family' ),
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 'path'              => array( 'typography', 'fontFamilies' ),
+                       'override'          => true,
+                       'use_default_names' => false,
+                       'value_key'         => 'fontFamily',
+                       'css_vars'          => '--wp--preset--font-family--$slug',
+                       'classes'           => array( '.has-$slug-font-family' => 'font-family' ),
+                       'properties'        => array( 'font-family' ),
</ins><span class="cx" style="display: block; padding: 0 10px">                 ),
</span><span class="cx" style="display: block; padding: 0 10px">        );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1524,12 +1530,24 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                $override_preset = self::should_override_preset( $this->theme_json, $node['path'], $preset['override'] );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                                foreach ( self::VALID_ORIGINS as $origin ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                        $path    = array_merge( $node['path'], $preset['path'], array( $origin ) );
-                                       $content = _wp_array_get( $incoming_data, $path, null );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                 $base_path = array_merge( $node['path'], $preset['path'] );
+                                       $path      = array_merge( $base_path, array( $origin ) );
+                                       $content   = _wp_array_get( $incoming_data, $path, null );
</ins><span class="cx" style="display: block; padding: 0 10px">                                         if ( ! isset( $content ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                                continue;
</span><span class="cx" style="display: block; padding: 0 10px">                                        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                        if ( 'theme' === $origin && $preset['use_default_names'] ) {
+                                               foreach ( $content as &$item ) {
+                                                       if ( ! array_key_exists( 'name', $item ) ) {
+                                                               $name = self::get_name_from_defaults( $item['slug'], $base_path );
+                                                               if ( null !== $name ) {
+                                                                       $item['name'] = $name;
+                                                               }
+                                                       }
+                                               }
+                                       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                                         if (
</span><span class="cx" style="display: block; padding: 0 10px">                                                ( 'theme' !== $origin ) ||
</span><span class="cx" style="display: block; padding: 0 10px">                                                ( 'theme' === $origin && $override_preset )
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1631,6 +1649,29 @@
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * Get a `default`'s preset name by a provided slug.
+        *
+        * @since 5.9.0
+        *
+        * @param string $slug The slug we want to find a match from default presets.
+        * @param array  $base_path The path to inspect. It's 'settings' by default.
+        * @return string|null
+        */
+       private function get_name_from_defaults( $slug, $base_path ) {
+               $path            = array_merge( $base_path, array( 'default' ) );
+               $default_content = _wp_array_get( $this->theme_json, $path, null );
+               if ( ! $default_content ) {
+                       return null;
+               }
+               foreach ( $default_content as $item ) {
+                       if ( $slug === $item['slug'] ) {
+                               return $item['name'];
+                       }
+               }
+               return null;
+       }
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * Removes the preset values whose slug is equal to any of given slugs.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 5.9.0
</span></span></pre></div>
<a id="trunktestsphpunitteststhemewpThemeJsonphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/theme/wpThemeJson.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/theme/wpThemeJson.php   2021-12-21 04:24:23 UTC (rev 52400)
+++ trunk/tests/phpunit/tests/theme/wpThemeJson.php     2021-12-21 06:00:34 UTC (rev 52401)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1484,6 +1484,100 @@
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * @ticket 54640
+        */
+       public function test_merge_incoming_data_presets_use_default_names() {
+               $defaults   = new WP_Theme_JSON(
+                       array(
+                               'version'  => WP_Theme_JSON::LATEST_SCHEMA,
+                               'settings' => array(
+                                       'typography' => array(
+                                               'fontSizes' => array(
+                                                       array(
+                                                               'name' => 'Small',
+                                                               'slug' => 'small',
+                                                               'size' => '12px',
+                                                       ),
+                                                       array(
+                                                               'name' => 'Large',
+                                                               'slug' => 'large',
+                                                               'size' => '20px',
+                                                       ),
+                                               ),
+                                       ),
+                               ),
+                       ),
+                       'default'
+               );
+               $theme_json = new WP_Theme_JSON(
+                       array(
+                               'version'  => WP_Theme_JSON::LATEST_SCHEMA,
+                               'settings' => array(
+                                       'typography' => array(
+                                               'fontSizes' => array(
+                                                       array(
+                                                               'slug' => 'small',
+                                                               'size' => '1.1rem',
+                                                       ),
+                                                       array(
+                                                               'slug' => 'large',
+                                                               'size' => '1.75rem',
+                                                       ),
+                                                       array(
+                                                               'name' => 'Huge',
+                                                               'slug' => 'huge',
+                                                               'size' => '3rem',
+                                                       ),
+                                               ),
+                                       ),
+                               ),
+                       ),
+                       'theme'
+               );
+               $expected   = array(
+                       'version'  => WP_Theme_JSON::LATEST_SCHEMA,
+                       'settings' => array(
+                               'typography' => array(
+                                       'fontSizes' => array(
+                                               'default' => array(
+                                                       array(
+                                                               'name' => 'Small',
+                                                               'slug' => 'small',
+                                                               'size' => '12px',
+                                                       ),
+                                                       array(
+                                                               'name' => 'Large',
+                                                               'slug' => 'large',
+                                                               'size' => '20px',
+                                                       ),
+                                               ),
+                                               'theme'   => array(
+                                                       array(
+                                                               'slug' => 'small',
+                                                               'size' => '1.1rem',
+                                                               'name' => 'Small',
+                                                       ),
+                                                       array(
+                                                               'slug' => 'large',
+                                                               'size' => '1.75rem',
+                                                               'name' => 'Large',
+                                                       ),
+                                                       array(
+                                                               'name' => 'Huge',
+                                                               'slug' => 'huge',
+                                                               'size' => '3rem',
+                                                       ),
+                                               ),
+                                       ),
+                               ),
+                       ),
+               );
+               $defaults->merge( $theme_json );
+               $actual = $defaults->get_raw_data();
+               $this->assertSameSetsWithIndex( $expected, $actual );
+       }
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * @ticket 54336
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="cx" style="display: block; padding: 0 10px">        public function test_remove_insecure_properties_removes_unsafe_styles() {
</span></span></pre>
</div>
</div>

</body>
</html>