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
This commit is contained in:
Dominik Schilling (ocean90)
2016-10-25 21:25:25 +00:00
parent 0c14ff0574
commit ad25902a65
12 changed files with 957 additions and 150 deletions

View File

@@ -2505,12 +2505,15 @@ final class WP_Customize_Manager {
* Helper function to compare two objects by priority, ensuring sort stability via instance_number.
*
* @since 3.4.0
* @deprecated 4.7.0 Use wp_list_sort()
*
* @param WP_Customize_Panel|WP_Customize_Section|WP_Customize_Control $a Object A.
* @param WP_Customize_Panel|WP_Customize_Section|WP_Customize_Control $b Object B.
* @return int
*/
protected function _cmp_priority( $a, $b ) {
_deprecated_function( __METHOD__, '4.7.0', 'wp_list_sort' );
if ( $a->priority === $b->priority ) {
return $a->instance_number - $b->instance_number;
} else {
@@ -2530,7 +2533,10 @@ final class WP_Customize_Manager {
public function prepare_controls() {
$controls = array();
uasort( $this->controls, array( $this, '_cmp_priority' ) );
$this->controls = wp_list_sort( $this->controls, array(
'priority' => 'ASC',
'instance_number' => 'ASC',
), 'ASC', true );
foreach ( $this->controls as $id => $control ) {
if ( ! isset( $this->sections[ $control->section ] ) || ! $control->check_capabilities() ) {
@@ -2543,7 +2549,10 @@ final class WP_Customize_Manager {
$this->controls = $controls;
// Prepare sections.
uasort( $this->sections, array( $this, '_cmp_priority' ) );
$this->sections = wp_list_sort( $this->sections, array(
'priority' => 'ASC',
'instance_number' => 'ASC',
), 'ASC', true );
$sections = array();
foreach ( $this->sections as $section ) {
@@ -2551,7 +2560,11 @@ final class WP_Customize_Manager {
continue;
}
usort( $section->controls, array( $this, '_cmp_priority' ) );
$section->controls = wp_list_sort( $section->controls, array(
'priority' => 'ASC',
'instance_number' => 'ASC',
) );
if ( ! $section->panel ) {
// Top-level section.
@@ -2566,7 +2579,10 @@ final class WP_Customize_Manager {
$this->sections = $sections;
// Prepare panels.
uasort( $this->panels, array( $this, '_cmp_priority' ) );
$this->panels = wp_list_sort( $this->panels, array(
'priority' => 'ASC',
'instance_number' => 'ASC',
), 'ASC', true );
$panels = array();
foreach ( $this->panels as $panel ) {
@@ -2574,14 +2590,20 @@ final class WP_Customize_Manager {
continue;
}
uasort( $panel->sections, array( $this, '_cmp_priority' ) );
$panel->sections = wp_list_sort( $panel->sections, array(
'priority' => 'ASC',
'instance_number' => 'ASC',
), 'ASC', true );
$panels[ $panel->id ] = $panel;
}
$this->panels = $panels;
// Sort panels and top-level sections together.
$this->containers = array_merge( $this->panels, $this->sections );
uasort( $this->containers, array( $this, '_cmp_priority' ) );
$this->containers = wp_list_sort( $this->containers, array(
'priority' => 'ASC',
'instance_number' => 'ASC',
), 'ASC', true );
}
/**