/**
 * Submit function
 */
function wpcf_admin_custom_taxonomies_form_submit($form)
{
    if (!isset($_POST['ct'])) {
        return false;
    }
    $data = $_POST['ct'];
    $update = false;
    // Sanitize data
    if (isset($data['wpcf-tax'])) {
        $update = true;
        $data['wpcf-tax'] = sanitize_title($data['wpcf-tax']);
    }
    if (isset($data['slug'])) {
        $data['slug'] = sanitize_title($data['slug']);
    }
    if (isset($data['rewrite']['slug'])) {
        $data['rewrite']['slug'] = remove_accents($data['rewrite']['slug']);
        $data['rewrite']['slug'] = strtolower($data['rewrite']['slug']);
        $data['rewrite']['slug'] = trim($data['rewrite']['slug']);
    }
    // Set tax name
    $tax = '';
    if (!empty($data['slug'])) {
        $tax = $data['slug'];
    } else {
        if (!empty($data['wpcf-tax'])) {
            $tax = $data['wpcf-tax'];
        } else {
            if (!empty($data['labels']['singular_name'])) {
                $tax = sanitize_title($data['labels']['singular_name']);
            }
        }
    }
    if (empty($tax)) {
        wpcf_admin_message(__('Please set taxonomy name', 'wpcf'), 'error');
        return false;
    }
    if (empty($data['labels']['singular_name'])) {
        $data['labels']['singular_name'] = $tax;
    }
    $data['slug'] = $tax;
    $custom_taxonomies = get_option('wpcf-custom-taxonomies', array());
    // Check reserved name
    if (wpcf_is_reserved_name($tax)) {
        wpcf_admin_message(sprintf(__('The name %s is reserved in WordPress and cannot be used in custom taxonomies. Please use a different name.', 'wpcf'), $tax), 'error');
        return false;
    }
    // Check if exists
    if ($update && !array_key_exists($data['wpcf-tax'], $custom_taxonomies)) {
        wpcf_admin_message(__("Custom taxonomy do not exist", 'wpcf'), 'error');
        return false;
    }
    // Check overwriting
    if (!$update && array_key_exists($tax, $custom_taxonomies)) {
        wpcf_admin_message(__('Custom taxonomy already exists', 'wpcf'), 'error');
        return false;
    }
    // Check if our tax overwrites some tax outside
    $tax_exists = get_taxonomy($tax);
    if (!$update && !empty($tax_exists)) {
        wpcf_admin_message(__('Taxonomy already exists', 'wpcf'), 'error');
        return false;
    }
    // Check if renaming
    if ($update && $data['wpcf-tax'] != $tax) {
        global $wpdb;
        $wpdb->update($wpdb->term_taxonomy, array('taxonomy' => $tax), array('taxonomy' => $data['wpcf-tax']), array('%s'), array('%s'));
        // Delete old type
        unset($custom_taxonomies[$data['wpcf-tax']]);
    }
    // Check if active
    if (isset($custom_taxonomies[$tax]['disabled'])) {
        $data['disabled'] = $custom_taxonomies[$tax]['disabled'];
    }
    // Sync with post types
    if (!empty($data['supports'])) {
        $post_types = get_option('wpcf-custom-types', array());
        foreach ($post_types as $id => $type) {
            if (array_key_exists($id, $data['supports'])) {
                $post_types[$id]['taxonomies'][$data['slug']] = 1;
            } else {
                unset($post_types[$id]['taxonomies'][$data['slug']]);
            }
        }
        update_option('wpcf-custom-types', $post_types);
    }
    $custom_taxonomies[$tax] = $data;
    update_option('wpcf-custom-taxonomies', $custom_taxonomies);
    // WPML register strings
    wpcf_custom_taxonimies_register_translation($tax, $data);
    wpcf_admin_message_store(__('Custom taxonomy saved', 'wpcf'));
    // Flush rewrite rules
    flush_rewrite_rules();
    // Redirect
    wp_redirect(admin_url('admin.php?page=wpcf-edit-tax&wpcf-tax=' . $tax . '&wpcf-rewrite=1'));
    die;
}
function ajax_wpcf_is_reserved_name()
{
    // slug
    $name = isset($_POST['slug']) ? $_POST['slug'] : '';
    // context
    $context = isset($_POST['context']) ? $_POST['context'] : false;
    // check also page slugs
    $check_pages = isset($_POST['check_pages']) && $_POST['check_pages'] == false ? false : true;
    // slug pre save
    if (isset($_POST['slugPreSave']) && $_POST['slugPreSave'] !== 0) {
        // for taxonomy
        if ($context == 'taxonomy') {
            $_POST['ct']['wpcf-tax'] = $_POST['slugPreSave'];
        }
        // for post_type
        if ($context == 'post_type') {
            $_POST['ct']['wpcf-post-type'] = $_POST['slugPreSave'];
        }
    }
    if ($context == 'post_type' || $context == 'taxonomy') {
        $used_reserved = wpcf_is_reserved_name($name, $context, $check_pages);
        if ($used_reserved) {
            die(json_encode(array('already_in_use' => 1)));
        }
    }
    // die( json_encode( $_POST ) );
    die(json_encode(array('already_in_use' => 0)));
}
/**
 * Submit function
 */
