class-wpml-pb-package-strings-resave.php
1.59 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
<?php
class WPML_PB_Package_Strings_Resave {
/** @var WPML_ST_String_Factory $string_factory */
private $string_factory;
public function __construct( WPML_ST_String_Factory $string_factory ) {
$this->string_factory = $string_factory;
}
/**
* @param WPML_Post_Element $post_element
*
* @return WPML_Package[]
*/
public function from_element( WPML_Post_Element $post_element ) {
if ( ! $post_element->get_source_element() ) {
return array();
}
$target_lang = $post_element->get_language_code();
$original_post_id = $post_element->get_source_element()->get_id();
/** @var WPML_Package[] $string_packages */
$string_packages = apply_filters( 'wpml_st_get_post_string_packages', array(), $original_post_id );
foreach ( $string_packages as $string_package ) {
/** @var stdClass[] $strings */
$strings = $string_package->get_package_strings();
foreach ( $strings as $string ) {
$this->resave_string_translation( $string->id, $target_lang );
}
}
return $string_packages;
}
/**
* @param int $string_id
* @param string $target_lang
*/
private function resave_string_translation( $string_id, $target_lang ) {
$string = $this->string_factory->find_by_id( $string_id );
$translations = wp_list_filter( $string->get_translations(), array( 'language' => $target_lang ) );
if ( $translations ) {
$translation = reset( $translations );
$string->set_translation(
$target_lang,
$translation->value,
$translation->status,
$translation->translator_id,
$translation->translation_service,
$translation->batch_id
);
}
}
}