@@ -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