PrivacyEraser.php
91 lines
| 1 | <?php |
| 2 | |
| 3 | declare( strict_types=1 ); |
| 4 | |
| 5 | namespace Automattic\WooCommerce\Internal\StockNotifications\Privacy; |
| 6 | |
| 7 | use Automattic\WooCommerce\Internal\StockNotifications\Enums\NotificationCancellationSource; |
| 8 | use Automattic\WooCommerce\Internal\StockNotifications\Factory; |
| 9 | use Automattic\WooCommerce\Internal\StockNotifications\Enums\NotificationStatus; |
| 10 | use Automattic\WooCommerce\Internal\StockNotifications\Notification; |
| 11 | use Automattic\WooCommerce\Internal\StockNotifications\NotificationQuery; |
| 12 | |
| 13 | /** |
| 14 | * Privacy eraser for WooCommerce Customer Stock Notifications. |
| 15 | * |
| 16 | * This class handles the erasure of customer stock notification data for users |
| 17 | * who request their personal data to be erased. |
| 18 | */ |
| 19 | class PrivacyEraser extends \WC_Abstract_Privacy { |
| 20 | |
| 21 | /** |
| 22 | * Constructor. |
| 23 | */ |
| 24 | public function __construct() { |
| 25 | parent::__construct(); |
| 26 | |
| 27 | add_action( 'init', array( $this, 'register_erasers_exporters' ) ); |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * Register the eraser for stock notifications. |
| 32 | */ |
| 33 | public function register_erasers_exporters() { |
| 34 | $this->add_eraser( |
| 35 | 'woocommerce-customer-stock-notifications', |
| 36 | __( 'WooCommerce Customer Stock Notifications', 'woocommerce' ), |
| 37 | array( $this, 'erase_notification_data' ) |
| 38 | ); |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Erase customer stock notification data for a given email address. |
| 43 | * |
| 44 | * This method anonymizes the user email and sets the status of the notifications to 'cancelled'. |
| 45 | * |
| 46 | * @param string $email_address The email address to erase data for. |
| 47 | * |
| 48 | * @return array Response containing the status of the operation and messages. |
| 49 | */ |
| 50 | public static function erase_notification_data( string $email_address ): array { |
| 51 | $response = array( |
| 52 | 'items_removed' => false, |
| 53 | 'items_retained' => false, |
| 54 | 'messages' => array(), |
| 55 | 'done' => true, |
| 56 | ); |
| 57 | |
| 58 | $notifications = NotificationQuery::get_notifications( |
| 59 | array( |
| 60 | 'user_email' => $email_address, |
| 61 | ) |
| 62 | ); |
| 63 | |
| 64 | foreach ( $notifications as $notification_id ) { |
| 65 | $notification = Factory::get_notification( $notification_id ); |
| 66 | if ( ! $notification instanceof Notification ) { |
| 67 | continue; |
| 68 | } |
| 69 | |
| 70 | $anonymous_email = wp_privacy_anonymize_data( 'email', $email_address ); |
| 71 | $notification->set_user_email( $anonymous_email ); |
| 72 | $notification->set_user_id( 0 ); |
| 73 | $notification->set_status( NotificationStatus::CANCELLED ); |
| 74 | $notification->set_cancellation_source( NotificationCancellationSource::USER ); |
| 75 | $notification->set_date_cancelled( time() ); |
| 76 | $notification->update_meta_data( '_anonymized', 'yes' ); |
| 77 | $notification->update_meta_data( 'verification_action_key', '' ); |
| 78 | $notification->update_meta_data( 'unsubscribe_action_key', '' ); |
| 79 | $notification->save(); |
| 80 | $response['messages'][] = sprintf( |
| 81 | /* translators: %d the numeric product ID */ |
| 82 | __( 'Removed back-in-stock notification for product id: %d', 'woocommerce' ), |
| 83 | $notification->get_product_id() |
| 84 | ); |
| 85 | $response['items_removed'] = true; |
| 86 | }//end foreach |
| 87 | |
| 88 | return $response; |
| 89 | } |
| 90 | } |
| 91 |