Ticket #15016: wp_list_filter.2.diff

File wp_list_filter.2.diff, 2.8 KB (added by scribu, 3 years ago)

Use in WP_Query

Line 
1Index: wp-includes/functions.php
2===================================================================
3--- wp-includes/functions.php   (revision 15685)
4+++ wp-includes/functions.php   (working copy)
5@@ -3019,26 +3019,35 @@
6  * @return array A list of objects or object fields
7  */
8 function wp_filter_object_list( $list, $args = array(), $operator = 'and', $field = false ) {
9-       if ( !is_array($list) )
10-               return array();
11+       $list = wp_list_filter( $list, $args, $operator );
12+       $list = wp_list_pluck( $list, $field );
13 
14-       if ( empty($args) )
15-               $args = array();
16+       return $list;
17+}
18 
19-       if ( empty($args) && !$field )
20-               return $list;   // nothing to do
21+/**
22+ * Filters a list of objects, based on a set of key => value arguments
23+ *
24+ * @since 3.1.0
25+ *
26+ * @param array $list An array of objects to filter
27+ * @param array $args An array of key => value arguments to match against each object
28+ * @param string $operator The logical operation to perform. 'or' means only one element
29+ *     from the array needs to match; 'and' means all elements must match. The default is 'and'.
30+ * @return array
31+ */
32+function wp_list_filter( $list, $args = array(), $operator = 'and' ) {
33+       if ( empty( $args ) )
34+               return $list;
35 
36-       $count = count($args);
37+       $count = count( $args );
38 
39        $filtered = array();
40 
41        foreach ( $list as $key => $obj ) {
42-               $matched = count( array_intersect_assoc( (array) ($obj), $args ) );
43+               $matched = count( array_intersect_assoc( (array) $obj, $args ) );
44                if ( ('and' == $operator && $matched == $count) || ('or' == $operator && $matched <= $count) ) {
45-                       if ( $field )
46-                               $filtered[] = $obj->$field;
47-                       else
48-                               $filtered[$key] = $obj;
49+                       $filtered[$key] = $obj;
50                }
51        }
52 
53@@ -3046,6 +3055,24 @@
54 }
55 
56 /**
57+ * Pluck a certain field out of each object in a list
58+ *
59+ * @since 3.1.0
60+ *
61+ * @param array $list A list of objects or arrays
62+ * @param int|string $field A field from the object to place instead of the entire object
63+ * @return array
64+ */
65+function wp_list_pluck( $list, $field ) {
66+       foreach ( $list as $key => $value ) {
67+               $value = (array) $value;
68+               $list[ $key ] = $value[ $field ];
69+       }
70+
71+       return $list;
72+}
73+
74+/**
75  * Determines if default embed handlers should be loaded.
76  *
77  * Checks to make sure that the embeds library hasn't already been loaded. If
78Index: wp-includes/query.php
79===================================================================
80--- wp-includes/query.php       (revision 15685)
81+++ wp-includes/query.php       (working copy)
82@@ -1951,7 +1951,7 @@
83 
84                        // Back-compat
85                        if ( !empty( $ids ) ) {
86-                               $cat_query = wp_filter_object_list( $tax_query, array( 'taxonomy' => 'category' ) );
87+                               $cat_query = wp_list_filter( $tax_query, array( 'taxonomy' => 'category' ) );
88                                if ( !empty( $cat_query ) ) {
89                                        $cat_query = reset( $cat_query );
90                                        $cat = get_term_by( $cat_query['field'], $cat_query['terms'][0], 'category' );