class-wpml-media-string-images-translation.php
1.46 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
<?php
/**
* Class WPML_Media_String_Images_Translation
* Translate images in posts strings translations when a string translation is created or updated
*/
class WPML_Media_String_Images_Translation implements IWPML_Action {
/**
* @var WPML_Media_String_Images_Translation
*/
private $images_updater;
/**
* @var WPML_ST_String_Factory
*/
private $string_factory;
/**
* WPML_Media_String_Images_Translation constructor.
*
* @param WPML_Media_Translated_Images_Update $images_updater
* @param WPML_ST_String_Factory $string_factory
*/
public function __construct( WPML_Media_Translated_Images_Update $images_updater, WPML_ST_String_Factory $string_factory ) {
$this->images_updater = $images_updater;
$this->string_factory = $string_factory;
}
public function add_hooks() {
add_filter( 'wpml_st_string_translation_before_save', array( $this, 'translate_images' ), PHP_INT_MAX, 3 );
}
/**
* @param array $translation_data
* @param string $target_language
* @param int $string_id
*
* @return array
*/
public function translate_images( $translation_data, $target_language, $string_id ) {
if ( ! empty( $translation_data['value'] ) ) {
$original_string = $this->string_factory->find_by_id( $string_id );
$translation_data['value'] = $this->images_updater->replace_images_with_translations(
$translation_data['value'],
$target_language,
$original_string->get_language()
);
}
return $translation_data;
}
}