__( 'Advently advent calendar', 'advently' ), 'callback' => [ self::class, 'export' ], ]; return (array) $exporters; } /** * @param array $erasers Registered erasers. */ public static function addEraser( $erasers ): array { $erasers['advently'] = [ 'eraser_friendly_name' => __( 'Advently advent calendar', 'advently' ), 'callback' => [ self::class, 'eraseByEmail' ], ]; return (array) $erasers; } /** * @param string $email Address being exported. */ public static function export( $email ): array { $row = self::row( (string) $email ); if ( ! $row ) { return [ 'data' => [], 'done' => true, ]; } $data = [ [ 'name' => __( 'Email', 'advently' ), 'value' => $row['email'], ], [ 'name' => __( 'Signed up', 'advently' ), 'value' => $row['created_at'], ], [ 'name' => __( 'Consent to receive offers', 'advently' ), 'value' => $row['consent_at'] ? $row['consent_at'] : __( 'Not given', 'advently' ), ], ]; foreach ( self::opens( (int) $row['id'] ) as $open ) { $data[] = [ /* translators: 1: door number, 2: calendar start date. */ 'name' => sprintf( __( 'Opened door %1$d (calendar starting %2$s)', 'advently' ), $open['door'], $open['calendar_start'] ), 'value' => $open['opened_at'], ]; } return [ 'data' => [ [ 'group_id' => self::GROUP, 'group_label' => __( 'Advent calendar', 'advently' ), 'item_id' => 'advently-subscriber-' . $row['id'], 'data' => $data, ], ], 'done' => true, ]; } /** * @param string $email Address being erased. */ public static function eraseByEmail( $email ): array { $row = self::row( (string) $email ); $removed = $row ? self::erase( (int) $row['id'] ) : false; return [ 'items_removed' => $removed, 'items_retained' => false, 'messages' => $removed ? [ __( 'Advent calendar sign-up, door history and unused coupons removed. Coupons already used on orders are kept with those orders.', 'advently' ) ] : [], 'done' => true, ]; } /** * Deletes a subscriber, their door history and their coupons that were never used. */ public static function erase( int $subscriberId ): bool { global $wpdb; // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- custom tables. $coupons = $wpdb->get_col( $wpdb->prepare( "SELECT coupon_id FROM {$wpdb->prefix}advently_opens WHERE subscriber_id = %d AND coupon_id IS NOT NULL", $subscriberId ) ); foreach ( array_map( 'intval', $coupons ) as $couponId ) { $coupon = new \WC_Coupon( $couponId ); if ( $coupon->get_id() && 0 === (int) $coupon->get_usage_count() ) { $coupon->delete( true ); } } $wpdb->delete( $wpdb->prefix . 'advently_opens', [ 'subscriber_id' => $subscriberId ], [ '%d' ] ); $deleted = $wpdb->delete( $wpdb->prefix . 'advently_subscribers', [ 'id' => $subscriberId ], [ '%d' ] ); // phpcs:enable return (bool) $deleted; } public static function policyText(): void { if ( ! function_exists( 'wp_add_privacy_policy_content' ) ) { return; } $text = '

' . esc_html__( 'Our advent calendar asks for your email address (or uses your account) before you open a door. We store the address, when you signed up and gave consent, which doors you opened and when, and a one-way hash of your IP address used only to limit abuse. Each coupon is made for your address and sent to it by email.', 'advently' ) . '

'; $text .= '

' . esc_html__( 'You can ask us to export or erase this data at any time. Coupons you already used stay attached to those orders.', 'advently' ) . '

'; wp_add_privacy_policy_content( __( 'Advently advent calendar', 'advently' ), wp_kses_post( $text ) ); } private static function row( string $email ): ?array { global $wpdb; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- custom table. $row = $wpdb->get_row( $wpdb->prepare( "SELECT id, email, created_at, consent_at FROM {$wpdb->prefix}advently_subscribers WHERE email = %s", strtolower( trim( $email ) ) ), ARRAY_A ); return $row ? $row : null; } private static function opens( int $subscriberId ): array { global $wpdb; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- custom table. $rows = $wpdb->get_results( $wpdb->prepare( "SELECT calendar_start, door, opened_at FROM {$wpdb->prefix}advently_opens WHERE subscriber_id = %d ORDER BY calendar_start, door", $subscriberId ), ARRAY_A ); return is_array( $rows ) ? $rows : []; } }