-
Notifications
You must be signed in to change notification settings - Fork 0
Ensure compat WP 7+ : enqueue assets for shared blocks #17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -76,6 +76,8 @@ public function render_callback( $attributes, $content, $block ): string { | |
|
|
||
| // When displaying full content, just return the rendered content. | ||
| if ( 'full' === $display ) { | ||
| $this->enqueue_embedded_block_assets( $block_data['html'] ); | ||
|
|
||
| /** | ||
| * Filters if dependencies for the blocks contain in the shared block should be enqueued or not on | ||
| * the current site. | ||
|
|
@@ -330,6 +332,49 @@ private function get_rendered_block( int $site_id, int $post_id, string $block_i | |
| return $block_data; | ||
| } | ||
|
|
||
| /** | ||
| * Enqueue assets for blocks present in shared HTML. | ||
| * This is necessary to ensure that the blocks are styled correctly in the consumer page. | ||
| * | ||
| * Also enqueues view script modules (`viewScriptModule` / `view_script_module_ids`). | ||
| * | ||
| * @param string $html Rendered shared block HTML. | ||
| * | ||
| * @return void | ||
| * @author Jules Fell | ||
| */ | ||
| private function enqueue_embedded_block_assets( string $html ): void { | ||
| if ( '' === $html ) { | ||
| return; | ||
| } | ||
|
|
||
| foreach ( \WP_Block_Type_Registry::get_instance()->get_all_registered() as $block_type ) { | ||
| if ( empty( $block_type->name ) ) { | ||
| continue; | ||
| } | ||
|
|
||
| // Convert block name to class name. | ||
| // For example: core/media-text → wp-block-media-text ; beapi/icon-block → wp-block-beapi-icon-block | ||
| $block_class = 'wp-block-' . preg_replace( '/^core-/', '', str_replace( '/', '-', $block_type->name ) ); | ||
|
|
||
| if ( ! str_contains( $html, $block_class ) ) { | ||
| continue; | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unreliable HTML block detectionMedium Severity Block presence is inferred with Additional Locations (1)Reviewed by Cursor Bugbot for commit 11c6a3c. Configure here. |
||
|
|
||
| foreach ( (array) ( $block_type->style_handles ?? [] ) as $handle ) { | ||
| wp_enqueue_style( $handle ); | ||
| } | ||
|
|
||
| foreach ( (array) ( $block_type->view_script_handles ?? [] ) as $handle ) { | ||
| wp_enqueue_script( $handle ); | ||
| } | ||
|
|
||
| foreach ( (array) ( $block_type->view_script_module_ids ?? [] ) as $module_id ) { | ||
| wp_enqueue_script_module( $module_id ); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Make excerpt from block's html content. | ||
| * | ||
|
|
||


There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Skip dependencies filter bypassed
Medium Severity
enqueue_embedded_block_assetsruns beforemultisite_shared_block_skip_block_dependenciesis applied, so assets are always enqueued even when that filter returns true. The existing dependency path still honors the filter, so behavior is now inconsistent and the documented opt-out no longer fully works.Reviewed by Cursor Bugbot for commit 11c6a3c. Configure here.