PluginProbe ʕ •ᴥ•ʔ
Pods – Custom Content Types and Fields / 2.7.31.4
Pods – Custom Content Types and Fields v2.7.31.4
2.7.31.4 2.8.23.5 2.9.19.5 3.0.10.5 3.1.4.3 3.2.8.4 3.3.9.2 2.8.23.4 2.9.19.4 3.0.10.4 3.1.4.2 3.2.8.3 3.3.9.1 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 / components / I18n / I18n.php
pods / components / I18n Last commit date
I18n-polylang.php 1 week ago I18n-wpml.php 1 week ago I18n.php 1 week ago pods-admin-i18n.js 1 week ago
I18n.php
1002 lines
1 <?php
2 /**
3 * Name: Translate Pods Admin
4 *
5 * Menu Name: Translate Pods
6 *
7 * Description: Allow UI of Pods and fields to be translated.
8 *
9 * Version: 0.2
10 *
11 * Category: I18n
12 *
13 * Class: Pods_Component_I18n
14 *
15 * @package Pods\Components
16 * @subpackage i18n
17 */
18
19 ! defined( 'ABSPATH' ) and die();
20
21 if ( class_exists( 'Pods_Component_I18n' ) ) {
22 return;
23 }
24
25 /**
26 * Class Pods_Component_I18n
27 */
28 class Pods_Component_I18n extends PodsComponent {
29
30 public $settings = array();
31 public $locale = null;
32 public $languages = array();
33 public $languages_available = array();
34 public $languages_translated = array();
35 public $cur_pod = null;
36 public $option_key = 'pods_component_i18n_settings';
37 public $admin_page = 'pods-component-translate-pods-admin';
38 public $capability = 'pods_i18n_activate_lanuages';
39 public $nonce = 'pods_i18n_activate_lanuages';
40
41 public $translatable_fields = array(
42 'label',
43 'description',
44 'placeholder',
45 'menu_name',
46 'name_admin_bar',
47 'pick_select_text',
48 'file_add_button',
49 'file_modal_title',
50 'file_modal_add_button',
51 );
52
53 /**
54 * {@inheritdoc}
55 */
56 public function init() {
57
58 $this->settings = get_option( $this->option_key, array() );
59
60 // Polylang
61 if ( function_exists( 'PLL' ) && file_exists( plugin_dir_path( __FILE__ ) . 'I18n-polylang.php' ) ) {
62 include_once plugin_dir_path( __FILE__ ) . 'I18n-polylang.php';
63 }
64 // WPML
65 elseif ( did_action( 'wpml_loaded' ) && file_exists( plugin_dir_path( __FILE__ ) . 'I18n-wpml.php' ) ) {
66 include_once plugin_dir_path( __FILE__ ) . 'I18n-wpml.php';
67 }
68
69 $active = false;
70 // Are there active languages?
71 if ( ! empty( $this->settings['enabled_languages'] ) ) {
72 $this->languages = $this->settings['enabled_languages'];
73
74 if ( function_exists( 'get_user_locale' ) ) {
75 // WP 4.7+
76 $this->locale = get_user_locale();
77 } else {
78 $this->locale = get_locale();
79 }
80
81 $active = true;
82 }
83
84 $is_component_page = false;
85 $is_pods_edit_page = false;
86
87 if ( is_admin() && isset( $_GET['page'] ) ) {
88
89 $page = $_GET['page'];
90
91 // Is the current page the admin page of this component or a Pods edit page?
92 if ( $this->admin_page === $page ) {
93 $is_component_page = true;
94 } elseif ( 'pods' === $page ) {
95 $is_pods_edit_page = true;
96 }
97
98 if ( $is_component_page || ( $is_pods_edit_page && $active ) ) {
99 add_action( 'admin_enqueue_scripts', array( $this, 'admin_assets' ) );
100 }
101 }
102
103 if ( $is_component_page ) {
104 // Do save action here because otherwise the loading of post_types get done first and labels aren't translated
105 if ( pods_is_admin( $this->capability ) && isset( $_POST['_nonce_i18n'] ) && wp_verify_nonce( $_POST['_nonce_i18n'], $this->nonce ) ) {
106 $this->admin_save();
107 }
108 }
109
110 if ( $active ) {
111 // WP Object filters (post_type and taxonomy)
112 add_filter( 'pods_register_post_type', array( $this, 'pods_register_wp_object_i18n' ), 10, 2 );
113 add_filter( 'pods_register_taxonomy', array( $this, 'pods_register_wp_object_i18n' ), 10, 2 );
114
115 // ACT's
116 add_filter(
117 'pods_advanced_content_type_pod_data',
118 array( $this, 'pods_filter_object_strings_i18n' ),
119 10,
120 2
121 );
122
123 // Setting pages
124 add_filter( 'pods_admin_menu_page_title', array( $this, 'admin_menu_page_title_i18n' ), 10, 2 );
125 add_filter( 'pods_admin_menu_label', array( $this, 'admin_menu_label_i18n' ), 10, 2 );
126
127 // Default filters for all fields
128 add_filter( 'pods_form_ui_label_text', array( $this, 'fields_ui_label_text_i18n' ), 10, 4 );
129 add_filter( 'pods_form_ui_comment_text', array( $this, 'fields_ui_comment_text_i18n' ), 10, 3 );
130
131 foreach ( pods_form()->field_types() as $type => $data ) {
132 add_filter(
133 'pods_form_ui_field_' . $type . '_options',
134 array( $this, 'form_ui_field_options_i18n' ),
135 10,
136 5
137 );
138 }
139
140 // Field specific
141 // add_filter( 'pods_field_pick_data', array( $this, 'field_pick_data_i18n' ), 10, 6 );
142 if ( $is_pods_edit_page ) {
143
144 $pod = null;
145 // Get the pod if available
146 if ( isset( $_GET['id'] ) && is_numeric( $_GET['id'] ) ) {
147 $pod = pods_api()->load_pod( array( 'id' => $_GET['id'] ) );
148 // Append options to root array for pods_v function to work
149 foreach ( $pod['options'] as $_option => $_value ) {
150 $pod[ $_option ] = $_value;
151 }
152 }
153 $this->cur_pod = $pod;
154
155 // Add option tab for post types
156 // add_filter( 'pods_admin_setup_edit_tabs_post_type', array( $this, 'pod_tab' ), 11, 3 );
157 // Add the same tab for taxonomies
158 // add_filter( 'pods_admin_setup_edit_tabs_taxonomy', array( $this, 'pod_tab' ), 11, 3 );
159 // Add options to the new tab
160 // add_filter( 'pods_admin_setup_edit_options', array( $this, 'pod_options' ), 12, 2 );
161 // Add options metabox to the pod edit screens
162 add_action( 'pods_add_meta_boxes', array( $this, 'admin_meta_box' ) );
163
164 // Add the i18n input fields based on existing fields
165 add_filter( 'pods_form_ui_field_text', array( $this, 'add_i18n_inputs' ), 10, 6 );
166 }//end if
167 }//end if
168 }
169
170 /**
171 * Load assets for this component
172 *
173 * @since 0.1.0
174 */
175 public function admin_assets() {
176
177 wp_enqueue_script(
178 'pods-admin-i18n',
179 PODS_URL . 'components/I18n/pods-admin-i18n.js',
180 array(
181 'jquery',
182 'pods-i18n',
183 ),
184 '1.0',
185 true
186 );
187 $localize_script = array();
188 if ( ! empty( $this->languages ) ) {
189 foreach ( $this->languages as $lang => $lang_data ) {
190 $lang_label = $this->create_lang_label( $lang_data );
191 if ( ! empty( $lang_label ) ) {
192 $lang_label = $lang . ' (' . $lang_label . ')';
193 } else {
194 $lang_label = $lang;
195 }
196 $localize_script[ $lang ] = $lang_label;
197 }
198 }
199 wp_localize_script( 'pods-admin-i18n', 'pods_admin_i18n_strings', $localize_script );
200
201 // Add strings to the i18n js object
202 add_filter( 'pods_localized_strings', array( $this, 'localize_assets' ) );
203 }
204
205 /**
206 * Localize the assets
207 *
208 * @param array $str Existing strings
209 *
210 * @return array
211 */
212 public function localize_assets( $str ) {
213
214 $str['Add translation'] = __( 'Add translation', 'pods' );
215 $str['Toggle translations'] = __( 'Toggle translations', 'pods' );
216 $str['Show translations'] = __( 'Show translations', 'pods' );
217 $str['Hide translations'] = __( 'Hide translations', 'pods' );
218 $str['Select'] = __( 'Select', 'pods' );
219
220 return $str;
221 }
222
223 /**
224 * Check is a field name is set for translation
225 *
226 * @since 0.1.0
227 *
228 * @param string $name
229 *
230 * @return bool
231 */
232 public function is_translatable_field( $name ) {
233
234 $translatable_fields = $this->get_translatable_fields();
235
236 // All fields that start with "label"
237 if ( strpos( $name, 'label' ) === 0 ) {
238 return true;
239 }
240 // All translatable fields
241 if ( in_array( $name, $translatable_fields, true ) ) {
242 return true;
243 }
244 // Custom fields data, the name must begin with field_data[
245 if ( strpos( $name, 'field_data[' ) === 0 ) {
246 $name = str_replace( 'field_data[', '', $name );
247 $name = rtrim( $name, ']' );
248 $name = explode( '][', $name );
249 $name = end( $name );
250 // All translatable fields from field_data[ (int) ][ $name ]
251 if ( in_array( $name, $translatable_fields, true ) ) {
252 return true;
253 }
254 }
255
256 return false;
257 }
258
259 /**
260 * Get a translated option for a field key (if available)
261 *
262 * @since 0.1.0
263 *
264 * @param string $current Current value
265 * @param string $key The key / opion name to search for
266 * @param array $data Pod data (can also be an options array of a pod or field)
267 *
268 * @return string
269 */
270 public function get_value_translation( $current, $key, $data ) {
271
272 $locale = $this->locale;
273 // Validate locale and pod
274 if ( is_array( $data ) && array_key_exists( $locale, $this->languages ) && $this->obj_is_language_enabled( $locale, $data ) ) {
275 // Add option keys to $data array
276 if ( ! empty( $data['options'] ) ) {
277 $data = array_merge( $data, $data['options'] );
278 }
279 // Check if the i18n option exists and isn't empty
280 if ( ! empty( $data[ $key . '_' . $locale ] ) ) {
281 return (string) $data[ $key . '_' . $locale ];
282 }
283 }
284
285 return $current;
286 }
287
288 /**
289 * Page title for setting pages
290 *
291 * @since 0.1.0
292 * @see PodsAdmin.php >> admin_menu()
293 * @see PodsAdmin.php >> admin_content_settings()
294 *
295 * @param string $page_title Current page title
296 * @param array $pod Pod data
297 *
298 * @return string
299 */
300 public function admin_menu_page_title_i18n( $page_title, $pod ) {
301
302 return (string) $this->get_value_translation( $page_title, 'label', $pod );
303 }
304
305 /**
306 * Menu title for setting pages
307 *
308 * @since 0.1.0
309 * @see PodsAdmin.php >> admin_menu()
310 *
311 * @param string $menu_label Current menu label
312 * @param array $pod Pod data
313 *
314 * @return string
315 */
316 public function admin_menu_label_i18n( $menu_label, $pod ) {
317
318 return (string) $this->get_value_translation( $menu_label, 'menu_name', $pod );
319 }
320
321 /**
322 * Returns the translated label if available
323 *
324 * @since 0.1.0
325 * @see PodsForm.php >> 'pods_form_ui_label_text' (filter)
326 *
327 * @param string $label The default label
328 * @param string $name The field name
329 * @param string $help The help text
330 * @param array $options The field options
331 *
332 * @return string
333 */
334 public function fields_ui_label_text_i18n( $label, $name, $help, $options ) {
335
336 return (string) $this->get_value_translation( $label, 'label', $options );
337 }
338
339 /**
340 * Returns the translated description if available
341 *
342 * @since 0.1.0
343 * @see PodsForm.php >> 'pods_form_ui_comment_text' (filter)
344 *
345 * @param string $message The default description
346 * @param string $name The field name
347 * @param array $options The field options
348 *
349 * @return string
350 */
351 public function fields_ui_comment_text_i18n( $message, $name, $options ) {
352
353 return (string) $this->get_value_translation( $message, 'description', $options );
354 }
355
356 /**
357 * Replaces the default selected text with a translation if available
358 *
359 * @since 0.1.0
360 * @see pick.php >> 'pods_field_pick_data' (filter)
361 *
362 * @param array $data The default data of the field
363 * @param string $name The field name
364 * @param string $value The field value
365 * @param array $options The field options
366 * @param array $pod The Pod
367 * @param int $id The field ID
368 *
369 * @return array
370 */
371 public function field_pick_data_i18n( $data, $name, $value, $options, $pod, $id ) {
372
373 if ( isset( $data[''] ) && isset( $options['pick_select_text'] ) ) {
374 $locale = $this->locale;
375 if ( isset( $options[ 'pick_select_text_' . $locale ] ) && array_key_exists( $locale, $this->languages ) && $this->obj_is_language_enabled( $locale, $pod ) ) {
376 $data[''] = $options[ 'pick_select_text_' . $locale ];
377 }
378 }
379
380 return $data;
381 }
382
383 /**
384 * Replaces the default values with a translation if available
385 *
386 * @since 0.1.0
387 * @see PodsForm.php >> 'pods_form_ui_field_' . $type . '_options' (filter)
388 *
389 * @param array $options The field options
390 * @param string $name The field name
391 * @param string $value The field value
392 * @param array $pod The Pod
393 * @param int $id The field ID
394 *
395 * @return array
396 */
397 public function form_ui_field_options_i18n( $options, $name, $value, $pod, $id ) {
398
399 foreach ( $this->get_translatable_fields() as $field ) {
400 $locale = $this->locale;
401 if ( isset( $options[ $field . '_' . $locale ] ) && array_key_exists( $locale, $this->languages ) && $this->obj_is_language_enabled( $locale, $pod ) ) {
402 $options[ $field ] = $options[ $field . '_' . $locale ];
403 }
404 }
405
406 return $options;
407 }
408
409 /**
410 * Filter hook function to overwrite the labels and description with translations (if available)
411 *
412 * @since 0.1.0
413 * @see PodsInit.php >> setup_content_types()
414 *
415 * @param array $options The array of object options
416 * @param string $object The object type name/slug
417 *
418 * @return array
419 */
420 public function pods_register_wp_object_i18n( $options, $object ) {
421
422 $locale = $this->locale;
423
424 $pod = pods_api()->load_pod( $object );
425
426 if ( ! $this->obj_is_language_enabled( $locale, $pod ) ) {
427 return $options;
428 }
429
430 // Load the pod
431 foreach ( $pod['options'] as $_option => $_value ) {
432 $pod[ $_option ] = $_value;
433 }
434
435 $labels = array(
436 // Default
437 'name' => 'label', // Different.
438 'singular_name' => 'label_singular', // Different.
439 'menu_name' => 'menu_name',
440 'add_new_item' => 'label_add_new_item',
441 'edit_item' => 'label_edit_item',
442 'view_item' => 'label_view_item',
443 'all_items' => 'label_all_items',
444 'search_items' => 'label_search_items',
445 'parent_item_colon' => 'label_parent_item_colon',
446 'not_found' => 'label_not_found',
447 'items_list_navigation' => 'label_items_list_navigation',
448 'items_list' => 'label_items_list',
449
450 // Post Types
451 'name_admin_bar' => 'name_admin_bar',
452 'add_new' => 'label_add_new',
453 'new_item' => 'label_new_item',
454 'edit' => 'label_edit',
455 'view' => 'label_view',
456 'view_items' => 'label_view_items',
457 'parent' => 'label_parent',
458 'not_found_in_trash' => 'label_not_found_in_trash',
459 'archives' => 'label_archives',
460 'attributes' => 'label_attributes',
461 'insert_into_item' => 'label_insert_into_item',
462 'uploaded_to_this_item' => 'label_uploaded_to_this_item',
463 'featured_image' => 'label_featured_image',
464 'set_featured_image' => 'label_set_featured_image',
465 'remove_featured_image' => 'label_remove_featured_image',
466 'use_featured_image' => 'label_use_featured_image',
467 'filter_items_list' => 'label_filter_items_list',
468 // Block Editor (WP 5.0+)
469 'item_published' => 'label_item_published',
470 'item_published_privately' => 'label_item_published_privately',
471 'item_reverted_to_draft' => 'label_item_reverted_to_draft',
472 'item_scheduled' => 'label_item_scheduled',
473 'item_updated' => 'label_item_updated',
474 'filter_by_date' => 'label_filter_by_date', // WP 5.7
475
476 // Taxonomies
477 'update_item' => 'label_update_item',
478 'popular_items' => 'label_popular_items',
479 'parent_item' => 'label_parent_item',
480 'new_item_name' => 'label_new_item_name',
481 'separate_items_with_commas' => 'label_separate_items_with_commas',
482 'add_or_remove_items' => 'label_add_or_remove_items',
483 'choose_from_most_used' => 'label_choose_from_the_most_used', // Different.
484 'no_terms' => 'label_no_terms',
485 'filter_by_item' => 'label_filter_by_item', // WP 5.7
486 );
487
488 if ( ! isset( $options['labels'] ) || ! is_array( $options['labels'] ) ) {
489 $options['labels'] = array();
490 } else {
491 // Try to find new labels.
492 foreach ( $options['labels'] as $key => $val ) {
493 if ( ! isset( $labels[ $key ] ) ) {
494 $labels[ $key ] = 'label_' . $key;
495 }
496 }
497 }
498
499 foreach ( $labels as $key => $pods_key ) {
500 $label = pods_v( $pods_key . '_' . $locale, $pod, '', true );
501 if ( $label ) {
502 $options['labels'][ $key ] = $label;
503 }
504 }
505
506 return $options;
507 }
508
509 /**
510 * Filter hook function to overwrite the labels and description with translations (if available)
511 *
512 * @since 0.1.0
513 * @see PodsInit.php >> admin_menu()
514 *
515 * @param array $options The array of object options
516 * @param string $object The object type name/slug
517 *
518 * @return array
519 */
520 public function pods_filter_object_strings_i18n( $options, $object ) {
521
522 /**
523 * @todo allow labels to be set even if the default language isn't
524 *
525 * - Find all keys that end with the current locale
526 * - Assign them to the keys without that locale
527 */
528
529 foreach ( $options as $key => $option ) {
530 if ( is_string( $option ) && $this->is_translatable_field( $key ) ) {
531 $options[ $key ] = $this->get_value_translation( $option, $key, $options );
532 }
533 }
534
535 if ( ! empty( $options['options'] ) ) {
536 foreach ( $options['options'] as $key => $option ) {
537 if ( is_string( $option ) && $this->is_translatable_field( $key ) ) {
538 $options['options'][ $key ] = $this->get_value_translation( $option, $key, $options['options'] );
539 }
540 }
541 }
542
543 return $options;
544 }
545
546 /**
547 * Save component settings
548 *
549 * @since 0.1.0
550 */
551 public function admin_save() {
552
553 $this->languages_available = get_available_languages();
554
555 /**
556 * format: array( language, version, updated, english_name, native_name, package, iso, strings )
557 */
558 require_once ABSPATH . 'wp-admin/includes/translation-install.php';
559 $this->languages_translated = wp_get_available_translations();
560
561 $new_languages = array();
562
563 if ( isset( $_POST['pods_i18n_enabled_languages'] ) && is_array( $_POST['pods_i18n_enabled_languages'] ) ) {
564 foreach ( $_POST['pods_i18n_enabled_languages'] as $locale ) {
565 $locale = sanitize_text_field( $locale );
566
567 if ( in_array( $locale, $this->languages_available, true ) ) {
568 $new_languages[ $locale ] = array();
569
570 if ( isset( $this->languages_translated[ $locale ]['language'] ) ) {
571 $new_languages[ $locale ]['language'] = $this->languages_translated[ $locale ]['language'];
572 }
573
574 if ( isset( $this->languages_translated[ $locale ]['english_name'] ) ) {
575 $new_languages[ $locale ]['english_name'] = $this->languages_translated[ $locale ]['english_name'];
576 }
577
578 if ( isset( $this->languages_translated[ $locale ]['native_name'] ) ) {
579 $new_languages[ $locale ]['native_name'] = $this->languages_translated[ $locale ]['native_name'];
580 }
581 }
582 }
583 }//end if
584
585 $this->languages = $new_languages;
586 $this->settings['enabled_languages'] = $new_languages;
587
588 update_option( $this->option_key, $this->settings );
589
590 }
591
592 /**
593 * Build admin area
594 *
595 * @since 0.1.0
596 *
597 * @param $options
598 * @param $component
599 *
600 * @return void
601 */
602 public function admin( $options, $component ) {
603
604 $this->languages_available = get_available_languages();
605
606 /**
607 * format: array( language, version, updated, english_name, native_name, package, iso, strings )
608 */
609 require_once ABSPATH . 'wp-admin/includes/translation-install.php';
610
611 $this->languages_translated = wp_get_available_translations();
612
613 // en_US is always installed (default locale of WP)
614 $data = array(
615 'en_US' => array(
616 'id' => 'en_US',
617 'locale' => 'en_US',
618 'lang' => 'English',
619 'lang_native' => 'English',
620 'enabled' => 'Default',
621 ),
622 );
623
624 foreach ( $this->languages_available as $locale ) {
625 $checked = checked( isset( $this->languages[ $locale ] ), true, false );
626
627 $enabled = sprintf( '<input type="checkbox" name="pods_i18n_enabled_languages[%s]" value="%s"%s />', esc_attr( $locale ), esc_attr( $locale ), $checked );
628
629 $data[ $locale ] = array(
630 'id' => $locale,
631 'locale' => $locale,
632 'lang' => $this->languages_translated[ $locale ]['english_name'],
633 'lang_native' => $this->languages_translated[ $locale ]['native_name'],
634 'enabled' => $enabled,
635 );
636 }
637
638 $ui = array(
639 'component' => $component,
640 // 'data' => $data,
641 // 'total' => count( $data ),
642 // 'total_found' => count( $data ),
643 'items' => __( 'Languages', 'pods' ),
644 'item' => __( 'Language', 'pods' ),
645 'fields' => array(
646 'manage' => array(
647 'enabled' => array(
648 'label' => __( 'Active', 'pods' ),
649 'type' => 'raw',
650 ),
651 'locale' => array(
652 'label' => __( 'Locale', 'pods' ),
653 'classes' => array( 'column-secondary' ),
654 ),
655 'lang' => array( 'label' => __( 'Language', 'pods' ) ),
656 'lang_native' => array( 'label' => __( 'Native name', 'pods' ) ),
657 /*
658 'fields' => array(
659 'label' => __( 'Fields', 'pods' ),
660 'type' => 'text',
661 'options' => array(
662 'text_allow_html' => 1,
663 'text_allowed_html_tags' => 'br code'
664 )
665 ),*/
666 ),
667 ),
668 'actions_disabled' => array( 'edit', 'add', 'delete', 'duplicate', 'view', 'export' ),
669 'actions_custom' => array(
670 // 'add' => array( $this, 'admin_add' ),
671 // 'edit' => array( $this, 'admin_edit' ),
672 // 'delete' => array( $this, 'admin_delete' )
673 ),
674 'search' => false,
675 'searchable' => false,
676 'sortable' => false,
677 'pagination' => false,
678 );
679
680 /**
681 * Filter the language data
682 *
683 * @since 0.1.0
684 *
685 * @param array
686 */
687 $data = apply_filters( 'pods_component_i18n_admin_data', $data );
688
689 /**
690 * Filter the UI fields
691 *
692 * @since 0.1.0
693 *
694 * @param array
695 */
696 $ui['fields'] = apply_filters( 'pods_component_i18n_admin_ui_fields', $ui['fields'], $data );
697
698 $ui['data'] = $data;
699 $ui['total'] = count( $data );
700 $ui['total_found'] = count( $data );
701
702 /*
703 if ( !pods_is_admin( array( 'pods_i18n_activate_lanuages' ) ) )
704 $ui[ 'actions_disabled' ][] = 'edit';*/
705
706 echo '<div id="pods_admin_i18n" class="pods-submittable-fields">';
707
708 // Do save action here because otherwise the loading of post_types get done first and labels aren't translated
709 if ( pods_is_admin( $this->capability ) && isset( $_POST['_nonce_i18n'] ) && wp_verify_nonce( $_POST['_nonce_i18n'], $this->nonce ) ) {
710 pods_message( __( 'Updated active languages.', 'pods' ) );
711 }
712
713 pods_ui( $ui );
714
715 // @todo Do this in pods_ui so we don't rely on javascript
716 echo '<div id="pods_i18n_settings_save">';
717 wp_nonce_field( $this->nonce, '_nonce_i18n', false );
718 submit_button();
719 echo '</div>';
720
721 echo '</div>';
722 }
723
724 /**
725 * The i18n option tab.
726 *
727 * @todo Remove if not used in final version
728 *
729 * @since 0.1.0
730 *
731 * @param array $tabs
732 * @param array $pod
733 * @param array $args
734 *
735 * @return array
736 */
737 public function pod_tab( $tabs, $pod, $args ) {
738
739 $tabs['pods-i18n'] = __( 'Translation Options', 'pods' );
740
741 return $tabs;
742 }
743
744 /**
745 * The i18n options
746 *
747 * @todo Remove if not used in final version
748 *
749 * @since 0.1.0
750 *
751 * @param array $options
752 * @param array $pod
753 *
754 * @return array
755 */
756 public function pod_options( $options, $pod ) {
757
758 // if ( $pod['type'] === '' )
759 /*
760 $options[ 'pods-i18n' ] = array(
761 'enabled_languages' => array(
762 'label' => __( 'Enable/Disable languages for this Pod', 'pods' ),
763 'help' => __( 'This overwrites the defaults set in the component admin.', 'pods' ),
764 'group' => array(),
765 ),
766 );
767
768 foreach ( $this->languages as $locale => $lang_data ) {
769 $options['pods-i18n']['enabled_languages']['group']['enable_i18n'][ $locale ] = array(
770 'label' => $locale . ' (' . $this->create_lang_label( $lang_data ) . ')',
771 'default' => 1,
772 'type' => 'boolean',
773 );
774 }*/
775
776 return $options;
777
778 }
779
780 /**
781 * Add the i18n metabox.
782 *
783 * @since 0.1.0
784 */
785 public function admin_meta_box() {
786
787 add_meta_box(
788 'pods_i18n',
789 // ID
790 __( 'Translation options', 'pods' ),
791 // Label
792 array( $this, 'meta_box' ),
793 // Callback
794 '_pods_pod',
795 // Screen
796 'side',
797 // Context (side)
798 'default'
799 // Priority
800 );
801 }
802
803 /**
804 * The i18n metabox.
805 *
806 * @todo Store enabled languages serialized instead of separate inputs
807 *
808 * @since 0.1.0
809 */
810 public function meta_box() {
811
812 $pod = $this->cur_pod;
813
814 if ( ! empty( $this->languages ) ) {
815 ?>
816 <p><?php esc_html_e( 'Enable/Disable languages for this Pod', 'pods' ); ?></p>
817 <p>
818 <small class="description"><?php esc_html_e( 'This overwrites the defaults set in the component admin.', 'pods' ); ?></small>
819 </p>
820 <div class="pods-field-enable-disable-language">
821 <?php
822 foreach ( $this->languages as $locale => $lang_data ) {
823
824 if ( ! isset( $pod['options']['enable_i18n'][ $locale ] ) ) {
825 // Enabled by default
826 $pod['options']['enable_i18n'][ $locale ] = 1;
827 }
828 ?>
829 <div class="pods-field-option pods-enable-disable-language" data-locale="<?php echo esc_attr( $locale ); ?>">
830 <?php
831 echo PodsForm::field(
832 'enable_i18n[' . $locale . ']',
833 $pod['options']['enable_i18n'][ $locale ],
834 'boolean',
835 array(
836 'boolean_yes_label' => '<code>' . $locale . '</code> ' . $this->create_lang_label( $lang_data ),
837 'boolean_no_label' => '',
838 )
839 );
840 ?>
841 </div>
842 <?php
843 }
844 ?>
845 </div>
846 <hr>
847 <p>
848 <button id="toggle_i18n" class="button-secondary"><?php esc_html_e( 'Toggle translation visibility', 'pods' ); ?></button>
849 </p>
850 <?php
851 }//end if
852 }
853
854 /**
855 * Adds translation inputs to fields
856 *
857 * @since 0.1.0
858 * @see PodsForm.php >> 'pods_form_ui_field_' . $type (filter)
859 *
860 * @param string $output The default output of the field
861 * @param string $name The field name
862 * @param string $value The field value
863 * @param array $options The field options
864 * @param array $pod The Pod
865 * @param int $id The field ID
866 *
867 * @return string
868 */
869 public function add_i18n_inputs( $output, $name, $value, $options, $pod, $id ) {
870
871 if ( ! empty( $pod ) || empty( $name ) || ! $this->is_translatable_field( $name ) ) {
872 return $output;
873 }
874
875 $pod = $this->cur_pod;
876 // print_r( $pod );
877 if ( empty( $pod ) ) {
878 // Setting the $pod var to a non-empty value is mandatory to prevent a loop
879 $pod = true;
880 }
881
882 $output .= '<br clear="both" />';
883 $output .= '<div class="pods-i18n-field">';
884 foreach ( $this->languages as $locale => $lang_data ) {
885
886 if ( ! $this->obj_is_language_enabled( $locale, (array) $pod ) ) {
887 continue;
888 }
889 // Our own shiny label with language information
890 $lang_code = '<code style="font-size: 1em;">' . $locale . '</code>';
891 /*
892 $lang_label = $this->create_lang_label( $lang_data );
893 if ( ! empty( $lang_label ) ) {
894 $lang_label = $lang_code . ' ('. $lang_label .')';
895 } else {*/
896 $lang_label = $lang_code;
897 // }
898 $lang_label = '<small>' . $lang_label . '</small>';
899
900 $style = '';
901
902 // Add language data to name for normal strings and array formatted strings
903 if ( strpos( $name, ']' ) !== false ) {
904 // Hide the i18n options for fields by default if they are empty
905 $field_value = pods_v( $name, $pod );
906
907 if ( strpos( $name, 'field_data' ) !== false && empty( $field_value ) ) {
908 $style = ' style="display: none;"';
909 }
910
911 $field_name = rtrim( $name, ']' );
912 $field_name .= '_' . $locale . ']';
913 } else {
914 $field_name = $name . '_' . $locale;
915 }
916
917 // Add the translation fields
918 $output .= '<div class="pods-i18n-input pods-i18n-input-' . $locale . '" data-locale="' . $locale . '" ' . $style . '>';
919 $output .= PodsForm::label( $field_name, $lang_label );
920 $output .= PodsForm::field( $field_name, pods_v( $field_name, $pod ), 'text', null, $pod );
921 $output .= '</div>';
922 }//end foreach
923 $output .= '</div>';
924
925 return $output;
926 }
927
928 /**
929 * Check if a language is get to enabled for an object
930 *
931 * @since 0.1.0
932 *
933 * @param string $locale The locale to validate
934 * @param array $data Object data
935 *
936 * @return bool
937 */
938 public function obj_is_language_enabled( $locale, $data ) {
939
940 // If the locale isn't enabled in the global scope from the component it's always disabled
941 if ( ! array_key_exists( $locale, $this->languages ) ) {
942 return false;
943 }
944 $data = (array) $data;
945 $options = ( isset( $data['options'] ) ) ? $data['options'] : $data;
946 // If it doesn't exist in the object data then use the default (enabled)
947 if ( isset( $options['enable_i18n'][ $locale ] ) && false === (bool) $options['enable_i18n'][ $locale ] ) {
948 return false;
949 }
950
951 return true;
952 }
953
954 /**
955 * Create a label with the english and native name combined
956 *
957 * @since 0.1.0
958 *
959 * @param array $lang_data
960 *
961 * @return string
962 */
963 public function create_lang_label( $lang_data ) {
964
965 $english_name = '';
966 $native_name = '';
967
968 if ( isset( $lang_data['english_name'] ) ) {
969 $english_name = $lang_data['english_name'];
970 }
971 if ( isset( $lang_data['native_name'] ) ) {
972 $native_name = $lang_data['native_name'];
973 }
974
975 if ( ! empty( $native_name ) && ! empty( $english_name ) ) {
976 if ( $native_name == $english_name ) {
977 return $english_name;
978 } else {
979 return $english_name . ' / ' . $native_name;
980 }
981 } else {
982 if ( ! empty( $english_name ) ) {
983 return $english_name;
984 }
985 if ( ! empty( $native_name ) ) {
986 return $native_name;
987 }
988 }
989
990 return '';
991 }
992
993 /**
994 * @return array
995 */
996 public function get_translatable_fields() {
997
998 return apply_filters( 'pods_translatable_fields', $this->translatable_fields );
999 }
1000
1001 }
1002