#16137 closed defect (bug) (fixed)
wp_list_filter() is inexact when dealing with nested arrays
| Reported by: |
|
Owned by: |
|
|---|---|---|---|
| Priority: | normal | Milestone: | 3.3 |
| Component: | General | Version: | |
| Severity: | normal | Keywords: | has-patch |
| Cc: | mark@… |
Description (last modified by scribu)
To reproduce:
add_action('init', 'test_admin_notices', 11);
function test_admin_notices() {
global $wp_taxonomies;
print_r( wp_filter_object_list( array_values( $wp_taxonomies ), array( 'object_type' => array( 'post' ) ), 'and', 'object_type' ) );
}
This is because array_intersect_assoc() does a strict comparison: (string) $elem1 === (string) $elem2
Using a manual loop fixes the problem.
Attachments (6)
Change History (23)
comment:3
mark8barnes
— 2 years ago
- Cc mark@… added
I just came across this problem. The bug breaks get_taxonomies( $args, $output, $operator ) when $args = array('object_type' => array($custom_post_type)), but the patch fixes it. Can we get this in 3.2?
SergeyBiryukov
— 23 months ago
comment:4
SergeyBiryukov
— 23 months ago
- Keywords needs-refresh 3.2-early removed
- Milestone changed from Future Release to 3.3
comment:5
follow-up:
↓ 7
scribu
— 23 months ago
While we're at it, we should use get_object_vars() instead of casting to array, in case the object has non-public properties that we don't care about.
comment:7
in reply to:
↑ 5
SergeyBiryukov
— 23 months ago
Replying to scribu:
While we're at it, we should use get_object_vars() instead of casting to array
Done.
SergeyBiryukov
— 23 months ago
comment:10
scribu
— 23 months ago
- Keywords needs-unit-tests added; commit removed
comment:11
nacin
— 23 months ago
Performance benchmark?
comment:12
ampt
— 22 months ago
add unit tests, i'm not exactly certain of the correct behaviour and cases for test_filter_object_list_nested_array_or and also test_filter_object_list_or_nested_array_or_field, any guidance would be appreciated. any other cases required?
comment:13
scribu
— 22 months ago
It seems using a foreach is actually faster than array_intersect_assoc().
Using wplf-test.php I get:
wp_list_filter_old: 1.6865890026093 wp_list_filter_new: 1.3801169395447 wp_list_filter_new2: 1.871533870697
wp_list_filter_new is the one in 16137.patch.
wp_list_filter_new2 uses an if + get_object_vars(). Although I made this suggestion, I think it's better to just use casting to arrays.
comment:14
ampt
— 22 months ago
Fix typo in function names
comment:15
ryan
— 22 months ago
- Owner set to ryan
- Resolution set to fixed
- Status changed from new to closed
In [18606]:
comment:17
SergeyBiryukov
— 20 months ago
- Keywords needs-unit-tests removed
Context: #14084