class-wpml-translations-queue.php
3.56 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
use WPML\API\Sanitize;
use WPML\Element\API\Languages;
use WPML\FP\Fns;
use WPML\FP\Logic;
use WPML\FP\Lst;
use WPML\FP\Obj;
use WPML\FP\Relation;
use WPML\Setup\Option;
use WPML\TM\API\Translators;
use WPML\TM\ATE\Review\ApproveTranslations;
use WPML\TM\ATE\Review\Cancel;
use WPML\TM\ATE\Review\ReviewStatus;
use WPML\FP\Str;
use WPML\API\PostTypes;
use WPML\TM\Editor\Editor;
use WPML\TM\API\Jobs;
use WPML\TM\Menu\TranslationQueue\PostTypeFilters;
use function WPML\FP\pipe;
class WPML_Translations_Queue {
/** @var SitePress $sitepress */
private $sitepress;
private $must_render_the_editor = false;
/** @var WPML_Translation_Editor_UI */
private $translation_editor;
/**
* @var Editor
*/
private $editor;
/**
* @param SitePress $sitepress
* @param Editor $editor
*/
public function __construct( SitePress $sitepress, Editor $editor ) {
$this->sitepress = $sitepress;
$this->editor = $editor;
}
public function init_hooks() {
add_action( 'current_screen', array( $this, 'load' ) );
}
public function load() {
if ( $this->must_open_the_editor() ) {
$response = $this->editor->open( $_GET );
if ( Relation::propEq( 'editor', WPML_TM_Editors::ATE, $response ) ) {
wp_safe_redirect( Obj::prop('url', $response), 302, 'WPML' );
return;
} elseif (Relation::propEq( 'editor', WPML_TM_Editors::WPML, $response )) {
$this->openClassicTranslationEditor( Obj::prop('jobObject', $response) );
}
}
}
private function openClassicTranslationEditor( $job_object ) {
global $wpdb;
$this->must_render_the_editor = true;
$this->translation_editor = new WPML_Translation_Editor_UI(
$wpdb,
$this->sitepress,
wpml_load_core_tm(),
$job_object,
new WPML_TM_Job_Action_Factory( wpml_tm_load_job_factory() ),
new WPML_TM_Job_Layout( $wpdb, $this->sitepress->get_wp_api() )
);
}
public function display() {
if ( $this->must_render_the_editor ) {
$this->translation_editor->render();
return;
}
?>
<div class="wrap">
<h2><?php echo __( 'Translations queue', 'wpml-translation-management' ); ?></h2>
<div class="js-wpml-abort-review-dialog"></div>
<div id='wpml-remote-jobs-container'></div>
</div>
<?php
}
/**
* @return bool
*/
private function must_open_the_editor() {
return Obj::prop( 'job_id', $_GET ) > 0 || Obj::prop( 'trid', $_GET ) > 0;
}
/**
* @todo this method should be removed but we have to check firts the logic in NextTranslationLink
* @return array
*/
public static function get_cookie_filters() {
$filters = [];
if ( isset( $_COOKIE['wp-translation_ujobs_filter'] ) ) {
parse_str( $_COOKIE['wp-translation_ujobs_filter'], $filters );
$filters = filter_var_array(
$filters,
[
'type' => FILTER_SANITIZE_FULL_SPECIAL_CHARS,
'from' => FILTER_SANITIZE_FULL_SPECIAL_CHARS,
'to' => FILTER_SANITIZE_FULL_SPECIAL_CHARS,
'status' => FILTER_SANITIZE_NUMBER_INT,
]
);
$isTypeValid = Logic::anyPass(
[
Str::startsWith( 'package_' ),
Str::includes( 'st-batch_strings' ),
pipe( Str::replace( 'post_', '' ), Lst::includes( Fns::__, PostTypes::getTranslatable() ) ),
]
);
$activeLanguageCodes = Obj::keys( Languages::getActive() );
if (
$filters['from'] && ! Lst::includes( $filters['from'], $activeLanguageCodes ) ||
$filters['to'] && ! Lst::includes( $filters['to'], $activeLanguageCodes ) ||
( $filters['type'] && ! $isTypeValid( $filters['type'] ) )
) {
$filters = [];
}
}
return $filters;
}
}