diff --git a/inc/cli/class-migrate-command.php b/inc/cli/class-migrate-command.php index c8d5893..017303b 100644 --- a/inc/cli/class-migrate-command.php +++ b/inc/cli/class-migrate-command.php @@ -49,6 +49,12 @@ class Migrate_Command extends WP_CLI_Command { * - false * --- * + * [--post-type=] + * : Post type, or comma separated list of post types. + * --- + * default: post + * --- + * * ## EXAMPLES * * wp authorship migrate wp-authors --dry-run=true @@ -79,6 +85,9 @@ public function wp_authors( $args, $assoc_args ) : void { WP_CLI::warning( 'Overwriting of previous Authorship data is set to true.' ); } + $post_types = explode( ',', $assoc_args['post-type'] ); + WP_CLI::line( sprintf( 'Updating post types: %s', implode( ', ', $post_types ) ) ); + $tax_query = $overwrite ? [] : [ [ 'taxonomy' => 'authorship', @@ -93,6 +102,7 @@ public function wp_authors( $args, $assoc_args ) : void { $posts = get_posts( [ 'posts_per_page' => $posts_per_page, 'paged' => $paged, + 'post_type' => $post_types, 'post_status' => 'any', 'ignore_sticky_posts' => true, 'suppress_filters' => false, diff --git a/tests/phpunit/test-cli.php b/tests/phpunit/test-cli.php new file mode 100644 index 0000000..42f7392 --- /dev/null +++ b/tests/phpunit/test-cli.php @@ -0,0 +1,78 @@ +post; + + // Post. Owned by editor, attributed to nobody. + $post1 = $factory->create_and_get( [ + 'post_author' => self::$users['editor']->ID, + ] ); + + // Unset author data. + wp_set_post_terms( $post1->ID, [], TAXONOMY ); + + // Check initial authorship data unset. + $authorship_authors = \Authorship\get_authors( $post1 ); + $this->assertCount( 0, $authorship_authors ); + + $command = new CLI\Migrate_Command; + $command->wp_authors( [], [ + 'dry-run' => false, + 'post-type' => 'post', // Note, have to set default values manually. + ] ); + + // Check migration command has correctly set the author. + $authorship_authors = \Authorship\get_authors( $post1 ); + $this->assertCount( 1, $authorship_authors ); + $this->assertSame( self::$users['editor']->ID, $authorship_authors[0]->ID ); + } + + public function testMigratePostTypePage() : void { + $factory = self::factory()->post; + + // Page. Owned by editor, attributed to nobody. + $page1 = $factory->create_and_get( [ + 'post_author' => self::$users['editor']->ID, + 'post_type' => 'page', + ] ); + + // Unset author data. + wp_set_post_terms( $page1->ID, [], TAXONOMY ); + + // Check initial authorship data unset. + $authorship_authors = \Authorship\get_authors( $page1 ); + $this->assertCount( 0, $authorship_authors ); + + $command = new CLI\Migrate_Command(); + $command->wp_authors( [], [ + 'dry-run' => false, + 'post-type' => 'post,page', + ] ); + + // Check migration command has correctly set the author. + $authorship_authors = \Authorship\get_authors( $page1 ); + $this->assertCount( 1, $authorship_authors ); + $this->assertSame( self::$users['editor']->ID, $authorship_authors[0]->ID ); + } +}