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 / Polylang.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
Polylang.php
477 lines
1 <?php
2
3 namespace Pods\Integrations;
4
5 use Pods\Integration;
6
7 /**
8 * Class Polylang
9 *
10 * @since 2.8.0
11 */
12 class Polylang extends Integration {
13
14 /**
15 * @inheritdoc
16 */
17 protected $hooks = [
18 'action' => [
19 'pods_meta_init' => [ 'pods_meta_init' ],
20 'pods_form_ui_field_pick_related_objects_other' => [ 'pods_pick_field_add_related_objects' ],
21 ],
22 'filter' => [
23 'pods_get_current_language' => [ 'pods_get_current_language', 10, 2 ],
24 'pods_api_get_table_info' => [ 'pods_api_get_table_info', 10, 7 ],
25 'pods_data_traverse_recurse_ignore_aliases' => [ 'pods_data_traverse_recurse_ignore_aliases', 10 ],
26 'pods_meta_ignored_types' => [ 'pods_meta_ignored_types' ],
27 'pods_component_i18n_admin_data' => [ 'pods_component_i18n_admin_data' ],
28 'pods_component_i18n_admin_ui_fields' => [ 'pods_component_i18n_admin_ui_fields', 10, 2 ],
29 'pods_var_post_id' => [ 'pods_var_post_id' ],
30 'pll_get_post_types' => [ 'pll_get_post_types', 10, 2 ],
31 ],
32 ];
33
34 /**
35 * @inheritDoc
36 */
37 public static function is_active() {
38 return function_exists( 'PLL' ) || ! empty( $GLOBALS['polylang'] );
39 }
40
41 /**
42 * @since 2.8.2
43 *
44 * @param int $id
45 *
46 * @return mixed|void
47 */
48 public function pods_var_post_id( $id ) {
49 $polylang_id = pll_get_post( $id );
50 if ( ! empty( $polylang_id ) ) {
51 $id = $polylang_id;
52 }
53 return $id;
54 }
55
56 /**
57 * Add Pods templates to possible i18n enabled post-types (polylang settings).
58 *
59 * @since 2.7.0
60 * @since 2.8.0 Moved from PodsI18n class.
61 *
62 * @param array $post_types
63 * @param bool $is_settings
64 *
65 * @return array mixed
66 */
67 public function pll_get_post_types( $post_types, $is_settings = false ) {
68
69 if ( $is_settings ) {
70 $post_types['_pods_template'] = '_pods_template';
71 }
72
73 return $post_types;
74 }
75
76 /**
77 * @since 2.8.0
78 *
79 * @param \PodsMeta $pods_meta
80 */
81 public function pods_meta_init( $pods_meta ) {
82
83 if ( function_exists( 'pll_current_language' ) ) {
84 add_action( 'init', array( $pods_meta, 'cache_pods' ), 101, 0 );
85 }
86 }
87
88 /**
89 * @since 2.8.8
90 *
91 * @param array[] $ignored_types
92 *
93 * @return mixed
94 */
95 public function pods_meta_ignored_types( $ignored_types ) {
96
97 // Add Polylang related taxonomies to the ignored types for PodsMeta.
98 $ignored_types['taxonomy']['language'] = true;
99 $ignored_types['taxonomy']['term_language'] = true;
100 $ignored_types['taxonomy']['post_translations'] = true;
101 $ignored_types['taxonomy']['term_translations'] = true;
102
103 return $ignored_types;
104 }
105
106 /**
107 * Add the Polylang language taxonomy to be used in relationships.
108 *
109 * @since 2.8.21
110 */
111 public function pods_pick_field_add_related_objects() {
112 $taxonomy = get_taxonomy( 'language' );
113
114 \PodsField_Pick::$related_objects[ 'taxonomy-language' ] = array(
115 'label' => $taxonomy->label . ' (' . $taxonomy->name . ')',
116 'group' => __( 'Polylang', 'pods' ),
117 'bidirectional' => false,
118 );
119 }
120
121 /**
122 * @since 2.8.0
123 *
124 * @param array $ignore_aliases
125 *
126 * @return array
127 */
128 public function pods_data_traverse_recurse_ignore_aliases( $ignore_aliases ) {
129
130 $ignore_aliases[] = 'polylang_languages';
131
132 return $ignore_aliases;
133 }
134
135 /**
136 * Get the current language.
137 *
138 * @since 2.8.0
139 *
140 * @param string $current_language
141 * @param array $context
142 *
143 * @return string
144 */
145 public function pods_get_current_language( $current_language, $context ) {
146
147 if ( ! is_admin() ) {
148 // Get the global current language (if set).
149 return pll_current_language( 'slug' );
150 }
151
152 $defaults = [
153 'is_admin' => is_admin(),
154 'is_ajax' => null,
155 'is_pods_ajax' => null,
156 'current_page' => '',
157 'current_object_type' => '',
158 'current_item_id' => '',
159 'current_item_type' => '',
160 ];
161
162 $context = wp_parse_args( $context, $defaults );
163
164 $object_type = $context['current_object_type'];
165 $item_id = $context['current_item_id'];
166 $item_type = $context['current_item_type'];
167
168 /**
169 * Get the current user's preferred language.
170 * This is a user meta setting that will overwrite the language returned from pll_current_language().
171 *
172 * @see \PLL_Admin_Base::init_user() (polylang/admin/admin-base.php)
173 */
174 $current_language = get_user_meta( get_current_user_id(), 'pll_filter_content', true );
175
176 if ( ! $item_type ) {
177 return $current_language;
178 }
179
180 /**
181 * In polylang the preferred language could be anything.
182 */
183 switch ( $object_type ) {
184 case 'post':
185 if ( $this->is_translated_post_type( $item_type ) ) {
186
187 /**
188 * Polylang (1.5.4+).
189 * We only want the related objects if they are not translatable OR the same language as the current object.
190 */
191 if ( $item_id && function_exists( 'pll_get_post_language' ) ) {
192 // Overwrite the current language if this is a translatable post_type.
193 $current_language = pll_get_post_language( $item_id );
194 }
195
196 /**
197 * Polylang (1.0.1+).
198 * When we're adding a new object and language is set we only want the related objects if they are not translatable OR the same language.
199 */
200 $current_language = pods_v( 'new_lang', 'request', $current_language );
201 }
202 break;
203
204 case 'term':
205 if ( $this->is_translated_taxonomy( $item_type ) ) {
206
207 /**
208 * Polylang (1.5.4+).
209 * We only want the related objects if they are not translatable OR the same language as the current object.
210 */
211 if ( $item_id && function_exists( 'pll_get_term_language' ) ) {
212 // Overwrite the current language if this is a translatable taxonomy
213 $current_language = pll_get_term_language( $item_id );
214 }
215
216 /**
217 * Polylang (1.0.1+).
218 * When we're adding a new object and language is set we only want the related objects if they are not translatable OR the same language.
219 */
220 $current_language = pods_v( 'new_lang', 'request', $current_language );
221 }
222 break;
223 }
224
225 return $current_language;
226 }
227
228 /**
229 * Filter table info data.
230 *
231 * @since 2.8.0
232 *
233 * @param array $info
234 * @param string $object_type
235 * @param string $object
236 * @param string $name
237 * @param array|\Pods $pod
238 * @param array $field
239 * @param \PodsAPI $pods_api
240 *
241 * @return array
242 */
243 public function pods_api_get_table_info( $info, $object_type, $object, $name, $pod, $field, $pods_api ) {
244 global $wpdb;
245 $object_name = pods_sanitize( ( empty( $object ) ? $name : $object ) );
246
247 // Get current language data
248 $lang_data = $this->get_language_data();
249
250 if ( ! $lang_data ) {
251 return $info;
252 }
253
254 $current_language_tt_id = 0;
255 $current_language_tl_tt_id = 0;
256
257 if ( ! empty( $lang_data['tt_id'] ) ) {
258 $current_language_tt_id = $lang_data['tt_id'];
259 }
260 if ( ! empty( $lang_data['tl_tt_id'] ) ) {
261 $current_language_tl_tt_id = $lang_data['tl_tt_id'];
262 }
263
264 switch ( $object_type ) {
265
266 case 'post':
267 case 'post_type':
268 case 'media':
269 if ( $current_language_tt_id && $this->is_translated_post_type( $object_name ) ) {
270 $info['join']['polylang_languages'] = "
271 LEFT JOIN `{$wpdb->term_relationships}` AS `polylang_languages`
272 ON `polylang_languages`.`object_id` = `t`.`ID`
273 AND `polylang_languages`.`term_taxonomy_id` = {$current_language_tt_id}
274 ";
275
276 $info['where']['polylang_languages'] = "`polylang_languages`.`object_id` IS NOT NULL";
277 }
278 break;
279
280 case 'taxonomy':
281 case 'term':
282 case 'nav_menu':
283 case 'post_format':
284 if ( $current_language_tl_tt_id && $this->is_translated_taxonomy( $object_name ) ) {
285 $info['join']['polylang_languages'] = "
286 LEFT JOIN `{$wpdb->term_relationships}` AS `polylang_languages`
287 ON `polylang_languages`.`object_id` = `t`.`term_id`
288 AND `polylang_languages`.`term_taxonomy_id` = {$current_language_tl_tt_id}
289 ";
290
291 $info['where']['polylang_languages'] = "`polylang_languages`.`object_id` IS NOT NULL";
292 }
293 break;
294 }
295
296 return $info;
297 }
298
299 /**
300 * @param array $data
301 *
302 * @return array
303 */
304 public function pods_component_i18n_admin_data( $data ) {
305
306 foreach ( $data as $lang => $field_data ) {
307 if ( in_array( $lang, $this->get_locales(), true ) ) {
308 $data[ $lang ]['polylang'] = true;
309 } else {
310 $data[ $lang ]['polylang'] = false;
311 }
312 }
313
314 return $data;
315 }
316
317 /**
318 * @param array $fields
319 * @param array $data
320 *
321 * @return array
322 */
323 public function pods_component_i18n_admin_ui_fields( $fields, $data ) {
324
325 $fields['manage']['polylang'] = array(
326 'label' => __( 'Polylang', 'pods' ),
327 'type' => 'boolean',
328 );
329
330 return $fields;
331 }
332
333 /**
334 * Helper method for backwards compatibility.
335 *
336 * @since 2.8.0
337 *
338 * @param string $object_name
339 *
340 * @return false|mixed|void
341 */
342 public function is_translated_post_type( $object_name ) {
343 if ( function_exists( 'pll_is_translated_post_type' ) ) {
344 return pll_is_translated_post_type( $object_name );
345 }
346 return false;
347 }
348
349 /**
350 * Helper method for backwards compatibility.
351 *
352 * @since 2.8.0
353 *
354 * @param string $object_name
355 *
356 * @return false|mixed|void
357 */
358 public function is_translated_taxonomy( $object_name ) {
359 if ( function_exists( 'pll_is_translated_taxonomy' ) ) {
360 return pll_is_translated_taxonomy( $object_name );
361 }
362 return false;
363 }
364
365 /**
366 * Get the language taxonomy object for the current language.
367 *
368 * @since 2.8.0
369 *
370 * @param string $locale
371 *
372 * @return array
373 */
374 public function get_language_data( $locale = null ) {
375 static $lang_data = [];
376
377 if ( ! $locale ) {
378 $locale = pods_i18n()->get_current_language();
379 }
380
381 if ( isset( $lang_data[ $locale ] ) ) {
382 return $lang_data[ $locale ];
383 }
384
385 if ( ! $locale ) {
386 return null;
387 }
388
389 // We need to return language data
390 $lang_data = array(
391 'language' => $locale,
392 't_id' => 0,
393 'tt_id' => 0,
394 'tl_t_id' => 0,
395 'tl_tt_id' => 0,
396 'term' => null,
397 );
398
399 $language = $this->get_language( $locale );
400
401 // If the language object exists, add it!
402 if ( $language && ! empty( $language->term_id ) ) {
403
404 $lang_data['term'] = $language;
405 $lang_data['t_id'] = (int) $language->term_id;
406
407 if ( method_exists( $language, 'get_tax_prop' ) ) {
408 // Since Polylang 3.4
409 $lang_data['tt_id'] = (int) $language->get_tax_prop( 'language', 'term_taxonomy_id' );
410 $lang_data['tl_t_id'] = (int) $language->get_tax_prop( 'term_language', 'term_id' );
411 $lang_data['tl_tt_id'] = (int) $language->get_tax_prop( 'term_language', 'term_taxonomy_id' );
412 } else {
413 // Pre Polylang 3.4
414 $lang_data['tt_id'] = (int) $language->term_taxonomy_id;
415 $lang_data['tl_t_id'] = (int) $language->tl_term_id;
416 $lang_data['tl_tt_id'] = (int) $language->tl_term_taxonomy_id;
417 }
418 }
419
420 $lang_data[ $locale ] = $lang_data;
421
422 return $lang_data[ $locale ];
423 }
424
425 /**
426 * @param $locale
427 *
428 * @return false|\PLL_Language
429 */
430 public function get_language( $locale ) {
431 $language = false;
432
433 if ( ! $locale ) {
434 $locale = pods_i18n()->get_current_language();
435 }
436
437 // Get the language term object.
438 if ( function_exists( 'PLL' ) && isset( PLL()->model ) && method_exists( PLL()->model, 'get_language' ) ) {
439 // Polylang 1.8 and newer.
440 $language = PLL()->model->get_language( $locale );
441 } else {
442 global $polylang;
443 if ( is_object( $polylang ) && isset( $polylang->model ) && method_exists( $polylang->model, 'get_language' ) ) {
444 // Polylang 1.2 - 1.7.x
445 $language = $polylang->model->get_language( $locale );
446 } elseif ( is_object( $polylang ) && method_exists( $polylang, 'get_language' ) ) {
447 // Polylang 1.1.x and older.
448 $language = $polylang->get_language( $locale );
449 }
450 }
451
452 return $language;
453 }
454
455 /**
456 * @return string[]
457 */
458 public function get_locales() {
459 $locales = [];
460 if ( function_exists( 'pll_languages_list' ) ) {
461 $locales = pll_languages_list( array( 'fields' => 'locale' ) );
462 }
463 return $locales;
464 }
465
466 /**
467 * @return array
468 */
469 public function get_languages() {
470 $languages = [];
471 if ( function_exists( 'pll_languages_list' ) ) {
472 $languages = pll_languages_list( array( 'fields' => null ) );
473 }
474 return $languages;
475 }
476 }
477