Skip to content

Commit 1070c33

Browse files
committed
Avoid duplicate warnings
1 parent 0d28424 commit 1070c33

File tree

2 files changed

+43
-14
lines changed

2 files changed

+43
-14
lines changed

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

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,18 @@ class WP_Dependencies {
104104
*/
105105
private $queued_before_register = array();
106106

107+
/**
108+
* List of IDs for dependencies encountered which themselves have missing dependencies.
109+
*
110+
* A dependency handle is added to this list when it is discovered to have missing dependencies. At this time, a
111+
* warning is emitted with {@see _doing_it_wrong()}. The handle is then added to this list, so that duplicate
112+
* warnings don't occur.
113+
*
114+
* @since 7.0.0
115+
* @var string[]
116+
*/
117+
private $dependencies_with_missing_dependencies = array();
118+
107119
/**
108120
* Processes the items and dependencies.
109121
*
@@ -207,11 +219,14 @@ public function all_deps( $handles, $recursion = false, $group = false ) {
207219
if ( ! isset( $this->registered[ $handle ] ) ) {
208220
$keep_going = false; // Item doesn't exist.
209221
} elseif ( count( $missing_dependencies ) > 0 ) {
210-
_doing_it_wrong(
211-
__METHOD__,
212-
$this->get_dependency_warning_message( $handle, $missing_dependencies ),
213-
'7.0.0'
214-
);
222+
if ( ! in_array( $handle, $this->dependencies_with_missing_dependencies, true ) ) {
223+
_doing_it_wrong(
224+
__METHOD__,
225+
$this->get_dependency_warning_message( $handle, $missing_dependencies ),
226+
'7.0.0'
227+
);
228+
$this->dependencies_with_missing_dependencies[] = $handle;
229+
}
215230
$keep_going = false; // Item requires dependencies that don't exist.
216231
} elseif ( $this->registered[ $handle ]->deps && ! $this->all_deps( $this->registered[ $handle ]->deps, true, $new_group ) ) {
217232
$keep_going = false; // Item requires dependencies that don't exist.

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

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,17 @@ class WP_Script_Modules {
7070
'high',
7171
);
7272

73+
/**
74+
* List of IDs for script modules encountered which have missing dependencies.
75+
*
76+
* An ID is added to this list when it is discovered to have missing dependencies. At this time, a warning is
77+
* emitted with {@see _doing_it_wrong()}. The ID is then added to this list, so that duplicate warnings don't occur.
78+
*
79+
* @since 7.0.0
80+
* @var string[]
81+
*/
82+
private $modules_with_missing_dependencies = array();
83+
7384
/**
7485
* Registers the script module if no script module with that script module
7586
* identifier has already been registered.
@@ -724,16 +735,19 @@ private function sort_item_dependencies( string $id, array $import_types, array
724735
// If the item requires dependencies that do not exist, fail.
725736
$missing_dependencies = array_diff( $dependency_ids, array_keys( $this->registered ) );
726737
if ( count( $missing_dependencies ) > 0 ) {
727-
_doing_it_wrong(
728-
__METHOD__,
729-
sprintf(
738+
if ( ! in_array( $id, $this->modules_with_missing_dependencies, true ) ) {
739+
_doing_it_wrong(
740+
__METHOD__,
741+
sprintf(
730742
/* translators: 1: Script module ID, 2: Comma-separated list of missing dependency IDs. */
731-
__( 'The script module "%1$s" was enqueued with dependencies that are not registered: %2$s.' ),
732-
$id,
733-
implode( ', ', $missing_dependencies )
734-
),
735-
'7.0.0'
736-
);
743+
__( 'The script module "%1$s" was enqueued with dependencies that are not registered: %2$s.' ),
744+
$id,
745+
implode( ', ', $missing_dependencies )
746+
),
747+
'7.0.0'
748+
);
749+
$this->modules_with_missing_dependencies[] = $id;
750+
}
737751

738752
return false;
739753
}

0 commit comments

Comments
 (0)