Skip to content

Code Modernization: Use array_all() in various places.#12260

Closed
Soean wants to merge 1 commit into
WordPress:trunkfrom
Soean:try/array_all
Closed

Code Modernization: Use array_all() in various places.#12260
Soean wants to merge 1 commit into
WordPress:trunkfrom
Soean:try/array_all

Conversation

@Soean

@Soean Soean commented Jun 22, 2026

Copy link
Copy Markdown
Member

Replaces several foreach loops that iterate an array, return false as soon as an element fails a condition, and otherwise fall through to true. This is exactly what PHP 8.4's array_all() expresses in a single, more readable call.

Core already ships an array_all() polyfill in wp-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.

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.
@github-actions

This comment was marked as outdated.

@github-actions

Copy link
Copy Markdown

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 props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props soean.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@github-actions

Copy link
Copy Markdown

Test using WordPress Playground

The 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

  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

@mukeshpanchal27 mukeshpanchal27 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice 🚀

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 use array_all() instead of a manual key scan.
  • Refactors WP_User::has_cap() “must have all caps” check to use array_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 ] ) );
@Soean

Soean commented Jun 25, 2026

Copy link
Copy Markdown
Member Author

@Soean Soean closed this Jun 25, 2026
@Soean Soean deleted the try/array_all branch June 25, 2026 05:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants