LoadMissingMOFiles.php
4.43 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
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
<?php
namespace WPML\ST\MO\Hooks;
use WPML\Collect\Support\Collection;
use WPML\ST\MO\Generate\MissingMOFile;
use WPML\WP\OptionManager;
use function WPML\Container\make;
class LoadMissingMOFiles implements \IWPML_Action {
const MISSING_MO_FILES_DIR = '/wpml/missing/';
const OPTION_GROUP = 'ST-MO';
const MISSING_MO_OPTION = 'missing-mo';
const TIMEOUT = 10;
const WPML_VERSION_INTRODUCING_ST_MO_FLOW = '4.3.0';
/**
* @var MissingMOFile
*/
private $generateMissingMoFile;
/**
* @var OptionManager
*/
private $optionManager;
/** @var \WPML_ST_Translations_File_Dictionary_Storage_Table */
private $moFilesDictionary;
public function __construct(
MissingMOFile $generateMissingMoFile,
OptionManager $optionManager,
\WPML_ST_Translations_File_Dictionary_Storage_Table $moFilesDictionary
) {
$this->generateMissingMoFile = $generateMissingMoFile;
$this->optionManager = $optionManager;
$this->moFilesDictionary = $moFilesDictionary;
}
public function add_hooks() {
if ( $this->wasWpmlInstalledPriorToMoFlowChanges() ) {
add_filter( 'load_textdomain_mofile', [ $this, 'recordMissing' ], 10, 2 );
add_action( 'shutdown', [ $this, 'generateMissing' ] );
}
}
/**
* @param string $mofile
* @param string $domain
*
* @return string
*/
public function recordMissing( $mofile, $domain ) {
if ( strpos( $mofile, WP_LANG_DIR . '/themes/' ) === 0 ) {
return $mofile;
}
if ( strpos( $mofile, WP_LANG_DIR . '/plugins/' ) === 0 ) {
return $mofile;
}
$missing = $this->getMissing();
if ( self::isReadable( $mofile ) ) {
if ( $missing->has( $domain ) ) {
$this->saveMissing( $missing->forget( $domain ) );
}
return $mofile;
}
if ( ! $this->moFilesDictionary->find( $mofile ) ) {
return $mofile;
}
$generatedFile = $this->getGeneratedFileName( $mofile, $domain );
if ( self::isReadable( $generatedFile ) ) {
return $generatedFile;
}
if ( $this->generateMissingMoFile->isNotProcessed( $generatedFile ) ) {
$this->saveMissing( $missing->put( $domain, $mofile ) );
}
return $mofile;
}
public function generateMissing() {
$lock = make( 'WPML\Utilities\Lock', [ ':name' => self::class ] );
$missing = $this->getMissing();
if ( $missing->count() && $lock->create() ) {
$generate = function ( $pair ) {
list( $domain, $mofile ) = $pair;
$generatedFile = $this->getGeneratedFileName( $mofile, $domain );
$this->generateMissingMoFile->run( $generatedFile, $domain );
};
$unProcessed = $missing->assocToPair()
->eachWithTimeout( $generate, self::getTimeout() )
->pairToAssoc();
$this->saveMissing( $unProcessed );
$lock->release();
}
}
public static function isReadable( $mofile ) {
return is_readable( $mofile );
}
/**
* @return \WPML\Collect\Support\Collection
*/
private function getMissing() {
return wpml_collect( $this->optionManager->get( self::OPTION_GROUP, self::MISSING_MO_OPTION, [] ) );
}
/**
* @param \WPML\Collect\Support\Collection $missing
*/
private function saveMissing( \WPML\Collect\Support\Collection $missing ) {
$this->optionManager->set( self::OPTION_GROUP, self::MISSING_MO_OPTION, $missing->toArray() );
}
public static function getTimeout() {
return self::TIMEOUT;
}
/**
* @return bool
*/
private function wasWpmlInstalledPriorToMoFlowChanges() {
$wpml_start_version = \get_option( \WPML_Installation::WPML_START_VERSION_KEY, '0.0.0' );
return version_compare( $wpml_start_version, self::WPML_VERSION_INTRODUCING_ST_MO_FLOW, '<' );
}
/**
* @param string $mofile
* @param string $domain
*
* @return string
*/
private function getGeneratedFileName( $mofile, $domain ) {
$fileName = basename( $mofile );
if ( $this->isNonDefaultWithMissingDomain( $fileName, $domain ) ) {
$fileName = $domain . '-' . $fileName;
}
return WP_LANG_DIR . self::MISSING_MO_FILES_DIR . $fileName;
}
/**
* There's a fallback for theme that is looking for
* this kind of file `wp-content/themes/hybrid/ru_RU.mo`.
* We need to add the domain otherwise it collides with
* the MO file for the default domain.
*
* @param string $fileName
* @param string $domain
*
* @return bool
*/
private function isNonDefaultWithMissingDomain( $fileName, $domain ) {
return 'default' !== $domain
&& preg_match( '/^[a-z]+_?[A-Z]*\.mo$/', $fileName );
}
}