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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
|
<?php /** * A project is a set of translations within a Text Domain. * Often a text domain will have just one set, but this allows domains to be split into multiple POT files. */ class Loco_package_Project { /** * Text Domain in which project lives * @var Loco_package_TextDomain */ private $domain; /** * Bundle in which project lives * @var Loco_package_Bundle */ private $bundle; /** * Friendly project name, e.g. "Network Admin" * @var string */ private $name; /** * Short name used for naming files, e.g "admin" * @var string */ private $slug;
/** * Configured domain path[s] not including global search paths * @var Loco_fs_FileList */ private $dpaths;
/** * Additional system domain path[s] added separately from bundle config * @var Loco_fs_FileList */ private $gpaths;
/** * Directory paths to exclude during target scanning * @var Loco_fs_FileList */ private $xdpaths;
/** * Locations where POT, PO and MO files may be saved, including standard global paths * @var Loco_fs_FileFinder */ private $target; /** * Configured source path[s] not including global search paths * @var Loco_fs_FileList */ private $spaths; /** * File and directory paths to exclude from source file extraction * @var Loco_fs_FileList */ private $xspaths;
/** * Locations where extractable source files may be found * @var Loco_fs_FileFinder */ private $source;
/** * Explicitly added individual PHP source files * @var Loco_fs_FileList */ private $sfiles;
/** * Paths globally excluded by bundle-level configuration * @var Loco_fs_FileList */ private $xgpaths;
/** * POT template file, ideally named "<name>.pot" * @var Loco_fs_File */ private $pot;
/** * Whether POT file is protected from end-user update and sync operations. * @var bool */ private $potlock = false;
/** * Construct project from its domain and a descriptive name * @param Loco_package_Bundle $bundle * @param Loco_package_TextDomain $domain * @param string $name */ public function __construct( Loco_package_Bundle $bundle, Loco_package_TextDomain $domain, $name ){ $this->setName($name); $this->bundle = $bundle; $this->domain = $domain; // take default slug from domain, avoiding wildcard $slug = $domain->getName(); if( '*' === $slug ){ $slug = ''; } $this->slug = $slug; // sources $this->sfiles = new Loco_fs_FileList; $this->spaths = new Loco_fs_FileList; $this->xspaths = new Loco_fs_FileList; // targets $this->dpaths = new Loco_fs_FileList; $this->gpaths = new Loco_fs_FileList; $this->xdpaths = new Loco_fs_FileList; // global $this->xgpaths = new Loco_fs_FileList; }
/** * Split project ID into domain and slug. * null and "" are meaningfully different. "" means deliberately empty slug, whereas null means default * @param string $id <domain>[.<slug>] * @return string[] [ <domain>, <slug> ] */ public static function splitId( $id ){ $r = preg_split('/(?<!\\\\)\\./', $id, 2 ); $domain = stripcslashes($r[0]); $slug = isset($r[1]) ? stripcslashes($r[1]) : $domain; return [ $domain, $slug ]; }
/** * Get ID identifying project uniquely within a bundle * @return string */ public function getId(){ $slug = $this->getSlug(); $domain = (string) $this->getDomain(); if( $slug === $domain ){ return $slug; } return addcslashes($domain,'.').'.'.addcslashes($slug,'.'); }
/** * @return string */ public function __toString(){ return $this->name; }
/** * Set friendly name of project * @param string $name * @return self */ public function setName( $name ){ $this->name = (string) $name; return $this; }
/** * Set short name of project * @param string * @return Loco_package_Project */ public function setSlug( $slug ){ $this->slug = (string) $slug; return $this; }
/** * Get friendly name of project, e.g. "Network Admin" * @return string */ public function getName(){ return $this->name; }
/** * Get short name of project, e.g. "admin" * @return string */ public function getSlug(){ return $this->slug; }
/** * @return Loco_package_TextDomain */ public function getDomain(){ return $this->domain; }
/** * @return Loco_package_Bundle */ public function getBundle(){ return $this->bundle; }
/** * Whether project is the default for its domain. * @return bool */ public function isDomainDefault(){ $slug = $this->getSlug(); $name = $this->getDomain()->getName(); // default if slug matches text domain. // else special case for Core "default" domain which has empty slug return $slug === $name || ( 'default' === $name && '' === $slug ) || 1 === count($this->bundle); }
/** * Add a root path where translation files may live * @param string|Loco_fs_File $location * @return self */ public function addTargetDirectory( $location ){ $this->target = null; $this->dpaths->add( new Loco_fs_Directory($location) ); return $this; }
/** * Add a global search path where translation files may live * @param string|Loco_fs_Directory $location * @return Loco_package_Project */ public function addSystemTargetDirectory( $location ){ $this->target = null; $this->gpaths->add( new Loco_fs_Directory($location) ); return $this; }
/** * Get domain paths configured in project * @return Loco_fs_FileList */ public function getConfiguredTargets(){ return $this->dpaths; }
/** * Get system paths added to project after configuration * @return Loco_fs_FileList */ public function getSystemTargets(){ return $this->gpaths; } /** * Get all target directory roots including global search paths * @return Loco_fs_FileList */ public function getDomainTargets(){ return $this->getTargetFinder()->getRootDirectories(); } /** * Lazy create all searchable domain paths including global directories * @return Loco_fs_FileFinder */ private function getTargetFinder(){ if( ! $this->target ){ $target = new Loco_fs_FileFinder; $target->setRecursive(false)->group('pot','po','mo'); foreach( $this->dpaths as $path ){ // TODO search need not be recursive if it was the configured DomainPath // currently no way to know at this point, so recursing by default. $target->addRoot( (string) $path, true ); } foreach( $this->gpaths as $path ){ $target->addRoot( (string) $path, false ); } $this->excludeTargets( $target ); $this->target = $target; } return $this->target; }
/** * Utility excludes current exclude paths from target finder * @return void */ private function excludeTargets( Loco_fs_FileFinder $finder ){ foreach( $this->xdpaths as $file ){ if( $path = realpath( (string) $file ) ){ $finder->exclude( $path ); } } foreach( $this->xgpaths as $file ){ if( $path = realpath( (string) $file ) ){ $finder->exclude( $path ); } } }
/** * Check if target file or directory is excluded * @return bool */ private function isTargetExcluded( Loco_fs_File $file ){ return $this->xgpaths->has($file) || $this->xdpaths->has($file); }
/** * Add a path for excluding in a recursive target file search * @param string|Loco_fs_File $path * @return self */ public function excludeTargetPath( $path ){ $this->target = null; $this->xdpaths->add( new Loco_fs_File($path) ); return $this; }
/** * Get all paths excluded when searching for targets * @return Loco_fs_FileList */ public function getConfiguredTargetsExcluded(){ return $this->xdpaths; }
/** * Get first valid domain path * @return Loco_fs_Directory */ private function getSafeDomainPath(){ // use first configured domain path that exists foreach( $this->getConfiguredTargets() as $d ){ if( $d->exists() ){ return $d; } } // fallback to unconfigured, but possibly existent folders $base = $this->getBundle()->getDirectoryPath(); foreach( ['languages','language','lang','l10n','i18n'] as $d ){ $d = new Loco_fs_Directory($d); $d->normalize($base); if( $this->isTargetExcluded($d) ){ continue; } if( $d->exists() ){ return $d; } } // Give up and place in root return new Loco_fs_Directory($base); }
/** * Lazy create all searchable source paths * @return Loco_fs_FileFinder */ public function getSourceFinder(){ if( ! $this->source ){ $source = new Loco_fs_FileFinder; $exts = $this->getSourceExtensions(); $source->setRecursive(true)->filterExtensions($exts); /* @var $file Loco_fs_File */ foreach( $this->spaths as $file ){ $path = realpath( (string) $file ); if( $path && is_dir($path) ){ $source->addRoot( $path, true ); } } $this->excludeSources( $source ); $this->source = $source; } return $this->source; }
/** * @return string[] */ public function getSourceExtensions(){ // TODO source extensions should be moved from plugin settings to project settings $conf = Loco_data_Settings::get(); $exts = $conf->php_alias; $exts = array_merge( $exts, $conf->jsx_alias ); // ensure we always scan *.php and block.json files return array_merge( $exts, ['php','json'] ); }
/** * Utility excludes current exclude paths from passed target finder * @return void */ private function excludeSources( Loco_fs_FileFinder $finder ){ foreach( [$this->xspaths,$this->xgpaths] as $list ){ foreach( $list as $file ){ $real = realpath( (string) $file ); if( is_string($real) && '' !== $real ){ $finder->exclude($real); } } } }
/** * Add a root path where source files may live under for this project * @param string|Loco_fs_File $location * @return Loco_package_Project */ public function addSourceDirectory( $location ){ $this->source = null; $this->spaths->add( new Loco_fs_File($location) ); return $this; }
/** * Add Explicit source file to project config * @param string|Loco_fs_File $path * @return Loco_package_Project */ public function addSourceFile( $path ){ $this->source = null; $this->sfiles->add( new Loco_fs_File($path) ); return $this; }
/** * Add a file or directory as a source location * @param string|Loco_fs_File $path * @return Loco_package_Project */ public function addSourceLocation( $path ){ $file = new Loco_fs_File( $path ); if( $file->isDirectory() ){ $this->addSourceDirectory( $file ); } else { $this->addSourceFile( $file ); } return $this; }
/** * Get all source directories and files defined in project * @return Loco_fs_FileList */ public function getConfiguredSources(){ $dynamic = $this->spaths->getArrayCopy(); $statics = $this->sfiles->getArrayCopy(); return new Loco_fs_FileList( array_merge( $dynamic, $statics ) ); }
/** * Test if bundle has configured source files (even if they're excluded by other rules) * @return bool */ public function hasSourceFiles(){ return count( $this->sfiles ) || count( $this->spaths ); }
/** * Add a path for excluding in source file search * @param string|Loco_fs_File $path * @return Loco_package_Project */ public function excludeSourcePath( $path ){ $this->source = null; $this->xspaths->add( new Loco_fs_File($path) ); return $this; }
/** * Get all paths excluded when searching for sources * @return Loco_fs_FileList */ public function getConfiguredSourcesExcluded(){ return $this->xspaths; }
/** * Add a globally excluded location affecting sources and targets * @param string|Loco_fs_File $path * @return Loco_package_Project */ public function excludeLocation( $path ){ $this->source = null; $this->target = null; $this->xgpaths->add( new Loco_fs_File($path) ); return $this; }
/** * Check whether POT file is protected from end-user update and sync operations. * @return bool */ public function isPotLocked(){ return $this->potlock; }
/** * Lock POT file to prevent end-user updates0 * @param bool $locked * @return Loco_package_Project */ public function setPotLock( $locked ){ $this->potlock = (bool) $locked; return $this; }
/** * Get full path to template POT (file) whether it exists or nor * @return Loco_fs_File */ public function getPot(){ if( ! $this->pot ){ $slug = $this->getSlug(); $name = ( $slug ?: $this->getDomain()->getName() ).'.pot'; if( '.pot' !== $name ){ // find actual file under configured domain paths $targets = $this->getConfiguredTargets()->copy(); // always permit POT file in the bundle root (i.e. outside domain path) if( $this->isDomainDefault() && $this->bundle->hasDirectoryPath() ){ $root = $this->bundle->getDirectoryPath(); $targets->add( new Loco_fs_Directory($root) ); // look in alternative language directories if only root is configured if( 1 === count($targets) ){ foreach( ['languages','language','lang','l10n','i18n'] as $d ) { $alt = new Loco_fs_Directory($root.'/'.$d); if( ! $this->isTargetExcluded($alt) ){ $targets->add($alt); } } } } // pot check is for exact name and not recursive foreach( $targets as $dir ){ $file = new Loco_fs_File($name); $file->normalize( $dir->getPath() ); if( $file->exists() && ! $this->isTargetExcluded($file) ){ $this->pot = $file; break; } } } // fall back to a directory that exists, but where the POT may not if( ! $this->pot ){ $this->pot = new Loco_fs_File($name); $this->pot->normalize( (string) $this->getSafeDomainPath() ); } } return $this->pot; }
/** * Force the use of a known POT file. This could be a PO file if necessary * @param Loco_fs_File $pot template POT file * @return Loco_package_Project */ public function setPot( Loco_fs_File $pot ){ $this->pot = $pot; return $this; }
/** * Take a guess at most likely POT file under target locations * @return Loco_fs_File|null Existent file, or null */ public function guessPot(){ $slug = $this->getSlug(); if( ! is_string($slug) || '' === $slug ){ $slug = (string) $this->getDomain(); if( '' === $slug ){ $slug = 'default'; } } // search only inside bundle for template $finder = new Loco_fs_FileFinder; foreach( $this->dpaths as $path ){ $finder->addRoot( (string) $path, true ); } $this->excludeTargets($finder); $files = $finder->group('pot','po','mo')->exportGroups(); foreach( ['pot','po'] as $ext ){ /* @var $pot Loco_fs_File */ foreach( $files[$ext] as $pot ){ $name = $pot->filename(); // use exact match on project slug if found if( $slug === $name ){ return $pot; } // support unconventional "{slug}-en_US.{ext}" foreach( ['-en_US'=>6, '-en'=>3 ] as $tail => $len ){ if( $tail === substr($name,-$len) && $slug === substr($name,0,-$len) ){ return $pot; } } } } // Failed to find correctly named POT file, // but if a single POT file is found we'll use it. if( 1 === count($files['pot']) ){ return $files['pot'][0]; } // Either no POT files are found, or multiple are found. // if the project is the default in its domain, we can try aliases which may be PO if( $this->isDomainDefault() ){ $options = Loco_data_Settings::get(); if( $aliases = $options->pot_alias ){ $found = []; /* @var $pot Loco_fs_File */ foreach( $finder as $pot ){ $priority = array_search( $pot->basename(), $aliases, true ); if( false !== $priority ){ $found[$priority] = $pot; } } if( $found ){ ksort( $found ); return current($found); } } } // failed to guess POT file return null; }
/** * Get all extractable PHP source files found under all source paths * @return Loco_fs_FileList */ public function findSourceFiles(){ $source = $this->getSourceFinder(); // augment file list from directories unless already done so $list = $this->sfiles->copy(); $crawled = $source->exportGroups(); foreach( $crawled as $ext => $files ){ /* @var Loco_fs_File $file */ foreach( $files as $file ){ $name = $file->filename(); // skip "{name}.min.{ext}" but only if "{name}.{ext}" exists if( '.min' === substr($name,-4) && file_exists( $file->dirname().'/'.substr($name,0,-4).'.'.$ext ) ){ continue; } // .json source files like block.json theme.json etc.. if( 'json' === $ext && 'block' !== $name && 'theme' !== $name ){ // arbitrarily named theme jsons, like onyx.json (twentytwentyfour) if( ! $this->getBundle()->isTheme() ){ continue; } // Skip JED. We will merge these in separately as needed if( preg_match('/-[0-9a-f]{32}]$/',$name ) ){ continue; } // Ok, treat as json schema file. May fail later... } $list->add($file); } } return $list; }
/** * Get all translation files matching project prefix across target directories * @param string $ext File extension, usually "po" or "mo" * @return Loco_fs_LocaleFileList */ public function findLocaleFiles( $ext ){ $finder = $this->getTargetFinder(); $list = new Loco_fs_LocaleFileList; $files = $finder->exportGroups(); $prefix = $this->getSlug(); $domain = $this->domain->getName(); $default = $this->isDomainDefault(); $prefs = Loco_data_Preferences::get(); /* @var $file Loco_fs_File */ foreach( $files[$ext] as $file ){ $file = new Loco_fs_LocaleFile( $file ); // restrict locale by user preference if( $prefs && ! $prefs->has_locale( $file->getLocale() ) ){ continue; } // add file if prefix matches and has a suffix. locale will be validated later if( $file->getPrefix() === $prefix && $file->getSuffix() ){ $list->addLocalized( $file ); } // else in some cases a suffix-only file like "el.po" can match else if( $default && $file->hasSuffixOnly() ){ // theme files under their own directory if( $file->underThemeDirectory() ){ $list->addLocalized( $file ); } // check followed links if they were originally under theme dir else if( ( $link = $finder->getFollowed($file) ) && $link->underThemeDirectory() ){ $list->addLocalized( $file ); } // WordPress core "default" domain, default project else if( 'default' === $domain ){ $list->addLocalized( $file ); } } } return $list; }
/** * @param string $ext File extension * @return Loco_fs_FileList */ public function findNotLocaleFiles( $ext ){ $list = new Loco_fs_LocaleFileList; $files = $this->getTargetFinder()->exportGroups(); /* @var $file Loco_fs_LocaleFile */ foreach( $files[$ext] as $file ){ $file = new Loco_fs_LocaleFile( $file ); // add file if it has no locale suffix and is inside the bundle if( $file->hasPrefixOnly() && ! $file->underGlobalDirectory() ){ $list->add( $file ); } } return $list; }
/** * Initialize choice of PO file paths for a given locale * @param Loco_Locale $locale to initialize translation files for * @return Loco_fs_FileList */ public function initLocaleFiles( Loco_Locale $locale ){ $slug = $this->getSlug(); $domain = $this->domain->getName(); $default = $this->isDomainDefault(); $suffix = sprintf( '%s.po', $locale ); $prefix = $slug ? sprintf('%s-',$slug) : ''; $choice = new Loco_fs_FileList; /* @var Loco_fs_Directory $dir */ foreach( $this->getConfiguredTargets() as $dir ){ // theme files under their own directory normally have no file prefix if( $default && $dir->underThemeDirectory() ){ $path = $dir->getPath().'/'.$suffix; } // all other paths use configured prefix, which may be empty else { $path = $dir->getPath().'/'.$prefix.$suffix; } $choice->add( new Loco_fs_LocaleFile($path) ); } if( 'default' === $domain || '*' === $domain ){ $domain = ''; } /* @var Loco_fs_Directory $dir */ foreach( $this->getSystemTargets() as $dir ){ $path = $dir->getPath(); // themes and plugins under global locations will be loaded by domain, regardless of prefix if( ( '/themes' === substr($path,-7) || '/plugins' === substr($path,-8) ) && '' !== $domain ){ $path .= '/'.$domain.'-'.$suffix; } // all other paths (probably core) use the configured prefix, which may be empty else { $path .= '/'.$prefix.$suffix; } $choice->add( new Loco_fs_LocaleFile($path) ); }
return $choice; }
/** * Initialize a PO file path from required location * @return Loco_fs_LocaleFile * @throws Loco_error_Exception */ public function initLocaleFile( Loco_fs_Directory $dir, Loco_Locale $locale ){ $choice = $this->initLocaleFiles($locale); $pattern = '!^'.preg_quote($dir->getPath(),'!').'/[^/.]+\\.po$!'; /* @var Loco_fs_LocaleFile $file */ foreach( $choice as $file ){ if( preg_match($pattern,$file->getPath()) ){ return $file; } } throw new Loco_error_Exception('Unexpected file location: '.$dir ); }
/** * Get newest timestamp of all translation files (includes template, but excludes source files) * @return int */ public function getLastUpdated(){ $t = 0; $file = $this->getPot(); if( $file && $file->exists() ){ $t = $file->modified(); } /* @var Loco_fs_File $file */ foreach( $this->findLocaleFiles('po') as $file ){ $t = max( $t, $file->modified() ); } return $t; }
}
|