WordPress.org

Make WordPress Core

Ticket #43442: 43442_anonymize.diff

File 43442_anonymize.diff, 1.5 KB (added by fclaussen, 4 years ago)

Anonymization function for comments

  • src/wp-admin/includes/comment.php

    diff --git a/src/wp-admin/includes/comment.php b/src/wp-admin/includes/comment.php
    index 543cc8b2c3..32d25885fa 100644
    a b function comment_exists( $comment_author, $comment_date, $timezone = 'blog' ) { 
    4242        );
    4343}
    4444
     45/**
     46 * Anonymizes comments by e-mail for the GDPR "Right to Anonymization".
     47 *
     48 * @since 5.0.0
     49 *
     50 * @param string $email Commenters e-mail.
     51 *
     52 * @uses sanitize_email()
     53 * @uses is_email()
     54 * @uses get_comments()
     55 * @uses wp_update_comment()
     56 *
     57 * @return bool True on success, false if invalid email or no comments found.
     58 */
     59function anonymize_comments( $email ) {
     60
     61        $email = sanitize_email( $_POST['user_email'] );
     62
     63        if ( ! is_email( $email ) ) {
     64                return false;
     65        }
     66
     67        $args = array(
     68                'author_email' => $email,
     69                'include_unapproved' => true,
     70        );
     71
     72        $comments = get_comments( $args );
     73
     74        if ( empty( $comments ) ) {
     75                return false;
     76        }
     77
     78        foreach ( $comments as $comment ) {
     79                $new_comment = array();
     80                $new_comment['comment_ID'] = $comment->comment_ID;
     81                $new_comment['comment_author_IP'] = '0.0.0.0';
     82                $new_comment['comment_author_email'] = '';
     83                $new_comment['comment_author_url'] = '';
     84                $new_comment['comment_agent'] = '';
     85                $new_comment['comment_author'] = esc_html__( 'Guest', 'gdpr' );
     86                $new_comment['user_id'] = 0;
     87                wp_update_comment( $new_comment );
     88        }
     89
     90        return true;
     91}
     92
    4593/**
    4694 * Update a comment with values provided in $_POST.
    4795 *