@@ -223,11 +223,6 @@ public function create( $args, $assoc_args ) {
223223 $ porcelain = Utils \get_flag_value ( $ assoc_args , 'porcelain ' );
224224 unset( $ assoc_args ['porcelain ' ] );
225225
226- // Compatibility for < WP 4.0
227- if ( $ assoc_args ['parent ' ] > 0 && ! term_exists ( (int ) $ assoc_args ['parent ' ] ) ) {
228- WP_CLI ::error ( 'Parent term does not exist. ' );
229- }
230-
231226 $ assoc_args = wp_slash ( $ assoc_args );
232227 $ term = wp_slash ( $ term );
233228 $ result = wp_insert_term ( $ term , $ taxonomy , $ assoc_args );
@@ -537,10 +532,13 @@ public function generate( $args, $assoc_args ) {
537532 WP_CLI ::error ( "' {$ taxonomy }' is not a registered taxonomy. " );
538533 }
539534
540- $ label = get_taxonomy ( $ taxonomy )->labels ->singular_name ;
541- $ slug = sanitize_title_with_dashes ( $ label );
535+ /**
536+ * @var \WP_Taxonomy $tax
537+ */
538+ $ tax = get_taxonomy ( $ taxonomy );
542539
543- $ hierarchical = get_taxonomy ( $ taxonomy )->hierarchical ;
540+ $ label = $ tax ->labels ->singular_name ;
541+ $ slug = sanitize_title_with_dashes ( $ label );
544542
545543 $ format = Utils \get_flag_value ( $ assoc_args , 'format ' , 'progress ' );
546544
@@ -560,7 +558,7 @@ public function generate( $args, $assoc_args ) {
560558
561559 for ( $ index = $ max_id + 1 ; $ index <= $ max_id + $ count ; $ index ++ ) {
562560
563- if ( $ hierarchical ) {
561+ if ( $ tax -> hierarchical ) {
564562
565563 if ( $ previous_term_id && $ this ->maybe_make_child () && $ current_depth < $ max_depth ) {
566564
@@ -645,6 +643,10 @@ public function recount( $args ) {
645643 WP_CLI ::warning ( "Taxonomy {$ taxonomy } does not exist. " );
646644 } else {
647645
646+ /**
647+ * @var \WP_Term[] $terms
648+ */
649+
648650 // phpcs:ignore WordPress.WP.DeprecatedParameters.Get_termsParam2Found -- Required for backward compatibility.
649651 $ terms = get_terms ( $ taxonomy , [ 'hide_empty ' => false ] );
650652 $ term_taxonomy_ids = wp_list_pluck ( $ terms , 'term_taxonomy_id ' );
@@ -699,7 +701,11 @@ public function migrate( $args, $assoc_args ) {
699701 WP_CLI ::error ( "Taxonomy term ' {$ term_reference }' for taxonomy ' {$ original_taxonomy }' doesn't exist. " );
700702 }
701703
702- $ original_taxonomy = get_taxonomy ( $ original_taxonomy );
704+ $ tax = get_taxonomy ( $ original_taxonomy );
705+
706+ if ( ! $ tax ) {
707+ WP_CLI ::error ( "Taxonomy ' {$ original_taxonomy }' doesn't exist. " );
708+ }
703709
704710 $ id = wp_insert_term (
705711 $ term ->name ,
@@ -715,11 +721,14 @@ public function migrate( $args, $assoc_args ) {
715721 WP_CLI ::error ( $ id ->get_error_message () );
716722 }
717723
718- $ post_ids = get_objects_in_term ( $ term ->term_id , $ original_taxonomy ->name );
724+ /**
725+ * @var string[] $post_ids
726+ */
727+ $ post_ids = get_objects_in_term ( $ term ->term_id , $ tax ->name );
719728
720729 foreach ( $ post_ids as $ post_id ) {
721730 $ type = get_post_type ( (int ) $ post_id );
722- if ( in_array ( $ type , $ original_taxonomy ->object_type , true ) ) {
731+ if ( in_array ( $ type , $ tax ->object_type , true ) ) {
723732 $ term_taxonomy_id = wp_set_object_terms ( (int ) $ post_id , $ id ['term_id ' ], $ destination_taxonomy , true );
724733
725734 if ( is_wp_error ( $ term_taxonomy_id ) ) {
@@ -736,7 +745,7 @@ public function migrate( $args, $assoc_args ) {
736745
737746 WP_CLI ::log ( "Term ' {$ term ->slug }' migrated. " );
738747
739- $ del = wp_delete_term ( $ term ->term_id , $ original_taxonomy ->name );
748+ $ del = wp_delete_term ( $ term ->term_id , $ tax ->name );
740749
741750 if ( is_wp_error ( $ del ) ) {
742751 WP_CLI ::error ( "Failed to delete the term ' {$ term ->slug }'. Reason: " . $ del ->get_error_message () );
@@ -745,7 +754,7 @@ public function migrate( $args, $assoc_args ) {
745754 WP_CLI ::log ( "Old instance of term ' {$ term ->slug }' removed from its original taxonomy. " );
746755 $ post_count = count ( $ post_ids );
747756 $ post_plural = Utils \pluralize ( 'post ' , $ post_count );
748- WP_CLI ::success ( "Migrated the term ' {$ term ->slug }' from taxonomy ' {$ original_taxonomy ->name }' to taxonomy ' {$ destination_taxonomy }' for {$ post_count } {$ post_plural }. " );
757+ WP_CLI ::success ( "Migrated the term ' {$ term ->slug }' from taxonomy ' {$ tax ->name }' to taxonomy ' {$ destination_taxonomy }' for {$ post_count } {$ post_plural }. " );
749758 }
750759
751760 private function maybe_make_child () {
0 commit comments