Skip to content

Commit ad25902

Browse files
committed
General: Introduce a wp_list_sort() helper function, v2.
In addition to `wp_list_filter()` for filtering a list of objects, and `wp_list_pluck()` for plucking a certain field out of each object in a list, this new function can be used for sorting a list of objects by specific fields. These functions are now all contained within the new `WP_List_Util()` class and `wp_list_sort()` is used in various parts of core for sorting lists. This was previously committed in [38859] but got reverted in [38862] and [38863]. To fix the previous issues, `wp_list_sort()` supports now an additional argument to preserve array keys via `uasort()`. Props flixos90, DrewAPicture, jorbin. Fixes #37128. git-svn-id: https://develop.svn.wordpress.org/trunk@38928 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 0c14ff0 commit ad25902

12 files changed

Lines changed: 956 additions & 149 deletions

src/wp-includes/category-template.php

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -99,45 +99,6 @@ function get_the_category( $id = false ) {
9999
return apply_filters( 'get_the_categories', $categories, $id );
100100
}
101101

102-
/**
103-
* Sort categories by name.
104-
*
105-
* Used by usort() as a callback, should not be used directly. Can actually be
106-
* used to sort any term object.
107-
*
108-
* @since 2.3.0
109-
* @access private
110-
*
111-
* @param object $a
112-
* @param object $b
113-
* @return int
114-
*/
115-
function _usort_terms_by_name( $a, $b ) {
116-
return strcmp( $a->name, $b->name );
117-
}
118-
119-
/**
120-
* Sort categories by ID.
121-
*
122-
* Used by usort() as a callback, should not be used directly. Can actually be
123-
* used to sort any term object.
124-
*
125-
* @since 2.3.0
126-
* @access private
127-
*
128-
* @param object $a
129-
* @param object $b
130-
* @return int
131-
*/
132-
function _usort_terms_by_ID( $a, $b ) {
133-
if ( $a->term_id > $b->term_id )
134-
return 1;
135-
elseif ( $a->term_id < $b->term_id )
136-
return -1;
137-
else
138-
return 0;
139-
}
140-
141102
/**
142103
* Retrieve category name based on category ID.
143104
*

src/wp-includes/class-wp-customize-manager.php

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2505,12 +2505,15 @@ public function render_control_templates() {
25052505
* Helper function to compare two objects by priority, ensuring sort stability via instance_number.
25062506
*
25072507
* @since 3.4.0
2508+
* @deprecated 4.7.0 Use wp_list_sort()
25082509
*
25092510
* @param WP_Customize_Panel|WP_Customize_Section|WP_Customize_Control $a Object A.
25102511
* @param WP_Customize_Panel|WP_Customize_Section|WP_Customize_Control $b Object B.
25112512
* @return int
25122513
*/
25132514
protected function _cmp_priority( $a, $b ) {
2515+
_deprecated_function( __METHOD__, '4.7.0', 'wp_list_sort' );
2516+
25142517
if ( $a->priority === $b->priority ) {
25152518
return $a->instance_number - $b->instance_number;
25162519
} else {
@@ -2530,7 +2533,10 @@ protected function _cmp_priority( $a, $b ) {
25302533
public function prepare_controls() {
25312534

25322535
$controls = array();
2533-
uasort( $this->controls, array( $this, '_cmp_priority' ) );
2536+
$this->controls = wp_list_sort( $this->controls, array(
2537+
'priority' => 'ASC',
2538+
'instance_number' => 'ASC',
2539+
), 'ASC', true );
25342540

25352541
foreach ( $this->controls as $id => $control ) {
25362542
if ( ! isset( $this->sections[ $control->section ] ) || ! $control->check_capabilities() ) {
@@ -2543,15 +2549,22 @@ public function prepare_controls() {
25432549
$this->controls = $controls;
25442550

25452551
// Prepare sections.
2546-
uasort( $this->sections, array( $this, '_cmp_priority' ) );
2552+
$this->sections = wp_list_sort( $this->sections, array(
2553+
'priority' => 'ASC',
2554+
'instance_number' => 'ASC',
2555+
), 'ASC', true );
25472556
$sections = array();
25482557

25492558
foreach ( $this->sections as $section ) {
25502559
if ( ! $section->check_capabilities() ) {
25512560
continue;
25522561
}
25532562

2554-
usort( $section->controls, array( $this, '_cmp_priority' ) );
2563+
2564+
$section->controls = wp_list_sort( $section->controls, array(
2565+
'priority' => 'ASC',
2566+
'instance_number' => 'ASC',
2567+
) );
25552568

25562569
if ( ! $section->panel ) {
25572570
// Top-level section.
@@ -2566,22 +2579,31 @@ public function prepare_controls() {
25662579
$this->sections = $sections;
25672580

25682581
// Prepare panels.
2569-
uasort( $this->panels, array( $this, '_cmp_priority' ) );
2582+
$this->panels = wp_list_sort( $this->panels, array(
2583+
'priority' => 'ASC',
2584+
'instance_number' => 'ASC',
2585+
), 'ASC', true );
25702586
$panels = array();
25712587

25722588
foreach ( $this->panels as $panel ) {
25732589
if ( ! $panel->check_capabilities() ) {
25742590
continue;
25752591
}
25762592

2577-
uasort( $panel->sections, array( $this, '_cmp_priority' ) );
2593+
$panel->sections = wp_list_sort( $panel->sections, array(
2594+
'priority' => 'ASC',
2595+
'instance_number' => 'ASC',
2596+
), 'ASC', true );
25782597
$panels[ $panel->id ] = $panel;
25792598
}
25802599
$this->panels = $panels;
25812600

25822601
// Sort panels and top-level sections together.
25832602
$this->containers = array_merge( $this->panels, $this->sections );
2584-
uasort( $this->containers, array( $this, '_cmp_priority' ) );
2603+
$this->containers = wp_list_sort( $this->containers, array(
2604+
'priority' => 'ASC',
2605+
'instance_number' => 'ASC',
2606+
), 'ASC', true );
25852607
}
25862608

25872609
/**

0 commit comments

Comments
 (0)