/**
* 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;
}
Example #3
0
 // 	if (isset($_POST['engrave'])) {
 // 		$id = $_POST['prodid'];
 // 		$engrave = $_POST['engrave'];
 // 		$updatelink_sql = "SELECT * FROM ".$wpdb->prefix."wpsc_productmeta WHERE product_id = $id AND meta_key='merchant_notes'";
 // 		$updatelink_data = $wpdb->get_results($updatelink_sql, ARRAY_A);
 // 		if (count($updatelink_data)>0){
 // 			$updatelink_sql = "UPDATE ".$wpdb->prefix."wpsc_productmeta SET meta_value = '$notes' WHERE product_id = $id AND meta_key='merchant_notes'";
 // 			$updatelink_data = $wpdb->query($updatelink_sql);
 // 		} else {
 // 			$updatelink_sql = "INSERT INTO ".$wpdb->prefix."wpsc_productmeta VALUES('',$id,'merchant_notes' ,'$notes')";
 // 			$updatelink_data = $wpdb->query($updatelink_sql);
 // 		}
 // 	}
 /* handle editing file uploads here */
 if (!empty($_FILES['file']['name'])) {
     $fileid = wpsc_item_process_file('edit');
     $file = $fileid;
 } else {
     if ($_POST['select_product_file'] != '') {
         $fileid = wpsc_item_reassign_file($_POST['select_product_file'], 'edit');
         $file = $fileid;
     }
 }
 if (file_exists($_FILES['preview_file']['tmp_name'])) {
     $fileid = $wpdb->get_var("SELECT `file` FROM `" . $wpdb->prefix . "product_list` WHERE `id` = '{$id}' LIMIT 1");
     copy($_FILES['preview_file']['tmp_name'], WPSC_PREVIEW_DIR . basename($_FILES['preview_file']['name']));
     $mimetype = wpsc_get_mimetype(WPSC_PREVIEW_DIR . basename($_FILES['preview_file']['name']));
     $wpdb->query("UPDATE `" . $wpdb->prefix . "product_files` SET `preview` = '" . $wpdb->escape(basename($_FILES['preview_file']['name'])) . "', `preview_mimetype` = '" . $mimetype . "' WHERE `id` = '{$fileid}' LIMIT 1");
 }
 /* Handle new image uploads here */
 $image = wpsc_item_process_image();
