Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions inc/Core/Assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,25 @@ public function __construct() {
* @since 1.0.0
*/
public function register_hooks(): void {
add_action( 'after_setup_theme', [ $this, 'add_editor_styles' ] );
add_action( 'wp_enqueue_scripts', [ $this, 'register_assets' ] );
add_action( 'wp_enqueue_scripts', [ $this, 'enqueue_assets' ] );
add_filter( 'render_block', [ $this, 'enqueue_block_specific_assets' ], 10, 2 );
}

/**
* Register Tailwind stylesheet for the block editor.
*
* @since 1.0.0
*
* @action after_setup_theme
*/
public function add_editor_styles(): void {
if ( $this->tailwind_enabled ) {
add_editor_style( $this->assets_dir . '/css/frontend/tailwind.css' );
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason for using add_editor_style on after_theme_setup instead of using wp_enqueue_style on enqueue_block_editor_assets?

https://developer.wordpress.org/block-editor/how-to-guides/enqueueing-assets-in-the-editor/

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The docs say: Editor-specific stylesheets should almost always be added with add_editor_style() or wp_enqueue_block_style().
Both do similar things. What do you recommend

}
}
Comment thread
Swanand01 marked this conversation as resolved.

/**
* Register assets.
*
Expand Down
74 changes: 74 additions & 0 deletions tests/php/inc/Core/AssetsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ public function set_up(): void {
$this->instance = new Assets();
}

/**
* Tear down test.
*/
public function tear_down(): void {
wp_dequeue_style( 'elementary-theme-tailwind' );
wp_deregister_style( 'elementary-theme-tailwind' );
parent::tear_down();
}

/**
* Test class exists.
*/
Expand All @@ -52,8 +61,73 @@ public function test_implements_registrable(): void {
public function test_register_hooks(): void {
$this->instance->register_hooks();

$this->assertGreaterThan( 0, has_action( 'after_setup_theme', [ $this->instance, 'add_editor_styles' ] ) );
$this->assertGreaterThan( 0, has_action( 'wp_enqueue_scripts', [ $this->instance, 'register_assets' ] ) );
$this->assertGreaterThan( 0, has_action( 'wp_enqueue_scripts', [ $this->instance, 'enqueue_assets' ] ) );
$this->assertGreaterThan( 0, has_filter( 'render_block', [ $this->instance, 'enqueue_block_specific_assets' ] ) );
}

/**
* Test add_editor_styles registers the Tailwind stylesheet when Tailwind is enabled.
*/
public function test_add_editor_styles_when_tailwind_enabled(): void {
global $editor_styles;
$before = (array) $editor_styles;

add_filter( 'elementary_theme_tailwind_enabled', '__return_true' );
$instance = new Assets();
$instance->add_editor_styles();
remove_filter( 'elementary_theme_tailwind_enabled', '__return_true' );

$added = array_diff( (array) $editor_styles, $before );
$found = array_filter( $added, fn( string $s ): bool => str_contains( $s, 'tailwind.css' ) );
$this->assertNotEmpty( $found );
}

/**
* Test add_editor_styles does not register the Tailwind stylesheet when Tailwind is disabled.
*/
public function test_add_editor_styles_when_tailwind_disabled(): void {
global $editor_styles;
$before = (array) $editor_styles;

add_filter( 'elementary_theme_tailwind_enabled', '__return_false' );
$instance = new Assets();
$instance->add_editor_styles();
remove_filter( 'elementary_theme_tailwind_enabled', '__return_false' );

$added = array_diff( (array) $editor_styles, $before );
$found = array_filter( $added, fn( string $s ): bool => str_contains( $s, 'tailwind.css' ) );
$this->assertEmpty( $found );
}

/**
* Test enqueue_assets enqueues the Tailwind stylesheet on the frontend when enabled.
*/
public function test_enqueue_assets_enqueues_tailwind_when_enabled(): void {
add_filter( 'elementary_theme_tailwind_enabled', '__return_true' );
$instance = new Assets();

wp_register_style( 'elementary-theme-tailwind', 'https://example.com/tailwind.css', [], '1.0.0' );
$instance->enqueue_assets();

remove_filter( 'elementary_theme_tailwind_enabled', '__return_true' );

$this->assertTrue( wp_style_is( 'elementary-theme-tailwind', 'enqueued' ) );
}

/**
* Test enqueue_assets does not enqueue the Tailwind stylesheet on the frontend when disabled.
*/
public function test_enqueue_assets_does_not_enqueue_tailwind_when_disabled(): void {
add_filter( 'elementary_theme_tailwind_enabled', '__return_false' );
$instance = new Assets();

wp_register_style( 'elementary-theme-tailwind', 'https://example.com/tailwind.css', [], '1.0.0' );
$instance->enqueue_assets();

remove_filter( 'elementary_theme_tailwind_enabled', '__return_false' );

$this->assertFalse( wp_style_is( 'elementary-theme-tailwind', 'enqueued' ) );
}
}