/**
  * AJAX - Création d'un nouveau produit / Create a new product
  */
 function create_product()
 {
     global $wpdb;
     $response = array('status' => false, 'output' => __('Error at product creation!', 'wpshop'), 'pid' => -1);
     $post_title = !empty($_POST['post_title']) ? $_POST['post_title'] : -1;
     $post_content = !empty($_POST['post_content']) ? $_POST['post_content'] : '';
     $attributes = !empty($_POST['attribute']) ? $_POST['attribute'] : -1;
     $id_attribute_set = !empty($_POST['wps-product-attribute-set']) ? $_POST['wps-product-attribute-set'] : -1;
     if (-1 != $post_title && -1 != $id_attribute_set) {
         $new_product_id = wp_insert_post(array('post_type' => WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT, 'post_status' => 'publish', 'post_title' => $post_title, 'post_content' => $post_content));
         if (!is_wp_error($new_product_id)) {
             update_post_meta($new_product_id, '_' . WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT . '_attribute_set_id', $id_attribute_set);
             $data_to_save['post_ID'] = $data_to_save['product_id'] = intval($new_product_id);
             $data_to_save['wpshop_product_attribute'] = !empty($attributes) ? $attributes : array();
             $data_to_save['user_ID'] = get_current_user_id();
             $data_to_save['action'] = 'editpost';
             // Get current barcode
             if (empty($data_to_save['wpshop_product_attribute']['varchar']['barcode'])) {
                 // Get current barcode
                 $wps_product_mdl = new wps_product_mdl();
                 $attid = wpshop_attributes::getElement('barcode', "'valid'", 'code')->id;
                 $barcode_value = wpshop_attributes::wpshop_att_val_func(array('pid' => $new_product_id, 'attid' => $attid));
                 $data_to_save['wpshop_product_attribute']['varchar']['barcode'] = $barcode_value;
             }
             $response['pid'] = $new_product_id;
             $response['status'] = false;
             $response['output'] = __('Product created partially!', 'wpshop');
             $response['letter'] = substr($post_title, 0, 1);
             if (!empty($new_product_id) && !empty($data_to_save['user_ID'])) {
                 $product_class = new wpshop_products();
                 $product_class->save_product_custom_informations($new_product_id, $data_to_save);
                 $response['status'] = true;
                 $response['output'] = __('Product created successfully.', 'wpshop');
             }
             $wps_quick_creation_hook = do_action('wps-new-product-quick-created', $new_product_id);
             if (!empty($wps_quick_creation_hook)) {
                 $response[''] = $wps_quick_creation_hook;
             }
         }
     }
     wp_die(json_encode($response));
 }
 /**
  * AJAX - Save datas
  */
 function wps_save_product_quick_interface()
 {
     global $wpdb;
     $response = '';
     $status = false;
     $count_products_to_update = 0;
     $total_updated_products = 0;
     if (!empty($_REQUEST['wps_product_quick_save'])) {
         $count_products_to_update = count($_REQUEST['wps_product_quick_save']);
         foreach ($_REQUEST['wps_product_quick_save'] as $product_to_save) {
             $data_to_save = array();
             // Update post
             $updated_post = wp_update_post(array('ID' => $product_to_save, 'post_title' => $_REQUEST['wps_mass_interface'][$product_to_save]['post_title'], 'post_content' => $_REQUEST['wps_mass_interface'][$product_to_save]['post_content']));
             // Update attributes
             if (!empty($updated_post)) {
                 // Update Featured picture
                 if (!empty($_REQUEST['wps_mass_interface'][$product_to_save]['picture'])) {
                     $thumbnail_exist = get_post_meta($updated_post, '_thumbnail_id', true);
                     if ($_REQUEST['wps_mass_interface'][$product_to_save]['picture'] != 'deleted') {
                         wp_update_post(array('ID' => $_REQUEST['wps_mass_interface'][$product_to_save]['picture'], 'post_parent' => $updated_post));
                         update_post_meta($updated_post, '_thumbnail_id', $_REQUEST['wps_mass_interface'][$product_to_save]['picture']);
                     } elseif ($_REQUEST['wps_mass_interface'][$product_to_save]['picture'] == 'deleted' && !empty($thumbnail_exist)) {
                         delete_post_meta($updated_post, '_thumbnail_id');
                     }
                 }
                 // Update files datas
                 if (!empty($_REQUEST['wps_mass_interface'][$product_to_save]['files'])) {
                     $files_data = explode(',', $_REQUEST['wps_mass_interface'][$product_to_save]['files']);
                     if (!empty($files_data) && is_array($files_data)) {
                         foreach ($files_data as $file_id) {
                             if (!empty($file_id)) {
                                 wp_update_post(array('ID' => $file_id, 'post_parent' => $updated_post));
                             }
                         }
                     }
                 }
                 $data_to_save['post_ID'] = $data_to_save['product_id'] = intval($product_to_save);
                 $data_to_save['wpshop_product_attribute'] = !empty($_REQUEST['wpshop_product_attribute'][$product_to_save]) ? $_REQUEST['wpshop_product_attribute'][$product_to_save] : array();
                 if (empty($data_to_save['wpshop_product_attribute']['varchar']['barcode'])) {
                     // Get current barcode
                     $wps_product_mdl = new wps_product_mdl();
                     $attid = wpshop_attributes::getElement('barcode', "'valid'", 'code')->id;
                     $barcode_value = wpshop_attributes::wpshop_att_val_func(array('pid' => $data_to_save['post_ID'], 'attid' => $attid));
                     $data_to_save['wpshop_product_attribute']['varchar']['barcode'] = $barcode_value;
                 }
                 $data_to_save['user_ID'] = get_current_user_id();
                 $data_to_save['action'] = 'editpost';
                 if (!empty($_REQUEST['wps_mass_interface'][$product_to_save]['post_delete']) && $_REQUEST['wps_mass_interface'][$product_to_save]['post_delete'] == "true") {
                     wp_trash_post($product_to_save);
                 }
                 if (!empty($product_to_save) && !empty($data_to_save['user_ID'])) {
                     $product_class = new wpshop_products();
                     $product_class->save_product_custom_informations($product_to_save, $data_to_save);
                     $total_updated_products++;
                 }
             }
         }
     }
     // Checking status
     $status = $total_updated_products == $count_products_to_update ? true : false;
     if ($status) {
         $response = sprintf(__('%d products have been successfully updated', 'wpshop'), $total_updated_products);
     } else {
         if (!empty($total_updated_products)) {
             $response = sprintf(__('All selected products do not be updated. %d on %d products have been successfully updated', 'wpshop'), $total_updated_products, $count_products_to_update);
         } else {
             $response = __('No product have been updated', 'wpshop');
         }
     }
     echo json_encode(array('status' => $status, 'response' => $response));
     wp_die();
 }