-
Notifications
You must be signed in to change notification settings - Fork 41
Add wp media prune command to remove generated thumbnails
#239
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2d45f30
Initial plan
Copilot 87ff004
Add wp media prune command to remove generated thumbnails
Copilot 9102a6f
Update src/Media_Command.php
swissspidy 071fc73
Check unlink() return value in process_prune, log warning on failure
Copilot 2cd99a8
Apply suggestions from code review
swissspidy 0c9ee9b
Update src/Media_Command.php
swissspidy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,175 @@ | ||
| Feature: Prune WordPress attachment thumbnails | ||
|
|
||
| Background: | ||
| Given a WP install | ||
| And I try `wp theme install twentynineteen --activate` | ||
|
|
||
| Scenario: Prune all images while none exists | ||
| When I try `wp media prune --yes` | ||
| Then STDERR should contain: | ||
| """ | ||
| No images found. | ||
| """ | ||
| And the return code should be 0 | ||
|
|
||
| @require-wp-5.3 | ||
| Scenario: Prune all thumbnails for all images | ||
| Given download: | ||
| | path | url | | ||
| | {CACHE_DIR}/large-image.jpg | http://wp-cli.org/behat-data/large-image.jpg | | ||
| | {CACHE_DIR}/canola.jpg | http://wp-cli.org/behat-data/canola.jpg | | ||
| And I run `wp option update uploads_use_yearmonth_folders 0` | ||
|
|
||
| When I run `wp media import {CACHE_DIR}/large-image.jpg --title="My large attachment" --porcelain` | ||
| Then save STDOUT as {LARGE_ATTACHMENT_ID} | ||
| And the wp-content/uploads/large-image.jpg file should exist | ||
| And the wp-content/uploads/large-image-scaled.jpg file should exist | ||
| And the wp-content/uploads/large-image-150x150.jpg file should exist | ||
| And the wp-content/uploads/large-image-300x225.jpg file should exist | ||
|
|
||
| When I run `wp media import {CACHE_DIR}/canola.jpg --title="My medium attachment" --porcelain` | ||
| Then save STDOUT as {MEDIUM_ATTACHMENT_ID} | ||
| And the wp-content/uploads/canola.jpg file should exist | ||
| And the wp-content/uploads/canola-150x150.jpg file should exist | ||
| And the wp-content/uploads/canola-300x225.jpg file should exist | ||
|
|
||
| When I run `wp media prune --yes` | ||
| Then STDOUT should contain: | ||
| """ | ||
| Found 2 images to prune. | ||
| """ | ||
| And STDOUT should contain: | ||
| """ | ||
| /2 Pruned thumbnails for "My large attachment" (ID {LARGE_ATTACHMENT_ID}) | ||
| """ | ||
| And STDOUT should contain: | ||
| """ | ||
| /2 Pruned thumbnails for "My medium attachment" (ID {MEDIUM_ATTACHMENT_ID}) | ||
| """ | ||
| And STDOUT should contain: | ||
| """ | ||
| Success: Pruned 2 of 2 images. | ||
| """ | ||
| And the wp-content/uploads/large-image.jpg file should exist | ||
| And the wp-content/uploads/large-image-scaled.jpg file should exist | ||
| And the wp-content/uploads/large-image-150x150.jpg file should not exist | ||
| And the wp-content/uploads/large-image-300x225.jpg file should not exist | ||
| And the wp-content/uploads/canola.jpg file should exist | ||
| And the wp-content/uploads/canola-150x150.jpg file should not exist | ||
| And the wp-content/uploads/canola-300x225.jpg file should not exist | ||
|
|
||
| And I run `wp post meta get {LARGE_ATTACHMENT_ID} _wp_attachment_metadata --format=json` | ||
| Then STDOUT should not contain: | ||
| """ | ||
| "thumbnail" | ||
| """ | ||
| And STDOUT should not contain: | ||
| """ | ||
| "medium" | ||
| """ | ||
|
|
||
| @require-wp-5.3 | ||
| Scenario: Prune a specific image size | ||
| Given download: | ||
| | path | url | | ||
| | {CACHE_DIR}/large-image.jpg | http://wp-cli.org/behat-data/large-image.jpg | | ||
| And I run `wp option update uploads_use_yearmonth_folders 0` | ||
|
|
||
| When I run `wp media import {CACHE_DIR}/large-image.jpg --title="My large attachment" --porcelain` | ||
| Then save STDOUT as {LARGE_ATTACHMENT_ID} | ||
| And the wp-content/uploads/large-image-150x150.jpg file should exist | ||
| And the wp-content/uploads/large-image-300x225.jpg file should exist | ||
|
|
||
| When I run `wp media prune --image_size=thumbnail {LARGE_ATTACHMENT_ID}` | ||
| Then STDOUT should contain: | ||
| """ | ||
| Pruned thumbnails for "My large attachment" (ID {LARGE_ATTACHMENT_ID}) | ||
| """ | ||
| And STDOUT should contain: | ||
| """ | ||
| Success: Pruned 1 of 1 images. | ||
| """ | ||
| And the wp-content/uploads/large-image-150x150.jpg file should not exist | ||
| And the wp-content/uploads/large-image-300x225.jpg file should exist | ||
|
|
||
| @require-wp-5.3 | ||
| Scenario: Prune does not remove abandoned (unregistered) thumbnails by default | ||
| Given download: | ||
| | path | url | | ||
| | {CACHE_DIR}/large-image.jpg | http://wp-cli.org/behat-data/large-image.jpg | | ||
| And a wp-content/mu-plugins/media-settings.php file: | ||
| """ | ||
| <?php | ||
| add_action( 'after_setup_theme', function(){ | ||
| add_image_size( 'abandoned_size', 200, 200, true ); | ||
| }); | ||
| """ | ||
| And I run `wp option update uploads_use_yearmonth_folders 0` | ||
|
|
||
| When I run `wp media import {CACHE_DIR}/large-image.jpg --title="My large attachment" --porcelain` | ||
| Then save STDOUT as {LARGE_ATTACHMENT_ID} | ||
| And the wp-content/uploads/large-image-200x200.jpg file should exist | ||
|
|
||
| # Remove the custom image size (simulating an abandoned size). | ||
| Given a wp-content/mu-plugins/media-settings.php file: | ||
| """ | ||
| <?php | ||
| """ | ||
|
|
||
| When I run `wp media prune --yes` | ||
| Then STDOUT should contain: | ||
| """ | ||
| Success: Pruned | ||
| """ | ||
| And the wp-content/uploads/large-image-200x200.jpg file should exist | ||
|
|
||
| And I run `wp post meta get {LARGE_ATTACHMENT_ID} _wp_attachment_metadata --format=json` | ||
|
swissspidy marked this conversation as resolved.
Outdated
|
||
| Then STDOUT should contain: | ||
| """ | ||
| "abandoned_size" | ||
| """ | ||
|
|
||
| @require-wp-5.3 | ||
| Scenario: Prune removes abandoned thumbnails with --remove-abandoned | ||
| Given download: | ||
| | path | url | | ||
| | {CACHE_DIR}/large-image.jpg | http://wp-cli.org/behat-data/large-image.jpg | | ||
| And a wp-content/mu-plugins/media-settings.php file: | ||
| """ | ||
| <?php | ||
| add_action( 'after_setup_theme', function(){ | ||
| add_image_size( 'abandoned_size', 200, 200, true ); | ||
| }); | ||
| """ | ||
| And I run `wp option update uploads_use_yearmonth_folders 0` | ||
|
|
||
| When I run `wp media import {CACHE_DIR}/large-image.jpg --title="My large attachment" --porcelain` | ||
| Then save STDOUT as {LARGE_ATTACHMENT_ID} | ||
| And the wp-content/uploads/large-image-200x200.jpg file should exist | ||
|
|
||
| # Remove the custom image size (simulating an abandoned size). | ||
| Given a wp-content/mu-plugins/media-settings.php file: | ||
| """ | ||
| <?php | ||
| """ | ||
|
|
||
| When I run `wp media prune --remove-abandoned --yes` | ||
| Then STDOUT should contain: | ||
| """ | ||
| Success: Pruned | ||
| """ | ||
| And the wp-content/uploads/large-image-200x200.jpg file should not exist | ||
|
|
||
| And I run `wp post meta get {LARGE_ATTACHMENT_ID} _wp_attachment_metadata --format=json` | ||
|
swissspidy marked this conversation as resolved.
Outdated
|
||
| Then STDOUT should not contain: | ||
| """ | ||
| "abandoned_size" | ||
| """ | ||
|
|
||
| Scenario: Error on unknown image size | ||
| When I try `wp media prune --image_size=nonexistent --yes` | ||
| Then STDERR should contain: | ||
| """ | ||
| Unknown image size "nonexistent". | ||
| """ | ||
| And the return code should be 1 | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.