'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', ), $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; } if ( function_exists( 'wp_privacy_anonymize_data' ) ) { $anon_value = wp_privacy_anonymize_data( $data_type, $value ); } else { $anon_value = ''; } /** * Expose a way to control the anonymized value of a prop via 3rd party code. * * @since 3.4.0 * @param bool $anonymized_data 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 ); $order->update_meta_data( '_anonymized', 'yes' ); $order->save(); // Add note that this event occured. $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 A customer object. */ do_action( 'woocommerce_privacy_remove_order_personal_data', $order ); } /** * For a given query trash all matches. * * @since 3.4.0 * @param array $query Query array to pass to wc_get_orders(). * @return int Count of orders that were trashed. */ protected static function trash_orders_query( $query ) { $orders = wc_get_orders( $query ); $count = 0; if ( $orders ) { foreach ( $orders as $order ) { $order->delete( false ); $count ++; } } return $count; } /** * For a given query, anonymize all matches. * * @since 3.4.0 * @param array $query Query array to pass to wc_get_orders(). * @return int Count of orders that were anonymized. */ protected static function anonymize_orders_query( $query ) { $orders = wc_get_orders( $query ); $count = 0; if ( $orders ) { foreach ( $orders as $order ) { self::remove_order_personal_data( $order ); $count ++; } } return $count; } /** * Spawn events for order cleanup. */ public static function order_cleanup_process() { self::$background_process->push_to_queue( array( 'task' => 'trash_pending_orders' ) ); self::$background_process->push_to_queue( array( 'task' => 'trash_failed_orders' ) ); self::$background_process->push_to_queue( array( 'task' => 'trash_cancelled_orders' ) ); self::$background_process->push_to_queue( array( 'task' => 'anonymize_completed_orders' ) ); self::$background_process->save()->dispatch(); } /** * Find and trash old orders. * * @since 3.4.0 * @param int $limit Limit orders to process per batch. * @return int Number of orders processed. */ public static function trash_pending_orders( $limit = 20 ) { $option = wc_parse_relative_date_option( get_option( 'woocommerce_trash_pending_orders' ) ); if ( empty( $option['number'] ) ) { return 0; } return self::trash_orders_query( array( 'date_created' => '<' . strtotime( '-' . $option['number'] . ' ' . $option['unit'] ), 'limit' => $limit, // Batches of 20. 'status' => 'wc-pending', ) ); } /** * Find and trash old orders. * * @since 3.4.0 * @param int $limit Limit orders to process per batch. * @return int Number of orders processed. */ public static function trash_failed_orders( $limit = 20 ) { $option = wc_parse_relative_date_option( get_option( 'woocommerce_trash_failed_orders' ) ); if ( empty( $option['number'] ) ) { return 0; } return self::trash_orders_query( array( 'date_created' => '<' . strtotime( '-' . $option['number'] . ' ' . $option['unit'] ), 'limit' => $limit, // Batches of 20. 'status' => 'wc-failed', ) ); } /** * Find and trash old orders. * * @since 3.4.0 * @param int $limit Limit orders to process per batch. * @return int Number of orders processed. */ public static function trash_cancelled_orders( $limit = 20 ) { $option = wc_parse_relative_date_option( get_option( 'woocommerce_trash_cancelled_orders' ) ); if ( empty( $option['number'] ) ) { return 0; } return self::trash_orders_query( array( 'date_created' => '<' . strtotime( '-' . $option['number'] . ' ' . $option['unit'] ), 'limit' => $limit, // Batches of 20. 'status' => 'wc-cancelled', ) ); } /** * Anonymize old completed orders from guests. * * @since 3.4.0 * @param int $limit Limit orders to process per batch. * @param int $page Page to process. * @return int Number of orders processed. */ public static function anonymize_completed_orders( $limit = 20, $page = 1 ) { $option = wc_parse_relative_date_option( get_option( 'woocommerce_anonymize_completed_orders' ) ); if ( empty( $option['number'] ) ) { return 0; } return self::anonymize_orders_query( array( 'date_created' => '<' . strtotime( '-' . $option['number'] . ' ' . $option['unit'] ), 'limit' => $limit, // Batches of 20. 'status' => 'wc-completed', 'anonymized' => false, 'customer_id' => 0, ) ); } /** * Handle some custom types of data and anonymize them. * * @param string $anonymous Anonymized string. * @param string $type Type of data. * @param string $data The data being anonymized. * @return string Anonymized string. */ public static function anonymize_custom_data_types( $anonymous, $type, $data ) { switch ( $type ) { case 'address_state': case 'address_country': $anonymous = ''; // Empty string - we don't want to store anything after removal. break; case 'phone': $anonymous = preg_replace( '/\d/u', '0', $data ); break; } return $anonymous; } } WC_Privacy::init();