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 |