class-wpml-tm-icl-translation-status.php
4.21 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<?php
use WPML\FP\Lst;
use WPML\FP\Maybe;
use WPML\FP\Just;
use WPML\FP\Nothing;
use function WPML\Container\make;
use WPML\Element\API\TranslationsRepository;
class WPML_TM_ICL_Translation_Status {
/** @var wpdb $wpdb */
public $wpdb;
private $tm_records;
private $table = 'icl_translation_status';
private $translation_id = 0;
private $rid = 0;
private $status_result;
/**
* WPML_TM_ICL_Translation_Status constructor.
*
* @param wpdb $wpdb
* @param WPML_TM_Records $tm_records
* @param int $id
* @param string $type
*/
public function __construct( wpdb $wpdb, WPML_TM_Records $tm_records, $id, $type = 'translation_id' ) {
$this->wpdb = $wpdb;
$this->tm_records = $tm_records;
if ( $id > 0 && Lst::includes( $type, [ 'translation_id', 'rid' ] ) ) {
$this->{$type} = $id;
} else {
throw new InvalidArgumentException( 'Unknown column: ' . $type . ' or invalid id: ' . $id );
}
}
/**
* @param array $args in the same format used by \wpdb::update()
*
* @return $this
*/
public function update( $args ) {
$this->wpdb->update(
$this->wpdb->prefix . $this->table,
$args,
$this->get_args()
);
$this->status_result = null;
return $this;
}
/**
* Wrapper for \wpdb::delete()
*/
public function delete() {
$this->wpdb->delete(
$this->wpdb->prefix . $this->table,
$this->get_args()
);
}
/**
* @return int
*/
public function rid() {
return (int) $this->wpdb->get_var(
"SELECT rid
FROM {$this->wpdb->prefix}{$this->table} "
. $this->get_where()
);
}
/**
* @return int
*/
public function status() {
if ( $this->status_result === null ) {
$status = $this->tm_records->get_preloaded_translation_status( $this->translation_id, $this->rid );
if ( $status ) {
$this->status_result = (int) $status->status;
} else {
if ( $this->translation_id ) {
$job = TranslationsRepository::getByTranslationId( $this->translation_id );
if ( $job ) {
$this->status_result = \WPML\FP\Obj::prop( 'status', $job );
return $this->status_result;
}
}
$this->status_result = (int) $this->wpdb->get_var(
"SELECT status
FROM {$this->wpdb->prefix}{$this->table} "
. $this->get_where()
);
}
}
return (int) $this->status_result;
}
/**
* @return Just|Nothing
*/
public function previous() {
return Maybe::fromNullable( $this->wpdb->get_var(
"SELECT _prevstate
FROM {$this->wpdb->prefix}{$this->table} "
. $this->get_where()
) )->map( 'unserialize' );
}
/**
* @return string
*/
public function md5() {
return $this->wpdb->get_var(
"SELECT md5
FROM {$this->wpdb->prefix}{$this->table} "
. $this->get_where()
);
}
/**
* @return int
*/
public function translation_id() {
return (int) $this->wpdb->get_var(
"SELECT translation_id
FROM {$this->wpdb->prefix}{$this->table} "
. $this->get_where()
);
}
public function trid() {
return $this->tm_records->icl_translations_by_translation_id( $this->translation_id() )->trid();
}
public function element_id() {
return $this->tm_records->icl_translations_by_translation_id( $this->translation_id() )->element_id();
}
/**
* @return int
*/
public function translator_id() {
return (int) $this->wpdb->get_var(
"SELECT translator_id
FROM {$this->wpdb->prefix}{$this->table} "
. $this->get_where()
);
}
/**
* @return string|int
*/
public function service() {
return (int) $this->wpdb->get_var(
"SELECT translation_service
FROM {$this->wpdb->prefix}{$this->table} "
. $this->get_where()
);
}
private function get_where() {
return ' WHERE ' .
( $this->translation_id
? $this->wpdb->prepare( ' translation_id = %d ', $this->translation_id )
: $this->wpdb->prepare( ' rid = %d ', $this->rid ) );
}
private function get_args() {
return $this->translation_id
? array( 'translation_id' => $this->translation_id )
: array( 'rid' => $this->rid );
}
/**
* @param $id
*
* @return \WPML_TM_ICL_Translation_Status
* @throws \WPML\Auryn\InjectionException
*/
public static function makeByRid( $id ) {
return make( self::class, [ ':id' => $id, ':type' => 'rid' ] );
}
}