/**
 * Shows the created attributes and lets you add new ones.
 * The added attributes are stored in the database and can be used for layered navigation.
 *
 * @since    1.0
 * @usedby    jigoshop_admin_menu2()
 */
function jigoshop_attributes()
{
    if (isset($_GET['edit']) && $_GET['edit'] > 0) {
        jigoshop_edit_attribute();
    } else {
        jigoshop_add_attribute();
    }
}
/**
 * Shows the created attributes and lets you add new ones.
 * The added attributes are stored in the database and can be used for layered navigation.
 *
 * @since 		1.0
 * @usedby 		jigoshop_admin_menu2()
 */
function jigoshop_attributes()
{
    global $wpdb;
    if (isset($_POST['add_new_attribute']) && $_POST['add_new_attribute']) {
        $attribute_name = (string) $_POST['attribute_name'];
        $attribute_type = (string) $_POST['attribute_type'];
        if (isset($_POST['show-on-product-page']) && $_POST['show-on-product-page']) {
            $product_page = 1;
        } else {
            $product_page = 0;
        }
        if ($attribute_name && $attribute_type && !taxonomy_exists('product_attribute_' . strtolower(sanitize_title($attribute_name)))) {
            $wpdb->insert($wpdb->prefix . "jigoshop_attribute_taxonomies", array('attribute_name' => $attribute_name, 'attribute_type' => $attribute_type), array('%s', '%s'));
            update_option('jigowatt_update_rewrite_rules', '1');
            wp_safe_redirect(get_admin_url() . 'admin.php?page=attributes');
            exit;
        }
    } elseif (isset($_POST['save_attribute']) && $_POST['save_attribute'] && isset($_GET['edit'])) {
        $edit = absint($_GET['edit']);
        if ($edit > 0) {
            $attribute_type = $_POST['attribute_type'];
            $wpdb->update($wpdb->prefix . "jigoshop_attribute_taxonomies", array('attribute_type' => $attribute_type), array('attribute_id' => $_GET['edit']), array('%s'));
        }
        wp_safe_redirect(get_admin_url() . 'admin.php?page=attributes');
        exit;
    } elseif (isset($_GET['delete'])) {
        $delete = absint($_GET['delete']);
        if ($delete > 0) {
            $att_name = $wpdb->get_var("SELECT attribute_name FROM " . $wpdb->prefix . "jigoshop_attribute_taxonomies WHERE attribute_id = '{$delete}'");
            if ($att_name && $wpdb->query("DELETE FROM " . $wpdb->prefix . "jigoshop_attribute_taxonomies WHERE attribute_id = '{$delete}'")) {
                $taxonomy = 'product_attribute_' . strtolower(sanitize_title($att_name));
                if (taxonomy_exists($taxonomy)) {
                    $terms = get_terms($taxonomy, 'orderby=name&hide_empty=0');
                    foreach ($terms as $term) {
                        wp_delete_term($term->term_id, $taxonomy);
                    }
                }
                wp_safe_redirect(get_admin_url() . 'admin.php?page=attributes');
                exit;
            }
        }
    }
    if (isset($_GET['edit']) && $_GET['edit'] > 0) {
        jigoshop_edit_attribute();
    } else {
        jigoshop_add_attribute();
    }
}