/**
  * Ajax handler for adding a term via Ajax.
  */
 public static function wpml_save_term()
 {
     global $sitepress;
     $taxonomy = false;
     $lang = false;
     $name = false;
     $slug = false;
     $trid = false;
     $description = false;
     $sync = false;
     $nonce = filter_input(INPUT_POST, '_icl_nonce');
     if (!wp_verify_nonce($nonce, 'wpml_save_term_nonce')) {
         wp_send_json_error('Wrong Nonce');
     }
     if (isset($_POST['term_language_code'])) {
         $lang = $_POST['term_language_code'];
     }
     if (isset($_POST['taxonomy'])) {
         $taxonomy = $_POST['taxonomy'];
     }
     if (isset($_POST['slug'])) {
         $slug = $_POST['slug'];
     }
     if (isset($_POST['name'])) {
         $name = $_POST['name'];
     }
     if (isset($_POST['trid'])) {
         $trid = $_POST['trid'];
     }
     if (isset($_POST['description'])) {
         $description = $_POST['description'];
     }
     if (isset($_POST['force_hierarchical_sync'])) {
         $sync = $_POST['force_hierarchical_sync'];
     }
     $new_term_object = false;
     if ($name && $taxonomy && $trid && $lang) {
         $args = array('taxonomy' => $taxonomy, 'lang_code' => $lang, 'term' => $name, 'trid' => $trid, 'overwrite' => true);
         if ($slug) {
             $args['slug'] = $slug;
         }
         if ($description) {
             $args['description'] = $description;
         }
         $res = WPML_Terms_Translations::create_new_term($args);
         if ($res && isset($res['term_taxonomy_id'])) {
             /* res holds the term taxonomy id, we return the whole term objects to the ajax call */
             $new_term_object = get_term_by('term_taxonomy_id', (int) $res['term_taxonomy_id'], $taxonomy);
             $lang_details = $sitepress->get_element_language_details($new_term_object->term_taxonomy_id, 'tax_' . $new_term_object->taxonomy);
             $new_term_object->trid = $lang_details->trid;
             $new_term_object->language_code = $lang_details->language_code;
             WPML_Terms_Translations::icl_save_term_translation_action($taxonomy, $res);
             if ($sync) {
                 $tree = new WPML_Translation_Tree($taxonomy);
                 $tree->sync_tree($lang, $sync);
             }
         }
     }
     wp_send_json_success($new_term_object);
 }
 /**
  * @param $terms    array
  * @param $taxonomy string
  *                  Orders a list of terms alphabetically and hierarchy-wise
  *
  * @return array
  */
 private static function order_terms_list($terms, $taxonomy)
 {
     global $wpdb, $sitepress;
     $terms_tree = new WPML_Translation_Tree($wpdb, $sitepress, $taxonomy, false, $terms);
     $ordered_terms = $terms_tree->get_alphabetically_ordered_list();
     foreach ($ordered_terms as $key => $trid_group) {
         $ordered_terms[$key] = self::set_language_information($trid_group, $terms);
     }
     return $ordered_terms;
 }
 /**
  * @param $taxonomy
  * @param $lang
  * Synchronizes the parent child relationships with those of the source terms, for all terms in the given taxonomy
  * and language.
  * @return bool
  */
 private static function sync_parent_child_relations($taxonomy, $lang)
 {
     $taxonomy_tree = new WPML_Translation_Tree($taxonomy);
     $taxonomy_tree->sync_tree($lang);
     return true;
 }
 /**
  * Orders a list of terms alphabetically and hierarchy-wise
  *
  * @param $terms    array
  *
  * @return array
  */
 private function order_terms_list($terms)
 {
     $terms_tree = new WPML_Translation_Tree($this->sitepress, $this->taxonomy, $terms);
     $ordered_terms = $terms_tree->get_alphabetically_ordered_list();
     foreach ($ordered_terms as $key => $trid_group) {
         $ordered_terms[$key] = self::set_language_information($trid_group, $terms);
     }
     return $ordered_terms;
 }