Skip to content

Commit 73c2b36

Browse files
authored
improve wp pll string list command (#59)
1 parent 2e787e4 commit 73c2b36

File tree

2 files changed

+82
-10
lines changed

2 files changed

+82
-10
lines changed

README.md

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1145,17 +1145,38 @@ wp pll string
11451145
List string translations.
11461146

11471147
~~~
1148-
wp pll string list [--format=<format>]
1148+
wp pll string list [<language-code>] [--fields=<value>] [--format=<format>] [--s=<value>] [--orderby=<value>] [--order=<value>]
11491149
~~~
11501150

11511151
**OPTIONS**
11521152

1153+
[<language-code>]
1154+
The language code (slug) to get the string translations for. Optional.
1155+
1156+
[--fields=<value>]
1157+
Limit the output to specific object fields. Valid values are: name, string, context, multiline, translations, row.
1158+
11531159
[--format=<format>]
11541160
Accepted values: table, csv, json, count, yaml. Default: table
11551161

1162+
[--s=<value>]
1163+
Search for a string in `name` and `string` fields.
1164+
1165+
[--orderby=<value>]
1166+
Define which column to sort.
1167+
1168+
[--order=<value>]
1169+
Define the order of the results, asc or desc.
1170+
11561171
**EXAMPLES**
11571172

1158-
$ wp pll string list
1173+
$ wp pll string list --s="WordPress site"
1174+
1175+
$ wp pll string list --order=asc --orderby=string
1176+
1177+
$ wp pll string list de --fields=string,translations
1178+
1179+
$ wp pll string list es --format=csv
11591180

11601181

11611182

src/Commands/String.php

Lines changed: 59 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,80 @@ class StringCommand extends BaseCommand {
1414
*
1515
* ## OPTIONS
1616
*
17+
* [<language-code>]
18+
* : The language code (slug) to get the string translations for. Optional.
19+
*
20+
* [--fields=<value>]
21+
* : Limit the output to specific object fields. Valid values are: name, string, context, multiline, translations, row.
22+
*
1723
* [--format=<format>]
1824
* : Accepted values: table, csv, json, count, yaml. Default: table
1925
*
26+
* [--s=<value>]
27+
* : Search for a string in `name` and `string` fields.
28+
*
29+
* [--orderby=<value>]
30+
* : Define which column to sort.
31+
*
32+
* [--order=<value>]
33+
* : Define the order of the results, asc or desc.
34+
*
2035
* ## EXAMPLES
2136
*
22-
* $ wp pll string list
37+
* $ wp pll string list --s="WordPress site"
38+
*
39+
* $ wp pll string list --order=asc --orderby=string
40+
*
41+
* $ wp pll string list de --fields=string,translations
42+
*
43+
* $ wp pll string list es --format=csv
2344
*
2445
* @subcommand list
2546
*/
2647
public function list_( $args, $assoc_args ) {
2748

28-
$strings = \PLL_Admin_Strings::get_strings();
49+
if ( isset( $args[0] ) && ! $this->pll->model->get_language( $args[0] ) ) {
50+
$this->cli->error( sprintf( '%s is not a valid language slug.', $args[0] ) );
51+
}
52+
53+
foreach ( array( 's', 'order', 'orderby' ) as $_g ) {
54+
if ( $value = $this->cli->flag( $assoc_args, $_g ) ) {
55+
$_GET[$_g] = $value;
56+
}
57+
}
58+
59+
$fields = $this->cli->flag( $assoc_args, 'fields' );
60+
61+
add_filter( 'pll_strings_per_page', function( $per_page ) { return PHP_INT_MAX; } );
62+
63+
$string_table = new \PLL_Table_String( $this->pll->model->get_languages_list() );
64+
65+
$string_table->prepare_items();
2966

3067
$keys = $items = array();
3168

32-
foreach ( $strings as $key => $data ) {
33-
$keys = array_merge( $keys, array_keys( $data ) );
34-
$obj = (object) $data;
35-
$obj->key = $key;
36-
$items[] = $obj;
69+
foreach ( $string_table->items as $data ) {
70+
71+
$keys = array_merge( $keys, array_keys( $data ) );
72+
73+
if ( isset( $args[0] ) ) {
74+
$data['translations'] = $data['translations'][$args[0]];
75+
}
76+
77+
if ( $fields ) {
78+
$data = array_intersect_key( $data, array_flip( explode( ',', $fields ) ) );
79+
}
80+
81+
$items[] = (object) $data;
82+
}
83+
84+
$keys = array_unique( $keys );
85+
86+
if ( $fields ) {
87+
$keys = array_intersect_key( array_combine( $keys, $keys ), explode( ',', $fields ) );
3788
}
3889

39-
$formatter = $this->cli->formatter( $assoc_args, array_merge( array( 'key' ), array_unique( $keys ) ) );
90+
$formatter = $this->cli->formatter( $assoc_args, $keys );
4091

4192
$formatter->display_items( $items );
4293
}

0 commit comments

Comments
 (0)