Skip to content

Commit 3cc06a9

Browse files
Copilotswissspidy
andcommitted
Add ID range expansion support to post, comment, user, term, and site commands
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
1 parent 9950d33 commit 3cc06a9

11 files changed

Lines changed: 347 additions & 1 deletion

features/comment.feature

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,3 +472,32 @@ Feature: Manage WordPress comments
472472
And I run `wp comment unspam {COMMENT_ID} --url=www.example.com`
473473
And I run `wp comment trash {COMMENT_ID} --url=www.example.com`
474474
And I run `wp comment untrash {COMMENT_ID} --url=www.example.com`
475+
476+
Scenario: Delete comments using ID ranges
477+
Given a WP install
478+
479+
When I run `wp comment create --comment_post_ID=1 --comment_content='Comment A' --comment_author='A' --porcelain`
480+
Then STDOUT should be a number
481+
And save STDOUT as {COMMENT_ID_1}
482+
483+
When I run `wp comment create --comment_post_ID=1 --comment_content='Comment B' --comment_author='B' --porcelain`
484+
Then STDOUT should be a number
485+
And save STDOUT as {COMMENT_ID_2}
486+
487+
When I run `wp comment create --comment_post_ID=1 --comment_content='Comment C' --comment_author='C' --porcelain`
488+
Then STDOUT should be a number
489+
And save STDOUT as {COMMENT_ID_3}
490+
491+
When I run `wp comment delete {COMMENT_ID_1}-{COMMENT_ID_3} --force`
492+
Then STDOUT should contain:
493+
"""
494+
Deleted comment {COMMENT_ID_1}.
495+
"""
496+
And STDOUT should contain:
497+
"""
498+
Deleted comment {COMMENT_ID_2}.
499+
"""
500+
And STDOUT should contain:
501+
"""
502+
Deleted comment {COMMENT_ID_3}.
503+
"""

features/post.feature

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,3 +602,58 @@ Feature: Manage WordPress posts
602602
"""
603603
{"block_version":1}
604604
"""
605+
606+
607+
Scenario: Delete posts using ID ranges
608+
When I run `wp post create --post_title='Post A' --porcelain`
609+
Then STDOUT should be a number
610+
And save STDOUT as {POST_ID_1}
611+
612+
When I run `wp post create --post_title='Post B' --porcelain`
613+
Then STDOUT should be a number
614+
And save STDOUT as {POST_ID_2}
615+
616+
When I run `wp post create --post_title='Post C' --porcelain`
617+
Then STDOUT should be a number
618+
And save STDOUT as {POST_ID_3}
619+
620+
When I run `wp post delete {POST_ID_1}-{POST_ID_3} --force`
621+
Then STDOUT should contain:
622+
"""
623+
Deleted post {POST_ID_1}.
624+
"""
625+
And STDOUT should contain:
626+
"""
627+
Deleted post {POST_ID_2}.
628+
"""
629+
And STDOUT should contain:
630+
"""
631+
Deleted post {POST_ID_3}.
632+
"""
633+
634+
Scenario: Update posts using ID ranges
635+
When I run `wp post create --post_title='Post A' --post_status=draft --porcelain`
636+
Then STDOUT should be a number
637+
And save STDOUT as {POST_ID_1}
638+
639+
When I run `wp post create --post_title='Post B' --post_status=draft --porcelain`
640+
Then STDOUT should be a number
641+
And save STDOUT as {POST_ID_2}
642+
643+
When I run `wp post create --post_title='Post C' --post_status=draft --porcelain`
644+
Then STDOUT should be a number
645+
And save STDOUT as {POST_ID_3}
646+
647+
When I run `wp post update {POST_ID_1}-{POST_ID_3} --post_status=publish`
648+
Then STDOUT should contain:
649+
"""
650+
Updated post {POST_ID_1}.
651+
"""
652+
And STDOUT should contain:
653+
"""
654+
Updated post {POST_ID_2}.
655+
"""
656+
And STDOUT should contain:
657+
"""
658+
Updated post {POST_ID_3}.
659+
"""

