PluginProbe ʕ •ᴥ•ʔ
Loco Translate / 2.8.0
Loco Translate v2.8.0
2.8.5 2.8.4 2.5.8 2.6.0 2.6.1 2.6.10 2.6.11 2.6.12 2.6.13 2.6.14 2.6.2 2.6.3 2.6.4 2.6.5 2.6.6 2.6.7 2.6.8 2.6.9 2.7.0 2.7.1 2.7.2 2.7.3 2.8.0 2.8.1 2.8.2 2.8.3 trunk 1.0.0 1.1.0 1.1.1 1.1.2 1.1.3 1.2 1.2.1 1.2.2 1.3 1.3.1 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.5 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 2.0.0 2.0.1 2.0.10 2.0.11 2.0.12 2.0.13 2.0.14 2.0.15 2.0.16 2.0.17 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.2.0 2.2.1 2.2.2 2.3.0 2.3.1 2.3.2 2.3.3 2.3.4 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.5.0 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 2.5.7
loco-translate / src / cli / Utils.php
loco-translate / src / cli Last commit date
Commands.php 3 years ago ExtractCommand.php 1 year ago FetchCommand.php 1 year ago SyncCommand.php 1 year ago Utils.php 1 year ago
Utils.php
161 lines
1 <?php
2 /**
3 * Utility functions for wp cli commands
4 */
5 abstract class Loco_cli_Utils {
6
7
8 /**
9 * Collect translation sets according to type/domain filter
10 * @return Loco_package_Project[]
11 */
12 public static function collectProjects( $filter ):array {
13 $projects = [];
14 $domain = null;
15 $slug = null;
16 // bundle type filter, with optional argument
17 if( preg_match('/^(plugins|themes|core)(?::(.+))?/i',$filter,$matched) ){
18 $type = strtolower($matched[1]);
19 $handle = isset($matched[2]) ? $matched[2] : '';
20 if( 'plugins' === $type ){
21 if( $handle ){
22 $bundles = [ Loco_package_Plugin::create($handle) ];
23 }
24 else {
25 $bundles = Loco_package_Plugin::getAll();
26 }
27 }
28 else if( 'themes' === $type ){
29 if( $handle ){
30 $bundles = [ Loco_package_Theme::create($handle) ];
31 }
32 else {
33 $bundles = Loco_package_Theme::getAll();
34 }
35 }
36 else {
37 $bundles = [ Loco_package_Core::create() ];
38 $slug = $handle;
39 }
40 }
41 // else fall back to text domain filter
42 else {
43 $domain = $filter;
44 $bundles = [ Loco_package_Core::create() ];
45 $bundles = array_merge( $bundles, Loco_package_Plugin::getAll() );
46 $bundles = array_merge( $bundles, Loco_package_Theme::getAll() );
47 }
48 /* @var Loco_package_Project $project */
49 foreach( $bundles as $bundle ){
50 foreach( $bundle as $project ){
51 if( $domain && $project->getDomain()->getName() !== $domain ){
52 continue;
53 }
54 if( $slug && $project->getSlug() !== $slug ){
55 continue;
56 }
57 $projects[] = $project;
58 }
59 }
60 if( ! $projects ){
61 throw new Loco_error_Exception('No translation sets found');
62 }
63 return $projects;
64 }
65
66
67 /**
68 * Collect locales from one or more language tags
69 * @param string zero or more language tags
70 * @return Loco_Locale[]
71 */
72 public static function collectLocales( $tags ){
73 $locales = [];
74 if( '' !== $tags ){
75 $api = new Loco_api_WordPressTranslations;
76 foreach( preg_split('/[\\s,;]+/i',$tags,-1,PREG_SPLIT_NO_EMPTY) as $tag ){
77 $locale = Loco_Locale::parse($tag);
78 if( ! $locale->isValid() ){
79 throw new Loco_error_Exception('Invalid locale: '.json_encode($tag) );
80 }
81 // TODO could expand language-only tags to known WordPress locales e.g. fr -> fr_FR
82 $locales[ (string) $locale ] = $locale;
83 $locale->ensureName($api);
84 }
85 // empty locales means ALL locales, so refuse to return ALL when filter was non-empty
86 if( 0 === count($locales) ){
87 throw new Loco_error_Exception('No valid locales in: '.json_encode($tags) );
88 }
89 }
90 return $locales;
91 }
92
93
94 /**
95 * Simple space-padded table
96 * @param string[][] data rows to print
97 */
98 public static function tabulate( array $t ){
99 $w = [];
100 foreach( $t as $y => $row ){
101 foreach( $row as $x => $value ){
102 $width = mb_strlen($value,'UTF-8');
103 $w[$x] = isset($w[$x]) ? max($w[$x],$width) : $width;
104 }
105 }
106 foreach( $t as $y => $row ){
107 $line = [];
108 foreach( $w as $x => $width ){
109 $value = isset($row[$x]) ? $row[$x] : '';
110 $value = str_pad($value,$width,' ',STR_PAD_RIGHT);
111 $line[] = $value;
112 }
113 self::debug( implode(' ',$line) );
114 }
115 }
116
117
118 /**
119 * Prints file listing to stdout
120 */
121 public static function tabulateFiles(){
122 $t = [];
123 /* @var Loco_fs_File $file */
124 foreach( func_get_args() as $file ){
125 if( $file instanceof Loco_fs_File && $file->exists() ){
126 $f = new Loco_mvc_FileParams([],$file);
127 $t[] = [ $f->owner, $f->group, $f->smode, $f->relpath ];
128 }
129 }
130 self::tabulate($t);
131 }
132
133
134 /**
135 * WP_CLI debug logger
136 */
137 public static function debug(){
138 $args = func_get_args();
139 $message = array_shift($args);
140 if( $args ){
141 $message = vsprintf($message,$args);
142 }
143 WP_CLI::debug( $message,'loco' );
144 }
145
146
147 /**
148 * Parse boolean command line option. Absence is equal to false
149 * @param string[]
150 * @param string
151 * @return bool
152 */
153 public static function bool( array $opts, $key ){
154 $value = isset($opts[$key]) ? $opts[$key] : false;
155 if( ! is_bool($value) ){
156 $value = $value && 'false' !== $value & 'no' !== $value;
157 }
158 return $value;
159 }
160
161 }