Opened 4 years ago
Last modified 5 months ago
#56262 new enhancement
WP_List_Util::pluck doesn't support a null $field
| Reported by: | iandunn | Owned by: | |
|---|---|---|---|
| Priority: | low | Milestone: | Awaiting Review |
| Component: | General | Version: | 3.1 |
| Severity: | minor | Keywords: | has-patch has-unit-tests |
| Cc: | Focuses: |
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 (6)
#4
@
8 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, I am needs-testing and added needs-refresh.
This ticket was mentioned in PR #12048 on WordPress/wordpress-develop by @sukhendu2002.
5 months ago
#5
- Keywords has-unit-tests added; needs-refresh removed
Trac ticket: https://core.trac.wordpress.org/ticket/56262
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)
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.