Privacy: add functionality for exporting personal data from comments.

Props allendav, xkon.
Fixes #43440.

git-svn-id: https://develop.svn.wordpress.org/trunk@42888 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Andrew Ozz 2018-03-28 17:24:06 +00:00
parent 73d060e5ac
commit 9c6b0a2548
2 changed files with 102 additions and 0 deletions

View File

@ -3274,3 +3274,104 @@ function wp_handle_comment_submission( $comment_data ) {
return get_comment( $comment_id );
}
/**
* Registers the personal data exporter for comments
*
* @param array $exporters An array of personal data exporters.
* @return array An array of personal data exporters.
*/
function wp_register_comment_personal_data_exporter( $exporters ) {
$exporters[] = array(
'exporter_friendly_name' => __( 'WordPress Comments' ),
'callback' => 'wp_comments_personal_data_exporter',
);
return $exporters;
}
/**
* Finds and exports personal data associated with an email address from the comments table.
*
* @param string $email_address The comment author email address.
* @param int $page Comment page.
* @return array An array of personal data.
*/
function wp_comments_personal_data_exporter( $email_address, $page = 1 ) {
// Technically, strtolower isn't necessary since get_comments will match email insensitive
// But it is a good example for plugin developers whose targets might not be as generous
$email_address = trim( strtolower( $email_address ) );
// Limit us to 500 comments at a time to avoid timing out
$number = 500;
$page = (int) $page;
$data_to_export = array();
$comments = get_comments(
array(
'author_email' => $email_address,
'number' => $number,
'paged' => $page,
'order_by' => 'comment_ID',
'order' => 'ASC',
)
);
$comment_prop_to_export = array(
'comment_author' => __( 'Comment Author' ),
'comment_author_email' => __( 'Comment Author Email' ),
'comment_author_url' => __( 'Comment Author URL' ),
'comment_author_IP' => __( 'Comment Author IP' ),
'comment_agent' => __( 'Comment Agent' ),
'comment_date' => __( 'Comment Date' ),
'comment_content' => __( 'Comment Content' ),
'comment_link' => __( 'Comment URL' ),
);
foreach ( (array) $comments as $comment ) {
$comment_data_to_export = array();
foreach ( $comment_prop_to_export as $key => $name ) {
$value = '';
switch( $key ) {
case 'comment_author':
case 'comment_author_email':
case 'comment_author_url':
case 'comment_author_IP':
case 'comment_agent':
case 'comment_date':
$value = $comment->$key;
break;
case 'comment_content':
$value = get_comment_text( $comment->comment_ID );
break;
case 'comment_link':
$value = get_comment_link( $comment->comment_ID );
break;
}
if ( ! empty( $value ) ) {
$comment_data_to_export[] = array( 'name' => $name, 'value' => $value );
}
}
$data_to_export[] = array(
'group_id' => 'comments',
'group_label' => __( 'Comments' ),
'item_id' => "comment-{$comment->comment_ID}",
'data' => $comment_data_to_export,
);
}
$done = count( $comments ) < $number;
return array(
'data' => $data_to_export,
'done' => $done,
);
}

View File

@ -328,6 +328,7 @@ add_action( 'do_feed_atom', 'do_feed_atom', 10, 1 );
add_action( 'do_pings', 'do_all_pings', 10, 1 );
add_action( 'do_robots', 'do_robots' );
add_action( 'set_comment_cookies', 'wp_set_comment_cookies', 10, 3 );
add_filter( 'wp_privacy_personal_data_exporters', 'wp_register_comment_personal_data_exporter', 10 );
add_action( 'sanitize_comment_cookies', 'sanitize_comment_cookies' );
add_action( 'admin_print_scripts', 'print_emoji_detection_script' );
add_action( 'admin_print_scripts', 'print_head_scripts', 20 );