class-wpml-translations.php
10.9 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
<?php
/**
* @author OnTheGo Systems
*/
class WPML_Translations extends WPML_SP_User {
/** @var bool */
public $skip_empty = false;
/** @var bool */
public $all_statuses = false;
/** @var bool */
public $skip_cache = false;
/** @var bool */
public $skip_recursions = false;
private $duplicated_by = array();
private $mark_as_duplicate_meta_key = '_icl_lang_duplicate_of';
private $wpml_cache;
/**
* WPML_Translations constructor.
*
* @param SitePress $sitepress
* @param WPML_WP_Cache $wpml_cache
*/
public function __construct( SitePress $sitepress, WPML_WP_Cache $wpml_cache = null ) {
parent::__construct( $sitepress );
$this->wpml_cache = $wpml_cache ? $wpml_cache : new WPML_WP_Cache( WPML_ELEMENT_TRANSLATIONS_CACHE_GROUP );
}
/**
* @param int $trid
* @param string $wpml_element_type
* @param bool $skipPrivilegeChecking
*
* @return array<string,\stdClass>
*/
public function get_translations( $trid, $wpml_element_type, $skipPrivilegeChecking = false ) {
$cache_key_args = array_filter( array( $trid, $wpml_element_type, $this->skip_empty, $this->all_statuses, $this->skip_recursions ) );
$cache_key = md5( (string) wp_json_encode( $cache_key_args ) );
$cache_found = false;
$temp_elements = $this->wpml_cache->get( $cache_key, $cache_found );
if ( ! $this->skip_cache && $cache_found ) {
return $temp_elements;
}
$translations = array();
$sql_parts = array(
'select' => array(),
'join' => array(),
'where' => array(),
'group_by' => array(),
);
if ( $trid ) {
if ( $this->wpml_element_type_is_post( $wpml_element_type ) ) {
$sql_parts = $this->get_sql_parts_for_post( $wpml_element_type, $sql_parts, $skipPrivilegeChecking );
} elseif ( $this->wpml_element_type_is_taxonomy( $wpml_element_type ) ) {
$sql_parts = $this->get_sql_parts_for_taxonomy( $sql_parts );
}
$sql_parts['where'][] = $this->sitepress->get_wpdb()->prepare( ' AND wpml_translations.trid=%d ', $trid );
$select = implode( ' ', $sql_parts['select'] );
$join = implode( ' ', $sql_parts['join'] );
$where = implode( ' ', $sql_parts['where'] );
$group_by = implode( ' ', $sql_parts['group_by'] );
$query = "
SELECT wpml_translations.translation_id, wpml_translations.language_code, wpml_translations.element_id, wpml_translations.source_language_code, wpml_translations.element_type, NULLIF(wpml_translations.source_language_code, '') IS NULL AS original
{$select}
FROM {$this->sitepress->get_wpdb()->prefix}icl_translations wpml_translations
{$join}
WHERE 1 {$where}
{$group_by}
";
$results = $this->sitepress->get_wpdb()->get_results( $query );
foreach ( $results as $translation ) {
if ( $this->must_ignore_translation( $translation ) ) {
continue;
}
$translations[ $translation->language_code ] = $translation;
}
}
if ( $translations ) {
$this->wpml_cache->set( $cache_key, $translations );
}
return $translations;
}
public function link_elements( WPML_Translation_Element $source_translation_element, WPML_Translation_Element $target_translation_element, $target_language = null ) {
if ( null !== $target_language ) {
$this->set_language_code( $target_translation_element, $target_language );
}
$this->set_source_element( $target_translation_element, $source_translation_element );
}
public function set_source_element( WPML_Translation_Element $element, WPML_Translation_Element $source_element ) {
$this->elements_type_matches( $element, $source_element );
$this->sitepress->set_element_language_details( $element->get_element_id(), $element->get_wpml_element_type(), $source_element->get_trid(), $element->get_language_code(), $source_element->get_language_code() );
$element->flush_cache();
}
private function elements_type_matches( $element1, $element2 ) {
if ( get_class( $element1 ) !== get_class( $element2 ) ) {
throw new UnexpectedValueException( '$source_element is not an instance of ' . get_class( $element1 ) . ': instance of ' . get_class( $element2 ) . ' received instead.' );
}
}
/**
* @param WPML_Translation_Element $element
* @param string $language_code
*/
public function set_language_code( WPML_Translation_Element $element, $language_code ) {
$element_id = $element->get_element_id();
$wpml_element_type = $element->get_wpml_element_type();
$trid = $element->get_trid();
$this->sitepress->set_element_language_details( $element_id, $wpml_element_type, $trid, $language_code );
$element->flush_cache();
}
/**
* @param WPML_Translation_Element $element
* @param int $trid
*
* @throws \UnexpectedValueException
*/
public function set_trid( WPML_Translation_Element $element, $trid ) {
if ( ! $element->get_language_code() ) {
throw new UnexpectedValueException( 'Element has no language information.' );
}
$this->sitepress->set_element_language_details( $element->get_element_id(), $element->get_wpml_element_type(), $trid, $element->get_language_code() );
$element->flush_cache();
}
/**
* @param \WPML_Translation_Element $duplicate
* @param \WPML_Translation_Element $original
*
* @throws \UnexpectedValueException
*/
public function make_duplicate_of( WPML_Translation_Element $duplicate, WPML_Translation_Element $original ) {
$this->validate_duplicable_element( $duplicate );
$this->validate_duplicable_element( $original, 'source' );
$this->set_source_element( $duplicate, $original );
update_post_meta( $duplicate->get_id(), $this->mark_as_duplicate_meta_key, $original->get_id() );
$duplicate->flush_cache();
$this->duplicated_by[ $duplicate->get_id() ] = array();
}
/**
* @param \WPML_Translation_Element $element
*
* @return WPML_Post_Element
* @throws \InvalidArgumentException
*/
public function is_a_duplicate_of( WPML_Translation_Element $element ) {
$this->validate_duplicable_element( $element );
$duplicate_of = get_post_meta( $element->get_id(), $this->mark_as_duplicate_meta_key, true );
if ( $duplicate_of ) {
return new WPML_Post_Element( $duplicate_of, $this->sitepress );
}
return null;
}
/**
* @param \WPML_Translation_Element $element
*
* @return array
* @throws \UnexpectedValueException
* @throws \InvalidArgumentException
*/
public function is_duplicated_by( WPML_Translation_Element $element ) {
$this->validate_duplicable_element( $element );
$this->init_cache_for_element( $element );
if ( ! $this->duplicated_by[ $element->get_id() ] ) {
$this->duplicated_by[ $element->get_id() ] = array();
$args = array(
'post_type' => $element->get_wp_element_type(),
'meta_query' => array(
array(
'key' => $this->mark_as_duplicate_meta_key,
'value' => $element->get_id(),
'compare' => '=',
),
),
);
$query = new WP_Query( $args );
$results = $query->get_posts();
foreach ( $results as $post ) {
$this->duplicated_by[ $element->get_id() ][] = new WPML_Post_Element( $post->ID, $this->sitepress );
}
}
return $this->duplicated_by[ $element->get_id() ];
}
/**
* @param \WPML_Translation_Element $element
* @param string $argument_name
*
* @throws \UnexpectedValueException
*/
private function validate_duplicable_element( WPML_Translation_Element $element, $argument_name = 'element' ) {
if ( ! ( $element instanceof WPML_Duplicable_Element ) ) {
throw new UnexpectedValueException( sprintf( 'Argument %s does not implement `WPML_Duplicable_Element`.', $argument_name ) );
}
}
/**
* @param \WPML_Translation_Element $element
*/
private function init_cache_for_element( WPML_Translation_Element $element ) {
if ( ! array_key_exists( $element->get_id(), $this->duplicated_by ) ) {
$this->duplicated_by[ $element->get_id() ] = array();
}
}
/**
* @param string $element_type
* @param array<string,array<string>> $sql_parts
* @param bool $skipPrivilegeChecking
*
* @return array<string,array<string>>
*/
private function get_sql_parts_for_post( $element_type, $sql_parts, $skipPrivilegeChecking = false ) {
$sql_parts['select'][] = ', p.post_title, p.post_status';
$sql_parts['join'][] = " LEFT JOIN {$this->sitepress->get_wpdb()->posts} p ON wpml_translations.element_id=p.ID";
if ( ! $this->all_statuses && 'post_attachment' !== $element_type && ! is_admin() ) {
$public_statuses_where = $this->get_public_statuses();
// the current user may not be the admin but may have read private post/page caps!
if ( current_user_can( 'read_private_pages' ) || current_user_can( 'read_private_posts' ) || $skipPrivilegeChecking ) {
$sql_parts['where'][] = ' AND (p.post_status IN (' . $public_statuses_where . ", 'draft', 'private', 'pending' ))";
} else {
$sql_parts['where'][] = ' AND (';
$sql_parts['where'][] = 'p.post_status IN (' . $public_statuses_where . ') ';
if ( $uid = $this->sitepress->get_current_user()->ID ) {
$sql_parts['where'][] = $this->sitepress->get_wpdb()->prepare( " OR (post_status in ('draft', 'private', 'pending') AND post_author = %d)", $uid );
}
$sql_parts['where'][] = ') ';
}
}
return $sql_parts;
}
/**
* @return string
*/
private function get_public_statuses() {
return wpml_prepare_in( get_post_stati( [ 'public' => true ] ) );
}
/**
* @param array<string,array<string>> $sql_parts
*
* @return array<string,array<string>>
*/
private function get_sql_parts_for_taxonomy( $sql_parts ) {
$sql_parts['select'][] = ', tm.name, tm.term_id, COUNT(tr.object_id) AS instances';
$sql_parts['join'][] = " LEFT JOIN {$this->sitepress->get_wpdb()->term_taxonomy} tt ON wpml_translations.element_id=tt.term_taxonomy_id
LEFT JOIN {$this->sitepress->get_wpdb()->terms} tm ON tt.term_id = tm.term_id
LEFT JOIN {$this->sitepress->get_wpdb()->term_relationships} tr ON tr.term_taxonomy_id=tt.term_taxonomy_id
";
$sql_parts['group_by'][] = 'GROUP BY tm.term_id';
return $sql_parts;
}
/**
* @param stdClass $translation
*
* @return bool
*/
private function must_ignore_translation( stdClass $translation ) {
return $this->skip_empty
&& (
! $translation->element_id
|| $this->must_ignore_translation_for_taxonomy( $translation )
);
}
/**
* @param stdClass $translation
*
* @return bool
*/
private function must_ignore_translation_for_taxonomy( stdClass $translation ) {
return $this->wpml_element_type_is_taxonomy( $translation->element_type )
&& $translation->instances === 0
&& ( ! $this->skip_recursions && ! _icl_tax_has_objects_recursive( $translation->element_id ) );
}
/**
* @param string $wpml_element_type
*
* @return int
*/
private function wpml_element_type_is_taxonomy( $wpml_element_type ) {
return preg_match( '#^tax_(.+)$#', $wpml_element_type );
}
/**
* @param string $wpml_element_type
*
* @return bool
*/
private function wpml_element_type_is_post( $wpml_element_type ) {
return 0 === strpos( $wpml_element_type, 'post_' );
}
}