#54751 closed defect (bug) (fixed)
Invalid argument in class-wp-list-util.php
Reported by: | marv2 | Owned by: | peterwilsoncc |
---|---|---|---|
Milestone: | 6.0 | Priority: | normal |
Severity: | normal | Version: | 3.1 |
Component: | General | Keywords: | good-first-bug has-patch has-unit-tests |
Focuses: | Cc: |
Description
I've been getting this error since v5.8.2.
Seems to be coming from WP core.
PHP Warning: Invalid argument supplied for foreach() in /htdocs/wp-includes/class-wp-list-util.php on line 164
Attachments (2)
Change History (16)
#1
@
3 years ago
- Keywords good-first-bug needs-patch needs-unit-tests added
- Milestone changed from Awaiting Review to 6.0
- Version changed from 5.8.2 to 3.1
#2
@
3 years ago
Hello to everybody!
I'am using WordPress a lot and now there is the time for me to give something back. Found this ticket from 'good-first-bug'.
So this would be my first patch and I tried to follow all the contribution guides and the recommendations of @davidbaumwald.
I added some more test as suggested, too.
Maybe someone e.g. @davidbaumwald can check this patch.
#3
@
3 years ago
Thanks for the patch and for including a unit test!
This looks like a great start, but the patch seems reversed: it shows the added lines as removed, and vice versa. Once that is corrected, it should be easier to continue the review.
#4
@
3 years ago
- Keywords has-patch has-unit-tests needs-refresh added; needs-patch needs-unit-tests removed
This ticket was mentioned in PR #2492 on WordPress/wordpress-develop by dd32.
3 years ago
#8
This matches the behaviour of wp_filter_object_list()
, wp_list_filter()
, and wp_list_sort()
. wp_list_pluck()
was the only function related to WP_List_Util
that doesn't validate the input.
Trac Ticket: https://core.trac.wordpress.org/ticket/54751
#9
@
3 years ago
PR2492 is an alternative, simpler take on this.
Simply validating the input values, before passing invalid data to WP_List_Util
, and matches the other utility functions.
The benefit to taking this direction is that existing 'invalid' uses of wp_list_pluck()
won't need to do anything to gain the benefit.
I've not included a unit test, as the above patches and #55300 cover it appropriately.
#10
@
3 years ago
- Owner set to peterwilsoncc
- Resolution set to fixed
- Status changed from new to closed
In 53045:
peterwilsoncc commented on PR #2492:
3 years ago
#11
Committed in https://core.trac.wordpress.org/changeset/53045
Also, best to create patches in your own fork. I think it sometimes creates work for the meta folk or something.
3 years ago
#12
best to create patches in your own fork. I think it sometimes creates work for the meta folk or something.
😱 Well that was a total accident! I've gotten too used to clicking edit on GitHub and it opening in my own fork..
#13
@
3 years ago
Should a condition be added to see if the array is empty? The error below was encountered when the array was empty:
Warning: Invalid argument supplied for foreach() in /code/wp-includes/class-wp-list-util.php on line 165
Or should I open a new ticket?
#14
@
3 years ago
@RMarks I'm unable to reproduce this on the trunk branch that includes this fix. The function returns an empty array, I tried various combinations:
wp> wp_list_pluck( [], 'bob' ); => : array(0) { } wp_list_pluck( [[],[],[]], 'bob' ); => : array(3) { [0] => NULL [1] => NULL [2] => NULL } wp> wp_list_pluck( [(object)[],(object)[],(object)[]], 'bob' ); => : array(3) { [0] => NULL [1] => NULL [2] => NULL }
If you're able to reproduce the error on trunk, a new ticket would be better to allow this ticket to remain focused on one bug.
@marv2 Welcome to WordPress Core Trac, and thanks for the ticket!
This can happen when using the
wp_list_pluck
function which, in the underlyingWP_List_Util
class expects the list to be an array.I think adding some checks before iterating over the array in
WP_List_Util->pluck
to ensure the list is an actual array would be nice to get rid of this warning. Perhaps throwing adoing_it_wrong
is also needed?Also, going to tag this for
needs-unit-tests
. It would be good to have some "unhpappy path" tests to check some invalid types sent to thepluck
method.