Skip to content

Commit 554b2e7

Browse files
committed
Merge branch 'trunk' into try/php85-tests
2 parents 0efa5fc + c6a9f26 commit 554b2e7

20 files changed

+711
-810
lines changed

src/js/_enqueues/vendor/json2.js

Lines changed: 1 addition & 519 deletions
Large diffs are not rendered by default.

src/wp-includes/class-wp-block.php

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -492,13 +492,12 @@ public function replace_rich_text( $rich_text ) {
492492
public function render( $options = array() ) {
493493
global $post;
494494

495-
// Capture the current assets queues and then clear out to capture the diff of what was introduced by rendering.
495+
$before_wp_enqueue_scripts_count = did_action( 'wp_enqueue_scripts' );
496+
497+
// Capture the current assets queues.
496498
$before_styles_queue = wp_styles()->queue;
497499
$before_scripts_queue = wp_scripts()->queue;
498-
$before_script_modules_queue = wp_script_modules()->queue;
499-
wp_styles()->queue = array();
500-
wp_scripts()->queue = array();
501-
wp_script_modules()->queue = array();
500+
$before_script_modules_queue = wp_script_modules()->get_queue();
502501

503502
/*
504503
* There can be only one root interactive block at a time because the rendered HTML of that block contains
@@ -670,21 +669,27 @@ public function render( $options = array() ) {
670669
}
671670

672671
// Capture the new assets enqueued during rendering, and restore the queues the state prior to rendering.
673-
$new_styles_queue = wp_styles()->queue;
674-
$new_scripts_queue = wp_scripts()->queue;
675-
$new_script_modules_queue = wp_script_modules()->queue;
676-
wp_styles()->queue = $before_styles_queue;
677-
wp_scripts()->queue = $before_scripts_queue;
678-
wp_script_modules()->queue = $before_script_modules_queue;
679-
$has_new_styles = count( $new_styles_queue ) > 0;
680-
$has_new_scripts = count( $new_scripts_queue ) > 0;
681-
$has_new_script_modules = count( $new_script_modules_queue ) > 0;
682-
683-
// Merge the newly enqueued assets with the existing assets if the rendered block is not empty.
672+
$after_styles_queue = wp_styles()->queue;
673+
$after_scripts_queue = wp_scripts()->queue;
674+
$after_script_modules_queue = wp_script_modules()->get_queue();
675+
676+
/*
677+
* As a very special case, a dynamic block may in fact include a call to wp_head() (and thus wp_enqueue_scripts()),
678+
* in which all of its enqueued assets are targeting wp_footer. In this case, nothing would be printed, but this
679+
* shouldn't indicate that the just-enqueued assets should be dequeued due to it being an empty block.
680+
*/
681+
$just_did_wp_enqueue_scripts = ( did_action( 'wp_enqueue_scripts' ) !== $before_wp_enqueue_scripts_count );
682+
683+
$has_new_styles = ( $before_styles_queue !== $after_styles_queue );
684+
$has_new_scripts = ( $before_scripts_queue !== $after_scripts_queue );
685+
$has_new_script_modules = ( $before_script_modules_queue !== $after_script_modules_queue );
686+
687+
// Dequeue the newly enqueued assets with the existing assets if the rendered block was empty & wp_enqueue_scripts did not fire.
684688
if (
689+
! $just_did_wp_enqueue_scripts &&
685690
( $has_new_styles || $has_new_scripts || $has_new_script_modules ) &&
686691
(
687-
trim( $block_content ) !== '' ||
692+
trim( $block_content ) === '' &&
688693
/**
689694
* Filters whether to enqueue assets for a block which has no rendered content.
690695
*
@@ -693,17 +698,17 @@ public function render( $options = array() ) {
693698
* @param bool $enqueue Whether to enqueue assets.
694699
* @param string $block_name Block name.
695700
*/
696-
(bool) apply_filters( 'enqueue_empty_block_content_assets', false, $this->name )
701+
! (bool) apply_filters( 'enqueue_empty_block_content_assets', false, $this->name )
697702
)
698703
) {
699-
if ( $has_new_styles ) {
700-
wp_styles()->queue = array_unique( array_merge( wp_styles()->queue, $new_styles_queue ) );
704+
foreach ( array_diff( $after_styles_queue, $before_styles_queue ) as $handle ) {
705+
wp_dequeue_style( $handle );
701706
}
702-
if ( $has_new_scripts ) {
703-
wp_scripts()->queue = array_unique( array_merge( wp_scripts()->queue, $new_scripts_queue ) );
707+
foreach ( array_diff( $after_scripts_queue, $before_scripts_queue ) as $handle ) {
708+
wp_dequeue_script( $handle );
704709
}
705-
if ( $has_new_script_modules ) {
706-
wp_script_modules()->queue = array_unique( array_merge( wp_script_modules()->queue, $new_script_modules_queue ) );
710+
foreach ( array_diff( $after_script_modules_queue, $before_script_modules_queue ) as $handle ) {
711+
wp_dequeue_script_module( $handle );
707712
}
708713
}
709714

src/wp-includes/class-wp-dependencies.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,13 @@ public function add_data( $handle, $key, $value ) {
289289
if ( ! isset( $this->registered[ $handle ] ) ) {
290290
return false;
291291
}
292+
if ( 'conditional' === $key && '_required-conditional-dependency_' !== $value ) {
293+
_deprecated_argument(
294+
'WP_Dependencies->add_data()',
295+
'6.9.0',
296+
__( 'IE conditional comments are ignored by all supported browsers.' )
297+
);
298+
}
292299

293300
return $this->registered[ $handle ]->add_data( $key, $value );
294301
}

src/wp-includes/class-wp-script-modules.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class WP_Script_Modules {
2828
* @since 6.9.0
2929
* @var string[]
3030
*/
31-
public $queue = array();
31+
private $queue = array();
3232

3333
/**
3434
* Tracks whether the @wordpress/a11y script module is available.
@@ -137,6 +137,17 @@ public function register( string $id, string $src, array $deps = array(), $versi
137137
}
138138
}
139139

140+
/**
141+
* Gets IDs for queued script modules.
142+
*
143+
* @since 6.9.0
144+
*
145+
* @return string[] Script module IDs.
146+
*/
147+
public function get_queue(): array {
148+
return $this->queue;
149+
}
150+
140151
/**
141152
* Checks if the provided fetchpriority is valid.
142153
*
@@ -237,7 +248,7 @@ public function enqueue( string $id, string $src = '', array $deps = array(), $v
237248
* @param string $id The identifier of the script module.
238249
*/
239250
public function dequeue( string $id ) {
240-
$this->queue = array_diff( $this->queue, array( $id ) );
251+
$this->queue = array_values( array_diff( $this->queue, array( $id ) ) );
241252
}
242253

243254
/**

src/wp-includes/class-wp-scripts.php

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,9 @@ public function do_item( $handle, $group = false ) {
292292
}
293293

294294
$obj = $this->registered[ $handle ];
295+
if ( $obj->extra['conditional'] ?? false ) {
296+
return false;
297+
}
295298

296299
if ( null === $obj->ver ) {
297300
$ver = '';
@@ -303,12 +306,9 @@ public function do_item( $handle, $group = false ) {
303306
$ver = $ver ? $ver . '&' . $this->args[ $handle ] : $this->args[ $handle ];
304307
}
305308

306-
$src = $obj->src;
307-
$strategy = $this->get_eligible_loading_strategy( $handle );
308-
$intended_strategy = (string) $this->get_data( $handle, 'strategy' );
309-
$ie_conditional_prefix = '';
310-
$ie_conditional_suffix = '';
311-
$conditional = isset( $obj->extra['conditional'] ) ? $obj->extra['conditional'] : '';
309+
$src = $obj->src;
310+
$strategy = $this->get_eligible_loading_strategy( $handle );
311+
$intended_strategy = (string) $this->get_data( $handle, 'strategy' );
312312

313313
if ( ! $this->is_delayed_strategy( $intended_strategy ) ) {
314314
$intended_strategy = '';
@@ -333,16 +333,11 @@ public function do_item( $handle, $group = false ) {
333333
return false;
334334
}
335335

336-
if ( $conditional ) {
337-
$ie_conditional_prefix = "<!--[if {$conditional}]>\n";
338-
$ie_conditional_suffix = "<![endif]-->\n";
339-
}
340-
341336
$before_script = $this->get_inline_script_tag( $handle, 'before' );
342337
$after_script = $this->get_inline_script_tag( $handle, 'after' );
343338

344339
if ( $before_script || $after_script ) {
345-
$inline_script_tag = $ie_conditional_prefix . $before_script . $after_script . $ie_conditional_suffix;
340+
$inline_script_tag = $before_script . $after_script;
346341
} else {
347342
$inline_script_tag = '';
348343
}
@@ -378,7 +373,7 @@ public function do_item( $handle, $group = false ) {
378373
// Have to print the so-far concatenated scripts right away to maintain the right order.
379374
_print_scripts();
380375
$this->reset();
381-
} elseif ( $this->in_default_dir( $filtered_src ) && ! $conditional ) {
376+
} elseif ( $this->in_default_dir( $filtered_src ) ) {
382377
$this->print_code .= $this->print_extra_script( $handle, false );
383378
$this->concat .= "$handle,";
384379
$this->concat_version .= "$handle$ver";
@@ -389,18 +384,8 @@ public function do_item( $handle, $group = false ) {
389384
}
390385
}
391386

392-
$has_conditional_data = $conditional && $this->get_data( $handle, 'data' );
393-
394-
if ( $has_conditional_data ) {
395-
echo $ie_conditional_prefix;
396-
}
397-
398387
$this->print_extra_script( $handle );
399388

400-
if ( $has_conditional_data ) {
401-
echo $ie_conditional_suffix;
402-
}
403-
404389
// A single item may alias a set of items, by having dependencies, but no source.
405390
if ( ! $src ) {
406391
if ( $inline_script_tag ) {
@@ -453,13 +438,14 @@ public function do_item( $handle, $group = false ) {
453438
if ( is_string( $actual_fetchpriority ) && 'auto' !== $actual_fetchpriority ) {
454439
$attr['fetchpriority'] = $actual_fetchpriority;
455440
}
441+
456442
if ( $original_fetchpriority !== $actual_fetchpriority ) {
457443
$attr['data-wp-fetchpriority'] = $original_fetchpriority;
458444
}
459445

460-
$tag = $translations . $ie_conditional_prefix . $before_script;
446+
$tag = $translations . $before_script;
461447
$tag .= wp_get_script_tag( $attr );
462-
$tag .= $after_script . $ie_conditional_suffix;
448+
$tag .= $after_script;
463449

464450
/**
465451
* Filters the HTML script tag of an enqueued script.
@@ -851,6 +837,11 @@ public function add_data( $handle, $key, $value ) {
851837
return false;
852838
}
853839

840+
if ( 'conditional' === $key ) {
841+
// If a dependency is declared by a conditional script, remove it.
842+
$this->registered[ $handle ]->deps = array();
843+
}
844+
854845
if ( 'strategy' === $key ) {
855846
if ( ! empty( $value ) && ! $this->is_delayed_strategy( $value ) ) {
856847
_doing_it_wrong(

src/wp-includes/class-wp-styles.php

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,10 @@ public function do_item( $handle, $group = false ) {
154154
}
155155

156156
$obj = $this->registered[ $handle ];
157+
if ( $obj->extra['conditional'] ?? false ) {
157158

159+
return false;
160+
}
158161
if ( null === $obj->ver ) {
159162
$ver = '';
160163
} else {
@@ -165,16 +168,7 @@ public function do_item( $handle, $group = false ) {
165168
$ver = $ver ? $ver . '&amp;' . $this->args[ $handle ] : $this->args[ $handle ];
166169
}
167170

168-
$src = $obj->src;
169-
$ie_conditional_prefix = '';
170-
$ie_conditional_suffix = '';
171-
$conditional = isset( $obj->extra['conditional'] ) ? $obj->extra['conditional'] : '';
172-
173-
if ( $conditional ) {
174-
$ie_conditional_prefix = "<!--[if {$conditional}]>\n";
175-
$ie_conditional_suffix = "<![endif]-->\n";
176-
}
177-
171+
$src = $obj->src;
178172
$inline_style = $this->print_inline_style( $handle, false );
179173

180174
if ( $inline_style ) {
@@ -189,7 +183,7 @@ public function do_item( $handle, $group = false ) {
189183
}
190184

191185
if ( $this->do_concat ) {
192-
if ( $this->in_default_dir( $src ) && ! $conditional && ! isset( $obj->extra['alt'] ) ) {
186+
if ( $this->in_default_dir( $src ) && ! isset( $obj->extra['alt'] ) ) {
193187
$this->concat .= "$handle,";
194188
$this->concat_version .= "$handle$ver";
195189

@@ -279,17 +273,13 @@ public function do_item( $handle, $group = false ) {
279273
}
280274

281275
if ( $this->do_concat ) {
282-
$this->print_html .= $ie_conditional_prefix;
283276
$this->print_html .= $tag;
284277
if ( $inline_style_tag ) {
285278
$this->print_html .= $inline_style_tag;
286279
}
287-
$this->print_html .= $ie_conditional_suffix;
288280
} else {
289-
echo $ie_conditional_prefix;
290281
echo $tag;
291282
$this->print_inline_style( $handle );
292-
echo $ie_conditional_suffix;
293283
}
294284

295285
return true;
@@ -373,6 +363,28 @@ public function print_inline_style( $handle, $display = true ) {
373363
return true;
374364
}
375365

366+
/**
367+
* Overrides the add_data method from WP_Dependencies, to allow unsetting dependencies for conditional styles.
368+
*
369+
* @since 6.9.0
370+
*
371+
* @param string $handle Name of the item. Should be unique.
372+
* @param string $key The data key.
373+
* @param mixed $value The data value.
374+
* @return bool True on success, false on failure.
375+
*/
376+
public function add_data( $handle, $key, $value ) {
377+
if ( ! isset( $this->registered[ $handle ] ) ) {
378+
return false;
379+
}
380+
381+
if ( 'conditional' === $key ) {
382+
$this->registered[ $handle ]->deps = array();
383+
}
384+
385+
return parent::add_data( $handle, $key, $value );
386+
}
387+
376388
/**
377389
* Determines style dependencies.
378390
*

0 commit comments

Comments
 (0)