Skip to content

Commit 6132db5

Browse files
authored
Merge pull request #234 from Mte90/convert-term
Migrate term from a taxonomy to another one
2 parents 5517499 + 0922845 commit 6132db5

File tree

2 files changed

+173
-0
lines changed

2 files changed

+173
-0
lines changed

features/term-migrate.feature

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
Feature: Manage term custom fields
2+
3+
@require-wp-4.4
4+
Scenario: Migrate an existing term by slug
5+
Given a WP install
6+
7+
When I run `wp term create category apple`
8+
Then STDOUT should not be empty
9+
10+
When I run `wp post create --post_title='Test post' --porcelain`
11+
Then STDOUT should be a number
12+
And save STDOUT as {POST_ID}
13+
14+
When I run `wp post term set {POST_ID} category apple`
15+
Then STDOUT should not be empty
16+
17+
When I run `wp term migrate apple --by=slug --from=category --to=post_tag`
18+
Then STDOUT should be:
19+
"""
20+
Term 'apple' assigned to post 4.
21+
Term 'apple' migrated.
22+
Old instance of term 'apple' removed from its original taxonomy.
23+
Success: Migrated the term 'apple' from taxonomy 'category' to taxonomy 'post_tag' for 1 post.
24+
"""
25+
26+
@require-wp-4.4
27+
Scenario: Migrate an existing term by ID
28+
Given a WP install
29+
30+
When I run `wp term create category apple --porcelain`
31+
Then STDOUT should be a number
32+
And save STDOUT as {TERM_ID}
33+
34+
When I run `wp post create --post_title='Test post' --porcelain`
35+
Then STDOUT should be a number
36+
And save STDOUT as {POST_ID}
37+
38+
When I run `wp post term set {POST_ID} category {TERM_ID}`
39+
Then STDOUT should not be empty
40+
41+
When I run `wp term migrate {TERM_ID} --by=slug --from=category --to=post_tag`
42+
Then STDOUT should be:
43+
"""
44+
Term '{TERM_ID}' assigned to post 4.
45+
Term '{TERM_ID}' migrated.
46+
Old instance of term '{TERM_ID}' removed from its original taxonomy.
47+
Success: Migrated the term '{TERM_ID}' from taxonomy 'category' to taxonomy 'post_tag' for 1 post.
48+
"""
49+
50+
@require-wp-4.4
51+
Scenario: Migrate a term in multiple posts
52+
Given a WP install
53+
54+
When I run `wp term create category orange`
55+
Then STDOUT should not be empty
56+
57+
When I run `wp post create --post_title='Test post' --porcelain`
58+
Then STDOUT should be a number
59+
And save STDOUT as {POST_ID}
60+
61+
When I run `wp post term set {POST_ID} category orange`
62+
Then STDOUT should not be empty
63+
64+
When I run `wp post create --post_title='Test post 2' --porcelain`
65+
Then STDOUT should be a number
66+
And save STDOUT as {POST_ID}
67+
68+
When I run `wp post term set {POST_ID} category orange`
69+
Then STDOUT should not be empty
70+
71+
When I run `wp term migrate orange --by=slug --from=category --to=post_tag`
72+
Then STDOUT should be:
73+
"""
74+
Term 'orange' assigned to post 4.
75+
Term 'orange' assigned to post 5.
76+
Term 'orange' migrated.
77+
Old instance of term 'orange' removed from its original taxonomy.
78+
Success: Migrated the term 'orange' from taxonomy 'category' to taxonomy 'post_tag' for 2 posts.
79+
"""
80+
81+
@require-wp-4.4
82+
Scenario: Try to migrate a term that does not exist
83+
Given a WP install
84+
85+
When I try `wp term migrate peach --by=slug --from=category --to=post_tag`
86+
Then STDERR should be:
87+
"""
88+
Error: Taxonomy term 'peach' for taxonomy 'category' doesn't exist.
89+
"""

src/Term_Command.php

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,90 @@ public function recount( $args ) {
636636
}
637637
}
638638

