PluginProbe ʕ •ᴥ•ʔ
Pods – Custom Content Types and Fields / 3.2.2
Pods – Custom Content Types and Fields v3.2.2
trunk 1.14.8 2.7.31.3 2.8.23.3 2.9.19.3 3.0.10.3 3.1.4.1 3.2.0 3.2.1 3.2.1.1 3.2.2 3.2.4 3.2.5 3.2.6 3.2.7 3.2.7.1 3.2.8 3.2.8.1 3.2.8.2 3.3.0 3.3.1 3.3.2 3.3.3 3.3.4 3.3.5 3.3.6 3.3.7 3.3.8 3.3.9
pods / src / Pods / Integrations / WPML.php
pods / src / Pods / Integrations Last commit date
WPGraphQL 3 years ago Enfold.php 4 years ago Genesis.php 4 years ago Jetpack.php 4 years ago Polylang.php 2 years ago Service_Provider.php 3 years ago WPML.php 4 years ago YARPP.php 4 years ago
WPML.php
363 lines
1 <?php
2
3 namespace Pods\Integrations;
4
5 use Pods\Integration;
6
7 /**
8 * Class WPML
9 *
10 * @since 2.8.0
11 */
12 class WPML extends Integration {
13
14 /**
15 * @inheritdoc
16 */
17 protected $hooks = [
18 'action' => [
19 'wpml_language_has_switched' => [ 'wpml_language_has_switched' ],
20 ],
21 'filter' => [
22 'pods_get_current_language' => [ 'pods_get_current_language', 10, 2 ],
23 'pods_api_get_table_info' => [ 'pods_api_get_table_info', 10, 7 ],
24 'pods_data_traverse_recurse_ignore_aliases' => [ 'pods_data_traverse_recurse_ignore_aliases', 10 ],
25 'pods_pods_field_get_metadata_object_id' => [ 'pods_pods_field_get_metadata_object_id', 10, 4 ],
26 'pods_component_i18n_admin_data' => [ 'pods_component_i18n_admin_data' ],
27 'pods_component_i18n_admin_ui_fields' => [ 'pods_component_i18n_admin_ui_fields', 10, 2 ],
28 'pods_var_post_id' => [ 'pods_var_post_id' ],
29 ],
30 ];
31
32 /**
33 * @inheritDoc
34 */
35 public static function is_active() {
36 return defined( 'ICL_SITEPRESS_VERSION' ) || ! empty( $GLOBALS['sitepress'] );
37 }
38
39 /**
40 * Refresh language cache after WPML has switch language.
41 *
42 * @since 2.8.11
43 *
44 * @see \SitePress::switch_lang()
45 *
46 * @return void
47 */
48 public function wpml_language_has_switched() {
49 pods_i18n()->get_current_language( array( 'refresh' => true ) );
50 }
51
52 /**
53 * @since 2.8.2
54 *
55 * @param int $id
56 *
57 * @return mixed|void
58 */
59 public function pods_var_post_id( $id ) {
60 return apply_filters( 'wpml_object_id', $id, get_post_type( $id ), true );
61 }
62
63 /**
64 * @since 2.8.0
65 *
66 * @param array $ignore_aliases
67 *
68 * @return array
69 */
70 public function pods_data_traverse_recurse_ignore_aliases( $ignore_aliases ) {
71
72 $ignore_aliases[] = 'wpml_languages';
73
74 return $ignore_aliases;
75 }
76
77 /**
78 * Get the current language.
79 *
80 * @since 2.8.0
81 *
82 * @param string $current_language
83 * @param array $context
84 *
85 * @return string
86 */
87 public function pods_get_current_language( $current_language, $context ) {
88 // Get the global current language (if set).
89 $wpml_language = apply_filters( 'wpml_current_language', null );
90 $current_language = ( 'all' !== $wpml_language ) ? $wpml_language : '';
91
92 if ( ! is_admin() ) {
93 return $current_language;
94 }
95
96 $defaults = [
97 'is_admin' => is_admin(),
98 'is_ajax' => null,
99 'is_pods_ajax' => null,
100 'current_page' => '',
101 'current_object_type' => '',
102 'current_item_id' => '',
103 'current_item_type' => '',
104 ];
105
106 $context = wp_parse_args( $context, $defaults );
107
108 $object_type = $context['current_object_type'];
109 $item_type = $context['current_item_type'];
110
111 if ( ! $item_type ) {
112 return $current_language;
113 }
114
115 /**
116 * In WPML the current language is always set to default on an edit screen.
117 * We need to overwrite this when the current object is not-translatable to enable relationships with different languages.
118 */
119 switch ( $object_type ) {
120 case 'post':
121 if ( ! $this->is_translated_post_type( $item_type ) ) {
122 // Overwrite the current language to nothing if this is a NOT-translatable post_type.
123 $current_language = '';
124 }
125 break;
126
127 case 'term':
128 if ( ! $this->is_translated_taxonomy( $item_type ) ) {
129 // Overwrite the current language to nothing if this is a NOT-translatable taxonomy.
130 $current_language = '';
131 }
132 break;
133
134 case 'comment';
135 // @todo Get comment post parent??
136 //$current_language = '';
137 break;
138 }
139
140 return $current_language;
141 }
142
143 /**
144 * Support for WPML 'duplicated' translation handling.
145 *
146 * @param int $id
147 * @param string $metadata_type
148 * @param array $params
149 * @param array|\Pods $pod
150 *
151 * @return int
152 */
153 public function pods_pods_field_get_metadata_object_id( $id, $metadata_type, $params, $pod ) {
154 if ( ! did_action( 'wpml_loaded' ) ) {
155 return $id;
156 }
157 switch ( $metadata_type ) {
158 case 'post':
159 if ( $this->is_translated_post_type( $pod->pod_data['name'] ) ) {
160 $master_post_id = (int) apply_filters( 'wpml_master_post_from_duplicate', $id );
161
162 if ( $master_post_id ) {
163 $id = $master_post_id;
164 }
165 }
166 break;
167 }
168 return $id;
169 }
170
171 /**
172 * Filter table info data.
173 *
174 * @since 2.8.0
175 *
176 * @param array $info
177 * @param string $object_type
178 * @param string $object
179 * @param string $name
180 * @param array|\Pods $pod
181 * @param array $field
182 * @param \PodsAPI $pods_api
183 *
184 * @return array
185 */
186 public function pods_api_get_table_info( $info, $object_type, $object, $name, $pod, $field, $pods_api ) {
187 global $wpdb;
188
189 if ( ! apply_filters( 'wpml_setting', true, 'auto_adjust_ids' ) ) {
190 return $info;
191 }
192
193 // Get current language
194 $current_language = pods_i18n()->get_current_language();
195
196 if ( ! $current_language ) {
197 return $info;
198 }
199
200 $object_name = pods_sanitize( ( empty( $object ) ? $name : $object ) );
201
202 $db_prefix = $wpdb->get_blog_prefix();
203
204 $wpml_translations = false;
205
206 switch ( $object_type ) {
207
208 case 'post':
209 case 'post_type':
210 case 'media':
211 if ( $this->is_translated_post_type( $object_name ) ) {
212 $wpml_translations = "
213 LEFT JOIN `{$db_prefix}icl_translations` AS `wpml_translations`
214 ON `wpml_translations`.`element_id` = `t`.`ID`
215 AND `wpml_translations`.`element_type` = 'post_" . pods_sanitize( $object_name ) . "'
216 AND `wpml_translations`.`language_code` = '" . pods_sanitize( $current_language ) . "'
217 ";
218 }
219 break;
220
221 case 'taxonomy':
222 case 'term':
223 case 'nav_menu':
224 case 'post_format':
225 if ( $this->is_translated_taxonomy( $object_name ) ) {
226 $wpml_translations = "
227 LEFT JOIN `{$db_prefix}icl_translations` AS `wpml_translations`
228 ON `wpml_translations`.`element_id` = `tt`.`term_taxonomy_id`
229 AND `wpml_translations`.`element_type` = 'tax_" . pods_sanitize( $object_name ) . "'
230 AND `wpml_translations`.`language_code` = '" . pods_sanitize( $current_language ) . "'
231 ";
232 }
233 break;
234 }
235
236 if ( $wpml_translations ) {
237
238 $info['join']['wpml_translations'] = $wpml_translations;
239
240 $info['join']['wpml_languages'] = "
241 LEFT JOIN `{$db_prefix}icl_languages` AS `wpml_languages`
242 ON `wpml_languages`.`code` = `wpml_translations`.`language_code` AND `wpml_languages`.`active` = 1
243 ";
244
245 $info['where']['wpml_languages'] = "`wpml_languages`.`code` IS NOT NULL";
246 }
247
248 return $info;
249 }
250
251 /**
252 * @param array $data
253 *
254 * @return array
255 */
256 public function pods_component_i18n_admin_data( $data ) {
257
258 foreach ( $data as $lang => $field_data ) {
259 if ( in_array( $lang, $this->get_locales(), true ) ) {
260 $data[ $lang ]['wpml'] = true;
261 } else {
262 $data[ $lang ]['wpml'] = false;
263 }
264 }
265
266 return $data;
267 }
268
269 /**
270 * @param array $fields
271 * @param array $data
272 *
273 * @return array
274 */
275 public function pods_component_i18n_admin_ui_fields( $fields, $data ) {
276
277 $fields['manage']['wpml'] = array(
278 'label' => __( 'WPML', 'pods' ),
279 'type' => 'boolean',
280 );
281
282 return $fields;
283 }
284
285 /**
286 * Helper method for backwards compatibility.
287 *
288 * @since 2.8.0
289 *
290 * @param string $object_name
291 *
292 * @return false|mixed|void
293 */
294 public function is_translated_post_type( $object_name ) {
295 global $sitepress;
296 if ( has_filter( 'wpml_is_translated_post_type' ) ) {
297 return apply_filters( 'wpml_is_translated_post_type', false, $object_name );
298 } elseif ( is_callable( [ $sitepress, 'is_translated_post_type' ] ) ) {
299 return $sitepress->is_translated_post_type( $object_name );
300 }
301 return false;
302 }
303
304 /**
305 * Helper method for backwards compatibility.
306 *
307 * @since 2.8.0
308 *
309 * @param string $object_name
310 *
311 * @return false|mixed|void
312 */
313 public function is_translated_taxonomy( $object_name ) {
314 global $sitepress;
315 if ( has_filter( 'wpml_is_translated_taxonomy' ) ) {
316 return apply_filters( 'wpml_is_translated_taxonomy', false, $object_name );
317 } elseif ( is_callable( [ $sitepress, 'is_translated_taxonomy' ] ) ) {
318 return $sitepress->is_translated_taxonomy( $object_name );
319 }
320 return false;
321 }
322
323 /**
324 * @return array
325 */
326 public function get_language( $locale ) {
327 $languages = $this->get_languages();
328 $language = null;
329 if ( ! empty( $languages ) ) {
330 foreach ( $languages as $lang => $lang_data ) {
331 if ( isset( $lang_data['default_locale'] ) && $locale === $lang_data['default_locale'] ) {
332 $language = $lang_data;
333 break;
334 }
335 }
336 }
337 return $language;
338 }
339
340 /**
341 * @return string[]
342 */
343 public function get_locales() {
344 $languages = $this->get_languages();
345 $locales = [];
346 if ( ! empty( $languages ) ) {
347 foreach ( $languages as $lang => $lang_data ) {
348 if ( isset( $lang_data['default_locale'] ) ) {
349 $locales[] = $lang_data['default_locale'];;
350 }
351 }
352 }
353 return $locales;
354 }
355
356 /**
357 * @return array
358 */
359 public function get_languages() {
360 return apply_filters( 'wpml_active_languages', array() );
361 }
362 }
363