function wpsc_insert_product($post_data, $wpsc_error = false)
{
    global $wpdb;
    $adding = false;
    $update = false;
    if ((int) $post_data['product_id'] > 0) {
        $product_id = absint($post_data['product_id']);
        $update = true;
    }
    $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] !== null) {
            $update_values[$column] = stripslashes($post_data[$column]);
        } else {
            if ($update != true && $default !== null) {
                $update_values[$column] = stripslashes($default);
            }
        }
    }
    if ($update === true) {
        $where = array('id' => $product_id);
        if (false === $wpdb->update(WPSC_TABLE_PRODUCT_LIST, $update_values, $where)) {
            if ($wpsc_error) {
                return new WP_Error('db_update_error', __('Could not update product in the database'), $wpdb->last_error);
            } else {
                return false;
            }
        }
    } else {
        if (false === $wpdb->insert(WPSC_TABLE_PRODUCT_LIST, $update_values)) {
            if ($wp_error) {
                return new WP_Error('db_insert_error', __('Could not insert product into the database'), $wpdb->last_error);
            } else {
                return 0;
            }
        }
        $adding = true;
        $product_id = (int) $wpdb->insert_id;
    }
    /* Add tidy url name */
    if ($post_data['name'] != '') {
        $existing_name = get_product_meta($product_id, 'url_name');
        // strip slashes, trim whitespace, convert to lowercase
        $tidied_name = strtolower(trim(stripslashes($post_data['name'])));
        // convert " - " to "-", all other spaces to dashes, and remove all foward slashes.
        //$url_name = preg_replace(array("/(\s-\s)+/","/(\s)+/", "/(\/)+/"), array("-","-", ""), $tidied_name);
        $url_name = sanitize_title($tidied_name);
        // Select all similar names, using an escaped version of the URL name
        $similar_names = (array) $wpdb->get_col("SELECT `meta_value` FROM `" . WPSC_TABLE_PRODUCTMETA . "` WHERE `product_id` NOT IN('{$product_id}}') AND `meta_key` IN ('url_name') AND `meta_value` REGEXP '^(" . $wpdb->escape(preg_quote($url_name)) . "){1}[[:digit:]]*\$' ");
        // Check desired name is not taken
        if (array_search($url_name, $similar_names) !== false) {
            // If it is, try to add a number to the end, if that is taken, try the next highest number...
            $i = 0;
            do {
                $i++;
            } while (array_search($url_name . $i, $similar_names) !== false);
            // Concatenate the first number found that wasn't taken
            $url_name .= $i;
        }
        // If our URL name is the same as the existing name, do othing more.
        if ($existing_name != $url_name) {
            update_product_meta($product_id, 'url_name', $url_name);
        }
    }
    // if we succeed, we can do further editing
    // update the categories
    wpsc_update_category_associations($product_id, $post_data['category']);
    // and the tags
    wpsc_update_product_tags($product_id, $post_data['product_tags'], $post_data['wpsc_existing_tags']);
    // and the meta
    wpsc_update_product_meta($product_id, $post_data['meta']);
    // and the custom meta
    wpsc_update_custom_meta($product_id, $post_data);
    // and the images
    wpsc_update_product_images($product_id, $post_data);
    //and the alt currency
    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 {
        wpsc_item_reassign_file($product_id, $post_data['select_product_file']);
    }
    //exit('<pre>'.print_r($post_data, true).'</pre>');
    if ($post_data['files']['preview_file']['tmp_name'] != '') {
        wpsc_item_add_preview_file($product_id, $post_data['files']['preview_file']);
    }
    $variations_processor = new nzshpcrt_variations();
    if ($adding === true && $_POST['variations'] != null) {
        foreach ((array) $_POST['variations'] as $variation_id => $state) {
            $variation_id = (int) $variation_id;
            if ($state == 1) {
                $variation_values = $variations_processor->falsepost_variation_values($variation_id);
                $variations_processor->add_to_existing_product($product_id, $variation_values);
            }
        }
    }
    if ($post_data['edit_variation_values'] != null) {
        $variations_processor->edit_product_values($product_id, $post_data['edit_variation_values']);
    }
    if ($post_data['edit_add_variation_values'] != null) {
        $variations_processor->edit_add_product_values($product_id, $post_data['edit_add_variation_values']);
    }
    if ($post_data['variation_priceandstock'] != null) {
        $variations_processor->update_variation_values($product_id, $post_data['variation_priceandstock']);
    }
    do_action('wpsc_edit_product', $product_id);
    wpsc_ping();
    return $product_id;
}
Example #5
0
/**
	* wpsc_insert_product function 
	*
	* @param unknown 
	* @return unknown
*/
function wpsc_insert_product($post_data, $wpsc_error = false)
{
    global $wpdb, $user_ID;
    $adding = false;
    $update = false;
    if ((int) $post_data['post_ID'] > 0) {
        $product_id = absint($post_data['post_ID']);
        $update = true;
    } else {
        if ((int) $post_data['product_id'] > 0) {
            $product_id = absint($post_data['product_id']);
            $update = true;
        }
    }
    $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);
            }
        }
    }
    $product_post_values = array('ID' => $product_id, 'post_author' => $user_ID, 'post_content' => $post_data['description'], 'post_excerpt' => $post_data['additional_description'], 'post_title' => $post_data['name'], 'post_status' => $post_data['post_status'], 'post_type' => "wpsc-product", 'post_name' => sanitize_title($post_data['name']));
    if ($post_data['meta']['_wpsc_product_metadata']['enable_comments'] == 0) {
        $product_post_values["comment_status"] = "closed";
    } else {
        $product_post_values["comment_status"] = "open";
    }
    if (isset($sku) && $sku != '') {
        $product_post_array['guid'] = $sku;
    }
    if ($update === true) {
        $where = array('id' => $product_id);
        $product_id = wp_update_post($product_post_values);
        if (isset($post_data["sticky"])) {
            stick_post($product_id);
        } else {
            unstick_post($product_id);
        }
        if ($product_id == 0) {
            if ($wpsc_error) {
                return new WP_Error('db_update_error', __('Could not update product in the database', 'wpsc'), $wpdb->last_error);
            } else {
                return false;
            }
        }
    } else {
        $product_post_values += array('post_date' => $product['date_added']);
        $product_id = wp_insert_post($product_post_values);
        if (isset($post_data["sticky"])) {
            stick_post($product_id);
        } else {
            unstick_post($product_id);
        }
        if ($product_id == 0) {
            if ($wp_error) {
                return new WP_Error('db_insert_error', __('Could not insert product into the database', 'wpsc'), $wpdb->last_error);
            } else {
                return 0;
            }
        }
        $adding = true;
    }
    // if we succeed, we can do further editing
    // and the meta
    wpsc_update_product_meta($product_id, $post_data['meta']);
    // and the custom meta
    wpsc_update_custom_meta($product_id, $post_data);
    //and the alt currency
    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;
}