WC_Post_Data

Post Data.

Defined (1)

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

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