Skip to content
Closed
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions src/wp-includes/class-wp-dependencies.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,31 @@ public function all_deps( $handles, $recursion = false, $group = false ) {
continue;
}

$keep_going = true;
$keep_going = true;
$missing_dependencies = array();
if ( isset( $this->registered[ $handle ] ) && $this->registered[ $handle ]->deps ) {
Comment thread
deepakpra marked this conversation as resolved.
Outdated
$missing_dependencies = array_diff( $this->registered[ $handle ]->deps, array_keys( $this->registered ) );
}
if ( ! isset( $this->registered[ $handle ] ) ) {
$keep_going = false; // Item doesn't exist.
} elseif ( $this->registered[ $handle ]->deps && array_diff( $this->registered[ $handle ]->deps, array_keys( $this->registered ) ) ) {
} elseif ( $missing_dependencies ) {
Comment thread
deepakpra marked this conversation as resolved.
Outdated
// Prevent duplicate notices.
static $reported = array();

if ( ! isset( $reported[ $handle ] ) ) {
$reported[ $handle ] = true;
Comment thread
deepakpra marked this conversation as resolved.
Outdated

_doing_it_wrong(
__METHOD__,
sprintf(
/* translators: 1: Script module ID, 2: Comma-separated list of missing dependency IDs. */
__( 'The script with the handle %1$s was enqueued with dependencies that are not registered: %2$s.' ),
Comment thread
deepakpra marked this conversation as resolved.
Outdated
$handle,
implode( ', ', $missing_dependencies )
),
'7.0.0'
);
}
$keep_going = false; // Item requires dependencies that don't exist.
} elseif ( $this->registered[ $handle ]->deps && ! $this->all_deps( $this->registered[ $handle ]->deps, true, $new_group ) ) {
$keep_going = false; // Item requires dependencies that don't exist.
Expand Down
21 changes: 20 additions & 1 deletion src/wp-includes/class-wp-script-modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -722,7 +722,26 @@ private function sort_item_dependencies( string $id, array $import_types, array
}

// If the item requires dependencies that do not exist, fail.
if ( count( array_diff( $dependency_ids, array_keys( $this->registered ) ) ) > 0 ) {
$missing_dependencies = array_diff( $dependency_ids, array_keys( $this->registered ) );
if ( count( $missing_dependencies ) > 0 ) {
// Prevent duplicate notices.
static $reported = array();

if ( ! isset( $reported[ $id ] ) ) {
$reported[ $id ] = true;

_doing_it_wrong(
__METHOD__,
sprintf(
/* translators: 1: Script module ID, 2: Comma-separated list of missing dependency IDs. */
__( 'The script module %1$s was enqueued with dependencies that are not registered: %2$s.' ),
$id,
implode( ', ', $missing_dependencies )
),
'7.0.0'
);
}

return false;
}

Expand Down
29 changes: 29 additions & 0 deletions tests/phpunit/tests/dependencies/scripts.php
Original file line number Diff line number Diff line change
Expand Up @@ -4093,4 +4093,33 @@ public function test_print_translations_no_display_no_sourceurl() {
$translations_script_data = $wp_scripts->print_translations( 'test-example', false );
$this->assertStringNotContainsStringIgnoringCase( 'sourceURL=', $translations_script_data );
}

/**
* Tests that WP_Scripts emits a _doing_it_wrong() notice for missing dependencies.
*
* @ticket 64229
* @covers WP_Dependencies::all_deps
*/
public function test_wp_scripts_doing_it_wrong_for_missing_dependencies() {
Comment thread
deepakpra marked this conversation as resolved.
$expected_key = 'WP_Dependencies::all_deps';
$this->setExpectedIncorrectUsage( $expected_key );

wp_register_script( 'registered-dep', '/registered-dep.js' );
wp_register_script( 'main', '/main.js', array( 'registered-dep', 'missing-dep' ) );
wp_enqueue_script( 'main' );

get_echo( 'wp_print_scripts' );

$this->assertArrayHasKey(
$expected_key,
$this->caught_doing_it_wrong,
'Expected WP_Dependencies::all_deps to trigger a _doing_it_wrong() notice for missing dependency.'
);

$this->assertStringContainsString(
'The script with the handle main was enqueued with dependencies that are not registered: missing-dep',
$this->caught_doing_it_wrong[ $expected_key ],
'Expected _doing_it_wrong() notice to indicate missing dependencies for enqueued script.'
);
}
}
30 changes: 30 additions & 0 deletions tests/phpunit/tests/script-modules/wpScriptModules.php
Original file line number Diff line number Diff line change
Expand Up @@ -1965,6 +1965,9 @@ public function test_script_module_printing_and_dependency_ordering( bool $use_g
global $wp_version;
$wp_version = '99.9.9';

if ( $use_global_function && $only_enqueue ) {
$this->setExpectedIncorrectUsage( 'WP_Script_Modules::sort_item_dependencies' );
}
$register = static function ( ...$args ) use ( $use_global_function ) {
if ( $use_global_function ) {
wp_register_script_module( ...$args );
Expand Down Expand Up @@ -2312,4 +2315,31 @@ public function test_static_import_dependency_with_dynamic_imports_depending_on_
"Expected script modules to match snapshot:\n$script_modules"
);
}

/**
* Tests that a missing script module dependency triggers a _doing_it_wrong() notice.
*
* @ticket 64229
* @covers WP_Script_Modules::sort_item_dependencies
*/
public function test_missing_script_module_dependency_triggers_incorrect_usage() {
$this->setExpectedIncorrectUsage( 'WP_Script_Modules::sort_item_dependencies' );

$this->script_modules->register( 'main-module', '/main-module.js', array( 'missing-mod-dep' ) );
$this->script_modules->enqueue( 'main-module' );

get_echo( array( $this->script_modules, 'print_enqueued_script_modules' ) );

$this->assertArrayHasKey(
'WP_Script_Modules::sort_item_dependencies',
$this->caught_doing_it_wrong,
'Expected WP_Script_Modules::sort_item_dependencies to be reported via doing_it_wrong().'
);

// Assert the message mentions the missing dependency handle.
$this->assertStringContainsString(
'The script module main-module was enqueued with dependencies that are not registered: missing-mod-dep',
$this->caught_doing_it_wrong['WP_Script_Modules::sort_item_dependencies']
);
}
}
Loading