General: Introduce a `wp_list_sort()` helper function.
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. Props flixos90, DrewAPicture, jorbin. Fixes #37128. Built from https://develop.svn.wordpress.org/trunk@38859 git-svn-id: http://core.svn.wordpress.org/trunk@38802 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
parent
7d9563801a
commit
f54558e75b
|
@ -99,45 +99,6 @@ function get_the_category( $id = false ) {
|
|||
return apply_filters( 'get_the_categories', $categories, $id );
|
||||
}
|
||||
|
||||
/**
|
||||
* Sort categories by name.
|
||||
*
|
||||
* Used by usort() as a callback, should not be used directly. Can actually be
|
||||
* used to sort any term object.
|
||||
*
|
||||
* @since 2.3.0
|
||||
* @access private
|
||||
*
|
||||
* @param object $a
|
||||
* @param object $b
|
||||
* @return int
|
||||
*/
|
||||
function _usort_terms_by_name( $a, $b ) {
|
||||
return strcmp( $a->name, $b->name );
|
||||
}
|
||||
|
||||
/**
|
||||
* Sort categories by ID.
|
||||
*
|
||||
* Used by usort() as a callback, should not be used directly. Can actually be
|
||||
* used to sort any term object.
|
||||
*
|
||||
* @since 2.3.0
|
||||
* @access private
|
||||
*
|
||||
* @param object $a
|
||||
* @param object $b
|
||||
* @return int
|
||||
*/
|
||||
function _usort_terms_by_ID( $a, $b ) {
|
||||
if ( $a->term_id > $b->term_id )
|
||||
return 1;
|
||||
elseif ( $a->term_id < $b->term_id )
|
||||
return -1;
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve category name based on category ID.
|
||||
*
|
||||
|
|
|
@ -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',
|
||||
) );
|
||||
|
||||
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',
|
||||
) );
|
||||
$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',
|
||||
) );
|
||||
$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',
|
||||
) );
|
||||
$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',
|
||||
) );
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -0,0 +1,260 @@
|
|||
<?php
|
||||
/**
|
||||
* WordPress List utility class
|
||||
*
|
||||
* @package WordPress
|
||||
* @since 4.7.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* List utility.
|
||||
*
|
||||
* Utility class to handle operations on an array of objects.
|
||||
*
|
||||
* @since 4.7.0
|
||||
*/
|
||||
class WP_List_Util {
|
||||
/**
|
||||
* The input array.
|
||||
*
|
||||
* @since 4.7.0
|
||||
* @access private
|
||||
* @var array
|
||||
*/
|
||||
private $input = array();
|
||||
|
||||
/**
|
||||
* The output array.
|
||||
*
|
||||
* @since 4.7.0
|
||||
* @access private
|
||||
* @var array
|
||||
*/
|
||||
private $output = array();
|
||||
|
||||
/**
|
||||
* Temporary arguments for sorting.
|
||||
*
|
||||
* @since 4.7.0
|
||||
* @access private
|
||||
* @var array
|
||||
*/
|
||||
private $orderby = array();
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* Sets the input array.
|
||||
*
|
||||
* @since 4.7.0
|
||||
*
|
||||
* @param array $input Array to perform operations on.
|
||||
*/
|
||||
public function __construct( $input ) {
|
||||
$this->output = $this->input = $input;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the original input array.
|
||||
*
|
||||
* @since 4.7.0
|
||||
* @access public
|
||||
*
|
||||
* @return array The input array.
|
||||
*/
|
||||
public function get_input() {
|
||||
return $this->input;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the output array.
|
||||
*
|
||||
* @since 4.7.0
|
||||
* @access public
|
||||
*
|
||||
* @return array The output array.
|
||||
*/
|
||||
public function get_output() {
|
||||
return $this->output;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters the list, based on a set of key => value arguments.
|
||||
*
|
||||
* @since 4.7.0
|
||||
*
|
||||
* @param array $args Optional. An array of key => value arguments to match
|
||||
* against each object. Default empty array.
|
||||
* @param string $operator Optional. The logical operation to perform. 'AND' means
|
||||
* all elements from the array must match. 'OR' means only
|
||||
* one element needs to match. 'NOT' means no elements may
|
||||
* match. Default 'AND'.
|
||||
* @return array Array of found values.
|
||||
*/
|
||||
public function filter( $args = array(), $operator = 'AND' ) {
|
||||
if ( empty( $args ) ) {
|
||||
return $this->output;
|
||||
}
|
||||
|
||||
$operator = strtoupper( $operator );
|
||||
|
||||
if ( ! in_array( $operator, array( 'AND', 'OR', 'NOT' ), true ) ) {
|
||||
return array();
|
||||
}
|
||||
|
||||
$count = count( $args );
|
||||
$filtered = array();
|
||||
|
||||
foreach ( $this->output as $key => $obj ) {
|
||||
$to_match = (array) $obj;
|
||||
|
||||
$matched = 0;
|
||||
foreach ( $args as $m_key => $m_value ) {
|
||||
if ( array_key_exists( $m_key, $to_match ) && $m_value == $to_match[ $m_key ] ) {
|
||||
$matched++;
|
||||
}
|
||||
}
|
||||
|
||||
if ( ( 'AND' == $operator && $matched == $count )
|
||||
|| ( 'OR' == $operator && $matched > 0 )
|
||||
|| ( 'NOT' == $operator && 0 == $matched ) ) {
|
||||
$filtered[$key] = $obj;
|
||||
}
|
||||
}
|
||||
|
||||
$this->output = $filtered;
|
||||
|
||||
return $this->output;
|
||||
}
|
||||
|
||||
/**
|
||||
* Plucks a certain field out of each object in the list.
|
||||
*
|
||||
* This has the same functionality and prototype of
|
||||
* array_column() (PHP 5.5) but also supports objects.
|
||||
*
|
||||
* @since 4.7.0
|
||||
*
|
||||
* @param int|string $field Field from the object to place instead of the entire object
|
||||
* @param int|string $index_key Optional. Field from the object to use as keys for the new array.
|
||||
* Default null.
|
||||
* @return array Array of found values. If `$index_key` is set, an array of found values with keys
|
||||
* corresponding to `$index_key`. If `$index_key` is null, array keys from the original
|
||||
* `$list` will be preserved in the results.
|
||||
*/
|
||||
public function pluck( $field, $index_key = null ) {
|
||||
if ( ! $index_key ) {
|
||||
/*
|
||||
* This is simple. Could at some point wrap array_column()
|
||||
* if we knew we had an array of arrays.
|
||||
*/
|
||||
foreach ( $this->output as $key => $value ) {
|
||||
if ( is_object( $value ) ) {
|
||||
$this->output[ $key ] = $value->$field;
|
||||
} else {
|
||||
$this->output[ $key ] = $value[ $field ];
|
||||
}
|
||||
}
|
||||
return $this->output;
|
||||
}
|
||||
|
||||
/*
|
||||
* When index_key is not set for a particular item, push the value
|
||||
* to the end of the stack. This is how array_column() behaves.
|
||||
*/
|
||||
$newlist = array();
|
||||
foreach ( $this->output as $value ) {
|
||||
if ( is_object( $value ) ) {
|
||||
if ( isset( $value->$index_key ) ) {
|
||||
$newlist[ $value->$index_key ] = $value->$field;
|
||||
} else {
|
||||
$newlist[] = $value->$field;
|
||||
}
|
||||
} else {
|
||||
if ( isset( $value[ $index_key ] ) ) {
|
||||
$newlist[ $value[ $index_key ] ] = $value[ $field ];
|
||||
} else {
|
||||
$newlist[] = $value[ $field ];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$this->output = $newlist;
|
||||
|
||||
return $this->output;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sorts the list, based on one or more orderby arguments.
|
||||
*
|
||||
* @since 4.7.0
|
||||
*
|
||||
* @param string|array $orderby Optional. Either the field name to order by or an array
|
||||
* of multiple orderby fields as $orderby => $order.
|
||||
* @param string $order Optional. Either 'ASC' or 'DESC'. Only used if $orderby
|
||||
* is a string.
|
||||
* @return array The sorted array.
|
||||
*/
|
||||
public function sort( $orderby = array(), $order = 'ASC' ) {
|
||||
if ( empty( $orderby ) ) {
|
||||
return $this->output;
|
||||
}
|
||||
|
||||
if ( is_string( $orderby ) ) {
|
||||
$orderby = array( $orderby => $order );
|
||||
}
|
||||
|
||||
foreach ( $orderby as $field => $direction ) {
|
||||
$orderby[ $field ] = 'DESC' === strtoupper( $direction ) ? 'DESC' : 'ASC';
|
||||
}
|
||||
|
||||
$this->orderby = $orderby;
|
||||
|
||||
usort( $this->output, array( $this, 'sort_callback' ) );
|
||||
|
||||
$this->orderby = array();
|
||||
|
||||
return $this->output;
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback to sort the list by specific fields.
|
||||
*
|
||||
* @since 4.7.0
|
||||
* @access private
|
||||
*
|
||||
* @see WP_List_Util::sort()
|
||||
*
|
||||
* @param object|array $a One object to compare.
|
||||
* @param object|array $b The other object to compare.
|
||||
* @return int 0 if both objects equal. -1 if second object should come first, 1 otherwise.
|
||||
*/
|
||||
private function sort_callback( $a, $b ) {
|
||||
if ( empty( $this->orderby ) ) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
$a = (array) $a;
|
||||
$b = (array) $b;
|
||||
|
||||
foreach ( $this->orderby as $field => $direction ) {
|
||||
if ( ! isset( $a[ $field ] ) || ! isset( $b[ $field ] ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ( $a[ $field ] == $b[ $field ] ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$results = 'DESC' === $direction ? array( 1, -1 ) : array( -1, 1 );
|
||||
|
||||
if ( is_numeric( $a[ $field ] ) && is_numeric( $b[ $field ] ) ) {
|
||||
return ( $a[ $field ] < $b[ $field ] ) ? $results[0] : $results[1];
|
||||
}
|
||||
|
||||
return 0 > strcmp( $a[ $field ], $b[ $field ] ) ? $results[0] : $results[1];
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
|
@ -532,8 +532,9 @@ class WP_Customize_Nav_Menu_Item_Setting extends WP_Customize_Setting {
|
|||
}
|
||||
|
||||
if ( ARRAY_A === $args['output'] ) {
|
||||
$GLOBALS['_menu_item_sort_prop'] = $args['output_key'];
|
||||
usort( $items, '_sort_nav_menu_items' );
|
||||
$items = wp_list_sort( $items, array(
|
||||
$args['output_key'] => 'ASC',
|
||||
) );
|
||||
$i = 1;
|
||||
|
||||
foreach ( $items as $k => $item ) {
|
||||
|
|
|
@ -287,8 +287,9 @@ class WP_Customize_Nav_Menu_Setting extends WP_Customize_Setting {
|
|||
|
||||
// Make sure the menu objects get re-sorted after an update/insert.
|
||||
if ( ! $is_delete && ! empty( $args['orderby'] ) ) {
|
||||
$this->_current_menus_sort_orderby = $args['orderby'];
|
||||
usort( $menus, array( $this, '_sort_menus_by_orderby' ) );
|
||||
$menus = wp_list_sort( $menus, array(
|
||||
$args['orderby'] => 'ASC',
|
||||
) );
|
||||
}
|
||||
// @todo add support for $args['hide_empty'] === true
|
||||
|
||||
|
@ -313,7 +314,9 @@ class WP_Customize_Nav_Menu_Setting extends WP_Customize_Setting {
|
|||
* This is a workaround for a lack of closures.
|
||||
*
|
||||
* @since 4.3.0
|
||||
* @deprecated 4.7.0 Use wp_list_sort()
|
||||
* @access protected
|
||||
*
|
||||
* @param object $menu1
|
||||
* @param object $menu2
|
||||
* @return int
|
||||
|
@ -321,6 +324,8 @@ class WP_Customize_Nav_Menu_Setting extends WP_Customize_Setting {
|
|||
* @see WP_Customize_Nav_Menu_Setting::filter_wp_get_nav_menus()
|
||||
*/
|
||||
protected function _sort_menus_by_orderby( $menu1, $menu2 ) {
|
||||
_deprecated_function( __METHOD__, '4.7.0', 'wp_list_sort' );
|
||||
|
||||
$key = $this->_current_menus_sort_orderby;
|
||||
return strcmp( $menu1->$key, $menu2->$key );
|
||||
}
|
||||
|
|
|
@ -3798,3 +3798,83 @@ function wp_kses_js_entities( $string ) {
|
|||
|
||||
return preg_replace( '%&\s*\{[^}]*(\}\s*;?|$)%', '', $string );
|
||||
}
|
||||
|
||||
/**
|
||||
* Sort categories by ID.
|
||||
*
|
||||
* Used by usort() as a callback, should not be used directly. Can actually be
|
||||
* used to sort any term object.
|
||||
*
|
||||
* @since 2.3.0
|
||||
* @deprecated 4.7.0 Use wp_list_sort()
|
||||
* @access private
|
||||
*
|
||||
* @param object $a
|
||||
* @param object $b
|
||||
* @return int
|
||||
*/
|
||||
function _usort_terms_by_ID( $a, $b ) {
|
||||
_deprecated_function( __FUNCTION__, '4.7.0', 'wp_list_sort' );
|
||||
|
||||
if ( $a->term_id > $b->term_id )
|
||||
return 1;
|
||||
elseif ( $a->term_id < $b->term_id )
|
||||
return -1;
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sort categories by name.
|
||||
*
|
||||
* Used by usort() as a callback, should not be used directly. Can actually be
|
||||
* used to sort any term object.
|
||||
*
|
||||
* @since 2.3.0
|
||||
* @deprecated 4.7.0 Use wp_list_sort()
|
||||
* @access private
|
||||
*
|
||||
* @param object $a
|
||||
* @param object $b
|
||||
* @return int
|
||||
*/
|
||||
function _usort_terms_by_name( $a, $b ) {
|
||||
_deprecated_function( __FUNCTION__, '4.7.0', 'wp_list_sort' );
|
||||
|
||||
return strcmp( $a->name, $b->name );
|
||||
}
|
||||
|
||||
/**
|
||||
* Sort menu items by the desired key.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @deprecated 4.7.0 Use wp_list_sort()
|
||||
* @access private
|
||||
*
|
||||
* @global string $_menu_item_sort_prop
|
||||
*
|
||||
* @param object $a The first object to compare
|
||||
* @param object $b The second object to compare
|
||||
* @return int -1, 0, or 1 if $a is considered to be respectively less than, equal to, or greater than $b.
|
||||
*/
|
||||
function _sort_nav_menu_items( $a, $b ) {
|
||||
global $_menu_item_sort_prop;
|
||||
|
||||
_deprecated_function( __FUNCTION__, '4.7.0', 'wp_list_sort' );
|
||||
|
||||
if ( empty( $_menu_item_sort_prop ) )
|
||||
return 0;
|
||||
|
||||
if ( ! isset( $a->$_menu_item_sort_prop ) || ! isset( $b->$_menu_item_sort_prop ) )
|
||||
return 0;
|
||||
|
||||
$_a = (int) $a->$_menu_item_sort_prop;
|
||||
$_b = (int) $b->$_menu_item_sort_prop;
|
||||
|
||||
if ( $a->$_menu_item_sort_prop == $b->$_menu_item_sort_prop )
|
||||
return 0;
|
||||
elseif ( $_a == $a->$_menu_item_sort_prop && $_b == $b->$_menu_item_sort_prop )
|
||||
return $_a < $_b ? -1 : 1;
|
||||
else
|
||||
return strcmp( $a->$_menu_item_sort_prop, $b->$_menu_item_sort_prop );
|
||||
}
|
||||
|
|
|
@ -3489,6 +3489,7 @@ function wp_is_numeric_array( $data ) {
|
|||
* Filters a list of objects, based on a set of key => value arguments.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 4.7.0 Uses WP_List_Util class.
|
||||
*
|
||||
* @param array $list An array of objects to filter
|
||||
* @param array $args Optional. An array of key => value arguments to match
|
||||
|
@ -3502,21 +3503,26 @@ function wp_is_numeric_array( $data ) {
|
|||
* @return array A list of objects or object fields.
|
||||
*/
|
||||
function wp_filter_object_list( $list, $args = array(), $operator = 'and', $field = false ) {
|
||||
if ( ! is_array( $list ) )
|
||||
if ( ! is_array( $list ) ) {
|
||||
return array();
|
||||
}
|
||||
|
||||
$list = wp_list_filter( $list, $args, $operator );
|
||||
$util = new WP_List_Util( $list );
|
||||
|
||||
if ( $field )
|
||||
$list = wp_list_pluck( $list, $field );
|
||||
$util->filter( $args, $operator );
|
||||
|
||||
return $list;
|
||||
if ( $field ) {
|
||||
$util->pluck( $field );
|
||||
}
|
||||
|
||||
return $util->get_output();
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters a list of objects, based on a set of key => value arguments.
|
||||
*
|
||||
* @since 3.1.0
|
||||
* @since 4.7.0 Uses WP_List_Util class.
|
||||
*
|
||||
* @param array $list An array of objects to filter.
|
||||
* @param array $args Optional. An array of key => value arguments to match
|
||||
|
@ -3528,33 +3534,12 @@ function wp_filter_object_list( $list, $args = array(), $operator = 'and', $fiel
|
|||
* @return array Array of found values.
|
||||
*/
|
||||
function wp_list_filter( $list, $args = array(), $operator = 'AND' ) {
|
||||
if ( ! is_array( $list ) )
|
||||
if ( ! is_array( $list ) ) {
|
||||
return array();
|
||||
|
||||
if ( empty( $args ) )
|
||||
return $list;
|
||||
|
||||
$operator = strtoupper( $operator );
|
||||
$count = count( $args );
|
||||
$filtered = array();
|
||||
|
||||
foreach ( $list as $key => $obj ) {
|
||||
$to_match = (array) $obj;
|
||||
|
||||
$matched = 0;
|
||||
foreach ( $args as $m_key => $m_value ) {
|
||||
if ( array_key_exists( $m_key, $to_match ) && $m_value == $to_match[ $m_key ] )
|
||||
$matched++;
|
||||
}
|
||||
|
||||
if ( ( 'AND' == $operator && $matched == $count )
|
||||
|| ( 'OR' == $operator && $matched > 0 )
|
||||
|| ( 'NOT' == $operator && 0 == $matched ) ) {
|
||||
$filtered[$key] = $obj;
|
||||
}
|
||||
}
|
||||
|
||||
return $filtered;
|
||||
$util = new WP_List_Util( $list );
|
||||
return $util->filter( $args, $operator );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -3565,6 +3550,7 @@ function wp_list_filter( $list, $args = array(), $operator = 'AND' ) {
|
|||
*
|
||||
* @since 3.1.0
|
||||
* @since 4.0.0 $index_key parameter added.
|
||||
* @since 4.7.0 Uses WP_List_Util class.
|
||||
*
|
||||
* @param array $list List of objects or arrays
|
||||
* @param int|string $field Field from the object to place instead of the entire object
|
||||
|
@ -3575,43 +3561,29 @@ function wp_list_filter( $list, $args = array(), $operator = 'AND' ) {
|
|||
* `$list` will be preserved in the results.
|
||||
*/
|
||||
function wp_list_pluck( $list, $field, $index_key = null ) {
|
||||
if ( ! $index_key ) {
|
||||
/*
|
||||
* This is simple. Could at some point wrap array_column()
|
||||
* if we knew we had an array of arrays.
|
||||
*/
|
||||
foreach ( $list as $key => $value ) {
|
||||
if ( is_object( $value ) ) {
|
||||
$list[ $key ] = $value->$field;
|
||||
} else {
|
||||
$list[ $key ] = $value[ $field ];
|
||||
}
|
||||
}
|
||||
return $list;
|
||||
$util = new WP_List_Util( $list );
|
||||
return $util->pluck( $field, $index_key );
|
||||
}
|
||||
|
||||
/**
|
||||
* Sorts a list of objects, based on one or more orderby arguments.
|
||||
*
|
||||
* @since 4.7.0
|
||||
*
|
||||
* @param array $list An array of objects to filter.
|
||||
* @param string|array $orderby Optional. Either the field name to order by or an array
|
||||
* of multiple orderby fields as $orderby => $order.
|
||||
* @param string $order Optional. Either 'ASC' or 'DESC'. Only used if $orderby
|
||||
* is a string.
|
||||
* @return array The sorted array.
|
||||
*/
|
||||
function wp_list_sort( $list, $orderby = array(), $order = 'ASC' ) {
|
||||
if ( ! is_array( $list ) ) {
|
||||
return array();
|
||||
}
|
||||
|
||||
/*
|
||||
* When index_key is not set for a particular item, push the value
|
||||
* to the end of the stack. This is how array_column() behaves.
|
||||
*/
|
||||
$newlist = array();
|
||||
foreach ( $list as $value ) {
|
||||
if ( is_object( $value ) ) {
|
||||
if ( isset( $value->$index_key ) ) {
|
||||
$newlist[ $value->$index_key ] = $value->$field;
|
||||
} else {
|
||||
$newlist[] = $value->$field;
|
||||
}
|
||||
} else {
|
||||
if ( isset( $value[ $index_key ] ) ) {
|
||||
$newlist[ $value[ $index_key ] ] = $value[ $field ];
|
||||
} else {
|
||||
$newlist[] = $value[ $field ];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $newlist;
|
||||
$util = new WP_List_Util( $list );
|
||||
return $util->sort( $orderby, $order );
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -169,7 +169,9 @@ function get_permalink( $post = 0, $leavename = false ) {
|
|||
if ( strpos($permalink, '%category%') !== false ) {
|
||||
$cats = get_the_category($post->ID);
|
||||
if ( $cats ) {
|
||||
usort($cats, '_usort_terms_by_ID'); // order by ID
|
||||
$cats = wp_list_sort( $cats, array(
|
||||
'term_id' => 'ASC',
|
||||
) );
|
||||
|
||||
/**
|
||||
* Filters the category that gets used in the %category% permalink token.
|
||||
|
|
|
@ -557,38 +557,6 @@ function wp_get_nav_menus( $args = array() ) {
|
|||
return apply_filters( 'wp_get_nav_menus', get_terms( 'nav_menu', $args), $args );
|
||||
}
|
||||
|
||||
/**
|
||||
* Sort menu items by the desired key.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @access private
|
||||
*
|
||||
* @global string $_menu_item_sort_prop
|
||||
*
|
||||
* @param object $a The first object to compare
|
||||
* @param object $b The second object to compare
|
||||
* @return int -1, 0, or 1 if $a is considered to be respectively less than, equal to, or greater than $b.
|
||||
*/
|
||||
function _sort_nav_menu_items( $a, $b ) {
|
||||
global $_menu_item_sort_prop;
|
||||
|
||||
if ( empty( $_menu_item_sort_prop ) )
|
||||
return 0;
|
||||
|
||||
if ( ! isset( $a->$_menu_item_sort_prop ) || ! isset( $b->$_menu_item_sort_prop ) )
|
||||
return 0;
|
||||
|
||||
$_a = (int) $a->$_menu_item_sort_prop;
|
||||
$_b = (int) $b->$_menu_item_sort_prop;
|
||||
|
||||
if ( $a->$_menu_item_sort_prop == $b->$_menu_item_sort_prop )
|
||||
return 0;
|
||||
elseif ( $_a == $a->$_menu_item_sort_prop && $_b == $b->$_menu_item_sort_prop )
|
||||
return $_a < $_b ? -1 : 1;
|
||||
else
|
||||
return strcmp( $a->$_menu_item_sort_prop, $b->$_menu_item_sort_prop );
|
||||
}
|
||||
|
||||
/**
|
||||
* Return if a menu item is valid.
|
||||
*
|
||||
|
@ -682,8 +650,9 @@ function wp_get_nav_menu_items( $menu, $args = array() ) {
|
|||
}
|
||||
|
||||
if ( ARRAY_A == $args['output'] ) {
|
||||
$GLOBALS['_menu_item_sort_prop'] = $args['output_key'];
|
||||
usort($items, '_sort_nav_menu_items');
|
||||
$items = wp_list_sort( $items, array(
|
||||
$args['output_key'] => 'ASC',
|
||||
) );
|
||||
$i = 1;
|
||||
foreach ( $items as $k => $item ) {
|
||||
$items[$k]->{$args['output_key']} = $i++;
|
||||
|
@ -776,7 +745,7 @@ function wp_setup_nav_menu_item( $menu_item ) {
|
|||
|
||||
$menu_item->type_label = __( 'Post Type Archive' );
|
||||
$post_content = wp_trim_words( $menu_item->post_content, 200 );
|
||||
$post_type_description = '' == $post_content ? $post_type_description : $post_content;
|
||||
$post_type_description = '' == $post_content ? $post_type_description : $post_content;
|
||||
$menu_item->url = get_post_type_archive_link( $menu_item->object );
|
||||
} elseif ( 'taxonomy' == $menu_item->type ) {
|
||||
$object = get_taxonomy( $menu_item->object );
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
*
|
||||
* @global string $wp_version
|
||||
*/
|
||||
$wp_version = '4.7-alpha-38858';
|
||||
$wp_version = '4.7-alpha-38859';
|
||||
|
||||
/**
|
||||
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
|
||||
|
|
|
@ -91,6 +91,7 @@ wp_set_lang_dir();
|
|||
|
||||
// Load early WordPress files.
|
||||
require( ABSPATH . WPINC . '/compat.php' );
|
||||
require( ABSPATH . WPINC . '/class-wp-list-util.php' );
|
||||
require( ABSPATH . WPINC . '/functions.php' );
|
||||
require( ABSPATH . WPINC . '/class-wp-matchesmapregex.php' );
|
||||
require( ABSPATH . WPINC . '/class-wp.php' );
|
||||
|
|
Loading…
Reference in New Issue