Make WordPress Core

Ticket #37128: 37128.2.diff

File 37128.2.diff, 11.3 KB (added by flixos90, 9 years ago)
  • src/wp-includes/class-wp-list-util.php

     
     1<?php
     2/**
     3 * WordPress List utility class
     4 *
     5 * @package WordPress
     6 * @since 4.7.0
     7 */
     8
     9/**
     10 * List utility.
     11 *
     12 * Utility class to handle operations on an array of objects.
     13 *
     14 * @since 4.7.0
     15 */
     16class WP_List_Util {
     17        /**
     18         * The input array.
     19         *
     20         * @since 4.7.0
     21         * @access private
     22         * @var array
     23         */
     24        private $input = array();
     25
     26        /**
     27         * The output array.
     28         *
     29         * @since 4.7.0
     30         * @access private
     31         * @var array
     32         */
     33        private $output = array();
     34
     35        /**
     36         * Temporary arguments for sorting.
     37         *
     38         * @since 4.7.0
     39         * @access private
     40         * @var array
     41         */
     42        private $orderby = array();
     43
     44        /**
     45         * Constructor.
     46         *
     47         * Sets the input array.
     48         *
     49         * @since 4.7.0
     50         *
     51         * @param array $input Array to perform operations on.
     52         */
     53        public function __construct( $input ) {
     54                $this->output = $this->input = $input;
     55        }
     56
     57        /**
     58         * Returns the original input array.
     59         *
     60         * @since 4.7.0
     61         * @access public
     62         *
     63         * @return array The input array.
     64         */
     65        public function get_input() {
     66                return $this->input;
     67        }
     68
     69        /**
     70         * Returns the output array.
     71         *
     72         * @since 4.7.0
     73         * @access public
     74         *
     75         * @return array The output array.
     76         */
     77        public function get_output() {
     78                return $this->output;
     79        }
     80
     81        /**
     82         * Filters the list, based on a set of key => value arguments.
     83         *
     84         * @since 4.7.0
     85         *
     86         * @param array  $args     Optional. An array of key => value arguments to match
     87         *                         against each object. Default empty array.
     88         * @param string $operator Optional. The logical operation to perform. 'AND' means
     89         *                         all elements from the array must match. 'OR' means only
     90         *                         one element needs to match. 'NOT' means no elements may
     91         *                         match. Default 'AND'.
     92         * @return array Array of found values.
     93         */
     94        public function filter( $args = array(), $operator = 'AND' ) {
     95                if ( empty( $args ) ) {
     96                        return $this->output;
     97                }
     98
     99                $operator = strtoupper( $operator );
     100                $count = count( $args );
     101                $filtered = array();
     102
     103                foreach ( $this->output as $key => $obj ) {
     104                        $to_match = (array) $obj;
     105
     106                        $matched = 0;
     107                        foreach ( $args as $m_key => $m_value ) {
     108                                if ( array_key_exists( $m_key, $to_match ) && $m_value == $to_match[ $m_key ] ) {
     109                                        $matched++;
     110                                }
     111                        }
     112
     113                        if ( ( 'AND' == $operator && $matched == $count )
     114                          || ( 'OR' == $operator && $matched > 0 )
     115                          || ( 'NOT' == $operator && 0 == $matched ) ) {
     116                                $filtered[$key] = $obj;
     117                        }
     118                }
     119
     120                $this->output = $filtered;
     121
     122                return $this->output;
     123        }
     124
     125        /**
     126         * Plucks a certain field out of each object in the list.
     127         *
     128         * This has the same functionality and prototype of
     129         * array_column() (PHP 5.5) but also supports objects.
     130         *
     131         * @since 4.7.0
     132         *
     133         * @param int|string $field     Field from the object to place instead of the entire object
     134         * @param int|string $index_key Optional. Field from the object to use as keys for the new array.
     135         *                              Default null.
     136         * @return array Array of found values. If `$index_key` is set, an array of found values with keys
     137         *               corresponding to `$index_key`. If `$index_key` is null, array keys from the original
     138         *               `$list` will be preserved in the results.
     139         */
     140        public function pluck( $field, $index_key = null ) {
     141                if ( ! $index_key ) {
     142                        /*
     143                         * This is simple. Could at some point wrap array_column()
     144                         * if we knew we had an array of arrays.
     145                         */
     146                        foreach ( $this->output as $key => $value ) {
     147                                if ( is_object( $value ) ) {
     148                                        $this->output[ $key ] = $value->$field;
     149                                } else {
     150                                        $this->output[ $key ] = $value[ $field ];
     151                                }
     152                        }
     153                        return $this->output;
     154                }
     155
     156                /*
     157                 * When index_key is not set for a particular item, push the value
     158                 * to the end of the stack. This is how array_column() behaves.
     159                 */
     160                $newlist = array();
     161                foreach ( $this->output as $value ) {
     162                        if ( is_object( $value ) ) {
     163                                if ( isset( $value->$index_key ) ) {
     164                                        $newlist[ $value->$index_key ] = $value->$field;
     165                                } else {
     166                                        $newlist[] = $value->$field;
     167                                }
     168                        } else {
     169                                if ( isset( $value[ $index_key ] ) ) {
     170                                        $newlist[ $value[ $index_key ] ] = $value[ $field ];
     171                                } else {
     172                                        $newlist[] = $value[ $field ];
     173                                }
     174                        }
     175                }
     176
     177                $this->output = $newlist;
     178
     179                return $this->output;
     180        }
     181
     182        /**
     183         * Sorts the list, based on one or more orderby arguments.
     184         *
     185         * @since 4.7.0
     186         *
     187         * @param string|array $orderby Optional. Either the field name to order by or an array
     188         *                              of multiple orderby fields as $orderby => $order.
     189         * @param string       $order   Optional. Either 'ASC' or 'DESC'. Only used if $orderby
     190         *                              is a string.
     191         * @return array The sorted array.
     192         */
     193        public function sort( $orderby = array(), $order = 'ASC' ) {
     194                if ( empty( $orderby ) ) {
     195                        return $this->output;
     196                }
     197
     198                if ( is_string( $orderby ) ) {
     199                        $orderby = array( $orderby => $order );
     200                }
     201
     202                foreach ( $orderby as $field => $direction ) {
     203                        $orderby[ $field ] = 'DESC' === strtoupper( $direction ) ? 'DESC' : 'ASC';
     204                }
     205
     206                $this->orderby = $orderby;
     207
     208                usort( $this->output, array( $this, 'sort_callback' ) );
     209
     210                $this->orderby = array();
     211
     212                return $this->output;
     213        }
     214
     215        /**
     216         * Callback to sort the list by specific fields.
     217         *
     218         * @since 4.7.0
     219         * @access private
     220         *
     221         * @see WP_List_Util::sort()
     222         *
     223         * @param object|array $a One object to compare.
     224         * @param object|array $b The other object to compare.
     225         * @return int 0 if both objects equal. -1 if second object should come first, 1 otherwise.
     226         */
     227        private function sort_callback( $a, $b ) {
     228                if ( empty( $this->orderby ) ) {
     229                        return 0;
     230                }
     231
     232                $a = (array) $a;
     233                $b = (array) $b;
     234
     235                foreach ( $this->orderby as $field => $direction ) {
     236                        if ( ! isset( $a[ $field ] ) || ! isset( $b[ $field ] ) ) {
     237                                continue;
     238                        }
     239
     240                        if ( $a[ $field ] == $b[ $field ] ) {
     241                                continue;
     242                        }
     243
     244                        $results = 'DESC' === $direction ? array( 1, -1 ) : array( -1, 1 );
     245
     246                        if ( is_numeric( $a[ $field ] ) && is_numeric( $b[ $field ] ) ) {
     247                                return ( $a[ $field ] < $b[ $field ] ) ? $results[0] : $results[1];
     248                        }
     249
     250                        return 0 > strcmp( $a[ $field ], $b[ $field ] ) ? $results[0] : $results[1];
     251                }
     252
     253                return 0;
     254        }
     255}
  • src/wp-includes/functions.php

     
    34433443 * Filters a list of objects, based on a set of key => value arguments.
    34443444 *
    34453445 * @since 3.0.0
     3446 * @since 4.7.0 Uses WP_List_Util class.
    34463447 *
    34473448 * @param array       $list     An array of objects to filter
    34483449 * @param array       $args     Optional. An array of key => value arguments to match
     
    34563457 * @return array A list of objects or object fields.
    34573458 */
    34583459function wp_filter_object_list( $list, $args = array(), $operator = 'and', $field = false ) {
    3459         if ( ! is_array( $list ) )
     3460        if ( ! is_array( $list ) ) {
    34603461                return array();
     3462        }
    34613463
    3462         $list = wp_list_filter( $list, $args, $operator );
     3464        $util = new WP_List_Util( $list );
    34633465
    3464         if ( $field )
    3465                 $list = wp_list_pluck( $list, $field );
     3466        $util->filter( $args, $operator );
    34663467
    3467         return $list;
     3468        if ( $field ) {
     3469                $util->pluck( $field );
     3470        }
     3471
     3472        return $util->get_output();
    34683473}
    34693474
    34703475/**
    34713476 * Filters a list of objects, based on a set of key => value arguments.
    34723477 *
    34733478 * @since 3.1.0
     3479 * @since 4.7.0 Uses WP_List_Util class.
    34743480 *
    34753481 * @param array  $list     An array of objects to filter.
    34763482 * @param array  $args     Optional. An array of key => value arguments to match
     
    34823488 * @return array Array of found values.
    34833489 */
    34843490function wp_list_filter( $list, $args = array(), $operator = 'AND' ) {
    3485         if ( ! is_array( $list ) )
     3491        if ( ! is_array( $list ) ) {
    34863492                return array();
    3487 
    3488         if ( empty( $args ) )
    3489                 return $list;
    3490 
    3491         $operator = strtoupper( $operator );
    3492         $count = count( $args );
    3493         $filtered = array();
    3494 
    3495         foreach ( $list as $key => $obj ) {
    3496                 $to_match = (array) $obj;
    3497 
    3498                 $matched = 0;
    3499                 foreach ( $args as $m_key => $m_value ) {
    3500                         if ( array_key_exists( $m_key, $to_match ) && $m_value == $to_match[ $m_key ] )
    3501                                 $matched++;
    3502                 }
    3503 
    3504                 if ( ( 'AND' == $operator && $matched == $count )
    3505                   || ( 'OR' == $operator && $matched > 0 )
    3506                   || ( 'NOT' == $operator && 0 == $matched ) ) {
    3507                         $filtered[$key] = $obj;
    3508                 }
    35093493        }
    35103494
    3511         return $filtered;
     3495        $util = new WP_List_Util( $list );
     3496        return $util->filter( $args, $operator );
    35123497}
    35133498
    35143499/**
     
    35193504 *
    35203505 * @since 3.1.0
    35213506 * @since 4.0.0 $index_key parameter added.
     3507 * @since 4.7.0 Uses WP_List_Util class.
    35223508 *
    35233509 * @param array      $list      List of objects or arrays
    35243510 * @param int|string $field     Field from the object to place instead of the entire object
     
    35293515 *               `$list` will be preserved in the results.
    35303516 */
    35313517function wp_list_pluck( $list, $field, $index_key = null ) {
    3532         if ( ! $index_key ) {
    3533                 /*
    3534                  * This is simple. Could at some point wrap array_column()
    3535                  * if we knew we had an array of arrays.
    3536                  */
    3537                 foreach ( $list as $key => $value ) {
    3538                         if ( is_object( $value ) ) {
    3539                                 $list[ $key ] = $value->$field;
    3540                         } else {
    3541                                 $list[ $key ] = $value[ $field ];
    3542                         }
    3543                 }
    3544                 return $list;
    3545         }
     3518        $util = new WP_List_Util( $list );
     3519        return $util->pluck( $field, $index_key );
     3520}
    35463521
    3547         /*
    3548          * When index_key is not set for a particular item, push the value
    3549          * to the end of the stack. This is how array_column() behaves.
    3550          */
    3551         $newlist = array();
    3552         foreach ( $list as $value ) {
    3553                 if ( is_object( $value ) ) {
    3554                         if ( isset( $value->$index_key ) ) {
    3555                                 $newlist[ $value->$index_key ] = $value->$field;
    3556                         } else {
    3557                                 $newlist[] = $value->$field;
    3558                         }
    3559                 } else {
    3560                         if ( isset( $value[ $index_key ] ) ) {
    3561                                 $newlist[ $value[ $index_key ] ] = $value[ $field ];
    3562                         } else {
    3563                                 $newlist[] = $value[ $field ];
    3564                         }
    3565                 }
     3522/**
     3523 * Sorts a list of objects, based on one or more orderby arguments.
     3524 *
     3525 * @since 4.7.0
     3526 *
     3527 * @param array        $list    An array of objects to filter.
     3528 * @param string|array $orderby Optional. Either the field name to order by or an array
     3529 *                              of multiple orderby fields as $orderby => $order.
     3530 * @param string       $order   Optional. Either 'ASC' or 'DESC'. Only used if $orderby
     3531 *                              is a string.
     3532 * @return array The sorted array.
     3533 */
     3534function wp_list_sort( $list, $orderby = array(), $order = 'ASC' ) {
     3535        if ( ! is_array( $list ) ) {
     3536                return array();
    35663537        }
    35673538
    3568         return $newlist;
     3539        $util = new WP_List_Util( $list );
     3540        return $util->sort( $orderby, $order );
    35693541}
    35703542
    35713543/**
  • src/wp-settings.php

     
    9191
    9292// Load early WordPress files.
    9393require( ABSPATH . WPINC . '/compat.php' );
     94require( ABSPATH . WPINC . '/class-wp-list-util.php' );
    9495require( ABSPATH . WPINC . '/functions.php' );
    9596require( ABSPATH . WPINC . '/class-wp.php' );
    9697require( ABSPATH . WPINC . '/class-wp-error.php' );