class-wpml-translation-element.php
5.55 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
<?php
/**
* Use this class as parent class for translatable elements in WPML,
* to have a common approach for retrieving and setting translation information.
*
* @author OnTheGo Systems
*/
abstract class WPML_Translation_Element extends WPML_SP_User {
/** @var int */
protected $id;
/** @var stdClass */
private $languages_details;
/** @var array */
private $element_translations;
/** @var WPML_WP_Cache */
protected $wpml_cache;
/**
* WPML_Translation_Element constructor.
*
* @param int $id
* @param SitePress $sitepress
* @param WPML_WP_Cache $wpml_cache
*/
public function __construct( $id, SitePress $sitepress, WPML_WP_Cache $wpml_cache = null ) {
if ( ! is_numeric( $id ) || $id <= 0 ) {
throw new InvalidArgumentException( 'Argument ID must be numeric and greater than 0.' );
}
$this->id = (int) $id;
$this->wpml_cache = $wpml_cache ? $wpml_cache : new WPML_WP_Cache( WPML_ELEMENT_TRANSLATIONS_CACHE_GROUP );
parent::__construct( $sitepress );
}
public function get_id() {
return $this->id;
}
/**
* @return string|null
*/
public function get_source_language_code() {
$source_language_code = null;
if ( $this->get_language_details() ) {
$source_language_code = $this->get_language_details()->source_language_code;
}
return $source_language_code;
}
/**
* @return stdClass
* @throws \UnexpectedValueException
*/
protected function get_language_details() {
$this->init_language_details();
return $this->languages_details;
}
abstract function get_element_id();
abstract function get_wpml_element_type();
/**
* @return array
*/
private function get_element_translations() {
return $this->sitepress->get_element_translations( $this->get_trid(), $this->get_wpml_element_type() );
}
/**
* @param string $language_code
*
* @return WPML_Translation_Element|null
* @throws \InvalidArgumentException
*/
public function get_translation( $language_code ) {
if ( ! $language_code ) {
throw new InvalidArgumentException( 'Argument $language_code must be a non empty string.' );
}
$this->maybe_init_translations();
$translation = null;
if ( $this->element_translations && array_key_exists( $language_code, $this->element_translations ) ) {
$translation = $this->element_translations[ $language_code ];
}
return $translation;
}
/**
* @return WPML_Translation_Element[]
*/
public function get_translations() {
return $this->maybe_init_translations();
}
/**
* @return WPML_Translation_Element[]
*/
public function maybe_init_translations() {
if ( ! $this->element_translations ) {
$this->element_translations = array();
$translations = $this->get_element_translations();
foreach ( $translations as $language_code => $element_data ) {
if ( ! isset( $element_data->element_id ) ) {
continue;
}
try {
$this->element_translations[ $language_code ] = $this->get_new_instance( $element_data );
} catch ( Exception $e ) {
}
}
}
return $this->element_translations;
}
/**
* @return false|int
*/
public function get_trid() {
$trid = false;
if ( $this->get_language_details() ) {
$trid = $this->get_language_details()->trid;
}
return $trid;
}
/**
* @return string|WP_Error
*/
function get_wp_element_type() {
$element = $this->get_wp_object();
if ( is_wp_error( $element ) ) {
return $element;
}
if ( false === (bool) $element ) {
return new WP_Error( 1, 'Element does not exists.' );
}
return $this->get_type( $element );
}
/**
* @return mixed|WP_Error
*/
abstract function get_wp_object();
/**
* @param mixed $element
*
* @return string
*/
abstract function get_type( $element = null );
/**
* @param null|object $element_data null, or a standard object containing at least the `translation_id`, `language_code`, `element_id`, `source_language_code`, `element_type`, and `original` properties.
*
* @return WPML_Translation_Element
*/
abstract function get_new_instance( $element_data );
/**
* @return null|WPML_Translation_Element
*/
public function get_source_element() {
$this->maybe_init_translations();
$source_element = null;
$source_language_code = $this->get_source_language_code();
if ( $this->element_translations && $source_language_code && array_key_exists( $source_language_code, $this->element_translations ) ) {
$source_element = $this->element_translations[ $source_language_code ];
}
return $source_element;
}
/**
* Determines whether the current element language is the root source element.
*
* @return bool
*/
public function is_root_source() {
return null !== $this->get_source_language_code() && $this->get_source_language_code() === $this->get_language_code();
}
/**
* @return string|null
*/
public function get_language_code() {
$language_code = null;
if ( $this->get_language_details() ) {
$language_code = $this->get_language_details()->language_code;
}
return $language_code;
}
protected function init_language_details() {
if ( ! $this->languages_details ) {
$this->languages_details = $this->sitepress->get_element_language_details( $this->get_element_id(), $this->get_wpml_element_type() );
}
}
public function flush_cache() {
$this->languages_details = null;
$this->element_translations = null;
$this->wpml_cache->flush_group_cache();
}
/** @return bool */
public function is_in_default_language() {
return $this->get_language_code() === $this->sitepress->get_default_language();
}
abstract function is_translatable();
abstract function is_display_as_translated();
}