在 WordPress 4.9.6 中,新增了工具以简化对《欧盟通用数据保护条例》(简称 GDPR)等法律的合规工作。新增的工具包括个人数据导出工具,该工具支持将指定用户的所有个人数据以 ZIP 文件形式导出。除了存储在 WordPress 评论等内容中的个人数据外,插件还可以钩入导出功能以导出其收集的个人数据,无论是在 postmeta 中还是在全新的自定义文章类型(CPT)中。
所有导出的“密钥”是用户的电子邮件地址——选择它是因为它支持导出完整注册的用户的个人数据以及未注册用户(例如登出后的评论者)的个人数据。
然而,由于组装个人数据导出可能是一个耗时的过程,并且很可能包含敏感数据,我们不想在未确认请求的情况下直接生成并通过电子邮件将其发送给请求者,因此面向管理员的用户界面首先要求管理员输入提出请求的用户名或电子邮件地址,然后发送一个链接供其点击以确认请求。
一旦请求得到确认,管理员可以生成并下载或直接通过电子邮件发送该用户的个人数据导出 ZIP 文件,或者在需要时进行导出。用户收到的 ZIP 文件中包含一个“微型网站”,其中有一个索引 HTML 页面,按组(例如评论组等)组织其个人数据。
无论管理员是下载个人数据导出 ZIP 文件还是直接发送给请求者,组装个人数据导出的方式都是相同的——并且依赖于钩入“exporter”回调函数来收集所有用于导出的数据。当管理员点击下载或电子邮件链接时,会开始一个 AJAX 循环,逐个遍历系统中注册的所有导出器。除了核心内置的导出器外,插件还可以注册自己的导出器回调。
导出器回调接口的设计尽可能简单。导出器回调接收我们正在处理的电子邮件地址和一个 page 参数。page 参数(从 1 开始)用于避免插件尝试一次性导出其收集的所有个人数据而导致超时。行为良好的插件将限制其每页尝试擦除的数据量(例如 100 篇文章、200 条评论等)。
导出器回调返回针对该电子邮件地址和页面的数据以及是否完成。如果导出器回调报告尚未完成,它将在单独的请求中再次被调用,page 参数递增 1。预期导出器回调将返回一个包含用于导出的项目的数组。每个项目包含该项目所属组的组标识符(例如评论、文章、订单等),可选的组标签(已翻译)、项目标识符(例如 comment-133)以及一个包含要为此项目导出的数据的名称和值对的数组。
值得注意的是,value 可以是媒体路径,在这种情况下,将在导出中的索引 HTML 页面中添加指向媒体文件的链接。
当所有导出器都调用完成时,WordPress 首先组装一个“index”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' );就是这样!您的插件现在将为导出提供数据!