/
term-gen-cli.php
326 lines (245 loc) · 7.65 KB
/
term-gen-cli.php
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
<?php
/**
* Generate Terms
*/
class Term_Gen_CLI extends WP_CLI_Command {
private $text;
/**
* Generate some terms.
*
* ## 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
*
* ## EXAMPLES
*
* wp term-gen create category --count=50 --max_depth=6
*/
public function create( $args, $assoc_args ) {
global $wpdb;
list ( $taxonomy ) = $args;
$defaults = array(
'count' => 100,
'max_depth' => 1,
);
extract( array_merge( $defaults, $assoc_args ), EXTR_SKIP );
$notify = \WP_CLI\Utils\make_progress_bar( 'Generating terms', $count );
if ( !taxonomy_exists( $taxonomy ) ) {
WP_CLI::error( sprintf( "'%s' is not a registered taxonomy.", $taxonomy ) );
}
$label = get_taxonomy( $taxonomy )->labels->singular_name;
$slug = sanitize_title_with_dashes( $label );
$hierarchical = get_taxonomy( $taxonomy )->hierarchical;
$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 = array();
$names = array();
$this->text = file_get_contents( plugin_dir_path( __FILE__ ) .'/lorem-terms.txt' );
for ( $i = $max_id + 1; $i <= $max_id + $count; $i++ ) {
if ( $hierarchical ) {
if ( $previous_term_id && $this->maybe_make_child() && $current_depth < $max_depth ) {
$current_parent = $previous_term_id;
$current_depth++;
} else if ( $this->maybe_reset_depth() ) {
$current_parent = 0;
$current_depth = 1;
}
}
$name = $this->get_random_term_name();
$name = $this->get_unique_term_name( $name, $taxonomy, $names );
$args = array(
'parent' => $current_parent,
'slug' => sanitize_title( $name ),
);
$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'];
$names[] = $name;
}
$notify->tick();
if ( 0 == $i % 200 ) {
sleep( 3 );
}
}
wp_suspend_cache_invalidation( $suspend_cache_invalidation );
clean_term_cache( $created, $taxonomy );
$notify->finish();
if ( count( $created ) ) {
WP_CLI::success( sprintf( "%s terms created.", count( $created ) ) );
} else {
WP_CLI::warning( "No terms created," );
}
}
/**
* Assign terms to a post type.
*
* ## OPTIONS
*
* <taxonomy>
* : The taxonomy to assign to posts.
*
* [--max-terms=<number>]
* : How many terms to assign per post. Default random max terms: 8
*
* [--posts=<number>]
* : How many posts to assign terms to. Default: 100
*
* [--post-type=<post-type>]
* : Post type to assign taxonomy terms to. Default: 'post'
*
*
* ## EXAMPLES
*
* wp term-gen assign post_tag --max-terms=9 --posts=100 --taxonomy=post_tag
*/
public function assign( $args, $assoc_args ) {
global $wpdb;
list ( $taxonomy ) = $args;
$defaults = array(
'max-terms' => 8, // per post
'posts' => 100, // posts to assign terms to
'post-type' => 'post',
);
$args = wp_parse_args( $assoc_args, $defaults );
$taxonomy = trim( $taxonomy );
$post_type = sanitize_key( $args['post-type'] );
$post_count = absint( $args['posts'] );
$term_count = absint( $args['max-terms'] );
if ( !taxonomy_exists( $taxonomy ) ) {
WP_CLI::error( sprintf( "'%s' is not a registered taxonomy.", $taxonomy ) );
}
if ( !post_type_exists( $post_type ) ) {
WP_CLI::error( sprintf( "'%s' is not a registered post type.", $post_type ) );
}
$taxonomy_names = get_object_taxonomies( $post_type );
if ( !in_array( $taxonomy, $taxonomy_names ) ) {
WP_CLI::error( sprintf( "'%s' is not a registered taxonomy for the post type %s.", $taxonomy, $post_type ) );
}
if ( !$post_count ) {
WP_CLI::error( 'Post count is not a number' );
}
if ( !$term_count ) {
WP_CLI::error( 'Term count is not a number' );
}
$args = array(
'posts_per_page' => $post_count,
'post_type' => $post_type,
'orderby' => 'rand',
'fields' => 'ids'
);
$posts = get_posts( $args );
if ( empty( $posts ) ) {
WP_CLI::error( sprintf( "No posts found for post type %s.", $post_type ) );
}
$notify = \WP_CLI\Utils\make_progress_bar( 'Assigning terms', (int) count( $posts ) );
$term_args = array(
'fields' => 'ids',
'hide_empty' => 0,
);
$assigned_terms = 0;
$default_category = get_option( 'default_category' );
$terms_names = array();
$loop_count = 0;
foreach ( $posts as $post ) {
$term_args['number'] = mt_rand( 0 , $term_count );
if ( !$term_args['number'] ) {
continue;
}
add_filter( 'get_terms_orderby', array( $this, 'get_random_terms' ) );
$terms = get_terms( $taxonomy, $term_args );
remove_filter( 'get_terms_orderby', array( $this, 'get_random_terms' ) );
if ( is_wp_error( $terms ) || empty( $terms ) ) {
continue;
}
$terms = array_map( 'intval', $terms );
$append = true;
if ( ( 'category' === $taxonomy ) && in_array( $default_category, $terms ) ) {
if ( mt_rand( 1, 100 ) <= 85 ) {
$key = array_search( $default_category, $terms ); // $key = 2;
unset( $terms[ $key ] );
}
}
if ( empty( $terms ) ) {
continue;
}
$assigned_terms += count( $terms );
wp_set_object_terms( $post, $terms, $taxonomy );
$notify->tick();
if ( 0 == $loop_count % 200 ) {
//WP_CLI::success('sleep ');
sleep( 3 );
}
$loop_count++;
}
$notify->finish();
if ( $assigned_terms ) {
WP_CLI::success( sprintf( "%s terms assigned.", $assigned_terms ) );
} else {
WP_CLI::warning( sprintf( "No terms from %s assigned.", $taxonomy ) );
}
}
function get_random_terms( $clause ) {
return ' RAND()';
}
/**
* Returns a unique term name.
* Checks to see if a term name exists for the taxonomy.
* Adds a number to the term name to make it unique.
*
* @since 1.0
* @param string $term Term name.
* @param string $taxonomy Taxonomy name
* @return string Unique term name
*/
private function get_unique_term_name( $term, $taxonomy, $names = array() ) {
global $wpdb;
$query = "SELECT t.term_id FROM $wpdb->terms AS t
INNER JOIN $wpdb->term_taxonomy as tt ON tt.term_id = t.term_id
WHERE t.name = %s AND tt.taxonomy = %s ORDER BY t.term_id ASC LIMIT 1";
if ( !in_array( $term, $names ) ) {
$result = $wpdb->get_var( $wpdb->prepare( $query, $term, $taxonomy ) );
} else {
$result = 1;
}
if ( !empty( $result ) ) {
$suffix = 2;
do {
$alt_term_name = $term . "-$suffix";
$result = $wpdb->get_var( $wpdb->prepare( $query, $alt_term_name, $taxonomy ) );
$term_name_check = !empty( $result ) ? true : false;
$suffix++;
} while ( $term_name_check );
$term = $alt_term_name;
}
return $term;
}
private function get_random_term_name() {
$length = strlen( $this->text );
$start = mt_rand( 0 , $length -16 );
// Make the chance for very long term names smaller (25%).
$word_length = mt_rand( 4 , mt_rand( 1, 100 ) <= 75 ? 12: 16 );
return substr( $this->text , $start, $word_length );
}
private function maybe_make_child() {
// 50% chance of making child term
return mt_rand( 1, 2 ) == 1;
}
private function maybe_reset_depth() {
// 10% chance of reseting to root depth
return mt_rand( 1, 10 ) == 7;
}
}
WP_CLI::add_command( 'term-gen', 'Term_Gen_CLI' );