class-wc-privacy-erasers.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. <?php
  2. /**
  3. * Personal data erasers.
  4. *
  5. * @since 3.4.0
  6. * @package WooCommerce\Classes
  7. */
  8. defined( 'ABSPATH' ) || exit;
  9. /**
  10. * WC_Privacy_Erasers Class.
  11. */
  12. class WC_Privacy_Erasers {
  13. /**
  14. * Finds and erases customer data by email address.
  15. *
  16. * @since 3.4.0
  17. * @param string $email_address The user email address.
  18. * @param int $page Page.
  19. * @return array An array of personal data in name value pairs
  20. */
  21. public static function customer_data_eraser( $email_address, $page ) {
  22. $response = array(
  23. 'items_removed' => false,
  24. 'items_retained' => false,
  25. 'messages' => array(),
  26. 'done' => true,
  27. );
  28. $user = get_user_by( 'email', $email_address ); // Check if user has an ID in the DB to load stored personal data.
  29. if ( ! $user instanceof WP_User ) {
  30. return $response;
  31. }
  32. $customer = new WC_Customer( $user->ID );
  33. if ( ! $customer ) {
  34. return $response;
  35. }
  36. $props_to_erase = apply_filters( 'woocommerce_privacy_erase_customer_personal_data_props', array(
  37. 'billing_first_name' => __( 'Billing First Name', 'woocommerce' ),
  38. 'billing_last_name' => __( 'Billing Last Name', 'woocommerce' ),
  39. 'billing_company' => __( 'Billing Company', 'woocommerce' ),
  40. 'billing_address_1' => __( 'Billing Address 1', 'woocommerce' ),
  41. 'billing_address_2' => __( 'Billing Address 2', 'woocommerce' ),
  42. 'billing_city' => __( 'Billing City', 'woocommerce' ),
  43. 'billing_postcode' => __( 'Billing Postal/Zip Code', 'woocommerce' ),
  44. 'billing_state' => __( 'Billing State', 'woocommerce' ),
  45. 'billing_country' => __( 'Billing Country', 'woocommerce' ),
  46. 'billing_phone' => __( 'Phone Number', 'woocommerce' ),
  47. 'billing_email' => __( 'Email Address', 'woocommerce' ),
  48. 'shipping_first_name' => __( 'Shipping First Name', 'woocommerce' ),
  49. 'shipping_last_name' => __( 'Shipping Last Name', 'woocommerce' ),
  50. 'shipping_company' => __( 'Shipping Company', 'woocommerce' ),
  51. 'shipping_address_1' => __( 'Shipping Address 1', 'woocommerce' ),
  52. 'shipping_address_2' => __( 'Shipping Address 2', 'woocommerce' ),
  53. 'shipping_city' => __( 'Shipping City', 'woocommerce' ),
  54. 'shipping_postcode' => __( 'Shipping Postal/Zip Code', 'woocommerce' ),
  55. 'shipping_state' => __( 'Shipping State', 'woocommerce' ),
  56. 'shipping_country' => __( 'Shipping Country', 'woocommerce' ),
  57. ), $customer );
  58. foreach ( $props_to_erase as $prop => $label ) {
  59. $erased = false;
  60. if ( is_callable( array( $customer, 'get_' . $prop ) ) && is_callable( array( $customer, 'set_' . $prop ) ) ) {
  61. $value = $customer->{"get_$prop"}( 'edit' );
  62. if ( $value ) {
  63. $customer->{"set_$prop"}( '' );
  64. $erased = true;
  65. }
  66. }
  67. $erased = apply_filters( 'woocommerce_privacy_erase_customer_personal_data_prop', $erased, $prop, $customer );
  68. if ( $erased ) {
  69. /* Translators: %s Prop name. */
  70. $response['messages'][] = sprintf( __( 'Removed customer "%s"', 'woocommerce' ), $label );
  71. $response['items_removed'] = true;
  72. }
  73. }
  74. $customer->save();
  75. /**
  76. * Allow extensions to remove data for this customer and adjust the response.
  77. *
  78. * @since 3.4.0
  79. * @param array $response Array resonse data. Must include messages, num_items_removed, num_items_retained, done.
  80. * @param WC_Order $order A customer object.
  81. */
  82. return apply_filters( 'woocommerce_privacy_erase_personal_data_customer', $response, $customer );
  83. }
  84. /**
  85. * Finds and erases data which could be used to identify a person from WooCommerce data assocated with an email address.
  86. *
  87. * Orders are erased in blocks of 10 to avoid timeouts.
  88. *
  89. * @since 3.4.0
  90. * @param string $email_address The user email address.
  91. * @param int $page Page.
  92. * @return array An array of personal data in name value pairs
  93. */
  94. public static function order_data_eraser( $email_address, $page ) {
  95. $page = (int) $page;
  96. $user = get_user_by( 'email', $email_address ); // Check if user has an ID in the DB to load stored personal data.
  97. $erasure_enabled = wc_string_to_bool( get_option( 'woocommerce_erasure_request_removes_order_data', 'no' ) );
  98. $response = array(
  99. 'items_removed' => false,
  100. 'items_retained' => false,
  101. 'messages' => array(),
  102. 'done' => true,
  103. );
  104. $order_query = array(
  105. 'limit' => 10,
  106. 'page' => $page,
  107. 'customer' => array( $email_address ),
  108. );
  109. if ( $user instanceof WP_User ) {
  110. $order_query['customer'][] = (int) $user->ID;
  111. }
  112. $orders = wc_get_orders( $order_query );
  113. if ( 0 < count( $orders ) ) {
  114. foreach ( $orders as $order ) {
  115. if ( apply_filters( 'woocommerce_privacy_erase_order_personal_data', $erasure_enabled, $order ) ) {
  116. self::remove_order_personal_data( $order );
  117. /* Translators: %s Order number. */
  118. $response['messages'][] = sprintf( __( 'Removed personal data from order %s.', 'woocommerce' ), $order->get_order_number() );
  119. $response['items_removed'] = true;
  120. } else {
  121. /* Translators: %s Order number. */
  122. $response['messages'][] = sprintf( __( 'Personal data within order %s has been retained.', 'woocommerce' ), $order->get_order_number() );
  123. $response['items_retained'] = true;
  124. }
  125. }
  126. $response['done'] = 10 > count( $orders );
  127. } else {
  128. $response['done'] = true;
  129. }
  130. return $response;
  131. }
  132. /**
  133. * Finds and removes customer download logs by email address.
  134. *
  135. * @since 3.4.0
  136. * @param string $email_address The user email address.
  137. * @param int $page Page.
  138. * @return array An array of personal data in name value pairs
  139. */
  140. public static function download_data_eraser( $email_address, $page ) {
  141. $page = (int) $page;
  142. $user = get_user_by( 'email', $email_address ); // Check if user has an ID in the DB to load stored personal data.
  143. $erasure_enabled = wc_string_to_bool( get_option( 'woocommerce_erasure_request_removes_download_data', 'no' ) );
  144. $response = array(
  145. 'items_removed' => false,
  146. 'items_retained' => false,
  147. 'messages' => array(),
  148. 'done' => true,
  149. );
  150. $downloads_query = array(
  151. 'limit' => -1,
  152. 'page' => $page,
  153. 'return' => 'ids',
  154. );
  155. if ( $user instanceof WP_User ) {
  156. $downloads_query['user_id'] = (int) $user->ID;
  157. } else {
  158. $downloads_query['user_email'] = $email_address;
  159. }
  160. $customer_download_data_store = WC_Data_Store::load( 'customer-download' );
  161. // Revoke download permissions.
  162. if ( apply_filters( 'woocommerce_privacy_erase_download_personal_data', $erasure_enabled, $email_address ) ) {
  163. if ( $user instanceof WP_User ) {
  164. $result = $customer_download_data_store->delete_by_user_id( (int) $user->ID );
  165. } else {
  166. $result = $customer_download_data_store->delete_by_user_email( $email_address );
  167. }
  168. if ( $result ) {
  169. $response['messages'][] = __( 'Removed access to downloadable files.', 'woocommerce' );
  170. $response['items_removed'] = true;
  171. }
  172. } else {
  173. $response['messages'][] = __( 'Customer download permissions have been retained.', 'woocommerce' );
  174. $response['items_retained'] = true;
  175. }
  176. return $response;
  177. }
  178. /**
  179. * Remove personal data specific to WooCommerce from an order object.
  180. *
  181. * Note; this will hinder order processing for obvious reasons!
  182. *
  183. * @param WC_Order $order Order object.
  184. */
  185. public static function remove_order_personal_data( $order ) {
  186. $anonymized_data = array();
  187. /**
  188. * Allow extensions to remove their own personal data for this order first, so order data is still available.
  189. *
  190. * @since 3.4.0
  191. * @param WC_Order $order A customer object.
  192. */
  193. do_action( 'woocommerce_privacy_before_remove_order_personal_data', $order );
  194. /**
  195. * Expose props and data types we'll be anonymizing.
  196. *
  197. * @since 3.4.0
  198. * @param array $props Keys are the prop names, values are the data type we'll be passing to wp_privacy_anonymize_data().
  199. * @param WC_Order $order A customer object.
  200. */
  201. $props_to_remove = apply_filters( 'woocommerce_privacy_remove_order_personal_data_props', array(
  202. 'customer_ip_address' => 'ip',
  203. 'customer_user_agent' => 'text',
  204. 'billing_first_name' => 'text',
  205. 'billing_last_name' => 'text',
  206. 'billing_company' => 'text',
  207. 'billing_address_1' => 'text',
  208. 'billing_address_2' => 'text',
  209. 'billing_city' => 'text',
  210. 'billing_postcode' => 'text',
  211. 'billing_state' => 'address_state',
  212. 'billing_country' => 'address_country',
  213. 'billing_phone' => 'phone',
  214. 'billing_email' => 'email',
  215. 'shipping_first_name' => 'text',
  216. 'shipping_last_name' => 'text',
  217. 'shipping_company' => 'text',
  218. 'shipping_address_1' => 'text',
  219. 'shipping_address_2' => 'text',
  220. 'shipping_city' => 'text',
  221. 'shipping_postcode' => 'text',
  222. 'shipping_state' => 'address_state',
  223. 'shipping_country' => 'address_country',
  224. 'customer_id' => 'numeric_id',
  225. 'transaction_id' => 'numeric_id',
  226. ), $order );
  227. if ( ! empty( $props_to_remove ) && is_array( $props_to_remove ) ) {
  228. foreach ( $props_to_remove as $prop => $data_type ) {
  229. // Get the current value in edit context.
  230. $value = $order->{"get_$prop"}( 'edit' );
  231. // If the value is empty, it does not need to be anonymized.
  232. if ( empty( $value ) || empty( $data_type ) ) {
  233. continue;
  234. }
  235. $anon_value = function_exists( 'wp_privacy_anonymize_data' ) ? wp_privacy_anonymize_data( $data_type, $value ) : '';
  236. /**
  237. * Expose a way to control the anonymized value of a prop via 3rd party code.
  238. *
  239. * @since 3.4.0
  240. * @param string $anon_value Value of this prop after anonymization.
  241. * @param string $prop Name of the prop being removed.
  242. * @param string $value Current value of the data.
  243. * @param string $data_type Type of data.
  244. * @param WC_Order $order An order object.
  245. */
  246. $anonymized_data[ $prop ] = apply_filters( 'woocommerce_privacy_remove_order_personal_data_prop_value', $anon_value, $prop, $value, $data_type, $order );
  247. }
  248. }
  249. // Set all new props and persist the new data to the database.
  250. $order->set_props( $anonymized_data );
  251. // Remove meta data.
  252. $meta_to_remove = apply_filters( 'woocommerce_privacy_remove_order_personal_data_meta', array(
  253. 'Payer first name' => 'text',
  254. 'Payer last name' => 'text',
  255. 'Payer PayPal address' => 'email',
  256. 'Transaction ID' => 'numeric_id',
  257. ) );
  258. if ( ! empty( $meta_to_remove ) && is_array( $meta_to_remove ) ) {
  259. foreach ( $meta_to_remove as $meta_key => $data_type ) {
  260. $value = $order->get_meta( $meta_key );
  261. // If the value is empty, it does not need to be anonymized.
  262. if ( empty( $value ) || empty( $data_type ) ) {
  263. continue;
  264. }
  265. $anon_value = function_exists( 'wp_privacy_anonymize_data' ) ? wp_privacy_anonymize_data( $data_type, $value ) : '';
  266. /**
  267. * Expose a way to control the anonymized value of a value via 3rd party code.
  268. *
  269. * @since 3.4.0
  270. * @param string $anon_value Value of this data after anonymization.
  271. * @param string $prop meta_key key being removed.
  272. * @param string $value Current value of the data.
  273. * @param string $data_type Type of data.
  274. * @param WC_Order $order An order object.
  275. */
  276. $anon_value = apply_filters( 'woocommerce_privacy_remove_order_personal_data_meta_value', $anon_value, $meta_key, $value, $data_type, $order );
  277. if ( $anon_value ) {
  278. $order->update_meta_data( $meta_key, $anon_value );
  279. } else {
  280. $order->delete_meta_data( $meta_key );
  281. }
  282. }
  283. }
  284. $order->update_meta_data( '_anonymized', 'yes' );
  285. $order->save();
  286. // Delete order notes which can contain PII.
  287. $notes = wc_get_order_notes( array(
  288. 'order_id' => $order->get_id(),
  289. ) );
  290. foreach ( $notes as $note ) {
  291. wc_delete_order_note( $note->id );
  292. }
  293. // Add note that this event occured.
  294. $order->add_order_note( __( 'Personal data removed.', 'woocommerce' ) );
  295. /**
  296. * Allow extensions to remove their own personal data for this order.
  297. *
  298. * @since 3.4.0
  299. * @param WC_Order $order A customer object.
  300. */
  301. do_action( 'woocommerce_privacy_remove_order_personal_data', $order );
  302. }
  303. /**
  304. * Finds and erases customer tokens by email address.
  305. *
  306. * @since 3.4.0
  307. * @param string $email_address The user email address.
  308. * @param int $page Page.
  309. * @return array An array of personal data in name value pairs
  310. */
  311. public static function customer_tokens_eraser( $email_address, $page ) {
  312. $response = array(
  313. 'items_removed' => false,
  314. 'items_retained' => false,
  315. 'messages' => array(),
  316. 'done' => true,
  317. );
  318. $user = get_user_by( 'email', $email_address ); // Check if user has an ID in the DB to load stored personal data.
  319. if ( ! $user instanceof WP_User ) {
  320. return $response;
  321. }
  322. $tokens = WC_Payment_Tokens::get_tokens( array(
  323. 'user_id' => $user->ID,
  324. ) );
  325. if ( empty( $tokens ) ) {
  326. return $response;
  327. }
  328. foreach ( $tokens as $token ) {
  329. WC_Payment_Tokens::delete( $token->get_id() );
  330. /* Translators: %s Prop name. */
  331. $response['messages'][] = sprintf( __( 'Removed payment token "%d"', 'woocommerce' ), $token->get_id() );
  332. $response['items_removed'] = true;
  333. }
  334. /**
  335. * Allow extensions to remove data for tokens and adjust the response.
  336. *
  337. * @since 3.4.0
  338. * @param array $response Array resonse data. Must include messages, num_items_removed, num_items_retained, done.
  339. * @param array $tokens Array of tokens.
  340. */
  341. return apply_filters( 'woocommerce_privacy_erase_personal_data_tokens', $response, $tokens );
  342. }
  343. }