TranslateEverything.php
5.35 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
171
172
<?php
namespace WPML\TM\ATE;
use WPML\API\PostTypes;
use WPML\Collect\Support\Collection;
use WPML\Element\API\Languages;
use WPML\FP\Fns;
use WPML\FP\Left;
use WPML\FP\Logic;
use WPML\FP\Lst;
use WPML\FP\Maybe;
use WPML\FP\Obj;
use WPML\FP\Relation;
use WPML\FP\Right;
use WPML\FP\Str;
use WPML\FP\Wrapper;
use WPML\LIB\WP\Post;
use WPML\Media\Option as MediaOption;
use WPML\Records\Translations;
use WPML\Setup\Option;
use WPML\TM\API\ATE\CachedLanguageMappings;
use WPML\TM\API\ATE\LanguageMappings;
use WPML\TM\ATE\TranslateEverything\UntranslatedPosts;
use WPML\TM\AutomaticTranslation\Actions\Actions;
use WPML\Utilities\KeyedLock;
use function WPML\Container\make;
use function WPML\FP\invoke;
use function WPML\FP\pipe;
class TranslateEverything {
/**
* @var UntranslatedPosts
*/
private $untranslatedPosts;
const LOCK_RELEASE_TIMEOUT = 2 * MINUTE_IN_SECONDS;
const QUEUE_SIZE = 15;
public function __construct( UntranslatedPosts $untranslatedPosts ) {
$this->untranslatedPosts = $untranslatedPosts;
}
public function run(
Collection $data,
Actions $actions
) {
if ( ! MediaOption::isSetupFinished() ) {
return Left::of( [ 'key' => 'media-setup-not-finished' ] );
}
$lock = make( KeyedLock::class, [ ':name' => self::class ] );
$key = $lock->create( $data->get( 'key' ), self::LOCK_RELEASE_TIMEOUT );
if ( $key ) {
$createdJobs = [];
if ( Option::shouldTranslateEverything() ) {
$createdJobs = $this->translateEverything( $actions );
}
if ( self::isEverythingProcessed() || ! Option::shouldTranslateEverything() ) {
$lock->release();
$key = false;
}
return Right::of( [ 'key' => $key, 'createdJobs' => $createdJobs ] );
} else {
return Left::of( [ 'key' => 'in-use', ] );
}
}
/**
* @param Actions $actions
*/
private function translateEverything( Actions $actions ) {
list( $postType, $languagesToProcess ) = self::getPostTypeToProcess();
if ( ! $postType || ! $languagesToProcess ) {
return [];
}
$elements = $this->untranslatedPosts->get( $languagesToProcess, $postType, self::QUEUE_SIZE + 1 );
if ( count( $elements ) <= self::QUEUE_SIZE ) {
/**
* We mark $postType as completed in all secondary languages, not only in eligible for automatic translations.
* This is important due to the problem:
* @see https://onthegosystems.myjetbrains.com/youtrack/issue/wpmldev-1456/Changing-translation-engines-configuration-may-trigger-Translate-Everything-process
*
* When we activate a new secondary language and it does not support automatic translations, we mark it as completed by default.
* It is done to prevent unexpected triggering Translate Everything process for that language,
* when it suddently becomes eligible, for example because adjustment of translation engines.
*/
Option::markPostTypeAsCompleted( $postType, Languages::getSecondaryCodes() );
}
return count( $elements ) ?
$actions->createNewTranslationJobs( Languages::getDefaultCode(), Lst::slice( 0, self::QUEUE_SIZE, $elements ) ) :
[];
}
/**
* @return array Eg. ['post', ['fr', 'de', 'es']]
*/
private static function getPostTypeToProcess() {
$postTypes = self::getPostTypesToTranslate(
PostTypes::getAutomaticTranslatable(),
LanguageMappings::geCodesEligibleForAutomaticTranslations()
);
return wpml_collect( $postTypes )
->prioritize( Relation::propEq(0, 'post') )
->prioritize( Relation::propEq(0, 'page') )
->first();
}
/**
* @param array $postTypes
* @param array $targetLanguages
*
* @return array Eg. [['post', ['fr', 'de', 'es']], ['page', ['fr', 'de', 'es']]]
*/
public static function getPostTypesToTranslate( array $postTypes, array $targetLanguages ) {
$completed = Option::getTranslateEverythingCompleted();
$getLanguageCodesNotCompletedForPostType = pipe( Obj::propOr( [], Fns::__, $completed ), Lst::diff( $targetLanguages ) );
$getPostTypesToTranslate = pipe(
Fns::map( function ( $postType ) use ( $getLanguageCodesNotCompletedForPostType ) {
return [ $postType, $getLanguageCodesNotCompletedForPostType( $postType ) ];
} ),
Fns::filter( pipe( Obj::prop( 1 ), Lst::length() ) )
);
return $getPostTypesToTranslate( $postTypes );
}
/**
* @param string $postType
* @param array $targetLanguages
*
* @return string[] Eg. ['fr', 'de', 'es']
*/
public static function getLanguagesToTranslate( $postType, array $targetLanguages ) {
$completed = Option::getTranslateEverythingCompleted();
return Lst::diff( $targetLanguages, Obj::propOr( [], $postType, $completed ) );
}
/**
* Checks if Translate Everything is processed for a given Post Type and Language.
*
* @param string|bool $postType
* @param string $language
*
* @return bool
*/
public static function isEverythingProcessedForPostTypeAndLanguage( $postType, $language ) {
$completed = Option::getTranslateEverythingCompleted();
return isset( $completed[ $postType ] ) && in_array( $language, $completed[ $postType ] );
}
/**
* @param bool $cached
*
* @return bool
*/
public static function isEverythingProcessed( $cached = false ) {
$postTypes = PostTypes::getAutomaticTranslatable();
$getTargetLanguages = [ $cached ? CachedLanguageMappings::class : LanguageMappings::class, 'geCodesEligibleForAutomaticTranslations'];
return count( self::getPostTypesToTranslate( $postTypes, $getTargetLanguages() ) ) === 0;
}
}