#49339 closed defect (bug) (fixed)
Issue when comparing old and new meta of type integer on update_multi_meta_value
Reported by: |
|
Owned by: |
|
---|---|---|---|
Milestone: | 5.5 | Priority: | normal |
Severity: | normal | Version: | 4.7 |
Component: | REST API | Keywords: | has-patch has-unit-tests commit |
Focuses: | rest-api | Cc: |
Description
Hi there!
I found an issue when updating a meta through JS (REST API). I'm using a plugin sidebar to update a post meta with the type integer
.
When I update the meta value, the WP_REST_Meta_Fields::update_multi_meta_value
method tries to compare the old and the new values to remove and add only the necessary.
But the $current = get_metadata( $meta_type, $object_id, $meta_key, false );
line always return an array
of string
and the $values
is correctly an array
of integer
. So any value doesn't match, and all old post metas are deleted and all new post metas are added.
The line that tries to compare is: $remove_keys = array_keys( $to_remove, $value, true );
Example:
- We have as a post metas:
[ 1, 2 ]
and try to update for[ 2, 3 ]
- It's removing the
1
and2
. And after that it's adding the2
and3
- It should remove the
1
and add the3
Workaround:
As a workaround, I'm thinking to use temporarily get_post_metadata
to return the value correctly (as integer).
The steps to reproduce:
I created a clean repo with the scenario to make it easier to simulate: https://github.com/renatho/meta-type-test-plugin/tree/master
- Clone the repo
- Run
npm i && npm run build
. - Add the plugin to a WP env.
- Go to a post.
- Click on the smile on the top right.
- Change between the options clicking on the buttons and saving the post.
- Check the post comments to see the result.
Change History (7)
This ticket was mentioned in PR #274 on WordPress/wordpress-develop by TimothyBJacobs.
5 years ago
#1
#2
@
5 years ago
- Keywords has-patch has-unit-tests dev-feedback added
- Milestone changed from Awaiting Review to 5.5
- Owner set to TimothyBlynJacobs
- Status changed from new to assigned
- Version changed from 5.4 to 4.7
Hi @renathoc,
Thanks for reporting this and welcome to trac!
As far as I can tell, this is the same underlying cause as #42069 but for multi meta.
I've attached a preliminary patch which applies the same sanitization we use when updating a single value. Needs more unit tests covering the different meta types.
Could you try applying the change to see if it fixes your issue @renathoc?
Looping in @kadamwhite.
#3
@
5 years ago
Hey @TimothyBlynJacobs!
Sure! I tested and it's working with the change. Thank you for fixing!
Trac ticket: https://core.trac.wordpress.org/ticket/49339