@@ -57,9 +57,10 @@ protected static function init_faker() {
5757 *
5858 * @param int $limit Number of term IDs to get.
5959 * @param string $taxonomy Taxonomy name.
60+ * @param string $name Product name to extract terms from.
6061 * @return array
6162 */
62- protected static function generate_term_ids ( $ limit , $ taxonomy ) {
63+ protected static function generate_term_ids ( $ limit , $ taxonomy, $ name = '' ) {
6364 self ::init_faker ();
6465
6566 $ term_ids = array ();
@@ -68,29 +69,44 @@ protected static function generate_term_ids( $limit, $taxonomy ) {
6869 return $ term_ids ;
6970 }
7071
71- $ terms = self ::$ faker ->words ( $ limit );
72+ $ words = str_word_count ( $ name , 1 );
73+ $ extra_terms = str_word_count ( self ::$ faker ->department ( $ limit ), 1 );
74+ $ words = array_merge ( $ words , $ extra_terms );
75+
76+ if ( 'product_cat ' === $ taxonomy ) {
77+ $ terms = array_slice ( $ words , 1 );
78+ } else {
79+ $ terms = array_merge ( self ::$ faker ->words ( $ limit ), array ( strtolower ( $ words [0 ] ) ) );
80+ }
7281
7382 foreach ( $ terms as $ term ) {
7483 if ( isset ( self ::$ term_ids [ $ taxonomy ], self ::$ term_ids [ $ taxonomy ][ $ term ] ) ) {
75- $ term_ids [] = self ::$ term_ids [ $ taxonomy ][ $ term ];
84+ $ term_id = self ::$ term_ids [ $ taxonomy ][ $ term ];
85+ $ term_ids [] = $ term_id ;
86+
7687 continue ;
7788 }
7889
79- $ term_id = 0 ;
80- $ existing = get_term_by ( 'name ' , $ term , $ taxonomy );
90+ $ term_id = 0 ;
91+ $ args = array (
92+ 'taxonomy ' => $ taxonomy ,
93+ 'name ' => $ term ,
94+ );
95+
96+ $ existing = get_terms ( $ args );
8197
82- if ( $ existing && ! is_wp_error ( $ existing ) ) {
83- $ term_id = $ existing ->term_id ;
98+ if ( $ existing && count ( $ existing ) && ! is_wp_error ( $ existing ) ) {
99+ $ term_id = $ existing[ 0 ] ->term_id ;
84100 } else {
85- $ term_ob = wp_insert_term ( $ term , $ taxonomy );
101+ $ term_ob = wp_insert_term ( $ term , $ taxonomy, $ args );
86102
87103 if ( $ term_ob && ! is_wp_error ( $ term_ob ) ) {
88104 $ term_id = $ term_ob ['term_id ' ];
89105 }
90106 }
91107
92108 if ( $ term_id ) {
93- $ term_ids [] = $ term_id ;
109+ $ term_ids [] = $ term_id ;
94110 self ::$ term_ids [ $ taxonomy ][ $ term ] = $ term_id ;
95111 }
96112 }
0 commit comments