首页 / 插件开发手册 / 隐私政策 / 导出个人数据

导出个人数据

在你的插件中添加个人数据导出功能

在WordPress 4.9.6中,添加了新的工具,以使遵守欧盟的一般数据保护法规(简称GDPR)等法律更加容易。在添加的工具中,有一个个人数据导出工具,它支持将给定用户的所有个人数据导出到ZIP文件中。除了存储在WordPress评论中的个人数据外,插件还可以连接到导出功能以导出他们收集的个人数据,无论是在postmeta中,还是在一种全新的自定义文章类型(CPT)中。

所有导出的“关键”是用户的电子邮件地址 —— 之所以选择该地址,是因为它支持为正式注册用户和未注册用户导出个人数据(例如,如注销的评论者)。

然而,由于组装个人数据导出可能是一个密集的过程,并且可能包含敏感数据,我们不希望在不确认请求的情况下仅生成并通过电子邮件发送给请求者,因此,面向管理员的用户界面通过让管理员输入发出请求的用户名或电子邮件地址来启动所有请求,然后发送一个链接以单击确认其请求。

一旦确认请求,管理员可以为用户生成并下载或直接通过电子邮件发送个人数据导出ZIP文件,或者在需要时进行导出。在用户收到的ZIP文件中,他们会发现一个“迷你网站”,其中有一个索引HTML页面,包含按组组织的个人数据(例如,评论组等)

无论管理员下载个人数据导出ZIP文件还是将其直接发送给请求者,个人数据导出的组装方式都是相同的,并且依赖于挂接“导出器”回调来完成收集导出所有数据的繁重工作。当管理员单击下载或电子邮件链接时,一个AJAX循环开始迭代系统中注册的所有导出器,每次迭代一个。除了内置在核心中的导出器之外,插件还可以注册自己的导出器回调。

导出器回调接口设计得尽可能简单。导出器回调接收我们正在处理的电子邮件地址以及页面参数。页面参数(从1开始)用于避免插件试图一次导出收集的所有个人数据,从而可能导致超时。一个表现良好的插件将限制它试图删除每页的数据量(例如100篇文章、200条评论等)

导出器回调会回复该电子邮件地址和页面的任何数据,以及是否完成了回复。如果导出器回调报告未完成,将再次调用它(在单独的请求中),页面参数增加1。导出器回调预计将返回导出的项目数组。每个项目都包含一个组标识符,该组的
项目是该组的一部分(例如评论、文章、订单等),一个可选的组标签(翻译),一个项目标识符(例如comment-133),然后是一个键值对数组,其中包含要为该项目导出的数据。

值得注意的是,该值可能是媒体路径,在这种情况下,指向媒体文件的链接将添加到导出中的索引HTML页面。

当所有导出都被调用完成时,WordPress首先汇编一个“索引”HTML文档,作为导出报告的核心。如果一个插件报告WordPress或另一个插件已经添加的项目的额外数据,那么该项目的所有数据将一起显示。

导出在服务器上缓存3天,然后删除。

一个插件可以注册一个或多个导出器,但大多数插件只需要一个。让我们研究一个假设的插件,它将评论者的位置数据添加到评论中。

首先,假设插件使用`add_comment_meta`添加位置数据,使用`meta_key`s的`latitude`和`longitude`

插件需要做的第一件事是创建一个导出器函数,该函数接受电子邮件地址和页面,例如:

/**
 * Export user meta for a user using the supplied email.
 *
 * @param string $email_address   email address to manipulate
 * @param int    $page            pagination
 *
 * @return array
 */
function wporg_export_user_data_by_email( $email_address, $page = 1 ) {
	$number = 500; // Limit us to avoid timing out
	$page   = (int) $page;

	$export_items = array();

	$comments = get_comments(
		array(
			'author_email' => $email_address,
			'number'       => $number,
			'paged'        => $page,
			'order_by'     => 'comment_ID',
			'order'        => 'ASC',
		)
	);

	foreach ( (array) $comments as $comment ) {
		$latitude  = get_comment_meta( $comment->comment_ID, 'latitude', true );
		$longitude = get_comment_meta( $comment->comment_ID, 'longitude', true );

		// Only add location data to the export if it is not empty.
		if ( ! empty( $latitude ) ) {
			// Most item IDs should look like postType-postID. If you don't have a post, comment or other ID to work with,
			// use a unique value to avoid having this item's export combined in the final report with other items
			// of the same id.
			$item_id = "comment-{$comment->comment_ID}";

			// Core group IDs include 'comments', 'posts', etc. But you can add your own group IDs as needed
			$group_id = 'comments';

			// Optional group label. Core provides these for core groups. If you define your own group, the first
			// exporter to include a label will be used as the group label in the final exported report.
			$group_label = __( 'Comments', 'text-domain' );

			// Plugins can add as many items in the item data array as they want.
			$data = array(
				array(
					'name'  => __( 'Commenter Latitude', 'text-domain' ),
					'value' => $latitude,
				),
				array(
					'name'  => __( 'Commenter Longitude', 'text-domain' ),
					'value' => $longitude,
				),
			);

			$export_items[] = array(
				'group_id'    => $group_id,
				'group_label' => $group_label,
				'item_id'     => $item_id,
				'data'        => $data,
			);
		}
	}

	// Tell core if we have more comments to work on still.
	$done = count( $comments ) > $number;
	return array(
		'data' => $export_items,
		'done' => $done,
	);
}

插件需要做的下一件事是通过使用`wp_privacy_personal_data_exporters`过滤器过滤导出器数组来注册回调。

注册时,请为导出和回调提供友好名称(以帮助调试-此友好名称此时不会显示给任何人),例如。

/**
 * Registers all data exporters.
 *
 * @param array $exporters
 *
 * @return mixed
 */
function wporg_register_user_data_exporters( $exporters ) {
	$exporters['my-plugin-slug'] = array(
		'exporter_friendly_name' => __( 'Comment Location Plugin', 'text-domain' ),
		'callback'               => 'my_plugin_exporter',
	);
	return $exporters;
}

add_filter( 'wp_privacy_personal_data_exporters', 'wporg_register_user_data_exporters' );

这就是全部!你的插件现在将为导出提供数据!