class-wpml-element-translation.php
9.81 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
<?php
/**
* WPML_Element_Translation Class
*
* @package wpml-core
* @abstract
*/
abstract class WPML_Element_Translation extends WPML_WPDB_User {
/** @var array[] $element_data */
protected $element_data = [];
/** @var array[] $translations */
protected $translations = [];
/** @var array[] $trid_groups */
protected $trid_groups = [];
/** @var array[] $trid_groups */
protected $translation_ids_element = [];
/** @var int $type_prefix_length */
private $type_prefix_length;
/**
* @param wpdb $wpdb
*/
public function __construct( &$wpdb ) {
parent::__construct( $wpdb );
$this->type_prefix_length = strlen( $this->get_type_prefix() );
}
abstract protected function get_element_join();
abstract protected function get_type_prefix();
/**
* Clears the cached translations.
*/
public function reload() {
$this->element_data = [];
$this->translations = [];
$this->trid_groups = [];
$this->translation_ids_element = [];
}
public function get_element_trid( $element_id ) {
return $this->maybe_populate_cache( $element_id )
? $this->element_data[ $element_id ]['trid'] : null;
}
/**
* @param int $element_id
* @param string $lang
* @param bool|false $original_fallback if true will return input $element_id if no translation is found
*
* @return null|int
*/
public function element_id_in( $element_id, $lang, $original_fallback = false ) {
$result = ( $original_fallback ? (int) $element_id : null );
if ( $this->maybe_populate_cache( $element_id ) && isset( $this->translations[ $element_id ][ $lang ] ) ) {
$result = (int) $this->translations[ $element_id ][ $lang ];
}
return $result;
}
/**
* @param int $element_id
* @param bool $root if true gets the root element of the trid which itself
* has no original. Otherwise returns the direct original of the given
* element_id.
*
* @return int|null null if the element has no original
*/
public function get_original_element( $element_id, $root = false ) {
$element_id = (int) $element_id;
$source_lang = $this->maybe_populate_cache( $element_id )
? $this->element_data[ $element_id ]['source_lang'] : null;
if ( null === $source_lang ) {
return null;
}
if ( ! $root && isset( $this->translations[ $element_id ][ $source_lang ] ) ) {
return (int) $this->translations[ $element_id ][ $source_lang ];
}
if ( $root ) {
foreach ( $this->translations[ $element_id ] as $trans_id ) {
if ( ! $this->element_data[ $trans_id ]['source_lang'] ) {
return (int) $trans_id;
}
}
}
return null;
}
public function get_element_id( $lang, $trid ) {
$this->maybe_populate_cache( false, $trid );
return isset( $this->trid_groups [ $trid ][ $lang ] ) ? $this->trid_groups [ $trid ][ $lang ] : null;
}
/**
* @param int $element_id
*
* @return null|string
*/
public function get_element_lang_code( $element_id ) {
$result = null;
if ( $this->maybe_populate_cache( $element_id ) ) {
$result = $this->element_data[ $element_id ]['lang'];
}
return $result;
}
/**
* @param int $element_id
* @param string $output
*
* @return array|null|stdClass
*/
public function get_element_language_details( $element_id, $output = OBJECT ) {
$result = null;
if ( $element_id && $this->maybe_populate_cache( $element_id ) ) {
$result = new stdClass();
$result->element_id = $element_id;
$result->trid = $this->element_data[ $element_id ]['trid'];
$result->language_code = $this->element_data[ $element_id ]['lang'];
$result->source_language_code = $this->element_data[ $element_id ]['source_lang'];
}
if ( $output == ARRAY_A ) {
return $result ? get_object_vars( $result ) : null;
} elseif ( $output == ARRAY_N ) {
return $result ? array_values( get_object_vars( $result ) ) : null;
} else {
return $result;
}
}
public function get_source_lang_code( $element_id ) {
return $this->maybe_populate_cache( $element_id )
? $this->element_data[ $element_id ]['source_lang'] : null;
}
public function get_type( $element_id ) {
return $this->maybe_populate_cache( $element_id ) ? $this->element_data[ $element_id ]['type'] : null;
}
public function get_source_lang_from_translation_id( $translation_id ) {
$lang = array(
'code' => null,
'found' => false,
);
$element_id = $this->get_element_from_translation_id( $translation_id );
if ( $element_id ) {
$lang['code'] = $this->get_source_lang_code( $element_id );
$lang['found'] = true;
}
return $lang;
}
public function get_translation_id( $element_id ) {
return $this->maybe_populate_cache( $element_id )
? $this->element_data[ $element_id ]['translation_id'] : null;
}
public function get_translations_ids() {
$translation_ids = array();
foreach ( $this->element_data as $data ) {
$translation_ids[] = $data['translation_id'];
}
return $translation_ids;
}
/**
* @param int $element_id
* @param int|false $trid
* @param bool $actual_translations_only
*
* @return array<int,int>
*/
public function get_element_translations( $element_id, $trid = false, $actual_translations_only = false ) {
$valid_element = $this->maybe_populate_cache( $element_id, $trid );
if ( $element_id ) {
$res = $valid_element
? ( $actual_translations_only
? $this->filter_for_actual_trans( $element_id ) : $this->translations[ $element_id ] ) : array();
} elseif ( $trid ) {
$res = isset( $this->trid_groups[ $trid ] ) ? $this->trid_groups[ $trid ] : array();
}
return isset( $res ) ? $res : array();
}
public function get_element_from_translation_id( $translation_id ) {
return isset( $this->translation_ids_element[ $translation_id ] ) ? $this->translation_ids_element[ $translation_id ] : null;
}
public function get_trid_from_translation_id( $translation_id ) {
$trid = null;
$element_id = $this->get_element_from_translation_id( $translation_id );
if ( $element_id ) {
$trid = $this->get_element_trid( $element_id );
}
return $trid;
}
public function get_trids() {
return array_keys( $this->trid_groups );
}
public function prefetch_ids( $element_ids ) {
$element_ids = (array) $element_ids;
$element_ids = array_diff( $element_ids, array_keys( $this->element_data ) );
if ( (bool) $element_ids === false ) {
return;
}
$trid_snippet = ' tridt.element_id IN (' . wpml_prepare_in( $element_ids, '%d' ) . ')';
$sql = $this->build_sql( $trid_snippet );
$elements = $this->wpdb->get_results( $sql, ARRAY_A );
$this->group_and_populate_cache( $elements );
}
/**
* @param string $trid_snippet
*
* @return string
*/
private function build_sql( $trid_snippet ) {
return 'SELECT wpml_translations.translation_id, wpml_translations.element_id, wpml_translations.language_code, wpml_translations.source_language_code, wpml_translations.trid, wpml_translations.element_type
' . $this->get_element_join() . "
JOIN {$this->wpdb->prefix}icl_translations tridt
ON tridt.element_type = wpml_translations.element_type
AND tridt.trid = wpml_translations.trid
WHERE {$trid_snippet}";
}
private function maybe_populate_cache( $element_id, $trid = false ) {
if ( ! $element_id && ! $trid ) {
return false;
}
if ( ! $element_id && isset( $this->trid_groups [ $trid ] ) ) {
return true;
}
if ( ! $element_id || ! isset( $this->translations[ $element_id ] ) ) {
if ( ! $element_id ) {
$trid_snippet = $this->wpdb->prepare( ' tridt.trid = %d ', $trid );
} else {
$trid_snippet = $this->wpdb->prepare(
' tridt.trid = (SELECT trid ' . $this->get_element_join() . ' WHERE element_id = %d LIMIT 1)',
$element_id
);
}
$sql = $this->build_sql( (string) $trid_snippet );
$elements = $this->wpdb->get_results( $sql, ARRAY_A );
$this->populate_cache( $elements );
if ( $element_id && ! isset( $this->translations[ $element_id ] ) ) {
$this->translations[ $element_id ] = array();
}
}
return ! empty( $this->translations[ $element_id ] );
}
private function group_and_populate_cache( $elements ) {
$trids = array();
foreach ( $elements as $element ) {
$trid = $element['trid'];
if ( ! isset( $trids[ $trid ] ) ) {
$trids[ $trid ] = array();
}
$trids[ $trid ][] = $element;
}
foreach ( $trids as $trid_group ) {
$this->populate_cache( $trid_group );
}
}
private function populate_cache( $elements ) {
if ( ! $elements ) {
return;
}
$element_ids = array();
foreach ( $elements as $element ) {
$element_id = $element['element_id'];
$language_code = $element['language_code'];
$element_ids[ $language_code ] = $element_id;
$this->element_data[ $element_id ] = array(
'translation_id' => $element['translation_id'],
'trid' => $element['trid'],
'lang' => $language_code,
'source_lang' => $element['source_language_code'],
'type' => substr( $element['element_type'], $this->type_prefix_length ),
);
$this->translation_ids_element[ $element['translation_id'] ] = $element_id;
}
foreach ( $element_ids as $element_id ) {
$trid = $this->element_data[ $element_id ]['trid'];
$this->trid_groups[ $trid ] = $element_ids;
$this->translations[ $element_id ] = &$this->trid_groups[ $trid ];
}
}
private function filter_for_actual_trans( $element_id ) {
$res = $this->translations[ $element_id ];
foreach ( $res as $lang => $element ) {
if ( $this->element_data[ $element ]['source_lang'] !== $this->element_data[ $element_id ]['lang'] ) {
unset( $res[ $lang ] );
}
}
return $res;
}
/**
* @param int $post_id
*
* @return bool
*/
public function is_a_duplicate( $post_id ) {
return (bool) get_post_meta( $post_id, '_icl_lang_duplicate_of', true );
}
}