3 * WordPress List utility class
12 * Utility class to handle operations on an array of objects.
24 private $input = array();
33 private $output = array();
36 * Temporary arguments for sorting.
42 private $orderby = array();
47 * Sets the input array.
51 * @param array $input Array to perform operations on.
53 public function __construct( $input ) {
54 $this->output = $this->input = $input;
58 * Returns the original input array.
63 * @return array The input array.
65 public function get_input() {
70 * Returns the output array.
75 * @return array The output array.
77 public function get_output() {
82 * Filters the list, based on a set of key => value arguments.
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.
94 public function filter( $args = array(), $operator = 'AND' ) {
95 if ( empty( $args ) ) {
99 $operator = strtoupper( $operator );
101 if ( ! in_array( $operator, array( 'AND', 'OR', 'NOT' ), true ) ) {
105 $count = count( $args );
108 foreach ( $this->output as $key => $obj ) {
109 $to_match = (array) $obj;
112 foreach ( $args as $m_key => $m_value ) {
113 if ( array_key_exists( $m_key, $to_match ) && $m_value == $to_match[ $m_key ] ) {
119 ( 'AND' == $operator && $matched == $count ) ||
120 ( 'OR' == $operator && $matched > 0 ) ||
121 ( 'NOT' == $operator && 0 == $matched )
123 $filtered[$key] = $obj;
127 $this->output = $filtered;
129 return $this->output;
133 * Plucks a certain field out of each object in the list.
135 * This has the same functionality and prototype of
136 * array_column() (PHP 5.5) but also supports objects.
140 * @param int|string $field Field from the object to place instead of the entire object
141 * @param int|string $index_key Optional. Field from the object to use as keys for the new array.
143 * @return array Array of found values. If `$index_key` is set, an array of found values with keys
144 * corresponding to `$index_key`. If `$index_key` is null, array keys from the original
145 * `$list` will be preserved in the results.
147 public function pluck( $field, $index_key = null ) {
148 if ( ! $index_key ) {
150 * This is simple. Could at some point wrap array_column()
151 * if we knew we had an array of arrays.
153 foreach ( $this->output as $key => $value ) {
154 if ( is_object( $value ) ) {
155 $this->output[ $key ] = $value->$field;
157 $this->output[ $key ] = $value[ $field ];
160 return $this->output;
164 * When index_key is not set for a particular item, push the value
165 * to the end of the stack. This is how array_column() behaves.
168 foreach ( $this->output as $value ) {
169 if ( is_object( $value ) ) {
170 if ( isset( $value->$index_key ) ) {
171 $newlist[ $value->$index_key ] = $value->$field;
173 $newlist[] = $value->$field;
176 if ( isset( $value[ $index_key ] ) ) {
177 $newlist[ $value[ $index_key ] ] = $value[ $field ];
179 $newlist[] = $value[ $field ];
184 $this->output = $newlist;
186 return $this->output;
190 * Sorts the list, based on one or more orderby arguments.
194 * @param string|array $orderby Optional. Either the field name to order by or an array
195 * of multiple orderby fields as $orderby => $order.
196 * @param string $order Optional. Either 'ASC' or 'DESC'. Only used if $orderby
198 * @param bool $preserve_keys Optional. Whether to preserve keys. Default false.
199 * @return array The sorted array.
201 public function sort( $orderby = array(), $order = 'ASC', $preserve_keys = false ) {
202 if ( empty( $orderby ) ) {
203 return $this->output;
206 if ( is_string( $orderby ) ) {
207 $orderby = array( $orderby => $order );
210 foreach ( $orderby as $field => $direction ) {
211 $orderby[ $field ] = 'DESC' === strtoupper( $direction ) ? 'DESC' : 'ASC';
214 $this->orderby = $orderby;
216 if ( $preserve_keys ) {
217 uasort( $this->output, array( $this, 'sort_callback' ) );
219 usort( $this->output, array( $this, 'sort_callback' ) );
222 $this->orderby = array();
224 return $this->output;
228 * Callback to sort the list by specific fields.
233 * @see WP_List_Util::sort()
235 * @param object|array $a One object to compare.
236 * @param object|array $b The other object to compare.
237 * @return int 0 if both objects equal. -1 if second object should come first, 1 otherwise.
239 private function sort_callback( $a, $b ) {
240 if ( empty( $this->orderby ) ) {
247 foreach ( $this->orderby as $field => $direction ) {
248 if ( ! isset( $a[ $field ] ) || ! isset( $b[ $field ] ) ) {
252 if ( $a[ $field ] == $b[ $field ] ) {
256 $results = 'DESC' === $direction ? array( 1, -1 ) : array( -1, 1 );
258 if ( is_numeric( $a[ $field ] ) && is_numeric( $b[ $field ] ) ) {
259 return ( $a[ $field ] < $b[ $field ] ) ? $results[0] : $results[1];
262 return 0 > strcmp( $a[ $field ], $b[ $field ] ) ? $results[0] : $results[1];