Opened 5 years ago
Last modified 4 weeks ago
#55467 reopened defect (bug)
WP_REST_Meta_Fields::update_meta_value handles update_metadata return value incorrectly
| Reported by: | wordpressscyt | Owned by: | |
|---|---|---|---|
| Priority: | normal | Milestone: | Awaiting Review |
| Component: | Options, Meta APIs | Version: | 5.9 |
| Severity: | normal | Keywords: | has-patch has-unit-tests |
| Cc: | Focuses: | rest-api |
Description
In the WP_REST_Meta_Fields::update_meta_value method the update_metadata function is used and handles every "false" return value from this function as an error.
update_metadata returns false if the value is not updated. That also happens if the old and new value are the same. Which is obviously not an error.
Change History (8)
#1
@
5 years ago
- Component General → Options, Meta APIs
- Focuses rest-api added
- Keywords needs-patch added
- Milestone Awaiting Review → Future Release
#3
in reply to: ↑ 2
@
2 years ago
- Resolution invalid
- Status closed → reopened
Replying to TimothyBlynJacobs:
Hi @wordpressscyt,
Welcome to trac! Yes, you are correct that
update_metadatacan returnfalsein those instances which is why we first check whether the stored value is equal to the proposed new value before calling the function in the previous lines of theWP_REST_Meta_Fields::update_meta_valuemethod.
So I've been chasing a bug that has lead me to also find this issue as reported above, and upon closer look, I believe there is a possible issue here, and at minimum an opportunity to DRY up the code a little more.
Even though update_meta_value says // Do the exact same check for a duplicate value as in update_metadata() to avoid update_metadata() returning false. in it, it does not appear to be truly "exact". Where update_meta_value uses get_metadata, update_metadata uses get_metadata_raw. update_metadata as calls wp_unslash on $meta_value beforehand. Admittedly, I am having trouble holding it all in my head and walking through the these processes side by side, and it may be fine, but I cannot tell. Whether or not there is an issue here, it would be prudent for these functions to call upon the same DRY function, that would call and check the meta values. This would truly be "the exact same check".
Note: the meta value that is being checked and/or updated in my bug hunting instance appears to only be an integer 0 or 1. It's a value stored by a plugin, and when a post is updated it is not saved because of error returning from update_post_meta being called. I'm curious if the get_metadata_raw and get_metadata have a discrepancy in this case. If this helps in any way.
#4
@
19 months ago
Hi @shanemac10,
I tried to replicate your issue on my end, and it seems like the WP core is working correctly to me. I have checked the codebase for the get_metadata() and get_metadata_raw(), the get_metadata() itself is calling get_metadata_raw(), so there will be no difference in the value added by the user and stored in the DB if those are the same.
About the wp_unslash(), the WP core itself handles the values between wp_slash() and wp_unslash(), so the output will always be in a format that is needed.
@SergeyBiryukov @TimothyBlynJacobs Do you guys suggest any next steps on this ticket?
#6
@
4 weeks ago
Reproduced on trunk (7.2-alpha-63166-src), PHP 8.3, MariaDB 10.11.
The original report is covered by the pre-check in update_meta_value(), and since 6.7
that pre-check uses get_metadata_raw(), so the get_metadata() concern in comment:3
no longer applies. However, there is a reproducible failure when a single registered
key has more than one row holding the submitted value.
Steps:
- Register the meta (mu-plugin):
add_action( 'init', function () { register_post_meta( 'post', 'repro_flag', array( 'type' => 'integer', 'single' => true, 'show_in_rest' => true, 'auth_callback' => '__return_true', ) ); } );
- Create a post with two identical rows:
wp post create --post_title=repro --post_status=publish --porcelain # e.g. 42 wp post meta add 42 repro_flag 1 wp post meta add 42 repro_flag 1
- Update via REST with the same value:
curl -u admin:APP_PASSWORD -X POST 'http://example.test/wp-json/wp/v2/posts/42' \ -H 'Content-Type: application/json' -d '{"meta":{"repro_flag":1}}'
Expected: 200 (value unchanged).
Actual: 500 rest_meta_database_error "Could not update the meta value of repro_flag in database."
Sending a different value (0) returns 200. One row with the same value returns 200.
Cause: both the REST pre-check (1 === count( $old_value )) and update_metadata()'s own
comparison (count( $old_value ) === 1) only run when exactly one row exists. With two
rows, update_metadata() runs the UPDATE, it affects 0 rows, $wpdb->update() returns 0,
update_metadata() returns false, and update_meta_value() reports a database error.
Duplicate rows are common when plugins use add_post_meta() without $unique, which may
match the symptom in comment:3. Suggest retitling to:
"REST API: Updating single meta returns 500 when duplicate rows already hold the submitted value"
Happy to work on a patch with unit tests.
This ticket was mentioned in PR #14505 on WordPress/wordpress-develop by @irozum.
4 weeks ago
#7
- Keywords has-patch has-unit-tests added; needs-patch removed
Updating a single registered meta key through the REST API returns a 500 rest_meta_database_error when the object has more than one row for that key and the submitted value equals the stored value. The existing "unchanged value" pre-check in WP_REST_Meta_Fields::update_meta_value() only runs when exactly one row exists, so with duplicate rows update_metadata() runs an UPDATE that affects 0 rows and returns false, which is reported as a database error.
Changes
- Treat the value as unchanged when every stored row already matches the submitted value, via a new
are_all_stored_values_same()helper. - Add a REST post meta test covering two identical rows; it returns 500 without the fix and 200 with it.
Use of AI Tools
AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Sonnet 5.5
Used for: Implementation, tests, and PR description.
---
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.
#8
@
4 weeks ago
Test Report
Description
This report validates whether the indicated patch works as expected.
Patch tested: https://github.com/WordPress/wordpress-develop/pull/14505
Environment
- WordPress: 7.2-alpha-63166-src (trunk + PR 14505)
- PHP: 8.3.35
- Database: MySQL 9.7
- Environment: wordpress-develop local Docker environment, PHPUnit
- OS: Windows 11
Actual Results
- ✅ Issue resolved with patch.
The PR's new test (test_update_value_return_success_with_same_value_in_duplicate_rows) returns 500 on trunk and 200 with the patch. All 152 tests in WP_Test_REST_Post_Meta_Fields pass with the patch.
Additional Notes
I also tried some cases the PR's test doesn't cover. These were throwaway tests, not part of the PR:
| Case | Trunk | With patch |
|---|---|---|
Integer meta stored as '5' twice, 5 submitted | 500 | 200 |
| Term meta (category) with two identical rows | 500 | 200 |
| Three identical rows | 500 | 200 |
Two rows a and b, a submitted | 200 | 200, both rows become a
|
| Two identical rows, a new value submitted | 200 | 200, both rows updated |
So the fix also covers term meta, which goes through the same WP_REST_Meta_Fields::update_meta_value(), and integer values that need type casting. The last two cases show that real updates still behave as before.
A possible follow-up for the PR: the integer case could be added to the test, since it exercises the sanitizing in is_meta_value_same_as_stored_value().
The PR passes PHPCS and merges cleanly with current trunk.
AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 5.5
Used for: Writing and running the edge-case tests and drafting this report. I reviewed the results.
Hi @wordpressscyt,
Welcome to trac! Yes, you are correct that
update_metadatacan returnfalsein those instances which is why we first check whether the stored value is equal to the proposed new value before calling the function in the previous lines of theWP_REST_Meta_Fields::update_meta_valuemethod.