WordPress.org

Make WordPress Core

Ticket #35956: 35956_sort.diff

File 35956_sort.diff, 1.4 KB (added by gitlost, 6 years ago)

Demo sort of dependencies by group when recursing in all_deps().

  • src/wp-includes/class.wp-scripts.php

     
    356356         * @return bool True on success, false on failure.
    357357         */
    358358        public function all_deps( $handles, $recursion = false, $group = false ) {
     359
     360                if ( $recursion ) { // Need to go from highest group to lowest.
     361                        $this->usort_deps = $handles; // Used to make sort stable in usort_deps_cb() (optional but nice).
     362                        usort( $handles, array( $this, 'usort_deps_cb' ) );
     363                }
     364
    359365                $r = parent::all_deps( $handles, $recursion );
    360366                if ( ! $recursion ) {
    361367                        /**
     
    420426                $this->ext_version = '';
    421427                $this->ext_handles = '';
    422428        }
     429
     430        /**
     431         * Callback for usort to reverse sort dependency handles by group.
     432         */
     433        protected function usort_deps_cb( $a, $b ) {
     434                $a_grp = isset( $this->registered[$a]->args ) && $this->registered[$a]->args === 1 ? 1 : (int) $this->get_data( $a, 'group' );
     435                $b_grp = isset( $this->registered[$b]->args ) && $this->registered[$b]->args === 1 ? 1 : (int) $this->get_data( $b, 'group' );
     436                if ( $a_grp === $b_grp && isset( $this->usort_deps ) ) { // If available...
     437                        return array_search( $a, $this->usort_deps ) - array_search( $b, $this->usort_deps ); // ...make sort stable.
     438                }
     439                return $b_grp - $a_grp; // Reverse.
     440        }
    423441}