Make WordPress Core


Ignore:
Timestamp:
11/30/2025 12:54:26 AM (5 months ago)
Author:
westonruter
Message:

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

Developed in https://github.com/WordPress/wordpress-develop/pull/10545

Follow-up to [60999].

Props deepakprajapati, westonruter.
See #63486.
Fixes #64229.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-script-modules.php

    r61073 r61323  
    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 7.0.0
     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: Comma-separated 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( ', ', $missing_dependencies )
     746                    ),
     747                    '7.0.0'
     748                );
     749                $this->modules_with_missing_dependencies[] = $id;
     750            }
     751
    726752            return false;
    727753        }
Note: See TracChangeset for help on using the changeset viewer.