Make WordPress Core


Ignore:
Timestamp:
05/02/2018 12:10:36 AM (8 years ago)
Author:
SergeyBiryukov
Message:

Privacy: add functionality for exporting personal data from comments.

Props allendav, xkon.
Merges [42888] to the 4.9 branch.
See #43440.

Location:
branches/4.9
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/4.9

  • branches/4.9/src/wp-includes/comment.php

    r41980 r43076  
    31463146        return get_comment( $comment_id );
    31473147}
     3148
     3149/**
     3150 * Registers the personal data exporter for comments
     3151 *
     3152 * @param array   $exporters   An array of personal data exporters.
     3153 * @return array  An array of personal data exporters.
     3154 */
     3155function wp_register_comment_personal_data_exporter( $exporters ) {
     3156        $exporters[] = array(
     3157                'exporter_friendly_name' => __( 'WordPress Comments' ),
     3158                'callback'               => 'wp_comments_personal_data_exporter',
     3159        );
     3160
     3161        return $exporters;
     3162}
     3163
     3164/**
     3165 * Finds and exports personal data associated with an email address from the comments table.
     3166 *
     3167 * @param string  $email_address The comment author email address.
     3168 * @param int     $page          Comment page.
     3169 * @return array  An array of personal data.
     3170 */
     3171function wp_comments_personal_data_exporter( $email_address, $page = 1 ) {
     3172
     3173        // Technically, strtolower isn't necessary since get_comments will match email insensitive
     3174        // But it is a good example for plugin developers whose targets might not be as generous
     3175        $email_address = trim( strtolower( $email_address ) );
     3176
     3177        // Limit us to 500 comments at a time to avoid timing out
     3178        $number = 500;
     3179        $page = (int) $page;
     3180
     3181        $data_to_export = array();
     3182
     3183        $comments = get_comments(
     3184                array(
     3185                        'author_email' => $email_address,
     3186                        'number'       => $number,
     3187                        'paged'        => $page,
     3188                        'order_by'     => 'comment_ID',
     3189                        'order'        => 'ASC',
     3190                )
     3191        );
     3192
     3193        $comment_prop_to_export = array(
     3194                'comment_author'       => __( 'Comment Author' ),
     3195                'comment_author_email' => __( 'Comment Author Email' ),
     3196                'comment_author_url'   => __( 'Comment Author URL' ),
     3197                'comment_author_IP'    => __( 'Comment Author IP' ),
     3198                'comment_agent'        => __( 'Comment Agent' ),
     3199                'comment_date'         => __( 'Comment Date' ),
     3200                'comment_content'      => __( 'Comment Content' ),
     3201                'comment_link'         => __( 'Comment URL' ),
     3202        );
     3203
     3204        foreach ( (array) $comments as $comment ) {
     3205                $comment_data_to_export = array();
     3206
     3207                foreach ( $comment_prop_to_export as $key => $name ) {
     3208                        $value = '';
     3209
     3210                        switch( $key ) {
     3211                                case 'comment_author':
     3212                                case 'comment_author_email':
     3213                                case 'comment_author_url':
     3214                                case 'comment_author_IP':
     3215                                case 'comment_agent':
     3216                                case 'comment_date':
     3217                                        $value = $comment->$key;
     3218                                        break;
     3219
     3220                                case 'comment_content':
     3221                                        $value = get_comment_text( $comment->comment_ID );
     3222                                        break;
     3223
     3224                                case 'comment_link':
     3225                                        $value = get_comment_link( $comment->comment_ID );
     3226                                        break;
     3227                        }
     3228
     3229                        if ( ! empty( $value ) ) {
     3230                                $comment_data_to_export[] = array( 'name' => $name, 'value' => $value );
     3231                        }
     3232                }
     3233
     3234                $data_to_export[] = array(
     3235                        'group_id'    => 'comments',
     3236                        'group_label' => __( 'Comments' ),
     3237                        'item_id'     => "comment-{$comment->comment_ID}",
     3238                        'data'        => $comment_data_to_export,
     3239                );
     3240        }
     3241
     3242        $done = count( $comments ) < $number;
     3243
     3244        return array(
     3245                'data' => $data_to_export,
     3246                'done' => $done,
     3247        );
     3248}
Note: See TracChangeset for help on using the changeset viewer.