Make WordPress Core

Changeset 42888 for trunk


Ignore:
Timestamp:
03/28/2018 05:24:06 PM (7 years ago)
Author:
azaozz
Message:

Privacy: add functionality for exporting personal data from comments.

Props allendav, xkon.
Fixes #43440.

Location:
trunk/src/wp-includes
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/comment.php

    r42876 r42888  
    32753275    return get_comment( $comment_id );
    32763276}
     3277
     3278/**
     3279 * Registers the personal data exporter for comments
     3280 *
     3281 * @param array   $exporters   An array of personal data exporters.
     3282 * @return array  An array of personal data exporters.
     3283 */
     3284function wp_register_comment_personal_data_exporter( $exporters ) {
     3285    $exporters[] = array(
     3286        'exporter_friendly_name' => __( 'WordPress Comments' ),
     3287        'callback'               => 'wp_comments_personal_data_exporter',
     3288    );
     3289
     3290    return $exporters;
     3291}
     3292
     3293/**
     3294 * Finds and exports personal data associated with an email address from the comments table.
     3295 *
     3296 * @param string  $email_address The comment author email address.
     3297 * @param int     $page          Comment page.
     3298 * @return array  An array of personal data.
     3299 */
     3300function wp_comments_personal_data_exporter( $email_address, $page = 1 ) {
     3301
     3302    // Technically, strtolower isn't necessary since get_comments will match email insensitive
     3303    // But it is a good example for plugin developers whose targets might not be as generous
     3304    $email_address = trim( strtolower( $email_address ) );
     3305
     3306    // Limit us to 500 comments at a time to avoid timing out
     3307    $number = 500;
     3308    $page = (int) $page;
     3309
     3310    $data_to_export = array();
     3311
     3312    $comments = get_comments(
     3313        array(
     3314            'author_email' => $email_address,
     3315            'number'       => $number,
     3316            'paged'        => $page,
     3317            'order_by'     => 'comment_ID',
     3318            'order'        => 'ASC',
     3319        )
     3320    );
     3321
     3322    $comment_prop_to_export = array(
     3323        'comment_author'       => __( 'Comment Author' ),
     3324        'comment_author_email' => __( 'Comment Author Email' ),
     3325        'comment_author_url'   => __( 'Comment Author URL' ),
     3326        'comment_author_IP'    => __( 'Comment Author IP' ),
     3327        'comment_agent'        => __( 'Comment Agent' ),
     3328        'comment_date'         => __( 'Comment Date' ),
     3329        'comment_content'      => __( 'Comment Content' ),
     3330        'comment_link'         => __( 'Comment URL' ),
     3331    );
     3332
     3333    foreach ( (array) $comments as $comment ) {
     3334        $comment_data_to_export = array();
     3335
     3336        foreach ( $comment_prop_to_export as $key => $name ) {
     3337            $value = '';
     3338
     3339            switch( $key ) {
     3340                case 'comment_author':
     3341                case 'comment_author_email':
     3342                case 'comment_author_url':
     3343                case 'comment_author_IP':
     3344                case 'comment_agent':
     3345                case 'comment_date':
     3346                    $value = $comment->$key;
     3347                    break;
     3348
     3349                case 'comment_content':
     3350                    $value = get_comment_text( $comment->comment_ID );
     3351                    break;
     3352
     3353                case 'comment_link':
     3354                    $value = get_comment_link( $comment->comment_ID );
     3355                    break;
     3356            }
     3357
     3358            if ( ! empty( $value ) ) {
     3359                $comment_data_to_export[] = array( 'name' => $name, 'value' => $value );
     3360            }
     3361        }
     3362
     3363        $data_to_export[] = array(
     3364            'group_id'    => 'comments',
     3365            'group_label' => __( 'Comments' ),
     3366            'item_id'     => "comment-{$comment->comment_ID}",
     3367            'data'        => $comment_data_to_export,
     3368        );
     3369    }
     3370
     3371    $done = count( $comments ) < $number;
     3372
     3373    return array(
     3374        'data' => $data_to_export,
     3375        'done' => $done,
     3376    );
     3377}
  • trunk/src/wp-includes/default-filters.php

    r42772 r42888  
    329329add_action( 'do_robots', 'do_robots' );
    330330add_action( 'set_comment_cookies', 'wp_set_comment_cookies', 10, 3 );
     331add_filter( 'wp_privacy_personal_data_exporters', 'wp_register_comment_personal_data_exporter', 10 );
    331332add_action( 'sanitize_comment_cookies', 'sanitize_comment_cookies' );
    332333add_action( 'admin_print_scripts', 'print_emoji_detection_script' );
Note: See TracChangeset for help on using the changeset viewer.