Нет описания

class-wc-privacy-erasers.php 14KB

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