/includes/class-wc-post-data.php

  1. <?php 
  2.  
  3. if ( ! defined( 'ABSPATH' ) ) { 
  4. exit; // Exit if accessed directly 
  5.  
  6. /** 
  7. * Post Data. 
  8. * 
  9. * Standardises certain post data on save. 
  10. * 
  11. * @class WC_Post_Data 
  12. * @version 2.2.0 
  13. * @package WooCommerce/Classes/Data 
  14. * @category Class 
  15. * @author WooThemes 
  16. */ 
  17. class WC_Post_Data { 
  18.  
  19. /** 
  20. * Editing term. 
  21. * 
  22. * @var object 
  23. */ 
  24. private static $editing_term = null; 
  25.  
  26. /** 
  27. * Hook in methods. 
  28. */ 
  29. public static function init() { 
  30. add_filter( 'post_type_link', array( __CLASS__, 'variation_post_link' ), 10, 2 ); 
  31. add_action( 'shutdown', array( __CLASS__, 'do_deferred_product_sync' ), 10 ); 
  32. add_action( 'set_object_terms', array( __CLASS__, 'set_object_terms' ), 10, 6 ); 
  33.  
  34. add_action( 'transition_post_status', array( __CLASS__, 'transition_post_status' ), 10, 3 ); 
  35. add_action( 'woocommerce_product_set_stock_status', array( __CLASS__, 'delete_product_query_transients' ) ); 
  36. add_action( 'woocommerce_product_set_visibility', array( __CLASS__, 'delete_product_query_transients' ) ); 
  37. add_action( 'woocommerce_product_type_changed', array( __CLASS__, 'product_type_changed' ), 10, 3 ); 
  38.  
  39. add_action( 'edit_term', array( __CLASS__, 'edit_term' ), 10, 3 ); 
  40. add_action( 'edited_term', array( __CLASS__, 'edited_term' ), 10, 3 ); 
  41. add_filter( 'update_order_item_metadata', array( __CLASS__, 'update_order_item_metadata' ), 10, 5 ); 
  42. add_filter( 'update_post_metadata', array( __CLASS__, 'update_post_metadata' ), 10, 5 ); 
  43. add_filter( 'wp_insert_post_data', array( __CLASS__, 'wp_insert_post_data' ) ); 
  44.  
  45. // Status transitions 
  46. add_action( 'delete_post', array( __CLASS__, 'delete_post' ) ); 
  47. add_action( 'wp_trash_post', array( __CLASS__, 'trash_post' ) ); 
  48. add_action( 'untrashed_post', array( __CLASS__, 'untrash_post' ) ); 
  49. add_action( 'before_delete_post', array( __CLASS__, 'delete_order_items' ) ); 
  50. add_action( 'before_delete_post', array( __CLASS__, 'delete_order_downloadable_permissions' ) ); 
  51.  
  52. // Download permissions 
  53. add_action( 'woocommerce_process_product_file_download_paths', array( __CLASS__, 'process_product_file_download_paths' ), 10, 3 ); 
  54.  
  55. /** 
  56. * Link to parent products when getting permalink for variation. 
  57. * 
  58. * @return string 
  59. */ 
  60. public static function variation_post_link( $permalink, $post ) { 
  61. if ( isset( $post->ID, $post->post_type ) && 'product_variation' === $post->post_type && ( $variation = wc_get_product( $post->ID ) ) ) { 
  62. return $variation->get_permalink(); 
  63. return $permalink; 
  64.  
  65. /** 
  66. * Sync products queued to sync. 
  67. */ 
  68. public static function do_deferred_product_sync() { 
  69. global $wc_deferred_product_sync; 
  70.  
  71. if ( ! empty( $wc_deferred_product_sync ) ) { 
  72. $wc_deferred_product_sync = wp_parse_id_list( $wc_deferred_product_sync ); 
  73. array_walk( $wc_deferred_product_sync, array( __CLASS__, 'deferred_product_sync' ) ); 
  74.  
  75. /** 
  76. * Sync a product. 
  77. * @param int $product_id 
  78. */ 
  79. public static function deferred_product_sync( $product_id ) { 
  80. $product = wc_get_product( $product_id ); 
  81.  
  82. if ( is_callable( array( $product, 'sync' ) ) ) { 
  83. $product->sync( $product ); 
  84.  
  85. /** 
  86. * Delete transients when terms are set. 
  87. */ 
  88. public static function set_object_terms( $object_id, $terms, $tt_ids, $taxonomy, $append, $old_tt_ids ) { 
  89. foreach ( array_merge( $tt_ids, $old_tt_ids ) as $id ) { 
  90. delete_transient( 'wc_ln_count_' . md5( sanitize_key( $taxonomy ) . sanitize_key( $id ) ) ); 
  91.  
  92. /** 
  93. * When a post status changes. 
  94. */ 
  95. public static function transition_post_status( $new_status, $old_status, $post ) { 
  96. if ( ( 'publish' === $new_status || 'publish' === $old_status ) && in_array( $post->post_type, array( 'product', 'product_variation' ) ) ) { 
  97. self::delete_product_query_transients(); 
  98.  
  99. /** 
  100. * Delete product view transients when needed e.g. when post status changes, or visibility/stock status is modified. 
  101. */ 
  102. public static function delete_product_query_transients() { 
  103. // Increments the transient version to invalidate cache 
  104. WC_Cache_Helper::get_transient_version( 'product_query', true ); 
  105.  
  106. // If not using an external caching system, we can clear the transients out manually and avoid filling our DB 
  107. if ( ! wp_using_ext_object_cache() ) { 
  108. global $wpdb; 
  109.  
  110. $wpdb->query( " 
  111. DELETE FROM `$wpdb->options` 
  112. WHERE `option_name` LIKE ('\_transient\_wc\_uf\_pid\_%') 
  113. OR `option_name` LIKE ('\_transient\_timeout\_wc\_uf\_pid\_%') 
  114. OR `option_name` LIKE ('\_transient\_wc\_products\_will\_display\_%') 
  115. OR `option_name` LIKE ('\_transient\_timeout\_wc\_products\_will\_display\_%') 
  116. " ); 
  117.  
  118. /** 
  119. * Handle type changes. 
  120. * 
  121. * @since 3.0.0 
  122. * @param WC_Product $product 
  123. * @param string $from 
  124. * @param string $to 
  125. */ 
  126. public static function product_type_changed( $product, $from, $to ) { 
  127. if ( 'variable' === $from && 'variable' !== $to ) { 
  128. // If the product is no longer variable, we should ensure all variations are removed. 
  129. $data_store = WC_Data_Store::load( 'product-variable' ); 
  130. $data_store->delete_variations( $product->get_id() ); 
  131.  
  132. /** 
  133. * When editing a term, check for product attributes. 
  134. * @param id $term_id 
  135. * @param id $tt_id 
  136. * @param string $taxonomy 
  137. */ 
  138. public static function edit_term( $term_id, $tt_id, $taxonomy ) { 
  139. if ( strpos( $taxonomy, 'pa_' ) === 0 ) { 
  140. self::$editing_term = get_term_by( 'id', $term_id, $taxonomy ); 
  141. } else { 
  142. self::$editing_term = null; 
  143.  
  144. /** 
  145. * When a term is edited, check for product attributes and update variations. 
  146. * @param id $term_id 
  147. * @param id $tt_id 
  148. * @param string $taxonomy 
  149. */ 
  150. public static function edited_term( $term_id, $tt_id, $taxonomy ) { 
  151. if ( ! is_null( self::$editing_term ) && strpos( $taxonomy, 'pa_' ) === 0 ) { 
  152. $edited_term = get_term_by( 'id', $term_id, $taxonomy ); 
  153.  
  154. if ( $edited_term->slug !== self::$editing_term->slug ) { 
  155. global $wpdb; 
  156.  
  157. $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->postmeta} SET meta_value = %s WHERE meta_key = %s AND meta_value = %s;", $edited_term->slug, 'attribute_' . sanitize_title( $taxonomy ), self::$editing_term->slug ) ); 
  158. } else { 
  159. self::$editing_term = null; 
  160.  
  161. /** 
  162. * Ensure floats are correctly converted to strings based on PHP locale. 
  163. * 
  164. * @param null $check 
  165. * @param int $object_id 
  166. * @param string $meta_key 
  167. * @param mixed $meta_value 
  168. * @param mixed $prev_value 
  169. * @return null|bool 
  170. */ 
  171. public static function update_order_item_metadata( $check, $object_id, $meta_key, $meta_value, $prev_value ) { 
  172. if ( ! empty( $meta_value ) && is_float( $meta_value ) ) { 
  173.  
  174. // Convert float to string 
  175. $meta_value = wc_float_to_string( $meta_value ); 
  176.  
  177. // Update meta value with new string 
  178. update_metadata( 'order_item', $object_id, $meta_key, $meta_value, $prev_value ); 
  179.  
  180. // Return 
  181. return true; 
  182. return $check; 
  183.  
  184. /** 
  185. * Ensure floats are correctly converted to strings based on PHP locale. 
  186. * 
  187. * @param null $check 
  188. * @param int $object_id 
  189. * @param string $meta_key 
  190. * @param mixed $meta_value 
  191. * @param mixed $prev_value 
  192. * @return null|bool 
  193. */ 
  194. public static function update_post_metadata( $check, $object_id, $meta_key, $meta_value, $prev_value ) { 
  195. // Delete product cache if someone uses meta directly. 
  196. if ( in_array( get_post_type( $object_id ), array( 'product', 'product_variation' ) ) ) { 
  197. wp_cache_delete( 'product-' . $object_id, 'products' ); 
  198.  
  199. if ( ! empty( $meta_value ) && is_float( $meta_value ) && in_array( get_post_type( $object_id ), array_merge( wc_get_order_types(), array( 'shop_coupon', 'product', 'product_variation' ) ) ) ) { 
  200.  
  201. // Convert float to string 
  202. $meta_value = wc_float_to_string( $meta_value ); 
  203.  
  204. // Update meta value with new string 
  205. update_metadata( 'post', $object_id, $meta_key, $meta_value, $prev_value ); 
  206.  
  207. // Return 
  208. return true; 
  209. return $check; 
  210.  
  211. /** 
  212. * When setting stock level, ensure the stock status is kept in sync. 
  213. * @param int $meta_id 
  214. * @param int $object_id 
  215. * @param string $meta_key 
  216. * @param mixed $meta_value 
  217. * @deprecated 
  218. */ 
  219. public static function sync_product_stock_status( $meta_id, $object_id, $meta_key, $meta_value ) {} 
  220.  
  221. /** 
  222. * Forces the order posts to have a title in a certain format (containing the date). 
  223. * Forces certain product data based on the product's type, e.g. grouped products cannot have a parent. 
  224. * 
  225. * @param array $data 
  226. * @return array 
  227. */ 
  228. public static function wp_insert_post_data( $data ) { 
  229. if ( 'shop_order' === $data['post_type'] && isset( $data['post_date'] ) ) { 
  230. $order_title = 'Order'; 
  231. if ( $data['post_date'] ) { 
  232. $order_title .= ' – ' . date_i18n( 'F j, Y @ h:i A', strtotime( $data['post_date'] ) ); 
  233. $data['post_title'] = $order_title; 
  234. } elseif ( 'product' === $data['post_type'] && isset( $_POST['product-type'] ) ) { 
  235. $product_type = stripslashes( $_POST['product-type'] ); 
  236. switch ( $product_type ) { 
  237. case 'grouped' : 
  238. case 'variable' : 
  239. $data['post_parent'] = 0; 
  240. break; 
  241. } elseif ( 'product' === $data['post_type'] && 'auto-draft' === $data['post_status'] ) { 
  242. $data['post_title'] = 'AUTO-DRAFT'; 
  243.  
  244. return $data; 
  245.  
  246. /** 
  247. * Removes variations etc belonging to a deleted post, and clears transients. 
  248. * 
  249. * @param mixed $id ID of post being deleted 
  250. */ 
  251. public static function delete_post( $id ) { 
  252. if ( ! current_user_can( 'delete_posts' ) || ! $id ) { 
  253. return; 
  254.  
  255. $post_type = get_post_type( $id ); 
  256.  
  257. switch ( $post_type ) { 
  258. case 'product' : 
  259. $data_store = WC_Data_Store::load( 'product-variable' ); 
  260. $data_store->delete_variations( $id, true ); 
  261.  
  262. if ( $parent_id = wp_get_post_parent_id( $id ) ) { 
  263. wc_delete_product_transients( $parent_id ); 
  264. break; 
  265. case 'product_variation' : 
  266. wc_delete_product_transients( wp_get_post_parent_id( $id ) ); 
  267. break; 
  268. case 'shop_order' : 
  269. global $wpdb; 
  270.  
  271. $refunds = $wpdb->get_results( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type = 'shop_order_refund' AND post_parent = %d", $id ) ); 
  272.  
  273. if ( ! is_null( $refunds ) ) { 
  274. foreach ( $refunds as $refund ) { 
  275. wp_delete_post( $refund->ID, true ); 
  276. break; 
  277.  
  278. /** 
  279. * woocommerce_trash_post function. 
  280. * 
  281. * @param mixed $id 
  282. */ 
  283. public static function trash_post( $id ) { 
  284. if ( ! $id ) { 
  285. return; 
  286.  
  287. $post_type = get_post_type( $id ); 
  288.  
  289. // If this is an order, trash any refunds too. 
  290. if ( in_array( $post_type, wc_get_order_types( 'order-count' ) ) ) { 
  291. global $wpdb; 
  292.  
  293. $refunds = $wpdb->get_results( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type = 'shop_order_refund' AND post_parent = %d", $id ) ); 
  294.  
  295. foreach ( $refunds as $refund ) { 
  296. $wpdb->update( $wpdb->posts, array( 'post_status' => 'trash' ), array( 'ID' => $refund->ID ) ); 
  297.  
  298. wc_delete_shop_order_transients( $id ); 
  299.  
  300. // If this is a product, trash children variations. 
  301. } elseif ( 'product' === $post_type ) { 
  302. $data_store = WC_Data_Store::load( 'product-variable' ); 
  303. $data_store->delete_variations( $id, false ); 
  304.  
  305. /** 
  306. * woocommerce_untrash_post function. 
  307. * 
  308. * @param mixed $id 
  309. */ 
  310. public static function untrash_post( $id ) { 
  311. if ( ! $id ) { 
  312. return; 
  313.  
  314. $post_type = get_post_type( $id ); 
  315.  
  316. if ( in_array( $post_type, wc_get_order_types( 'order-count' ) ) ) { 
  317. global $wpdb; 
  318.  
  319. $refunds = $wpdb->get_results( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type = 'shop_order_refund' AND post_parent = %d", $id ) ); 
  320.  
  321. foreach ( $refunds as $refund ) { 
  322. $wpdb->update( $wpdb->posts, array( 'post_status' => 'wc-completed' ), array( 'ID' => $refund->ID ) ); 
  323.  
  324. wc_delete_shop_order_transients( $id ); 
  325.  
  326. } elseif ( 'product' === $post_type ) { 
  327. $data_store = WC_Data_Store::load( 'product-variable' ); 
  328. $data_store->untrash_variations( $id ); 
  329.  
  330. wc_product_force_unique_sku( $id ); 
  331.  
  332. /** 
  333. * Remove item meta on permanent deletion. 
  334. */ 
  335. public static function delete_order_items( $postid ) { 
  336. global $wpdb; 
  337.  
  338. if ( in_array( get_post_type( $postid ), wc_get_order_types() ) ) { 
  339. do_action( 'woocommerce_delete_order_items', $postid ); 
  340.  
  341. $wpdb->query( " 
  342. DELETE {$wpdb->prefix}woocommerce_order_items, {$wpdb->prefix}woocommerce_order_itemmeta 
  343. FROM {$wpdb->prefix}woocommerce_order_items 
  344. JOIN {$wpdb->prefix}woocommerce_order_itemmeta ON {$wpdb->prefix}woocommerce_order_items.order_item_id = {$wpdb->prefix}woocommerce_order_itemmeta.order_item_id 
  345. WHERE {$wpdb->prefix}woocommerce_order_items.order_id = '{$postid}'; 
  346. " ); 
  347.  
  348. do_action( 'woocommerce_deleted_order_items', $postid ); 
  349.  
  350. /** 
  351. * Remove downloadable permissions on permanent order deletion. 
  352. */ 
  353. public static function delete_order_downloadable_permissions( $postid ) { 
  354. global $wpdb; 
  355.  
  356. if ( in_array( get_post_type( $postid ), wc_get_order_types() ) ) { 
  357. do_action( 'woocommerce_delete_order_downloadable_permissions', $postid ); 
  358.  
  359. $data_store = WC_Data_Store::load( 'customer-download' ); 
  360. $data_store->delete_by_order_id( $postid ); 
  361.  
  362. do_action( 'woocommerce_deleted_order_downloadable_permissions', $postid ); 
  363.  
  364. /** 
  365. * Update changed downloads. 
  366. * 
  367. * @param int $product_id product identifier 
  368. * @param int $variation_id optional product variation identifier 
  369. * @param array $downloads newly set files 
  370. */ 
  371. public static function process_product_file_download_paths( $product_id, $variation_id, $downloads ) { 
  372. if ( $variation_id ) { 
  373. $product_id = $variation_id; 
  374. $data_store = WC_Data_Store::load( 'customer-download' ); 
  375.  
  376. if ( $downloads ) { 
  377. foreach ( $downloads as $download ) { 
  378. $new_hash = md5( $download->get_file() ); 
  379.  
  380. if ( $download->get_previous_hash() && $download->get_previous_hash() !== $new_hash ) { 
  381. // Update permissions. 
  382. $data_store->update_download_id( $product_id, $download->get_previous_hash(), $new_hash ); 
  383.  
  384. WC_Post_Data::init(); 
.