DomainsLocalesMapper.php
2.3 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
<?php
namespace WPML\ST\TranslationFile;
use wpdb;
use WPML\Collect\Support\Collection;
use WPML\FP\Fns;
use WPML\FP\Lst;
use WPML\FP\Obj;
use WPML_Locale;
class DomainsLocalesMapper {
const ALIAS_STRINGS = 's';
const ALIAS_STRING_TRANSLATIONS = 'st';
/** @var wpdb $wpdb */
private $wpdb;
/** @var WPML_Locale $locale */
private $locale;
public function __construct( wpdb $wpdb, WPML_Locale $locale ) {
$this->wpdb = $wpdb;
$this->locale = $locale;
}
/**
* @param array $string_translation_ids
*
* @return Collection of objects with properties `domain` and `locale`
*/
public function get_from_translation_ids( array $string_translation_ids ) {
return $this->get_results_where( self::ALIAS_STRING_TRANSLATIONS, $string_translation_ids );
}
/**
* @param array $string_ids
*
* @return Collection of objects with properties `domain` and `locale`
*/
public function get_from_string_ids( array $string_ids ) {
return $this->get_results_where( self::ALIAS_STRINGS, $string_ids );
}
/**
* @param callable $getActiveLanguages
* @param string $domain
*
* @return array
*/
public function get_from_domain( callable $getActiveLanguages, $domain ) {
$createEntity = function ( $locale ) use ( $domain ) {
return (object) [
'domain' => $domain,
'locale' => $locale,
];
};
/** @var array $defaultLocaleList */
$defaultLocaleList = Lst::pluck( 'default_locale', $getActiveLanguages() );
return Fns::map( $createEntity, Obj::values( $defaultLocaleList ) );
}
/**
* @param string $table_alias
* @param array $ids
*
* @return Collection
*/
private function get_results_where( $table_alias, array $ids ) {
$results = [];
if ( array_filter( $ids ) ) {
$results = $this->wpdb->get_results(
"
SELECT DISTINCT
s.context AS domain,
st.language
FROM {$this->wpdb->prefix}icl_string_translations AS " . self::ALIAS_STRING_TRANSLATIONS . "
JOIN {$this->wpdb->prefix}icl_strings AS " . self::ALIAS_STRINGS . " ON s.id = st.string_id
WHERE $table_alias.id IN(" . wpml_prepare_in( $ids ) . ')
'
);
}
return wpml_collect( $results )->map(
function( $row ) {
return (object) [
'domain' => $row->domain,
'locale' => $this->locale->get_locale( $row->language ),
];
}
);
}
}