WordPress.org

Make WordPress Core


Ignore:
Timestamp:
12/14/2018 03:35:55 AM (23 months ago)
Author:
desrosj
Message:

Styles: Add helper functions for loading block styles.

Blocks are able to register styles that used in the editor and the frontend, or only in the editor. These functions ensure the correct styles are loaded in the correct place.

Props pento.

Merges [43812] to trunk.

See #45065.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk

  • trunk/tests/phpunit/tests/dependencies/styles.php

    r42343 r44157  
    55 */
    66class Tests_Dependencies_Styles extends WP_UnitTestCase {
    7     var $old_wp_styles;
     7    private $old_wp_styles;
     8    private $old_wp_scripts;
    89
    910    function setUp() {
    1011        parent::setUp();
     12
    1113        if ( empty( $GLOBALS['wp_styles'] ) ) {
    1214            $GLOBALS['wp_styles'] = null;
    1315        }
     16
    1417        $this->old_wp_styles = $GLOBALS['wp_styles'];
     18
     19        if ( empty( $GLOBALS['wp_scripts'] ) ) {
     20            $GLOBALS['wp_scripts'] = null;
     21        }
     22
     23        $this->old_wp_styles = $GLOBALS['wp_scripts'];
     24
    1525        remove_action( 'wp_default_styles', 'wp_default_styles' );
    1626        remove_action( 'wp_print_styles', 'print_emoji_styles' );
     27
    1728        $GLOBALS['wp_styles']                  = new WP_Styles();
    1829        $GLOBALS['wp_styles']->default_version = get_bloginfo( 'version' );
     30
     31        $GLOBALS['wp_scripts']                  = new WP_Scripts();
     32        $GLOBALS['wp_scripts']->default_version = get_bloginfo( 'version' );
    1933    }
    2034
    2135    function tearDown() {
    22         $GLOBALS['wp_styles'] = $this->old_wp_styles;
     36        $GLOBALS['wp_styles']  = $this->old_wp_styles;
     37        $GLOBALS['wp_scripts'] = $this->old_wp_scripts;
     38
    2339        add_action( 'wp_default_styles', 'wp_default_styles' );
    2440        add_action( 'wp_print_styles', 'print_emoji_styles' );
     41
     42        if ( current_theme_supports( 'wp-block-styles' ) ) {
     43            remove_theme_support( 'wp-block-styles' );
     44        }
     45
    2546        parent::tearDown();
    2647    }
     
    306327        );
    307328    }
     329
     330    /**
     331     * Tests that visual block styles are enqueued in the editor even when there is not theme support for 'wp-block-styles'.
     332     *
     333     * Visual block styles should always be enqueued when editing to avoid the appearance of a broken editor.
     334     */
     335    function test_block_styles_for_editing_without_theme_support() {
     336        // Confirm we are without theme support by default.
     337        $this->assertFalse( current_theme_supports( 'wp-block-styles' ) );
     338
     339        wp_default_styles( $GLOBALS['wp_styles'] );
     340
     341        $this->assertFalse( wp_style_is( 'wp-block-library-theme' ) );
     342        wp_enqueue_style( 'wp-edit-blocks' );
     343        $this->assertTrue( wp_style_is( 'wp-block-library-theme' ) );
     344    }
     345
     346    /**
     347     * Tests that visual block styles are enqueued when there is theme support for 'wp-block-styles'.
     348     *
     349     * Visual block styles should always be enqueued when editing to avoid the appearance of a broken editor.
     350     */
     351    function test_block_styles_for_editing_with_theme_support() {
     352        add_theme_support( 'wp-block-styles' );
     353
     354        wp_default_styles( $GLOBALS['wp_styles'] );
     355
     356        $this->assertFalse( wp_style_is( 'wp-block-library-theme' ) );
     357        wp_common_block_scripts_and_styles();
     358        $this->assertTrue( wp_style_is( 'wp-block-library-theme' ) );
     359    }
     360
     361    /**
     362     * Tests that visual block styles are not enqueued for viewing when there is no theme support for 'wp-block-styles'.
     363     *
     364     * Visual block styles should not be enqueued unless a theme opts in.
     365     * This way we avoid style conflicts with existing themes.
     366     */
     367    function test_no_block_styles_for_viewing_without_theme_support() {
     368        // Confirm we are without theme support by default.
     369        $this->assertFalse( current_theme_supports( 'wp-block-styles' ) );
     370
     371        wp_default_styles( $GLOBALS['wp_styles'] );
     372
     373        $this->assertFalse( wp_style_is( 'wp-block-library-theme' ) );
     374        wp_enqueue_style( 'wp-block-library' );
     375        $this->assertFalse( wp_style_is( 'wp-block-library-theme' ) );
     376    }
     377
     378    /**
     379     * Tests that visual block styles are enqueued for viewing when there is theme support for 'wp-block-styles'.
     380     *
     381     * Visual block styles should be enqueued when a theme opts in.
     382     */
     383    function test_block_styles_for_viewing_with_theme_support() {
     384        add_theme_support( 'wp-block-styles' );
     385
     386        wp_default_styles( $GLOBALS['wp_styles'] );
     387
     388        $this->assertFalse( wp_style_is( 'wp-block-library-theme' ) );
     389        wp_common_block_scripts_and_styles();
     390        $this->assertTrue( wp_style_is( 'wp-block-library-theme' ) );
     391    }
    308392}
Note: See TracChangeset for help on using the changeset viewer.