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