Make WordPress Core


Ignore:
Timestamp:
05/24/2021 05:38:59 PM (5 years ago)
Author:
jorgefilipecosta
Message:

Block Editor: Add Global Styles support using theme.json file.

This is the second piece of landing the theme.json processing in WordPress core.
It includes the mechanism that outputs the CSS styles of a theme.json file.

Props nosolosw, youknowriad.
See #53175.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/theme/wpThemeJson.php

    r50967 r50973  
    3535                ),
    3636                'styles'   => array(
    37                     'color' => array(
    38                         'link' => 'blue',
     37                    'elements' => array(
     38                        'link' => array(
     39                            'color' => array(
     40                                'text' => '#111',
     41                            ),
     42                        ),
    3943                    ),
    4044                ),
     
    5862
    5963        $this->assertEqualSetsWithIndex( $expected, $actual );
     64    }
     65
     66    function test_get_stylesheet() {
     67        $theme_json = new WP_Theme_JSON(
     68            array(
     69                'version'  => WP_Theme_JSON::LATEST_SCHEMA,
     70                'settings' => array(
     71                    'color'  => array(
     72                        'text'    => 'value',
     73                        'palette' => array(
     74                            array(
     75                                'slug'  => 'grey',
     76                                'color' => 'grey',
     77                            ),
     78                        ),
     79                    ),
     80                    'misc'   => 'value',
     81                    'blocks' => array(
     82                        'core/group' => array(
     83                            'custom' => array(
     84                                'base-font'   => 16,
     85                                'line-height' => array(
     86                                    'small'  => 1.2,
     87                                    'medium' => 1.4,
     88                                    'large'  => 1.8,
     89                                ),
     90                            ),
     91                        ),
     92                    ),
     93                ),
     94                'styles'   => array(
     95                    'color'    => array(
     96                        'text' => 'var:preset|color|grey',
     97                    ),
     98                    'misc'     => 'value',
     99                    'elements' => array(
     100                        'link' => array(
     101                            'color' => array(
     102                                'text'       => '#111',
     103                                'background' => '#333',
     104                            ),
     105                        ),
     106                    ),
     107                    'blocks'   => array(
     108                        'core/group'     => array(
     109                            'elements' => array(
     110                                'link' => array(
     111                                    'color' => array(
     112                                        'text' => '#111',
     113                                    ),
     114                                ),
     115                            ),
     116                            'spacing'  => array(
     117                                'padding' => array(
     118                                    'top'    => '12px',
     119                                    'bottom' => '24px',
     120                                ),
     121                            ),
     122                        ),
     123                        'core/heading'   => array(
     124                            'color'    => array(
     125                                'text' => '#123456',
     126                            ),
     127                            'elements' => array(
     128                                'link' => array(
     129                                    'color'      => array(
     130                                        'text'       => '#111',
     131                                        'background' => '#333',
     132                                    ),
     133                                    'typography' => array(
     134                                        'fontSize' => '60px',
     135                                    ),
     136                                ),
     137                            ),
     138                        ),
     139                        'core/post-date' => array(
     140                            'color'    => array(
     141                                'text' => '#123456',
     142                            ),
     143                            'elements' => array(
     144                                'link' => array(
     145                                    'color' => array(
     146                                        'background' => '#777',
     147                                        'text'       => '#555',
     148                                    ),
     149                                ),
     150                            ),
     151                        ),
     152                    ),
     153                ),
     154                'misc'     => 'value',
     155            )
     156        );
     157
     158        $this->assertEquals(
     159            'body{--wp--preset--color--grey: grey;}.wp-block-group{--wp--custom--base-font: 16;--wp--custom--line-height--small: 1.2;--wp--custom--line-height--medium: 1.4;--wp--custom--line-height--large: 1.8;}body{color: var(--wp--preset--color--grey);}a{background-color: #333;color: #111;}.wp-block-group{padding-top: 12px;padding-bottom: 24px;}.wp-block-group a{color: #111;}h1,h2,h3,h4,h5,h6{color: #123456;}h1 a,h2 a,h3 a,h4 a,h5 a,h6 a{background-color: #333;color: #111;font-size: 60px;}.wp-block-post-date{color: #123456;}.wp-block-post-date a{background-color: #777;color: #555;}.has-grey-color{color: grey !important;}.has-grey-background-color{background-color: grey !important;}',
     160            $theme_json->get_stylesheet()
     161        );
     162        $this->assertEquals(
     163            'body{color: var(--wp--preset--color--grey);}a{background-color: #333;color: #111;}.wp-block-group{padding-top: 12px;padding-bottom: 24px;}.wp-block-group a{color: #111;}h1,h2,h3,h4,h5,h6{color: #123456;}h1 a,h2 a,h3 a,h4 a,h5 a,h6 a{background-color: #333;color: #111;font-size: 60px;}.wp-block-post-date{color: #123456;}.wp-block-post-date a{background-color: #777;color: #555;}.has-grey-color{color: grey !important;}.has-grey-background-color{background-color: grey !important;}',
     164            $theme_json->get_stylesheet( 'block_styles' )
     165        );
     166        $this->assertEquals(
     167            'body{--wp--preset--color--grey: grey;}.wp-block-group{--wp--custom--base-font: 16;--wp--custom--line-height--small: 1.2;--wp--custom--line-height--medium: 1.4;--wp--custom--line-height--large: 1.8;}',
     168            $theme_json->get_stylesheet( 'css_variables' )
     169        );
     170    }
     171
     172    function test_get_stylesheet_preset_rules_come_after_block_rules() {
     173        $theme_json = new WP_Theme_JSON(
     174            array(
     175                'version'  => WP_Theme_JSON::LATEST_SCHEMA,
     176                'settings' => array(
     177                    'blocks' => array(
     178                        'core/group' => array(
     179                            'color' => array(
     180                                'palette' => array(
     181                                    array(
     182                                        'slug'  => 'grey',
     183                                        'color' => 'grey',
     184                                    ),
     185                                ),
     186                            ),
     187                        ),
     188                    ),
     189                ),
     190                'styles'   => array(
     191                    'blocks' => array(
     192                        'core/group' => array(
     193                            'color' => array(
     194                                'text' => 'red',
     195                            ),
     196                        ),
     197                    ),
     198                ),
     199            )
     200        );
     201
     202        $this->assertEquals(
     203            '.wp-block-group{--wp--preset--color--grey: grey;}.wp-block-group{color: red;}.wp-block-group.has-grey-color{color: grey !important;}.wp-block-group.has-grey-background-color{background-color: grey !important;}',
     204            $theme_json->get_stylesheet()
     205        );
     206        $this->assertEquals(
     207            '.wp-block-group{color: red;}.wp-block-group.has-grey-color{color: grey !important;}.wp-block-group.has-grey-background-color{background-color: grey !important;}',
     208            $theme_json->get_stylesheet( 'block_styles' )
     209        );
     210    }
     211
     212    public function test_get_stylesheet_preset_values_are_marked_as_important() {
     213        $theme_json = new WP_Theme_JSON(
     214            array(
     215                'version'  => WP_Theme_JSON::LATEST_SCHEMA,
     216                'settings' => array(
     217                    'color' => array(
     218                        'palette' => array(
     219                            array(
     220                                'slug'  => 'grey',
     221                                'color' => 'grey',
     222                            ),
     223                        ),
     224                    ),
     225                ),
     226                'styles'   => array(
     227                    'blocks' => array(
     228                        'core/paragraph' => array(
     229                            'color'      => array(
     230                                'text'       => 'red',
     231                                'background' => 'blue',
     232                            ),
     233                            'typography' => array(
     234                                'fontSize'   => '12px',
     235                                'lineHeight' => '1.3',
     236                            ),
     237                        ),
     238                    ),
     239                ),
     240            )
     241        );
     242
     243        $this->assertEquals(
     244            'body{--wp--preset--color--grey: grey;}p{background-color: blue;color: red;font-size: 12px;line-height: 1.3;}.has-grey-color{color: grey !important;}.has-grey-background-color{background-color: grey !important;}',
     245            $theme_json->get_stylesheet()
     246        );
    60247    }
    61248
     
    251438                        'color' => array(
    252439                            'custom' => false,
     440                        ),
     441                    ),
     442                ),
     443            ),
     444            'styles'   => array(
     445                'typography' => array(
     446                    'fontSize' => '12',
     447                ),
     448                'blocks'     => array(
     449                    'core/group' => array(
     450                        'spacing' => array(
     451                            'padding' => array(
     452                                'top'    => '12px',
     453                                'bottom' => '12px',
     454                            ),
     455                        ),
     456                    ),
     457                    'core/list'  => array(
     458                        'typography' => array(
     459                            'fontSize' => '12',
     460                        ),
     461                        'color'      => array(
     462                            'background' => 'brown',
    253463                        ),
    254464                    ),
Note: See TracChangeset for help on using the changeset viewer.