Skip to content

Commit 2413aa3

Browse files
authored
Merge pull request #129 from thrijith/GH-4605
update post create/update
2 parents 21b9e01 + 95f1414 commit 2413aa3

File tree

2 files changed

+147
-2
lines changed

2 files changed

+147
-2
lines changed

features/post.feature

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,124 @@ Feature: Manage WordPress posts
2929
When I try the previous command again
3030
Then the return code should be 1
3131

32+
Scenario: Setting post categories
33+
When I run `wp term create category "First Category" --porcelain`
34+
And save STDOUT as {TERM_ID}
35+
And I run `wp term create category "Second Category" --porcelain`
36+
And save STDOUT as {SECOND_TERM_ID}
37+
38+
When I run `wp post create --post_title="Test category" --post_category="First Category" --porcelain`
39+
Then STDOUT should be a number
40+
And save STDOUT as {POST_ID}
41+
42+
When I run `wp post term list {POST_ID} category --field=term_id`
43+
Then STDOUT should be:
44+
"""
45+
{TERM_ID}
46+
"""
47+
48+
When I run `wp post update {POST_ID} --post_category={SECOND_TERM_ID}`
49+
Then STDOUT should be:
50+
"""
51+
Success: Updated post {POST_ID}.
52+
"""
53+
54+
When I run `wp post term list {POST_ID} category --field=term_id`
55+
Then STDOUT should be:
56+
"""
57+
{SECOND_TERM_ID}
58+
"""
59+
60+
When I run `wp post update {POST_ID} --post_category='Uncategorized,{TERM_ID},Second Category'`
61+
Then STDOUT should be:
62+
"""
63+
Success: Updated post {POST_ID}.
64+
"""
65+
66+
When I run `wp post term list {POST_ID} category --field=term_id`
67+
And save STDOUT as {MULTI_CATEGORIES_STDOUT}
68+
Then STDOUT should contain:
69+
"""
70+
{TERM_ID}
71+
"""
72+
And STDOUT should contain:
73+
"""
74+
{SECOND_TERM_ID}
75+
"""
76+
And STDOUT should contain:
77+
"""
78+
1
79+
"""
80+
81+
# Blank categories with non-blank ignored.
82+
When I run `wp post update {POST_ID} --post_category='Uncategorized, ,{TERM_ID},Second Category,'`
83+
Then STDOUT should be:
84+
"""
85+
Success: Updated post {POST_ID}.
86+
"""
87+
88+
When I run `wp post term list {POST_ID} category --field=term_id`
89+
Then STDOUT should be:
90+
"""
91+
{MULTI_CATEGORIES_STDOUT}
92+
"""
93+
94+
# Zero category same as default Uncategorized (1) category.
95+
When I try `wp post update {POST_ID} --post_category=0`
96+
Then STDOUT should be:
97+
"""
98+
Success: Updated post {POST_ID}.
99+
"""
100+
101+
When I run `wp post term list {POST_ID} category --field=term_id`
102+
Then STDOUT should be:
103+
"""
104+
1
105+
"""
106+
107+
# Blank category/categories same as default Uncategorized (1) category.
108+
When I try `wp post update {POST_ID} --post_category=,`
109+
Then STDOUT should be:
110+
"""
111+
Success: Updated post {POST_ID}.
112+
"""
113+
114+
When I run `wp post term list {POST_ID} category --field=term_id`
115+
Then STDOUT should be:
116+
"""
117+
1
118+
"""
119+
120+
# Null category same as no categories.
121+
When I try `wp post update {POST_ID} --post_category=' '`
122+
Then STDOUT should be:
123+
"""
124+
Success: Updated post {POST_ID}.
125+
"""
126+
127+
When I run `wp post term list {POST_ID} category --field=term_id`
128+
Then STDOUT should be empty
129+
130+
# Non-existent category.
131+
When I try `wp post update {POST_ID} --post_category=test`
132+
Then STDERR should be:
133+
"""
134+
Error: No such post category 'test'.
135+
"""
136+
137+
When I try `wp post create --post_title="Non-existent Category" --post_category={SECOND_TERM_ID},Test --porcelain`
138+
Then STDERR should be:
139+
"""
140+
Error: No such post category 'Test'.
141+
"""
142+
143+
# Error on first non-existent category found.
144+
When I try `wp post create --post_title="More than one non-existent Category" --post_category={SECOND_TERM_ID},Test,Bad --porcelain`
145+
Then STDERR should be:
146+
"""
147+
Error: No such post category 'Test'.
148+
"""
149+
32150
Scenario: Creating/getting/editing posts
33151
Given a content.html file:
34152
"""

src/Post_Command.php

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ public function create( $args, $assoc_args ) {
163163
}
164164

165165
if ( isset( $assoc_args['post_category'] ) ) {
166-
$assoc_args['post_category'] = explode( ',', $assoc_args['post_category'] );
166+
$assoc_args['post_category'] = $this->get_category_ids( $assoc_args['post_category'] );
167167
}
168168

169169
$array_arguments = array( 'meta_input' );
@@ -293,7 +293,7 @@ public function update( $args, $assoc_args ) {
293293
}
294294

295295
if ( isset( $assoc_args['post_category'] ) ) {
296-
$assoc_args['post_category'] = explode( ',', $assoc_args['post_category'] );
296+
$assoc_args['post_category'] = $this->get_category_ids( $assoc_args['post_category'] );
297297
}
298298

299299
$array_arguments = array( 'meta_input' );
@@ -762,4 +762,31 @@ private function read_from_file_or_stdin( $arg ) {
762762
}
763763
return file_get_contents( $readfile );
764764
}
765+
766+
/**
767+
* Resolves post_category arg into an array of category ids.
768+
*
769+
* @param string $arg Supplied argument.
770+
* @return array
771+
*/
772+
private function get_category_ids( $arg ) {
773+
774+
$categories = explode( ',', $arg );
775+
$category_ids = array();
776+
foreach ( $categories as $post_category ) {
777+
if ( trim( $post_category ) ) {
778+
if ( is_numeric( $post_category ) && (int) $post_category ) {
779+
$category_id = category_exists( (int) $post_category );
780+
} else {
781+
$category_id = category_exists( $post_category );
782+
}
783+
if ( ! $category_id ) {
784+
WP_CLI::error( "No such post category '$post_category'." );
785+
}
786+
$category_ids[] = $category_id;
787+
}
788+
}
789+
// If no category ids found, return exploded array for compat with previous WP-CLI versions.
790+
return $category_ids ? $category_ids : $categories;
791+
}
765792
}

0 commit comments

Comments
 (0)