Skip to content

REST API: Fix status-only comment update route never running - #13281

Open
HasnainAshfaq wants to merge 2 commits into
WordPress:trunkfrom
HasnainAshfaq:feature/64248-rest-api-comment-status-only-update
Open

REST API: Fix status-only comment update route never running#13281
HasnainAshfaq wants to merge 2 commits into
WordPress:trunkfrom
HasnainAshfaq:feature/64248-rest-api-comment-status-only-update

Conversation

@HasnainAshfaq

Copy link
Copy Markdown

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

Description

When only status is changed via the REST API (PUT /wp/v2/comments/{id}), update_item() is supposed to take a lightweight path through handle_status_param() rather than a full wp_update_comment() call:

if ( empty( $prepared_args ) && isset( $request['status'] ) ) {
    // Only the comment status is being changed.
    $change = $this->handle_status_param( $request['status'], $id );
    ...
}

This path never runs because prepare_item_for_database() unconditionally sets comment_author_IP — falling back to $_SERVER['REMOTE_ADDR'] or '127.0.0.1' — so $prepared_args is always non-empty.

The bug was introduced in #38819 and has existed since 4.7.

Fix

In update_item(), after calling prepare_item_for_database(), unset comment_author_IP and comment_agent from $prepared_args unless the caller explicitly included author_ip or author_user_agent in the request body. This restores the intended routing without changing create behaviour.

Testing

Two new PHPUnit tests added to WP_Test_REST_Comments_Controller:

  • test_update_comment_status_only_does_not_overwrite_existing_fields() — verifies that a status-only PUT does not overwrite the stored IP, agent, or content.
  • test_update_comment_status_only_uses_handle_status_param_path() — verifies that wp_update_comment_data filter is not triggered (i.e. wp_update_comment() is not called) on a status-only update.

Use of AI Tools

Generated with Claude Sonnet and adjusted/reviewed manually.

🤖 Generated with Claude Code

When only the comment status is changed via the REST API, update_item()
is supposed to take a lightweight path through handle_status_param()
rather than calling wp_update_comment() for the full record.

prepare_item_for_database() always sets comment_author_IP (falling back
to the request's REMOTE_ADDR or '127.0.0.1') and may set comment_agent
from the User-Agent header, so $prepared_args is never empty and the
status-only branch at "if ( empty( $prepared_args ) && isset( $request['status'] ) )"
can never be reached.

Fix: in update_item(), unset comment_author_IP and comment_agent from
$prepared_args after prepare_item_for_database() unless the caller
explicitly included author_ip or author_user_agent in the request.

Fixes #64248
@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 hasnainashfaq.

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.

…ne entry.

- Align array double-arrow operators in test method.
- Remove the if.alwaysFalse PHPStan baseline entry for
  class-wp-rest-comments-controller.php: the empty( $prepared_args )
  condition that was always false is now reachable after the fix in
  the previous commit.
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.

1 participant