repository = $repository; } /** * Register the personal data exporter. * * @param array $exporters Registered exporters. * @return array */ public function register_exporter( array $exporters ): array { $exporters['ability-guard-execution-logs'] = array( 'exporter_friendly_name' => __( 'AbilityGuard execution logs', 'ability-guard' ), 'callback' => array( $this, 'export_user_logs' ), ); return $exporters; } /** * Register the personal data eraser. * * @param array $erasers Registered erasers. * @return array */ public function register_eraser( array $erasers ): array { $erasers['ability-guard-execution-logs'] = array( 'eraser_friendly_name' => __( 'AbilityGuard execution logs', 'ability-guard' ), 'callback' => array( $this, 'erase_user_logs' ), ); return $erasers; } /** * Export AbilityGuard log data for the requested user email. * * @param string $email_address User email address. * @param int $page Export page. * @return array */ public function export_user_logs( string $email_address, int $page = 1 ): array { $user = get_user_by( 'email', $email_address ); if ( ! $user ) { return array( 'data' => array(), 'done' => true, ); } $logs = $this->repository->get_user_privacy_logs( (int) $user->ID, $page, self::EXPORT_BATCH_SIZE ); $data = array(); foreach ( $logs as $log ) { $item = array( array( 'name' => __( 'Ability name', 'ability-guard' ), 'value' => (string) $log->ability_name, ), array( 'name' => __( 'Triggered via', 'ability-guard' ), 'value' => (string) $log->triggered_via, ), array( 'name' => __( 'Status', 'ability-guard' ), 'value' => (string) $log->status, ), array( 'name' => __( 'Execution time', 'ability-guard' ), 'value' => sprintf( /* translators: %d: execution time in milliseconds. */ __( '%d ms', 'ability-guard' ), absint( $log->execution_ms ) ), ), array( 'name' => __( 'IP address', 'ability-guard' ), 'value' => (string) $log->ip_address, ), array( 'name' => __( 'Logged at', 'ability-guard' ), 'value' => (string) $log->created_at, ), ); if ( ! empty( $log->error_code ) ) { $item[] = array( 'name' => __( 'Error code', 'ability-guard' ), 'value' => (string) $log->error_code, ); } $data[] = array( 'group_id' => 'ability-guard-execution-logs', 'group_label' => __( 'AbilityGuard execution logs', 'ability-guard' ), 'item_id' => 'ability-guard-log-' . absint( $log->id ), 'data' => $item, ); } return array( 'data' => $data, 'done' => count( $logs ) < self::EXPORT_BATCH_SIZE, ); } /** * Erase personal data from AbilityGuard logs for the requested user email. * * @param string $email_address User email address. * @param int $page Erasure page. * @return array */ public function erase_user_logs( string $email_address, int $page = 1 ): array { $user = get_user_by( 'email', $email_address ); if ( ! $user ) { return array( 'items_removed' => false, 'items_retained' => false, 'messages' => array(), 'done' => true, ); } $changed = $this->repository->anonymize_user_logs( (int) $user->ID ); return array( 'items_removed' => $changed > 0, 'items_retained' => false, 'messages' => $changed > 0 ? array( __( 'AbilityGuard removed personal data from matching execution logs.', 'ability-guard' ) ) : array(), 'done' => true, ); } }