@@ -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