-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathTerm_Command.php
More file actions
908 lines (796 loc) · 22.3 KB
/
Term_Command.php
File metadata and controls
908 lines (796 loc) · 22.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
<?php
use WP_CLI\Formatter;
use WP_CLI\Utils;
/**
* Manages taxonomy terms and term meta, with create, delete, and list commands.
*
* See reference for [taxonomies and their terms](https://wordpress.org/documentation/article/taxonomies).
*
* ## EXAMPLES
*
* # Create a new term.
* $ wp term create category Apple --description="A type of fruit"
* Success: Created category 199.
*
* # Get details about a term.
* $ wp term get category 199 --format=json --fields=term_id,name,slug,count
* {"term_id":199,"name":"Apple","slug":"apple","count":1}
*
* # Update an existing term.
* $ wp term update category 15 --name=Apple
* Success: Term updated.
*
* # Get the term's URL.
* $ wp term list post_tag --include=123 --field=url
* http://example.com/tag/tips-and-tricks
*
* # Delete post category
* $ wp term delete category 15
* Success: Deleted category 15.
*
* # Recount posts assigned to each categories and tags
* $ wp term recount category post_tag
* Success: Updated category term count
* Success: Updated post_tag term count
*
* # Prune terms with 0 or 1 published posts
* $ wp term prune post_tag
* Deleted post_tag 15.
* Success: Pruned 1 of 5 terms.
*
* @package wp-cli
*/
class Term_Command extends WP_CLI_Command {
private $fields = [
'term_id',
'term_taxonomy_id',
'name',
'slug',
'description',
'parent',
'count',
];
/**
* Lists terms in a taxonomy.
*
* ## OPTIONS
*
* <taxonomy>...
* : List terms of one or more taxonomies
*
* [--<field>=<value>]
* : Filter by one or more fields (see get_terms() $args parameter for a list of fields).
*
* [--field=<field>]
* : Prints the value of a single field for each term.
*
* [--fields=<fields>]
* : Limit the output to specific object fields.
*
* [--format=<format>]
* : Render output in a particular format.
* ---
* default: table
* options:
* - table
* - csv
* - ids
* - json
* - count
* - yaml
* ---
*
* ## AVAILABLE FIELDS
*
* These fields will be displayed by default for each term:
*
* * term_id
* * term_taxonomy_id
* * name
* * slug
* * description
* * parent
* * count
*
* These fields are optionally available:
*
* * term_group
* * url
*
* ## EXAMPLES
*
* # List post categories
* $ wp term list category --format=csv
* term_id,term_taxonomy_id,name,slug,description,parent,count
* 2,2,aciform,aciform,,0,1
* 3,3,antiquarianism,antiquarianism,,0,1
* 4,4,arrangement,arrangement,,0,1
* 5,5,asmodeus,asmodeus,,0,1
*
* # List post tags
* $ wp term list post_tag --fields=name,slug
* +-----------+-------------+
* | name | slug |
* +-----------+-------------+
* | 8BIT | 8bit |
* | alignment | alignment-2 |
* | Articles | articles |
* | aside | aside |
* +-----------+-------------+
*
* @subcommand list
*/
public function list_( $args, $assoc_args ) {
foreach ( $args as $taxonomy ) {
if ( ! taxonomy_exists( $taxonomy ) ) {
WP_CLI::error( "Taxonomy {$taxonomy} doesn't exist." );
}
}
$formatter = $this->get_formatter( $assoc_args );
$defaults = [
'hide_empty' => false,
];
$assoc_args = array_merge( $defaults, $assoc_args );
if ( ! empty( $assoc_args['term_id'] ) ) {
/**
* @var \WP_Term $term
*/
$term = get_term_by( 'id', $assoc_args['term_id'], $args[0] );
$terms = [ $term ];
} else {
$terms = get_terms(
array_merge(
$assoc_args,
[
'taxonomy' => $args,
]
)
);
// This should never happen because of the taxonomy_exists check above.
if ( is_wp_error( $terms ) ) {
WP_CLI::error( $terms );
}
/**
* @var \WP_Term[] $terms
*/
}
$terms = array_map(
function ( $term ) {
$term->count = (int) $term->count;
$term->parent = (int) $term->parent;
// @phpstan-ignore property.notFound
$term->url = get_term_link( $term );
return $term;
},
$terms
);
if ( 'ids' === $formatter->format ) {
$terms = wp_list_pluck( $terms, 'term_id' );
echo implode( ' ', $terms );
} else {
$formatter->display_items( $terms );
}
}
/**
* Creates a new term.
*
* ## OPTIONS
*
* <taxonomy>
* : Taxonomy for the new term.
*
* <term>
* : A name for the new term.
*
* [--slug=<slug>]
* : A unique slug for the new term. Defaults to sanitized version of name.
*
* [--description=<description>]
* : A description for the new term.
*
* [--parent=<term-id>]
* : A parent for the new term.
*
* [--porcelain]
* : Output just the new term id.
*
* ## EXAMPLES
*
* # Create a new category "Apple" with a description.
* $ wp term create category Apple --description="A type of fruit"
* Success: Created category 199.
*/
public function create( $args, $assoc_args ) {
list( $taxonomy, $term ) = $args;
$defaults = [
'slug' => sanitize_title( $term ),
'description' => '',
'parent' => 0,
];
$assoc_args = wp_parse_args( $assoc_args, $defaults );
$porcelain = Utils\get_flag_value( $assoc_args, 'porcelain' );
unset( $assoc_args['porcelain'] );
$assoc_args = wp_slash( $assoc_args );
$term = wp_slash( $term );
/**
* @var string $term
*/
$result = wp_insert_term( $term, $taxonomy, $assoc_args );
if ( is_wp_error( $result ) ) {
WP_CLI::error( $result->get_error_message() );
} elseif ( $porcelain ) {
WP_CLI::line( (string) $result['term_id'] );
} else {
WP_CLI::success( "Created {$taxonomy} {$result['term_id']}." );
}
}
/**
* Gets details about a term.
*
* ## OPTIONS
*
* <taxonomy>
* : Taxonomy of the term to get
*
* <term>
* : ID or slug of the term to get
*
* [--by=<field>]
* : Explicitly handle the term value as a slug or id.
* ---
* default: id
* options:
* - slug
* - id
* ---
*
* [--field=<field>]
* : Instead of returning the whole term, returns the value of a single field.
*
* [--fields=<fields>]
* : Limit the output to specific fields. Defaults to all fields.
*
* [--format=<format>]
* : Render output in a particular format.
* ---
* default: table
* options:
* - table
* - csv
* - json
* - yaml
* ---
*
* ## EXAMPLES
*
* # Get details about a category with id 199.
* $ wp term get category 199 --format=json
* {"term_id":199,"name":"Apple","slug":"apple","term_group":0,"term_taxonomy_id":199,"taxonomy":"category","description":"A type of fruit","parent":0,"count":0,"filter":"raw"}
*
* # Get details about a category with slug apple.
* $ wp term get category apple --by=slug --format=json
* {"term_id":199,"name":"Apple","slug":"apple","term_group":0,"term_taxonomy_id":199,"taxonomy":"category","description":"A type of fruit","parent":0,"count":0,"filter":"raw"}
*/
public function get( $args, $assoc_args ) {
list( $taxonomy, $term ) = $args;
/**
* @var string $field
*/
$field = Utils\get_flag_value( $assoc_args, 'by' );
$term = get_term_by( $field, $term, $taxonomy );
if ( ! $term ) {
WP_CLI::error( "Term doesn't exist." );
}
// @phpstan-ignore property.notFound
if ( ! isset( $term->url ) ) {
// @phpstan-ignore property.notFound
$term->url = get_term_link( $term );
}
if ( empty( $assoc_args['fields'] ) ) {
$term_array = get_object_vars( $term );
$assoc_args['fields'] = array_keys( $term_array );
}
$term->count = (int) $term->count;
$term->parent = (int) $term->parent;
$formatter = $this->get_formatter( $assoc_args );
$formatter->display_item( $term );
}
/**
* Updates an existing term.
*
* ## OPTIONS
*
* <taxonomy>
* : Taxonomy of the term to update.
*
* <term>
* : ID or slug for the term to update.
*
* [--by=<field>]
* : Explicitly handle the term value as a slug or id.
* ---
* default: id
* options:
* - slug
* - id
* ---
*
* [--name=<name>]
* : A new name for the term.
*
* [--slug=<slug>]
* : A new slug for the term.
*
* [--description=<description>]
* : A new description for the term.
*
* [--parent=<term-id>]
* : A new parent for the term.
*
* ## EXAMPLES
*
* # Change category with id 15 to use the name "Apple"
* $ wp term update category 15 --name=Apple
* Success: Term updated.
*
* # Change category with slug apple to use the name "Apple"
* $ wp term update category apple --by=slug --name=Apple
* Success: Term updated.
*/
public function update( $args, $assoc_args ) {
list( $taxonomy, $term ) = $args;
$defaults = [
'name' => null,
'slug' => null,
'description' => null,
'parent' => null,
];
$assoc_args = wp_parse_args( $assoc_args, $defaults );
foreach ( $assoc_args as $key => $value ) {
if ( null === $value ) {
unset( $assoc_args[ $key ] );
}
}
$assoc_args = wp_slash( $assoc_args );
/**
* @var string $field
*/
$field = Utils\get_flag_value( $assoc_args, 'by' );
$term = get_term_by( $field, $term, $taxonomy );
if ( ! $term ) {
WP_CLI::error( "Term doesn't exist." );
}
// Update term.
$result = wp_update_term( $term->term_id, $taxonomy, $assoc_args );
if ( is_wp_error( $result ) ) {
WP_CLI::error( $result->get_error_message() );
} else {
WP_CLI::success( 'Term updated.' );
}
}
/**
* Deletes an existing term.
*
* Errors if the term doesn't exist, or there was a problem in deleting it.
*
* ## OPTIONS
*
* <taxonomy>
* : Taxonomy of the term to delete.
*
* <term>...
* : One or more IDs or slugs of terms to delete.
*
* [--by=<field>]
* : Explicitly handle the term value as a slug or id.
* ---
* default: id
* options:
* - slug
* - id
* ---
*
* ## EXAMPLES
*
* # Delete post category by id
* $ wp term delete category 15
* Deleted category 15.
* Success: Deleted 1 of 1 terms.
*
* # Delete post category by slug
* $ wp term delete category apple --by=slug
* Deleted category 15.
* Success: Deleted 1 of 1 terms.
*
* # Delete all post tags
* $ wp term list post_tag --field=term_id | xargs wp term delete post_tag
* Deleted post_tag 159.
* Deleted post_tag 160.
* Deleted post_tag 161.
* Success: Deleted 3 of 3 terms.
*/
public function delete( $args, $assoc_args ) {
$taxonomy = array_shift( $args );
$successes = 0;
$errors = 0;
foreach ( $args as $term_id ) {
$term = $term_id;
// Get term by specified argument otherwise get term by id.
$field = Utils\get_flag_value( $assoc_args, 'by' );
if ( 'slug' === $field ) {
// Get term by slug.
$term = get_term_by( $field, $term, $taxonomy );
// If term not found, then show error message and skip the iteration.
if ( ! $term ) {
WP_CLI::warning( "{$taxonomy} {$term_id} doesn't exist." );
continue;
}
// Get the term id;
$term_id = $term->term_id;
}
$result = wp_delete_term( $term_id, $taxonomy );
if ( is_wp_error( $result ) ) {
WP_CLI::warning( $result );
++$errors;
} elseif ( $result ) {
WP_CLI::log( "Deleted {$taxonomy} {$term_id}." );
++$successes;
} else {
WP_CLI::warning( "{$taxonomy} {$term_id} doesn't exist." );
}
}
Utils\report_batch_operation_results( 'term', 'delete', count( $args ), $successes, $errors );
}
/**
* Generates some terms.
*
* Creates a specified number of new terms with dummy data.
*
* ## OPTIONS
*
* <taxonomy>
* : The taxonomy for the generated terms.
*
* [--count=<number>]
* : How many terms to generate?
* ---
* default: 100
* ---
*
* [--max_depth=<number>]
* : Generate child terms down to a certain depth.
* ---
* default: 1
* ---
*
* [--format=<format>]
* : Render output in a particular format.
* ---
* default: progress
* options:
* - progress
* - ids
* ---
*
* ## EXAMPLES
*
* # Generate post categories.
* $ wp term generate category --count=10
* Generating terms 100% [=========] 0:02 / 0:02
*
* # Add meta to every generated term.
* $ wp term generate category --format=ids --count=3 | xargs -d ' ' -I % wp term meta add % foo bar
* Success: Added custom field.
* Success: Added custom field.
* Success: Added custom field.
*/
public function generate( $args, $assoc_args ) {
global $wpdb;
list ( $taxonomy ) = $args;
$defaults = [
'count' => 100,
'max_depth' => 1,
];
$final_args = array_merge( $defaults, $assoc_args );
$count = $final_args['count'];
$max_depth = $final_args['max_depth'];
if ( ! taxonomy_exists( $taxonomy ) ) {
WP_CLI::error( "'{$taxonomy}' is not a registered taxonomy." );
}
/**
* @var \WP_Taxonomy $tax
*/
$tax = get_taxonomy( $taxonomy );
$label = $tax->labels->singular_name;
$slug = sanitize_title_with_dashes( $label );
$format = Utils\get_flag_value( $assoc_args, 'format', 'progress' );
$notify = false;
if ( 'progress' === $format ) {
$notify = Utils\make_progress_bar( 'Generating terms', $count );
}
$previous_term_id = 0;
$current_parent = 0;
$current_depth = 1;
$max_id = (int) $wpdb->get_var( "SELECT term_taxonomy_id FROM {$wpdb->term_taxonomy} ORDER BY term_taxonomy_id DESC LIMIT 1" );
$suspend_cache_invalidation = wp_suspend_cache_invalidation( true );
$created = [];
for ( $index = $max_id + 1; $index <= $max_id + $count; $index++ ) {
if ( $tax->hierarchical ) {
if ( $previous_term_id && $this->maybe_make_child() && $current_depth < $max_depth ) {
$current_parent = $previous_term_id;
++$current_depth;
} elseif ( $this->maybe_reset_depth() ) {
$current_parent = 0;
$current_depth = 1;
}
}
$args = [
'parent' => $current_parent,
'slug' => $slug . "-{$index}",
];
$name = "{$label} {$index}";
$term = wp_insert_term( $name, $taxonomy, $args );
if ( is_wp_error( $term ) ) {
WP_CLI::warning( $term );
} else {
$created[] = $term['term_id'];
$previous_term_id = $term['term_id'];
if ( 'ids' === $format ) {
echo $term['term_id'];
if ( $index < $max_id + $count ) {
echo ' ';
}
}
}
if ( 'progress' === $format ) {
$notify->tick();
}
}
wp_suspend_cache_invalidation( $suspend_cache_invalidation );
clean_term_cache( $created, $taxonomy );
if ( 'progress' === $format ) {
$notify->finish();
}
}
/**
* Recalculates number of posts assigned to each term.
*
* In instances where manual updates are made to the terms assigned to
* posts in the database, the number of posts associated with a term
* can become out-of-sync with the actual number of posts.
*
* This command runs wp_update_term_count() on the taxonomy's terms
* to bring the count back to the correct value.
*
* ## OPTIONS
*
* <taxonomy>...
* : One or more taxonomies to recalculate.
*
* ## EXAMPLES
*
* # Recount posts assigned to each categories and tags
* $ wp term recount category post_tag
* Success: Updated category term count.
* Success: Updated post_tag term count.
*
* # Recount all listed taxonomies
* $ wp taxonomy list --field=name | xargs wp term recount
* Success: Updated category term count.
* Success: Updated post_tag term count.
* Success: Updated nav_menu term count.
* Success: Updated link_category term count.
* Success: Updated post_format term count.
*/
public function recount( $args ) {
foreach ( $args as $taxonomy ) {
if ( ! taxonomy_exists( $taxonomy ) ) {
WP_CLI::warning( "Taxonomy {$taxonomy} does not exist." );
} else {
$terms = get_terms(
[
'taxonomy' => $taxonomy,
'hide_empty' => false,
]
);
// This should never happen because of the taxonomy_exists check above.
if ( is_wp_error( $terms ) ) {
WP_CLI::warning( "Taxonomy {$taxonomy} does not exist." );
continue;
}
/**
* @var \WP_Term[] $terms
*/
$term_taxonomy_ids = wp_list_pluck( $terms, 'term_taxonomy_id' );
wp_update_term_count( $term_taxonomy_ids, $taxonomy );
WP_CLI::success( "Updated {$taxonomy} term count." );
}
}
}
/**
* Removes terms with 0 or 1 published posts from one or more taxonomies.
*
* Useful for cleaning up large sites with many unused or barely-used terms.
* The term count is based on the number of published posts assigned to each
* term.
*
* ## OPTIONS
*
* <taxonomy>...
* : One or more taxonomies to prune.
*
* [--dry-run]
* : Preview the terms to be pruned, without actually deleting them.
*
* ## EXAMPLES
*
* # Prune post tags with 0 or 1 published posts.
* $ wp term prune post_tag
* Deleted post_tag 15.
* Success: Pruned 1 of 5 terms.
*
* # Dry run to preview which terms would be pruned.
* $ wp term prune post_tag --dry-run
* Would delete post_tag 15.
* Success: 1 post_tag term would be pruned.
*
* # Prune multiple taxonomies at once.
* $ wp term prune category post_tag
* Deleted category 8.
* Success: Pruned 1 of 3 terms.
* Deleted post_tag 15.
* Success: Pruned 1 of 5 terms.
*/
public function prune( $args, $assoc_args ) {
global $wpdb;
$dry_run = (bool) Utils\get_flag_value( $assoc_args, 'dry-run', false );
foreach ( $args as $taxonomy ) {
if ( ! taxonomy_exists( $taxonomy ) ) {
WP_CLI::error( "Taxonomy {$taxonomy} doesn't exist." );
}
$term_ids_to_prune = $wpdb->get_col(
$wpdb->prepare(
"SELECT term_id FROM {$wpdb->term_taxonomy} WHERE taxonomy = %s AND count <= 1",
$taxonomy
)
);
$total = count( $term_ids_to_prune );
$successes = 0;
$errors = 0;
foreach ( $term_ids_to_prune as $term_id ) {
if ( $dry_run ) {
WP_CLI::log( "Would delete {$taxonomy} {$term_id}." );
++$successes;
continue;
}
$result = wp_delete_term( $term_id, $taxonomy );
if ( is_wp_error( $result ) ) {
WP_CLI::warning( $result );
++$errors;
} elseif ( $result ) {
WP_CLI::log( "Deleted {$taxonomy} {$term_id}." );
++$successes;
} else {
WP_CLI::warning( "Term {$term_id} in taxonomy {$taxonomy} doesn't exist." );
++$errors;
}
}
if ( $dry_run ) {
$term_word = Utils\pluralize( 'term', $successes );
WP_CLI::success( "{$successes} {$taxonomy} {$term_word} would be pruned." );
} else {
Utils\report_batch_operation_results( 'term', 'prune', $total, $successes, $errors );
}
}
}
/**
* Migrate a term of a taxonomy to another taxonomy.
*
* ## OPTIONS
*
* <term>
* : Slug or ID of the term to migrate.
*
* [--by=<field>]
* : Explicitly handle the term value as a slug or id.
* ---
* default: id
* options:
* - slug
* - id
* ---
*
* [--from=<taxonomy>]
* : Taxonomy slug of the term to migrate.
*
* [--to=<taxonomy>]
* : Taxonomy slug to migrate to.
*
* ## EXAMPLES
*
* # Migrate a category's term (video) to tag taxonomy.
* $ wp term migrate 9190 --from=category --to=post_tag
* Term 'video' assigned to post 1155.
* Term 'video' migrated.
* Old instance of term 'video' removed from its original taxonomy.
* Success: Migrated the term 'video' from taxonomy 'category' to taxonomy 'post_tag' for 1 post.
*/
public function migrate( $args, $assoc_args ) {
$term_reference = $args[0];
/**
* @var string $original_taxonomy
*/
$original_taxonomy = Utils\get_flag_value( $assoc_args, 'from' );
/**
* @var string $destination_taxonomy
*/
$destination_taxonomy = Utils\get_flag_value( $assoc_args, 'to' );
/**
* @var string $field
*/
$field = Utils\get_flag_value( $assoc_args, 'by' );
$term = get_term_by( $field, $term_reference, $original_taxonomy );
if ( ! $term ) {
WP_CLI::error( "Taxonomy term '{$term_reference}' for taxonomy '{$original_taxonomy}' doesn't exist." );
}
$tax = get_taxonomy( $original_taxonomy );
if ( ! $tax ) {
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,
[
'slug' => $term->slug,
'parent' => 0,
'description' => $term->description,
]
);
if ( is_wp_error( $id ) ) {
WP_CLI::error( $id->get_error_message() );
}
/**
* @var string[] $post_ids
*/
$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, $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 );
}
clean_term_cache( $term->term_id );
WP_CLI::log( "Term '{$term->slug}' migrated." );
$del = wp_delete_term( $term->term_id, $tax->name );
if ( is_wp_error( $del ) ) {
WP_CLI::error( "Failed to delete the term '{$term->slug}'. Reason: " . $del->get_error_message() );
}
WP_CLI::log( "Old instance of term '{$term->slug}' removed from its original taxonomy." );
$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}." );
}
private function maybe_make_child() {
// 50% chance of making child term.
return ( wp_rand( 1, 2 ) === 1 );
}
private function maybe_reset_depth() {
// 10% chance of resetting to root depth.
return ( wp_rand( 1, 10 ) === 7 );
}
private function get_formatter( &$assoc_args ) {
return new Formatter( $assoc_args, $this->fields, 'term' );
}
}