Make WordPress Core

Ticket #43440: 43440.1.diff

File 43440.1.diff, 1.6 KB (added by xkon, 7 years ago)

minor fixes and correct json output

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

     
    210210        include( ABSPATH . 'wp-admin/admin-footer.php' );
    211211        die;
    212212}
     213
     214/**
     215 * Export comments by e-mail for the GDPR "Right to Portability" as json
     216 *
     217 * @since 5.0.0
     218 *
     219 * @param string $email Commenters Email
     220 *
     221 * @uses sanitize_email()
     222 * @uses is_email()
     223 * @uses get_comments()
     224 * @uses json_encode()
     225 *
     226 * @return string $comments Returns comments if they exist, else fall back to a notification message.
     227 *
     228 */
     229
     230function export_comments( $email ) {
     231
     232        $commenter_email = sanitize_email( $email );
     233
     234        if ( is_email( $commenter_email ) ) {
     235
     236                $args = array(
     237                        'author_email' => $commenter_email,
     238                );
     239
     240                $comments = get_comments( $args );
     241
     242                if ( ! empty( $comments ) ) {
     243                        $the_comments = array();
     244                        foreach ( $comments as $comment ) {
     245                                $the_comments[] = array(
     246                                        'date'         => $comment->comment_date,
     247                                        'author_email' => $comment->comment_author_email,
     248                                        'author_name'  => $comment->comment_author,
     249                                        'author_url'   => $comment->comment_author_url,
     250                                        'comment'      => $comment->comment_content,
     251                                );
     252                        }
     253                        return json_encode( $the_comments );
     254                } else {
     255                        return sprintf(
     256                                // translators: %s is the commenters email address.
     257                                __( '%s has no comments.' ),
     258                                $email
     259                        );
     260                }
     261        } else {
     262                return sprintf(
     263                        // translators: %s is the email address provided.
     264                        __( '%s is not a valid e-mail.' ),
     265                        $email
     266                );
     267        }
     268
     269}