/includes/admin/meta-boxes/class-wc-meta-box-product-data.php

  1. <?php 
  2. /** 
  3. * Product Data 
  4. * 
  5. * Displays the product data box, tabbed, with several panels covering price, stock etc. 
  6. * 
  7. * @author WooThemes 
  8. * @category Admin 
  9. * @package WooCommerce/Admin/Meta Boxes 
  10. * @version 3.0.0 
  11. */ 
  12.  
  13. if ( ! defined( 'ABSPATH' ) ) { 
  14. exit; 
  15.  
  16. /** 
  17. * WC_Meta_Box_Product_Data Class. 
  18. */ 
  19. class WC_Meta_Box_Product_Data { 
  20.  
  21. /** 
  22. * Output the metabox. 
  23. * 
  24. * @param WP_Post $post 
  25. */ 
  26. public static function output( $post ) { 
  27. global $post, $thepostid, $product_object; 
  28.  
  29. $thepostid = $post->ID; 
  30. $product_object = $thepostid ? wc_get_product( $thepostid ) : new WC_Product; 
  31.  
  32. include( 'views/html-product-data-panel.php' ); 
  33.  
  34. /** 
  35. * Show tab content/settings. 
  36. */ 
  37. private static function output_tabs() { 
  38. global $post, $thepostid, $product_object; 
  39.  
  40. include( 'views/html-product-data-general.php' ); 
  41. include( 'views/html-product-data-inventory.php' ); 
  42. include( 'views/html-product-data-shipping.php' ); 
  43. include( 'views/html-product-data-linked-products.php' ); 
  44. include( 'views/html-product-data-attributes.php' ); 
  45. include( 'views/html-product-data-advanced.php' ); 
  46.  
  47. /** 
  48. * Return array of product type options. 
  49. * @return array 
  50. */ 
  51. private static function get_product_type_options() { 
  52. return apply_filters( 'product_type_options', array( 
  53. 'virtual' => array( 
  54. 'id' => '_virtual',  
  55. 'wrapper_class' => 'show_if_simple',  
  56. 'label' => __( 'Virtual', 'woocommerce' ),  
  57. 'description' => __( 'Virtual products are intangible and aren\'t shipped.', 'woocommerce' ),  
  58. 'default' => 'no',  
  59. ),  
  60. 'downloadable' => array( 
  61. 'id' => '_downloadable',  
  62. 'wrapper_class' => 'show_if_simple',  
  63. 'label' => __( 'Downloadable', 'woocommerce' ),  
  64. 'description' => __( 'Downloadable products give access to a file upon purchase.', 'woocommerce' ),  
  65. 'default' => 'no',  
  66. ),  
  67. ) ); 
  68.  
  69. /** 
  70. * Return array of tabs to show. 
  71. * @return array 
  72. */ 
  73. private static function get_product_data_tabs() { 
  74. return apply_filters( 'woocommerce_product_data_tabs', array( 
  75. 'general' => array( 
  76. 'label' => __( 'General', 'woocommerce' ),  
  77. 'target' => 'general_product_data',  
  78. 'class' => array( 'hide_if_grouped' ),  
  79. ),  
  80. 'inventory' => array( 
  81. 'label' => __( 'Inventory', 'woocommerce' ),  
  82. 'target' => 'inventory_product_data',  
  83. 'class' => array( 'show_if_simple', 'show_if_variable', 'show_if_grouped', 'show_if_external' ),  
  84. ),  
  85. 'shipping' => array( 
  86. 'label' => __( 'Shipping', 'woocommerce' ),  
  87. 'target' => 'shipping_product_data',  
  88. 'class' => array( 'hide_if_virtual', 'hide_if_grouped', 'hide_if_external' ),  
  89. ),  
  90. 'linked_product' => array( 
  91. 'label' => __( 'Linked Products', 'woocommerce' ),  
  92. 'target' => 'linked_product_data',  
  93. 'class' => array(),  
  94. ),  
  95. 'attribute' => array( 
  96. 'label' => __( 'Attributes', 'woocommerce' ),  
  97. 'target' => 'product_attributes',  
  98. 'class' => array(),  
  99. ),  
  100. 'variations' => array( 
  101. 'label' => __( 'Variations', 'woocommerce' ),  
  102. 'target' => 'variable_product_options',  
  103. 'class' => array( 'variations_tab', 'show_if_variable' ),  
  104. ),  
  105. 'advanced' => array( 
  106. 'label' => __( 'Advanced', 'woocommerce' ),  
  107. 'target' => 'advanced_product_data',  
  108. 'class' => array(),  
  109. ),  
  110. ) ); 
  111.  
  112. /** 
  113. * Filter callback for finding variation attributes. 
  114. * @param WC_Product_Attribute $attribute 
  115. * @return bool 
  116. */ 
  117. private static function filter_variation_attributes( $attribute ) { 
  118. return true === $attribute->get_variation(); 
  119.  
  120. /** 
  121. * Show options for the variable product type. 
  122. */ 
  123. public static function output_variations() { 
  124. global $post, $wpdb, $product_object; 
  125.  
  126. $variation_attributes = array_filter( $product_object->get_attributes(), array( __CLASS__, 'filter_variation_attributes' ) ); 
  127. $default_attributes = $product_object->get_default_attributes(); 
  128. $variations_count = absint( $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(ID) FROM $wpdb->posts WHERE post_parent = %d AND post_type = 'product_variation' AND post_status IN ('publish', 'private')", $post->ID ) ) ); 
  129. $variations_per_page = absint( apply_filters( 'woocommerce_admin_meta_boxes_variations_per_page', 15 ) ); 
  130. $variations_total_pages = ceil( $variations_count / $variations_per_page ); 
  131.  
  132. include( 'views/html-product-data-variations.php' ); 
  133.  
  134. /** 
  135. * Prepare downloads for save. 
  136. * @return array 
  137. */ 
  138. private static function prepare_downloads( $file_names, $file_urls, $file_hashes ) { 
  139. $downloads = array(); 
  140.  
  141. if ( ! empty( $file_urls ) ) { 
  142. $file_url_size = sizeof( $file_urls ); 
  143.  
  144. for ( $i = 0; $i < $file_url_size; $i ++ ) { 
  145. if ( ! empty( $file_urls[ $i ] ) ) { 
  146. $downloads[] = array( 
  147. 'name' => wc_clean( $file_names[ $i ] ),  
  148. 'file' => wp_unslash( trim( $file_urls[ $i ] ) ),  
  149. 'previous_hash' => wc_clean( $file_hashes[ $i ] ),  
  150. ); 
  151. return $downloads; 
  152.  
  153. /** 
  154. * Prepare children for save. 
  155. * @return array 
  156. */ 
  157. private static function prepare_children() { 
  158. return isset( $_POST['grouped_products'] ) ? array_filter( array_map( 'intval', (array) $_POST['grouped_products'] ) ) : array(); 
  159.  
  160. /** 
  161. * Prepare attributes for save. 
  162. * @return array 
  163. */ 
  164. public static function prepare_attributes( $data = false ) { 
  165. $attributes = array(); 
  166.  
  167. if ( ! $data ) { 
  168. $data = $_POST; 
  169.  
  170. if ( isset( $data['attribute_names'], $data['attribute_values'] ) ) { 
  171. $attribute_names = $data['attribute_names']; 
  172. $attribute_values = $data['attribute_values']; 
  173. $attribute_visibility = isset( $data['attribute_visibility'] ) ? $data['attribute_visibility'] : array(); 
  174. $attribute_variation = isset( $data['attribute_variation'] ) ? $data['attribute_variation'] : array(); 
  175. $attribute_position = $data['attribute_position']; 
  176. $attribute_names_max_key = max( array_keys( $attribute_names ) ); 
  177.  
  178. for ( $i = 0; $i <= $attribute_names_max_key; $i++ ) { 
  179. if ( empty( $attribute_names[ $i ] ) || ! isset( $attribute_values[ $i ] ) ) { 
  180. continue; 
  181. $attribute_id = 0; 
  182. $attribute_name = wc_clean( $attribute_names[ $i ] ); 
  183.  
  184. if ( 'pa_' === substr( $attribute_name, 0, 3 ) ) { 
  185. $attribute_id = wc_attribute_taxonomy_id_by_name( $attribute_name ); 
  186.  
  187. $options = isset( $attribute_values[ $i ] ) ? $attribute_values[ $i ] : ''; 
  188.  
  189. if ( is_array( $options ) ) { 
  190. // Term ids sent as array. 
  191. $options = wp_parse_id_list( $options ); 
  192. } else { 
  193. // Terms or text sent in textarea. 
  194. $options = 0 < $attribute_id ? wc_sanitize_textarea( wc_sanitize_term_text_based( $options ) ) : wc_sanitize_textarea( $options ); 
  195. $options = wc_get_text_attributes( $options ); 
  196.  
  197. if ( empty( $options ) ) { 
  198. continue; 
  199.  
  200. $attribute = new WC_Product_Attribute(); 
  201. $attribute->set_id( $attribute_id ); 
  202. $attribute->set_name( $attribute_name ); 
  203. $attribute->set_options( $options ); 
  204. $attribute->set_position( $attribute_position[ $i ] ); 
  205. $attribute->set_visible( isset( $attribute_visibility[ $i ] ) ); 
  206. $attribute->set_variation( isset( $attribute_variation[ $i ] ) ); 
  207. $attributes[] = $attribute; 
  208. return $attributes; 
  209.  
  210. /** 
  211. * Prepare attributes for a specific variation or defaults. 
  212. * @param array $all_attributes 
  213. * @param string $key_prefix 
  214. * @param int $index 
  215. * @return array 
  216. */ 
  217. private static function prepare_set_attributes( $all_attributes, $key_prefix = 'attribute_', $index = null ) { 
  218. $attributes = array(); 
  219.  
  220. if ( $all_attributes ) { 
  221. foreach ( $all_attributes as $attribute ) { 
  222. if ( $attribute->get_variation() ) { 
  223. $attribute_key = sanitize_title( $attribute->get_name() ); 
  224.  
  225. if ( ! is_null( $index ) ) { 
  226. $value = isset( $_POST[ $key_prefix . $attribute_key ][ $index ] ) ? stripslashes( $_POST[ $key_prefix . $attribute_key ][ $index ] ) : ''; 
  227. } else { 
  228. $value = isset( $_POST[ $key_prefix . $attribute_key ] ) ? stripslashes( $_POST[ $key_prefix . $attribute_key ] ) : ''; 
  229.  
  230. $value = $attribute->is_taxonomy() ? sanitize_title( $value ) : wc_clean( $value ); // Don't use wc_clean as it destroys sanitized characters in terms. 
  231. $attributes[ $attribute_key ] = $value; 
  232.  
  233. return $attributes; 
  234.  
  235. /** 
  236. * Save meta box data. 
  237. */ 
  238. public static function save( $post_id, $post ) { 
  239. // Process product type first so we have the correct class to run setters. 
  240. $product_type = empty( $_POST['product-type'] ) ? WC_Product_Factory::get_product_type( $post_id ) : sanitize_title( stripslashes( $_POST['product-type'] ) ); 
  241. $classname = WC_Product_Factory::get_product_classname( $post_id, $product_type ? $product_type : 'simple' ); 
  242. $product = new $classname( $post_id ); 
  243. $attributes = self::prepare_attributes(); 
  244. $errors = $product->set_props( array( 
  245. 'sku' => isset( $_POST['_sku'] ) ? wc_clean( $_POST['_sku'] ) : null,  
  246. 'purchase_note' => wp_kses_post( stripslashes( $_POST['_purchase_note'] ) ),  
  247. 'downloadable' => isset( $_POST['_downloadable'] ),  
  248. 'virtual' => isset( $_POST['_virtual'] ),  
  249. 'featured' => isset( $_POST['_featured'] ),  
  250. 'catalog_visibility' => wc_clean( $_POST['_visibility'] ),  
  251. 'tax_status' => isset( $_POST['_tax_status'] ) ? wc_clean( $_POST['_tax_status'] ) : null,  
  252. 'tax_class' => isset( $_POST['_tax_class'] ) ? wc_clean( $_POST['_tax_class'] ) : null,  
  253. 'weight' => wc_clean( $_POST['_weight'] ),  
  254. 'length' => wc_clean( $_POST['_length'] ),  
  255. 'width' => wc_clean( $_POST['_width'] ),  
  256. 'height' => wc_clean( $_POST['_height'] ),  
  257. 'shipping_class_id' => absint( $_POST['product_shipping_class'] ),  
  258. 'sold_individually' => ! empty( $_POST['_sold_individually'] ),  
  259. 'upsell_ids' => isset( $_POST['upsell_ids'] ) ? array_map( 'intval', (array) $_POST['upsell_ids'] ) : array(),  
  260. 'cross_sell_ids' => isset( $_POST['crosssell_ids'] ) ? array_map( 'intval', (array) $_POST['crosssell_ids'] ) : array(),  
  261. 'regular_price' => wc_clean( $_POST['_regular_price'] ),  
  262. 'sale_price' => wc_clean( $_POST['_sale_price'] ),  
  263. 'date_on_sale_from' => wc_clean( $_POST['_sale_price_dates_from'] ),  
  264. 'date_on_sale_to' => wc_clean( $_POST['_sale_price_dates_to'] ),  
  265. 'manage_stock' => ! empty( $_POST['_manage_stock'] ),  
  266. 'backorders' => wc_clean( $_POST['_backorders'] ),  
  267. 'stock_status' => wc_clean( $_POST['_stock_status'] ),  
  268. 'stock_quantity' => wc_stock_amount( $_POST['_stock'] ),  
  269. 'download_limit' => '' === $_POST['_download_limit'] ? '' : absint( $_POST['_download_limit'] ),  
  270. 'download_expiry' => '' === $_POST['_download_expiry'] ? '' : absint( $_POST['_download_expiry'] ),  
  271. 'downloads' => self::prepare_downloads( 
  272. isset( $_POST['_wc_file_names'] ) ? $_POST['_wc_file_names'] : array(),  
  273. isset( $_POST['_wc_file_urls'] ) ? $_POST['_wc_file_urls'] : array(),  
  274. isset( $_POST['_wc_file_hashes'] ) ? $_POST['_wc_file_hashes'] : array() 
  275. ),  
  276. 'product_url' => esc_url_raw( $_POST['_product_url'] ),  
  277. 'button_text' => wc_clean( $_POST['_button_text'] ),  
  278. 'children' => 'grouped' === $product_type ? self::prepare_children() : null,  
  279. 'reviews_allowed' => ! empty( $_POST['_reviews_allowed'] ),  
  280. 'attributes' => $attributes,  
  281. 'default_attributes' => self::prepare_set_attributes( $attributes, 'default_attribute_' ),  
  282. ) ); 
  283.  
  284. if ( is_wp_error( $errors ) ) { 
  285. WC_Admin_Meta_Boxes::add_error( $errors->get_error_message() ); 
  286.  
  287. /** 
  288. * @since 3.0.0 to set props before save. 
  289. */ 
  290. do_action( 'woocommerce_admin_process_product_object', $product ); 
  291.  
  292. $product->save(); 
  293.  
  294. if ( $product->is_type( 'variable' ) ) { 
  295. $product->get_data_store()->sync_variation_names( $product, wc_clean( $_POST['original_post_title'] ), wc_clean( $_POST['post_title'] ) ); 
  296.  
  297. do_action( 'woocommerce_process_product_meta_' . $product_type, $post_id ); 
  298.  
  299. /** 
  300. * Save meta box data. 
  301. * 
  302. * @param int $post_id 
  303. * @param WP_Post $post 
  304. */ 
  305. public static function save_variations( $post_id, $post ) { 
  306. if ( isset( $_POST['variable_post_id'] ) ) { 
  307. $parent = wc_get_product( $post_id ); 
  308. $parent->set_default_attributes( self::prepare_set_attributes( $parent->get_attributes(), 'default_attribute_' ) ); 
  309. $parent->save(); 
  310.  
  311. $max_loop = max( array_keys( $_POST['variable_post_id'] ) ); 
  312. $data_store = $parent->get_data_store(); 
  313. $data_store->sort_all_product_variations( $parent->get_id() ); 
  314.  
  315. for ( $i = 0; $i <= $max_loop; $i ++ ) { 
  316.  
  317. if ( ! isset( $_POST['variable_post_id'][ $i ] ) ) { 
  318. continue; 
  319. $variation_id = absint( $_POST['variable_post_id'][ $i ] ); 
  320. $variation = new WC_Product_Variation( $variation_id ); 
  321. $errors = $variation->set_props( array( 
  322. 'status' => isset( $_POST['variable_enabled'][ $i ] ) ? 'publish' : 'private',  
  323. 'menu_order' => wc_clean( $_POST['variation_menu_order'][ $i ] ),  
  324. 'regular_price' => wc_clean( $_POST['variable_regular_price'][ $i ] ),  
  325. 'sale_price' => wc_clean( $_POST['variable_sale_price'][ $i ] ),  
  326. 'virtual' => isset( $_POST['variable_is_virtual'][ $i ] ),  
  327. 'downloadable' => isset( $_POST['variable_is_downloadable'][ $i ] ),  
  328. 'date_on_sale_from' => wc_clean( $_POST['variable_sale_price_dates_from'][ $i ] ),  
  329. 'date_on_sale_to' => wc_clean( $_POST['variable_sale_price_dates_to'][ $i ] ),  
  330. 'description' => wp_kses_post( $_POST['variable_description'][ $i ] ),  
  331. 'download_limit' => wc_clean( $_POST['variable_download_limit'][ $i ] ),  
  332. 'download_expiry' => wc_clean( $_POST['variable_download_expiry'][ $i ] ),  
  333. 'downloads' => self::prepare_downloads( 
  334. isset( $_POST['_wc_variation_file_names'][ $variation_id ] ) ? $_POST['_wc_variation_file_names'][ $variation_id ] : array(),  
  335. isset( $_POST['_wc_variation_file_urls'][ $variation_id ] ) ? $_POST['_wc_variation_file_urls'][ $variation_id ] : array(),  
  336. isset( $_POST['_wc_variation_file_hashes'][ $variation_id ] ) ? $_POST['_wc_variation_file_hashes'][ $variation_id ] : array() 
  337. ),  
  338. 'manage_stock' => isset( $_POST['variable_manage_stock'][ $i ] ),  
  339. 'stock_quantity' => wc_clean( $_POST['variable_stock'][ $i ] ),  
  340. 'backorders' => wc_clean( $_POST['variable_backorders'][ $i ] ),  
  341. 'stock_status' => wc_clean( $_POST['variable_stock_status'][ $i ] ),  
  342. 'image_id' => wc_clean( $_POST['upload_image_id'][ $i ] ),  
  343. 'attributes' => self::prepare_set_attributes( $parent->get_attributes(), 'attribute_', $i ),  
  344. 'sku' => isset( $_POST['variable_sku'][ $i ] ) ? wc_clean( $_POST['variable_sku'][ $i ] ) : '',  
  345. 'weight' => isset( $_POST['variable_weight'][ $i ] ) ? wc_clean( $_POST['variable_weight'][ $i ] ) : '',  
  346. 'length' => isset( $_POST['variable_length'][ $i ] ) ? wc_clean( $_POST['variable_length'][ $i ] ) : '',  
  347. 'width' => isset( $_POST['variable_width'][ $i ] ) ? wc_clean( $_POST['variable_width'][ $i ] ) : '',  
  348. 'height' => isset( $_POST['variable_height'][ $i ] ) ? wc_clean( $_POST['variable_height'][ $i ] ) : '',  
  349. 'shipping_class_id' => wc_clean( $_POST['variable_shipping_class'][ $i ] ),  
  350. 'tax_class' => isset( $_POST['variable_tax_class'][ $i ] ) ? wc_clean( $_POST['variable_tax_class'][ $i ] ) : null,  
  351. ) ); 
  352.  
  353. if ( is_wp_error( $errors ) ) { 
  354. WC_Admin_Meta_Boxes::add_error( $errors->get_error_message() ); 
  355.  
  356. $variation->save(); 
  357.  
  358. do_action( 'woocommerce_save_product_variation', $variation_id, $i ); 
.