diff --git a/composer.json b/composer.json index d002c66..a98fc7d 100644 --- a/composer.json +++ b/composer.json @@ -32,6 +32,7 @@ "files": [ "revisions-cli.php" ] }, "require-dev": { + "wp-cli/entity-command": "^1.3 || ^2", "wp-cli/wp-cli-tests": "^4" }, "minimum-stability": "dev", diff --git a/features/list.feature b/features/list.feature index cf1a45c..5185080 100644 --- a/features/list.feature +++ b/features/list.feature @@ -59,3 +59,17 @@ Feature: Revisions """ [] """ + + When I run `wp revisions generate 5 --post_type=page` + And I run `wp revisions list --post_type=page --format=count` + Then STDOUT should contain: + """ + 10 + """ + + When I run `wp post delete $(wp post list --post_type=page --field=ID) --force` + And I run `wp revisions list --post_type=page --format=count` + Then STDOUT should contain: + """ + 0 + """ diff --git a/inc/class-revisions-cli.php b/inc/class-revisions-cli.php index 0eedf85..e756386 100644 --- a/inc/class-revisions-cli.php +++ b/inc/class-revisions-cli.php @@ -104,19 +104,21 @@ function ( $i ) { wp_parse_slug_list( $assoc_args['post_type'] ) ); $where = sprintf( 'AND post_type IN ( %s )', implode( ',', $post_types ) ); - // get all IDs for posts in given post type(s). - $ids = $wpdb->get_col( "SELECT ID FROM $wpdb->posts WHERE 1=2 {$where}" ); - - // Prepare the IDs for inclusion in the query. - $post__in = array_map( 'esc_sql', $ids ); - $post__in = implode( ',', $ids ); + $ids = $wpdb->get_col( "SELECT ID FROM $wpdb->posts WHERE 1=1 {$where}" ); - // get revisions of those IDs. - $revs = $wpdb->get_results( - "SELECT * FROM $wpdb->posts WHERE post_type = 'revision' AND post_parent IN ({$post__in}) ORDER BY post_parent DESC" - ); + if ( $ids ) { + // Prepare the IDs for inclusion in the query. + $post__in = array_map( 'esc_sql', $ids ); + $post__in = implode( ',', $ids ); + // get revisions of those IDs. + $revs = $wpdb->get_results( + "SELECT * FROM $wpdb->posts WHERE post_type = 'revision' AND post_parent IN ({$post__in}) ORDER BY post_parent DESC" + ); + } else { + $revs = []; + } } else { $revs = $wpdb->get_results(