@@ -48,40 +48,24 @@ public static function setup() {
4848 return ;
4949 }
5050
51- self ::init ();
52- add_action ( 'init ' , array ( __CLASS__ , 'override_block_style ' ) );
53- add_filter ( 'register_block_type_args ' , array ( __CLASS__ , 'register_block_type_args ' ), 150 , 2 );
54- add_action ( 'enqueue_block_editor_assets ' , array ( __CLASS__ , 'enqueue_editor_assets ' ) );
55- add_action (
56- 'wp_enqueue_scripts ' ,
57- function () {
58- if ( wp_should_load_block_editor_scripts_and_styles () ) {
59- self ::enqueue_editor_assets ();
60- }
61-
62- /*
63- * Core should handle this, but Script Module assets are not currently handled.
64- *
65- * `wp_should_load_block_assets_on_demand()` was added in WordPress 6.8. The
66- * `function_exists()` can be removed when 6.8+ is required.
67- */
68- if (
69- function_exists ( 'wp_should_load_block_assets_on_demand ' )
70- && ! wp_should_load_block_assets_on_demand () // @phan-suppress-current-line PhanUndeclaredFunction, UnusedPluginSuppression
71- ) {
72- wp_enqueue_script_module ( self ::MODULE_PREFIX . 'block-front ' );
73- }
74- }
75- );
7651 add_action ( 'after_setup_theme ' , array ( __CLASS__ , 'after_setup_theme ' ), 100 );
52+ add_filter ( 'register_block_type_args ' , array ( __CLASS__ , 'register_block_type_args ' ), 150 , 2 );
7753 }
7854
7955 /**
80- * Registration of scripts and styles.
56+ * Registration of editor scripts, styles, and modules.
57+ *
58+ * Called lazily when editor assets are needed, not on every request.
8159 */
82- private static function init () {
83- $ block_definition_asset_file = include Jetpack_Mu_Wpcom::BASE_DIR . 'build/wpcom-blocks-code-block-definition/wpcom-blocks-code-block-definition.asset.php ' ;
84- $ jetpack_wpcom_modules_asset_file = include Jetpack_Mu_Wpcom::BASE_DIR . 'build-module/assets.php ' ;
60+ private static function register_editor_assets () {
61+ static $ done = false ;
62+ if ( $ done ) {
63+ return ;
64+ }
65+ $ done = true ;
66+
67+ $ block_definition_asset_file = include Jetpack_Mu_Wpcom::BASE_DIR . 'build/wpcom-blocks-code-block-definition/wpcom-blocks-code-block-definition.asset.php ' ;
68+ $ jetpack_wpcom_modules_assets = self ::get_module_asset_data ();
8569
8670 // The block definition must contain the script dependencies that the edit function script module requires.
8771 // Append static dependency list here. Some duplicates may appear, that should be harmless.
@@ -110,8 +94,8 @@ private static function init() {
11094 wp_register_script_module (
11195 self ::MODULE_PREFIX . 'block-edit-function ' ,
11296 plugins_url ( 'build-module/wpcom-blocks-code-edit-function/wpcom-blocks-code-edit-function.js ' , Jetpack_Mu_Wpcom::BASE_FILE ),
113- $ jetpack_wpcom_modules_asset_file ['wpcom-blocks-code-edit-function/wpcom-blocks-code-edit-function.js ' ]['dependencies ' ],
114- $ jetpack_wpcom_modules_asset_file ['wpcom-blocks-code-edit-function/wpcom-blocks-code-edit-function.js ' ]['version ' ]
97+ $ jetpack_wpcom_modules_assets ['wpcom-blocks-code-edit-function/wpcom-blocks-code-edit-function.js ' ]['dependencies ' ],
98+ $ jetpack_wpcom_modules_assets ['wpcom-blocks-code-edit-function/wpcom-blocks-code-edit-function.js ' ]['version ' ]
11599 );
116100
117101 $ editor_style_asset_file = include Jetpack_Mu_Wpcom::BASE_DIR . 'build/wpcom-blocks-code-editor-style/wpcom-blocks-code-editor-style.asset.php ' ;
@@ -122,15 +106,8 @@ private static function init() {
122106 $ editor_style_asset_file ['version ' ]
123107 );
124108
125- wp_register_script_module (
126- self ::MODULE_PREFIX . 'block-front ' ,
127- plugins_url ( 'build-module/wpcom-blocks-code-block-front/wpcom-blocks-code-block-front.js ' , Jetpack_Mu_Wpcom::BASE_FILE ),
128- $ jetpack_wpcom_modules_asset_file ['wpcom-blocks-code-block-front/wpcom-blocks-code-block-front.js ' ]['dependencies ' ],
129- $ jetpack_wpcom_modules_asset_file ['wpcom-blocks-code-block-front/wpcom-blocks-code-block-front.js ' ]['version ' ]
130- );
131-
132109 $ block_worker_url = plugins_url ( 'build-module/wpcom-blocks-code-worker/wpcom-blocks-code-worker.js ' , Jetpack_Mu_Wpcom::BASE_FILE );
133- $ block_worker_version = $ jetpack_wpcom_modules_asset_file ['wpcom-blocks-code-worker/wpcom-blocks-code-worker.js ' ]['version ' ];
110+ $ block_worker_version = $ jetpack_wpcom_modules_assets ['wpcom-blocks-code-worker/wpcom-blocks-code-worker.js ' ]['version ' ];
134111 add_filter (
135112 'script_module_data_ ' . self ::MODULE_PREFIX . 'block-edit-function ' ,
136113 function ( array $ data ) use ( $ block_worker_url , $ block_worker_version ): array {
@@ -141,6 +118,38 @@ function ( array $data ) use ( $block_worker_url, $block_worker_version ): array
141118 );
142119 }
143120
121+ /**
122+ * Enqueue view script module.
123+ */
124+ private static function enqueue_view_assets () {
125+ static $ done = false ;
126+ if ( $ done ) {
127+ return ;
128+ }
129+ $ done = true ;
130+
131+ $ jetpack_wpcom_modules_assets = self ::get_module_asset_data ();
132+ wp_enqueue_script_module (
133+ self ::MODULE_PREFIX . 'block-front ' ,
134+ plugins_url ( 'build-module/wpcom-blocks-code-block-front/wpcom-blocks-code-block-front.js ' , Jetpack_Mu_Wpcom::BASE_FILE ),
135+ $ jetpack_wpcom_modules_assets ['wpcom-blocks-code-block-front/wpcom-blocks-code-block-front.js ' ]['dependencies ' ],
136+ $ jetpack_wpcom_modules_assets ['wpcom-blocks-code-block-front/wpcom-blocks-code-block-front.js ' ]['version ' ]
137+ );
138+ }
139+
140+ /**
141+ * Get the module asset data.
142+ *
143+ * @return array
144+ */
145+ private static function get_module_asset_data () {
146+ static $ jetpack_wpcom_modules_assets = null ;
147+ if ( null === $ jetpack_wpcom_modules_assets ) {
148+ $ jetpack_wpcom_modules_assets = include Jetpack_Mu_Wpcom::BASE_DIR . 'build-module/assets.php ' ;
149+ }
150+ return $ jetpack_wpcom_modules_assets ;
151+ }
152+
144153 /**
145154 * Set up the block view styles.
146155 *
@@ -150,13 +159,22 @@ function ( array $data ) use ( $block_worker_url, $block_worker_version ): array
150159 * Instead of using a different style handle, replace the registered style for `wp-block-code`.
151160 *
152161 * @see https://core.trac.wordpress.org/browser/tags/6.8.3/src/wp-includes/global-styles-and-settings.php#L322
162+ *
163+ * @global \WP_Styles $wp_styles
153164 */
154165 public static function override_block_style () {
166+ global $ wp_styles ;
167+
168+ $ src = plugins_url ( 'build/wpcom-blocks-code-style/wpcom-blocks-code-style.css ' , Jetpack_Mu_Wpcom::BASE_FILE );
169+ // Skip work if style is registered as desired.
170+ if ( isset ( $ wp_styles ->registered ['wp-block-code ' ] ) && $ wp_styles ->registered ['wp-block-code ' ]->src === $ src ) {
171+ return ;
172+ }
173+
155174 $ was_enqueued = wp_style_is ( 'wp-block-code ' , 'enqueued ' );
156175 wp_deregister_style ( 'wp-block-code ' );
157176
158177 $ style_asset_file = include Jetpack_Mu_Wpcom::BASE_DIR . 'build/wpcom-blocks-code-style/wpcom-blocks-code-style.asset.php ' ;
159- $ src = plugins_url ( 'build/wpcom-blocks-code-style/wpcom-blocks-code-style.css ' , Jetpack_Mu_Wpcom::BASE_FILE );
160178 $ version = $ style_asset_file ['version ' ];
161179
162180 wp_register_style (
@@ -181,13 +199,49 @@ public static function override_block_style() {
181199 public static function register_block_type_args ( array $ args , string $ block_type ): array {
182200 if (
183201 'core/code ' !== $ block_type
202+
184203 // In some cases the block may not include the content attribute.
185204 // Only perform enhancement on the _full_, expected block.
186205 || ! isset ( $ args ['attributes ' ]['content ' ] )
206+
207+ // Skip if the block is already processed.
208+ || $ args ['render_callback ' ] === array ( __CLASS__ , 'render_block ' )
187209 ) {
188210 return $ args ;
189211 }
190212
213+ // Register assets and hooks only when overriding the block.
214+ self ::register_editor_assets ();
215+ self ::override_block_style ();
216+
217+ static $ hooks_registered = false ;
218+ if ( ! $ hooks_registered ) {
219+ $ hooks_registered = true ;
220+ add_action ( 'enqueue_block_editor_assets ' , array ( __CLASS__ , 'enqueue_editor_assets ' ) );
221+ add_action (
222+ 'wp_enqueue_scripts ' ,
223+ function () {
224+ if ( wp_should_load_block_editor_scripts_and_styles () ) {
225+ self ::enqueue_editor_assets ();
226+ }
227+
228+ /*
229+ * Core should handle this, but Script Module assets are not currently handled.
230+ *
231+ * `wp_should_load_block_assets_on_demand()` was added in WordPress 6.8. The
232+ * `function_exists()` can be removed when 6.8+ is required.
233+ */
234+ if (
235+ function_exists ( 'wp_should_load_block_assets_on_demand ' )
236+ && ! wp_should_load_block_assets_on_demand () // @phan-suppress-current-line PhanUndeclaredFunction, UnusedPluginSuppression
237+ && has_block ( 'core/code ' )
238+ ) {
239+ self ::enqueue_view_assets ();
240+ }
241+ }
242+ );
243+ }
244+
191245 $ args ['render_callback ' ] = array ( __CLASS__ , 'render_block ' );
192246 $ args ['editor_script_handles ' ] = array_merge ( array ( self ::MODULE_PREFIX . 'block-definition ' ), $ args ['editor_script_handles ' ] ?? array () );
193247
@@ -338,6 +392,12 @@ public static function register_block_type_args( array $args, string $block_type
338392 * Enqueue plugin assets necessary for the block editor.
339393 */
340394 public static function enqueue_editor_assets () {
395+ static $ done = false ;
396+ if ( $ done ) {
397+ return ;
398+ }
399+ $ done = true ;
400+
341401 /*
342402 * The code block registration script depends on some script modules.
343403 * This "dummy" module ensures those dependencies are available.
@@ -375,8 +435,8 @@ public static function render_block( array $attributes, string $content ): strin
375435 $ extra_attrs = array ();
376436 $ style_properties = array ();
377437
378- if ( isset ( $ attributes ['showCopyButton ' ] ) ) {
379- wp_enqueue_script_module ( self ::MODULE_PREFIX . ' block-front ' );
438+ if ( $ attributes ['showCopyButton ' ] ?? false ) {
439+ self ::enqueue_view_assets ( );
380440 }
381441
382442 $ show_line_numbers = $ attributes ['showLineNumbers ' ] ?? false ;
0 commit comments