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