639+
/**
640+
* Migrate a term of a taxonomy to another taxonomy.
641+
*
642+
* ## OPTIONS
643+
*
644+
* <term>
645+
* : Slug or ID of the term to migrate.
646+
*
647+
* [--by=<field>]
648+
* : Explicitly handle the term value as a slug or id.
649+
* ---
650+
* default: id
651+
* options:
652+
* - slug
653+
* - id
654+
* ---
655+
*
656+
* [--from=<taxonomy>]
657+
* : Taxonomy slug of the term to migrate.
658+
*
659+
* [--to=<taxonomy>]
660+
* : Taxonomy slug to migrate to.
661+
*
662+
* ## EXAMPLES
663+
*
664+
* # Migrate a category's term (video) to tag taxonomy.
665+
* $ wp term migrate 9190 --from=category --to=post_tag
666+
* Term '9190' migrated!
667+
* Old instance of term '9190' removed from its original taxonomy.
668+
* Success: Migrated the term '9190' from taxonomy 'category' to taxonomy 'post_tag' for 1 posts
669+
*/
670+
public function migrate( $args, $assoc_args ) {
671+
$clean_term_cache = $values = array();
672+
$term_reference = $args[0];
673+
$original_taxonomy = Utils\get_flag_value( $assoc_args, 'from' );
674+
$destination_taxonomy = Utils\get_flag_value( $assoc_args, 'to' );
675+
676+
$term = get_term_by( Utils\get_flag_value( $assoc_args, 'by' ), $term_reference, $original_taxonomy );
677+
678+
if ( ! $term ) {
679+
WP_CLI::error( "Taxonomy term '{$term_reference}' for taxonomy '{$original_taxonomy}' doesn't exist." );
680+
}
681+
682+
$original_taxonomy = get_taxonomy( $original_taxonomy );
683+
684+
$id = wp_insert_term( $term->name, $destination_taxonomy, array( 'slug' => $term->slug, 'parent' => 0, 'description' => $term->description ) );
685+
686+
if ( is_wp_error( $id ) ) {
687+
WP_CLI::error( $id->get_error_message() );
688+
}
689+
690+
$post_ids = get_objects_in_term( $term->term_id, $original_taxonomy->name );
691+
692+
foreach ( $post_ids as $post_id ) {
693+
$type = get_post_type( $post_id );
694+
if ( in_array( $type, $original_taxonomy->object_type ) ) {
695+
$term_taxonomy_id = wp_set_object_terms( $post_id, $id['term_id'], $destination_taxonomy, true );
696+
697+
if ( is_wp_error( $term_taxonomy_id ) ) {
698+
WP_CLI::error( "Failed to assign the term '{$term->slug}' to the post {$post_id}. Reason: " . $term_taxonomy_id->get_error_message() );
699+
}
700+
701+
WP_CLI::log( "Term '{$term->slug}' assigned to post {$post_id}." );
702+
}
703+
704+
clean_post_cache( $post_id );
705+
}
706+
707+
clean_term_cache( $term->term_id );
708+
709+
WP_CLI::log( "Term '{$term->slug}' migrated." );
710+
711+
$del = wp_delete_term( $term->term_id, $original_taxonomy->name );
712+
713+
if ( is_wp_error( $del ) ) {
714+
WP_CLI::error( "Failed to delete the term '{$term->slug}'. Reason: " . $del->get_error_message() );
715+
}
716+
717+
WP_CLI::log( "Old instance of term '{$term->slug}' removed from its original taxonomy." );
718+
$post_count = count( $post_ids );
719+
$post_plural = WP_CLI\Utils\pluralize( 'post', $post_count );
720+
WP_CLI::success( "Migrated the term '{$term->slug}' from taxonomy '{$original_taxonomy->name}' to taxonomy '{$destination_taxonomy}' for {$post_count} {$post_plural}." );
721+
}
722+
639723
private function maybe_make_child() {
640724
// 50% chance of making child term
641725
return ( mt_rand(1, 2) == 1 );

0 commit comments

Comments
 (0)