Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions features/comment.feature
Original file line number Diff line number Diff line change
Expand Up @@ -472,3 +472,32 @@ Feature: Manage WordPress comments
And I run `wp comment unspam {COMMENT_ID} --url=www.example.com`
And I run `wp comment trash {COMMENT_ID} --url=www.example.com`
And I run `wp comment untrash {COMMENT_ID} --url=www.example.com`

Scenario: Delete comments using ID ranges
Given a WP install

When I run `wp comment create --comment_post_ID=1 --comment_content='Comment A' --comment_author='A' --porcelain`
Then STDOUT should be a number
And save STDOUT as {COMMENT_ID_1}

When I run `wp comment create --comment_post_ID=1 --comment_content='Comment B' --comment_author='B' --porcelain`
Then STDOUT should be a number
And save STDOUT as {COMMENT_ID_2}

When I run `wp comment create --comment_post_ID=1 --comment_content='Comment C' --comment_author='C' --porcelain`
Then STDOUT should be a number
And save STDOUT as {COMMENT_ID_3}

When I run `wp comment delete {COMMENT_ID_1}-{COMMENT_ID_3} --force`
Then STDOUT should contain:
"""
Deleted comment {COMMENT_ID_1}.
"""
And STDOUT should contain:
"""
Deleted comment {COMMENT_ID_2}.
"""
And STDOUT should contain:
"""
Deleted comment {COMMENT_ID_3}.
"""
58 changes: 58 additions & 0 deletions features/post.feature
Original file line number Diff line number Diff line change
Expand Up @@ -602,3 +602,61 @@ Feature: Manage WordPress posts
"""
{"block_version":1}
"""

Scenario: Delete posts using ID ranges
Given a WP install

When I run `wp post create --post_title='Post A' --porcelain`
Then STDOUT should be a number
And save STDOUT as {POST_ID_1}

When I run `wp post create --post_title='Post B' --porcelain`
Then STDOUT should be a number
And save STDOUT as {POST_ID_2}

When I run `wp post create --post_title='Post C' --porcelain`
Then STDOUT should be a number
And save STDOUT as {POST_ID_3}

When I run `wp post delete {POST_ID_1}-{POST_ID_3} --force`
Then STDOUT should contain:
"""
Deleted post {POST_ID_1}.
"""
And STDOUT should contain:
"""
Deleted post {POST_ID_2}.
"""
And STDOUT should contain:
"""
Deleted post {POST_ID_3}.
"""

Scenario: Update posts using ID ranges
Given a WP install

When I run `wp post create --post_title='Post A' --post_status=draft --porcelain`
Then STDOUT should be a number
And save STDOUT as {POST_ID_1}

When I run `wp post create --post_title='Post B' --post_status=draft --porcelain`
Then STDOUT should be a number
And save STDOUT as {POST_ID_2}

When I run `wp post create --post_title='Post C' --post_status=draft --porcelain`
Then STDOUT should be a number
And save STDOUT as {POST_ID_3}

