public static function remove_order_personal_data( $order ) {
$anonymized_data = array();
/**
* Allow extensions to remove their own personal data for this order first, so order data is still available.
*
* @since 3.4.0
* @param WC_Order $order A customer object.
*/
do_action( 'woocommerce_privacy_before_remove_order_personal_data', $order );
/**
* Expose props and data types we'll be anonymizing.
*
* @since 3.4.0
* @param array $props Keys are the prop names, values are the data type we'll be passing to wp_privacy_anonymize_data().
* @param WC_Order $order A customer object.
*/
$props_to_remove = apply_filters(
'woocommerce_privacy_remove_order_personal_data_props',
array(
'customer_ip_address' => 'ip',
'customer_user_agent' => 'text',
'billing_first_name' => 'text',
'billing_last_name' => 'text',
'billing_company' => 'text',
'billing_address_1' => 'text',
'billing_address_2' => 'text',
'billing_city' => 'text',
'billing_postcode' => 'text',
'billing_state' => 'address_state',
'billing_country' => 'address_country',
'billing_phone' => 'phone',
'billing_email' => 'email',
'shipping_first_name' => 'text',
'shipping_last_name' => 'text',
'shipping_company' => 'text',
'shipping_address_1' => 'text',
'shipping_address_2' => 'text',
'shipping_city' => 'text',
'shipping_postcode' => 'text',
'shipping_state' => 'address_state',
'shipping_country' => 'address_country',
'shipping_phone' => 'phone',
'customer_id' => 'numeric_id',
'transaction_id' => 'numeric_id',
),
$order
);
if ( ! empty( $props_to_remove ) && is_array( $props_to_remove ) ) {
foreach ( $props_to_remove as $prop => $data_type ) {
// Get the current value in edit context.
$value = $order->{"get_$prop"}( 'edit' );
// If the value is empty, it does not need to be anonymized.
if ( empty( $value ) || empty( $data_type ) ) {
continue;
}
$anon_value = function_exists( 'wp_privacy_anonymize_data' ) ? wp_privacy_anonymize_data( $data_type, $value ) : '';
/**
* Expose a way to control the anonymized value of a prop via 3rd party code.
*
* @since 3.4.0
* @param string $anon_value Value of this prop after anonymization.
* @param string $prop Name of the prop being removed.
* @param string $value Current value of the data.
* @param string $data_type Type of data.
* @param WC_Order $order An order object.
*/
$anonymized_data[ $prop ] = apply_filters( 'woocommerce_privacy_remove_order_personal_data_prop_value', $anon_value, $prop, $value, $data_type, $order );
}
}
// Set all new props and persist the new data to the database.
$order->set_props( $anonymized_data );
// Remove meta data.
$meta_to_remove = apply_filters(
'woocommerce_privacy_remove_order_personal_data_meta',
array(
'Payer first name' => 'text',
'Payer last name' => 'text',
'Payer PayPal address' => 'email',
'Transaction ID' => 'numeric_id',
)
);
if ( ! empty( $meta_to_remove ) && is_array( $meta_to_remove ) ) {
foreach ( $meta_to_remove as $meta_key => $data_type ) {
$value = $order->get_meta( $meta_key );
// If the value is empty, it does not need to be anonymized.
if ( empty( $value ) || empty( $data_type ) ) {
continue;
}
$anon_value = function_exists( 'wp_privacy_anonymize_data' ) ? wp_privacy_anonymize_data( $data_type, $value ) : '';
/**
* Expose a way to control the anonymized value of a value via 3rd party code.
*
* @since 3.4.0
* @param string $anon_value Value of this data after anonymization.
* @param string $prop meta_key key being removed.
* @param string $value Current value of the data.
* @param string $data_type Type of data.
* @param WC_Order $order An order object.
*/
$anon_value = apply_filters( 'woocommerce_privacy_remove_order_personal_data_meta_value', $anon_value, $meta_key, $value, $data_type, $order );
if ( $anon_value ) {
$order->update_meta_data( $meta_key, $anon_value );
} else {
$order->delete_meta_data( $meta_key );
}
}
}
$order->update_meta_data( '_anonymized', 'yes' );
$order->save();
// Delete order notes which can contain PII.
$notes = wc_get_order_notes(
array(
'order_id' => $order->get_id(),
)
);
foreach ( $notes as $note ) {
wc_delete_order_note( $note->id );
}
// Add note that this event occurred.
$order->add_order_note( __( 'Personal data removed.', 'woocommerce' ) );
/**
* Allow extensions to remove their own personal data for this order.
*
* @since 3.4.0
* @param WC_Order $order Order instance.
*/
do_action( 'woocommerce_privacy_remove_order_personal_data', $order );
}