Skip to content

Commit

Permalink
Merge pull request #3744 from wp-cli/double-underscore
Browse files Browse the repository at this point in the history
Use `process_csv_arguments_to_arrays()` to process `__` CSV into arrays
  • Loading branch information
danielbachhuber committed Jan 17, 2017
2 parents 305b689 + aa1bdc6 commit c718d02
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 16 deletions.
31 changes: 31 additions & 0 deletions features/comment-list.feature
@@ -0,0 +1,31 @@
Feature: List WordPress comments

@require-wp-4.1
Scenario: Filter comments based on `comment__in` and `comment__not_in`
Given a WP install

When I run `wp comment create --comment_post_ID=1 --porcelain`
Then save STDOUT as {COMMENT_ID}

When I run `wp comment list --comment__in=1,{COMMENT_ID} --format=ids --orderby=comment_ID --order=ASC`
Then STDOUT should be:
"""
1 {COMMENT_ID}
"""

When I run `wp comment list --comment__in=1 --format=ids --orderby=comment_ID --order=ASC`
Then STDOUT should be:
"""
1
"""

When I run `wp comment list --comment__not_in=1,{COMMENT_ID} --format=ids --orderby=comment_ID --order=ASC`
Then STDOUT should be:
"""
"""

When I run `wp comment list --comment__not_in=1 --format=ids --orderby=comment_ID --order=ASC`
Then STDOUT should be:
"""
{COMMENT_ID}
"""
8 changes: 8 additions & 0 deletions features/post.feature
Expand Up @@ -194,6 +194,14 @@ Feature: Manage WordPress posts
Sample Page
"""

When I run `wp post list --post_type=any --fields=post_title,post_name,post_status --format=csv --orderby=post_title --order=ASC`
Then STDOUT should be CSV containing:
| post_title | post_name | post_status |
| Draft post | | draft |
| Hello world! | hello-world | publish |
| Publish post | publish-post | publish |
| Sample Page | sample-page | publish |

Scenario: Update categories on a post
When I run `wp term create category "Test Category" --porcelain`
Then save STDOUT as {TERM_ID}
Expand Down
15 changes: 15 additions & 0 deletions php/WP_CLI/CommandWithDBObject.php
Expand Up @@ -82,6 +82,21 @@ protected function _update( $args, $assoc_args, $callback ) {
exit( $status );
}

/**
* Transforms arguments with '__' from CSV into expected arrays
*
* @param array $assoc_args
* @return array
*/
protected static function process_csv_arguments_to_arrays( $assoc_args ) {
foreach( $assoc_args as $k => $v ) {
if ( false !== strpos( $k, '__' ) ) {
$assoc_args[ $k ] = explode( ',', $v );
}
}
return $assoc_args;
}

/**
* Delete a given database object.
* Exits with status.
Expand Down
4 changes: 1 addition & 3 deletions php/commands/comment.php
Expand Up @@ -323,9 +323,7 @@ public function list_( $_, $assoc_args ) {
$assoc_args['fields'] = 'comment_ID';
}

if ( ! empty( $assoc_args['comment__in'] ) ) {
$assoc_args['comment__in'] = explode( ',', $assoc_args['comment__in'] );
}
$assoc_args = self::process_csv_arguments_to_arrays( $assoc_args );

if ( ! empty( $assoc_args['comment__in'] )
&& ! empty( $assoc_args['orderby'] )
Expand Down
9 changes: 3 additions & 6 deletions php/commands/post.php
Expand Up @@ -370,12 +370,9 @@ public function list_( $_, $assoc_args ) {
'post_status' => 'any',
);
$query_args = array_merge( $defaults, $assoc_args );

foreach ( $query_args as $key => $query_arg ) {
if ( false !== strpos( $key, '__' )
|| ( 'post_type' == $key && 'any' != $query_arg ) ) {
$query_args[$key] = explode( ',', $query_arg );
}
$query_args = self::process_csv_arguments_to_arrays( $query_args );
if ( isset( $query_args['post_type'] ) && 'any' !== $query_args['post_type'] ) {
$query_args['post_type'] = explode( ',', $query_args['post_type'] );
}

if ( 'ids' == $formatter->format ) {
Expand Down
8 changes: 1 addition & 7 deletions php/commands/user.php
Expand Up @@ -144,13 +144,7 @@ public function list_( $args, $assoc_args ) {
}

$assoc_args['count_total'] = false;

foreach( $assoc_args as $k => $v ) {
if ( false !== strpos( $k, '__' ) ) {
$assoc_args[ $k ] = explode( ',', $v );
}
}

$assoc_args = self::process_csv_arguments_to_arrays( $assoc_args );
$users = get_users( $assoc_args );

if ( 'ids' == $formatter->format ) {
Expand Down

0 comments on commit c718d02

Please sign in to comment.