wpml-media-translations-ui.php
19.6 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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
<?php
/**
* Class WPML_Media_Translations_UI
*/
class WPML_Media_Translations_UI extends WPML_Templates_Factory {
const PREVIEW_MAX_WIDTH = 800;
const PREVIEW_MAX_HEIGHT = 600;
/**
* @var SitePress
*/
private $sitepress;
/**
* @var wpdb
*/
private $wpdb;
/**
* @var WP_Locale
*/
private $wp_locale;
/**
* @var WPML_Query_Filter
*/
private $wpml_query_filter;
/**
* @var WPML_Admin_Pagination
*/
private $pagination;
/**
* @var array
*/
private $query_args = array();
/**
* WPML_Media_Translations_UI constructor.
*
* @param SitePress $sitepress
* @param wpdb $wpdb
* @param WP_Locale $wp_locale
* @param WPML_Query_Filter $wpml_query_filter
* @param WPML_Admin_Pagination $pagination
*/
public function __construct(
SitePress $sitepress,
wpdb $wpdb,
WP_Locale $wp_locale,
WPML_Query_Filter $wpml_query_filter,
WPML_Admin_Pagination $pagination
) {
parent::__construct();
$this->sitepress = $sitepress;
$this->wpdb = $wpdb;
$this->wp_locale = $wp_locale;
$this->wpml_query_filter = $wpml_query_filter;
$this->pagination = $pagination;
}
/**
* @return array
*/
public function get_model() {
$this->set_query_args();
$languages = $this->get_languages();
$model = array(
'strings' => array(
'heading' => __( 'Media Translation', 'wpml-media' ),
'filter_by_date' => __( 'Filter by date', 'wpml-media' ),
'all_dates' => __( 'All dates', 'wpml-media' ),
'in' => __( 'in', 'wpml_media' ),
'to' => __( 'to', 'wpml_media' ),
'filter_by_status' => __( 'Filter by translation status', 'wpml-media' ),
'status_all' => __( 'All translation statuses', 'wpml-media' ),
'status_not' => __( 'Media file not translated', 'wpml-media' ),
'status_translated' => __( 'Translated media uploaded', 'wpml-media' ),
'status_in_progress' => __( 'Translation in progress', 'wpml-media' ),
'status_needs_translation' => __( 'Needs media file translation', 'wpml-media' ),
'filter_by_language' => __( 'Filter by language', 'wpml-media' ),
'any_language' => __( 'Any language', 'wpml-media' ),
'search_media' => __( 'Search Media:', 'wpml-media' ),
'search_placeholder' => __( 'Title, caption or description', 'wpml-media' ),
'search_button_label' => __( 'Filter', 'wpml-media' ),
'original_language' => __( 'Original language', 'wpml-media' ),
'no_attachments' => __( 'No attachments found', 'wpml-media' ),
'add_translation' => __( 'Add media file translation', 'wpml-media' ),
'edit_translation' => __( 'Edit %s translation', 'wpml-media' ),
'original' => __( 'Original:', 'wpml-media' ),
'translation' => __( 'Translation:', 'wpml-media' ),
'file' => __( 'File', 'wpml-media' ),
'name' => __( 'Name', 'wpml-media' ),
'caption' => __( 'Caption', 'wpml-media' ),
'alt_text' => __( 'Alt text', 'wpml-media' ),
'description' => __( 'Description', 'wpml-media' ),
'copy_from_original' => __( 'Copy from original', 'wpml-media' ),
'upload_translated_media' => __( 'Upload translated media file', 'wpml-media' ),
'use_different_file' => __( 'Use a different file', 'wpml-media' ),
'revert_to_original' => __( 'Revert to original', 'wpml-media' ),
'restore_original_media' => __( 'Restore original media file', 'wpml-media' ),
'statuses' => self::get_translation_status_labels(),
'texts_change_notice' => __( 'Any changes you make to the text here will not affect any previous publications of this media on your website. This edited version will only appear if you select it from the library to be embedded.', 'wpml-media' ),
),
'months' => $this->get_months(),
'selected_month' => isset( $this->query_args['m'] ) ? (int) $this->query_args['m'] : 0,
'selected_status' => isset( $this->query_args['status'] ) ? $this->query_args['status'] : '',
'from_language' => isset( $this->query_args['slang'] ) ? $this->query_args['slang'] : '',
'to_language' => isset( $this->query_args['tlang'] ) ? $this->query_args['tlang'] : '',
'statuses' => array(
'not_translated' => WPML_Media_Translation_Status::NOT_TRANSLATED,
'in_progress' => WPML_Media_Translation_Status::IN_PROGRESS,
'translated' => WPML_Media_Translation_Status::TRANSLATED,
'needs_translation' => WPML_Media_Translation_Status::NEEDS_MEDIA_TRANSLATION,
),
'search' => isset( $this->query_args['s'] ) ? $this->query_args['s'] : '',
'languages' => $languages,
'attachments' => $this->get_attachments( $languages ),
'nonce' => wp_nonce_field( 'media-translation', 'wpnonce', false, false ),
'pagination' => $this->get_pagination(),
'target_language' => $this->should_filter_by_target_language() ? $this->query_args['tlang'] : '',
'batch_translation' => $this->get_batch_translation(),
'show_text_change_notice' => ! get_user_meta( get_current_user_id(), WPML_Media_Editor_Notices::TEXT_EDIT_NOTICE_DISMISSED, true ),
);
return $model;
}
public static function get_translation_status_labels() {
return array(
WPML_Media_Translation_Status::NOT_TRANSLATED => __( 'Not translated', 'wpml-media' ),
WPML_Media_Translation_Status::IN_PROGRESS => __( 'In progress', 'wpml-media' ),
WPML_Media_Translation_Status::TRANSLATED => __( 'Translated', 'wpml-media' ),
WPML_Media_Translation_Status::NEEDS_MEDIA_TRANSLATION => __( 'Needs translation', 'wpml-media' ),
);
}
private function set_query_args() {
$arg_keys = array( 'm', 'status', 'slang', 'tlang', 's', 'paged' );
foreach ( $arg_keys as $key ) {
if ( isset( $_GET[ $key ] ) ) {
$this->query_args[ $key ] = sanitize_text_field( $_GET[ $key ] );
}
}
}
/**
* @return array
*/
private function get_months() {
$months = array();
$month_results = $this->wpdb->get_results(
"
SELECT DISTINCT YEAR( post_date ) AS year, MONTH( post_date ) AS month
FROM {$this->wpdb->posts}
WHERE post_type = 'attachment'
ORDER BY post_date DESC
"
);
foreach ( $month_results as $month ) {
$months[] = array(
'id' => $month->year . zeroise( $month->month, 2 ),
'label' => sprintf( __( '%1$s %2$d' ), $this->wp_locale->get_month( $month->month ), $month->year ),
);
}
return $months;
}
/**
* @return array
*/
private function get_languages() {
$languages = array();
$active_languages = $this->sitepress->get_active_languages();
foreach ( $active_languages as $language ) {
$languages[ $language['code'] ] = array(
'name' => $language['display_name'],
'flag' => $this->sitepress->get_flag_url( $language['code'] ),
);
}
return $languages;
}
private function get_items_per_page() {
return get_option( 'wpml_media_translation_dashboard_items_per_page', 20 );
}
/**
* @param array $languages
*
* @return array
*/
private function get_attachments( $languages ) {
$attachments = array();
$args = array(
'post_type' => 'attachment',
'orderby' => 'ID',
'order' => 'DESC',
'posts_per_page' => $this->get_items_per_page(),
);
$args = array_merge( $args, $this->query_args );
if ( $this->should_filter_by_status() ) {
$args['meta_query'] = array(
'key' => '_media_translation_status',
'value' => $this->query_args['status'],
);
}
$this->add_query_filters();
$attachment_posts = get_posts( $args );
$this->remove_query_filters();
foreach ( $attachment_posts as $attachment ) {
$attachment_id = $attachment->ID;
$post_element = new WPML_Post_Element( $attachment_id, $this->sitepress );
$media_file_original = get_post_meta( $attachment_id, '_wp_attached_file', true );
$translations = array();
$is_image = (int) ( 0 === strpos( $attachment->post_mime_type, 'image/' ) );
foreach ( $languages as $code => $language ) {
$translation = $post_element->get_translation( $code );
if ( $translation ) {
$translation_post = $translation->get_wp_object();
$media_file_translated = get_post_meta( $translation->get_id(), '_wp_attached_file', true );
$translations[ $code ] = array(
'id' => $translation->get_id(),
'file_name' => basename( $media_file_translated ),
'title' => $translation_post->post_title,
'caption' => $translation_post->post_excerpt,
'description' => $translation_post->post_content,
'alt' => get_post_meta( $translation->get_id(), '_wp_attachment_image_alt', true ),
'thumb' => $this->get_attachment_thumb( $translation->get_id(), $is_image ),
'media_is_translated' => $media_file_translated && $media_file_translated !== $media_file_original,
'status' => $this->get_translation_status( $attachment_id, $translation ),
);
} else {
$translations[ $code ] = array(
'status' => WPML_Media_Translation_Status::NOT_TRANSLATED,
);
}
}
$attachments[] = array(
'post' => $attachment,
'mime_type' => $is_image ? 'image/*' : $attachment->post_mime_type,
'is_image' => $is_image,
'file_name' => basename( $media_file_original ),
'alt' => get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ),
'meta' => get_post_meta( $attachment_id, '_wp_attachment_metadata', true ),
'language' => $post_element->get_language_code(),
'thumb' => $this->get_attachment_thumb( $attachment_id, $is_image ),
'url' => $is_image ? $this->get_attachment_url( $attachment_id ) : '',
'translations' => $translations,
'preview' => [
'width' => self::PREVIEW_MAX_WIDTH,
'height' => self::PREVIEW_MAX_HEIGHT,
],
);
}
return $attachments;
}
/**
* @param int $attachment_id
*
* @return array
*/
private function get_attachment_url( $attachment_id ) {
$image = wp_get_attachment_image_src( $attachment_id, [ self::PREVIEW_MAX_WIDTH, self::PREVIEW_MAX_HEIGHT ] );
return $image[0];
}
/**
* @param int $attachment_id
* @param bool $is_image
*
* @return array
*/
private function get_attachment_thumb( $attachment_id, $is_image = true ) {
$image = wp_get_attachment_image_src( $attachment_id, 'thumbnail', true );
$thumb = array(
'src' => $image ? $image[0] : '',
'width' => $is_image ? 60 : $image[1],
'height' => $is_image ? 60 : $image[2],
);
return $thumb;
}
public function set_total_items_in_pagination( $query ) {
$query_count = preg_replace(
'/^SELECT /',
'SELECT SQL_CALC_FOUND_ROWS ',
trim( $query )
);
$this->wpdb->get_results( $query_count );
$this->pagination->set_total_items( $this->wpdb->get_var( 'SELECT FOUND_ROWS()' ) );
return $query;
}
private function get_pagination() {
$this->pagination->set_items_per_page( $this->get_items_per_page() );
$current_page = isset( $this->query_args['paged'] ) ? $this->query_args['paged'] : 1;
$this->pagination->set_current_page( $current_page );
$model = array(
'strings' => array(
'list_navigation' => __( 'Navigation', 'wpml-media' ),
'of' => __( 'of', 'wpml-media' ),
),
'pagination' => array(
'get_first_page_url' => $this->pagination->get_first_page_url(),
'first_page' => __( 'First page', 'wpml-media' ),
'get_previous_page_url' => $this->pagination->get_previous_page_url(),
'previous_page' => __( 'Previous page', 'wpml-media' ),
'get_current_page' => $this->pagination->get_current_page(),
'get_total_pages' => $this->pagination->get_total_pages(),
'get_next_page_url' => $this->pagination->get_next_page_url(),
'next_page' => __( 'Next page', 'wpml-media' ),
'get_last_page_url' => $this->pagination->get_last_page_url(),
'last_page' => __( 'Last page', 'wpml-media' ),
),
'total_items' => sprintf(
_n( '%s item', '%s items', $this->pagination->get_total_items() ),
$this->pagination->get_total_items()
),
);
return $model;
}
private function get_batch_translation() {
$model = array(
'strings' => array(
'close' => __( 'Close', 'wpml-media' ),
'was_replaced' => __( 'The media that you uploaded was replaced in these translated posts:', 'wpml-media' ),
'other_posts' => __( 'The same media might be used in other posts too. Do you want to scan and replace it?', 'wpml-media' ),
'without_usage' => __( 'The media that you uploaded will be used in future post translations. The same media might be used in already existing posts. Do you want to scan and replace it now?', 'wpml-media' ),
'scan_for_this_media' => __( 'Scan for content that has specifically this media (faster)', 'wpml-media' ),
'scan_for_all_media' => __( 'Scan for content that has any of the media files that I translated (takes more time)', 'wpml-media' ),
'button_label' => _x( 'Scan & Replace', 'Button label (verb)', 'wpml-media' ),
),
);
return $model;
}
private function add_query_filters() {
remove_filter( 'posts_join', array( $this->wpml_query_filter, 'posts_join_filter' ), 10 );
remove_filter( 'posts_where', array( $this->wpml_query_filter, 'posts_where_filter' ), 10 );
add_filter( 'posts_join', array( $this, 'filter_request_clause_join' ), PHP_INT_MAX );
add_filter( 'posts_where', array( $this, 'filter_request_clause_where' ), PHP_INT_MAX );
add_filter( 'posts_request', array( $this, 'set_total_items_in_pagination' ), - PHP_INT_MAX );
}
private function remove_query_filters() {
add_filter( 'posts_join', array( $this->wpml_query_filter, 'posts_join_filter' ), 10, 2 );
add_filter( 'posts_where', array( $this->wpml_query_filter, 'posts_where_filter' ), 10, 2 );
remove_filter( 'posts_join', array( $this, 'filter_request_clause_join' ), PHP_INT_MAX );
remove_filter( 'posts_where', array( $this, 'filter_request_clause_where' ), PHP_INT_MAX );
remove_filter( 'posts_request', array( $this, 'set_total_items_in_pagination' ), - PHP_INT_MAX );
}
public function filter_request_clause_join( $join ) {
$join .= " LEFT JOIN {$this->wpdb->prefix}icl_translations icl_translations_source
ON icl_translations_source.element_id = {$this->wpdb->posts}.ID ";
if ( $this->should_filter_by_status() ) {
if ( $this->should_filter_by_target_language() ) {
$join .= $this->wpdb->prepare(
" LEFT JOIN {$this->wpdb->postmeta} post_meta
ON icl_translations_source.element_id = post_meta.post_id
AND post_meta.meta_key = %s
",
WPML_Media_Translation_Status::STATUS_PREFIX . $this->query_args['tlang']
);
} else {
$active_language = $this->sitepress->get_active_languages();
$default_lanuage = $this->sitepress->get_default_language();
foreach ( $active_language as $language ) {
if ( $language['code'] !== $default_lanuage ) {
$sanitized_code = str_replace( '-', '_', $language['code'] );
$join .= $this->wpdb->prepare(
" LEFT JOIN {$this->wpdb->postmeta} post_meta_{$sanitized_code}
ON {$this->wpdb->posts}.ID = post_meta_{$sanitized_code}.post_id
AND post_meta_{$sanitized_code}.meta_key=%s",
WPML_Media_Translation_Status::STATUS_PREFIX . $language['code']
);
}
}
}
} else {
if ( $this->should_filter_by_target_language() ) {
$join .= " JOIN {$this->wpdb->prefix}icl_translations icl_translations_target
ON icl_translations_target.trid = icl_translations_source.trid
AND icl_translations_target.language_code='{$this->query_args['tlang']}'";
}
}
return $join;
}
public function filter_request_clause_where( $where ) {
$where .= " AND icl_translations_source.element_type='post_attachment'
AND icl_translations_source.source_language_code IS NULL ";
if ( ! empty( $this->query_args['slang'] ) && $this->query_args['slang'] !== 'all' ) {
$where .= $this->wpdb->prepare( ' AND icl_translations_source.language_code = %s ', $this->query_args['slang'] );
}
if ( $this->should_filter_by_status() ) {
if ( $this->should_filter_by_target_language() ) {
if ( $this->query_args['status'] === WPML_Media_Translation_Status::NOT_TRANSLATED ) {
$where .= $this->wpdb->prepare(
' AND (
post_meta.meta_value IS NULL OR
post_meta.meta_value = %s OR
post_meta.meta_value = %s OR
post_meta.meta_value = %s
)',
WPML_Media_Translation_Status::NOT_TRANSLATED,
WPML_Media_Translation_Status::IN_PROGRESS,
WPML_Media_Translation_Status::NEEDS_MEDIA_TRANSLATION
);
} else {
$where .= $this->wpdb->prepare( ' AND post_meta.meta_value = %s', $this->query_args['status'] );
}
} else {
$active_language = $this->sitepress->get_active_languages();
$default_language = $this->sitepress->get_default_language();
if ( $this->query_args['status'] === WPML_Media_Translation_Status::TRANSLATED ) {
foreach ( $active_language as $language ) {
$sanitized_code = str_replace( '-', '_', $language['code'] );
if ( $language['code'] !== $default_language ) {
$where .= $this->wpdb->prepare(
"AND post_meta_{$sanitized_code}.meta_value = %s",
WPML_Media_Translation_Status::TRANSLATED
);
}
}
} elseif ( $this->query_args['status'] === WPML_Media_Translation_Status::NOT_TRANSLATED ) {
$where .= 'AND ( 0 ';
foreach ( $active_language as $language ) {
$sanitized_code = str_replace( '-', '_', $language['code'] );
if ( $language['code'] !== $default_language ) {
$where .= $this->wpdb->prepare(
"
OR post_meta_{$sanitized_code}.meta_value = %s
OR post_meta_{$sanitized_code}.meta_value IS NULL",
$this->query_args['status']
);
}
}
$where .= ') ';
} else {
$where .= 'AND ( 0 ';
foreach ( $active_language as $language ) {
$sanitized_code = str_replace( '-', '_', $language['code'] );
if ( $language['code'] !== $default_language ) {
$where .= $this->wpdb->prepare(
"
OR post_meta_{$sanitized_code}.meta_value = %s",
$this->query_args['status']
);
}
}
$where .= ') ';
}
}
}
return $where;
}
private function should_filter_by_status() {
return isset( $this->query_args['status'] ) && $this->query_args['status'] !== '';
}
private function should_filter_by_target_language() {
return isset( $this->query_args['tlang'] ) && $this->query_args['tlang'] !== '';
}
protected function init_template_base_dir() {
$this->template_paths = array(
WPML_MEDIA_PATH . '/templates/menus/',
WPML_PLUGIN_PATH . '/templates/pagination/',
);
}
/**
* @return string
*/
public function get_template() {
return 'media-translation.twig';
}
/**
* @param $attachment_id
* @param $translation
*
* @return mixed|string
*/
private function get_translation_status( $attachment_id, $translation ) {
$translation_status = get_post_meta(
$attachment_id,
WPML_Media_Translation_Status::STATUS_PREFIX . $translation->get_language_code(),
true
);
if ( ! $translation_status ) {
$translation_status = WPML_Media_Translation_Status::NOT_TRANSLATED;
}
return $translation_status;
}
}