function wpcf_admin_custom_types_form_submit($form)
{
    global $wpcf;
    if (!isset($_POST['ct'])) {
        return false;
    }
    $data = $_POST['ct'];
    $update = false;
    // Sanitize data
    if (isset($data['wpcf-post-type'])) {
        $update = true;
        $data['wpcf-post-type'] = sanitize_title($data['wpcf-post-type']);
    }
    if (isset($data['slug'])) {
        $data['slug'] = sanitize_title($data['slug']);
    }
    if (isset($data['rewrite']['slug'])) {
        $data['rewrite']['slug'] = remove_accents($data['rewrite']['slug']);
        $data['rewrite']['slug'] = strtolower($data['rewrite']['slug']);
        $data['rewrite']['slug'] = trim($data['rewrite']['slug']);
    }
    // Set post type name
    $post_type = '';
    if (!empty($data['slug'])) {
        $post_type = $data['slug'];
    } else {
        if (!empty($data['wpcf-post-type'])) {
            $post_type = $data['wpcf-post-type'];
        } else {
            if (!empty($data['labels']['singular_name'])) {
                $post_type = sanitize_title($data['labels']['singular_name']);
            }
        }
    }
    if (empty($post_type)) {
        wpcf_admin_message(__('Please set post type name', 'wpcf'), 'error');
        //        $form->triggerError();
        return false;
    }
    $data['slug'] = $post_type;
    $custom_types = get_option('wpcf-custom-types', array());
    // Check reserved name
    $reserved = wpcf_is_reserved_name($post_type);
    if (is_wp_error($reserved)) {
        wpcf_admin_message($reserved->get_error_message(), 'error');
        return false;
    }
    // Check overwriting
    if (!$update && array_key_exists($post_type, $custom_types)) {
        wpcf_admin_message(__('Custom post type already exists', 'wpcf'), 'error');
        //            $form->triggerError();
        return false;
    }
    /*
     * Since Types 1.2
     * We do not allow plural and singular names to be same.
     */
    if ($wpcf->post_types->check_singular_plural_match($data)) {
        wpcf_admin_message($wpcf->post_types->message('warning_singular_plural_match'), 'error');
        return false;
    }
    // Check if renaming then rename all post entries and delete old type
    if (!empty($data['wpcf-post-type']) && $data['wpcf-post-type'] != $post_type) {
        global $wpdb;
        $wpdb->update($wpdb->posts, array('post_type' => $post_type), array('post_type' => $data['wpcf-post-type']), array('%s'), array('%s'));
        // Set protected data
        $protected_data_check = $custom_types[$data['wpcf-post-type']];
        // Delete old type
        unset($custom_types[$data['wpcf-post-type']]);
        $data['wpcf-post-type'] = $post_type;
    } else {
        // Set protected data
        $protected_data_check = !empty($custom_types[$post_type]) ? $custom_types[$post_type] : array();
    }
    // Check if active
    if (isset($custom_types[$post_type]['disabled'])) {
        $data['disabled'] = $custom_types[$post_type]['disabled'];
    }
    // Sync taxes with custom taxes
    if (!empty($data['taxonomies'])) {
        $taxes = get_option('wpcf-custom-taxonomies', array());
        foreach ($taxes as $id => $tax) {
            if (array_key_exists($id, $data['taxonomies'])) {
                $taxes[$id]['supports'][$data['slug']] = 1;
            } else {
                unset($taxes[$id]['supports'][$data['slug']]);
            }
        }
        update_option('wpcf-custom-taxonomies', $taxes);
    }
    // Preserve protected data
    foreach ($protected_data_check as $key => $value) {
        if (strpos($key, '_') !== 0) {
            unset($protected_data_check[$key]);
        }
    }
    // Merging protected data
    $custom_types[$post_type] = array_merge($protected_data_check, $data);
    update_option('wpcf-custom-types', $custom_types);
    // WPML register strings
    wpcf_custom_types_register_translation($post_type, $data);
    wpcf_admin_message_store(apply_filters('types_message_custom_post_type_saved', __('Custom post type saved', 'wpcf'), $data, $update), 'custom');
    // Flush rewrite rules
    flush_rewrite_rules();
    do_action('wpcf_custom_types_save', $data);
    // Redirect
    wp_redirect(admin_url('admin.php?page=wpcf-edit-type&wpcf-post-type=' . $post_type . '&wpcf-rewrite=1'));
    die;
}
 /**
  * Summary.
  *
  * Description.
  *
  * @since x.x.x
  * @access (for functions: only use if private)
  *
  * @see Function/method/class relied on
  * @link URL
  * @global type $varname Description.
  * @global type $varname Description.
  *
  * @param type $var Description.
  * @param type $var Optional. Description.
  * @return type Description.
  */
 private function save()
 {
     global $wpcf;
     if (!isset($_POST['ct'])) {
         return false;
     }
     $data = $_POST['ct'];
     $update = false;
     // Sanitize data
     if (isset($data[$this->get_id])) {
         $update = true;
         $data[$this->get_id] = sanitize_title($data[$this->get_id]);
     } else {
         $data[$this->get_id] = null;
     }
     if (isset($data['slug'])) {
         $data['slug'] = sanitize_title($data['slug']);
     } elseif ($_GET['wpcf-post-type'] == 'post' || $_GET['wpcf-post-type'] == 'page' || $_GET['wpcf-post-type'] == 'attachment') {
         $data['slug'] = $_GET['wpcf-post-type'];
     } else {
         $data['slug'] = null;
     }
     if (isset($data['rewrite']['slug'])) {
         $data['rewrite']['slug'] = remove_accents($data['rewrite']['slug']);
         $data['rewrite']['slug'] = strtolower($data['rewrite']['slug']);
         $data['rewrite']['slug'] = trim($data['rewrite']['slug']);
     }
     $data['_builtin'] = false;
     // Set post type name
     $post_type = null;
     if (!empty($data['slug'])) {
         $post_type = $data['slug'];
     } elseif (!empty($data[$this->get_id])) {
         $post_type = $data[$this->get_id];
     } elseif (!empty($data['labels']['singular_name'])) {
         $post_type = sanitize_title($data['labels']['singular_name']);
     }
     if (empty($post_type)) {
         wpcf_admin_message(__('Please set post type name', 'wpcf'), 'error');
         return false;
     }
     $data['slug'] = $post_type;
     $custom_types = get_option(WPCF_OPTION_NAME_CUSTOM_TYPES, array());
     $protected_data_check = array();
     if (wpcf_is_builtin_post_types($data['slug'])) {
         $data['_builtin'] = true;
         $update = true;
     } else {
         // Check reserved name
         $reserved = wpcf_is_reserved_name($post_type, 'post_type');
         if (is_wp_error($reserved)) {
             wpcf_admin_message($reserved->get_error_message(), 'error');
             return false;
         }
         // Check overwriting
         if ((!array_key_exists($this->get_id, $data) || $data[$this->get_id] != $post_type) && array_key_exists($post_type, $custom_types)) {
             wpcf_admin_message(__('Post Type already exists', 'wpcf'), 'error');
             return false;
         }
         /*
          * Since Types 1.2
          * We do not allow plural and singular names to be same.
          */
         if ($wpcf->post_types->check_singular_plural_match($data)) {
             wpcf_admin_message($wpcf->post_types->message('warning_singular_plural_match'), 'error');
             return false;
         }
         // Check if renaming then rename all post entries and delete old type
         if (!empty($data[$this->get_id]) && $data[$this->get_id] != $post_type) {
             global $wpdb;
             $wpdb->update($wpdb->posts, array('post_type' => $post_type), array('post_type' => $data[$this->get_id]), array('%s'), array('%s'));
             /**
              * update post meta "_wp_types_group_post_types"
              */
             $sql = $wpdb->prepare(sprintf('select meta_id, meta_value from %s where meta_key = %%s', $wpdb->postmeta), '_wp_types_group_post_types');
             $all_meta = $wpdb->get_results($sql, OBJECT_K);
             $re = sprintf('/,%s,/', $data[$this->get_id]);
             foreach ($all_meta as $meta) {
                 if (!preg_match($re, $meta->meta_value)) {
                     continue;
                 }
                 $wpdb->update($wpdb->postmeta, array('meta_value' => preg_replace($re, ',' . $post_type . ',', $meta->meta_value)), array('meta_id' => $meta->meta_id), array('%s'), array('%d'));
             }
             /**
              * update _wpcf_belongs_{$data[$this->get_id]}_id
              */
             $wpdb->update($wpdb->postmeta, array('meta_key' => sprintf('_wpcf_belongs_%s_id', $post_type)), array('meta_key' => sprintf('_wpcf_belongs_%s_id', $data[$this->get_id])), array('%s'), array('%s'));
             /**
              * update options "wpv_options"
              */
             $wpv_options = get_option('wpv_options', true);
             if (is_array($wpv_options)) {
                 $re = sprintf('/(views_template_(archive_)?for_)%s/', $data[$this->get_id]);
                 foreach ($wpv_options as $key => $value) {
                     if (!preg_match($re, $key)) {
                         continue;
                     }
                     unset($wpv_options[$key]);
                     $key = preg_replace($re, "\$1" . $post_type, $key);
                     $wpv_options[$key] = $value;
                 }
                 update_option('wpv_options', $wpv_options);
             }
             /**
              * update option "wpcf-custom-taxonomies"
              */
             $wpcf_custom_taxonomies = get_option(WPCF_OPTION_NAME_CUSTOM_TAXONOMIES, true);
             if (is_array($wpcf_custom_taxonomies)) {
                 $update_wpcf_custom_taxonomies = false;
                 foreach ($wpcf_custom_taxonomies as $key => $value) {
                     if (array_key_exists('supports', $value) && array_key_exists($data[$this->get_id], $value['supports'])) {
                         unset($wpcf_custom_taxonomies[$key]['supports'][$data[$this->get_id]]);
                         $update_wpcf_custom_taxonomies = true;
                     }
                 }
                 if ($update_wpcf_custom_taxonomies) {
                     update_option(WPCF_OPTION_NAME_CUSTOM_TAXONOMIES, $wpcf_custom_taxonomies);
                 }
             }
             // Sync action
             do_action('wpcf_post_type_renamed', $post_type, $data[$this->get_id]);
             // Set protected data
             $protected_data_check = $custom_types[$data[$this->get_id]];
             // Delete old type
             unset($custom_types[$data[$this->get_id]]);
             $data[$this->get_id] = $post_type;
         } else {
             // Set protected data
             $protected_data_check = !empty($custom_types[$post_type]) ? $custom_types[$post_type] : array();
         }
         // Check if active
         if (isset($custom_types[$post_type]['disabled'])) {
             $data['disabled'] = $custom_types[$post_type]['disabled'];
         }
     }
     // Sync taxes with custom taxes
     $taxes = get_option(WPCF_OPTION_NAME_CUSTOM_TAXONOMIES, array());
     foreach ($taxes as $id => $tax) {
         if (isset($data['taxonomies']) && !empty($data['taxonomies']) && array_key_exists($id, $data['taxonomies'])) {
             $taxes[$id]['supports'][$data['slug']] = 1;
         } else {
             if (isset($taxes[$id]['supports'][$data['slug']])) {
                 unset($taxes[$id]['supports'][$data['slug']]);
             }
         }
     }
     update_option(WPCF_OPTION_NAME_CUSTOM_TAXONOMIES, $taxes);
     // Preserve protected data
     foreach ($protected_data_check as $key => $value) {
         if (strpos($key, '_') !== 0) {
             unset($protected_data_check[$key]);
         }
     }
     /**
      * save custom field group
      */
     /* removed types-608
        $post_to_groups = isset($_POST['ct']['custom-field-group'])?$_POST['ct']['custom-field-group']:array();
        $groups = $this->fields->get_groups_with_post_types();
        foreach( $groups as $group) {
            $post_types_to_save = $group['_wp_types_group_post_types'];
            // save
            if ( array_key_exists($group['id'], $post_to_groups)) {
                $post_types_to_save[] = $data['slug'];
            } else {
                if(($key = array_search($data['slug'], $post_types_to_save)) !== false) {
                    unset($post_types_to_save[$key]);
                }
                if (
                    false
                    || empty($post_types_to_save)
                    || (
                        true
                        && 1 == sizeof($post_types_to_save)
                        && 'all' == current($post_types_to_save)
                    )
                ) {
                    $post_types_to_save = array();
                    foreach( get_post_types() as $key => $value ) {
                        if ( $data['slug'] == $value) {
                            continue;
                        }
                        if ( in_array($value, $wpcf->excluded_post_types) ) {
                            continue;
                        }
                        $post_types_to_save[] = $value;
                    }
                }
            }
            wpcf_admin_fields_save_group_post_types($group['id'], $post_types_to_save);
        }
        */
     /**
      * set last edit time
      */
     $data[TOOLSET_EDIT_LAST] = time();
     /**
      * set last edit author
      */
     $data[WPCF_AUTHOR] = get_current_user_id();
     /**
      * add builid in
      */
     if ($data['_builtin'] && !isset($protected_data_check[$data['slug']])) {
         $protected_data_check[$data['slug']] = array();
     }
     // Merging protected data
     $custom_types[$post_type] = array_merge($protected_data_check, $data);
     update_option(WPCF_OPTION_NAME_CUSTOM_TYPES, $custom_types);
     // WPML register strings
     if (!$data['_builtin']) {
         wpcf_custom_types_register_translation($post_type, $data);
     }
     // success message
     $msg = $update ? __('Post Type saved.', 'wpcf') : __('New Post Type created.', 'wpcf');
     wpcf_admin_message_store($msg, 'updated notice notice-success is-dismissible');
     flush_rewrite_rules();
     if (!$data['_builtin']) {
         do_action('wpcf_custom_types_save', $data);
     }
     // Redirect
     wp_safe_redirect(esc_url_raw(add_query_arg(array('page' => 'wpcf-edit-type', $this->get_id => $post_type, 'wpcf-message' => 'view', 'flush' => '1'), admin_url('admin.php'))));
     die;
 }
 /**
  * Summary.
  *
  * Description.
  *
  * @since x.x.x
  * @access (for functions: only use if private)
  *
  * @see Function/method/class relied on
  * @link URL
  * @global type $varname Description.
  * @global type $varname Description.
  *
  * @param type $var Description.
  * @param type $var Optional. Description.
  * @return type Description.
  */
 private function save()
 {
     if (!isset($_POST['ct'])) {
         return false;
     }
     $data = $_POST['ct'];
     $update = false;
     // Sanitize data
     if (isset($data[$this->get_id])) {
         $update = true;
         $data[$this->get_id] = sanitize_title($data[$this->get_id]);
     }
     if (isset($data['slug'])) {
         $data['slug'] = sanitize_title($data['slug']);
     }
     if (isset($data['rewrite']['slug'])) {
         $data['rewrite']['slug'] = remove_accents($data['rewrite']['slug']);
         $data['rewrite']['slug'] = strtolower($data['rewrite']['slug']);
         $data['rewrite']['slug'] = trim($data['rewrite']['slug']);
     }
     // Set tax name
     $tax = '';
     if (!empty($data['slug'])) {
         $tax = $data['slug'];
     } else {
         if (!empty($data[$this->get_id])) {
             $tax = $data[$this->get_id];
         } else {
             if (!empty($data['labels']['singular_name'])) {
                 $tax = sanitize_title($data['labels']['singular_name']);
             }
         }
     }
     if (empty($tax)) {
         wpcf_admin_message(__('Please set taxonomy name', 'wpcf'), 'error');
         return false;
     }
     if (empty($data['labels']['singular_name'])) {
         $data['labels']['singular_name'] = $tax;
     }
     $data['slug'] = $tax;
     $taxonomies = $this->taxonomies->get();
     /**
      * is built-in?
      */
     $tax_is_built_in = wpcf_is_builtin_taxonomy($tax);
     // Check reserved name
     $reserved = wpcf_is_reserved_name($tax, 'taxonomy') && !$tax_is_built_in;
     if (is_wp_error($reserved)) {
         wpcf_admin_message($reserved->get_error_message(), 'error');
         return false;
     }
     // Check if exists
     if ($update && !array_key_exists($data[$this->get_id], $taxonomies)) {
         wpcf_admin_message(__("Taxonomy do not exist", 'wpcf'), 'error');
         return false;
     }
     // Check overwriting
     if (!$update && array_key_exists($tax, $taxonomies)) {
         /**
          * set last edit author
          */
         $data[WPCF_AUTHOR] = get_current_user_id();
         wpcf_admin_message(__('Taxonomy already exists', 'wpcf'), 'error');
         return false;
     }
     // Check if our tax overwrites some tax outside
     $tax_exists = get_taxonomy($tax);
     if (!$update && !empty($tax_exists)) {
         wpcf_admin_message(__('Taxonomy already exists', 'wpcf'), 'error');
         return false;
     }
     // Check if renaming
     if (!$tax_is_built_in && $update && $data[$this->get_id] != $tax) {
         global $wpdb;
         $wpdb->update($wpdb->term_taxonomy, array('taxonomy' => esc_sql($tax)), array('taxonomy' => esc_sql($data[$this->get_id])), array('%s'), array('%s'));
         // Sync action
         do_action('wpcf_taxonomy_renamed', $tax, $data[$this->get_id]);
         // Delete old type
         unset($taxonomies[$data[$this->get_id]]);
     }
     // Check if active
     if (isset($taxonomies[$tax]['disabled'])) {
         $data['disabled'] = $taxonomies[$tax]['disabled'];
     }
     /**
      * Sync with post types
      */
     $post_types = get_option(WPCF_OPTION_NAME_CUSTOM_TYPES, array());
     foreach ($post_types as $id => $type) {
         if (!empty($data['supports']) && array_key_exists($id, $data['supports'])) {
             if (empty($post_types[$id]['taxonomies'][$data['slug']])) {
                 $post_types[$id][TOOLSET_EDIT_LAST] = time();
             }
             $post_types[$id]['taxonomies'][$data['slug']] = 1;
         } else {
             if (!empty($post_types[$id]['taxonomies'][$data['slug']])) {
                 $post_types[$id][TOOLSET_EDIT_LAST] = time();
             }
             unset($post_types[$id]['taxonomies'][$data['slug']]);
         }
     }
     update_option(WPCF_OPTION_NAME_CUSTOM_TYPES, $post_types);
     /**
      * fix built-in
      */
     if ($tax_is_built_in) {
         $data['_builtin'] = true;
         unset($data['icon']);
         // make sure default labels are used for the built-in taxonomies
         // for the case a smart user enables disabled="disabled" inputs
         $data['labels'] = $taxonomies[$tax]['labels'];
         unset($data['wpcf-tax']);
     }
     $taxonomies[$tax] = $data;
     $taxonomies[$tax][TOOLSET_EDIT_LAST] = time();
     // set last edit author
     $taxonomies[$tax][WPCF_AUTHOR] = get_current_user_id();
     foreach ($taxonomies as $id => $taxonomy) {
         // make sure "supports" field is saved for ALL taxonomies
         if (!isset($taxonomy['supports']) && isset($taxonomy['object_type'])) {
             if (!empty($taxonomy['object_type'])) {
                 foreach ($taxonomy['object_type'] as $supported_post) {
                     $taxonomy['supports'][$supported_post] = 1;
                 }
             }
         }
         // make sure "slug" field is set
         if (!isset($taxonomy['slug'])) {
             $taxonomy['slug'] = isset($taxonomy['name']) ? $taxonomy['name'] : $id;
         }
         // make sure "name" field is set
         if (!isset($taxonomy['name'])) {
             $taxonomy['name'] = isset($taxonomy['slug ']);
         }
         // make sure "supports" field is set
         if (!isset($taxonomy['supports'])) {
             $taxonomy['supports'] = array();
         }
         $taxonomies[$id] = $taxonomy;
     }
     /**
      * save
      */
     update_option(WPCF_OPTION_NAME_CUSTOM_TAXONOMIES, $taxonomies);
     // WPML register strings
     wpcf_custom_taxonimies_register_translation($tax, $data);
     if (wpcf_is_client()) {
         $msg = $update ? __('Taxonomy saved.', 'wpcf') : __('New Taxonomy created.', 'wpcf');
         wpcf_admin_message_store($msg, 'updated notice notice-success is-dismissible');
     } else {
         wpcf_admin_message_store(apply_filters('types_message_custom_taxonomy_saved', __('Taxonomy saved.', 'wpcf'), $data, $update), 'custom');
     }
     // Flush rewrite rules
     flush_rewrite_rules();
     // Redirect
     wp_safe_redirect(esc_url_raw(add_query_arg(array('page' => 'wpcf-edit-tax', $this->get_id => $tax, 'wpcf-message' => get_user_option('types-modal')), admin_url('admin.php'))));
     die;
 }
