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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file. The format
on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). Each changelog entry gets prefixed with the category of the
item (Added, Changed, Depreciated, Removed, Fixed, Security).

## [2026.03]

- Updated: Asset enqueuing for core blocks should now allow block scripts & styles to properly render across the FE & editor.

## [2026.02]
- Added: `moderntribe/tribe_embed` composer package v1.1.0.
- Updated: Renamed "Moose" to "ModernPress" across the project (excluding Lando configs and deployment pipelines).
Expand Down
90 changes: 66 additions & 24 deletions wp-content/plugins/core/src/Blocks/Block_Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,57 +64,99 @@ public function register_core_block_variations(): void {
/**
* Enqueue core block styles
*
* Adds the core block styles to both the public site and to the editor.
* On the public site, styles are inlined into the document inside a `<style>` element.
* Styles are added as a `<link>` for both block & site editor regardless of if it is inline or iframed.
* Additionally, the selectors are prefixed with `.editor-styles-wrapper` in the editors.
* On the public site, CSS is inlined onto WordPress's registered style handle for that block
* (e.g. `wp-block-paragraph`) so it only prints when that block's stylesheet is enqueued.
*
* In wp-admin (block / site editor), styles are loaded as `<link>` tags so they work in the
* editor shell and iframed canvas without relying on core's per-block queue on the front end.
*
* On the front end, if the core style handle is not registered, styles are skipped and
* `_doing_it_wrong()` is triggered so the misconfiguration is visible during development.
*/
public function enqueue_core_block_public_styles(): void {
$handle = $this->get_block_style_handle();
$block = $this->get_block_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;
$args = $this->get_asset_file_args( get_theme_file_path( "dist/blocks/$path/editor.asset.php" ) );
$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;
if ( is_admin() ) {
$src = get_theme_file_uri( "dist/blocks/$path/style-index.css" );

wp_enqueue_style(
"tribe-$block",
$src,
[],
$args['version'] ?? false,
'all'
);

return;
}

wp_add_inline_style( $handle, file_get_contents( $src_path ) );
add_editor_style( $src );
$css = file_get_contents( $src_path );

if ( $css === false ) {
return;
}

$core_handle = $this->get_block_style_handle();

if ( ! wp_style_is( $core_handle, 'registered' ) ) {
_doing_it_wrong(
__METHOD__,
sprintf(
'Extended block "%1$s" has no registered stylesheet handle "%2$s" when attaching front-end styles. Override get_block_style_handle() to match the block\'s registered style, or ensure the block registers its stylesheet on init before wp_enqueue_scripts.',
$this->get_block_name(),
$core_handle
),
''
);

return;
}

wp_add_inline_style( $core_handle, $css );
}

/**
* Enqueue editor-specific styles
*
* These are the editor specific style overrides for the block.
* Styles are added as a `<link>` file for both block & site editor regardless of if it is inline or iframed.
* Additionally, the selectors are prefixed with `.editor-styles-wrapper` in the editors.
*/
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 ) ) {
if ( ! is_admin() ) {
return;
}

if ( ! empty( $version ) ) {
$editor_src = $editor_src . '?ver=' . $version;
$block = $this->get_block_handle();
$path = $this->get_block_path();
$args = $this->get_asset_file_args( get_theme_file_path( "dist/blocks/$path/editor.asset.php" ) );
$src_path = get_theme_file_path( "dist/blocks/$path/editor.css" );
$src = get_theme_file_uri( "dist/blocks/$path/editor.css" );

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

add_editor_style( $editor_src );
wp_enqueue_style(
"tribe-editor-$block",
$src,
[],
$args['version'] ?? false,
'all'
);
}

public function enqueue_core_block_editor_scripts(): void {
if ( ! is_admin() ) {
return;
}

$block = $this->get_block_handle();
$path = $this->get_block_path();
$args = $this->get_asset_file_args( get_theme_file_path( "dist/blocks/$path/editor.asset.php" ) );
Expand All @@ -126,7 +168,7 @@ public function enqueue_core_block_editor_scripts(): void {
}

wp_enqueue_script(
"admin-$block-scripts",
"tribe-editor-$block",
$src,
$args['dependencies'] ?? [],
$args['version'] ?? false,
Expand Down
21 changes: 8 additions & 13 deletions wp-content/plugins/core/src/Blocks/Blocks_Subscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,26 +49,21 @@ public function register(): void {
}
}, 10, 0 );

/**
* Enqueue styles in the editor for WP Core blocks
*/
add_action( 'admin_init', function (): void {
foreach ( $this->container->get( Blocks_Definer::EXTENDED ) as $block ) {
// core block public styles
$block->enqueue_core_block_public_styles();
// core block editor-only styles
$block->enqueue_core_block_editor_styles();
}
}, 10, 0 );

/**
* Enqueue block editor-only scripts
*
* Core blocks shouldn't ever have FE scripts and should only include
* editor scripts in order to override default block functionality
*/
add_action( 'enqueue_block_editor_assets', function (): void {
add_action( 'enqueue_block_assets', function (): void {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

this always makes me nervous. every time we update this action, we end up needing the _assets one somewhere else (or the other way around). 😂

foreach ( $this->container->get( Blocks_Definer::EXTENDED ) as $block ) {
// Public overrides as `<link>` only in wp-admin. On the front end they are attached
// from `wp_enqueue_scripts` via inline styles on the core block handle (see Block_Base).
if ( is_admin() ) {
$block->enqueue_core_block_public_styles();
}
// core block editor-only styles
$block->enqueue_core_block_editor_styles();
// core block editor-only scripts
$block->enqueue_core_block_editor_scripts();
}
Expand Down
Loading