| | 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 | */ |
| | 3284 | function 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 data which could be used to identify a person from comments assocated with an email address. |
| | 3295 | * |
| | 3296 | * @param string $email The comment author email address. |
| | 3297 | * @param int $page Comment page, zero based. |
| | 3298 | * @return array An array of personal data in name value pairs |
| | 3299 | */ |
| | 3300 | function wp_comments_personal_data_exporter( $email_address, $page = 0 ) { |
| | 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 | $offset = $number * (int) $page; |
| | 3309 | |
| | 3310 | $comments = get_comments( |
| | 3311 | array( |
| | 3312 | 'author_email' => $email_address, |
| | 3313 | 'number' => $number, |
| | 3314 | 'offset' => $offset, |
| | 3315 | 'order_by' => 'comment_ID', |
| | 3316 | 'order' => 'ASC', |
| | 3317 | ) |
| | 3318 | ); |
| | 3319 | |
| | 3320 | $comment_personal_data_props = array( |
| | 3321 | 'comment_author' => __( 'Comment Author' ), |
| | 3322 | 'comment_author_email' => __( 'Comment Author Email' ), |
| | 3323 | 'comment_author_url' => __( 'Comment Author URL' ), |
| | 3324 | 'comment_author_IP' => __( 'Comment Author IP' ), |
| | 3325 | 'comment_agent' => __( 'Comment Agent' ), |
| | 3326 | ); |
| | 3327 | |
| | 3328 | $personal_data = $compare = array_fill_keys( array_keys( $comment_personal_data_props ), array() ); |
| | 3329 | |
| | 3330 | foreach ( $comment_personal_data_props as $key => $name ) { |
| | 3331 | $personal_data[ $key ] = array( 'name' => $name, 'values' => array() ); |
| | 3332 | } |
| | 3333 | |
| | 3334 | foreach ( (array) $comments as $comment ) { |
| | 3335 | foreach ( $comment_personal_data_props as $key => $name ) { |
| | 3336 | $value = $comment->$key; |
| | 3337 | |
| | 3338 | if ( ! empty( $value ) && ! in_array( $value, $compare[ $key ], true ) ) { |
| | 3339 | // Add the value to the comparison array... |
| | 3340 | $compare[ $key ][] = $value; |
| | 3341 | // and to the data export. |
| | 3342 | $personal_data[ $key ]['values'][] = $value; |
| | 3343 | } |
| | 3344 | } |
| | 3345 | } |
| | 3346 | |
| | 3347 | $done = count( $comments ) < $number; |
| | 3348 | |
| | 3349 | return array( |
| | 3350 | 'data' => $personal_data, |
| | 3351 | 'done' => $done, |
| | 3352 | ); |
| | 3353 | } |