/**
 * Submit function
 */
function wpcf_admin_custom_types_form_submit($form)
{
    global $wpcf;
    if (!isset($_POST['ct'])) {
        return false;
    }
    $data = $_POST['ct'];
    $update = false;
    // Sanitize data
    if (isset($data['wpcf-post-type'])) {
        $update = true;
        $data['wpcf-post-type'] = sanitize_title($data['wpcf-post-type']);
    } else {
        $data['wpcf-post-type'] = null;
    }
    if (isset($data['slug'])) {
        $data['slug'] = sanitize_title($data['slug']);
    } else {
        $data['slug'] = null;
    }
    if (isset($data['rewrite']['slug'])) {
        $data['rewrite']['slug'] = remove_accents($data['rewrite']['slug']);
        $data['rewrite']['slug'] = strtolower($data['rewrite']['slug']);
        $data['rewrite']['slug'] = trim($data['rewrite']['slug']);
    }
    // Set post type name
    $post_type = null;
    if (!empty($data['slug'])) {
        $post_type = $data['slug'];
    } elseif (!empty($data['wpcf-post-type'])) {
        $post_type = $data['wpcf-post-type'];
    } elseif (!empty($data['labels']['singular_name'])) {
        $post_type = sanitize_title($data['labels']['singular_name']);
    }
    if (empty($post_type)) {
        wpcf_admin_message(__('Please set post type name', 'wpcf'), 'error');
        return false;
    }
    $data['slug'] = $post_type;
    $custom_types = get_option('wpcf-custom-types', array());
    // Check reserved name
    $reserved = wpcf_is_reserved_name($post_type, 'post_type');
    if (is_wp_error($reserved)) {
        wpcf_admin_message($reserved->get_error_message(), 'error');
        return false;
    }
    // Check overwriting
    if ((!array_key_exists('wpcf-post-type', $data) || $data['wpcf-post-type'] != $post_type) && array_key_exists($post_type, $custom_types)) {
        wpcf_admin_message(__('Custom post type already exists', 'wpcf'), 'error');
        return false;
    }
    /*
     * Since Types 1.2
     * We do not allow plural and singular names to be same.
     */
    if ($wpcf->post_types->check_singular_plural_match($data)) {
        wpcf_admin_message($wpcf->post_types->message('warning_singular_plural_match'), 'error');
        return false;
    }
    // Check if renaming then rename all post entries and delete old type
    if (!empty($data['wpcf-post-type']) && $data['wpcf-post-type'] != $post_type) {
        global $wpdb;
        $wpdb->update($wpdb->posts, array('post_type' => $post_type), array('post_type' => $data['wpcf-post-type']), array('%s'), array('%s'));
        /**
         * update post meta "_wp_types_group_post_types"
         */
        $sql = sprintf('select meta_id, meta_value from %s where meta_key = \'%s\'', $wpdb->postmeta, '_wp_types_group_post_types');
        $all_meta = $wpdb->get_results($sql, OBJECT_K);
        $re = sprintf('/,%s,/', $data['wpcf-post-type']);
        foreach ($all_meta as $meta) {
            if (!preg_match($re, $meta->meta_value)) {
                continue;
            }
            $wpdb->update($wpdb->postmeta, array('meta_value' => preg_replace($re, ',' . $post_type . ',', $meta->meta_value)), array('meta_id' => $meta->meta_id), array('%s'), array('%d'));
        }
        /**
         * update _wpcf_belongs_{$data['wpcf-post-type']}_id
         */
        $wpdb->update($wpdb->postmeta, array('meta_key' => sprintf('_wpcf_belongs_%s_id', $post_type)), array('meta_key' => sprintf('_wpcf_belongs_%s_id', $data['wpcf-post-type'])), array('%s'), array('%s'));
        /**
         * update options "wpv_options"
         */
        $wpv_options = get_option('wpv_options', true);
        if (is_array($wpv_options)) {
            $re = sprintf('/(views_template_(archive_)?for_)%s/', $data['wpcf-post-type']);
            foreach ($wpv_options as $key => $value) {
                if (!preg_match($re, $key)) {
                    continue;
                }
                unset($wpv_options[$key]);
                $key = preg_replace($re, "\$1" . $post_type, $key);
                $wpv_options[$key] = $value;
            }
            update_option('wpv_options', $wpv_options);
        }
        /**
         * update option "wpcf-custom-taxonomies"
         */
        $wpcf_custom_taxonomies = get_option('wpcf-custom-taxonomies', true);
        if (is_array($wpcf_custom_taxonomies)) {
            $update_wpcf_custom_taxonomies = false;
            foreach ($wpcf_custom_taxonomies as $key => $value) {
                if (array_key_exists('supports', $value) && array_key_exists($data['wpcf-post-type'], $value['supports'])) {
                    unset($wpcf_custom_taxonomies[$key]['supports'][$data['wpcf-post-type']]);
                    $update_wpcf_custom_taxonomies = true;
                }
            }
            if ($update_wpcf_custom_taxonomies) {
                update_option('wpcf-custom-taxonomies', $wpcf_custom_taxonomies);
            }
        }
        // Sync action
        do_action('wpcf_post_type_renamed', $post_type, $data['wpcf-post-type']);
        // Set protected data
        $protected_data_check = $custom_types[$data['wpcf-post-type']];
        // Delete old type
        unset($custom_types[$data['wpcf-post-type']]);
        $data['wpcf-post-type'] = $post_type;
    } else {
        // Set protected data
        $protected_data_check = !empty($custom_types[$post_type]) ? $custom_types[$post_type] : array();
    }
    // Check if active
    if (isset($custom_types[$post_type]['disabled'])) {
        $data['disabled'] = $custom_types[$post_type]['disabled'];
    }
    // Sync taxes with custom taxes
    if (!empty($data['taxonomies'])) {
        $taxes = get_option('wpcf-custom-taxonomies', array());
        foreach ($taxes as $id => $tax) {
            if (array_key_exists($id, $data['taxonomies'])) {
                $taxes[$id]['supports'][$data['slug']] = 1;
            } else {
                unset($taxes[$id]['supports'][$data['slug']]);
            }
        }
        update_option('wpcf-custom-taxonomies', $taxes);
    }
    // Preserve protected data
    foreach ($protected_data_check as $key => $value) {
        if (strpos($key, '_') !== 0) {
            unset($protected_data_check[$key]);
        }
    }
    // Merging protected data
    $custom_types[$post_type] = array_merge($protected_data_check, $data);
    update_option('wpcf-custom-types', $custom_types);
    // WPML register strings
    wpcf_custom_types_register_translation($post_type, $data);
    wpcf_admin_message_store(apply_filters('types_message_custom_post_type_saved', __('Custom post type saved', 'wpcf'), $data, $update), 'custom');
    // Flush rewrite rules
    flush_rewrite_rules();
    do_action('wpcf_custom_types_save', $data);
    // Redirect
    wp_redirect(add_query_arg(array('page' => 'wpcf-edit-type', 'wpcf-post-type' => $post_type, 'wpcf-rewrite' => 1, 'wpcf-message' => 'view'), admin_url('admin.php')));
    die;
}
/**
 * Submit function
 *
 * @global object $wpdb
 *
 */
