| 6679 | |
| 6680 | /** |
| 6681 | * Registers the personal data eraser for posts. |
| 6682 | * |
| 6683 | * @since 4.9.6 |
| 6684 | * |
| 6685 | * @param array $erasers An array of personal data erasers. |
| 6686 | * @return array $erasers An array of personal data erasers. |
| 6687 | */ |
| 6688 | function wp_register_posts_personal_data_eraser( $erasers ) { |
| 6689 | $erasers[] = array( |
| 6690 | 'erasser_friendly_name' => __( 'WordPress Posts' ), |
| 6691 | 'callback' => 'wp_posts_personal_data_eraser', |
| 6692 | ); |
| 6693 | |
| 6694 | return $erasers; |
| 6695 | } |
| 6696 | |
| 6697 | /** |
| 6698 | * Erases personal data assicuated with an email address from the post. |
| 6699 | * |
| 6700 | * @since 4.9.6 |
| 6701 | * |
| 6702 | * @param string $email_address The post author email address. |
| 6703 | * @param int $page Post page. |
| 6704 | * @return array |
| 6705 | */ |
| 6706 | function wp_posts_personal_data_eraser( $email_address, $page = 1 ) { |
| 6707 | global $wpdb; |
| 6708 | |
| 6709 | $user = get_user_by( 'email', $email_address ); |
| 6710 | |
| 6711 | if ( empty( $email_address ) || ! $user ) { |
| 6712 | return array( |
| 6713 | 'num_items_removed' => 0, |
| 6714 | 'num_items_retained' => 0, |
| 6715 | 'messages' => array(), |
| 6716 | 'done' => true, |
| 6717 | ); |
| 6718 | } |
| 6719 | |
| 6720 | // Limit us to 500 posts at a time to avoid timing out. |
| 6721 | $number = 500; |
| 6722 | $page = (int) $page; |
| 6723 | $num_items_removed = 0; |
| 6724 | |
| 6725 | $post_query = new WP_Query( |
| 6726 | array( |
| 6727 | 'author' => $user->ID, |
| 6728 | 'posts_per_page' => $number, |
| 6729 | 'paged' => $page, |
| 6730 | 'post_type' => array( |
| 6731 | 'post', |
| 6732 | 'page', |
| 6733 | ), |
| 6734 | 'post_status' => 'any', |
| 6735 | 'order_by' => 'ID', |
| 6736 | 'order' => 'ASC' |
| 6737 | ) |
| 6738 | ); |
| 6739 | |
| 6740 | $messages = array(); |
| 6741 | |
| 6742 | foreach ( (array) $post_query->posts as $post ) { |
| 6743 | $anonymized_post = array(); |
| 6744 | $anonymized_post['post_author'] = wp_privacy_anonymize_data( 'user_id', $post->post_author ); |
| 6745 | |
| 6746 | $post_id = (int) $post->ID; |
| 6747 | |
| 6748 | /** |
| 6749 | * Filters whether to anonymize the post. |
| 6750 | * |
| 6751 | * @since 4.9.6 |
| 6752 | * |
| 6753 | * @param bool|string Whether to apply the user anonymization (bool). |
| 6754 | * Custom prevention message (string). Default true. |
| 6755 | * @param WP_Post $post WP_Post object. |
| 6756 | * @param array $anonymized_post Anonymized post data. |
| 6757 | */ |
| 6758 | $anon_message = apply_filters( 'wp_anonymize_post', true, $post, $anonymized_post ); |
| 6759 | |
| 6760 | if ( true !== $anon_message ) { |
| 6761 | if ($anon_message && is_string ( $anon_message ) ) { |
| 6762 | $messages = esc_html( $anon_message ); |
| 6763 | } else { |
| 6764 | /* translators: [d: Post ID */ |
| 6765 | $messages[] = sprintf( __( 'Post %d contains personal data but could not be anonymized.'), $post_id ); |
| 6766 | } |
| 6767 | |
| 6768 | continue; |
| 6769 | } |
| 6770 | |
| 6771 | $args = array( |
| 6772 | 'ID' => $post_id, |
| 6773 | ); |
| 6774 | |
| 6775 | $updated = $wpdb->update( $wpdb->posts, $anonymized_post, $args ); |
| 6776 | |
| 6777 | if ( $updated ) { |
| 6778 | $num_items_removed++; |
| 6779 | clean_post_cache( $post_id ); |
| 6780 | } |
| 6781 | } |
| 6782 | |
| 6783 | $done = $post_query->max_num_pages <= $page; |
| 6784 | |
| 6785 | return array( |
| 6786 | 'num_items_removed' => $num_items_removed, |
| 6787 | 'num_items_retained' => $post_query->post_count - $num_items_removed, |
| 6788 | 'messages' => $messages, |
| 6789 | 'done' => $done, |
| 6790 | ); |
| 6791 | } |