Make WordPress Core


Ignore:
Timestamp:
11/30/2025 12:54:26 AM (9 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-dependencies.php

    r61299 r61323  
    104104         */
    105105        private $queued_before_register = array();
     106
     107        /**
     108         * List of handles 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();
    106118
    107119        /**
     
    200212                        }
    201213
    202                         $keep_going = true;
     214                        $keep_going           = true;
     215                        $missing_dependencies = array();
     216                        if ( isset( $this->registered[ $handle ] ) && count( $this->registered[ $handle ]->deps ) > 0 ) {
     217                                $missing_dependencies = array_diff( $this->registered[ $handle ]->deps, array_keys( $this->registered ) );
     218                        }
    203219                        if ( ! isset( $this->registered[ $handle ] ) ) {
    204220                                $keep_going = false; // Item doesn't exist.
    205                         } elseif ( $this->registered[ $handle ]->deps && array_diff( $this->registered[ $handle ]->deps, array_keys( $this->registered ) ) ) {
     221                        } elseif ( count( $missing_dependencies ) > 0 ) {
     222                                if ( ! in_array( $handle, $this->dependencies_with_missing_dependencies, true ) ) {
     223                                        _doing_it_wrong(
     224                                                get_class( $this ) . '::add',
     225                                                $this->get_dependency_warning_message( $handle, $missing_dependencies ),
     226                                                '7.0.0'
     227                                        );
     228                                        $this->dependencies_with_missing_dependencies[] = $handle;
     229                                }
    206230                                $keep_going = false; // Item requires dependencies that don't exist.
    207231                        } elseif ( $this->registered[ $handle ]->deps && ! $this->all_deps( $this->registered[ $handle ]->deps, true, $new_group ) ) {
     
    536560                return 'W/"' . md5( $etag ) . '"';
    537561        }
     562
     563        /**
     564         * Gets a dependency warning message for a handle.
     565         *
     566         * @since 7.0.0
     567         *
     568         * @param string   $handle                     Handle with missing dependencies.
     569         * @param string[] $missing_dependency_handles Missing dependency handles.
     570         * @return string Formatted, localized warning message.
     571         */
     572        protected function get_dependency_warning_message( $handle, $missing_dependency_handles ) {
     573                return sprintf(
     574                        /* translators: 1: Handle, 2: Comma-separated list of missing dependency handles. */
     575                        __( 'The handle "%1$s" was enqueued with dependencies that are not registered: %2$s.' ),
     576                        $handle,
     577                        implode( ', ', $missing_dependency_handles )
     578                );
     579        }
    538580}
Note: See TracChangeset for help on using the changeset viewer.