|
| 1 | +<?php |
| 2 | +/* |
| 3 | +Plugin Name: WP-CLI Term Merge |
| 4 | +Plugin URI: https://github.com/aucor/wp-cli-term-merge |
| 5 | +Version: 1.0.0 |
| 6 | +Author: Aucor Oy |
| 7 | +Author URI: https://github.com/aucor |
| 8 | +Description: Merge two terms into one |
| 9 | +License: GPLv2 or later |
| 10 | +License URI: http://www.gnu.org/licenses/gpl-2.0.html |
| 11 | +Text Domain: wp-cli-term-merge |
| 12 | +*/ |
| 13 | + |
| 14 | +defined('ABSPATH') or die('Nope'); |
| 15 | + |
| 16 | +if (defined('WP_CLI') && WP_CLI) { |
| 17 | + |
| 18 | + class WP_CLI_Term_Merge { |
| 19 | + |
| 20 | + protected $updated; |
| 21 | + protected $ignored; |
| 22 | + |
| 23 | + /** |
| 24 | + * wp term-merge run --from=123 --to=321 |
| 25 | + * wp term-merge run --from=123 --to=321 --skip-delete |
| 26 | + * |
| 27 | + * --post_type=page |
| 28 | + */ |
| 29 | + public function run($args = array(), $assoc_args = array()) { |
| 30 | + |
| 31 | + if (!isset($assoc_args['from']) || !isset($assoc_args['to'])) { |
| 32 | + WP_CLI::error('Missing required args "from" and "to"'); |
| 33 | + } |
| 34 | + |
| 35 | + $from_term = get_term(absint($assoc_args['from'])); |
| 36 | + if (!($from_term instanceof WP_Term)) { |
| 37 | + WP_CLI::error('Invalid term in "from"'); |
| 38 | + } |
| 39 | + |
| 40 | + $to_term = get_term(absint($assoc_args['to'])); |
| 41 | + if (!($to_term instanceof WP_Term)) { |
| 42 | + WP_CLI::error('Invalid term in "to"'); |
| 43 | + } |
| 44 | + |
| 45 | + $skip_delete = false; |
| 46 | + if (isset($assoc_args['skip-delete'])) { |
| 47 | + $skip_delete = true; |
| 48 | + } |
| 49 | + |
| 50 | + $count = 0; |
| 51 | + |
| 52 | + $query = new WP_Query([ |
| 53 | + 'post_type' => 'any', |
| 54 | + 'posts_per_page' => -1, |
| 55 | + 'post_status' => 'any', |
| 56 | + 'lang' => '', |
| 57 | + 'tax_query' => [ |
| 58 | + [ |
| 59 | + 'taxonomy' => $from_term->taxonomy, |
| 60 | + 'field' => 'term_id', |
| 61 | + 'terms' => [$from_term->term_id], |
| 62 | + ], |
| 63 | + ], |
| 64 | + ]); |
| 65 | + |
| 66 | + while ($query->have_posts()) : $query->the_post(); |
| 67 | + |
| 68 | + // remove "from" term |
| 69 | + $from_terms = wp_get_post_terms(get_the_ID(), $from_term->taxonomy); |
| 70 | + $from_terms_ids = []; |
| 71 | + foreach ($from_terms as $term) { |
| 72 | + if ($term instanceof WP_Term && $term->term_id !== $from_term->term_id) { |
| 73 | + $from_terms_ids[] = $term->term_id; |
| 74 | + } |
| 75 | + } |
| 76 | + wp_set_post_terms(get_the_ID(), $from_terms_ids, $from_term->taxonomy, false); |
| 77 | + |
| 78 | + // insert "to" term |
| 79 | + $to_terms = wp_get_post_terms(get_the_ID(), $to_term->taxonomy); |
| 80 | + $to_terms_ids = []; |
| 81 | + foreach ($to_terms as $term) { |
| 82 | + if ($term instanceof WP_Term) { |
| 83 | + $to_terms_ids[] = $term->term_id; |
| 84 | + } |
| 85 | + } |
| 86 | + $to_terms_ids[] = $to_term->term_id; |
| 87 | + wp_set_post_terms(get_the_ID(), $to_terms_ids, $to_term->taxonomy, false); |
| 88 | + |
| 89 | + // update count |
| 90 | + $count++; |
| 91 | + |
| 92 | + endwhile; |
| 93 | + |
| 94 | + WP_CLI::success('Done: ' . $count . ' posts moved from #' . $from_term->term_id . ' (' . $from_term->name . ') to #' . $to_term->term_id . ' (' . $to_term->name . ')'); |
| 95 | + |
| 96 | + // delete "from" term |
| 97 | + if (!$skip_delete) { |
| 98 | + wp_delete_term($from_term->term_id, $from_term->taxonomy); |
| 99 | + WP_CLI::success('Deleted term: #' . $from_term->term_id . ' (' . $from_term->name . ')'); |
| 100 | + } |
| 101 | + |
| 102 | + } |
| 103 | + |
| 104 | + } |
| 105 | + |
| 106 | + WP_CLI::add_command('term-merge', 'WP_CLI_Term_Merge'); |
| 107 | + |
| 108 | +} |
0 commit comments