REST API: Fix status-only comment update route never running - #13281
REST API: Fix status-only comment update route never running#13281HasnainAshfaq wants to merge 2 commits into
Conversation
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
|
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 Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Test using WordPress PlaygroundThe 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
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
…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.
Trac ticket: https://core.trac.wordpress.org/ticket/64248
Description
When only
statusis changed via the REST API (PUT /wp/v2/comments/{id}),update_item()is supposed to take a lightweight path throughhandle_status_param()rather than a fullwp_update_comment()call:This path never runs because
prepare_item_for_database()unconditionally setscomment_author_IP— falling back to$_SERVER['REMOTE_ADDR']or'127.0.0.1'— so$prepared_argsis always non-empty.The bug was introduced in #38819 and has existed since 4.7.
Fix
In
update_item(), after callingprepare_item_for_database(), unsetcomment_author_IPandcomment_agentfrom$prepared_argsunless the caller explicitly includedauthor_iporauthor_user_agentin 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-onlyPUTdoes not overwrite the stored IP, agent, or content.test_update_comment_status_only_uses_handle_status_param_path()— verifies thatwp_update_comment_datafilter 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