-
Notifications
You must be signed in to change notification settings - Fork 94
Add ID range notation to commands that accept one or more IDs #593
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
base: main
Are you sure you want to change the base?
Changes from all commits
9950d33
3cc06a9
5e1bf72
ddec615
40062cd
3788b95
baac6f0
0854b02
f05f337
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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, | ||
|
|
@@ -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, | ||
|
|
@@ -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.' ); | ||
| } | ||
|
|
@@ -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.' ); | ||
| } | ||
|
|
@@ -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 ) { | ||
| $this->call( $id, __FUNCTION__, 'Marked %s as spam.', 'Failed marking %s as spam.' ); | ||
| } | ||
|
|
@@ -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.' ); | ||
| } | ||
|
|
@@ -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' ); | ||
| } | ||
|
|
@@ -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' ); | ||
| } | ||
|
|
@@ -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 ) ) | ||
| ); | ||
| } | ||
|
Comment on lines
+805
to
+819
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method can be made more concise and DRY by building the query dynamically instead of having two separate return paths. protected function get_comment_ids_in_range( int $start, ?int $end ): array {
global $wpdb;
$query = "SELECT comment_ID FROM {$wpdb->comments} WHERE ";
$params = [ $start ];
if ( null === $end ) {
$query .= 'comment_ID >= %d';
} else {
$query .= 'comment_ID BETWEEN %d AND %d';
$params[] = $end;
}
$query .= ' ORDER BY comment_ID ASC';
return array_map(
'intval',
$wpdb->get_col( $wpdb->prepare( $query, ...$params ) )
);
} |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 ) ) { | ||
|
|
@@ -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 ); | ||
|
|
||
|
|
@@ -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 ) ) | ||
| ); | ||
| } | ||
|
Comment on lines
+1254
to
+1268
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method can be made more concise and DRY by building the query dynamically instead of having two separate return paths. protected function get_post_ids_in_range( int $start, ?int $end ): array {
global $wpdb;
$query = "SELECT ID FROM {$wpdb->posts} WHERE ";
$params = [ $start ];
if ( null === $end ) {
$query .= 'ID >= %d';
} else {
$query .= 'ID BETWEEN %d AND %d';
$params[] = $end;
}
$query .= ' ORDER BY ID ASC';
return array_map(
'intval',
$wpdb->get_col( $wpdb->prepare( $query, ...$params ) )
);
} |
||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.