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-dependencies.php

    r60948 r61550  
    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 6.9.1
     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                                                '6.9.1'
     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 6.9.1
     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: List of missing dependency handles. */
     575                        __( 'The handle "%1$s" was enqueued with dependencies that are not registered: %2$s.' ),
     576                        $handle,
     577                        implode( wp_get_list_item_separator(), $missing_dependency_handles )
     578                );
     579        }
    538580}
Note: See TracChangeset for help on using the changeset viewer.