From c0babc4b9e1dc4852a062088161bf5b73b1544d2 Mon Sep 17 00:00:00 2001 From: Andrew Duthie Date: Fri, 29 May 2020 13:14:15 -0400 Subject: [PATCH 1/2] Framework: Enqueue assets for rendered blocks only --- lib/class-wp-block.php | 8 ++++++++ lib/compat.php | 9 +++++++++ 2 files changed, 17 insertions(+) diff --git a/lib/class-wp-block.php b/lib/class-wp-block.php index 6bd82378e39acb..ccc3f5f9423077 100644 --- a/lib/class-wp-block.php +++ b/lib/class-wp-block.php @@ -206,6 +206,14 @@ public function render( $options = array() ) { $post = $global_post; } + if ( ! empty( $this->block_type->script ) ) { + wp_enqueue_script( $this->block_type->script ); + } + + if ( ! empty( $this->block_type->style ) ) { + wp_enqueue_style( $this->block_type->style ); + } + /** This filter is documented in src/wp-includes/blocks.php */ return apply_filters( 'render_block', $block_content, $this->parsed_block ); } diff --git a/lib/compat.php b/lib/compat.php index 9dd224adb2f19e..dd44b26ebf9937 100644 --- a/lib/compat.php +++ b/lib/compat.php @@ -291,3 +291,12 @@ function gutenberg_render_block_with_assigned_block_context( $pre_render, $parse return $block->render(); } add_filter( 'pre_render_block', 'gutenberg_render_block_with_assigned_block_context', 9, 2 ); + +/** + * Avoid enqueueing block assets of all registered blocks for all posts, instead + * deferring to block render mechanics to enqueue scripts, thereby ensuring only + * blocks of the content have their assets enqueued. + * + * @see WP_Block::render + */ +remove_action( 'enqueue_block_assets', 'wp_enqueue_registered_block_scripts_and_styles' ); From cf73a7eb4cdadde331fcc341ad62ba4061a8b0c1 Mon Sep 17 00:00:00 2001 From: Andrew Duthie Date: Fri, 5 Jun 2020 16:35:04 -0400 Subject: [PATCH 2/2] Compat: Add core reference ticket --- lib/compat.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/compat.php b/lib/compat.php index dd44b26ebf9937..c97c72b8ef89f5 100644 --- a/lib/compat.php +++ b/lib/compat.php @@ -297,6 +297,11 @@ function gutenberg_render_block_with_assigned_block_context( $pre_render, $parse * deferring to block render mechanics to enqueue scripts, thereby ensuring only * blocks of the content have their assets enqueued. * + * This can be removed once minimum support for the plugin is outside the range + * of the version associated with closure of the following ticket. + * + * @see https://core.trac.wordpress.org/ticket/50328 + * * @see WP_Block::render */ remove_action( 'enqueue_block_assets', 'wp_enqueue_registered_block_scripts_and_styles' );