Skip to content

Commit cf1741d

Browse files
authored
Merge pull request #154 from sagarnasit/master
Add `--from-post=<post_id>` flag to create duplicate posts
2 parents 84b317b + ffe42d6 commit cf1741d

File tree

3 files changed

+174
-1
lines changed

3 files changed

+174
-1
lines changed

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1997,7 +1997,7 @@ wp post
19971997
Creates a new post.
19981998

19991999
~~~
2000-
wp post create [--post_author=<post_author>] [--post_date=<post_date>] [--post_date_gmt=<post_date_gmt>] [--post_content=<post_content>] [--post_content_filtered=<post_content_filtered>] [--post_title=<post_title>] [--post_excerpt=<post_excerpt>] [--post_status=<post_status>] [--post_type=<post_type>] [--comment_status=<comment_status>] [--ping_status=<ping_status>] [--post_password=<post_password>] [--post_name=<post_name>] [--to_ping=<to_ping>] [--pinged=<pinged>] [--post_modified=<post_modified>] [--post_modified_gmt=<post_modified_gmt>] [--post_parent=<post_parent>] [--menu_order=<menu_order>] [--post_mime_type=<post_mime_type>] [--guid=<guid>] [--post_category=<post_category>] [--tags_input=<tags_input>] [--tax_input=<tax_input>] [--meta_input=<meta_input>] [<file>] [--<field>=<value>] [--edit] [--porcelain]
2000+
wp post create [--post_author=<post_author>] [--post_date=<post_date>] [--post_date_gmt=<post_date_gmt>] [--post_content=<post_content>] [--post_content_filtered=<post_content_filtered>] [--post_title=<post_title>] [--post_excerpt=<post_excerpt>] [--post_status=<post_status>] [--post_type=<post_type>] [--comment_status=<comment_status>] [--ping_status=<ping_status>] [--post_password=<post_password>] [--post_name=<post_name>] [--from-post=<post_id>] [--to_ping=<to_ping>] [--pinged=<pinged>] [--post_modified=<post_modified>] [--post_modified_gmt=<post_modified_gmt>] [--post_parent=<post_parent>] [--menu_order=<menu_order>] [--post_mime_type=<post_mime_type>] [--guid=<guid>] [--post_category=<post_category>] [--tags_input=<tags_input>] [--tax_input=<tax_input>] [--meta_input=<meta_input>] [<file>] [--<field>=<value>] [--edit] [--porcelain]
20012001
~~~
20022002

