Make WordPress Core


Ignore:
Timestamp:
05/02/2018 12:20:31 AM (7 years ago)
Author:
SergeyBiryukov
Message:

Privacy: add functionality to anonymize commenters.

Props xkon, fclaussen, allendav, birgire, azaozz.
Merges [42994] to the 4.9 branch.
See #43442.

Location:
branches/4.9
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/4.9

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

    r43079 r43080  
    32503250    );
    32513251}
     3252
     3253/**
     3254 * Registers the personal data eraser for comments.
     3255 *
     3256 * @since 4.9.6
     3257 *
     3258 * @param  array $erasers An array of personal data erasers.
     3259 * @return array $erasers An array of personal data erasers.
     3260 */
     3261function wp_register_comment_personal_data_eraser( $erasers ) {
     3262    $erasers[] = array(
     3263        'eraser_friendly_name' => __( 'WordPress Comments' ),
     3264        'callback'             => 'wp_comments_personal_data_eraser',
     3265    );
     3266
     3267    return $erasers;
     3268}
     3269
     3270/**
     3271 * Erases personal data associated with an email address from the comments table.
     3272 *
     3273 * @since 4.9.6
     3274 *
     3275 * @param  string $email_address The comment author email address.
     3276 * @param  int    $page          Comment page.
     3277 * @return array
     3278 */
     3279function wp_comments_personal_data_eraser( $email_address, $page = 1 ) {
     3280    global $wpdb;
     3281
     3282    if ( empty( $email_address ) ) {
     3283        return array(
     3284            'num_items_removed'  => 0,
     3285            'num_items_retained' => 0,
     3286            'messages'           => array(),
     3287            'done'               => true,
     3288        );
     3289    }
     3290
     3291    // Limit us to 500 comments at a time to avoid timing out.
     3292    $number            = 500;
     3293    $page              = (int) $page;
     3294    $num_items_removed = 0;
     3295
     3296    $comments = get_comments(
     3297        array(
     3298            'author_email'       => $email_address,
     3299            'number'             => $number,
     3300            'paged'              => $page,
     3301            'order_by'           => 'comment_ID',
     3302            'order'              => 'ASC',
     3303            'include_unapproved' => true,
     3304        )
     3305    );
     3306
     3307    $anon_author = __( 'Anonymous' );
     3308    $messages    = array();
     3309
     3310    foreach ( (array) $comments as $comment ) {
     3311        $anonymized_comment                         = array();
     3312        $anonymized_comment['comment_agent']        = '';
     3313        $anonymized_comment['comment_author']       = $anon_author;
     3314        $anonymized_comment['comment_author_email'] = wp_privacy_anonymize_data( 'email', $comment->comment_author_email );
     3315        $anonymized_comment['comment_author_IP']    = wp_privacy_anonymize_data( 'ip', $comment->comment_author_IP );
     3316        $anonymized_comment['comment_author_url']   = wp_privacy_anonymize_data( 'url', $comment->comment_author_url );
     3317        $anonymized_comment['user_id']              = 0;
     3318
     3319        $comment_id = (int) $comment->comment_ID;
     3320
     3321        /**
     3322         * Filters whether to anonymize the comment.
     3323         *
     3324         * @since 4.9.6
     3325         *
     3326         * @param bool|string                    Whether to apply the comment anonymization (bool).
     3327         *                                       Custom prevention message (string). Default true.
     3328         * @param WP_Comment $comment            WP_Comment object.
     3329         * @param array      $anonymized_comment Anonymized comment data.
     3330         */
     3331        $anon_message = apply_filters( 'wp_anonymize_comment', true, $comment, $anonymized_comment );
     3332
     3333        if ( true !== $anon_message ) {
     3334            if ( $anon_message && is_string( $anon_message ) ) {
     3335                $messages[] = esc_html( $anon_message );
     3336            } else {
     3337                /* translators: %d: Comment ID */
     3338                $messages[] = sprintf( __( 'Comment %d contains personal data but could not be anonymized.' ), $comment_id );
     3339            }
     3340
     3341            continue;
     3342        }
     3343
     3344        $args = array(
     3345            'comment_ID' => $comment_id,
     3346        );
     3347
     3348        $updated = $wpdb->update( $wpdb->comments, $anonymized_comment, $args );
     3349
     3350        if ( $updated ) {
     3351            $num_items_removed++;
     3352            clean_comment_cache( $comment_id );
     3353        }
     3354    }
     3355
     3356    $done = count( $comments ) < $number;
     3357
     3358    return array(
     3359        'num_items_removed'  => $num_items_removed,
     3360        'num_items_retained' => count( $comments ) - $num_items_removed,
     3361        'messages'           => $messages,
     3362        'done'               => $done,
     3363    );
     3364}
Note: See TracChangeset for help on using the changeset viewer.