Opened 4 years ago
Last modified 2 months ago
#56262 new enhancement
WP_List_Util::pluck doesn't support a null $field
| Reported by: |
|
Owned by: | |
|---|---|---|---|
| Milestone: | Awaiting Review | Priority: | low |
| Severity: | minor | Version: | 3.1 |
| Component: | General | Keywords: | has-patch needs-refresh |
| Focuses: | Cc: |
Description
array_column() accepts a null value for the $column_key parameter. When passed null, it returns the complete array, which is a useful trick for reindexing database results, etc.
$array = [
[ 'id' => 123, 'name' => 'Joe' ],
[ 'id' => 345, 'name' => 'Sally' ]
];
array_column( $array, null, 'id' ); // [ 123 => [ 'id' => 123, 'name' => 'Joe'] ...
wp_list_pluck() doesn't work when using that on an object, though:
Warning: Undefined property: stdClass::$ in wp-includes/class-wp-list-util.php on line 185
It seems like array_column() has had support for objects since PHP7, so one way to achieve this might be something like:
if ( version_compare( ... ) {
return array_column( $this->output, $field, $index_key )
}
// existing code remains as fallback for older versions
It might be good to use something like 7.0.7 to skip a few bugs that were fixed.
Attachments (1)
Change History (5)
#4
@
2 months ago
- Keywords needs-refresh added; needs-testing removed
When I try to apply the patch https://core.trac.wordpress.org/attachment/ticket/56262/56262.diff against the latest trunk 7.0-alpha-61215-src, it fails with the following message:
Running "patch:56262" (patch) task patching file src/wp-includes/class-wp-list-util.php Hunk #1 FAILED at 164. Hunk #2 FAILED at 182. 2 out of 2 hunks FAILED -- saving rejects to file src/wp-includes/class-wp-list-util.php.rej
As the patch is failing to apply, thus removed needs-testing and added needs-refresh.
56262.diff looks like a good start!
Two helpful improvements could be adding unit tests and inline docs (adding a
@since, updating the$fieldparameter description, etc).is_null( $field )might be more explicit / self documenting thanisset(), especially after the$fielddocs are updated to mention thatnullcan be passed.Using
isset()could also unintentionally hide PHP warnings (and therefore bugs) when devs make a typo in the field name, etc.