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; }
function wpsc_admin_ajax() { global $wpdb, $user_level, $wp_rewrite; get_currentuserinfo(); if (is_numeric($_POST['catid'])) { /* fill category form */ echo nzshpcrt_getcategoryform($_POST['catid']); exit; } else { if (is_numeric($_POST['brandid'])) { /* fill brand form */ echo nzshpcrt_getbrandsform($_POST['brandid']); exit; } else { if (is_numeric($_POST['variation_id'])) { echo nzshpcrt_getvariationform($_POST['variation_id']); exit; } } } if ($_POST['action'] == 'product-page-order') { $order = $_POST['order']; if (!isset($order[0])) { $order = $order['normal']; } else { $order = $order[0]; } $order = array_unique(explode(',', $order)); update_option('wpsc_product_page_order', $order); exit(print_r($order, 1)); } if ($_POST['save_image_upload_state'] == "true" && is_numeric($_POST['image_upload_state'])) { //get_option('wpsc_image_upload_state'); $upload_state = (int) (bool) $_POST['image_upload_state']; update_option('wpsc_use_flash_uploader', $upload_state); exit("done"); } if ($_POST['remove_variation_value'] == "true" && is_numeric($_POST['variation_value_id'])) { $wpdb->query("DELETE FROM `" . WPSC_TABLE_VARIATION_VALUES_ASSOC . "` WHERE `value_id` = '" . (int) $_POST['variation_value_id'] . "'"); $wpdb->query("DELETE FROM `" . WPSC_TABLE_VARIATION_VALUES . "` WHERE `id` = '" . (int) $_POST['variation_value_id'] . "' LIMIT 1"); exit; } if ($_POST['edit_variation_value_list'] == 'true' && is_numeric($_POST['variation_id']) && is_numeric($_POST['product_id'])) { $variation_id = (int) $_POST['variation_id']; $product_id = (int) $_POST['product_id']; $variations_processor = new nzshpcrt_variations(); $variation_values = $variations_processor->falsepost_variation_values($variation_id); if (is_array($variation_values)) { //echo(print_r($variation_values,true)); $check_variation_added = $wpdb->get_var("SELECT `id` FROM `" . WPSC_TABLE_VARIATION_ASSOC . "` WHERE `type` IN ('product') AND `associated_id` IN ('{$product_id}') AND `variation_id` IN ('{$variation_id}') LIMIT 1"); //exit("<pre>".print_r($variation_values,true)."<pre>"); if ($check_variation_added == null) { $variations_processor->add_to_existing_product($product_id, $variation_values); } echo $variations_processor->display_attached_variations($product_id); echo $variations_processor->variations_grid_view($product_id); } else { echo "false"; } exit; } if ($_POST['remove_form_field'] == "true" && is_numeric($_POST['form_id'])) { //exit(print_r($user,true)); if (current_user_can('level_7')) { $wpdb->query($wpdb->prepare("UPDATE `" . WPSC_TABLE_CHECKOUT_FORMS . "` SET `active` = '0' WHERE `id` = %d LIMIT 1 ;", $_POST['form_id'])); exit(' '); } } if ($_POST['hide_ecom_dashboard'] == 'true') { require_once ABSPATH . WPINC . '/rss.php'; $rss = fetch_rss('http://www.instinct.co.nz/feed/'); $rss->items = array_slice($rss->items, 0, 5); $rss_hash = sha1(serialize($rss->items)); update_option('wpsc_ecom_news_hash', $rss_hash); exit(1); } if ($_POST['remove_meta'] == 'true' && is_numeric($_POST['meta_id'])) { $meta_id = (int) $_POST['meta_id']; $selected_meta = $wpdb->get_row("SELECT * FROM `" . WPSC_TABLE_PRODUCTMETA . "` WHERE `id` IN('{$meta_id}') ", ARRAY_A); if ($selected_meta != null) { if ($wpdb->query("DELETE FROM `" . WPSC_TABLE_PRODUCTMETA . "` WHERE `id` IN('{$meta_id}') LIMIT 1")) { echo $meta_id; exit; } } echo 0; exit; } if ($_REQUEST['log_state'] == "true" && is_numeric($_POST['id']) && is_numeric($_POST['value'])) { $newvalue = $_POST['value']; if ($_REQUEST['suspend'] == 'true') { if ($_REQUEST['value'] == 1) { wpsc_member_dedeactivate_subscriptions($_POST['id']); } else { wpsc_member_deactivate_subscriptions($_POST['id']); } exit; } else { $log_data = $wpdb->get_row("SELECT * FROM `" . WPSC_TABLE_PURCHASE_LOGS . "` WHERE `id` = '" . $_POST['id'] . "' LIMIT 1", ARRAY_A); if ($newvalue == 2 && function_exists('wpsc_member_activate_subscriptions')) { wpsc_member_activate_subscriptions($_POST['id']); } $update_sql = "UPDATE `" . WPSC_TABLE_PURCHASE_LOGS . "` SET `processed` = '" . $newvalue . "' WHERE `id` = '" . $_POST['id'] . "' LIMIT 1"; $wpdb->query($update_sql); //echo("/*"); if ($newvalue > $log_data['processed'] && $log_data['processed'] < 2) { transaction_results($log_data['sessionid'], false); } //echo("*/"); $stage_sql = "SELECT * FROM `" . WPSC_TABLE_PURCHASE_STATUSES . "` WHERE `id`='" . $newvalue . "' AND `active`='1' LIMIT 1"; $stage_data = $wpdb->get_row($stage_sql, ARRAY_A); echo "document.getElementById(\"form_group_" . $_POST['id'] . "_text\").innerHTML = '" . $stage_data['name'] . "';\n"; echo "document.getElementById(\"form_group_" . $_POST['id'] . "_text\").style.color = '#" . $stage_data['colour'] . "';\n"; $year = date("Y"); $month = date("m"); $start_timestamp = mktime(0, 0, 0, $month, 1, $year); $end_timestamp = mktime(0, 0, 0, $month + 1, 0, $year); echo "document.getElementById(\"log_total_month\").innerHTML = '" . addslashes(nzshpcrt_currency_display(admin_display_total_price($start_timestamp, $end_timestamp), 1)) . "';\n"; echo "document.getElementById(\"log_total_absolute\").innerHTML = '" . addslashes(nzshpcrt_currency_display(admin_display_total_price(), 1)) . "';\n"; exit; } } if ($_POST['list_variation_values'] == "true") { // retrieve the forms for associating variations and their values with products $variation_processor = new nzshpcrt_variations(); $variations_selected = array(); foreach ((array) $_POST['variations'] as $variation_id => $checked) { $variations_selected[] = (int) $variation_id; } if (is_numeric($_POST['product_id']) && $_POST['product_id'] > 0) { $product_id = absint($_POST['product_id']); $selected_price = (double) $_POST['selected_price']; // variation values housekeeping $completed_variation_values = $variation_processor->edit_product_values($product_id, $_POST['edit_var_val'], $selected_price); // get all the currently associated variations from the database $associated_variations = $wpdb->get_results("SELECT * FROM `" . WPSC_TABLE_VARIATION_ASSOC . "` WHERE `type` IN ('product') AND `associated_id` IN ('{$product_id}')", ARRAY_A); $variations_still_associated = array(); foreach ((array) $associated_variations as $associated_variation) { // remove variations not checked that are in the database if (array_search($associated_variation['variation_id'], $variations_selected) === false) { $wpdb->query("DELETE FROM `" . WPSC_TABLE_VARIATION_ASSOC . "` WHERE `id` = '{$associated_variation['id']}' LIMIT 1"); $wpdb->query("DELETE FROM `" . WPSC_TABLE_VARIATION_VALUES_ASSOC . "` WHERE `product_id` = '{$product_id}' AND `variation_id` = '{$associated_variation['variation_id']}' "); } else { // make an array for adding in the variations next step, for efficiency $variations_still_associated[] = $associated_variation['variation_id']; } } foreach ((array) $variations_selected as $variation_id) { // add variations not already in the database that have been checked. $variation_values = $variation_processor->falsepost_variation_values($variation_id); if (array_search($variation_id, $variations_still_associated) === false) { $variation_processor->add_to_existing_product($product_id, $variation_values); } } //echo "/* ".print_r($variation_values,true)." */\n\r"; echo "edit_variation_combinations_html = \"" . str_replace(array("\n", "\r"), array('\\n', '\\r'), addslashes($variation_processor->variations_grid_view($product_id, (array) $completed_variation_values))) . "\";\n"; } else { if (count($variations_selected) > 0) { // takes an array of variations, returns a form for adding data to those variations. if ((double) $_POST['selected_price'] > 0) { $selected_price = (double) $_POST['selected_price']; } $limited_stock = false; if ($_POST['limited_stock'] == 'true') { $limited_stock = true; } $selected_variation_values = array(); foreach ($_POST['edit_var_val'] as $variation_value_array) { //echo "/* ".print_r($variation_value_array,true)." */\n\r"; $selected_variation_values = array_merge(array_keys($variation_value_array), $selected_variation_values); } ////echo "/* ".print_r($selected_variation_values,true)." */\n\r"; echo "edit_variation_combinations_html = \"" . __('Edit Variation Set', 'wpsc') . "<br />" . str_replace(array("\n", "\r"), array('\\n', '\\r'), addslashes($variation_processor->variations_grid_view(0, (array) $variations_selected, (array) $selected_variation_values, $selected_price, $limited_stock))) . "\";\n"; } else { echo "edit_variation_combinations_html = \"\";\n"; } } exit; } if (isset($_POST['language_setting']) && ($_GET['page'] = WPSC_DIR_NAME . '/wpsc-admin/display-options.page.php')) { if ($user_level >= 7) { update_option('language_setting', $_POST['language_setting']); } } }
} } $image_resize = $_POST['image_resize']; if (!is_numeric($image_resize) || $image_resize < 1) { $image_resize = 0; } $wpdb->query("UPDATE `" . $wpdb->prefix . "product_list` SET `thumbnail_state` = '" . $image_resize . "' WHERE `id`='" . $_POST['prodid'] . "' LIMIT 1"); if ($_POST['deleteimage'] == 1) { $wpdb->query("UPDATE `" . $wpdb->prefix . "product_list` SET `image` = '' WHERE `id`='" . $_POST['prodid'] . "' LIMIT 1"); } $variations_procesor = new nzshpcrt_variations(); if ($_POST['variation_values'] != null) { //$variations_procesor->add_to_existing_product($_POST['prodid'],$_POST['variation_values']); } if ($_POST['edit_variation_values'] != null) { $variations_procesor->edit_product_values($_POST['prodid'], $_POST['edit_variation_values']); } if ($_POST['edit_add_variation_values'] != null) { $variations_procesor->edit_add_product_values($_POST['prodid'], $_POST['edit_add_variation_values']); } if ($_POST['variation_priceandstock'] != null) { $variations_procesor->update_variation_values($_POST['prodid'], $_POST['variation_priceandstock']); } // send the pings out. wpsc_ping(); echo "<div class='updated'><p align='center'>" . TXT_WPSC_PRODUCTHASBEENEDITED . "</p></div>"; } } if (is_numeric($_GET['deleteid'])) { $wpdb->query("DELETE FROM `" . $wpdb->prefix . "wpsc_productmeta` WHERE `product_id` = '" . $_GET['deleteid'] . "' AND `meta_key` IN ('url_name')"); $wpdb->query("UPDATE `" . $wpdb->prefix . "product_list` SET `active` = '0' WHERE `id`='" . $_GET['deleteid'] . "' LIMIT 1");