Skip to content

Commit 56c005e

Browse files
committed
migrate to another command
1 parent 391e876 commit 56c005e

File tree

2 files changed

+79
-78
lines changed

2 files changed

+79
-78
lines changed

src/Taxonomy_Command.php

Lines changed: 0 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -195,84 +195,6 @@ public function get( $args, $assoc_args ) {
195195
$formatter->display_item( $data );
196196
}
197197

198-
199-
200-
/**
201-
* Migrate a term of a taxonomy to another taxonomy.
202-
*
203-
* ## OPTIONS
204-
*
205-
* <term>
206-
* : Slug of the term to migrate.
207-
*
208-
* <orig-taxonomy>
209-
* : Taxonomy slug of the term to migrate.
210-
*
211-
* <dest-taxonomy>
212-
* : Taxonomy slug to migrate.
213-
* ---
214-
*
215-
* ## EXAMPLES
216-
*
217-
* # Migrate a category's term (video) to tag taxonomy.
218-
* $ wp taxonomy migrate video category post_tag
219-
* Term video has migrated from category taxonomy to post_tag taxonomy.
220-
*/
221-
public function migrate( $args, $assoc_args ) {
222-
// Code based from https://wordpress.org/plugins/taxonomy-converter/ plugin
223-
global $wpdb;
224-
$clean_term_cache = array();
225-
$exists = term_exists( $args[0], $args[1] );
226-
227-
if ( ! empty( $exists ) ) {
228-
WP_CLI::error( "Taxonomy {$args[0]} doesn't exist." );
229-
}
230-
231-
$original_taxonomy = get_taxonomy( $args[0] );
232-
$term = get_term( $args[0], $args[1] );
233-
234-
$id = wp_insert_term($term->name, $args[2], array( 'slug' => $term->slug ) );
235-
236-
if ( is_wp_error( $id ) ) {
237-
WP_CLI::error( "An error has occured: " . $id->get_error_message() );
238-
}
239-
240-
$id = $id['term_taxonomy_id'];
241-
$posts = get_objects_in_term( $term->term_id, $args[1] );
242-
243-
foreach ( $posts as $post ) {
244-
$type = get_post_type( $post );
245-
if ( in_array( $type, $original_taxonomy->object_type ) ) {
246-
$values[] = $wpdb->prepare( "(%d, %d, %d)", $post, $id, 0 );
247-
}
248-
249-
clean_post_cache( $post );
250-
}
251-
252-
if ( $values ) {
253-
$wpdb->query( "INSERT INTO $wpdb->term_relationships (object_id, term_taxonomy_id, term_order) VALUES " . join( ',', $values ) . " ON DUPLICATE KEY UPDATE term_order = VALUES(term_order)" );
254-
$wpdb->update($wpdb->term_taxonomy, array( 'count' => $term->count ), array( 'term_id' => $term->term_id, 'taxonomy' => $args[2] ) );
255-
256-
WP_CLI::success( "Term migrated!" );
257-
258-
$clean_term_cache[] = $term->term_id;
259-
}
260-
261-
$del = wp_delete_term( $term_id, $tax );
262-
263-
if ( is_wp_error( $del ) ) {
264-
WP_CLI::error( "An error has occured: " . $id->get_error_message() );
265-
}
266-
267-
// Set all parents to 0 (root-level) if their parent was the converted tag
268-
$wpdb->update( $wpdb->term_taxonomy, array( 'parent' => 0 ), array( 'parent' => $term_id, 'taxonomy' => $tax ) );
269-
270-
if ( ! empty( $clean_term_cache ) ) {
271-
$clean_term_cache = array_unique( array_values( $clean_term_cache ) );
272-
clean_term_cache ( $clean_term_cache, $args[2] );
273-
}
274-
}
275-
276198
private function get_formatter( &$assoc_args ) {
277199
return new \WP_CLI\Formatter( $assoc_args, $this->fields, 'taxonomy' );
278200
}

src/Term_Command.php

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,85 @@ 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+
* <orig-taxonomy>
648+
* : Taxonomy slug of the term to migrate.
649+
*
650+
* <dest-taxonomy>
651+
* : Taxonomy slug to migrate.
652+
* ---
653+
*
654+
* ## EXAMPLES
655+
*
656+
* # Migrate a category's term (video) to tag taxonomy.
657+
* $ wp taxonomy migrate video category post_tag
658+
* Term video has migrated from category taxonomy to post_tag taxonomy.
659+
*/
660+
public function migrate( $args, $assoc_args ) {
661+
// Code based from https://wordpress.org/plugins/taxonomy-converter/
662+
global $wpdb;
663+
$clean_term_cache = array();
664+
$term_reference = $args[0];
665+
$original_taxonomy = $args[1];
666+
$destination_taxonomy = $args[2];
667+
$exists = term_exists( $term_reference, $args[1] );
668+
669+
if ( ! empty( $exists ) ) {
670+
WP_CLI::error( "Taxonomy term `{$term_reference}` for taxonomy `{$original_taxonomy}` doesn't exist." );
671+
}
672+
673+
$original_taxonomy = get_taxonomy( $term_reference );
674+
$term = get_term( $term_reference, $original_taxonomy );
675+
676+
$id = wp_insert_term($term->name, $destination_taxonomy, array( 'slug' => $term->slug ) );
677+
678+
if ( is_wp_error( $id ) ) {
679+
WP_CLI::error( "An error has occured: " . $id->get_error_message() );
680+
}
681+
682+
$id = $id['term_taxonomy_id'];
683+
$posts = get_objects_in_term( $term->term_id, $args[1] );
684+
685+
foreach ( $posts as $post ) {
686+
$type = get_post_type( $post );
687+
if ( in_array( $type, $original_taxonomy->object_type ) ) {
688+
$values[] = $wpdb->prepare( "(%d, %d, %d)", $post, $id, 0 );
689+
}
690+
691+
clean_post_cache( $post );
692+
}
693+
694+
if ( $values ) {
695+
$wpdb->query( "INSERT INTO $wpdb->term_relationships (object_id, term_taxonomy_id, term_order) VALUES " . join( ',', $values ) . " ON DUPLICATE KEY UPDATE term_order = VALUES(term_order)" );
696+
$wpdb->update($wpdb->term_taxonomy, array( 'count' => $term->count ), array( 'term_id' => $term->term_id, 'taxonomy' => $destination_taxonomy ) );
697+
698+
WP_CLI::success( "Term migrated!" );
699+
700+
$clean_term_cache[] = $term->term_id;
701+
}
702+
703+
$del = wp_delete_term( $term_id, $tax );
704+
705+
if ( is_wp_error( $del ) ) {
706+
WP_CLI::error( "An error has occured: " . $id->get_error_message() );
707+
}
708+
709+
// Set all parents to 0 (root-level) if their parent was the converted tag
710+
$wpdb->update( $wpdb->term_taxonomy, array( 'parent' => 0 ), array( 'parent' => $term_id, 'taxonomy' => $tax ) );
711+
712+
if ( ! empty( $clean_term_cache ) ) {
713+
$clean_term_cache = array_unique( array_values( $clean_term_cache ) );
714+
clean_term_cache ( $clean_term_cache, $args[2] );
715+
}
716+
}
717+
639718
private function maybe_make_child() {
640719
// 50% chance of making child term
641720
return ( mt_rand(1, 2) == 1 );

0 commit comments

Comments
 (0)