function wpsc_swfupload_images() { global $wpdb, $current_user; // xdebug_start_trace(); $file = $_FILES['async-upload']; $product_id = absint($_POST['product_id']); $nonce = $_POST['_wpnonce']; $output = ''; // Flash often fails to send cookies with the POST or upload, so we need to pass it in GET or POST instead, code is from wp-admin/async-upload.php if (is_ssl() && empty($_COOKIE[SECURE_AUTH_COOKIE]) && !empty($_REQUEST['auth_cookie'])) { $_COOKIE[SECURE_AUTH_COOKIE] = $_REQUEST['auth_cookie']; } else { if (empty($_COOKIE[AUTH_COOKIE]) && !empty($_REQUEST['auth_cookie'])) { $_COOKIE[AUTH_COOKIE] = $_REQUEST['auth_cookie']; } } unset($current_user); require_once ABSPATH . 'wp-admin/admin.php'; if (!current_user_can('upload_files')) { exit("status=-1;\n"); } if (!wp_verify_nonce($nonce, 'product-swfupload')) { exit("status=-1;\n"); } if (function_exists('gold_shpcrt_display_gallery')) { // if more than one image is permitted $existing_image_data = $wpdb->get_row("SELECT COUNT(*) AS `count`, MAX(image_order) AS `order` FROM " . WPSC_TABLE_PRODUCT_IMAGES . " WHERE `product_id`='" . absint($product_id) . "' AND `image` NOT IN ('')", ARRAY_A); $order = (int) $existing_image_data['order']; $count = $existing_image_data['count']; $previous_image = $wpdb->get_var("SELECT `image` FROM `" . WPSC_TABLE_PRODUCT_LIST . "` WHERE `id`='" . absint($product_id) . "' LIMIT 1"); //echo "/* $count $previous_image */ "; if ($count > 0 && (strlen($previous_image) > 0 || absint($product_id) < 1)) { // if there is more than one image $success = move_uploaded_file($file['tmp_name'], WPSC_IMAGE_DIR . basename($file['name'])); if ($product_id == '') { copy(WPSC_IMAGE_DIR . basename($file['name']), WPSC_THUMBNAIL_DIR . basename($file['name'])); } $order++; if ($success) { $wpdb->query("INSERT INTO `" . WPSC_TABLE_PRODUCT_IMAGES . "` ( `product_id` , `image` , `width` , `height` , `image_order` ) VALUES( '{$product_id}','" . basename($file['name']) . "', '0', '0', '{$order}')"); $id = $wpdb->get_var("SELECT LAST_INSERT_ID() AS `id` FROM `" . WPSC_TABLE_PRODUCT_IMAGES . "` LIMIT 1"); $src = $file['name']; $output .= "upload_status=1;\n"; $output .= "image_src='" . $src . "';\n"; $output .= "image_id='{$id}';\n"; $output .= "product_id='{$product_id}';\n"; $output .= "replace_existing=0;"; } else { $output .= "status=0;\n"; } } else { // if thereare no images if ($product_id > 0) { $image_data = wpsc_item_process_image($product_id, $file['tmp_name'], $file['name'], 0, 0, 1, true); $src = $image_data['filename']; $image_id = $image_data['image_id']; } else { $success = move_uploaded_file($file['tmp_name'], WPSC_IMAGE_DIR . basename($file['name'])); copy(WPSC_IMAGE_DIR . basename($file['name']), WPSC_THUMBNAIL_DIR . basename($file['name'])); $wpdb->query("INSERT INTO `" . WPSC_TABLE_PRODUCT_IMAGES . "` ( `product_id` , `image` , `width` , `height` , `image_order` ) VALUES( '{$product_id}','" . basename($file['name']) . "', '0', '0', '0')"); $src = basename($file['name']); } if ($src != null) { if ($image_id < 1) { $image_id = $wpdb->get_var("SELECT LAST_INSERT_ID() AS `id` FROM `" . WPSC_TABLE_PRODUCT_IMAGES . "` LIMIT 1"); if ($product_id > 0) { $previous_image = $wpdb->get_var("UPDATE `" . WPSC_TABLE_PRODUCT_LIST . "` SET `image` = '{$image_id}' WHERE `id`='{$product_id}' LIMIT 1"); } } $output .= "upload_status=1;\n"; $output .= "image_src='" . $src . "';\n"; $output .= "image_id='{$image_ids}';\n"; $output .= "product_id='{$product_id}';\n"; $output .= "replace_existing=1;"; } else { $output .= "status=0;\n"; } } } else { // Otherwise... $previous_image = $wpdb->get_var("SELECT `image` FROM `" . WPSC_TABLE_PRODUCT_LIST . "` WHERE `id`='{$product_id}' LIMIT 1"); $image_replaced = false; if ($product_id > 0) { $image_data = wpsc_item_process_image($product_id, $file['tmp_name'], $file['name'], 0, 0, 1, true); $src = $image_data['filename']; $image_id = $image_data['image_id']; } else { $success = move_uploaded_file($file['tmp_name'], WPSC_IMAGE_DIR . basename($file['name'])); copy(WPSC_IMAGE_DIR . basename($file['name']), WPSC_THUMBNAIL_DIR . basename($file['name'])); $wpdb->query("INSERT INTO `" . WPSC_TABLE_PRODUCT_IMAGES . "` ( `product_id` , `image` , `width` , `height` , `image_order` ) VALUES( '{$product_id}','" . basename($file['name']) . "', '0', '0', '0')"); $src = basename($file['name']); } //$src = wpsc_item_process_image($product_id, $file['tmp_name'], $file['name']); if ($src != null) { if ($image_id < 1) { $image_id = $wpdb->get_var("SELECT LAST_INSERT_ID() AS `id` FROM `" . WPSC_TABLE_PRODUCT_IMAGES . "` LIMIT 1"); if ($product_id > 0) { $previous_image = $wpdb->get_var("UPDATE `" . WPSC_TABLE_PRODUCT_LIST . "` SET `image` = '{$image_id}' WHERE `id`='{$product_id}' LIMIT 1"); } } $image_replaced = true; $output .= "upload_status=1;\n"; $output .= "image_src='" . $src . "';\n"; $output .= "image_id='{$image_id}';\n"; $output .= "product_id='{$product_id}';\n"; if ($image_replaced == true) { $output .= "replace_existing=1;\n"; } else { $output .= "replace_existing=0;\n"; } } else { $output .= "status=0;\n"; } } exit($output); }
/** * wpsc_update_product_tags function * * @param integer product ID * @param array the post data */ function wpsc_update_product_images($product_id, $post_data) { global $wpdb; $uploaded_images = array(); // This segment is for associating the images uploaded using swfuploader when adding a product foreach ((array) $post_data['gallery_image_id'] as $added_image) { if ($added_image > 0) { $uploaded_images[] = absint($added_image); } } if (count($uploaded_images) > 0) { $uploaded_image_data = $wpdb->get_col("SELECT `id` FROM `" . WPSC_TABLE_PRODUCT_IMAGES . "` WHERE `id` IN (" . implode(', ', $uploaded_images) . ") AND `product_id` = '0'"); if (count($uploaded_image_data) > 0) { $first_image = null; foreach ($uploaded_image_data as $uploaded_image_id) { if ($first_image === null) { $first_image = absint($uploaded_image_id); } $wpdb->query("UPDATE `" . WPSC_TABLE_PRODUCT_IMAGES . "` SET `product_id` = '{$product_id}' WHERE `id` = '{$uploaded_image_id}' LIMIT 1;"); } $previous_image = $wpdb->get_var("SELECT `image` FROM `" . WPSC_TABLE_PRODUCT_LIST . "` WHERE `id`='{$product_id}' LIMIT 1"); if ($previous_image == 0) { $wpdb->query("UPDATE `" . WPSC_TABLE_PRODUCT_LIST . "` SET `image` = '{$first_image}' WHERE `id`='{$product_id}' LIMIT 1"); } wpsc_resize_image_thumbnail($product_id, 1); } } /* Handle new image uploads here */ if ($post_data['files']['image']['tmp_name'] != '') { $image = wpsc_item_process_image($product_id, $post_data['files']['image']['tmp_name'], str_replace(" ", "_", $post_data['files']['image']['name']), $post_data['width'], $post_data['height'], $post_data['image_resize']); $image_action = absint($post_data['image_resize']); $image_width = $post_data['width']; $image_height = $post_data['height']; } else { $image_action = absint($post_data['gallery_resize']); $image_width = $post_data['gallery_width']; $image_height = $post_data['gallery_height']; } // exit( "<pre>".print_r($image_action, true)."</pre>"); wpsc_resize_image_thumbnail($product_id, $image_action, $image_width, $image_height); //exit( " <pre>".print_r($post_data, true)."</pre>"); }
function wpsc_add_product($product_values) { global $wpdb; // takes an array, inserts it into the database as a product $success = false; $insertsql = "INSERT INTO `" . WPSC_TABLE_PRODUCT_LIST . "` SET"; $insertsql .= "`name` = '" . $wpdb->escape($product_values['name']) . "',"; $insertsql .= "`description` = '" . $wpdb->escape($product_values['description']) . "',"; $insertsql .= "`additional_description` = '" . $wpdb->escape($product_values['additional_description']) . "',"; $insertsql .= "`price` = '" . $wpdb->escape($product_values['price']) . "',"; $insertsql .= "`quantity_limited` = '" . $wpdb->escape($product_values['quantity_limited']) . "',"; $insertsql .= "`quantity` = '" . $wpdb->escape($product_values['quantity']) . "',"; $insertsql .= "`special` = '" . $wpdb->escape($product_values['special']) . "',"; $insertsql .= "`special_price` = '" . $wpdb->escape($product_values['special_price']) . "',"; $insertsql .= "`weight` = '" . $wpdb->escape($product_values['weight']) . "',"; $insertsql .= "`weight_unit` = '" . $wpdb->escape($product_values['weight_unit']) . "',"; $insertsql .= "`no_shipping` = '" . $wpdb->escape($product_values['no_shipping']) . "',"; $insertsql .= "`pnp` = '" . $wpdb->escape($product_values['pnp']) . "',"; $insertsql .= "`international_pnp` = '" . $wpdb->escape($product_values['international_pnp']) . "',"; $insertsql .= "`donation` = '" . $wpdb->escape($product_values['donation']) . "',"; $insertsql .= "`display_frontpage` = '" . $wpdb->escape($product_values['display_frontpage']) . "',"; $insertsql .= "`notax` = '" . $wpdb->escape($product_values['notax']) . "',"; $insertsql .= "`image` = '0',"; $insertsql .= "`file` = '0',"; $insertsql .= "`thumbnail_state` = '0' ;"; //Insert the data if ($wpdb->query($insertsql)) { // if we succeeded, we have a product id, we wants it for the next stuff $product_id = $wpdb->get_var("SELECT LAST_INSERT_ID() AS `id` FROM `" . WPSC_TABLE_PRODUCT_LIST . "` LIMIT 1"); // add the tags if (function_exists('wp_insert_term')) { product_tag_init(); $tags = $product_values['product_tag']; if ($tags != "") { $tags = explode(',', $tags); foreach ($tags as $tag) { $tt = wp_insert_term((string) $tag, 'product_tag'); } $return = wp_set_object_terms($product_id, $tags, 'product_tag'); } } $image = wpsc_item_process_image($product_id, $product_values['image_path'], basename($product_values['image_path']), $product_values['width'], $product_values['height'], $product_values['image_resize']); if ($image != null) { $wpdb->query("UPDATE `" . WPSC_TABLE_PRODUCT_LIST . "` SET `image` = '" . $wpdb->escape($image) . "' WHERE `id`='" . $product_id . "' LIMIT 1"); } // add the product meta values if ($product_values['productmeta_values'] != null) { foreach ((array) $product_values['productmeta_values'] as $key => $value) { if (get_product_meta($product_id, $key) != false) { update_product_meta($product_id, $key, $value); } else { add_product_meta($product_id, $key, $value); } } } // and the custom meta values if ($product_values['new_custom_meta'] != null) { foreach ((array) $product_values['new_custom_meta']['name'] as $key => $name) { $value = $product_values['new_custom_meta']['value'][(int) $key]; if ($name != '' && $value != '') { add_product_meta($product_id, $name, $value, false, true); } } } // Add the tidy url name $tidied_name = trim($product_values['name']); $tidied_name = strtolower($tidied_name); $url_name = sanitize_title($tidied_name); $similar_names = $wpdb->get_row("SELECT COUNT(*) AS `count`, MAX(REPLACE(`meta_value`, '" . $wpdb->escape($url_name) . "', '')) AS `max_number` FROM `" . WPSC_TABLE_PRODUCTMETA . "` WHERE `meta_key` IN ('url_name') AND `meta_value` REGEXP '^(" . $wpdb->escape($url_name) . "){1}(\\d)*\$' ", ARRAY_A); $extension_number = ''; if ($similar_names['count'] > 0) { $extension_number = (int) $similar_names['max_number'] + 1; } $url_name .= $extension_number; add_product_meta($product_id, 'url_name', $url_name, true); // Add the varations and associated values $variations_procesor = new nzshpcrt_variations(); if ($product_values['variation_values'] != null) { $variations_procesor->add_to_existing_product($product_id, $product_values['variation_values']); } if ($product_values['variation_priceandstock'] != null) { $variations_procesor->update_variation_values($product_id, $product_values['variation_priceandstock']); } // Add the selelcted categories $item_list = ''; if (count($product_values['category']) > 0) { foreach ($product_values['category'] as $category_id) { $category_id = (int) $category_id; $check_existing = $wpdb->get_var("SELECT `id` FROM `" . WPSC_TABLE_ITEM_CATEGORY_ASSOC . "` WHERE `product_id` = " . $product_id . " AND `category_id` = '{$category_id}' LIMIT 1"); if ($check_existing == null) { $wpdb->query("INSERT INTO `" . WPSC_TABLE_ITEM_CATEGORY_ASSOC . "` ( `product_id` , `category_id` ) VALUES ( '" . $product_id . "', '" . $category_id . "');"); } } } $success = true; } return $success; }
$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(); if (is_numeric($_POST['prodid'])) { if (($_POST['image_resize'] == 1 || $_POST['image_resize'] == 2) && $image == '') { /* resize the image if directed to do so and no new image is supplied */ $image_data = $wpdb->get_row("SELECT `id`,`image` FROM `" . $wpdb->prefix . "product_list` WHERE `id`=" . $_POST['prodid'] . " LIMIT 1", ARRAY_A); // prevent images from being replaced by those from other products $check_multiple_use = $wpdb->get_var("SELECT COUNT(`image`) AS `count` FROM `" . $wpdb->prefix . "product_list` WHERE `image`='" . $image_data['image'] . "'"); if ($check_multiple_use > 1) { $new_filename = $image_data['id'] . "_" . $image_data['image']; if (file_exists(WPSC_THUMBNAIL_DIR . $image_data['image']) && $image_data['image'] != null) { copy(WPSC_THUMBNAIL_DIR . $image_data['image'], WPSC_THUMBNAIL_DIR . $new_filename); } if (file_exists(WPSC_IMAGE_DIR . $image_data['image']) && $image_data['image'] != null) { copy(WPSC_IMAGE_DIR . $image_data['image'], WPSC_IMAGE_DIR . $new_filename); } $wpdb->query("UPDATE `" . $wpdb->prefix . "product_list` SET `image` = '" . $new_filename . "' WHERE `id`='" . $image_data['id'] . "' LIMIT 1");