Make WordPress Core


Ignore:
Timestamp:
01/28/2026 09:28:06 PM (6 months ago)
Author:
jorbin
Message:

Script Loader: Emit notices when enqueueing a script, style, or script module with missing dependencies.

First Developed in https://github.com/WordPress/wordpress-develop/pull/10545. Backport developed in https://github.com/WordPress/wordpress-develop/pull/10789.

Follow-up to [60999].

Reviewed by jorbin, wildworks.
Merges [61323], [61357], and [61542].

Props deepakprajapati, westonruter, mukeshpanchal27, jorbin, wildworks.
See #63486.
Fixes #64229.

Location:
branches/6.9
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/6.9

  • branches/6.9/src/wp-includes/class-wp-script-modules.php

    r61506 r61550  
    7070                'high',
    7171        );
     72
     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 6.9.1
     80         * @var string[]
     81         */
     82        private $modules_with_missing_dependencies = array();
    7283
    7384        /**
     
    723734
    724735                // If the item requires dependencies that do not exist, fail.
    725                 if ( count( array_diff( $dependency_ids, array_keys( $this->registered ) ) ) > 0 ) {
     736                $missing_dependencies = array_diff( $dependency_ids, array_keys( $this->registered ) );
     737                if ( count( $missing_dependencies ) > 0 ) {
     738                        if ( ! in_array( $id, $this->modules_with_missing_dependencies, true ) ) {
     739                                _doing_it_wrong(
     740                                        get_class( $this ) . '::register',
     741                                        sprintf(
     742                                                /* translators: 1: Script module ID, 2: List of missing dependency IDs. */
     743                                                __( 'The script module with the ID "%1$s" was enqueued with dependencies that are not registered: %2$s.' ),
     744                                                $id,
     745                                                implode( wp_get_list_item_separator(), $missing_dependencies )
     746                                        ),
     747                                        '6.9.1'
     748                                );
     749                                $this->modules_with_missing_dependencies[] = $id;
     750                        }
     751
    726752                        return false;
    727753                }
Note: See TracChangeset for help on using the changeset viewer.