function wpcf_admin_custom_taxonomies_form_submit($form)
{
    if (!isset($_POST['ct'])) {
        return false;
    }
    $data = $_POST['ct'];
    $update = false;
    // Sanitize data
    if (isset($data['wpcf-tax'])) {
        $update = true;
        $data['wpcf-tax'] = sanitize_title($data['wpcf-tax']);
    }
    if (isset($data['slug'])) {
        $data['slug'] = sanitize_title($data['slug']);
    }
    if (isset($data['rewrite']['slug'])) {
        $data['rewrite']['slug'] = remove_accents($data['rewrite']['slug']);
        $data['rewrite']['slug'] = strtolower($data['rewrite']['slug']);
        $data['rewrite']['slug'] = trim($data['rewrite']['slug']);
    }
    // Set tax name
    $tax = '';
    if (!empty($data['slug'])) {
        $tax = $data['slug'];
    } else {
        if (!empty($data['wpcf-tax'])) {
            $tax = $data['wpcf-tax'];
        } else {
            if (!empty($data['labels']['singular_name'])) {
                $tax = sanitize_title($data['labels']['singular_name']);
            }
        }
    }
    if (empty($tax)) {
        wpcf_admin_message(__('Please set taxonomy name', 'wpcf'), 'error');
        return false;
    }
    if (empty($data['labels']['singular_name'])) {
        $data['labels']['singular_name'] = $tax;
    }
    $data['slug'] = $tax;
    $custom_taxonomies = get_option('wpcf-custom-taxonomies', array());
    // Check reserved name
    $reserved = wpcf_is_reserved_name($tax, 'taxonomy');
    if (is_wp_error($reserved)) {
        wpcf_admin_message($reserved->get_error_message(), 'error');
        return false;
    }
    // Check if exists
    if ($update && !array_key_exists($data['wpcf-tax'], $custom_taxonomies)) {
        wpcf_admin_message(__("Custom taxonomy do not exist", 'wpcf'), 'error');
        return false;
    }
    // Check overwriting
    if (!$update && array_key_exists($tax, $custom_taxonomies)) {
        wpcf_admin_message(__('Custom taxonomy already exists', 'wpcf'), 'error');
        return false;
    }
    // Check if our tax overwrites some tax outside
    $tax_exists = get_taxonomy($tax);
    if (!$update && !empty($tax_exists)) {
        wpcf_admin_message(__('Taxonomy already exists', 'wpcf'), 'error');
        return false;
    }
    // Check if renaming
    if ($update && $data['wpcf-tax'] != $tax) {
        global $wpdb;
        $wpdb->update($wpdb->term_taxonomy, array('taxonomy' => $tax), array('taxonomy' => $data['wpcf-tax']), array('%s'), array('%s'));
        // Sync action
        do_action('wpcf_taxonomy_renamed', $tax, $data['wpcf-tax']);
        // Delete old type
        unset($custom_taxonomies[$data['wpcf-tax']]);
    }
    // Check if active
    if (isset($custom_taxonomies[$tax]['disabled'])) {
        $data['disabled'] = $custom_taxonomies[$tax]['disabled'];
    }
    // Sync with post types
    if (!empty($data['supports'])) {
        $post_types = get_option('wpcf-custom-types', array());
        foreach ($post_types as $id => $type) {
            if (array_key_exists($id, $data['supports'])) {
                if (empty($post_types[$id]['taxonomies'][$data['slug']])) {
                    $post_types[$id][TOOLSET_EDIT_LAST] = time();
                }
                $post_types[$id]['taxonomies'][$data['slug']] = 1;
            } else {
                if (!empty($post_types[$id]['taxonomies'][$data['slug']])) {
                    $post_types[$id][TOOLSET_EDIT_LAST] = time();
                }
                unset($post_types[$id]['taxonomies'][$data['slug']]);
            }
        }
        update_option('wpcf-custom-types', $post_types);
    }
    $custom_taxonomies[$tax] = $data;
    $custom_taxonomies[$tax][TOOLSET_EDIT_LAST] = time();
    update_option('wpcf-custom-taxonomies', $custom_taxonomies);
    // WPML register strings
    wpcf_custom_taxonimies_register_translation($tax, $data);
    wpcf_admin_message_store(apply_filters('types_message_custom_taxonomy_saved', __('Custom taxonomy saved', 'wpcf'), $data, $update), 'custom');
    // Flush rewrite rules
    flush_rewrite_rules();
    // Redirect
    wp_redirect(add_query_arg(array('page' => 'wpcf-edit-tax', 'wpcf-tax' => $tax, 'wpcf-rewrite' => 1, 'wpcf-message' => get_user_option('types-modal')), admin_url('admin.php')));
    die;
}