features/term.feature

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,3 +298,30 @@ Feature: Manage WordPress terms
298298
"term_group":0
299299
}]
300300
"""
301+
302+
Scenario: Delete terms using ID ranges
303+
When I run `wp term create category 'Range Term A' --porcelain`
304+
Then STDOUT should be a number
305+
And save STDOUT as {TERM_ID_1}
306+
307+
When I run `wp term create category 'Range Term B' --porcelain`
308+
Then STDOUT should be a number
309+
And save STDOUT as {TERM_ID_2}
310+
311+
When I run `wp term create category 'Range Term C' --porcelain`
312+
Then STDOUT should be a number
313+
And save STDOUT as {TERM_ID_3}
314+
315+
When I run `wp term delete category {TERM_ID_1}-{TERM_ID_3}`
316+
Then STDOUT should contain:
317+
"""
318+
Deleted category {TERM_ID_1}.
319+
"""
320+
And STDOUT should contain:
321+
"""
322+
Deleted category {TERM_ID_2}.
323+
"""
324+
And STDOUT should contain:
325+
"""
326+
Deleted category {TERM_ID_3}.
327+
"""

features/user.feature

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -774,3 +774,30 @@ Feature: Manage WordPress users
774774
"""
775775
newtestuser
776776
"""
777+
778+
Scenario: Delete users using ID ranges
779+
When I run `wp user create testrange1 testrange1@example.com --role=subscriber --porcelain`
780+
Then STDOUT should be a number
781+
And save STDOUT as {USER_ID_1}
782+
783+
When I run `wp user create testrange2 testrange2@example.com --role=subscriber --porcelain`
784+
Then STDOUT should be a number
785+
And save STDOUT as {USER_ID_2}
786+
787+
When I run `wp user create testrange3 testrange3@example.com --role=subscriber --porcelain`
788+
Then STDOUT should be a number
789+
And save STDOUT as {USER_ID_3}
790+
791+
When I run `wp user delete {USER_ID_1}-{USER_ID_3} --yes`
792+
Then STDOUT should contain:
793+
"""
794+
Removed user {USER_ID_1}
795+
"""
796+
And STDOUT should contain:
797+
"""
798+
Removed user {USER_ID_2}
799+
"""
800+
And STDOUT should contain:
801+
"""
802+
Removed user {USER_ID_3}
803+
"""

src/Comment_Command.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ function ( $params ) {
150150
* @param array<string, mixed> $assoc_args Associative arguments.
151151
*/
152152
public function update( $args, $assoc_args ) {
153+
$args = self::expand_id_ranges( $args, [ $this, 'get_comment_ids_in_range' ] );
153154
$assoc_args = wp_slash( $assoc_args );
154155
parent::_update(
155156
$args,
@@ -486,6 +487,7 @@ function ( $comment ) {
486487
* Success: Deleted comment 2341.
487488
*/
488489
public function delete( $args, $assoc_args ) {
490+
$args = self::expand_id_ranges( $args, [ $this, 'get_comment_ids_in_range' ] );
489491
parent::_delete(
490492
$args,
491493
$assoc_args,
@@ -556,6 +558,7 @@ private function check_server_name() {
556558
* Success: Trashed comment 1337.
557559
*/
558560
public function trash( $args, $assoc_args ) {
561+
$args = self::expand_id_ranges( $args, [ $this, 'get_comment_ids_in_range' ] );
559562
foreach ( $args as $id ) {
560563
$this->call( $id, __FUNCTION__, 'Trashed %s.', 'Failed trashing %s.' );
561564
}
@@ -577,6 +580,7 @@ public function trash( $args, $assoc_args ) {
577580
*/
578581
public function untrash( $args, $assoc_args ) {
579582
$this->check_server_name();
583+
$args = self::expand_id_ranges( $args, [ $this, 'get_comment_ids_in_range' ] );
580584
foreach ( $args as $id ) {
581585
$this->call( $id, __FUNCTION__, 'Untrashed %s.', 'Failed untrashing %s.' );
582586
}
@@ -597,6 +601,7 @@ public function untrash( $args, $assoc_args ) {
597601
* Success: Marked as spam comment 1337.
598602
*/
599603
public function spam( $args, $assoc_args ) {
604+
$args = self::expand_id_ranges( $args, [ $this, 'get_comment_ids_in_range' ] );
600605
foreach ( $args as $id ) {
601606
$this->call( $id, __FUNCTION__, 'Marked %s as spam.', 'Failed marking %s as spam.' );
602607
}
@@ -618,6 +623,7 @@ public function spam( $args, $assoc_args ) {
618623
*/
619624
public function unspam( $args, $assoc_args ) {
620625
$this->check_server_name();
626+
$args = self::expand_id_ranges( $args, [ $this, 'get_comment_ids_in_range' ] );
621627
foreach ( $args as $id ) {
622628
$this->call( $id, __FUNCTION__, 'Unspammed %s.', 'Failed unspamming %s.' );
623629
}
@@ -639,6 +645,7 @@ public function unspam( $args, $assoc_args ) {
639645
*/
640646
public function approve( $args, $assoc_args ) {
641647
$this->check_server_name();
648+
$args = self::expand_id_ranges( $args, [ $this, 'get_comment_ids_in_range' ] );
642649
foreach ( $args as $id ) {
643650
$this->set_status( $id, 'approve', 'Approved' );
644651
}
@@ -660,6 +667,7 @@ public function approve( $args, $assoc_args ) {
660667
*/
661668
public function unapprove( $args, $assoc_args ) {
662669
$this->check_server_name();
670+
$args = self::expand_id_ranges( $args, [ $this, 'get_comment_ids_in_range' ] );
663671
foreach ( $args as $id ) {
664672
$this->set_status( $id, 'hold', 'Unapproved' );
665673
}
@@ -786,4 +794,27 @@ public function exists( $args ) {
786794
WP_CLI::success( "Comment with ID {$args[0]} exists." );
787795
}
788796
}
797+
798+
/**
799+
* Returns existing comment IDs within the given range.
800+
*
801+
* @param int $start Start of the ID range (inclusive).
802+
* @param int|null $end End of the ID range (inclusive), or null for no upper bound.
803+
* @return int[] List of existing comment IDs.
804+
*/
805+
private function get_comment_ids_in_range( int $start, ?int $end ): array {
806+
global $wpdb;
807+
808+
if ( null === $end ) {
809+
return array_map(
810+
'intval',
811+
$wpdb->get_col( $wpdb->prepare( "SELECT comment_ID FROM {$wpdb->comments} WHERE comment_ID >= %d ORDER BY comment_ID ASC", $start ) )
812+
);
813+
}
814+
815+
return array_map(
816+
'intval',
817+
$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 ) )
818+
);
819+
}
789820
}

src/Post_Command.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,7 @@ function ( $params ) {
371371
* Success: Updated post 456.
372372
*/
373373
public function update( $args, $assoc_args ) {
374+
$args = self::expand_id_ranges( $args, [ $this, 'get_post_ids_in_range' ] );
374375

375376
foreach ( $args as $key => $arg ) {
376377
if ( is_numeric( $arg ) ) {
@@ -561,6 +562,7 @@ public function get( $args, $assoc_args ) {
561562
* Success: Deleted post 1294.
562563
*/
563564
public function delete( $args, $assoc_args ) {
565+
$args = self::expand_id_ranges( $args, [ $this, 'get_post_ids_in_range' ] );
564566
$defaults = [ 'force' => false ];
565567
$assoc_args = array_merge( $defaults, $assoc_args );
566568

@@ -1241,4 +1243,27 @@ private function maybe_convert_hyphenated_date_format( $date_string ) {
12411243
}
12421244
return $date_string;
12431245
}
1246+
1247+
/**
1248+
* Returns existing post IDs within the given range.
1249+
*
1250+
* @param int $start Start of the ID range (inclusive).
1251+
* @param int|null $end End of the ID range (inclusive), or null for no upper bound.
1252+
* @return int[] List of existing post IDs.
1253+
*/
1254+
private function get_post_ids_in_range( int $start, ?int $end ): array {
1255+
global $wpdb;
1256+
1257+
if ( null === $end ) {
1258+
return array_map(
1259+
'intval',
1260+
$wpdb->get_col( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE ID >= %d ORDER BY ID ASC", $start ) )
1261+
);
1262+
}
1263+
1264+
return array_map(
1265+
'intval',
1266+
$wpdb->get_col( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE ID BETWEEN %d AND %d ORDER BY ID ASC", $start, $end ) )
1267+
);
1268+
}
12441269
}

src/Site_Command.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1463,7 +1463,7 @@ private function get_sites_ids( $args, $assoc_args ) {
14631463
return [ $blog_id ];
14641464
}
14651465

1466-
return $args;
1466+
return self::expand_id_ranges( $args, [ $this, 'get_site_ids_in_range' ] );
14671467
}
14681468

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

14831483
return true;
14841484
}
1485+
1486+
/**
1487+
* Returns existing site IDs within the given range.
1488+
*
1489+
* @param int $start Start of the ID range (inclusive).
1490+
* @param int|null $end End of the ID range (inclusive), or null for no upper bound.
1491+
* @return int[] List of existing site IDs.
1492+
*/
1493+
private function get_site_ids_in_range( int $start, ?int $end ): array {
1494+
global $wpdb;
1495+
1496+
if ( null === $end ) {
1497+
return array_map(
1498+
'intval',
1499+
$wpdb->get_col( $wpdb->prepare( "SELECT blog_id FROM {$wpdb->blogs} WHERE blog_id >= %d ORDER BY blog_id ASC", $start ) )
1500+
);
1501+
}
1502+
1503+
return array_map(
1504+
'intval',
1505+
$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 ) )
1506+
);
1507+
}
14851508
}

0 commit comments

Comments
 (0)