wpml-st-translations-file-scan-storage.php
2.45 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
<?php
class WPML_ST_Translations_File_Scan_Storage {
/** @var wpdb */
private $wpdb;
/** @var WPML_ST_Bulk_Strings_Insert */
private $bulk_insert;
/**
* @param wpdb $wpdb
* @param WPML_ST_Bulk_Strings_Insert $bulk_insert
*/
public function __construct( wpdb $wpdb, WPML_ST_Bulk_Strings_Insert $bulk_insert ) {
$this->wpdb = $wpdb;
$this->bulk_insert = $bulk_insert;
}
public function save( array $translations, $domain, $lang ) {
$this->bulk_insert->insert_strings( $this->build_string_collection( $translations, $domain ) );
$string_translations = $this->build_string_translation_collection(
$translations,
$lang,
$this->get_string_maps( $domain )
);
$this->bulk_insert->insert_string_translations( $string_translations );
}
/**
* @param WPML_ST_Translations_File_Translation[] $translations
* @param string $domain
*
* @return WPML_ST_Models_String[]
*/
private function build_string_collection( array $translations, $domain ) {
$result = array();
foreach ( $translations as $translation ) {
$result[] = new WPML_ST_Models_String(
'en',
$domain,
$translation->get_context(),
$translation->get_original(),
ICL_TM_NOT_TRANSLATED
);
}
return $result;
}
/**
* @param string $domain
*
* @return array
*/
private function get_string_maps( $domain ) {
$sql = "
SELECT id, value, gettext_context FROM {$this->wpdb->prefix}icl_strings
WHERE context = %s
";
$rowset = $this->wpdb->get_results( $this->wpdb->prepare( $sql, $domain ) );
$result = array();
foreach ( $rowset as $row ) {
$result[ $row->value ][ $row->gettext_context ] = $row->id;
}
return $result;
}
/**
* @param WPML_ST_Translations_File_Translation[] $translations
* @param string $lang
* @param array $value_id_map
*
* @return WPML_ST_Models_String_Translation[]
*/
private function build_string_translation_collection( array $translations, $lang, $value_id_map ) {
$result = array();
foreach ( $translations as $translation ) {
if ( ! isset( $value_id_map[ $translation->get_original() ] ) ) {
continue;
}
$result[] = new WPML_ST_Models_String_Translation(
$value_id_map[ $translation->get_original() ][ $translation->get_context() ],
$lang,
ICL_TM_NOT_TRANSLATED,
null,
$translation->get_translation()
);
}
return $result;
}
}