Skip to content

Code Modernization: Use the null coalescing assignment operator in comment.php - #16004

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

noruzzamans wants to merge 1 commit into
WordPress:trunkfrom
noruzzamans:fix/65823-comments-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/comment.php:

// 1. Pagination and threading defaults in get_comment_pages_count()
$per_page ??= (int) get_query_var( 'comments_per_page' );
$threaded ??= get_option( 'thread_comments' );

// 2. Author IP and user agent defaults in wp_new_comment()
$commentdata['comment_author_IP'] ??= $_SERVER['REMOTE_ADDR'];
$commentdata['comment_agent']     ??= $_SERVER['HTTP_USER_AGENT'] ?? '';

Scope

This covers get_comment_pages_count() and wp_new_comment() in src/wp-includes/comment.php completely. All changed guards are simple default assignments to tested variables/keys, reducing boilerplate while keeping the function logic identical. 4 guard blocks replaced, 13 lines removed for 4 added (-9 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, so default values are applied in identical scenarios.
  • Pre-existing inline coalescing ($_SERVER['HTTP_USER_AGENT'] ?? '') is naturally complemented.
  • Assignment evaluation order is fully preserved.
  • Purely internal code simplification with no behavioral or architectural changes.

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

…mment.php

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

- `$per_page ??= (int) get_query_var( 'comments_per_page' );`
- `$threaded ??= get_option( 'thread_comments' );`
- `$commentdata['comment_author_IP'] ??= $_SERVER['REMOTE_ADDR'];`
- `$commentdata['comment_agent']     ??= $_SERVER['HTTP_USER_AGENT'] ?? '';`

**Why this is safe**
- `??=` requires PHP 7.4, which is the minimum supported version for WordPress Core.
- `??=` and `! isset()` evaluate the exact same condition, applying the default only when the variable or key is null or unset.
- Assignment order is preserved.
- No behavioral changes; purely internal code simplification.

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 reported for the comment-default assignment simplification.

No open issues 🎉

What changed in this PR

Modernizes default assignments in WordPress Core’s comment handling by replacing four ! isset() guard blocks with ??=, preserving existing behavior.

Changes:

  • Simplifies pagination and threading defaults in get_comment_pages_count().
  • Simplifies author IP and user agent defaults in wp_new_comment().
File Description
src/​wp-includes/​comment.php Replaces four default-assignment guards with null coalescing assignments.

Review effort: Balanced

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