function thb_duplicable_fields_save(THB_Field $field, $post_id = 0)
 {
     $fieldKey = $field->getName();
     $uniqids = array();
     thb_duplicable_remove($fieldKey, $post_id);
     $count = 0;
     if (isset($_POST[$fieldKey])) {
         if ($field->isComplex()) {
             $subKeys = $field->getSubkeys();
             $count = count($_POST[$fieldKey][$subKeys[0]]);
         } else {
             $count = count($_POST[$fieldKey]);
         }
     }
     if ($count > 0) {
         for ($i = 0; $i < $count; $i++) {
             if ($field->isComplex()) {
                 $value = array();
                 foreach ($field->getSubkeys() as $subKey) {
                     if (isset($_POST[$fieldKey][$subKey][$i])) {
                         $value[$subKey] = thb_text_toDB($_POST[$fieldKey][$subKey][$i]);
                     }
                 }
                 // $value = serialize($value);
             } else {
                 if (isset($_POST[$fieldKey][$i])) {
                     $value = thb_text_toDB($_POST[$fieldKey][$i]);
                 }
             }
             $meta['subtemplate'] = $_POST['subtemplate'][$fieldKey][$i];
             if (!empty($_POST['uniqid'][$fieldKey][$i])) {
                 $meta['uniqid'] = $_POST['uniqid'][$fieldKey][$i];
             } else {
                 $meta['uniqid'] = md5(time() . $i);
             }
             $uniqids[] = $meta['uniqid'];
             if (is_array($value)) {
                 foreach ($value as $k => $v) {
                     $value[$k] = thb_text_normalize($v);
                 }
             } else {
                 $value = thb_text_normalize($value);
             }
             thb_duplicable_add($fieldKey, array('ord' => $i, 'value' => $value, 'meta' => $meta, 'post_id' => $post_id));
         }
     }
     return $uniqids;
 }
 function thb_save_tab()
 {
     thb_system_verify_nonce('THB_tab');
     $theme = thb_theme();
     $page = $theme->getAdmin()->getPage($_POST['page']);
     $tab = $page->getTab($_POST['tab']);
     $containers = $tab->getContainers();
     $uniqids = array();
     foreach ($containers as $container) {
         if (!$container->isDuplicable()) {
             $newoptions = array();
             foreach ($container->getFields() as $field) {
                 if ($field->isComplex()) {
                     $value = array();
                     foreach ($field->getSubkeys() as $subKey) {
                         $value[$subKey] = thb_text_toDB($_POST[$field->getName()][$subKey]);
                     }
                 } else {
                     $value = thb_text_toDB($_POST[$field->getName()]);
                 }
                 $newoptions[$field->getName()] = $value;
             }
             $theme->saveOptions($newoptions);
         } else {
             $uniqids[$container->getSlug()] = thb_duplicable_fields_save($container->getField());
         }
     }
     thb_system_raise_success(__('All saved!', 'thb_text_domain'), $uniqids);
 }