/**
  * Build the $tax_input array
  *
  * Takes each term from the $tax_inputs parameter and builds an array of
  * language-specific term_id to term_id/term_name mappings for all languages.
  *
  * @since 2.11
  * @uses MLA_WPML::$tax_input
  * @uses MLA_WPML::$existing_terms
  *
  * @param	integer	$post_id ID of the current post
  * @param	array	$tax_inputs 'tax_input' request parameter
  * @param	array	$tax_actions 'tax_action' request parameter
  */
 private static function _build_tax_input($post_id, $tax_inputs = NULL, $tax_actions = NULL)
 {
     global $sitepress;
     //error_log( __LINE__ . " build_tax_input( {$post_id} ) tax_inputs = " . var_export( $tax_inputs, true ), 0 );
     //error_log( __LINE__ . " build_tax_input( {$post_id} ) tax_actions = " . var_export( $tax_actions, true ), 0 );
     //error_log( __LINE__ . " build_tax_input( {$post_id} ) self :: tax_input = " . var_export( self::$tax_input, true ), 0 );
     if ($post_id == self::$tax_input['tax_input_post_id']) {
         return;
     }
     self::$tax_input = array('tax_input_post_id' => $post_id);
     $active_languages = $sitepress->get_active_languages();
     /*
      * See if we are cloning/"replacing" the existing assignments
      */
     if (NULL == $tax_inputs && NULL == $tax_actions && isset(self::$existing_terms['element_id']) && $post_id == self::$existing_terms['element_id']) {
         $translation = self::$existing_terms[self::$existing_terms['language_code']];
         $taxonomies = $sitepress->get_translatable_taxonomies(true, 'attachment');
         $tax_inputs = array();
         $no_terms = true;
         foreach ($taxonomies as $taxonomy_name) {
             $terms = isset($translation[$taxonomy_name]) ? $translation[$taxonomy_name] : array();
             if (!empty($terms)) {
                 $no_terms = false;
                 $input_terms = array();
                 foreach ($terms as $term) {
                     $input_terms[] = $term->term_id;
                 }
                 $tax_inputs[$taxonomy_name] = $input_terms;
             } else {
                 $tax_inputs[$taxonomy_name] = array();
             }
         }
         // taxonomy_name
         //error_log( __LINE__ . " build_tax_input( {$post_id} ) cloned tax_inputs = " . var_export( $tax_inputs, true ), 0 );
         if ($no_terms) {
             foreach ($active_languages as $language => $language_details) {
                 self::$tax_input[$language] = array();
             }
             return;
         }
     }
     // cloning
     foreach ($tax_inputs as $taxonomy => $terms) {
         $tax_action = isset($tax_actions[$taxonomy]) ? $tax_actions[$taxonomy] : 'replace';
         $input_terms = array();
         // hierarchical taxonomy => array of term_id integer values; flat => comma-delimited string of names
         if ($hierarchical = is_array($terms)) {
             foreach ($terms as $term) {
                 if (0 == $term) {
                     continue;
                 }
                 $relevant_term = self::_get_relevant_term('term_id', $term, $taxonomy);
                 if (isset($relevant_term['translations'])) {
                     foreach ($relevant_term['translations'] as $language => $translation) {
                         if ($translated_term = self::_get_relevant_term('term_taxonomy_id', $translation->element_id, $taxonomy)) {
                             $input_terms[$language][$translation->element_id] = $translated_term['term'];
                         }
                     }
                     // for each language
                 }
                 // translations exist
             }
             // foreach term
         } else {
             // Convert names to an array
             $term_names = array_map('trim', explode(',', $terms));
             foreach ($term_names as $term_name) {
                 if (!empty($term_name)) {
                     $relevant_term = self::_get_relevant_term('name', $term_name, $taxonomy);
                     if (isset($relevant_term['translations'])) {
                         foreach ($relevant_term['translations'] as $language => $translation) {
                             if ($translated_term = self::_get_relevant_term('term_taxonomy_id', $translation->element_id, $taxonomy)) {
                                 $input_terms[$language][$translation->element_id] = $translated_term['term'];
                             }
                         }
                         // for each language
                     }
                     // translations exist
                 }
                 // not empty
             }
             // foreach name
         }
         // flat taxonomy
         foreach ($active_languages as $language => $language_details) {
             /*
              * Apply the tax_action to the terms_before to find the terms_after
              */
             $term_changes = isset($input_terms[$language]) ? $input_terms[$language] : array();
             if ('replace' == $tax_action) {
                 $terms_after = $term_changes;
             } else {
                 $terms_after = isset(self::$existing_terms[$language][$taxonomy]) ? self::$existing_terms[$language][$taxonomy] : array();
                 foreach ($term_changes as $term_taxonomy_id => $input_term) {
                     if ('add' == $tax_action) {
                         $terms_after[$term_taxonomy_id] = $input_term;
                     } else {
                         unset($terms_after[$term_taxonomy_id]);
                     }
                 }
                 // input_term
             }
             /*
              * Convert terms_after to tax_input format
              */
             $term_changes = array();
             foreach ($terms_after as $input_term) {
                 $term_changes[] = $input_term->term_id;
             }
             self::$tax_input[$language][$taxonomy] = $term_changes;
         }
         // language
     }
     // foreach taxonomy
     MLACore::mla_debug_add(__LINE__ . " MLA_WPML::_build_tax_input( {$post_id} ) self::\$tax_input = " . var_export(self::$tax_input, true), MLACore::MLA_DEBUG_CATEGORY_AJAX);
     MLACore::mla_debug_add(__LINE__ . " MLA_WPML::_build_tax_input( {$post_id} ) self::\$relevant_terms = " . var_export(self::$relevant_terms, true), MLACore::MLA_DEBUG_CATEGORY_AJAX);
 }