/**
* wpsc_admin_submit_product function
* @internal Was going to completely refactor sanitise forms and wpsc_insert_product, but they are also used by the import system
 * which I'm not really familiar with...so I'm not touching them :)  Erring on the side of redundancy and caution I'll just
 * refactor this to do the job.
* @return nothing
*/
function wpsc_admin_submit_product($post_ID, $post)
{
    global $current_screen, $wpdb;
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE || empty($current_screen) || $current_screen->id != 'wpsc-product' || $post->post_type != 'wpsc-product' || empty($_POST['meta'])) {
        return $post_ID;
    }
    //Type-casting ( not so much sanitization, which would be good to do )
    $post_data = $_POST;
    $product_id = $post_ID;
    $post_data['additional_description'] = isset($post_data['additional_description']) ? $post_data['additional_description'] : '';
    $post_meta['meta'] = (array) $_POST['meta'];
    if (isset($post_data['meta']['_wpsc_price'])) {
        $post_data['meta']['_wpsc_price'] = abs((double) str_replace(',', '', $post_data['meta']['_wpsc_price']));
    }
    if (isset($post_data['meta']['_wpsc_special_price'])) {
        $post_data['meta']['_wpsc_special_price'] = abs((double) str_replace(',', '', $post_data['meta']['_wpsc_special_price']));
    }
    if ($post_data['meta']['_wpsc_sku'] == __('N/A', 'wpsc')) {
        $post_data['meta']['_wpsc_sku'] = '';
    }
    if (isset($post_data['meta']['_wpsc_is_donation'])) {
        $post_data['meta']['_wpsc_is_donation'] = 1;
    } else {
        $post_data['meta']['_wpsc_is_donation'] = 0;
    }
    if (!isset($post_data['meta']['_wpsc_limited_stock'])) {
        $post_data['meta']['_wpsc_stock'] = false;
    } else {
        $post_data['meta']['_wpsc_stock'] = isset($post_data['meta']['_wpsc_stock']) ? (int) $post_data['meta']['_wpsc_stock'] : 0;
    }
    unset($post_data['meta']['_wpsc_limited_stock']);
    if (!isset($post_data['meta']['_wpsc_product_metadata']['unpublish_when_none_left'])) {
        $post_data['meta']['_wpsc_product_metadata']['unpublish_when_none_left'] = '';
    }
    if (!isset($post_data['quantity_limited'])) {
        $post_data['quantity_limited'] = '';
    }
    if (!isset($post_data['special'])) {
        $post_data['special'] = '';
    }
    if (!isset($post_data['meta']['_wpsc_product_metadata']['no_shipping'])) {
        $post_data['meta']['_wpsc_product_metadata']['no_shipping'] = '';
    }
    $post_data['meta']['_wpsc_product_metadata']['unpublish_when_none_left'] = (int) (bool) $post_data['meta']['_wpsc_product_metadata']['unpublish_when_none_left'];
    $post_data['meta']['_wpsc_product_metadata']['quantity_limited'] = (int) (bool) $post_data['quantity_limited'];
    $post_data['meta']['_wpsc_product_metadata']['special'] = (int) (bool) $post_data['special'];
    $post_data['meta']['_wpsc_product_metadata']['no_shipping'] = (int) (bool) $post_data['meta']['_wpsc_product_metadata']['no_shipping'];
    // Product Weight
    if (!isset($post_data['meta']['_wpsc_product_metadata']['display_weight_as'])) {
        $post_data['meta']['_wpsc_product_metadata']['display_weight_as'] = '';
    }
    if (!isset($post_data['meta']['_wpsc_product_metadata']['display_weight_as'])) {
        $post_data['meta']['_wpsc_product_metadata']['display_weight_as'] = '';
    }
    $weight = wpsc_convert_weight($post_data['meta']['_wpsc_product_metadata']['weight'], $post_data['meta']['_wpsc_product_metadata']['weight_unit'], "pound", true);
    $post_data['meta']['_wpsc_product_metadata']['weight'] = (double) $weight;
    $post_data['meta']['_wpsc_product_metadata']['display_weight_as'] = $post_data['meta']['_wpsc_product_metadata']['weight_unit'];
    // table rate price
    $post_data['meta']['_wpsc_product_metadata']['table_rate_price'] = isset($post_data['table_rate_price']) ? $post_data['table_rate_price'] : array();
    // if table_rate_price is unticked, wipe the table rate prices
    if (empty($post_data['table_rate_price']['state'])) {
        $post_data['meta']['_wpsc_product_metadata']['table_rate_price']['table_price'] = array();
        $post_data['meta']['_wpsc_product_metadata']['table_rate_price']['quantity'] = array();
    }
    if (!empty($post_data['meta']['_wpsc_product_metadata']['table_rate_price']['table_price'])) {
        foreach ((array) $post_data['meta']['_wpsc_product_metadata']['table_rate_price']['table_price'] as $key => $value) {
            if (empty($value)) {
                unset($post_data['meta']['_wpsc_product_metadata']['table_rate_price']['table_price'][$key]);
                unset($post_data['meta']['_wpsc_product_metadata']['table_rate_price']['quantity'][$key]);
            }
        }
    }
    $post_data['meta']['_wpsc_product_metadata']['shipping']['local'] = (double) $post_data['meta']['_wpsc_product_metadata']['shipping']['local'];
    $post_data['meta']['_wpsc_product_metadata']['shipping']['international'] = (double) $post_data['meta']['_wpsc_product_metadata']['shipping']['international'];
    // Advanced Options
    $post_data['meta']['_wpsc_product_metadata']['engraved'] = (int) (bool) $post_data['meta']['_wpsc_product_metadata']['engraved'];
    $post_data['meta']['_wpsc_product_metadata']['can_have_uploaded_image'] = (int) (bool) $post_data['meta']['_wpsc_product_metadata']['can_have_uploaded_image'];
    if (!isset($post_data['meta']['_wpsc_product_metadata']['google_prohibited'])) {
        $post_data['meta']['_wpsc_product_metadata']['google_prohibited'] = '';
    }
    $post_data['meta']['_wpsc_product_metadata']['google_prohibited'] = (int) (bool) $post_data['meta']['_wpsc_product_metadata']['google_prohibited'];
    $post_data['meta']['_wpsc_product_metadata']['enable_comments'] = $post_data['meta']['_wpsc_product_metadata']['enable_comments'];
    $post_data['meta']['_wpsc_product_metadata']['merchant_notes'] = $post_data['meta']['_wpsc_product_metadata']['merchant_notes'];
    $post_data['files'] = $_FILES;
    if (isset($post_data['post_title']) && $post_data['post_title'] != '') {
        $product_columns = array('name' => '', 'description' => '', 'additional_description' => '', 'price' => null, 'weight' => null, 'weight_unit' => '', 'pnp' => null, 'international_pnp' => null, 'file' => null, 'image' => '0', 'quantity_limited' => '', 'quantity' => null, 'special' => null, 'special_price' => null, 'display_frontpage' => null, 'notax' => null, 'publish' => null, 'active' => null, 'donation' => null, 'no_shipping' => null, 'thumbnail_image' => null, 'thumbnail_state' => null);
        foreach ($product_columns as $column => $default) {
            if (!isset($post_data[$column])) {
                $post_data[$column] = '';
            }
            if ($post_data[$column] !== null) {
                $update_values[$column] = stripslashes($post_data[$column]);
            } else {
                if ($update != true && $default !== null) {
                    $update_values[$column] = stripslashes($default);
                }
            }
        }
        // if we succeed, we can do further editing (todo - if_wp_error)
        // if we have no categories selected, assign one.
        if (isset($post_data['tax_input']['wpsc_product_category']) && count($post_data['tax_input']['wpsc_product_category']) == 1 && $post_data['tax_input']['wpsc_product_category'][0] == 0) {
            $post_data['tax_input']['wpsc_product_category'][1] = wpsc_add_product_category_default($product_id);
        }
        // and the meta
        wpsc_update_product_meta($product_id, $post_data['meta']);
        // and the custom meta
        wpsc_update_custom_meta($product_id, $post_data);
        // sort out the variations
        wpsc_edit_product_variations($product_id, $post_data);
        //and the alt currency
        if (!empty($post_data['newCurrency'])) {
            foreach ((array) $post_data['newCurrency'] as $key => $value) {
                wpsc_update_alt_product_currency($product_id, $value, $post_data['newCurrPrice'][$key]);
            }
        }
        if ($post_data['files']['file']['tmp_name'] != '') {
            wpsc_item_process_file($product_id, $post_data['files']['file']);
        } else {
            if (!isset($post_data['select_product_file'])) {
                $post_data['select_product_file'] = null;
            }
            wpsc_item_reassign_file($product_id, $post_data['select_product_file']);
        }
        if (isset($post_data['files']['preview_file']['tmp_name']) && $post_data['files']['preview_file']['tmp_name'] != '') {
            wpsc_item_add_preview_file($product_id, $post_data['files']['preview_file']);
        }
        do_action('wpsc_edit_product', $product_id);
        wpsc_ping();
    }
    return $product_id;
}
/**
* wpsc_admin_submit_product function
* @internal Was going to completely refactor sanitise forms and wpsc_insert_product, but they are also used by the import system
 * which I'm not really familiar with...so I'm not touching them :)  Erring on the side of redundancy and caution I'll just
 * refactor this to do the job.
* @return nothing
*/
function wpsc_admin_submit_product($post_ID, $post)
{
    if (!is_admin()) {
        return;
    }
    global $wpdb;
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE || $post->post_type != 'wpsc-product') {
        return;
    }
    //Type-casting ( not so much sanitization, which would be good to do )
    $post_data = stripslashes_deep($_POST);
    $product_id = $post_ID;
    $post_data['additional_description'] = isset($post_data['additional_description']) ? $post_data['additional_description'] : '';
    if (!isset($post_data['meta']) && isset($_POST['meta'])) {
        $post_data['meta'] = (array) $_POST['meta'];
    }
    if (isset($post_data['meta']['_wpsc_price'])) {
        $post_data['meta']['_wpsc_price'] = wpsc_string_to_float($post_data['meta']['_wpsc_price']);
    }
    if (isset($post_data['meta']['_wpsc_special_price'])) {
        $post_data['meta']['_wpsc_special_price'] = wpsc_string_to_float($post_data['meta']['_wpsc_special_price']);
    }
    if (isset($post_data['meta']['_wpsc_sku']) && $post_data['meta']['_wpsc_sku'] == __('N/A', 'wpsc')) {
        $post_data['meta']['_wpsc_sku'] = '';
    }
    if (isset($post_data['meta']['_wpsc_is_donation'])) {
        $post_data['meta']['_wpsc_is_donation'] = 1;
    } else {
        $post_data['meta']['_wpsc_is_donation'] = 0;
    }
    if (!isset($post_data['meta']['_wpsc_limited_stock'])) {
        $post_data['meta']['_wpsc_stock'] = false;
    } else {
        $post_data['meta']['_wpsc_stock'] = isset($post_data['meta']['_wpsc_stock']) ? (int) $post_data['meta']['_wpsc_stock'] : 0;
    }
    unset($post_data['meta']['_wpsc_limited_stock']);
    if (!isset($post_data['meta']['_wpsc_product_metadata']['notify_when_none_left'])) {
        $post_data['meta']['_wpsc_product_metadata']['notify_when_none_left'] = 0;
    }
    if (!isset($post_data['meta']['_wpsc_product_metadata']['unpublish_when_none_left'])) {
        $post_data['meta']['_wpsc_product_metadata']['unpublish_when_none_left'] = '';
    }
    if (!isset($post_data['quantity_limited'])) {
        $post_data['quantity_limited'] = '';
    }
    if (!isset($post_data['special'])) {
        $post_data['special'] = '';
    }
    if (!isset($post_data['meta']['_wpsc_product_metadata']['no_shipping'])) {
        $post_data['meta']['_wpsc_product_metadata']['no_shipping'] = '';
    }
    $post_data['meta']['_wpsc_product_metadata']['notify_when_none_left'] = (int) (bool) $post_data['meta']['_wpsc_product_metadata']['notify_when_none_left'];
    $post_data['meta']['_wpsc_product_metadata']['unpublish_when_none_left'] = (int) (bool) $post_data['meta']['_wpsc_product_metadata']['unpublish_when_none_left'];
    $post_data['meta']['_wpsc_product_metadata']['quantity_limited'] = (int) (bool) $post_data['quantity_limited'];
    $post_data['meta']['_wpsc_product_metadata']['special'] = (int) (bool) $post_data['special'];
    $post_data['meta']['_wpsc_product_metadata']['no_shipping'] = (int) (bool) $post_data['meta']['_wpsc_product_metadata']['no_shipping'];
    // Product Weight
    if (!isset($post_data['meta']['_wpsc_product_metadata']['display_weight_as'])) {
        $post_data['meta']['_wpsc_product_metadata']['display_weight_as'] = '';
    }
    if (isset($post_data['meta']['_wpsc_product_metadata']['weight'])) {
        $weight = wpsc_string_to_float($post_data['meta']['_wpsc_product_metadata']['weight']);
        $weight = wpsc_convert_weight($weight, $post_data['meta']['_wpsc_product_metadata']['weight_unit'], "pound", true);
        $post_data['meta']['_wpsc_product_metadata']['weight'] = $weight;
        $post_data['meta']['_wpsc_product_metadata']['display_weight_as'] = $post_data['meta']['_wpsc_product_metadata']['weight_unit'];
    }
    if (isset($post_data['meta']['_wpsc_product_metadata']['dimensions'])) {
        $dimensions =& $post_data['meta']['_wpsc_product_metadata']['dimensions'];
        foreach ($dimensions as $key => $value) {
            if (!in_array($key, array('height', 'width', 'length'))) {
                continue;
            }
            $dimensions[$key] = wpsc_string_to_float($value);
        }
    }
    // Update the table rate prices (quantity discounts)
    if (isset($post_data['wpsc-update-quantity-discounts']) && wp_verify_nonce($post_data['wpsc-update-quantity-discounts'], 'update-options')) {
        $post_data['meta']['_wpsc_product_metadata']['table_rate_price'] = isset($post_data['table_rate_price']) ? $post_data['table_rate_price'] : array();
        // If table_rate_price is empty, set empty table rate price arrays
        if (empty($post_data['meta']['_wpsc_product_metadata']['table_rate_price'])) {
            $post_data['meta']['_wpsc_product_metadata']['table_rate_price']['table_price'] = array();
            $post_data['meta']['_wpsc_product_metadata']['table_rate_price']['quantity'] = array();
        }
        // Remove any rates with no quantity or price
        if (!empty($post_data['meta']['_wpsc_product_metadata']['table_rate_price']['table_price'])) {
            foreach ((array) $post_data['meta']['_wpsc_product_metadata']['table_rate_price']['quantity'] as $key => $value) {
                if (empty($value)) {
                    unset($post_data['meta']['_wpsc_product_metadata']['table_rate_price']['table_price'][$key]);
                    unset($post_data['meta']['_wpsc_product_metadata']['table_rate_price']['quantity'][$key]);
                }
            }
            foreach ((array) $post_data['meta']['_wpsc_product_metadata']['table_rate_price']['table_price'] as $key => $value) {
                if (empty($value)) {
                    unset($post_data['meta']['_wpsc_product_metadata']['table_rate_price']['table_price'][$key]);
                    unset($post_data['meta']['_wpsc_product_metadata']['table_rate_price']['quantity'][$key]);
                }
            }
        }
    }
    if (isset($post_data['meta']['_wpsc_product_metadata']['shipping'])) {
        $post_data['meta']['_wpsc_product_metadata']['shipping']['local'] = wpsc_string_to_float($post_data['meta']['_wpsc_product_metadata']['shipping']['local']);
        $post_data['meta']['_wpsc_product_metadata']['shipping']['international'] = wpsc_string_to_float($post_data['meta']['_wpsc_product_metadata']['shipping']['international']);
    }
    if (!empty($post_data['meta']['_wpsc_product_metadata']['wpec_taxes_taxable_amount'])) {
        $post_data['meta']['_wpsc_product_metadata']['wpec_taxes_taxable_amount'] = wpsc_string_to_float($post_data['meta']['_wpsc_product_metadata']['wpec_taxes_taxable_amount']);
    }
    // Advanced Options
    if (isset($post_data['meta']['_wpsc_product_metadata']['engraved'])) {
        $post_data['meta']['_wpsc_product_metadata']['engraved'] = (int) (bool) $post_data['meta']['_wpsc_product_metadata']['engraved'];
    } else {
        $post_data['meta']['_wpsc_product_metadata']['engraved'] = 0;
    }
    if (isset($post_data['meta']['_wpsc_product_metadata']['can_have_uploaded_image'])) {
        $post_data['meta']['_wpsc_product_metadata']['can_have_uploaded_image'] = (int) (bool) $post_data['meta']['_wpsc_product_metadata']['can_have_uploaded_image'];
    } else {
        $post_data['meta']['_wpsc_product_metadata']['can_have_uploaded_image'] = 0;
    }
    if (!isset($post_data['meta']['_wpsc_product_metadata']['google_prohibited'])) {
        $post_data['meta']['_wpsc_product_metadata']['google_prohibited'] = '';
    }
    $post_data['meta']['_wpsc_product_metadata']['google_prohibited'] = (int) (bool) $post_data['meta']['_wpsc_product_metadata']['google_prohibited'];
    $post_data['files'] = $_FILES;
    if (isset($post_data['post_title']) && $post_data['post_title'] != '') {
        $product_columns = array('name' => '', 'description' => '', 'additional_description' => '', 'price' => null, 'weight' => null, 'weight_unit' => '', 'pnp' => null, 'international_pnp' => null, 'file' => null, 'image' => '0', 'quantity_limited' => '', 'quantity' => null, 'special' => null, 'special_price' => null, 'display_frontpage' => null, 'notax' => null, 'publish' => null, 'active' => null, 'donation' => null, 'no_shipping' => null, 'thumbnail_image' => null, 'thumbnail_state' => null);
        foreach ($product_columns as $column => $default) {
            if (!isset($post_data[$column])) {
                $post_data[$column] = '';
            }
            if ($post_data[$column] !== null) {
                $update_values[$column] = $post_data[$column];
            } else {
                if ($update != true && $default !== null) {
                    $update_values[$column] = $default;
                }
            }
        }
        // if we succeed, we can do further editing (todo - if_wp_error)
        // if we have no categories selected, assign one.
        if (isset($post_data['tax_input']['wpsc_product_category']) && count($post_data['tax_input']['wpsc_product_category']) == 1 && $post_data['tax_input']['wpsc_product_category'][0] == 0) {
            $post_data['tax_input']['wpsc_product_category'][1] = wpsc_add_product_category_default($product_id);
        }
        // and the meta
        wpsc_update_product_meta($product_id, $post_data['meta']);
        // and the custom meta
        wpsc_update_custom_meta($product_id, $post_data);
        // Update the alternative currencies
        if (isset($post_data['wpsc-update-currency-layers']) && wp_verify_nonce($post_data['wpsc-update-currency-layers'], 'update-options')) {
            // Clear currencies before re-saving to make sure deleted currencies are removed
            update_product_meta($product_id, 'currency', array());
            if (!empty($post_data['newCurrency'])) {
                foreach ((array) $post_data['newCurrency'] as $key => $value) {
                    wpsc_update_alt_product_currency($product_id, $value, $post_data['newCurrPrice'][$key]);
                }
            }
        }
        if (isset($post_data['files']['file']) && $post_data['files']['file']['tmp_name'] != '') {
            wpsc_item_process_file($product_id, $post_data['files']['file']);
        } else {
            if (!isset($post_data['select_product_file'])) {
                $post_data['select_product_file'] = null;
            }
            wpsc_item_reassign_file($product_id, $post_data['select_product_file']);
        }
        if (isset($post_data['files']['preview_file']['tmp_name']) && $post_data['files']['preview_file']['tmp_name'] != '') {
            wpsc_item_add_preview_file($product_id, $post_data['files']['preview_file']);
        }
        do_action('wpsc_edit_product', $product_id);
    }
    return $product_id;
}