class-wpml-st-string.php
9.1 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
<?php
/**
* WPML_ST_String class
*
* Low level access to string in Database
*
* NOTE: Don't use this class to process a large amount of strings as it doesn't
* do any caching, etc.
*/
class WPML_ST_String {
protected $wpdb;
private $string_id;
/** @var string $language */
private $language;
/** @var int $status */
private $status;
/** @var array|null */
private $string_properties;
/**
* @param int $string_id
* @param wpdb $wpdb
*/
public function __construct( $string_id, wpdb $wpdb ) {
$this->wpdb = $wpdb;
$this->string_id = $string_id;
}
/**
* @return int
*/
public function string_id() {
return $this->string_id;
}
/**
* @return string|null
*/
public function get_language() {
$this->language = $this->language
? $this->language
: $this->wpdb->get_var(
'SELECT language ' . $this->from_where_snippet() . ' LIMIT 1'
);
return $this->language;
}
/**
* @return string
*/
public function get_value() {
return $this->wpdb->get_var( 'SELECT value ' . $this->from_where_snippet() . ' LIMIT 1' );
}
/**
* @return int
*/
public function get_status() {
$this->status = $this->status !== null
? $this->status
: (int) $this->wpdb->get_var(
'SELECT status ' . $this->from_where_snippet() . ' LIMIT 1'
);
return $this->status;
}
/**
* @param string $language
*/
public function set_language( $language ) {
if ( $language !== $this->get_language() ) {
$this->language = $language;
$this->set_property( 'language', $language );
$this->update_status();
}
}
/**
* @return stdClass[]
*/
public function get_translation_statuses() {
/** @var array<\stdClass> $statuses */
$statuses = $this->wpdb->get_results( 'SELECT language, status, mo_string ' . $this->from_where_snippet( true ) );
foreach ( $statuses as &$status ) {
if ( ! empty( $status->mo_string ) ) {
$status->status = ICL_TM_COMPLETE;
}
unset( $status->mo_string );
}
return $statuses;
}
public function get_translations() {
return $this->wpdb->get_results( 'SELECT * ' . $this->from_where_snippet( true ) );
}
/**
* For a bulk update of all strings:
*
* @see WPML_ST_Bulk_Update_Strings_Status::run
*/
public function update_status() {
global $sitepress;
/**
* If the translation has a `mo_string`, the status of this
* translation will be set to `WPML_TM_COMPLETE`
*/
$st = $this->get_translation_statuses();
if ( $st ) {
$string_language = $this->get_language();
foreach ( $st as $t ) {
if ( $string_language != $t->language ) {
$translations[ $t->language ] = $t->status;
}
}
$active_languages = $sitepress->get_active_languages();
// If has no translation or all translations are not translated
if ( empty( $translations ) || max( $translations ) == ICL_TM_NOT_TRANSLATED ) {
$status = ICL_TM_NOT_TRANSLATED;
} elseif ( in_array( ICL_TM_WAITING_FOR_TRANSLATOR, $translations ) ) {
$status = ICL_TM_WAITING_FOR_TRANSLATOR;
} elseif ( in_array( ICL_TM_NEEDS_UPDATE, $translations ) ) {
$status = ICL_TM_NEEDS_UPDATE;
} elseif ( $this->has_less_translations_than_secondary_languages( $translations, $active_languages, $string_language ) ) {
if ( in_array( ICL_TM_COMPLETE, $translations ) ) {
$status = ICL_STRING_TRANSLATION_PARTIAL;
} else {
$status = ICL_TM_NOT_TRANSLATED;
}
} else {
if ( in_array( ICL_TM_NOT_TRANSLATED, $translations ) ) {
$status = ICL_STRING_TRANSLATION_PARTIAL;
} else {
$status = ICL_TM_COMPLETE;
}
}
} else {
$status = ICL_TM_NOT_TRANSLATED;
}
if ( $status !== $this->get_status() ) {
$this->status = $status;
$this->set_property( 'status', $status );
}
return $status;
}
/**
* @param array $translations
* @param array $active_languages
* @param string $string_language
*
* @return bool
*/
private function has_less_translations_than_secondary_languages( array $translations, array $active_languages, $string_language ) {
$active_lang_codes = array_keys( $active_languages );
$translations_in_active_langs = array_intersect( $active_lang_codes, array_keys( $translations ) );
return count( $translations_in_active_langs ) < count( $active_languages ) - intval( in_array( $string_language, $active_lang_codes, true ) );
}
/**
* @param string $language
* @param string|null|bool $value
* @param int|bool|false $status
* @param int|null $translator_id
* @param string|int|null $translation_service
* @param int|null $batch_id
*
* @return bool|int id of the translation
*/
public function set_translation( $language, $value = null, $status = false, $translator_id = null, $translation_service = null, $batch_id = null ) {
if ( ! $this->exists() ) {
return false;
}
/** @var $ICL_Pro_Translation WPML_Pro_Translation */
global $ICL_Pro_Translation;
/** @var \stdClass $res */
$res = $this->wpdb->get_row(
$this->wpdb->prepare(
'SELECT id, value, status
' . $this->from_where_snippet( true )
. ' AND language=%s',
$language
)
);
if (
isset( $res->status ) &&
$res->status == ICL_TM_WAITING_FOR_TRANSLATOR &&
is_null( $value ) &&
! in_array( $status, [ ICL_TM_IN_PROGRESS, ICL_TM_NOT_TRANSLATED ] )
&& ! $res->value
) {
return false;
}
$translation_data = array();
if ( $translation_service ) {
$translation_data['translation_service'] = $translation_service;
}
if ( $batch_id ) {
$translation_data['batch_id'] = $batch_id;
}
if ( ! is_null( $value ) ) {
$translation_data['value'] = $value;
}
if ( $translator_id ) {
$translation_data['translator_id'] = $translator_id;
}
$translation_data = apply_filters( 'wpml_st_string_translation_before_save', $translation_data, $language, $this->string_id );
if ( $res ) {
$st_id = $res->id;
if ( $status ) {
$translation_data['status'] = $status;
} elseif ( $status === ICL_TM_NOT_TRANSLATED ) {
$translation_data['status'] = ICL_TM_NOT_TRANSLATED;
}
if ( ! empty( $translation_data ) ) {
$this->wpdb->update( $this->wpdb->prefix . 'icl_string_translations', $translation_data, array( 'id' => $st_id ) );
$this->wpdb->query(
$this->wpdb->prepare( "UPDATE {$this->wpdb->prefix}icl_string_translations SET translation_date = NOW() WHERE id = %d", $st_id )
);
}
} else {
$translation_data = array_merge(
$translation_data,
array(
'string_id' => $this->string_id,
'language' => $language,
'status' => ( $status ? $status : ICL_TM_NOT_TRANSLATED ),
)
);
$this->wpdb->insert( $this->wpdb->prefix . 'icl_string_translations', $translation_data );
$st_id = $this->wpdb->insert_id;
}
if ( $ICL_Pro_Translation ) {
$ICL_Pro_Translation->fix_links_to_translated_content( $st_id, $language, 'string' );
}
icl_update_string_status( $this->string_id );
/**
* @deprecated Use wpml_st_add_string_translation instead
*/
do_action( 'icl_st_add_string_translation', $st_id );
do_action( 'wpml_st_add_string_translation', $st_id );
return $st_id;
}
public function set_location( $location ) {
$this->set_property( 'location', $location );
}
/**
* Set string wrap tag.
* Used for SEO significance, can contain values as h1 ... h6, etc.
*
* @param string $wrap_tag Wrap tag.
*/
public function set_wrap_tag( $wrap_tag ) {
$this->set_property( 'wrap_tag', $wrap_tag );
}
/**
* @param string $property
* @param mixed $value
*/
protected function set_property( $property, $value ) {
$this->wpdb->update( $this->wpdb->prefix . 'icl_strings', array( $property => $value ), array( 'id' => $this->string_id ) );
// Action called after string is updated.
do_action( 'wpml_st_string_updated' );
}
/**
* @param bool $translations sets whether to use original or translations table
*
* @return string
*/
protected function from_where_snippet( $translations = false ) {
if ( $translations ) {
$id_column = 'string_id';
$table = 'icl_string_translations';
} else {
$id_column = 'id';
$table = 'icl_strings';
}
return $this->wpdb->prepare( "FROM {$this->wpdb->prefix}{$table} WHERE {$id_column}=%d", $this->string_id );
}
public function exists() {
$sql = $this->wpdb->prepare( "SELECT id FROM {$this->wpdb->prefix}icl_strings WHERE id = %d", $this->string_id );
return $this->wpdb->get_var( $sql ) > 0;
}
/** @return string|null */
public function get_context() {
return $this->get_string_properties()->context;
}
/** @return string|null */
public function get_gettext_context() {
return $this->get_string_properties()->gettext_context;
}
/** @return string|null */
public function get_name() {
return $this->get_string_properties()->name;
}
private function get_string_properties() {
if ( ! $this->string_properties ) {
$row = $this->wpdb->get_row( 'SELECT name, context, gettext_context ' . $this->from_where_snippet() . ' LIMIT 1' );
$this->string_properties = $row ? $row : (object) [
'name' => null,
'gettext_context' => null,
'context' => null,
];
}
return $this->string_properties;
}
}