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