Skip to content

Commit 5e4e696

Browse files
CopilotswissspidyCopilot
authored
Add wp media prune command to remove generated thumbnails (#239)
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: Pascal Birchler <pascal.birchler@gmail.com> Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
1 parent d2edbef commit 5e4e696

3 files changed

Lines changed: 419 additions & 0 deletions

File tree

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"media",
3939
"media fix-orientation",
4040
"media import",
41+
"media prune",
4142
"media regenerate",
4243
"media image-size"
4344
]

features/media-prune.feature

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
Feature: Prune WordPress attachment thumbnails
2+
3+
Background:
4+
Given a WP install
5+
And I try `wp theme install twentynineteen --activate`
6+
7+
Scenario: Prune all images while none exists
8+
When I try `wp media prune --yes`
9+
Then STDERR should contain:
10+
"""
11+
No images found.
12+
"""
13+
And the return code should be 0
14+
15+
@require-wp-5.3
16+
Scenario: Prune all thumbnails for all images
17+
Given download:
18+
| path | url |
19+
| {CACHE_DIR}/large-image.jpg | http://wp-cli.org/behat-data/large-image.jpg |
20+
| {CACHE_DIR}/canola.jpg | http://wp-cli.org/behat-data/canola.jpg |
21+
And I run `wp option update uploads_use_yearmonth_folders 0`
22+
23+
When I run `wp media import {CACHE_DIR}/large-image.jpg --title="My large attachment" --porcelain`
24+
Then save STDOUT as {LARGE_ATTACHMENT_ID}
25+
And the wp-content/uploads/large-image.jpg file should exist
26+
And the wp-content/uploads/large-image-scaled.jpg file should exist
27+
And the wp-content/uploads/large-image-150x150.jpg file should exist
28+
And the wp-content/uploads/large-image-300x225.jpg file should exist
29+
30+
When I run `wp media import {CACHE_DIR}/canola.jpg --title="My medium attachment" --porcelain`
31+
Then save STDOUT as {MEDIUM_ATTACHMENT_ID}
32+
And the wp-content/uploads/canola.jpg file should exist
33+
And the wp-content/uploads/canola-150x150.jpg file should exist
34+
And the wp-content/uploads/canola-300x225.jpg file should exist
35+
36+
When I run `wp media prune --yes`
37+
Then STDOUT should contain:
38+
"""
39+
Found 2 images to prune.
40+
"""
41+
And STDOUT should contain:
42+
"""
43+
/2 Pruned thumbnails for "My large attachment" (ID {LARGE_ATTACHMENT_ID})
44+
"""
45+
And STDOUT should contain:
46+
"""
47+
/2 Pruned thumbnails for "My medium attachment" (ID {MEDIUM_ATTACHMENT_ID})
48+
"""
49+
And STDOUT should contain:
50+
"""
51+
Success: Pruned 2 of 2 images.
52+
"""
53+
And the wp-content/uploads/large-image.jpg file should exist
54+
And the wp-content/uploads/large-image-scaled.jpg file should exist
55+
And the wp-content/uploads/large-image-150x150.jpg file should not exist
56+
And the wp-content/uploads/large-image-300x225.jpg file should not exist
57+
And the wp-content/uploads/canola.jpg file should exist
58+
And the wp-content/uploads/canola-150x150.jpg file should not exist
59+
And the wp-content/uploads/canola-300x225.jpg file should not exist
60+
61+
When I run `wp post meta get {LARGE_ATTACHMENT_ID} _wp_attachment_metadata --format=json`
62+
Then STDOUT should not contain:
63+
"""
64+
"thumbnail"
65+
"""
66+
And STDOUT should not contain:
67+
"""
68+
"medium"
69+
"""
70+
71+
@require-wp-5.3
72+
Scenario: Prune a specific image size
73+
Given download:
74+
| path | url |
75+
| {CACHE_DIR}/large-image.jpg | http://wp-cli.org/behat-data/large-image.jpg |
76+
And I run `wp option update uploads_use_yearmonth_folders 0`
77+
78+
When I run `wp media import {CACHE_DIR}/large-image.jpg --title="My large attachment" --porcelain`
79+
Then save STDOUT as {LARGE_ATTACHMENT_ID}
80+
And the wp-content/uploads/large-image-150x150.jpg file should exist
81+
And the wp-content/uploads/large-image-300x225.jpg file should exist
82+
83+
When I run `wp media prune --image_size=thumbnail {LARGE_ATTACHMENT_ID}`
84+
Then STDOUT should contain:
85+
"""
86+
Pruned thumbnails for "My large attachment" (ID {LARGE_ATTACHMENT_ID})
87+
"""
88+
And STDOUT should contain:
89+
"""
90+
Success: Pruned 1 of 1 images.
91+
"""
92+
And the wp-content/uploads/large-image-150x150.jpg file should not exist
93+
And the wp-content/uploads/large-image-300x225.jpg file should exist
94+
95+
@require-wp-5.3
96+
Scenario: Prune does not remove abandoned (unregistered) thumbnails by default
97+
Given download:
98+
| path | url |
99+
| {CACHE_DIR}/large-image.jpg | http://wp-cli.org/behat-data/large-image.jpg |
100+
And a wp-content/mu-plugins/media-settings.php file:
101+
"""
102+
<?php
103+
add_action( 'after_setup_theme', function(){
104+
add_image_size( 'abandoned_size', 200, 200, true );
105+
});
106+
"""
107+
And I run `wp option update uploads_use_yearmonth_folders 0`
108+
109+
When I run `wp media import {CACHE_DIR}/large-image.jpg --title="My large attachment" --porcelain`
110+
Then save STDOUT as {LARGE_ATTACHMENT_ID}
111+
And the wp-content/uploads/large-image-200x200.jpg file should exist
112+
113+
# Remove the custom image size (simulating an abandoned size).
114+
Given a wp-content/mu-plugins/media-settings.php file:
115+
"""
116+
<?php
117+
"""
118+
119+
When I run `wp media prune --yes`
120+
Then STDOUT should contain:
121+
"""
122+
Success: Pruned
123+
"""
124+
And the wp-content/uploads/large-image-200x200.jpg file should exist
125+
126+
When I run `wp post meta get {LARGE_ATTACHMENT_ID} _wp_attachment_metadata --format=json`
127+
Then STDOUT should contain:
128+
"""
129+
"abandoned_size"
130+
"""
131+
132+
@require-wp-5.3
133+
Scenario: Prune removes abandoned thumbnails with --remove-abandoned
134+
Given download:
135+
| path | url |
136+
| {CACHE_DIR}/large-image.jpg | http://wp-cli.org/behat-data/large-image.jpg |
137+
And a wp-content/mu-plugins/media-settings.php file:
138+
"""
139+
<?php
140+
add_action( 'after_setup_theme', function(){
141+
add_image_size( 'abandoned_size', 200, 200, true );
142+
});
143+
"""
144+
And I run `wp option update uploads_use_yearmonth_folders 0`
145+
146+
When I run `wp media import {CACHE_DIR}/large-image.jpg --title="My large attachment" --porcelain`
147+
Then save STDOUT as {LARGE_ATTACHMENT_ID}
148+
And the wp-content/uploads/large-image-200x200.jpg file should exist
149+
150+
# Remove the custom image size (simulating an abandoned size).
151+
Given a wp-content/mu-plugins/media-settings.php file:
152+
"""
153+
<?php
154+
"""
155+
156+
When I run `wp media prune --remove-abandoned --yes`
157+
Then STDOUT should contain:
158+
"""
159+
Success: Pruned
160+
"""
161+
And the wp-content/uploads/large-image-200x200.jpg file should not exist
162+
163+
When I run `wp post meta get {LARGE_ATTACHMENT_ID} _wp_attachment_metadata --format=json`
164+
Then STDOUT should not contain:
165+
"""
166+
"abandoned_size"
167+
"""
168+
169+
Scenario: Error on unknown image size
170+
When I try `wp media prune --image_size=nonexistent --yes`
171+
Then STDERR should contain:
172+
"""
173+
Unknown image size "nonexistent".
174+
"""
175+
And the return code should be 1

0 commit comments

Comments
 (0)