forked from diggy/polylang-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTerm.php
449 lines (365 loc) · 13.2 KB
/
Term.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
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
<?php
namespace Polylang_CLI\Commands;
if ( ! class_exists( 'Polylang_CLI\Commands\TermCommand' ) ) {
/**
* Inspect and manage WordPress taxonomy terms and their translations.
*
* @package Polylang_CLI
*/
class TermCommand extends BaseCommand {
/**
* Get details about a translated term.
*
* ## OPTIONS
*
* <taxonomy>
* : Taxonomy of the term to get
*
* <term-id>
* : ID of the term to get
*
* [--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
* ---
*
* [--api]
* : Use the Polylang API function pll_get_term_translations()
*
* ## EXAMPLES
*
* # Get details about a category with term ID 18.
* $ wp pll term get category 18
*/
public function get( $args, $assoc_args ) {
list( $taxonomy, $term_id ) = $args;
$term = get_term_by( 'id', $term_id, $taxonomy );
if ( ! $term ) {
$this->cli->error( "Term doesn't exist." );
}
$terms = $this->api->get_term_translations( $term_id );
$obj_array = array();
if ( $this->cli->flag( $assoc_args, 'api' ) ) {
foreach ( $terms as $slug => $term_id ) {
$obj = new \stdClass();
$obj->slug = $slug;
$obj->term_id = $term_id;
$obj_array[$term_id] = $obj;
}
$formatter = $this->cli->formatter( $assoc_args, array( 'slug', 'term_id' ), 'ID' );
} else {
foreach ( $terms as $term_id ) {
$term = get_term_by( 'id', $term_id, $taxonomy );
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;
$obj_array[$term_id] = $term;
}
$formatter = $this->cli->formatter( $assoc_args, $this->fields_term, 'term' );
}
$formatter->display_items( $obj_array );
}
/**
* Delete an existing taxonomy term and its translations.
*
* Errors if the term doesn't exist, or there was a problem in deleting it.
*
* ## OPTIONS
*
* <taxonomy>
* : Taxonomy of the term to delete.
*
* <term-id>...
* : One or more IDs of terms to delete.
*
* ## EXAMPLES
*
* # Delete a term (English) and its translations (Spanish, French)
* $ wp pll term delete post_tag 56
* Deleted post_tag 56.
* Deleted post_tag 57.
* Deleted post_tag 58.
* Success: Deleted 3 of 3 terms.
*/
public function delete( $args, $assoc_args ) {
$taxonomy = array_shift( $args );
$is_taxonomy = get_taxonomy( $taxonomy );
if ( empty( $is_taxonomy ) ) {
$this->cli->error( sprintf( '%s is not a registered taxonomy.', sanitize_text_field( $taxonomy ) ) );
}
if ( ! $this->api->is_translated_taxonomy( $taxonomy ) ) {
$this->cli->error( 'Polylang does not manage languages and translations for this taxonomy.' );
}
$term_ids = array_filter( wp_parse_id_list( $args ) );
$all_term_ids = array();
foreach ( $term_ids as $term_id ) {
$all_term_ids[] = $term_id;
$term_translations = $this->api->get_term_translations( $term_id );
if ( ! empty( $term_translations ) ) {
foreach ( $term_translations as $translation ) {
$all_term_ids[] = $translation;
}
}
}
$this->cli->runcommand(
"term delete $taxonomy " . implode( ' ', array_unique( array_filter( $all_term_ids ) ) ),
array( 'return' => false, 'launch' => false, 'exit_error' => true )
);
}
/**
* Duplicate a taxonomy term to one or more languages.
*
* ## OPTIONS
*
* <taxonomy>
* : Taxonomy of the term to duplicate
*
* <term-id>
* : ID of the term to duplicate
*
* [<language-code>]
* : Language code (slug), or comma-separated list of language codes, to duplicate the term to. Omit to duplicate to all languages. Optional.
*
* ## EXAMPLES
*
* # Duplicate term 18 of the category taxonomy to all other languages.
* $ wp pll term duplicate category 18
*/
public function duplicate( $args, $assoc_args ) {
list( $taxonomy, $term_id ) = $args;
if ( ! $this->api->is_translated_taxonomy( $taxonomy ) ) {
$this->cli->error( 'Polylang does not manage languages and translations for this taxonomy.' );
}
$term_id = absint( $term_id );
$term = get_term_by( 'id', $term_id, $taxonomy );
if ( empty( $term ) ) {
$this->cli->error( sprintf( '%d is not a valid taxonomy term object.', $term_id ) );
}
if ( empty( $this->api->get_term_language( $term_id ) ) ) {
$this->cli->error( sprintf( 'There is no language associated with term %d.', $term_id ) );
}
$slugs = isset( $args[2] ) && $args[2]
? array_map( 'sanitize_title_with_dashes', explode( ',', $args[2] ) )
: array_diff( $this->api->languages_list(), array( $this->api->get_term_language( $term_id, 'slug' ) ) );
foreach ( $slugs as $slug ) {
if ( ! in_array( $slug, $this->api->languages_list() ) ) {
$this->cli->warning( sprintf( '%s is not a valid language.', $slug ) );
continue;
}
$this->duplicate_term( $taxonomy, $term, $slug );
}
}
private function duplicate_term( $taxonomy, $term, $slug )
{
$term_id = absint( $term->term_id );
$term_language = $this->api->get_term_language( $term_id );
if ( $slug === $term_language ) {
$this->cli->warning( sprintf( 'Term %d (%s) cannot be duplicated to itself.', $term_id, $slug ) );
} else {
$term_data = get_term( $term_id, $taxonomy, 'ARRAY_A' );
# check for translated post parent
$term_parent_id = get_term( $term->parent, $taxonomy );
if ( $term_parent_id && ! is_wp_error( $term_parent_id ) ) {
if ( $parent = $this->pll->model->term->get_translation( $term_parent_id, $slug ) ) {
$term_data['parent'] = absint( $parent );
}
}
# check if translation already exists
$exists = $this->api->get_term( $term_id, $slug );
$term_data['slug'] = sanitize_title( $term_data['name'] . '-' . $slug );
# insert or update translation
if ( ! empty( $exists ) ) {
$term_data['ID'] = absint( $exists );
$duplicate = wp_update_term( $term_data['ID'], $taxonomy, wp_slash( $term_data ) );
} else {
unset( $term_data['ID'] );
$duplicate = wp_insert_term( $term->name, $taxonomy, wp_slash( $term_data ) );
}
if ( empty( $duplicate ) ) {
$this->cli->warning( sprintf( 'Could not duplicate term %d to %s.', $term_id, $slug ) );
} elseif ( is_wp_error( $duplicate ) ) {
$this->cli->warning( sprintf( 'Term ID %d: %s (%s)', $term_id, $duplicate->get_error_message(), $slug ) );
} else {
# set term language
$this->api->set_term_language( $duplicate['term_id'], $slug );
# save term translations
$this->api->save_term_translations( array_unique( array_merge( array( $term_language => $term_id, $slug => $duplicate['term_id'] ), $this->api->get_term_translations( $term_id ) ) ) );
# sync taxonomies and post meta, if applicable
$sync = new \PLL_Admin_Sync( $this->pll );
$sync->sync_term_parent( $term_id, $taxonomy, $this->api->get_term_translations( $term_id ) );
# success message
$msg = $exists
? 'Updated term %3$d (%4$s) < term %1$d (%2$s)'
: 'Created term %3$d (%4$s) < term %1$d (%2$s)';
$this->cli->success( sprintf( $msg, $term_id, $term_language, $duplicate['term_id'], $slug ) );
}
}
}
/**
* Get a list of taxonomy terms for a language.
*
* ## OPTIONS
*
* <taxonomy>
* : List terms of one or more taxonomies. Required.
*
* <language-code>
* : The language code (slug) to get the taxonomy terms for. Required.
*
* [--<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:
*
* * url
*
* ## EXAMPLES
*
* # List post categories
* $ wp pll term list color nl --format=csv
* term_id,term_taxonomy_id,name,slug,description,parent,count
* 2,2,Rood,rood,,0,1
* 3,3,Blauw,blauw,,0,1
*
* # List post tags
* $ wp pll term list post_tag en --fields=name,slug
* +-----------+-------------+
* | name | slug |
* +-----------+-------------+
* | Articles | articles |
* | aside | aside |
* +-----------+-------------+
*
* @subcommand list
*/
public function list_( $args, $assoc_args )
{
list ( $taxonomy, $language ) = $args;
$this->pll->curlang = $this->pll->model->get_language( $this->pll->options['default_lang'] );
new \PLL_Frontend_Filters( $this->pll );
$this->cli->command(
array( 'term', 'list', $taxonomy ),
array_merge( array( 'lang' => $language ), $assoc_args )
);
}
/**
* Generate some taxonomy terms and their translations.
*
* Creates a specified number of sets of new terms and their translations with dummy data.
*
* ## OPTIONS
*
* <taxonomy>
* : The taxonomy for the generated terms.
*
* [--count=<number>]
* : How many sets of terms to generate?
* ---
* default: 5
* ---
*
* [--max_depth=<number>]
* : Generate child terms down to a certain depth.
* ---
* default: 1
* ---
*
* [--format=<format>]
* : Render output in a particular format.
* ---
* default: table
* options:
* - table
* - csv
* - json
* - yaml
* - ids
* ---
*
* ## EXAMPLES
*
* # Generate some post categories, and translations.
* $ wp pll term generate category --count=3 --format=ids
* 115 116 117 118 119 120
*/
public function generate( $args, $assoc_args ) {
list ( $taxonomy ) = $args;
if ( ! $this->api->is_translated_taxonomy( $taxonomy ) ) {
$this->cli->error( 'Polylang does not manage languages and translations for this taxonomy.' );
}
$languages = $this->api->languages_list();
$count = $this->cli->flag( $assoc_args, 'count' );
$count = ( $count < 1 ) ? 1 : absint( $count );
$count = $count * count( $languages );
ob_start();
$this->cli->command(
array( 'term', 'generate', $taxonomy ),
array_merge( $assoc_args, array( 'count' => $count, 'format' => 'ids' ) )
);
$ids = ob_get_clean();
$term_ids = wp_parse_id_list( $ids );
$terms = array_chunk( $term_ids, count( $languages ) );
foreach ( $terms as $i => $chunk ) {
$terms[$i] = array_combine( $languages, $chunk );
foreach ( $terms[$i] as $lang => $term_id ) {
$this->api->set_term_language( $term_id, $lang );
}
$this->api->save_term_translations( $terms[$i] );
}
$format = $this->cli->flag( $assoc_args, 'format' );
if ( 'ids' !== $format ) {
return $this->cli->command(
array( 'term', 'list', $taxonomy ),
array( 'format' => $format, 'include' => implode( ',', $term_ids ) )
);
}
echo implode( ' ', $term_ids );
}
}
}