Opened 7 years ago
Closed 6 years ago
#45121 closed defect (bug) (fixed)
wp_update_post() can modify post tag
| Reported by: |
|
Owned by: |
|
|---|---|---|---|
| Milestone: | 5.4 | Priority: | normal |
| Severity: | normal | Version: | 4.9.8 |
| Component: | Posts, Post Types | Keywords: | has-patch has-unit-tests commit |
| Focuses: | Cc: |
Description
Problem
Update of post by means of wp_update_post() can modify tags assigned to the post.
Steps to reproduce:
Fresh WP install.
Create two tags with the same name (let say wp_update_post_tag) and different slugs (let say wp_update_post_tag_1} and wp_update_post_tag_2, in this sequence).
Create a post with tag wp_update_post_tag_2. Check that it has tag wp_update_post_tag_2.
Use wp_update_post( $post ). Check that now it has tag wp_update_post_tag_1.
This sequence is demonstrated by the plugin https://github.com/kagg-design/update-post-bug.
Reason
Problem is caused by the following lines in wp_insert_post():
if ( isset( $postarr['tags_input'] ) && is_object_in_taxonomy( $post_type, 'post_tag' ) ) {
wp_set_post_tags( $post_ID, $postarr['tags_input'] );
}
At this point, $postarr['tags_input'] already is array( 'wp_update_post_tag' ), containing tag_name, not ID. This is because wp_update_post() executes
// First, get all of the original fields. $post = get_post($postarr['ID'], ARRAY_A);
Here $post gets tags_input as array( 'wp_update_post_tag' ), containing tag_name, not ID.
Attachments (5)
Change History (21)
#2
in reply to:
↑ 1
@
7 years ago
Replying to SergeyBiryukov:
Related: #30615
Yes, it is related, with the only difference that issue is properly fixed in /wp-admin/includes, but not in /wp-includes. I will try to propose similar commit with relevant tests.
#3
@
7 years ago
If several tags with same name exist, and post has one of such tags, then wp_update_post() will modify tag assigned to post.
This is caused by $post = get_post($postarr['ID'], ARRAY_A); in wp_update_post(), which returns 'tags_input' as strings, not ids.
We fix the issue by adding conversion of strings in 'tags_input' to ids. )
@
7 years ago
Improved test, to check situation when 3 tags with the same name exist, and 2 of them are assigned to the post.
#5
@
7 years ago
- Milestone changed from Awaiting Review to 5.2
- Owner set to SergeyBiryukov
- Status changed from new to reviewing
This ticket was mentioned in Slack in #core by audrasjb. View the logs.
7 years ago
This ticket was mentioned in Slack in #core by jeffpaul. View the logs.
7 years ago
#8
@
7 years ago
- Milestone changed from 5.2 to 5.3
As WP 5.2 RC1 is being released today, I'm moving this ticket to 5.3.
This ticket was mentioned in Slack in #core by david.baumwald. View the logs.
7 years ago
#10
@
7 years ago
- Keywords needs-refresh added
This ticket was discussed during today's bug scrub. The most recent patch needs a refresh, and the tests should be in an all-inclusive patch.
#11
@
7 years ago
@davidbaumwald @SergeyBiryukov Please find attached all-inclusive patch wp_update_post.diff with fix by itself and test. Code in the patch is refreshed and applicable to WordPress 5.3-beta1-46286.
Thank you.
This ticket was mentioned in Slack in #core by peterwilsoncc. View the logs.
7 years ago
#13
@
6 years ago
- Milestone changed from 5.3 to 5.4
With 5.3 RC 1 in a few hours and this needing more work, I'm going to punt this.
If someone has the time before RC to properly review and commit, it can be moved back.
#15
@
6 years ago
- Keywords commit added; needs-refresh removed
wp_update_post.diff does solve the reported problem, but it also prevents any modification of post tags via the tags_input parameter, even if it's intentional.
Something along the lines of [31359] seemed like it might work, however in practice it doesn't, since the tags_input value returned by WP_Post::__get() contains tag names, and if they are the same, there's no way to disambiguate them at that point.
I think the correct solution here would be to disregard the tags_input parameter if it's the same as existing post tags. That way tags are only modified if tags_input was explicitly provided, and is different from the existing tags.
See 45121.2.diff.
Related: #30615