Skip to content

Code Modernization: Use the null coalescing assignment operator in option.php - #16005

Open
noruzzamans wants to merge 1 commit into
WordPress:trunkfrom
noruzzamans:fix/65823-transient-defaults
Open

noruzzamans wants to merge 1 commit into
WordPress:trunkfrom
noruzzamans:fix/65823-transient-defaults

Conversation

@noruzzamans

Copy link
Copy Markdown
Member

Description

Replaces verbose ! isset() guard blocks that assign default values with the null coalescing assignment operator (??=) in src/wp-includes/option.php:

// 1. In get_transient()
$value ??= get_option( $transient_option );

// 2. In get_site_transient()
$value ??= get_site_option( $transient_option );

Scope

This covers get_transient() and get_site_transient() in src/wp-includes/option.php completely. Both transient functions normalize their fallback retrieval using the exact same idiom, making both functions internally consistent. 2 guard blocks replaced, 6 lines removed for 2 added (-4 net lines).

Why this is safe

  • ??= requires PHP 7.4, which is the minimum supported version for WordPress Core.
  • ??= and ! isset() test the exact same condition, applying the default only when $value is unset or null.
  • Evaluation order and fallback mechanics are 100% preserved.
  • Purely internal code modernization with no architectural or behavioral changes.

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

…tion.php

Replaces verbose `! isset()` guard blocks that assign default values in `get_transient()` and `get_site_transient()` with the null coalescing assignment operator (`??=`) in `src/wp-includes/option.php`:

- `$value ??= get_option( $transient_option );` in `get_transient()`
- `$value ??= get_site_option( $transient_option );` in `get_site_transient()`

**Why this is safe**
- `??=` requires PHP 7.4, which is the minimum supported version for WordPress Core.
- `??=` and `! isset()` test the exact same condition, applying the default only when `$value` is unset or null.
- Assignment order and fallback logic are preserved identically across both transient functions.
- Purely internal code simplification with no behavioral or architectural changes.

Props noruzzaman.
See #65823.
@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.

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

No active review issues are recorded for the transient fallback assignment simplification.

No open issues 🎉

What changed in this PR

Modernizes fallback retrieval in WordPress Core’s transient functions without changing unset-or-null handling.

Changes:

  • Replaces ! isset() assignment guards with ??= in get_transient() and get_site_transient().
  • Preserves conditional fallback calls to get_option() and get_site_option().
File Description
src/​wp-includes/​option.php Simplifies two transient fallback assignments using ??=.

Review effort: Balanced

@noruzzamans
noruzzamans marked this pull request as ready for review November 15, 2026 05:35
@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.

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

Props noruzzaman.

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


Props Bot commands

You can trigger Props Bot actions by commenting on this PR:

  • @propsbot add <username> to give props to a GitHub or WordPress.org user. Requires write access.
  • @propsbot remove <username> to leave a user out of the props. Requires write access.
  • @propsbot refresh to refresh this list. Adding the props-bot label works too.

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