Skip to content
Merged
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
63 changes: 63 additions & 0 deletions features/term-migrate.feature
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,66 @@ Feature: Migrate term custom fields
"""
Error: Taxonomy term 'peach' for taxonomy 'category' doesn't exist.
"""

@require-wp-4.4
Scenario: Migrate a term when posts have been migrated to a different post type that supports the destination taxonomy
Given a WP install
And a wp-content/mu-plugins/test-migrate.php file:
"""
<?php
// Plugin Name: Test Migrate

add_action( 'init', function() {
register_post_type( 'news', [ 'public' => true ] );
register_taxonomy( 'topic', 'news', [ 'public' => true ] );
} );
"""

When I run `wp term create category grape`
Then STDOUT should not be empty

When I run `wp post create --post_title='Test post' --post_type=post --porcelain`
Then STDOUT should be a number
And save STDOUT as {POST_ID}

When I run `wp post term set {POST_ID} category grape`
Then STDOUT should not be empty

When I run `wp post update {POST_ID} --post_type=news`
Then STDOUT should not be empty

When I run `wp term migrate grape --by=slug --from=category --to=topic`
Then STDOUT should be:
"""
Term 'grape' assigned to post {POST_ID}.
Term 'grape' migrated.
Old instance of term 'grape' removed from its original taxonomy.
Success: Migrated the term 'grape' from taxonomy 'category' to taxonomy 'topic' for 1 post.
"""

@require-wp-4.4
Scenario: Migrate a term warns when post type is not registered with destination taxonomy
Given a WP install

When I run `wp term create category grape`
Then STDOUT should not be empty

When I run `wp post create --post_title='Test post' --post_type=post --porcelain`
Then STDOUT should be a number
And save STDOUT as {POST_ID}

When I run `wp post term set {POST_ID} category grape`
Then STDOUT should not be empty

When I run `wp post update {POST_ID} --post_type=page`
Then STDOUT should not be empty

When I try `wp term migrate grape --by=slug --from=category --to=post_tag`
Then STDERR should contain:
"""
Warning: Term 'grape' not assigned to post {POST_ID}. Post type 'page' is not registered with taxonomy 'post_tag'.
"""
And STDOUT should contain:
"""
Success: Migrated the term 'grape' from taxonomy 'category' to taxonomy 'post_tag' for 0 posts.
"""
15 changes: 12 additions & 3 deletions src/Term_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -833,6 +833,12 @@ public function migrate( $args, $assoc_args ) {
WP_CLI::error( "Taxonomy '{$original_taxonomy}' doesn't exist." );
}

$dest_tax = get_taxonomy( $destination_taxonomy );

if ( ! $dest_tax ) {
WP_CLI::error( "Taxonomy '{$destination_taxonomy}' doesn't exist." );
}

$id = wp_insert_term(
$term->name,
$destination_taxonomy,
Expand All @@ -850,18 +856,22 @@ public function migrate( $args, $assoc_args ) {
/**
* @var string[] $post_ids
*/
$post_ids = get_objects_in_term( $term->term_id, $tax->name );
$post_ids = get_objects_in_term( $term->term_id, $tax->name );
$post_count = 0;

foreach ( $post_ids as $post_id ) {
$type = get_post_type( (int) $post_id );
if ( in_array( $type, $tax->object_type, true ) ) {
if ( in_array( $type, $dest_tax->object_type, true ) ) {
$term_taxonomy_id = wp_set_object_terms( (int) $post_id, $id['term_id'], $destination_taxonomy, true );

if ( is_wp_error( $term_taxonomy_id ) ) {
WP_CLI::error( "Failed to assign the term '{$term->slug}' to the post {$post_id}. Reason: " . $term_taxonomy_id->get_error_message() );
}

WP_CLI::log( "Term '{$term->slug}' assigned to post {$post_id}." );
++$post_count;
} else {
WP_CLI::warning( "Term '{$term->slug}' not assigned to post {$post_id}. Post type '{$type}' is not registered with taxonomy '{$destination_taxonomy}'." );
}

clean_post_cache( (int) $post_id );
Expand All @@ -878,7 +888,6 @@ public function migrate( $args, $assoc_args ) {
}

WP_CLI::log( "Old instance of term '{$term->slug}' removed from its original taxonomy." );
$post_count = count( $post_ids );
$post_plural = Utils\pluralize( 'post', $post_count );
WP_CLI::success( "Migrated the term '{$term->slug}' from taxonomy '{$tax->name}' to taxonomy '{$destination_taxonomy}' for {$post_count} {$post_plural}." );
}
Expand Down
Loading