View.php
1.72 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
<?php
namespace WPML\TM\ATE\TranslateEverything\Pause;
use WPML\Ajax\IHandler;
use WPML\Collect\Support\Collection;
use WPML\FP\Right;
use WPML\FP\Left;
class View implements IHandler {
const ACTION_PAUSE = 'pause';
const ACTION_RESUME = 'resume';
/** @var PauseAndResume $translate_everything */
private $translate_everything;
/** @var UserAuthorisation $user */
private $user;
public function __construct(
PauseAndResume $translate_everything,
UserAuthorisation $user
) {
$this->translate_everything = $translate_everything;
$this->user = $user;
}
public function run( Collection $data ) {
$action = $data->get( 'action' );
switch ( $action ) {
case self::ACTION_PAUSE:
return $this->pauseAutomaticTranslation();
case self::ACTION_RESUME:
$translateExisting = $data->get( 'translateExisting' );
return $this->resumeAutomaticTranslation( $translateExisting );
}
return $this->unexpectedError();
}
private function pauseAutomaticTranslation() {
if ( ! $this->user->isAllowedToPauseAutomaticTranslation() ) {
return Left::of(
__(
"You're not allowed to pause automatic translation.",
'sitepress'
)
);
}
$this->translate_everything->pause();
return Right::of( true );
}
private function resumeAutomaticTranslation( $translateExisting ) {
if ( ! $this->user->isAllowedToResumeAutomaticTranslation() ) {
return Left::of(
__(
"You're not allowed to start automatic translation.",
'sitepress'
)
);
}
$this->translate_everything->resume( $translateExisting );
return Right::of( true );
}
private function unexpectedError() {
return Left::of(
__( 'Server error. Please refresh and try again.', 'sitepress' )
);
}
}