wp-cli / media-command

Imports files as attachments, regenerates thumbnails, or lists registered image sizes.
MIT License
44 stars 41 forks source link

Offer flag to update post content when regenerating thumbnails #121

Open CodyMcMichael opened 4 years ago

CodyMcMichael commented 4 years ago

Describe the current, buggy behavior

I’m using wp-cli 2.4.0. I recently built a client a new theme and updated the large image size to fit their new theme. I then ran the command wp media regenerate --image_size=large --only-missing to regenerate new large thumbnails. Everything worked as planned. All new images were created, the old were removed, however none of the posts were updated with the new image size. They were all referencing the old images even though they no longer exists

Describe how other contributors can replicate this bug

Update large image size in settings > media run command wp media regenerate --image_size=large --only-missing

Describe what you would expect as the correct outcome

The command would go through all images and regenerate the large image size to the new image size. It should delete the old images and update the posts to which those images were attached with the new image size.

Let us know what environment you are running this on Dev, Staging, Production

genesis-jol (staging)* wp media regenerate 22633 --image_size=large --only-missing
Found 1 image to regenerate.
1/1 Regenerated "large" thumbnail for "Healthy Apple Crisp Recipe (Quick & Easy)-5" (ID 22633).
Success: Regenerated 1 of 1 images.
ChrisHardie commented 4 years ago

I noticed today that wp media regenerate does not seem to include functionality for updating post content referencing old versions of an image. It was a little confusing given that the Regenerate Thumbnails plugin documentation recommends this WP CLI command as complete replacement for its functionality:

If you have command line access to your server, I highly recommend using WP-CLI instead of this plugin as it’s faster (no HTTP requests overhead) and can be run inside of a screen for those with many thumbnails.

Perhaps it would be relatively straightforward to bring update_usages_in_posts() into WP CLI for feature parity. Otherwise, it could help future users expecting this step to happen if the WP CLI docs and command usage info explicitly noted that post content will not be updated.

Edited to add: I see on closer reading that the content update functionality in Regenerate Thumbnails has been disabled, so makes more sense that this is not in the CLI command either.

petersphilo commented 3 years ago

This is the same issue as the one here: https://github.com/wp-cli/media-command/issues/130

the bug seems to occur when invoking the --image_size option

i can confirm that running the command media regenerate --only-missing resolved all issues

janw-me commented 3 years ago

Steps to reproduce.

  1. Upload a (big image). Let's say it get ID 18
  2. Edit a post/page. Insert the image in large format.
  3. Go to media settings. 'large is 1024x432' by default. Change it to 1280x540 for example.
  4. Run wp media regenerate 18 --image_size=large --only-missing Now the image-1280x540.jpg is created.
  5. In the post the 1024x432 image is still included. The image of 1024x432 does still exists, which is expected.

Solution 1. Replacing the new image by setting a flag.

We have the old file name. Before the image is regenerated the old size is stored in the _wp_attachment_metadata meta. After that it's a basic search-replace for the post_content column for old name. > new name.

Questions.

  1. Should this be a search-replace on the post or a seperate wp_post_update so it's a propper revision?
  2. Which post types are to be affected? All? Only the publicly available?
  3. The same question for post status. Published or also Drafts? What about trash?
  4. This might get real slow if there are many posts and images and all images & sizes are regenerated.
  5. If the old size is replaced everywhere should the old size be removed? This might cause problems if images are linked outside the current WordPress like in a newsletter or on a multisite. I guess this could be an other flag-option.
  6. I have no idea if this might break some page builder.

Solution 2. Add an option for the output as CSV.

wp media regenerate 18 --image_size=large --format=csv

ID,oldfile,newfile,imagesize
18,2021/10/image-1024x432.jpg,2021/10/image-1280x540.jpg,large

This way every user can loop over this output themselves and the questions for Solution 1 can be implemented freely based on every usecase.

danielbachhuber commented 1 year ago

I removed the bug label from this issue and changed the title to Offer flag to update post content when regenerating thumbnails

We're fixing the missing metadata bug with https://github.com/wp-cli/media-command/issues/130