Elements.php
4.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
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
<?php
namespace WPML\TM\Jobs\Dispatch;
use Exception;
use WPML\API\Sanitize;
use WPML\FP\Fns;
use WPML\FP\Lst;
use WPML\FP\Obj;
use WPML\FP\Relation;
use WPML\LIB\WP\User;
use WPML\TM\API\Jobs;
use function WPML\Container\make;
use function WPML\FP\pipe;
abstract class Elements {
/**
* @param callable $sendBatch
* @param Messages $messages
* @param callable $buildBatch
* @param array $data
* @param string $type
*/
public static function dispatch(
callable $sendBatch,
Messages $messages,
callable $buildBatch,
$data,
$type
) {
$howToHandleExisting = Obj::propOr(\WPML_TM_Translation_Batch::HANDLE_EXISTING_LEAVE, 'wpml-how-to-handle-existing', $data );
$translateAutomatically = Relation::propEq( 'wpml-how-to-translate', 'automatic', $data );
$translationActions = filter_var_array(
Obj::propOr( [], 'tr_action', $data ),
FILTER_SANITIZE_NUMBER_INT
);
$sourceLanguage = Sanitize::stringProp( 'translate_from', $data );
$targetLanguages = self::getTargetLanguages( $translationActions );
$translators = self::getTranslators( $sourceLanguage, $targetLanguages );
$elementsForTranslation = self::getElements( $messages, $data[ $type ], $targetLanguages, $howToHandleExisting, $translateAutomatically );
$batch = $buildBatch( $elementsForTranslation, $sourceLanguage, $translators );
if ( $batch ) {
$batch->setTranslationMode( Relation::propEq( 'wpml-how-to-translate', 'automatic', $data ) ? 'auto' : 'manual' );
$batch->setHowToHandleExisting( $howToHandleExisting );
$sendBatch( $messages, $batch );
}
}
private static function getTargetLanguages( $translationActions ) {
return array_keys(
array_filter( $translationActions, function ( $action ) {
return (int) $action === \TranslationManagement::TRANSLATE_ELEMENT_ACTION;
} )
);
}
private static function getTranslators( $sourceLanguage, $targetLanguages ) {
$records = make( \WPML_Translator_Records::class );
$getTranslator = function ( $lang ) use ( $sourceLanguage, $records ) {
$translators = $records->get_users_with_languages( $sourceLanguage, [ $lang ] );
return count( $translators ) ? $translators[0] : User::getCurrent();
};
$translators = wpml_collect( $targetLanguages )
->map( $getTranslator )
->map( Obj::prop( 'ID') );
return Lst::zipObj( $targetLanguages, $translators->toArray() );
}
private static function getElements(
Messages $messages,
$data,
$targetLanguages,
$howToHandleExisting,
$translateAutomatically
) {
$getElementsToTranslate = pipe( Fns::filter( Obj::prop( 'checked' ) ), Lst::keyBy( 'checked' ) );
$elementsIds = $getElementsToTranslate( $data );
list( $elementsToTranslation, $ignoredElementsMessages ) = static::filterElements(
$messages,
$elementsIds,
$targetLanguages,
$howToHandleExisting,
$translateAutomatically
);
$messages->showForPosts( $ignoredElementsMessages, 'information' );
return array_filter( $elementsToTranslation, pipe( Obj::prop( 'target_languages' ), Lst::length() ) );
}
/**
* @param \stdClass $job
*
* @return bool
*/
protected static function isProgressJob( $job ) {
return Lst::includes( (int) $job->status, [ ICL_TM_WAITING_FOR_TRANSLATOR, ICL_TM_IN_PROGRESS ] ) && ! $job->needs_update;
}
/**
* @param \stdClass $job
*
* @return bool
*/
protected static function isCompletedJob( $job ) {
return (int) $job->status === ICL_TM_COMPLETE && ! $job->needs_update;
}
/**
* @param Messages $messages
* @param array $elementsData
* @param array $targetLanguages
* @param string $howToHandleExisting
* @param bool $translateAutomatically
*
* phpcs:disable Squiz.Commenting.FunctionComment.InvalidNoReturn
* @return array
* @throws Exception Throws an exception if the method is not properly extended.
*/
protected static function filterElements( Messages $messages, $elementsData, $targetLanguages, $howToHandleExisting, $translateAutomatically ) {
throw new Exception( ' this method is mandatory' );
}
/**
* @param \stdClass $job
* @param string|null $howToHandleExisting
* @param bool $translateAutomatically
*
* @return bool
*/
protected static function shouldJobBeIgnoredBecauseIsCompleted( $job, $howToHandleExisting, $translateAutomatically ) {
return $translateAutomatically && $job && self::isCompletedJob( $job ) && $howToHandleExisting === \WPML_TM_Translation_Batch::HANDLE_EXISTING_LEAVE;
}
}