Code Modernization: Use array_all() in various places.#12260
Conversation
Replace `foreach` loops that return `false` on the first element failing a condition (and `true` otherwise) with the more expressive `array_all()` function. `array_all()` was introduced in PHP 8.4; a polyfill has been available in core since WordPress 6.8.0 (see `wp-includes/compat.php`), so this is safe on all supported PHP versions.
This comment was marked as outdated.
This comment was marked as outdated.
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
There was a problem hiding this comment.
Pull request overview
Modernizes a few core utilities by replacing early-return foreach loops with array_all() (using WordPress’s existing polyfill in wp-includes/compat.php) to improve readability while preserving behavior across supported PHP versions.
Changes:
- Refactors REST value-equality checks to use
array_all()for array comparisons. - Refactors
wp_is_numeric_array()to usearray_all()instead of a manual key scan. - Refactors
WP_User::has_cap()“must have all caps” check to usearray_all().
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/wp-includes/rest-api.php | Uses array_all() to verify key presence and deep equality across arrays. |
| src/wp-includes/functions.php | Uses array_all() to ensure all array keys are non-strings for numeric-indexed arrays. |
| src/wp-includes/class-wp-user.php | Uses array_all() to confirm all requested capabilities are present. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| } | ||
|
|
||
| return true; | ||
| return array_all( (array) $caps, fn( $cap ) => ! empty( $capabilities[ $cap ] ) ); |
Replaces several
foreachloops that iterate an array, returnfalseas soon as an element fails a condition, and otherwise fall through totrue. This is exactly what PHP 8.4'sarray_all()expresses in a single, more readable call.Core already ships an
array_all()polyfill inwp-includes/compat.php(since 6.8.0), so the change works on every supported PHP version without raising the minimum requirement.Trac ticket: https://core.trac.wordpress.org/ticket/65519
This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.