Skip to content

Commit

Permalink
Merge pull request #155 from moderntribe/feature/MOOSE-114/inline-edi…
Browse files Browse the repository at this point in the history
…tor-styles

BE: Update editor style enqueues to use a version param to allow cache busting
  • Loading branch information
MlKilderkin authored Aug 15, 2024
2 parents 7f7830c + b4dc43e commit 9fc039e
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 0 deletions.
2 changes: 2 additions & 0 deletions tests/_data/editor_asset_test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?php
return array('dependencies' => array('react-jsx-runtime', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-compose', 'wp-element', 'wp-hooks', 'wp-i18n'), 'version' => '024bef6fb3a0bc82cb17');
8 changes: 8 additions & 0 deletions tests/acceptance/ExampleCest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php declare(strict_types=1);

class ExampleCest {
public function test_it_works( AcceptanceTester $I ): void {
$I->amOnPage('/');
}

}
38 changes: 38 additions & 0 deletions tests/wpunit/BlockBaseWpUnitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php declare(strict_types=1);

class BlockBaseWpUnitTest extends \Codeception\TestCase\WPTestCase {

use \Tribe\Plugin\Assets\Traits\Assets;

public function setUp(): void {
parent::setUp();

$block_dir = get_template_directory() . '/dist/blocks/core/button';
$editor_asset = $block_dir . '/editor.asset.php';
mkdir( $block_dir, 0777, true );

$editor_asset_content = file_get_contents( __DIR__ . '/../_data/editor_asset_test.php' );
$file_handler = fopen( $editor_asset, 'w' );
fwrite( $file_handler, $editor_asset_content );
fclose( $file_handler );
}

public function test_editor_styles_can_get_version_from_asset_php()
{
$args = $this->get_asset_file_args( get_theme_file_path( "dist/blocks/core/button/editor.asset.php" ) );
self::assertArrayHasKey( 'version', $args );
$version = $args['version'] ?? false;
self::assertEquals( '024bef6fb3a0bc82cb17', $version );
}

protected function tearDown(): void {
parent::tearDown();

$block_dir = get_template_directory() . '/dist/blocks/core/button';
$editor_asset = $block_dir . '/editor.asset.php';

unlink( $editor_asset );
rmdir( $block_dir );
}

}
12 changes: 12 additions & 0 deletions wp-content/plugins/core/src/Blocks/Block_Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,19 @@ public function register_core_block_variations(): void {
public function enqueue_core_block_public_styles(): void {
$handle = $this->get_block_style_handle();
$path = $this->get_block_path();
$args = $this->get_asset_file_args( get_theme_file_path( "dist/blocks/$path/index.asset.php" ) );
$version = $args['version'] ?? false;
$src_path = get_theme_file_path( "dist/blocks/$path/style-index.css" );
$src = get_theme_file_uri( "dist/blocks/$path/style-index.css" );

if ( ! file_exists( $src_path ) ) {
return;
}

if ( ! empty( $version ) ) {
$src = $src . '?ver=' . $version;
}

wp_add_inline_style( $handle, file_get_contents( $src_path ) );
add_editor_style( $src );
}
Expand All @@ -92,13 +98,19 @@ public function enqueue_core_block_public_styles(): void {
*/
public function enqueue_core_block_editor_styles(): void {
$path = $this->get_block_path();
$args = $this->get_asset_file_args( get_theme_file_path( "dist/blocks/$path/editor.asset.php" ) );
$version = $args['version'] ?? false;
$editor_src_path = get_theme_file_path( "dist/blocks/$path/editor.css" );
$editor_src = get_theme_file_uri( "dist/blocks/$path/editor.css" );

if ( ! file_exists( $editor_src_path ) ) {
return;
}

if ( ! empty( $version ) ) {
$editor_src = $editor_src . '?ver=' . $version;
}

add_editor_style( $editor_src );
}

Expand Down

0 comments on commit 9fc039e

Please sign in to comment.