Skip to content

Commit e077559

Browse files
committed
update post 'create/update'
update code and fix tests
1 parent 0554764 commit e077559

File tree

2 files changed

+47
-14
lines changed

2 files changed

+47
-14
lines changed

features/post.feature

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

32+
When I run `wp term create category "First Category" --porcelain`
33+
And save STDOUT as {TERM_ID}
34+
And I run `wp term create category "Second Category" --porcelain`
35+
And save STDOUT as {SECOND_TERM_ID}
36+
37+
When I run `wp post create --post_title="Test category" --post_category="First Category" --porcelain`
38+
Then STDOUT should be a number
39+
And save STDOUT as {POST_ID}
40+
41+
When I run `wp post update {POST_ID} --post_category={SECOND_TERM_ID}`
42+
Then STDOUT should be:
43+
"""
44+
Success: Updated post {POST_ID}.
45+
"""
46+
47+
When I run `wp post update {POST_ID} --post_category=test --porcelain`
48+
Then STDOUT should be:
49+
"""
50+
Warning: Unable to set test as post category
51+
Success: Updated post {POST_ID}.
52+
"""
53+
3254
Scenario: Creating/getting/editing posts
3355
Given a content.html file:
3456
"""

src/Post_Command.php

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -159,13 +159,7 @@ public function create( $args, $assoc_args ) {
159159
}
160160

161161
if ( isset( $assoc_args['post_category'] ) ) {
162-
$assoc_args['post_category'] = explode( ',', $assoc_args['post_category'] );
163-
foreach ( $assoc_args['post_category'] as $post_category ) {
164-
$post_category = ( true === is_numeric( $post_category ) ) ? intval( $post_category ) : $post_category;
165-
$category_id = category_exists( $post_category );
166-
$category_ids[] = ( null !== $category_id ? $category_id : $post_category );
167-
}
168-
$assoc_args['post_category'] = $category_ids;
162+
$assoc_args['post_category'] = $this->get_category_ids( $assoc_args['post_category'] );
169163
}
170164

171165
$assoc_args = wp_slash( $assoc_args );
@@ -288,13 +282,7 @@ public function update( $args, $assoc_args ) {
288282
}
289283

290284
if ( isset( $assoc_args['post_category'] ) ) {
291-
$assoc_args['post_category'] = explode( ',', $assoc_args['post_category'] );
292-
foreach ( $assoc_args['post_category'] as $post_category ) {
293-
$post_category = ( true === is_numeric( $post_category ) ) ? intval( $post_category ) : $post_category;
294-
$category_id = category_exists( $post_category );
295-
$category_ids[] = ( null !== $category_id ? $category_id : $post_category );
296-
}
297-
$assoc_args['post_category'] = $category_ids;
285+
$assoc_args['post_category'] = $this->get_category_ids( $assoc_args['post_category'] );
298286
}
299287

300288
$assoc_args = wp_slash( $assoc_args );
@@ -760,4 +748,27 @@ private function read_from_file_or_stdin( $arg ) {
760748
}
761749
return file_get_contents( $readfile );
762750
}
751+
752+
/**
753+
* Get category id if given name/slug
754+
*
755+
* @param string $arg Supplied argument.
756+
* @return array
757+
*/
758+
private function get_category_ids( $arg ) {
759+
$categoires = explode( ',', $arg );
760+
761+
foreach ( $categoires as $post_category ) {
762+
$post_category = ( true === is_numeric( $post_category ) ) ? intval( $post_category ) : $post_category;
763+
764+
$category_id = category_exists( $post_category );
765+
if ( null === $category_id ) {
766+
$category_ids[] = $post_category;
767+
WP_CLI::warning( "Unable to set $post_category as post category" );
768+
} else {
769+
$category_ids[] = $category_id;
770+
}
771+
}
772+
return $category_ids;
773+
}
763774
}

0 commit comments

Comments
 (0)