When I run `wp post update {POST_ID_1}-{POST_ID_3} --post_status=publish`
Then STDOUT should contain:
"""
Updated post {POST_ID_1}.
"""
And STDOUT should contain:
"""
Updated post {POST_ID_2}.
"""
And STDOUT should contain:
"""
Updated post {POST_ID_3}.
"""
29 changes: 29 additions & 0 deletions features/term.feature
Original file line number Diff line number Diff line change
Expand Up @@ -298,3 +298,32 @@ Feature: Manage WordPress terms
"term_group":0
}]
"""

Scenario: Delete terms using ID ranges
Given a WP install

When I run `wp term create category 'Range Term A' --porcelain`
Then STDOUT should be a number
And save STDOUT as {TERM_ID_1}

When I run `wp term create category 'Range Term B' --porcelain`
Then STDOUT should be a number
And save STDOUT as {TERM_ID_2}

When I run `wp term create category 'Range Term C' --porcelain`
Then STDOUT should be a number
And save STDOUT as {TERM_ID_3}

When I run `wp term delete category {TERM_ID_1}-{TERM_ID_3}`
Then STDOUT should contain:
"""
Deleted category {TERM_ID_1}.
"""
And STDOUT should contain:
"""
Deleted category {TERM_ID_2}.
"""
And STDOUT should contain:
"""
Deleted category {TERM_ID_3}.
"""
29 changes: 29 additions & 0 deletions features/user.feature
Original file line number Diff line number Diff line change
Expand Up @@ -774,3 +774,32 @@ Feature: Manage WordPress users
"""
newtestuser
"""

Scenario: Delete users using ID ranges
Given a WP install

When I run `wp user create testrange1 testrange1@example.com --role=subscriber --porcelain`
Then STDOUT should be a number
And save STDOUT as {USER_ID_1}

When I run `wp user create testrange2 testrange2@example.com --role=subscriber --porcelain`
Then STDOUT should be a number
And save STDOUT as {USER_ID_2}

When I run `wp user create testrange3 testrange3@example.com --role=subscriber --porcelain`
Then STDOUT should be a number
And save STDOUT as {USER_ID_3}

When I run `wp user delete {USER_ID_1}-{USER_ID_3} --yes`
Then STDOUT should contain:
"""
Removed user {USER_ID_1}
"""
And STDOUT should contain:
"""
Removed user {USER_ID_2}
"""
And STDOUT should contain:
"""
Removed user {USER_ID_3}
"""
1 change: 1 addition & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
<rule ref="WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedNamespaceFound">
<exclude-pattern>*/src/WP_CLI/Fetchers/(Comment|Post|Signup|Site|User)\.php$</exclude-pattern>
<exclude-pattern>*/src/WP_CLI/CommandWith(DBObject|Meta|Terms)\.php$</exclude-pattern>
<exclude-pattern>*/src/WP_CLI/ExpandsIdRanges\.php$</exclude-pattern>
</rule>

<rule ref="WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedClassFound">
Expand Down
31 changes: 31 additions & 0 deletions src/Comment_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ function ( $params ) {
* @param array<string, mixed> $assoc_args Associative arguments.
*/
public function update( $args, $assoc_args ) {
$args = self::expand_id_ranges( $args, [ $this, 'get_comment_ids_in_range' ] );
$assoc_args = wp_slash( $assoc_args );
parent::_update(
$args,
Expand Down Expand Up @@ -486,6 +487,7 @@ function ( $comment ) {
* Success: Deleted comment 2341.
*/
public function delete( $args, $assoc_args ) {
$args = self::expand_id_ranges( $args, [ $this, 'get_comment_ids_in_range' ] );
parent::_delete(
$args,
$assoc_args,
Expand Down Expand Up @@ -556,6 +558,7 @@ private function check_server_name() {
* Success: Trashed comment 1337.
*/
public function trash( $args, $assoc_args ) {
$args = self::expand_id_ranges( $args, [ $this, 'get_comment_ids_in_range' ] );
foreach ( $args as $id ) {
$this->call( $id, __FUNCTION__, 'Trashed %s.', 'Failed trashing %s.' );
}
Expand All @@ -577,6 +580,7 @@ public function trash( $args, $assoc_args ) {
*/
public function untrash( $args, $assoc_args ) {
$this->check_server_name();
$args = self::expand_id_ranges( $args, [ $this, 'get_comment_ids_in_range' ] );
foreach ( $args as $id ) {
$this->call( $id, __FUNCTION__, 'Untrashed %s.', 'Failed untrashing %s.' );
}
Expand All @@ -597,6 +601,7 @@ public function untrash( $args, $assoc_args ) {
* Success: Marked as spam comment 1337.
*/
public function spam( $args, $assoc_args ) {
$args = self::expand_id_ranges( $args, [ $this, 'get_comment_ids_in_range' ] );
foreach ( $args as $id ) {
Comment on lines 560 to 605
$this->call( $id, __FUNCTION__, 'Marked %s as spam.', 'Failed marking %s as spam.' );
}
Expand All @@ -618,6 +623,7 @@ public function spam( $args, $assoc_args ) {
*/
public function unspam( $args, $assoc_args ) {
$this->check_server_name();
$args = self::expand_id_ranges( $args, [ $this, 'get_comment_ids_in_range' ] );
foreach ( $args as $id ) {
$this->call( $id, __FUNCTION__, 'Unspammed %s.', 'Failed unspamming %s.' );
}
Expand All @@ -639,6 +645,7 @@ public function unspam( $args, $assoc_args ) {
*/
public function approve( $args, $assoc_args ) {
$this->check_server_name();
$args = self::expand_id_ranges( $args, [ $this, 'get_comment_ids_in_range' ] );
foreach ( $args as $id ) {
$this->set_status( $id, 'approve', 'Approved' );
}
Expand All @@ -660,6 +667,7 @@ public function approve( $args, $assoc_args ) {
*/
public function unapprove( $args, $assoc_args ) {
$this->check_server_name();
$args = self::expand_id_ranges( $args, [ $this, 'get_comment_ids_in_range' ] );
foreach ( $args as $id ) {
$this->set_status( $id, 'hold', 'Unapproved' );
}
Expand Down Expand Up @@ -786,4 +794,27 @@ public function exists( $args ) {
WP_CLI::success( "Comment with ID {$args[0]} exists." );
}
}

/**
* Returns existing comment IDs within the given range.
*
* @param int $start Start of the ID range (inclusive).
* @param int|null $end End of the ID range (inclusive), or null for no upper bound.
* @return int[] List of existing comment IDs.
*/
protected function get_comment_ids_in_range( int $start, ?int $end ): array {
global $wpdb;

if ( null === $end ) {
return array_map(
'intval',
$wpdb->get_col( $wpdb->prepare( "SELECT comment_ID FROM {$wpdb->comments} WHERE comment_ID >= %d ORDER BY comment_ID ASC", $start ) )
);
}

return array_map(
'intval',
$wpdb->get_col( $wpdb->prepare( "SELECT comment_ID FROM {$wpdb->comments} WHERE comment_ID BETWEEN %d AND %d ORDER BY comment_ID ASC", $start, $end ) )
);
}
}
25 changes: 25 additions & 0 deletions src/Post_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,7 @@ function ( $params ) {
* Success: Updated post 456.
*/
public function update( $args, $assoc_args ) {
$args = self::expand_id_ranges( $args, [ $this, 'get_post_ids_in_range' ] );

foreach ( $args as $key => $arg ) {
if ( is_numeric( $arg ) ) {
Expand Down Expand Up @@ -561,6 +562,7 @@ public function get( $args, $assoc_args ) {
* Success: Deleted post 1294.
*/
public function delete( $args, $assoc_args ) {
$args = self::expand_id_ranges( $args, [ $this, 'get_post_ids_in_range' ] );
$defaults = [ 'force' => false ];
$assoc_args = array_merge( $defaults, $assoc_args );

Expand Down Expand Up @@ -1241,4 +1243,27 @@ private function maybe_convert_hyphenated_date_format( $date_string ) {
}
return $date_string;
}

/**
* Returns existing post IDs within the given range.
*
* @param int $start Start of the ID range (inclusive).
* @param int|null $end End of the ID range (inclusive), or null for no upper bound.
* @return int[] List of existing post IDs.
*/
protected function get_post_ids_in_range( int $start, ?int $end ): array {
global $wpdb;

if ( null === $end ) {
return array_map(
'intval',
$wpdb->get_col( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE ID >= %d ORDER BY ID ASC", $start ) )
);
}

return array_map(
'intval',
$wpdb->get_col( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE ID BETWEEN %d AND %d ORDER BY ID ASC", $start, $end ) )
);
}
}
25 changes: 24 additions & 1 deletion src/Site_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -1463,7 +1463,7 @@ private function get_sites_ids( $args, $assoc_args ) {
return [ $blog_id ];
}

return $args;
return self::expand_id_ranges( $args, [ $this, 'get_site_ids_in_range' ] );
}
Comment on lines 1463 to 1467

/**
Expand All @@ -1482,4 +1482,27 @@ private function check_site_ids_and_slug( $args, $assoc_args ) {

return true;
}

/**
* Returns existing site IDs within the given range.
*
* @param int $start Start of the ID range (inclusive).
* @param int|null $end End of the ID range (inclusive), or null for no upper bound.
* @return int[] List of existing site IDs.
*/
protected function get_site_ids_in_range( int $start, ?int $end ): array {
global $wpdb;

if ( null === $end ) {
return array_map(
'intval',
$wpdb->get_col( $wpdb->prepare( "SELECT blog_id FROM {$wpdb->blogs} WHERE blog_id >= %d ORDER BY blog_id ASC", $start ) )
);
}

return array_map(
'intval',
$wpdb->get_col( $wpdb->prepare( "SELECT blog_id FROM {$wpdb->blogs} WHERE blog_id BETWEEN %d AND %d ORDER BY blog_id ASC", $start, $end ) )
);
}
}
Loading
Loading