PluginProbe ʕ •ᴥ•ʔ
Meta Box / 4.17.3
Meta Box v4.17.3
trunk 4.1.10 4.1.11 4.10 4.10.1 4.10.2 4.10.3 4.10.4 4.11 4.11.1 4.11.2 4.12.1 4.12.4 4.12.5 4.12.6 4.13.0 4.13.1 4.13.2 4.13.3 4.13.4 4.14.0 4.14.1 4.14.10 4.14.11 4.14.2 4.14.4 4.14.5 4.14.6 4.14.7 4.14.8 4.14.9 4.15.0 4.15.1 4.15.2 4.15.3 4.15.4 4.15.5 4.15.6 4.15.7 4.15.8 4.15.9 4.16.0 4.16.1 4.16.2 4.16.3 4.17.0 4.17.1 4.17.2 4.17.3 4.18.0 4.18.1 4.18.2 4.18.3 4.18.4 4.2 4.2.1 4.2.2 4.2.3 4.2.4 4.3 4.3.1 4.3.10 4.3.11 4.3.2 4.3.3 4.3.4 4.3.5 4.3.6 4.3.7 4.3.8 4.3.9 4.4.0 4.4.1 4.4.3 4.5 4.5.1 4.5.2 4.5.3 4.5.4 4.5.5 4.5.6 4.5.7 4.6 4.7 4.7.1 4.7.2 4.7.3 4.8.0 4.8.1 4.8.2 4.8.3 4.8.4 4.8.5 4.8.6 4.8.7 4.9 4.9.1 4.9.2 4.9.3 4.9.4 4.9.5 4.9.6 4.9.7 4.9.8 5.0.0 5.0.1 5.1.0 5.1.1 5.1.2 5.10.0 5.10.1 5.10.10 5.10.11 5.10.12 5.10.13 5.10.14 5.10.15 5.10.16 5.10.17 5.10.18 5.10.19 5.10.2 5.10.3 5.10.4 5.10.5 5.10.6 5.10.7 5.10.8 5.10.9 5.11.0 5.11.1 5.11.2 5.11.3 5.11.4 5.12.0 5.2.0 5.2.1 5.2.10 5.2.2 5.2.3 5.2.4 5.2.5 5.2.6 5.2.7 5.2.8 5.2.9 5.3.0 5.3.1 5.3.10 5.3.2 5.3.3 5.3.4 5.3.5 5.3.6 5.3.7 5.3.8 5.3.9 5.4.0 5.4.1 5.4.2 5.4.3 5.4.4 5.4.5 5.4.6 5.4.7 5.4.8 5.5.0 5.5.1 5.6.0 5.6.1 5.6.10 5.6.11 5.6.12 5.6.13 5.6.14 5.6.15 5.6.16 5.6.17 5.6.18 5.6.2 5.6.3 5.6.4 5.6.5 5.6.6 5.6.7 5.6.8 5.6.9 5.7.0 5.7.1 5.7.2 5.7.3 5.7.4 5.7.5 5.8.0 5.8.1 5.8.2 5.9.0 5.9.1 5.9.10 5.9.11 5.9.2 5.9.3 5.9.4 5.9.5 5.9.6 5.9.7 5.9.8 5.9.9
meta-box / inc / wpml.php
meta-box / inc Last commit date
about 7 years ago fields 7 years ago helpers 7 years ago interfaces 9 years ago storages 7 years ago templates 8 years ago walkers 7 years ago autoloader.php 7 years ago clone.php 7 years ago core.php 8 years ago field-registry.php 8 years ago field.php 7 years ago functions.php 7 years ago loader.php 7 years ago media-modal.php 8 years ago meta-box-registry.php 8 years ago meta-box.php 7 years ago sanitizer.php 7 years ago storage-registry.php 7 years ago validation.php 7 years ago wpml.php 8 years ago
wpml.php
126 lines
1 <?php
2 /**
3 * The WPML compatibility module, allowing all fields are translatable by WPML plugin.
4 *
5 * @package Meta Box
6 */
7
8 /**
9 * WPML compatibility class
10 */
11 class RWMB_WPML {
12 /**
13 * List of fields that need to translate values (because they're saved as IDs).
14 *
15 * @var array
16 */
17 protected $field_types = array( 'post', 'taxonomy_advanced' );
18
19 /**
20 * Initialize.
21 */
22 public function init() {
23 /**
24 * Run before meta boxes are registered so it can modify fields.
25 *
26 * @see modify_field()
27 */
28 add_action( 'init', array( $this, 'register_hooks' ), 9 );
29 }
30
31 /**
32 * Register hooks.
33 */
34 public function register_hooks() {
35 if ( ! defined( 'ICL_SITEPRESS_VERSION' ) ) {
36 return;
37 }
38 add_filter( 'wpml_duplicate_generic_string', array( $this, 'translate_ids' ), 10, 3 );
39 add_filter( 'rwmb_normalize_field', array( $this, 'modify_field' ) );
40 }
41
42 /**
43 * Translating IDs stored as field values upon WPML post/page duplication.
44 *
45 * @param mixed $value Meta value.
46 * @param string $target_language Target language.
47 * @param array $meta_data Meta arguments.
48 * @return mixed
49 */
50 public function translate_ids( $value, $target_language, $meta_data ) {
51 if ( 'custom_field' !== $meta_data['context'] ) {
52 return $value;
53 }
54
55 $field = rwmb_get_registry( 'field' )->get( $meta_data['key'], get_post_type( $meta_data['master_post_id'] ) );
56 if ( false === $field || ! in_array( $field['type'], $this->field_types, true ) ) {
57 return $value;
58 }
59
60 // Object type needed for WPML filter differs between fields.
61 $object_type = 'taxonomy_advanced' === $field['type'] ? $field['taxonomy'] : $field['post_type'];
62
63 // Translating values, whether are stored as comma separated strings or not.
64 if ( false === strpos( $value, ',' ) ) {
65 $value = apply_filters( 'wpml_object_id', $value, $object_type, true, $target_language );
66 return $value;
67 }
68
69 // Dealing with IDs stored as comma separated strings.
70 $translated_values = array();
71 $values = explode( ',', $value );
72
73 foreach ( $values as $v ) {
74 $translated_values[] = apply_filters( 'wpml_object_id', $v, $object_type, true, $target_language );
75 }
76
77 $value = implode( ',', $translated_values );
78 return $value;
79 }
80
81 /**
82 * Modified field depends on its translation status.
83 * If the post is a translated version of another post and the field is set to:
84 * - Do not translate: hide the field.
85 * - Copy: make it disabled so users cannot edit.
86 * - Translate: do nothing.
87 *
88 * @param array $field Field parameters.
89 *
90 * @return mixed
91 */
92 public function modify_field( $field ) {
93 global $wpml_post_translations;
94
95 if ( empty( $field['id'] ) ) {
96 return $field;
97 }
98
99 // Get post ID.
100 $post_id = filter_input( INPUT_GET, 'post', FILTER_SANITIZE_NUMBER_INT );
101 if ( ! $post_id ) {
102 $post_id = filter_input( INPUT_POST, 'post_ID', FILTER_SANITIZE_NUMBER_INT );
103 }
104
105 // If the post is the original one: do nothing.
106 if ( ! method_exists( $wpml_post_translations, 'get_source_lang_code' ) || ! $wpml_post_translations->get_source_lang_code( $post_id ) ) {
107 return $field;
108 }
109
110 // Get setting for the custom field translation.
111 $custom_fields_translation = apply_filters( 'wpml_sub_setting', false, 'translation-management', 'custom_fields_translation' );
112 if ( ! isset( $custom_fields_translation[ $field['id'] ] ) ) {
113 return $field;
114 }
115
116 $setting = intval( $custom_fields_translation[ $field['id'] ] );
117 if ( 0 === $setting ) { // Do not translate: hide it.
118 $field['class'] .= ' hidden';
119 } elseif ( 1 === $setting ) { // Copy: disable editing.
120 $field['disabled'] = true;
121 }
122
123 return $field;
124 }
125 }
126