wpsc_admin_submit_product

Wpsc_admin_submit_product function.

Description

wpsc_admin_submit_product( $post_ID, $post ); 

Parameters (2)

0. $post_id
The post ID.
1. $post
The post.

Usage

  1. if ( !function_exists( 'wpsc_admin_submit_product' ) ) { 
  2. require_once '/wpsc-admin/includes/product-functions.php'; 
  3.  
  4. // The post ID. 
  5. $post_id = null; 
  6.  
  7. // The post. 
  8. $post = null; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = wpsc_admin_submit_product($post_id, $post); 
  12.  

Defined (1)

The function is defined in the following location(s).

/wpsc-admin/includes/product-functions.php  
  1. function wpsc_admin_submit_product( $post_ID, $post ) { 
  2.  
  3. if ( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) || $post->post_type != 'wpsc-product' ) { 
  4. return; 
  5.  
  6. //Type-casting ( not so much sanitization, which would be good to do ) 
  7. $post_data = stripslashes_deep( $_POST ); 
  8. $product_id = $post_ID; 
  9.  
  10. $post_data['additional_description'] = isset( $post_data['additional_description'] ) ? $post_data['additional_description'] : ''; 
  11.  
  12. if ( ! isset( $post_data['meta'] ) && isset( $_POST['meta'] ) ) { 
  13. $post_data['meta'] = (array) $_POST['meta']; 
  14.  
  15.  
  16. if ( isset( $post_data['meta']['_wpsc_price'] ) ) 
  17. $post_data['meta']['_wpsc_price'] = wpsc_string_to_float( $post_data['meta']['_wpsc_price'] ); 
  18.  
  19. if ( isset( $post_data['meta']['_wpsc_special_price'] ) ) 
  20. $post_data['meta']['_wpsc_special_price'] = wpsc_string_to_float( $post_data['meta']['_wpsc_special_price'] ); 
  21.  
  22. if ( isset( $post_data['meta']['_wpsc_sku'] ) && $post_data['meta']['_wpsc_sku'] == __('N/A', 'wpsc') ) { 
  23. $post_data['meta']['_wpsc_sku'] = ''; 
  24.  
  25. // Update donation setting 
  26. if ( isset( $post_data['wpsc_product_pricing_nonce'] ) && wp_verify_nonce( $post_data['wpsc_product_pricing_nonce'], 'update' ) ) { 
  27. $post_data['meta']['_wpsc_is_donation'] = isset( $post_data['meta']['_wpsc_is_donation'] ) ? 1 : 0; 
  28.  
  29. if ( ! isset( $post_data['meta']['_wpsc_limited_stock'] ) ) { 
  30. $post_data['meta']['_wpsc_stock'] = false; 
  31. } else { 
  32. $post_data['meta']['_wpsc_stock'] = isset( $post_data['meta']['_wpsc_stock'] ) ? (int) $post_data['meta']['_wpsc_stock'] : 0; 
  33.  
  34. unset($post_data['meta']['_wpsc_limited_stock']); 
  35. if(!isset($post_data['quantity_limited'])) $post_data['quantity_limited'] = ''; 
  36. if(!isset($post_data['special'])) $post_data['special'] = ''; 
  37.  
  38. $post_data['meta']['_wpsc_product_metadata']['quantity_limited'] = (int)(bool)$post_data['quantity_limited']; 
  39. $post_data['meta']['_wpsc_product_metadata']['special'] = (int)(bool)$post_data['special']; 
  40.  
  41. // Update Stock Options 
  42. if ( isset( $_POST['wpsc_product_stock_nonce'] ) && wp_verify_nonce( $_POST['wpsc_product_stock_nonce'], 'update' ) ) { 
  43.  
  44. $post_data['meta']['_wpsc_product_metadata'] = wp_parse_args( $post_data['meta']['_wpsc_product_metadata'], array( 
  45. 'notify_when_none_left' => 0,  
  46. 'unpublish_when_none_left' => 0 
  47. ) ); 
  48. $post_data['meta']['_wpsc_product_metadata']['notify_when_none_left'] = absint( (bool) $post_data['meta']['_wpsc_product_metadata']['notify_when_none_left'] ); 
  49. $post_data['meta']['_wpsc_product_metadata']['unpublish_when_none_left'] = absint( (bool) $post_data['meta']['_wpsc_product_metadata']['unpublish_when_none_left'] ); 
  50.  
  51.  
  52. // Update shipping setting 
  53. if ( isset( $_POST['wpsc_product_shipping_nonce'] ) && wp_verify_nonce( $_POST['wpsc_product_shipping_nonce'], 'update' ) ) { 
  54.  
  55. $post_data['meta']['_wpsc_product_metadata'] = wp_parse_args( $post_data['meta']['_wpsc_product_metadata'], array( 
  56. 'no_shipping' => 0 
  57. ) ); 
  58. $post_data['meta']['_wpsc_product_metadata']['no_shipping'] = absint( (bool) $post_data['meta']['_wpsc_product_metadata']['no_shipping'] ); 
  59.  
  60.  
  61. // Product Weight 
  62. if(!isset($post_data['meta']['_wpsc_product_metadata']['display_weight_as'])) $post_data['meta']['_wpsc_product_metadata']['display_weight_as'] = ''; 
  63.  
  64. if ( isset( $post_data['meta']['_wpsc_product_metadata']['weight'] ) ) { 
  65. $weight = wpsc_string_to_float( $post_data['meta']['_wpsc_product_metadata']['weight'] ); 
  66. $weight = wpsc_convert_weight( $weight, $post_data['meta']['_wpsc_product_metadata']['weight_unit'], "pound", true); 
  67. $post_data['meta']['_wpsc_product_metadata']['weight'] = $weight; 
  68. $post_data['meta']['_wpsc_product_metadata']['display_weight_as'] = $post_data['meta']['_wpsc_product_metadata']['weight_unit']; 
  69.  
  70. if ( isset( $post_data['meta']['_wpsc_product_metadata']['dimensions'] ) ) { 
  71. $dimensions =& $post_data['meta']['_wpsc_product_metadata']['dimensions']; 
  72. foreach ( $dimensions as $key => $value ) { 
  73. if ( ! in_array( $key, array( 'height', 'width', 'length' ) ) ) 
  74. continue; 
  75.  
  76. $dimensions[$key] = wpsc_string_to_float( $value ); 
  77.  
  78. // Update the table rate prices (quantity discounts) 
  79. if ( isset( $post_data['wpsc-update-quantity-discounts'] ) && wp_verify_nonce( $post_data['wpsc-update-quantity-discounts'], 'update-options' ) ) { 
  80. $post_data['meta']['_wpsc_product_metadata']['table_rate_price'] = isset( $post_data['table_rate_price'] ) ? $post_data['table_rate_price'] : array(); 
  81.  
  82. // If table_rate_price is empty, set empty table rate price arrays 
  83. if ( empty( $post_data['meta']['_wpsc_product_metadata']['table_rate_price'] ) ) { 
  84. $post_data['meta']['_wpsc_product_metadata']['table_rate_price']['table_price'] = array(); 
  85. $post_data['meta']['_wpsc_product_metadata']['table_rate_price']['quantity'] = array(); 
  86.  
  87. // Remove any rates with no quantity or price 
  88. if ( ! empty( $post_data['meta']['_wpsc_product_metadata']['table_rate_price']['table_price'] ) ) { 
  89. foreach ( (array) $post_data['meta']['_wpsc_product_metadata']['table_rate_price']['quantity'] as $key => $value ) { 
  90. if ( empty( $value ) ) { 
  91. unset( $post_data['meta']['_wpsc_product_metadata']['table_rate_price']['table_price'][ $key ] ); 
  92. unset( $post_data['meta']['_wpsc_product_metadata']['table_rate_price']['quantity'][ $key ] ); 
  93. foreach ( (array) $post_data['meta']['_wpsc_product_metadata']['table_rate_price']['table_price'] as $key => $value ) { 
  94. if ( empty( $value ) ) { 
  95. unset( $post_data['meta']['_wpsc_product_metadata']['table_rate_price']['table_price'][ $key ] ); 
  96. unset( $post_data['meta']['_wpsc_product_metadata']['table_rate_price']['quantity'][ $key ] ); 
  97.  
  98. if ( isset( $post_data['meta']['_wpsc_product_metadata']['shipping'] ) ) { 
  99. $post_data['meta']['_wpsc_product_metadata']['shipping']['local'] = wpsc_string_to_float( $post_data['meta']['_wpsc_product_metadata']['shipping']['local'] ); 
  100. $post_data['meta']['_wpsc_product_metadata']['shipping']['international'] = wpsc_string_to_float( $post_data['meta']['_wpsc_product_metadata']['shipping']['international'] ); 
  101.  
  102. // Update product taxes 
  103. if ( isset( $_POST['wpsc_product_tax_nonce'] ) && wp_verify_nonce( $_POST['wpsc_product_tax_nonce'], 'update' ) ) { 
  104.  
  105. $post_data['meta']['_wpsc_product_metadata'] = wp_parse_args( $post_data['meta']['_wpsc_product_metadata'], array( 
  106. 'wpec_taxes_taxable_amount' => '',  
  107. 'wpec_taxes_taxable' => '' 
  108. ) ); 
  109. if ( ! empty( $post_data['meta']['_wpsc_product_metadata']['wpec_taxes_taxable_amount'] ) ) { 
  110. $post_data['meta']['_wpsc_product_metadata']['wpec_taxes_taxable_amount'] = wpsc_string_to_float($post_data['meta']['_wpsc_product_metadata']['wpec_taxes_taxable_amount'] ); 
  111. $post_data['meta']['_wpsc_product_metadata']['wpec_taxes_taxable'] = $post_data['meta']['_wpsc_product_metadata']['wpec_taxes_taxable']; 
  112.  
  113.  
  114. // External Link Options 
  115. if ( isset( $_POST['wpsc_product_external_link_nonce'] ) && wp_verify_nonce( $_POST['wpsc_product_external_link_nonce'], 'update' ) ) { 
  116.  
  117. // Parse post meta to ensure default values 
  118. $post_data['meta']['_wpsc_product_metadata'] = wp_parse_args( $post_data['meta']['_wpsc_product_metadata'], array( 
  119. 'external_link' => '',  
  120. 'external_link_text' => '',  
  121. 'external_link_target' => '' 
  122. ) ); 
  123.  
  124.  
  125. // Advanced Options 
  126. if ( isset( $_POST['wpsc_product_personalization_nonce'] ) && wp_verify_nonce( $_POST['wpsc_product_personalization_nonce'], 'update' ) ) { 
  127.  
  128. // Parse post meta to ensure default values (especially checkboxes) 
  129. $post_data['meta']['_wpsc_product_metadata'] = wp_parse_args( $post_data['meta']['_wpsc_product_metadata'], array( 
  130. 'engraved' => 0,  
  131. 'can_have_uploaded_image' => 0 
  132. ) ); 
  133.  
  134. $post_data['meta']['_wpsc_product_metadata']['engraved'] = absint( (bool) $post_data['meta']['_wpsc_product_metadata']['engraved'] ); 
  135. $post_data['meta']['_wpsc_product_metadata']['can_have_uploaded_image'] = absint( (bool) $post_data['meta']['_wpsc_product_metadata']['can_have_uploaded_image'] ); 
  136.  
  137.  
  138. if ( ! isset($post_data['meta']['_wpsc_product_metadata']['google_prohibited'])) $post_data['meta']['_wpsc_product_metadata']['google_prohibited'] = ''; 
  139. $post_data['meta']['_wpsc_product_metadata']['google_prohibited'] = (int)(bool)$post_data['meta']['_wpsc_product_metadata']['google_prohibited']; 
  140.  
  141. // Fill in any missing meta values with existing values. 
  142. $post_data['meta'] = wp_parse_args( $post_data['meta'], array( 
  143. '_wpsc_is_donation' => get_product_meta( $product_id, 'is_donation', true ) 
  144. ) ); 
  145.  
  146. // Fill in any missing product meta values with existing values. 
  147. $default_meta_values = wp_parse_args( get_product_meta( $product_id, 'product_metadata', true ), array( 
  148. 'notify_when_none_left' => 0,  
  149. 'unpublish_when_none_left' => 0,  
  150. 'no_shipping' => 0,  
  151. 'external_link' => '',  
  152. 'external_link_text' => '',  
  153. 'external_link_target' => '',  
  154. 'engraved' => 0,  
  155. 'can_have_uploaded_image' => 0 
  156. ) ); 
  157.  
  158. $post_data['meta']['_wpsc_product_metadata'] = wp_parse_args( $post_data['meta']['_wpsc_product_metadata'], $default_meta_values ); 
  159.  
  160. $post_data['files'] = $_FILES; 
  161.  
  162. if(isset($post_data['post_title']) && $post_data['post_title'] != '') { 
  163.  
  164. $product_columns = array( 
  165. 'name' => '',  
  166. 'description' => '',  
  167. 'additional_description' => '',  
  168. 'price' => null,  
  169. 'weight' => null,  
  170. 'weight_unit' => '',  
  171. 'pnp' => null,  
  172. 'international_pnp' => null,  
  173. 'file' => null,  
  174. 'image' => '0',  
  175. 'quantity_limited' => '',  
  176. 'quantity' => null,  
  177. 'special' => null,  
  178. 'special_price' => null,  
  179. 'display_frontpage' => null,  
  180. 'notax' => null,  
  181. 'publish' => null,  
  182. 'active' => null,  
  183. 'donation' => null,  
  184. 'no_shipping' => null,  
  185. 'thumbnail_image' => null,  
  186. 'thumbnail_state' => null 
  187. ); 
  188.  
  189. foreach ( $product_columns as $column => $default ) { 
  190. if ( ! isset( $post_data[ $column ] ) ) { 
  191. $post_data[ $column ] = ''; 
  192.  
  193. // if we succeed, we can do further editing (todo - if_wp_error) 
  194.  
  195. // if we have no categories selected, assign one. 
  196. 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) { 
  197. $post_data['tax_input']['wpsc_product_category'][1] = wpsc_add_product_category_default($product_id); 
  198.  
  199. // and the meta 
  200. wpsc_update_product_meta($product_id, $post_data['meta']); 
  201.  
  202. // and the custom meta 
  203. wpsc_update_custom_meta($product_id, $post_data); 
  204.  
  205.  
  206. // Update the alternative currencies 
  207. if ( isset( $post_data['wpsc-update-currency-layers'] ) && wp_verify_nonce( $post_data['wpsc-update-currency-layers'], 'update-options' ) ) { 
  208.  
  209. // Clear currencies before re-saving to make sure deleted currencies are removed 
  210. update_product_meta( $product_id, 'currency', array() ); 
  211.  
  212. if ( ! empty( $post_data['newCurrency'] ) ) { 
  213. foreach( (array) $post_data['newCurrency'] as $key =>$value ) { 
  214. wpsc_update_alt_product_currency( $product_id, $value, $post_data['newCurrPrice'][ $key ] ); 
  215.  
  216. if ( isset( $post_data['files']['file'] ) && $post_data['files']['file']['tmp_name'] != '' ) { 
  217. wpsc_item_process_file($product_id, $post_data['files']['file']); 
  218. } else { 
  219. if (!isset($post_data['select_product_file'])) $post_data['select_product_file'] = null; 
  220. wpsc_item_reassign_file($product_id, $post_data['select_product_file']); 
  221.  
  222. if(isset($post_data['files']['preview_file']['tmp_name']) && ($post_data['files']['preview_file']['tmp_name'] != '')) { 
  223. wpsc_item_add_preview_file($product_id, $post_data['files']['preview_file']); 
  224. do_action('wpsc_edit_product', $product_id); 
  225. return $product_id;