GenerateMissingMOFile.php
2.37 KB
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
<?php
namespace WPML\ST\MO\Generate;
use WPML\ST\MO\File\Builder;
use WPML\ST\MO\File\makeDir;
use WPML\ST\MO\Hooks\LoadMissingMOFiles;
use WPML\ST\TranslationFile\StringsRetrieve;
use WPML\WP\OptionManager;
use function WPML\Container\make;
class MissingMOFile {
use makeDir;
const OPTION_GROUP = 'ST-MO';
const OPTION_NAME = 'missing-mo-processed';
/**
* @var Builder
*/
private $builder;
/**
* @var StringsRetrieve
*/
private $stringsRetrieve;
/**
* @var \WPML_Language_Records
*/
private $languageRecords;
/**
* @var OptionManager
*/
private $optionManager;
public function __construct(
\WP_Filesystem_Direct $filesystem,
Builder $builder,
StringsRetrieveMOOriginals $stringsRetrieve,
\WPML_Language_Records $languageRecords,
OptionManager $optionManager
) {
$this->filesystem = $filesystem;
$this->builder = $builder;
$this->stringsRetrieve = $stringsRetrieve;
$this->languageRecords = $languageRecords;
$this->optionManager = $optionManager;
}
/**
* @param string $generateMoPath
* @param string $domain
*/
public function run( $generateMoPath, $domain ) {
$processed = $this->getProcessed();
if ( ! $processed->contains( basename( $generateMoPath ) ) && $this->maybeCreateSubdir() ) {
$locale = make( \WPML_ST_Translations_File_Locale::class )->get( $generateMoPath, $domain );
$strings = $this->stringsRetrieve->get(
$domain,
$this->languageRecords->get_language_code( $locale ),
false
);
if ( ! empty( $strings ) ) {
$fileContents = $this->builder
->set_language( $locale )
->get_content( $strings );
$chmod = defined( 'FS_CHMOD_FILE' ) ? FS_CHMOD_FILE : 0644;
$this->filesystem->put_contents( $generateMoPath, $fileContents, $chmod );
}
$processed->push( $generateMoPath );
$this->optionManager->set( self::OPTION_GROUP, self::OPTION_NAME, $processed->toArray() );
}
}
public function isNotProcessed( $generateMoPath ) {
return ! $this->getProcessed()->contains( basename($generateMoPath) );
}
public static function getSubdir() {
return WP_LANG_DIR . LoadMissingMOFiles::MISSING_MO_FILES_DIR;
}
/**
* @return \WPML\Collect\Support\Collection
*/
private function getProcessed() {
return wpml_collect( $this->optionManager->get( self::OPTION_GROUP, self::OPTION_NAME, [] ) )
->map( function ( $path ) {
return basename( $path );
} );
}
}