Skip to content

Commit 9ebfd56

Browse files
committed
Merge branch 'main' into copilot/skip-duplicates-feature
2 parents 1cac237 + 52a07ae commit 9ebfd56

5 files changed

Lines changed: 518 additions & 46 deletions

File tree

README.md

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ This package implements the following commands:
1313

1414
### wp media
1515

16-
Imports files as attachments, regenerates thumbnails, or lists registered image sizes.
16+
Imports files as attachments, regenerates thumbnails, replaces existing attachment files, or lists registered image sizes.
1717

1818
~~~
1919
wp media
@@ -34,6 +34,11 @@ wp media
3434
Imported file '/home/person/Downloads/image.png' as attachment ID 1753 and attached to post 123 as featured image.
3535
Success: Imported 1 of 1 images.
3636

37+
# Import an image from STDIN.
38+
$ curl http://example.com/image.jpg | wp media import -
39+
Imported file 'STDIN' as attachment ID 1754.
40+
Success: Imported 1 of 1 items.
41+
3742
# List all registered image sizes
3843
$ wp media image-size
3944
+---------------------------+-------+--------+-------+
@@ -113,6 +118,7 @@ wp media import <file>... [--post_id=<post_id>] [--post_name=<post_name>] [--fil
113118
Path to file or files to be imported. Supports the glob(3) capabilities of the current shell.
114119
If file is recognized as a URL (for example, with a scheme of http or ftp), the file will be
115120
downloaded to a temp file before being sideloaded.
121+
Use '-' to read file data from STDIN.
116122

117123
[--post_id=<post_id>]
118124
ID of the post to attach the imported files to.
@@ -189,6 +195,11 @@ wp media import <file>... [--post_id=<post_id>] [--post_name=<post_name>] [--fil
189195
$ wp media import http://s.wordpress.org/style/images/wp-header-logo.png --porcelain | xargs -I {} wp post list --post__in={} --field=url --post_type=attachment
190196
http://wordpress-develop.dev/wp-header-logo/
191197

198+
# Import an image from STDIN.
199+
$ curl http://example.com/image.jpg | wp media import - --title="From STDIN"
200+
Imported file 'STDIN' as attachment ID 1756.
201+
Success: Imported 1 of 1 items.
202+
192203

193204

194205
### wp media prune
@@ -325,6 +336,46 @@ wp media regenerate [<attachment-id>...] [--image_size=<image_size>...] [--skip-
325336

326337

327338

339+
### wp media replace
340+
341+
Replaces the file for an existing attachment while preserving its identity.
342+
343+
~~~
344+
wp media replace <attachment-id> <file> [--skip-delete] [--porcelain]
345+
~~~
346+
347+
**OPTIONS**
348+
349+
<attachment-id>
350+
ID of the attachment whose file is to be replaced.
351+
352+
<file>
353+
Path to the replacement file. Supports local paths and URLs.
354+
355+
[--skip-delete]
356+
Skip deletion of old thumbnail files after replacement.
357+
358+
[--porcelain]
359+
Output just the attachment ID after replacement.
360+
361+
**EXAMPLES**
362+
363+
# Replace an attachment file with a local file.
364+
$ wp media replace 123 ~/new-image.jpg
365+
Replaced file for attachment ID 123 with '/home/user/new-image.jpg'.
366+
Success: Replaced 1 of 1 images.
367+
368+
# Replace an attachment file with a file from a URL.
369+
$ wp media replace 123 'http://example.com/image.jpg'
370+
Replaced file for attachment ID 123 with 'http://example.com/image.jpg'.
371+
Success: Replaced 1 of 1 images.
372+
373+
# Replace and output just the attachment ID.
374+
$ wp media replace 123 ~/new-image.jpg --porcelain
375+
123
376+
377+
378+
328379
### wp media image-size
329380

330381
Lists image sizes registered with WordPress.

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"media import",
4141
"media prune",
4242
"media regenerate",
43+
"media replace",
4344
"media image-size"
4445
]
4546
},

features/media-import.feature

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,51 @@ Feature: Manage WordPress attachments
287287
Error: Invalid value for <porcelain>: invalid. Expected flag or 'url'.
288288
"""
289289

290+
Scenario: Import media from STDIN
291+
Given download:
292+
| path | url |
293+
| {CACHE_DIR}/codeispoetry.png | http://wp-cli.org/behat-data/codeispoetry.png |
294+
295+
When I run `cat {CACHE_DIR}/codeispoetry.png | wp media import - --title="From STDIN" --porcelain`
296+
Then save STDOUT as {ATTACHMENT_ID}
297+
298+
When I run `wp post get {ATTACHMENT_ID} --field=title`
299+
Then STDOUT should be:
300+
"""
301+
From STDIN
302+
"""
303+
304+
Scenario: Import media from STDIN with file_name
305+
Given download:
306+
| path | url |
307+
| {CACHE_DIR}/codeispoetry.png | http://wp-cli.org/behat-data/codeispoetry.png |
308+
309+
When I run `cat {CACHE_DIR}/codeispoetry.png | wp media import - --file_name=my-image.png --porcelain`
310+
Then save STDOUT as {ATTACHMENT_ID}
311+
312+
When I run `wp post get {ATTACHMENT_ID} --field=post_name`
313+
Then STDOUT should be:
314+
"""
315+
my-image
316+
"""
317+
318+
When I run `wp post meta get {ATTACHMENT_ID} _wp_attached_file`
319+
Then STDOUT should contain:
320+
"""
321+
my-image.png
322+
"""
323+
And STDOUT should not contain:
324+
"""
325+
my-image.png.png
326+
"""
327+
Scenario: Fail to import from STDIN when no input provided
328+
When I try `wp media import - </dev/null`
329+
Then STDERR should contain:
330+
"""
331+
Warning: Unable to import file from STDIN. Reason: No input provided.
332+
"""
333+
And the return code should be 1
334+
290335
Scenario: Upload files into a custom directory, relative to ABSPATH, when --destination-dir flag is applied.
291336
Given download:
292337
| path | url |

features/media-replace.feature

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
Feature: Replace WordPress attachment files
2+
3+
Background:
4+
Given a WP install
5+
6+
Scenario: Replace an attachment file with a local file
7+
Given download:
8+
| path | url |
9+
| {CACHE_DIR}/large-image.jpg | http://wp-cli.org/behat-data/large-image.jpg |
10+
| {CACHE_DIR}/canola.jpg | http://wp-cli.org/behat-data/canola.jpg |
11+
And I run `wp option update uploads_use_yearmonth_folders 0`
12+
13+
When I run `wp media import {CACHE_DIR}/large-image.jpg --porcelain`
14+
Then save STDOUT as {ATTACHMENT_ID}
15+
16+
When I run `wp media replace {ATTACHMENT_ID} {CACHE_DIR}/canola.jpg`
17+
Then STDOUT should contain:
18+
"""
19+
Replaced file for attachment ID {ATTACHMENT_ID}
20+
"""
21+
And STDOUT should contain:
22+
"""
23+
Success: Replaced 1 of 1 attachments.
24+
"""
25+
26+
Scenario: Replace an attachment file from a URL
27+
Given download:
28+
| path | url |
29+
| {CACHE_DIR}/large-image.jpg | http://wp-cli.org/behat-data/large-image.jpg |
30+
And I run `wp option update uploads_use_yearmonth_folders 0`
31+
32+
When I run `wp media import {CACHE_DIR}/large-image.jpg --porcelain`
33+
Then save STDOUT as {ATTACHMENT_ID}
34+
35+
When I run `wp media replace {ATTACHMENT_ID} 'http://wp-cli.org/behat-data/canola.jpg'`
36+
Then STDOUT should contain:
37+
"""
38+
Replaced file for attachment ID {ATTACHMENT_ID}
39+
"""
40+
And STDOUT should contain:
41+
"""
42+
Success: Replaced 1 of 1 attachments.
43+
"""
44+
45+
Scenario: Replace an attachment file and output only the attachment ID in porcelain mode
46+
Given download:
47+
| path | url |
48+
| {CACHE_DIR}/large-image.jpg | http://wp-cli.org/behat-data/large-image.jpg |
49+
| {CACHE_DIR}/canola.jpg | http://wp-cli.org/behat-data/canola.jpg |
50+
And I run `wp option update uploads_use_yearmonth_folders 0`
51+
52+
When I run `wp media import {CACHE_DIR}/large-image.jpg --porcelain`
53+
Then save STDOUT as {ATTACHMENT_ID}
54+
55+
When I run `wp media replace {ATTACHMENT_ID} {CACHE_DIR}/canola.jpg --porcelain`
56+
Then STDOUT should be:
57+
"""
58+
{ATTACHMENT_ID}
59+
"""
60+
61+
Scenario: Preserve attachment metadata after replacing the file
62+
Given download:
63+
| path | url |
64+
| {CACHE_DIR}/large-image.jpg | http://wp-cli.org/behat-data/large-image.jpg |
65+
| {CACHE_DIR}/canola.jpg | http://wp-cli.org/behat-data/canola.jpg |
66+
And I run `wp option update uploads_use_yearmonth_folders 0`
67+
68+
When I run `wp media import {CACHE_DIR}/large-image.jpg --title="My Image Title" --porcelain`
69+
Then save STDOUT as {ATTACHMENT_ID}
70+
71+
When I run `wp media replace {ATTACHMENT_ID} {CACHE_DIR}/canola.jpg`
72+
Then STDOUT should contain:
73+
"""
74+
Success: Replaced 1 of 1 attachments.
75+
"""
76+
77+
When I run `wp post get {ATTACHMENT_ID} --field=post_title`
78+
Then STDOUT should be:
79+
"""
80+
My Image Title
81+
"""
82+
83+
Scenario: Error when replacing with a non-existent local file
84+
Given download:
85+
| path | url |
86+
| {CACHE_DIR}/large-image.jpg | http://wp-cli.org/behat-data/large-image.jpg |
87+
88+
When I run `wp media import {CACHE_DIR}/large-image.jpg --porcelain`
89+
Then save STDOUT as {ATTACHMENT_ID}
90+
91+
When I try `wp media replace {ATTACHMENT_ID} /tmp/nonexistent-file.jpg`
92+
Then STDERR should contain:
93+
"""
94+
Error: Unable to replace attachment
95+
"""
96+
And STDERR should contain:
97+
"""
98+
File doesn't exist.
99+
"""
100+
And the return code should be 1
101+
102+
Scenario: Error when replacing with an invalid attachment ID
103+
When I try `wp media replace 999999 /tmp/fake.jpg`
104+
Then STDERR should contain:
105+
"""
106+
Error: Invalid attachment ID 999999.
107+
"""
108+
And the return code should be 1
109+
110+
Scenario: Skip deletion of old thumbnails when --skip-delete flag is used
111+
Given download:
112+
| path | url |
113+
| {CACHE_DIR}/large-image.jpg | http://wp-cli.org/behat-data/large-image.jpg |
114+
| {CACHE_DIR}/canola.jpg | http://wp-cli.org/behat-data/canola.jpg |
115+
And I run `wp option update uploads_use_yearmonth_folders 0`
116+
117+
When I run `wp media import {CACHE_DIR}/large-image.jpg --porcelain`
118+
Then save STDOUT as {ATTACHMENT_ID}
119+
120+
When I run `wp post meta get {ATTACHMENT_ID} _wp_attached_file`
121+
Then save STDOUT as {OLD_FILE}
122+
123+
When I run `wp media replace {ATTACHMENT_ID} {CACHE_DIR}/canola.jpg --skip-delete`
124+
Then STDOUT should contain:
125+
"""
126+
Success: Replaced 1 of 1 attachments.
127+
"""
128+
129+
And the wp-content/uploads/{OLD_FILE} file should exist

0 commit comments

Comments
 (0)