Skip to content

Commit b9af1ea

Browse files
committed
Merge branch 'main' into copilot/skip-duplicates-feature
2 parents e94bf00 + 1d86009 commit b9af1ea

5 files changed

Lines changed: 702 additions & 7 deletions

File tree

README.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@ wp media
5252
1/1 Fixing orientation for "Portrait_6" (ID 63).
5353
Success: Fixed 1 of 1 images.
5454

55+
# Remove all generated thumbnails, without confirmation.
56+
$ wp media prune --yes
57+
Found 3 images to prune.
58+
1/3 Pruned thumbnails for "Sydney Harbor Bridge" (ID 760).
59+
2/3 Pruned thumbnails for "Boardwalk" (ID 757).
60+
3/3 Pruned thumbnails for "Sunburst Over River" (ID 756).
61+
Success: Pruned 3 of 3 images.
62+
5563

5664

5765
### wp media fix-orientation
@@ -183,6 +191,62 @@ wp media import <file>... [--post_id=<post_id>] [--post_name=<post_name>] [--fil
183191

184192

185193

194+
### wp media prune
195+
196+
Removes all generated image files for one or more attachments.
197+
198+
~~~
199+
wp media prune [<attachment-id>...] [--image_size=<image_size>...] [--remove-abandoned] [--yes]
200+
~~~
201+
202+
Generated image files for registered sizes can be recreated with
203+
`wp media regenerate`. Thumbnails for image sizes that are no longer
204+
registered are kept by default because they cannot be regenerated; use
205+
`--remove-abandoned` to remove them as well.
206+
207+
**OPTIONS**
208+
209+
[<attachment-id>...]
210+
One or more IDs of the attachments to prune.
211+
212+
[--image_size=<image_size>...]
213+
Name of the image size to remove. Repeat the flag to specify multiple. Only thumbnails of specified image size(s) will be removed, thumbnails of other image sizes will not.
214+
215+
[--remove-abandoned]
216+
Also remove thumbnails for image sizes that are no longer registered.
217+
218+
[--yes]
219+
Answer yes to the confirmation message. Confirmation only shows when no IDs passed as arguments.
220+
221+
**EXAMPLES**
222+
223+
# Remove all generated thumbnails for all images, without confirmation.
224+
$ wp media prune --yes
225+
Found 3 images to prune.
226+
1/3 Pruned thumbnails for "Sydney Harbor Bridge" (ID 760).
227+
2/3 Pruned thumbnails for "Boardwalk" (ID 757).
228+
3/3 Pruned thumbnails for "Sunburst Over River" (ID 756).
229+
Success: Pruned 3 of 3 images.
230+
231+
# Remove only the "large" thumbnails for all images.
232+
$ wp media prune --image_size=large
233+
Do you really want to prune the "large" image size for all images? [y/n] y
234+
Found 3 images to prune.
235+
1/3 Pruned thumbnails for "Sydney Harbor Bridge" (ID 760).
236+
2/3 Pruned thumbnails for "Boardwalk" (ID 757).
237+
3/3 Pruned thumbnails for "Sunburst Over River" (ID 756).
238+
Success: Pruned 3 of 3 images.
239+
240+
# Remove all thumbnails including those for unregistered sizes.
241+
$ wp media prune --remove-abandoned --yes
242+
Found 3 images to prune.
243+
1/3 Pruned thumbnails for "Sydney Harbor Bridge" (ID 760).
244+
2/3 Pruned thumbnails for "Boardwalk" (ID 757).
245+
3/3 Pruned thumbnails for "Sunburst Over River" (ID 756).
246+
Success: Pruned 3 of 3 images.
247+
248+
249+
186250
### wp media regenerate
187251

188252
Regenerates thumbnails for one or more attachments.
@@ -337,6 +401,10 @@ Want to contribute a new feature? Please first [open a new issue](https://github
337401

338402
Once you've decided to commit the time to seeing your pull request through, [please follow our guidelines for creating a pull request](https://make.wordpress.org/cli/handbook/pull-requests/) to make sure it's a pleasant experience. See "[Setting up](https://make.wordpress.org/cli/handbook/pull-requests/#setting-up)" for details specific to working on this package locally.
339403

404+
### License
405+
406+
This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.
407+
340408
## Support
341409

342410
GitHub issues aren't for general support questions, but there are other venues you can try: https://wp-cli.org/#support

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

features/media-regenerate.feature

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1933,3 +1933,103 @@ Feature: Regenerate WordPress attachments
19331933
"""
19341934
site_icon-270
19351935
"""
1936+
1937+
Scenario: Update post content references when regenerating a specific image size
1938+
Given download:
1939+
| path | url |
1940+
| {CACHE_DIR}/canola.jpg | http://wp-cli.org/behat-data/canola.jpg |
1941+
And a wp-content/mu-plugins/media-settings.php file:
1942+
"""
1943+
<?php
1944+
add_action( 'after_setup_theme', function(){
1945+
add_image_size( 'test1', 400, 400, true );
1946+
});
1947+
"""
1948+
And I run `wp option update uploads_use_yearmonth_folders 0`
1949+
1950+
When I run `wp media import {CACHE_DIR}/canola.jpg --title="My imported attachment" --porcelain`
1951+
Then save STDOUT as {ATTACHMENT_ID}
1952+
And the wp-content/uploads/canola-400x400.jpg file should exist
1953+
1954+
# Get the full URL of the test1 thumbnail.
1955+
When I run `wp eval "echo wp_get_attachment_image_src( {ATTACHMENT_ID}, 'test1' )[0];"`
1956+
Then save STDOUT as {OLD_THUMBNAIL_URL}
1957+
1958+
# Create a post referencing the old thumbnail URL in post content.
1959+
When I run `wp post create --post_title="Test Post" --post_status=publish --post_content="{OLD_THUMBNAIL_URL}" --porcelain`
1960+
Then save STDOUT as {POST_ID}
1961+
1962+
# Confirm the old URL is in post content before regeneration.
1963+
When I run `wp post get {POST_ID} --field=post_content`
1964+
Then STDOUT should contain:
1965+
"""
1966+
canola-400x400.jpg
1967+
"""
1968+
1969+
# Change "test1" image size dimensions.
1970+
Given a wp-content/mu-plugins/media-settings.php file:
1971+
"""
1972+
<?php
1973+
add_action( 'after_setup_theme', function(){
1974+
add_image_size( 'test1', 350, 350, true );
1975+
});
1976+
"""
1977+
1978+
# Regenerate "test1" without --update-attachment-refs - post content should be unchanged.
1979+
When I run `wp media regenerate {ATTACHMENT_ID} --image_size=test1 --yes`
1980+
Then STDOUT should contain:
1981+
"""
1982+
1/1 Regenerated "test1" thumbnail for "My imported attachment"
1983+
"""
1984+
And the wp-content/uploads/canola-350x350.jpg file should exist
1985+
1986+
When I run `wp post get {POST_ID} --field=post_content`
1987+
Then STDOUT should contain:
1988+
"""
1989+
canola-400x400.jpg
1990+
"""
1991+
1992+
# Change "test1" back to 400x400 so we can test --update-attachment-refs.
1993+
Given a wp-content/mu-plugins/media-settings.php file:
1994+
"""
1995+
<?php
1996+
add_action( 'after_setup_theme', function(){
1997+
add_image_size( 'test1', 400, 400, true );
1998+
});
1999+
"""
2000+
When I run `wp media regenerate {ATTACHMENT_ID} --image_size=test1 --yes`
2001+
Then STDOUT should contain:
2002+
"""
2003+
1/1 Regenerated "test1" thumbnail for "My imported attachment"
2004+
"""
2005+
And the wp-content/uploads/canola-400x400.jpg file should exist
2006+
2007+
# Change "test1" to 350x350 and regenerate with --update-attachment-refs.
2008+
Given a wp-content/mu-plugins/media-settings.php file:
2009+
"""
2010+
<?php
2011+
add_action( 'after_setup_theme', function(){
2012+
add_image_size( 'test1', 350, 350, true );
2013+
});
2014+
"""
2015+
When I run `wp media regenerate {ATTACHMENT_ID} --image_size=test1 --update-attachment-refs --yes`
2016+
Then STDOUT should contain:
2017+
"""
2018+
1/1 Regenerated "test1" thumbnail for "My imported attachment"
2019+
"""
2020+
And STDOUT should contain:
2021+
"""
2022+
Success: Regenerated 1 of 1 images.
2023+
"""
2024+
And the wp-content/uploads/canola-350x350.jpg file should exist
2025+
2026+
# Confirm the post content was updated to use the new thumbnail URL.
2027+
When I run `wp post get {POST_ID} --field=post_content`
2028+
Then STDOUT should contain:
2029+
"""
2030+
canola-350x350.jpg
2031+
"""
2032+
And STDOUT should not contain:
2033+
"""
2034+
canola-400x400.jpg
2035+
"""

0 commit comments

Comments
 (0)