Ticket #16895: 16895.2.diff
File 16895.2.diff, 4.4 KB (added by , 7 years ago) |
---|
-
src/wp-includes/class-wp-list-util.php
diff --git a/src/wp-includes/class-wp-list-util.php b/src/wp-includes/class-wp-list-util.php index 0fbefd08e4..da2a897987 100644
a b public function filter( $args = array(), $operator = 'AND' ) { 140 140 * `$list` will be preserved in the results. 141 141 */ 142 142 public function pluck( $field, $index_key = null ) { 143 $newlist = array(); 144 143 145 if ( ! $index_key ) { 144 146 /* 145 147 * This is simple. Could at some point wrap array_column() … … public function pluck( $field, $index_key = null ) { 147 149 */ 148 150 foreach ( $this->output as $key => $value ) { 149 151 if ( is_object( $value ) ) { 150 $ this->output[ $key ] = $value->$field;152 $newlist[ $key ] = $value->$field; 151 153 } else { 152 $ this->output[ $key ] = $value[ $field ];154 $newlist[ $key ] = $value[ $field ]; 153 155 } 154 156 } 157 158 $this->output = $newlist; 159 155 160 return $this->output; 156 161 } 157 162 … … public function pluck( $field, $index_key = null ) { 159 164 * When index_key is not set for a particular item, push the value 160 165 * to the end of the stack. This is how array_column() behaves. 161 166 */ 162 $newlist = array();163 167 foreach ( $this->output as $value ) { 164 168 if ( is_object( $value ) ) { 165 169 if ( isset( $value->$index_key ) ) { -
tests/phpunit/tests/functions/listFilter.php
diff --git a/tests/phpunit/tests/functions/listFilter.php b/tests/phpunit/tests/functions/listFilter.php index 9be0a460a0..a736d62b3f 100644
a b function test_filter_object_list_and_field() { 83 83 'field2' => true, 84 84 ), 'AND', 'name' 85 85 ); 86 $this->assertEquals( 2, count( $list ) );87 86 $this->assertEquals( 88 87 array( 89 88 'foo' => 'foo', … … function test_filter_object_list_or_field() { 99 98 'field3' => true, 100 99 ), 'OR', 'name' 101 100 ); 102 $this->assertEquals( 2, count( $list ) );103 101 $this->assertEquals( 104 102 array( 105 103 'foo' => 'foo', … … function test_filter_object_list_not_field() { 115 113 'field3' => true, 116 114 ), 'NOT', 'name' 117 115 ); 118 $this->assertEquals( 1, count( $list ) );119 116 $this->assertEquals( array( 'baz' => 'baz' ), $list ); 120 117 } 121 118 … … function test_wp_list_pluck_mixed_index_key() { 213 210 ); 214 211 } 215 212 213 /** 214 * @ticket 16895 215 */ 216 function test_wp_list_pluck_containing_references() { 217 $ref_list = array( 218 & $this->object_list['foo'], 219 & $this->object_list['bar'], 220 ); 221 222 $this->assertInstanceOf( 'stdClass', $ref_list[0] ); 223 $this->assertInstanceOf( 'stdClass', $ref_list[1] ); 224 225 $list = wp_list_pluck( $ref_list, 'name' ); 226 $this->assertEquals( 227 array( 228 'foo', 229 'bar', 230 ), 231 $list 232 ); 233 234 $this->assertInstanceOf( 'stdClass', $ref_list[0] ); 235 $this->assertInstanceOf( 'stdClass', $ref_list[1] ); 236 } 237 238 /** 239 * @ticket 16895 240 */ 241 function test_wp_list_pluck_containing_references_keys() { 242 $ref_list = array( 243 & $this->object_list['foo'], 244 & $this->object_list['bar'], 245 ); 246 247 $this->assertInstanceOf( 'stdClass', $ref_list[0] ); 248 $this->assertInstanceOf( 'stdClass', $ref_list[1] ); 249 250 $list = wp_list_pluck( $ref_list, 'name', 'id' ); 251 $this->assertEquals( 252 array( 253 'f' => 'foo', 254 'b' => 'bar', 255 ), 256 $list 257 ); 258 259 $this->assertInstanceOf( 'stdClass', $ref_list[0] ); 260 $this->assertInstanceOf( 'stdClass', $ref_list[1] ); 261 } 262 216 263 function test_filter_object_list_nested_array_and() { 217 264 $list = wp_filter_object_list( $this->object_list, array( 'field4' => array( 'blue' ) ), 'AND' ); 218 265 $this->assertEquals( 1, count( $list ) ); … … function test_filter_object_list_nested_array_or_singular() { 247 294 248 295 function test_filter_object_list_nested_array_and_field() { 249 296 $list = wp_filter_object_list( $this->object_list, array( 'field4' => array( 'blue' ) ), 'AND', 'name' ); 250 $this->assertEquals( 1, count( $list ) );251 297 $this->assertEquals( array( 'baz' => 'baz' ), $list ); 252 298 } 253 299 254 300 function test_filter_object_list_nested_array_not_field() { 255 301 $list = wp_filter_object_list( $this->object_list, array( 'field4' => array( 'green' ) ), 'NOT', 'name' ); 256 $this->assertEquals( 2, count( $list ) );257 302 $this->assertEquals( 258 303 array( 259 304 'foo' => 'foo', … … function test_filter_object_list_nested_array_or_field() { 269 314 'field4' => array( 'blue' ), 270 315 ), 'OR', 'name' 271 316 ); 272 $this->assertEquals( 2, count( $list ) );273 317 $this->assertEquals( 274 318 array( 275 319 'foo' => 'foo',