class-wpml-st-tm-jobs.php
4.28 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
<?php
class WPML_ST_TM_Jobs extends WPML_WPDB_User {
/**
* @param wpdb $wpdb
* WPML_ST_TM_Jobs constructor.
*/
public function __construct( &$wpdb ) {
parent::__construct( $wpdb );
add_filter(
'wpml_tm_jobs_union_table_sql',
array(
$this,
'jobs_union_table_sql_filter',
),
10,
2
);
add_filter(
'wpml_post_translation_original_table',
array(
$this,
'filter_tm_post_job_table',
),
10,
1
);
add_filter(
'wpml_st_job_state_pending',
array(
$this,
'tm_external_job_in_progress_filter',
),
10,
2
);
}
/**
* @param bool $in_progress_status
* @param array|object $job_arr
*
* @return bool true if a job is in progress for the given arguments
*/
public function tm_external_job_in_progress_filter( $in_progress_status, $job_arr ) {
$job_arr = (array) $job_arr;
if ( isset( $job_arr['batch'] ) ) {
$job_arr['batch'] = (array) $job_arr['batch'];
}
return isset( $job_arr['batch']['id'] )
&& empty( $job_arr['cms_id'] )
&& ! empty( $job_arr['id'] )
&& ! empty( $job_arr['job_state'] )
&& $job_arr['job_state'] === 'delivered'
&& $this->wpdb->get_var(
$this->wpdb->prepare(
"
SELECT COUNT(*)
FROM {$this->wpdb->prefix}icl_core_status ct
JOIN {$this->wpdb->prefix}icl_string_status st
ON ct.rid = st.rid
JOIN {$this->wpdb->prefix}icl_string_translations t
ON st.string_translation_id = t.id
WHERE ct.rid = %d AND t.status < %d
",
$job_arr['id'],
ICL_TM_COMPLETE
)
) ? true : $in_progress_status;
}
public function jobs_union_table_sql_filter( $sql_statements, $args ) {
return $this->get_jobs_table_sql_part( $sql_statements, $args );
}
/**
* @param string $table
*
* @return string
*/
public function filter_tm_post_job_table( $table ) {
return " (SELECT ID, post_type FROM {$table}
UNION ALL
SELECT ID, NULL as post_type FROM {$this->wpdb->prefix}icl_string_packages) ";
}
private function get_jobs_table_sql_part( $sql_statements, $args ) {
$sql_statements[] = "SELECT st.translator_id,
st.id AS job_id,
'string' AS element_type_prefix,
NULL AS post_type,
st.batch_id
FROM {$this->wpdb->prefix}icl_string_translations st
JOIN {$this->wpdb->prefix}icl_strings s
ON s.id = st.string_id
" . $this->build_string_where( $args );
return $sql_statements;
}
/**
* @param array $args
* string_where
* translator_id
* from
* to
* status
* service
*
* @return string
*/
private function build_string_where( $args ) {
if ( isset( $args['overdue'] ) && $args['overdue'] ) {
// We do not save "deadline" for string jobs so we just want to exclude them in such case
return 'WHERE 1 = 0';
}
$string_where = isset( $args['string_where'] ) ? $args['string_where'] : '';
$translator_id = isset( $args['translator_id'] ) ? $args['translator_id'] : '';
$from = isset( $args['from'] ) ? $args['from'] : '';
$to = isset( $args['to'] ) ? $args['to'] : '';
$status = isset( $args['status'] ) ? $args['status'] : '';
$service = isset( $args['service'] ) ? $args['service'] : false;
$wheres = array();
$where_args = array();
if ( true === (bool) $from ) {
$wheres[] = 's.language = %s';
$where_args[] = $from;
}
if ( true === (bool) $to ) {
$wheres[] = 'st.language = %s';
$where_args[] = $to;
}
if ( $status ) {
$wheres[] = 'st.status = %d';
$where_args[] = ICL_TM_IN_PROGRESS === (int) $status ? ICL_TM_WAITING_FOR_TRANSLATOR : $status;
}
$service = ! $service && is_numeric( $translator_id ) ? 'local' : $service;
$service = 'local' !== $service && false !== strpos( $translator_id, 'ts-' ) ? substr( $translator_id, 3 ) : $service;
if ( 'local' === $service ) {
$wheres[] = 'st.translator_id = %s';
$where_args[] = $translator_id;
}
if ( false !== $service ) {
$wheres[] = 'st.translation_service = %s';
$where_args[] = $service;
}
if ( count( $wheres ) > 0 && count( $wheres ) === count( $where_args ) ) {
$where_sql = implode( ' AND ', $wheres );
/** @var string $sql */
$sql = $this->wpdb->prepare( $where_sql, $where_args );
$string_where = 'WHERE ' . $sql;
}
return $string_where;
}
}