-
Notifications
You must be signed in to change notification settings - Fork 95
Add --all
flag to remove all terms from a post
#23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add --all
flag to remove all terms from a post
#23
Conversation
src/WP_CLI/CommandWithTerms.php
Outdated
|
||
// No need to specify terms while removing all terms. | ||
if ( $terms ) { | ||
WP_CLI::error( "No need to specify terms while removing all terms." ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need for double quotes here.
src/WP_CLI/CommandWithTerms.php
Outdated
if ( $terms ) { | ||
$result = wp_remove_object_terms( $object_id, $terms, $taxonomy ); | ||
} else { | ||
WP_CLI::error( "Please specify one or more terms." ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need for double quotes here.
src/WP_CLI/CommandWithTerms.php
Outdated
$cat_id = array( 1 ); | ||
$result = wp_set_object_terms( $object_id, $cat_id, $taxonomy, true ); | ||
} else { | ||
if ( $terms ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Negate to bail early here to avoid the second else
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@schlessera I did a change here. Was this the thing which you want?
Please add relevant functional tests for this. See https://make.wordpress.org/cli/handbook/pull-requests/#running-and-writing-tests |
236ee03
to
77ac9d8
Compare
I tried running behat test.
Am I doing something wrong? |
@schlessera All changes reported by you are done. Please review it. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not quite what I meant.
The general principle is that you want to reduce both indentation levels and the number of else
branches that you have in your code. This helps reduce overally complexity, because your code ends up having less branches going on at the same time.
To achieve this, one popular and useful method is to bail early where it makes sense.
This can be done here by turning the code around.
So, instead of this:
} else {
if ( $terms ) {
$result = ...
} else {
WP_CLI::error( ... );
}
}
we want to prefer this:
} else {
if ( ! $terms ) {
WP_CLI::error( ... );
}
$result = ...
}
This reduces the indentation level of the $result = ...
statement, because we completed one possible branch above by exiting the code early if our condition is not met.
--all
flag to remove all terms from a post
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add functional tests please? Here's an introduction: https://make.wordpress.org/cli/handbook/pull-requests/
src/WP_CLI/CommandWithTerms.php
Outdated
$result = wp_delete_object_term_relationships( $object_id, $taxonomy ); | ||
|
||
// Set post to default category. | ||
$cat_id = array( 1 ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should only be assigned if the taxonomy is 'category'. Also, the default category is a configurable option we should respect.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is done
@schlessera @danielbachhuber I have added the functional test for the feature. Please check this. |
src/WP_CLI/CommandWithTerms.php
Outdated
|
||
if ( 'category' === $taxonomy ) { | ||
// Set post to default category. | ||
$cat_id = array( 1 ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will need to use get_option( 'default_category' )
, because this is a user-configurable value.
features/post-term.feature
Outdated
When I run `wp post term remove 1 category --all` | ||
Then STDOUT should be: | ||
""" | ||
Success: Removed term. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we improve the message in this case to be:
Success: Removed all terms.
src/WP_CLI/CommandWithTerms.php
Outdated
@@ -130,7 +133,32 @@ public function remove( $args, $assoc_args ) { | |||
if ( $field = Utils\get_flag_value( $assoc_args, 'by' ) ) { | |||
$terms = $this->prepare_terms( $field, $terms, $taxonomy ); | |||
} | |||
$result = wp_remove_object_terms( $object_id, $terms, $taxonomy ); | |||
|
|||
if ( $field = Utils\get_flag_value( $assoc_args, 'all' ) ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not necessary to define $field
if you're not using it later.
features/post-term.feature
Outdated
""" | ||
Success: Removed term. | ||
""" | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should also have a test that the default category was added back to the post.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have added this. Please review this.
@schlessera @danielbachhuber All changes are done. Please confirm this. |
Concerns have been addressed
@BhargavBhandari90 Thanks for your work on this! |
Add parameter to remove all terms from the particular post
Fixes #18