humanmade / humanmade/network-media-library
Featured image is not saved if the image ID is that of a deleted post ID on the non-media site
- Dominant language
- PHP
- Stars
- 296
- Forks
- 67
- PR merge metrics
- No merged PRs in 30d
Description
## Expected Behaviour
Using the Gutenberg editor, when setting or updating a featured image, it should be saved with the post.
## Current Behaviour
Occasionally the featured image does not get saved with the post. This depends on if the value of the post ID of the image on the media site exists in the non-media site that the featured image is being applied to (for example, a post on the non-media site has been deleted).
This does not happen if the classic editor plugin is being used - the featured image is saved correctly.
## Steps to Reproduce
1. Create a fresh WordPress site, enable multisite and install this plugin (and create the media site)
2. Create a new page or post on the non-media site and publish it (it should have an ID of 3)
3. Add at least 3 items to the media gallery
4. Completely delete the post / page just created (so that it's not just in 'trash')
5. Create a new post / page and set the featured image to be the third (or later) image uploaded
6. Press the publish button
7. The featured image will disappear from the Gutenberg editor
8. Set the featured image to be the first or second image uploaded
9. Press the update button - the featured image will remain visible
10. Set the featured image back to the third (or later) image uploaded
11. Press the update button - the featured image will change back to the previously selected image
## Detailed Description
When a featured image is added from the Gutenberg editor, and the publish / update button is pressed, the editor fires off a REST request to `/wp-json/wp/v2/posts/`, with a request payload containing `featured_media` with the ID of the featured image.
The WordPress REST controller passes this request to the WordPress REST post controller. The [featured image is passed](https://core.trac.wordpress.org/browser/trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php#L577) to the [`handle_featured_media()`](https://core.trac.wordpress.org/browser/trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php#L1164) function. This [function calls](https://core.trac.wordpress.org/browser/trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php#L1177) the [`set_post_thumbnail()`](https://core.trac.wordpress.org/browser/trunk/src/wp-includes/post.php#L6656) function. The `set_post_thumbnail()` function [attemps to get the ID](https://core.trac.wordpress.org/browser/trunk/src/wp-includes/post.php#L6668) of the featured image, but as it doesn't exist on the non-media site `set_post_thumbnail()` returns false, which then causes `handle_featured_media()` to [return a `WP_Error`](https://core.trac.wordpress.org/browser/trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php#L1181). As the ID of the featured image has not been updated in the posts meta `_thumbnail_id`, it either doesn't exist or still points to the previous ID.
## Additional Information
Wordpress 5.1
Network Media Library 1.4.1
This may or may not be related to issues #11 and #45
Contributor guide
No contributing guide indexed for this repository
Research direction
Start by reproducing the Gutenberg save flow described for a multisite with a separate media site. Read class-wp-rest-posts-controller.php around handle_featured_media() and post.php around set_post_thumbnail(), then trace the REST request containing featured_media. Done means a featured image whose ID collides with a deleted post ID on the non-media site remains saved after publishing or updating.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- php
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100