20032003
**OPTIONS**
@@ -2041,6 +2041,9 @@ wp post create [--post_author=<post_author>] [--post_date=<post_date>] [--post_d
20412041
[--post_name=<post_name>]
20422042
The post name. Default is the sanitized post title when creating a new post.
20432043

2044+
[--from-post=<post_id>]
2045+
Post id of a post to be duplicated.
2046+
20442047
[--to_ping=<to_ping>]
20452048
Space or carriage return-separated list of URLs to ping. Default empty.
20462049

@@ -2111,6 +2114,10 @@ wp post create [--post_author=<post_author>] [--post_date=<post_date>] [--post_d
21112114
$ wp post create --post_title='A post' --post_content='Just a small post.' --meta_input='{"key1":"value1","key2":"value2"}
21122115
Success: Created post 1923.
21132116

2117+
# Create a duplicate post from existing posts.
2118+
$ wp post create --from-post=123 --post_title='Different Title'
2119+
Success: Created post 2350.
2120+
21142121

21152122

21162123
### wp post delete
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
Feature: Create Duplicate WordPress post from existing posts.
2+
3+
Background:
4+
Given a WP install
5+
6+
Scenario: Generate duplicate post.
7+
When I run `wp term create category "Test Category" --porcelain`
8+
Then save STDOUT as {TERM_ID}
9+
10+
When I run `wp term create post_tag "Test Tag" --porcelain`
11+
Then save STDOUT as {TAG_ID}
12+
13+
When I run `wp post create --post_title='Test Duplicate Post' --post_category={TERM_ID} --porcelain`
14+
And save STDOUT as {POST_ID}
15+
16+
When I run `wp post term add {POST_ID} post_tag {TAG_ID} --by=id`
17+
Then STDOUT should contain:
18+
"""
19+
Success: Added term.
20+
"""
21+
22+
When I run `wp post create --from-post={POST_ID} --porcelain`
23+
Then STDOUT should be a number
24+
And save STDOUT as {DUPLICATE_POST_ID}
25+
26+
When I run `wp post get {DUPLICATE_POST_ID} --field=title`
27+
Then STDOUT should be:
28+
"""
29+
Test Duplicate Post
30+
"""
31+
32+
When I run `wp post term list {DUPLICATE_POST_ID} category --field=term_id`
33+
Then STDOUT should be:
34+
"""
35+
{TERM_ID}
36+
"""
37+
38+
When I run `wp post term list {DUPLICATE_POST_ID} post_tag --field=term_id`
39+
Then STDOUT should be:
40+
"""
41+
{TAG_ID}
42+
"""
43+
44+
@require-wp-4.4
45+
Scenario: Generate duplicate post with post metadata.
46+
When I run `wp post create --post_title='Test Post' --meta_input='{"key1":"value1","key2":"value2"}' --porcelain`
47+
Then save STDOUT as {POST_ID}
48+
49+
When I run `wp post create --from-post={POST_ID} --porcelain`
50+
Then save STDOUT as {DUPLICATE_POST_ID}
51+
52+
When I run `wp post meta list {DUPLICATE_POST_ID} --format=table`
53+
Then STDOUT should be a table containing rows:
54+
| post_id | meta_key | meta_value |
55+
| {DUPLICATE_POST_ID} | key1 | value1 |
56+
| {DUPLICATE_POST_ID} | key2 | value2 |
57+
58+
59+
Scenario: Generate duplicate page.
60+
When I run `wp post create --post_type="page" --post_title="Test Page" --post_content="Page Content" --porcelain`
61+
Then save STDOUT as {POST_ID}
62+
63+
When I run `wp post create --from-post={POST_ID} --post_title="Duplicate Page" --porcelain`
64+
Then save STDOUT as {DUPLICATE_POST_ID}
65+
66+
When I run `wp post list --post_type='page' --fields="title, content, type"`
67+
Then STDOUT should be a table containing rows:
68+
| post_title | post_content | post_type |
69+
| Test Page | Page Content | page |
70+
| Duplicate Page | Page Content | page |
71+
72+
Scenario: Change type of duplicate post.
73+
When I run `wp post create --post_title='Test Post' --porcelain`
74+
Then save STDOUT as {POST_ID}
75+
76+
When I run `wp post create --from-post={POST_ID} --post_type=page --porcelain`
77+
Then save STDOUT as {DUPLICATE_POST_ID}
78+
79+
When I run `wp post get {DUPLICATE_POST_ID} --fields=type`
80+
Then STDOUT should be a table containing rows:
81+
| Field | Value |
82+
| post_type | page |

src/Post_Command.php

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ public function __construct() {
7878
* [--post_name=<post_name>]
7979
* : The post name. Default is the sanitized post title when creating a new post.
8080
*
81+
* [--from-post=<post_id>]
82+
* : Post id of a post to be duplicated.
83+
*
8184
* [--to_ping=<to_ping>]
8285
* : Space or carriage return-separated list of URLs to ping. Default empty.
8386
*
@@ -147,6 +150,10 @@ public function __construct() {
147150
* # Create a post with multiple meta values.
148151
* $ wp post create --post_title='A post' --post_content='Just a small post.' --meta_input='{"key1":"value1","key2":"value2"}
149152
* Success: Created post 1923.
153+
*
154+
* # Create a duplicate post from existing posts.
155+
* $ wp post create --from-post=123 --post_title='Different Title'
156+
* Success: Created post 2350.
150157
*/
151158
public function create( $args, $assoc_args ) {
152159
if ( ! empty( $args[0] ) ) {
@@ -173,6 +180,27 @@ public function create( $args, $assoc_args ) {
173180
$array_arguments = array( 'meta_input' );
174181
$assoc_args = \WP_CLI\Utils\parse_shell_arrays( $assoc_args, $array_arguments );
175182

183+
if ( isset( $assoc_args['from-post'] ) ) {
184+
$post = $this->fetcher->get_check( $assoc_args['from-post'] );
185+
$post_arr = get_object_vars( $post );
186+
$post_id = $post_arr['ID'];
187+
unset( $post_arr['post_date'] );
188+
unset( $post_arr['post_date_gmt'] );
189+
unset( $post_arr['guid'] );
190+
unset( $post_arr['ID'] );
191+
192+
if ( empty( $assoc_args['meta_input'] ) ) {
193+
$assoc_args['meta_input'] = $this->get_metadata( $post_id );
194+
}
195+
if ( empty( $assoc_args['post_category'] ) ) {
196+
$post_arr['post_category'] = $this->get_category( $post_id );
197+
}
198+
if ( empty( $assoc_args['tags_input'] ) ) {
199+
$post_arr['tags_input'] = $this->get_tags( $post_id );
200+
}
201+
$assoc_args = array_merge( $post_arr, $assoc_args );
202+
}
203+
176204
$assoc_args = wp_slash( $assoc_args );
177205
parent::_create( $args, $assoc_args, function ( $params ) {
178206
return wp_insert_post( $params, true );
@@ -797,4 +825,60 @@ private function get_category_ids( $arg ) {
797825
// If no category ids found, return exploded array for compat with previous WP-CLI versions.
798826
return $category_ids ? $category_ids : $categories;
799827
}
828+
829+
/**
830+
* Get post metadata.
831+
*
832+
* @param $post_id ID of the post.
833+
*
834+
* @return array
835+
*/
836+
private function get_metadata( $post_id ) {
837+
$metadata = get_metadata( 'post', $post_id );
838+
$items = array();
839+
foreach ( $metadata as $key => $values ) {
840+
foreach ( $values as $item_value ) {
841+
$item_value = maybe_unserialize( $item_value );
842+
$items[$key] = $item_value;
843+
}
844+
}
845+
846+
return $items;
847+
}
848+
849+
/**
850+
* Get Categories of a post.
851+
*
852+
* @param $post_id ID of the post.
853+
*
854+
* @return array
855+
*/
856+
private function get_category( $post_id ) {
857+
$category_data = get_the_category( $post_id );
858+
$category_arr = array();
859+
foreach ( $category_data as $cat ) {
860+
array_push( $category_arr, $cat->term_id );
861+
}
862+
863+
return $category_arr;
864+
}
865+
866+
/**
867+
* Get Tags of a post.
868+
*
869+
* @param $post_id ID of the post.
870+
*
871+
* @return array
872+
*/
873+
private function get_tags( $post_id ) {
874+
$tag_data = get_the_tags( $post_id );
875+
$tag_arr = array();
876+
if ( $tag_data ) {
877+
foreach ( $tag_data as $tag ) {
878+
array_push( $tag_arr, $tag->slug );
879+
}
880+
}
881+
882+
return $tag_arr;
883+
}
800884
}

0 commit comments

Comments
 (0)