Make WordPress Core


Ignore:
Timestamp:
02/06/2023 03:31:50 PM (4 years ago)
Author:
jorgefilipecosta
Message:

Block editor: Update WP_Theme_JSON_Resolver and improve its performance.

This commit includes the latest updates WP_Theme_JSON_Resolver class made in the block editor. Some of these updates improve the performance of the class.

Props Mamaduka, hellofromTonya, flixos90, jorgefilipecosta, oandregal, spacedmonkey, audrasjb, costdev, scruffian.
Closes #57545.

File:
1 edited

Legend:

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

    r55118 r55231  
    236236                $this->assertSame(
    237237                        'Wariant motywu blokowego',
    238                         $style_variations[0]['title']
     238                        $style_variations[1]['title']
    239239                );
    240240        }
     
    776776                $this->assertNull( $property->getValue(), 'Theme i18n schema should not have been loaded without theme support.' );
    777777        }
     778
     779        /**
     780         * Tests that get_merged_data returns the data merged up to the proper origin.
     781         *
     782         * @ticket 57545
     783         *
     784         * @covers WP_Theme_JSON_Resolver::get_merged_data
     785         *
     786         * @dataProvider data_get_merged_data_returns_origin
     787         *
     788         * @param string $origin             What origin to get data from.
     789         * @param bool   $core_palette       Whether the core palette is present.
     790         * @param string $core_palette_text  Message.
     791         * @param string $block_styles       Whether the block styles are present.
     792         * @param string $block_styles_text  Message.
     793         * @param bool   $theme_palette      Whether the theme palette is present.
     794         * @param string $theme_palette_text Message.
     795         * @param bool   $user_palette        Whether the user palette is present.
     796         * @param string $user_palette_text   Message.
     797         */
     798        public function test_get_merged_data_returns_origin( $origin, $core_palette, $core_palette_text, $block_styles, $block_styles_text, $theme_palette, $theme_palette_text, $user_palette, $user_palette_text ) {
     799                // Make sure there is data from the blocks origin.
     800                register_block_type(
     801                        'my/block-with-styles',
     802                        array(
     803                                'api_version' => 2,
     804                                'attributes'  => array(
     805                                        'borderColor' => array(
     806                                                'type' => 'string',
     807                                        ),
     808                                        'style'       => array(
     809                                                'type' => 'object',
     810                                        ),
     811                                ),
     812                                'supports'    => array(
     813                                        '__experimentalStyle' => array(
     814                                                'typography' => array(
     815                                                        'fontSize' => '42rem',
     816                                                ),
     817                                        ),
     818                                ),
     819                        )
     820                );
     821
     822                // Make sure there is data from the theme origin.
     823                switch_theme( 'block-theme' );
     824
     825                // Make sure there is data from the user origin.
     826                wp_set_current_user( self::$administrator_id );
     827                $user_cpt = WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( wp_get_theme(), true );
     828                $config   = json_decode( $user_cpt['post_content'], true );
     829                $config['settings']['color']['palette']['custom'] = array(
     830                        array(
     831                                'color' => 'hotpink',
     832                                'name'  => 'My color',
     833                                'slug'  => 'my-color',
     834                        ),
     835                );
     836                $user_cpt['post_content']                         = wp_json_encode( $config );
     837                wp_update_post( $user_cpt, true, false );
     838
     839                $theme_json = WP_Theme_JSON_Resolver::get_merged_data( $origin );
     840                $settings   = $theme_json->get_settings();
     841                $styles     = $theme_json->get_styles_block_nodes();
     842                $styles     = array_filter(
     843                        $styles,
     844                        static function( $element ) {
     845                                return isset( $element['name'] ) && 'my/block-with-styles' === $element['name'];
     846                        }
     847                );
     848                unregister_block_type( 'my/block-with-styles' );
     849
     850                $this->assertSame( $core_palette, isset( $settings['color']['palette']['default'] ), $core_palette_text );
     851                $this->assertSame( $block_styles, count( $styles ) === 1, $block_styles_text );
     852                $this->assertSame( $theme_palette, isset( $settings['color']['palette']['theme'] ), $theme_palette_text );
     853                $this->assertSame( $user_palette, isset( $settings['color']['palette']['custom'] ), $user_palette_text );
     854
     855        }
     856
     857        /**
     858         * Data provider.
     859         *
     860         * @return array[]
     861         */
     862        public function data_get_merged_data_returns_origin() {
     863                return array(
     864                        'origin_default' => array(
     865                                'origin'             => 'default',
     866                                'core_palette'       => true,
     867                                'core_palette_text'  => 'Core palette must be present',
     868                                'block_styles'       => false,
     869                                'block_styles_text'  => 'Block styles should not be present',
     870                                'theme_palette'      => false,
     871                                'theme_palette_text' => 'Theme palette should not be present',
     872                                'user_palette'       => false,
     873                                'user_palette_text'  => 'User palette should not be present',
     874                        ),
     875                        'origin_blocks'  => array(
     876                                'origin'             => 'blocks',
     877                                'core_palette'       => true,
     878                                'core_palette_text'  => 'Core palette must be present',
     879                                'block_styles'       => true,
     880                                'block_styles_text'  => 'Block styles must be present',
     881                                'theme_palette'      => false,
     882                                'theme_palette_text' => 'Theme palette should not be present',
     883                                'user_palette'       => false,
     884                                'user_palette_text'  => 'User palette should not be present',
     885                        ),
     886                        'origin_theme'   => array(
     887                                'origin'             => 'theme',
     888                                'core_palette'       => true,
     889                                'core_palette_text'  => 'Core palette must be present',
     890                                'block_styles'       => true,
     891                                'block_styles_text'  => 'Block styles must be present',
     892                                'theme_palette'      => true,
     893                                'theme_palette_text' => 'Theme palette must be present',
     894                                'user_palette'       => false,
     895                                'user_palette_text'  => 'User palette should not be present',
     896                        ),
     897                        'origin_custom'  => array(
     898                                'origin'             => 'custom',
     899                                'core_palette'       => true,
     900                                'core_palette_text'  => 'Core palette must be present',
     901                                'block_styles'       => true,
     902                                'block_styles_text'  => 'Block styles must be present',
     903                                'theme_palette'      => true,
     904                                'theme_palette_text' => 'Theme palette must be present',
     905                                'user_palette'       => true,
     906                                'user_palette_text'  => 'User palette must be present',
     907                        ),
     908                );
     909        }
     910
     911        /**
     912         * Tests that get_style_variations returns all variations, including parent theme variations if the theme is a child,
     913         * and that the child variation overwrites the parent variation of the same name.
     914         *
     915         * @ticket 57545
     916         *
     917         * @covers WP_Theme_JSON_Resolver::get_style_variations
     918         **/
     919        public function test_get_style_variations_returns_all_variations() {
     920                // Switch to a child theme.
     921                switch_theme( 'block-theme-child' );
     922                wp_set_current_user( self::$administrator_id );
     923
     924                $actual_settings   = WP_Theme_JSON_Resolver::get_style_variations();
     925                $expected_settings = array(
     926                        array(
     927                                'version'  => 2,
     928                                'title'    => 'variation-b',
     929                                'settings' => array(
     930                                        'blocks' => array(
     931                                                'core/post-title' => array(
     932                                                        'color' => array(
     933                                                                'palette' => array(
     934                                                                        'theme' => array(
     935                                                                                array(
     936                                                                                        'slug'  => 'dark',
     937                                                                                        'name'  => 'Dark',
     938                                                                                        'color' => '#010101',
     939                                                                                ),
     940                                                                        ),
     941                                                                ),
     942                                                        ),
     943                                                ),
     944                                        ),
     945                                ),
     946                        ),
     947                        array(
     948                                'version'  => 2,
     949                                'title'    => 'Block theme variation',
     950                                'settings' => array(
     951                                        'color' => array(
     952                                                'palette' => array(
     953                                                        'theme' => array(
     954                                                                array(
     955                                                                        'slug'  => 'foreground',
     956                                                                        'name'  => 'Foreground',
     957                                                                        'color' => '#3F67C6',
     958                                                                ),
     959                                                        ),
     960                                                ),
     961                                        ),
     962                                ),
     963                                'styles'   => array(
     964                                        'blocks' => array(
     965                                                'core/post-title' => array(
     966                                                        'typography' => array(
     967                                                                'fontWeight' => '700',
     968                                                        ),
     969                                                ),
     970                                        ),
     971                                ),
     972                        ),
     973                );
     974
     975                wp_recursive_ksort( $actual_settings );
     976                wp_recursive_ksort( $expected_settings );
     977
     978                $this->assertSame(
     979                        $expected_settings,
     980                        $actual_settings
     981                );
     982        }
    778983}
Note: See TracChangeset for help on using the changeset viewer.