forked from diggy/polylang-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathString.php
101 lines (78 loc) · 2.7 KB
/
String.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
<?php
namespace Polylang_CLI\Commands;
if ( ! class_exists( 'Polylang_CLI\Commands\StringCommand' ) ) {
/**
* Inspect and manage Polylang string translations.
*
* @package Polylang_CLI
*/
class StringCommand extends BaseCommand {
/**
* List string translations.
*
* ## OPTIONS
*
* [<language-code>]
* : The language code (slug) to get the string translations for. Optional.
*
* [--fields=<value>]
* : Limit the output to specific object fields. Valid values are: name, string, context, multiline, translations, row.
*
* [--format=<format>]
* : Accepted values: table, csv, json, count, yaml. Default: table
*
* [--s=<value>]
* : Search for a string in `name` and `string` fields.
*
* [--orderby=<value>]
* : Define which column to sort.
*
* [--order=<value>]
* : Define the order of the results, asc or desc.
*
* ## EXAMPLES
*
* $ wp pll string list --s="WordPress site"
*
* $ wp pll string list --order=asc --orderby=string
*
* $ wp pll string list de --fields=string,translations
*
* $ wp pll string list es --format=csv
*
* @subcommand list
*/
public function list_( $args, $assoc_args ) {
if ( isset( $args[0] ) && ! $this->pll->model->get_language( $args[0] ) ) {
$this->cli->error( sprintf( '%s is not a valid language slug.', $args[0] ) );
}
foreach ( array( 's', 'order', 'orderby' ) as $_g ) {
if ( $value = $this->cli->flag( $assoc_args, $_g ) ) {
$_GET[$_g] = $value;
}
}
$fields = $this->cli->flag( $assoc_args, 'fields' );
add_filter( 'pll_strings_per_page', function( $per_page ) { return PHP_INT_MAX; } );
$GLOBALS['hook_suffix'] = null;
$string_table = new \PLL_Table_String( $this->pll->model->get_languages_list() );
$string_table->prepare_items();
$keys = $items = array();
foreach ( $string_table->items as $data ) {
$keys = array_merge( $keys, array_keys( $data ) );
if ( isset( $args[0] ) ) {
$data['translations'] = $data['translations'][$args[0]];
}
if ( $fields ) {
$data = array_intersect_key( $data, array_flip( explode( ',', $fields ) ) );
}
$items[] = (object) $data;
}
$keys = array_unique( $keys );
if ( $fields ) {
$keys = array_intersect_key( array_combine( $keys, $keys ), explode( ',', $fields ) );
}
$formatter = $this->cli->formatter( $assoc_args, $keys );
$formatter->display_items( $items );
}
}
}