Privacy.php
197 lines
| 1 | <?php |
| 2 | declare( strict_types = 1 ); |
| 3 | |
| 4 | namespace Automattic\WooCommerce\Internal\ShopperLists\Privacy; |
| 5 | |
| 6 | use Automattic\WooCommerce\Internal\ShopperLists\ShopperList; |
| 7 | use Automattic\WooCommerce\Internal\ShopperLists\ShopperListItem; |
| 8 | use Automattic\WooCommerce\Internal\ShopperLists\ShopperListsController; |
| 9 | use Automattic\WooCommerce\Internal\Utilities\Users; |
| 10 | |
| 11 | /** |
| 12 | * GDPR/CCPA privacy exporter and eraser for shopper lists. |
| 13 | * |
| 14 | * @internal Just for internal use. |
| 15 | */ |
| 16 | class Privacy extends \WC_Abstract_Privacy { |
| 17 | |
| 18 | /** |
| 19 | * Identifier used to register both the exporter and the eraser with WP. |
| 20 | */ |
| 21 | private const REGISTRATION_ID = 'woocommerce-shopper-lists'; |
| 22 | |
| 23 | /** |
| 24 | * Prefix for the per-list-type WP data group IDs. |
| 25 | */ |
| 26 | private const GROUP_ID_PREFIX = 'woocommerce-shopper-lists-'; |
| 27 | |
| 28 | /** |
| 29 | * Constructor. |
| 30 | */ |
| 31 | public function __construct() { |
| 32 | parent::__construct(); |
| 33 | |
| 34 | add_action( 'init', array( $this, 'register_exporters_and_erasers' ) ); |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Register the shopper-list exporter and eraser with WordPress. |
| 39 | * |
| 40 | * @internal |
| 41 | */ |
| 42 | public function register_exporters_and_erasers(): void { |
| 43 | $label = __( 'WooCommerce Shopper Lists', 'woocommerce' ); |
| 44 | |
| 45 | $this->add_exporter( self::REGISTRATION_ID, $label, array( $this, 'export_data' ) ); |
| 46 | $this->add_eraser( self::REGISTRATION_ID, $label, array( $this, 'erase_data' ) ); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Export every stored shopper list for the user matching the given email. |
| 51 | * |
| 52 | * @internal |
| 53 | * |
| 54 | * @param string $email_address Email address the request applies to. |
| 55 | * |
| 56 | * @return array{data: array<int, array<string, mixed>>, done: bool} |
| 57 | */ |
| 58 | public function export_data( string $email_address ): array { |
| 59 | $user = get_user_by( 'email', $email_address ); |
| 60 | if ( ! $user instanceof \WP_User ) { |
| 61 | return array( |
| 62 | 'data' => array(), |
| 63 | 'done' => true, |
| 64 | ); |
| 65 | } |
| 66 | |
| 67 | $user_id = (int) $user->ID; |
| 68 | $controller = wc_get_container()->get( ShopperListsController::class ); |
| 69 | $data = array(); |
| 70 | |
| 71 | foreach ( $controller->get_supported_slugs() as $slug ) { |
| 72 | $list = ShopperList::get_by_slug_raw( $slug, $user_id ); |
| 73 | if ( ! $list || ! $list->get_items() ) { |
| 74 | continue; |
| 75 | } |
| 76 | |
| 77 | $group_id = self::GROUP_ID_PREFIX . $slug; |
| 78 | $group_label = sprintf( |
| 79 | /* translators: %s: shopper-list slug. */ |
| 80 | __( 'Shopper List: %s', 'woocommerce' ), |
| 81 | $slug |
| 82 | ); |
| 83 | foreach ( $list->get_items() as $item ) { |
| 84 | $data[] = array( |
| 85 | 'group_id' => $group_id, |
| 86 | 'group_label' => $group_label, |
| 87 | 'item_id' => $item->get_key(), |
| 88 | 'data' => self::item_export_rows( $item ), |
| 89 | ); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | return array( |
| 94 | 'data' => $data, |
| 95 | 'done' => true, |
| 96 | ); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Erase every stored shopper list for the user matching the given email. |
| 101 | * |
| 102 | * @internal |
| 103 | * |
| 104 | * @param string $email_address Email address the request applies to. |
| 105 | * |
| 106 | * @return array{items_removed: bool, items_retained: bool, messages: array<int, string>, done: bool} |
| 107 | */ |
| 108 | public function erase_data( string $email_address ): array { |
| 109 | $response = array( |
| 110 | 'items_removed' => false, |
| 111 | 'items_retained' => false, |
| 112 | 'messages' => array(), |
| 113 | 'done' => true, |
| 114 | ); |
| 115 | |
| 116 | $user = get_user_by( 'email', $email_address ); |
| 117 | if ( ! $user instanceof \WP_User ) { |
| 118 | return $response; |
| 119 | } |
| 120 | |
| 121 | $user_id = (int) $user->ID; |
| 122 | $controller = wc_get_container()->get( ShopperListsController::class ); |
| 123 | |
| 124 | foreach ( $controller->get_supported_slugs() as $slug ) { |
| 125 | if ( ! Users::delete_site_user_meta( $user_id, ShopperList::META_KEY_PREFIX . $slug ) ) { |
| 126 | continue; |
| 127 | } |
| 128 | |
| 129 | $response['items_removed'] = true; |
| 130 | $response['messages'][] = sprintf( |
| 131 | /* translators: %s: shopper-list slug. */ |
| 132 | __( 'Shopper List: %s', 'woocommerce' ), |
| 133 | $slug |
| 134 | ); |
| 135 | } |
| 136 | |
| 137 | return $response; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Build the per-field `{name, value}` rows for a single saved item. |
| 142 | * |
| 143 | * @param ShopperListItem $item Item to export. |
| 144 | * |
| 145 | * @return array<int, array{name: string, value: string}> |
| 146 | */ |
| 147 | private static function item_export_rows( ShopperListItem $item ): array { |
| 148 | $product = $item->get_product(); |
| 149 | $title = ( $item->is_live() && $product instanceof \WC_Product ) |
| 150 | ? $product->get_title() |
| 151 | : $item->get_product_title_at_save(); |
| 152 | |
| 153 | $rows = array( |
| 154 | array( |
| 155 | 'name' => __( 'Product ID', 'woocommerce' ), |
| 156 | 'value' => (string) $item->get_product_id(), |
| 157 | ), |
| 158 | array( |
| 159 | 'name' => __( 'Product', 'woocommerce' ), |
| 160 | 'value' => $title, |
| 161 | ), |
| 162 | ); |
| 163 | |
| 164 | if ( $item->get_variation_id() > 0 ) { |
| 165 | $rows[] = array( |
| 166 | 'name' => __( 'Variation ID', 'woocommerce' ), |
| 167 | 'value' => (string) $item->get_variation_id(), |
| 168 | ); |
| 169 | $attributes = wc_get_formatted_variation( $item->get_variation_attributes(), true ); |
| 170 | if ( '' !== $attributes ) { |
| 171 | $rows[] = array( |
| 172 | 'name' => __( 'Variation', 'woocommerce' ), |
| 173 | 'value' => $attributes, |
| 174 | ); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | $rows[] = array( |
| 179 | 'name' => __( 'Quantity', 'woocommerce' ), |
| 180 | 'value' => (string) $item->get_quantity(), |
| 181 | ); |
| 182 | $rows[] = array( |
| 183 | 'name' => __( 'Date Added', 'woocommerce' ), |
| 184 | 'value' => $item->get_date_added_gmt(), |
| 185 | ); |
| 186 | |
| 187 | if ( $item->is_live() && $product instanceof \WC_Product ) { |
| 188 | $rows[] = array( |
| 189 | 'name' => __( 'URL', 'woocommerce' ), |
| 190 | 'value' => $product->get_permalink(), |
| 191 | ); |
| 192 | } |
| 193 | |
| 194 | return $rows; |
| 195 | } |
| 196 | } |
| 197 |