From 2f6f41170a436eb2297a19125e9d887d2f237816 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Wed, 2 May 2018 00:11:24 +0000 Subject: [PATCH] Privacy: add functionality for exporting personal data from comments. Props allendav, xkon. Merges [42888] to the 4.9 branch. See #43440. Built from https://develop.svn.wordpress.org/branches/4.9@43076 git-svn-id: http://core.svn.wordpress.org/branches/4.9@42905 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/comment.php | 101 ++++++++++++++++++++++++++++++++ wp-includes/default-filters.php | 1 + wp-includes/version.php | 2 +- 3 files changed, 103 insertions(+), 1 deletion(-) diff --git a/wp-includes/comment.php b/wp-includes/comment.php index 07fed8100e..ef6d97e9b0 100644 --- a/wp-includes/comment.php +++ b/wp-includes/comment.php @@ -3145,3 +3145,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, + ); +} diff --git a/wp-includes/default-filters.php b/wp-includes/default-filters.php index d28c07b12b..dacdc214c0 100644 --- a/wp-includes/default-filters.php +++ b/wp-includes/default-filters.php @@ -301,6 +301,7 @@ add_action( 'do_feed_atom', 'do_feed_atom', 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, 2 ); +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 ); diff --git a/wp-includes/version.php b/wp-includes/version.php index 073f2c1a85..f2ddf7c30a 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -4,7 +4,7 @@ * * @global string $wp_version */ -$wp_version = '4.9.6-alpha-43075'; +$wp_version = '4.9.6-alpha-43076'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.