/**
 * 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;
}
 /**
  * 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;
 }
Esempio n. 3
0
/**
 * Bulk translation. 
 */
function wpcf_admin_bulk_string_translation()
{
    if (!function_exists('icl_register_string')) {
        return false;
    }
    require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields.php';
    require_once WPCF_EMBEDDED_INC_ABSPATH . '/custom-types.php';
    require_once WPCF_INC_ABSPATH . '/custom-types-form.php';
    require_once WPCF_EMBEDDED_INC_ABSPATH . '/custom-taxonomies.php';
    require_once WPCF_INC_ABSPATH . '/custom-taxonomies-form.php';
    // Register groups
    $groups = wpcf_admin_fields_get_groups();
    foreach ($groups as $group_id => $group) {
        wpcf_translate_register_string('plugin Types', 'group ' . $group_id . ' name', $group['name']);
        if (isset($group['description'])) {
            wpcf_translate_register_string('plugin Types', 'group ' . $group_id . ' description', $group['description']);
        }
    }
    // Register fields
    $fields = wpcf_admin_fields_get_fields();
    foreach ($fields as $field_id => $field) {
        wpcf_translate_register_string('plugin Types', 'field ' . $field_id . ' name', $field['name']);
        if (isset($field['description'])) {
            wpcf_translate_register_string('plugin Types', 'field ' . $field_id . ' description', $field['description']);
        }
        // For radios or select
        if (!empty($field['data']['options'])) {
            foreach ($field['data']['options'] as $name => $option) {
                if ($name == 'default') {
                    continue;
                }
                if (isset($option['title'])) {
                    wpcf_translate_register_string('plugin Types', 'field ' . $field_id . ' option ' . $name . ' title', $option['title']);
                }
                if (isset($option['value'])) {
                    wpcf_translate_register_string('plugin Types', 'field ' . $field_id . ' option ' . $name . ' value', $option['value']);
                }
                if (isset($option['display_value'])) {
                    wpcf_translate_register_string('plugin Types', 'field ' . $field_id . ' option ' . $name . ' display value', $option['display_value']);
                }
            }
        }
        if ($field['type'] == 'checkbox' && (isset($field['set_value']) && $field['set_value'] != '1')) {
            // we need to translate the check box value to store
            wpcf_translate_register_string('plugin Types', 'field ' . $field_id . ' checkbox value', $field['set_value']);
        }
        if ($field['type'] == 'checkbox' && !empty($field['display_value_selected'])) {
            // we need to translate the check box value to store
            wpcf_translate_register_string('plugin Types', 'field ' . $field_id . ' checkbox value selected', $field['display_value_selected']);
        }
        if ($field['type'] == 'checkbox' && !empty($field['display_value_not_selected'])) {
            // we need to translate the check box value to store
            wpcf_translate_register_string('plugin Types', 'field ' . $field_id . ' checkbox value not selected', $field['display_value_not_selected']);
        }
        // Validation message
        if (!empty($field['data']['validate'])) {
            foreach ($field['data']['validate'] as $method => $validation) {
                if (!empty($validation['message'])) {
                    // Skip if it's same as default
                    $default_message = wpcf_admin_validation_messages($method);
                    if ($validation['message'] != $default_message) {
                        wpcf_translate_register_string('plugin Types', 'field ' . $field_id . ' validation message ' . $method, $validation['message']);
                    }
                }
            }
        }
    }
    // Register types
    $custom_types = get_option('wpcf-custom-types', array());
    foreach ($custom_types as $post_type => $data) {
        wpcf_custom_types_register_translation($post_type, $data);
    }
    // Register taxonomies
    $custom_taxonomies = get_option('wpcf-custom-taxonomies', array());
    foreach ($custom_taxonomies as $taxonomy => $data) {
        wpcf_custom_taxonimies_register_translation($taxonomy, $data);
    }
}
/**
 * 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;
}