@@ -89,15 +89,12 @@ public function register( string $id, string $src, array $deps = array(), $versi
8989 'version ' => $ version ,
9090 'enqueue ' => isset ( $ this ->enqueued_before_registered [ $ id ] ),
9191 'dependencies ' => $ dependencies ,
92- 'enqueued ' => false ,
93- 'preloaded ' => false ,
9492 );
9593 }
9694 }
9795
9896 /**
99- * Marks the script module to be enqueued in the page the next time
100- * `print_enqueued_script_modules` is called.
97+ * Marks the script module to be enqueued in the page.
10198 *
10299 * If a src is provided and the script module has not been registered yet, it
103100 * will be registered.
@@ -158,74 +155,49 @@ public function dequeue( string $id ) {
158155 * Adds the hooks to print the import map, enqueued script modules and script
159156 * module preloads.
160157 *
161- * It adds the actions to print the enqueued script modules and script module
162- * preloads to both `wp_head` and `wp_footer` because in classic themes, the
163- * script modules used by the theme and plugins will likely be able to be
164- * printed in the `head`, but the ones used by the blocks will need to be
165- * enqueued in the `footer`.
166- *
167- * As all script modules are deferred and dependencies are handled by the
168- * browser, the order of the script modules is not important, but it's still
169- * better to print the ones that are available when the `wp_head` is rendered,
170- * so the browser starts downloading those as soon as possible.
171- *
172- * The import map is also printed in the footer to be able to include the
173- * dependencies of all the script modules, including the ones printed in the
158+ * In classic themes, the script modules used by the blocks are not yet known
159+ * when the `wp_head` actions is fired, so it needs to print everything in the
174160 * footer.
175161 *
176162 * @since 6.5.0
177163 */
178164 public function add_hooks () {
179- add_action ( 'wp_head ' , array ( $ this , 'print_enqueued_script_modules ' ) );
180- add_action ( 'wp_head ' , array ( $ this , 'print_script_module_preloads ' ) );
181- add_action ( 'wp_footer ' , array ( $ this , 'print_enqueued_script_modules ' ) );
182- add_action ( 'wp_footer ' , array ( $ this , 'print_script_module_preloads ' ) );
183- add_action ( 'wp_footer ' , array ( $ this , 'print_import_map ' ) );
165+ $ position = wp_is_block_theme () ? 'wp_head ' : 'wp_footer ' ;
166+ add_action ( $ position , array ( $ this , 'print_import_map ' ) );
167+ add_action ( $ position , array ( $ this , 'print_enqueued_modules ' ) );
168+ add_action ( $ position , array ( $ this , 'print_module_preloads ' ) );
184169 }
185170
186171 /**
187172 * Prints the enqueued script modules using script tags with type="module"
188173 * attributes.
189174 *
190- * If a enqueued script module has already been printed, it will not be
191- * printed again on subsequent calls to this function.
192- *
193175 * @since 6.5.0
194176 */
195177 public function print_enqueued_script_modules () {
196178 foreach ( $ this ->get_marked_for_enqueue () as $ id => $ script_module ) {
197- if ( false === $ script_module ['enqueued ' ] ) {
198- // Mark it as enqueued so it doesn't get enqueued again.
199- $ this ->registered [ $ id ]['enqueued ' ] = true ;
200-
201- wp_print_script_tag (
202- array (
203- 'type ' => 'module ' ,
204- 'src ' => $ this ->get_versioned_src ( $ script_module ),
205- 'id ' => $ id . '-js-module ' ,
206- )
207- );
208- }
179+ wp_print_script_tag (
180+ array (
181+ 'type ' => 'module ' ,
182+ 'src ' => $ this ->get_versioned_src ( $ script_module ),
183+ 'id ' => $ id . '-js-module ' ,
184+ )
185+ );
209186 }
210187 }
211188
212189 /**
213190 * Prints the the static dependencies of the enqueued script modules using
214191 * link tags with rel="modulepreload" attributes.
215192 *
216- * If a script module is marked for enqueue, it will not be preloaded. If a
217- * preloaded script module has already been printed, it will not be printed
218- * again on subsequent calls to this function.
193+ * If a script module is marked for enqueue, it will not be preloaded.
219194 *
220195 * @since 6.5.0
221196 */
222197 public function print_script_module_preloads () {
223198 foreach ( $ this ->get_dependencies ( array_keys ( $ this ->get_marked_for_enqueue () ), array ( 'static ' ) ) as $ id => $ script_module ) {
224- // Don't preload if it's marked for enqueue or has already been preloaded.
225- if ( true !== $ script_module ['enqueue ' ] && false === $ script_module ['preloaded ' ] ) {
226- // Mark it as preloaded so it doesn't get preloaded again.
227- $ this ->registered [ $ id ]['preloaded ' ] = true ;
228-
199+ // Don't preload if it's marked for enqueue.
200+ if ( true !== $ script_module ['enqueue ' ] ) {
229201 echo sprintf (
230202 '<link rel="modulepreload" href="%s" id="%s"> ' ,
231203 esc_url ( $ this ->get_versioned_src ( $ script_module ) ),
0 commit comments