Skip to content

Code Modernization: Use array_any() and array_all() in more places - #14618

Open
Soean wants to merge 1 commit into
WordPress:trunkfrom
Soean:code-modernization/array-any-array-all
Open

Soean wants to merge 1 commit into
WordPress:trunkfrom
Soean:code-modernization/array-any-array-all

Conversation

@Soean

@Soean Soean commented Oct 21, 2026

Copy link
Copy Markdown
Member

Replaces foreach loops that return true on the first matching element, or false on the first failing one, with array_any() and array_all(). All of these loops were added after the first round of conversions in r62550 and r62553.

Why this is safe

  • array_any() and array_all() short-circuit exactly like the loops they replace, and return false and true respectively for an empty array, which matches the loops' fall-through values.
  • Every iterated value is an array, so the array parameter type of the functions cannot throw a TypeError: a cast (array), a list<string> return type, a class constant, headers_list(), or an array literal.
  • The negated conditions in the array_all() callbacks are written out positively, for example ! is_string( $value ) || '' === $value becomes is_string( $value ) && '' !== $value. The operands are evaluated in the same order and short-circuit in the same places, so trim() is still only called on strings.
  • WordPress core includes polyfills for both functions on PHP < 8.4 as of WordPress 6.8, so this works on every supported PHP version.
  • No behavior change.

Follow-up to r59783, r62550, r62553, r62564, r63453.

Trac ticket: https://core.trac.wordpress.org/ticket/65818


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.

This replaces `foreach` loops that return `true` on the first matching element, or `false` on the first failing one, with `array_any()` and `array_all()`. All of them were added after the first round of conversions in [62550] and [62553].

The negated conditions in the `array_all()` callbacks are written out positively, for example `! is_string( $value ) || '' === $value` becomes `is_string( $value ) && '' !== $value`, so the operands are still evaluated in the same order and short-circuit in the same places.

WordPress core includes polyfills for `array_any()` and `array_all()` on PHP < 8.4 as of WordPress 6.8, so the change works on every supported PHP version without raising the minimum requirement.

Follow-up to [59783], [62550], [62553], [62564], [63453].

See #65818.
@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.

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.

🟢 Approval recommended

The conversions preserve existing behavior and PHP compatibility, with no unresolved findings.

No open issues 🎉

What changed in this PR

Modernizes seven boolean checks across WordPress core without changing behavior.

Changes:

  • Replaces early-return loops with array_any() or array_all().
  • Preserves short-circuit evaluation and empty-array results, using existing compatibility polyfills.
File Description
src/​wp-includes/​secrets/​class-wp-secrets-config-key-provider.php Uses array_all() to validate salt values.
src/​wp-includes/​media.php Uses array_any() to detect isolation headers.
src/​wp-includes/​class-wp-rewrite.php Uses array_any() to detect random-base conflicts.
src/​wp-includes/​abilities/​class-wp-abilities-content.php Converts status permission and cache-priming checks.
src/​wp-includes/​abilities-api/​class-wp-ability.php Uses array_all() to validate capability strings.
src/​wp-admin/​includes/​plugin.php Uses array_any() to check restricted submenu entries.

Review effort: Balanced

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

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.

2 participants