Instantly share code, notes, and snippets.

Embed
What would you like to do?
Add support to your plugin or theme for WordPress Personal Data Exporter (GDPR Compliancy).
<?php
/**
* Register eraser for Plugin user data.
*
* @param array $erasers
*
* @return array
*/
function plugin_register_erasers( $erasers = array() ) {
$erasers[] = array(
'eraser_friendly_name' => __( 'User Social Accounts' ),
'callback' => 'plugin_user_data_eraser',
);
return $erasers;
}
add_filter( 'wp_privacy_personal_data_erasers', 'plugin_register_erasers' );
/**
* Eraser for Plugin user data.
*
* @param $email_address
* @param int $page
*
* @return array
*/
function plugin_user_data_eraser( $email_address, $page = 1 ) {
if ( empty( $email_address ) ) {
return array(
'items_removed' => false,
'items_retained' => false,
'messages' => array(),
'done' => true,
);
}
$user = get_user_by( 'email', $email_address );
$messages = array();
$items_removed = false;
$items_retained = false;
if ( $user && $user->ID ) {
// Delete their fb url
$deleted_fb_url = delete_user_meta( $user->ID, 'facebook_url' );
if ( $deleted_fb_url ) {
$items_removed = true;
} else {
$messages[] = __( 'Your Facebook url was unable to be removed at this time.');
$items_retained = true;
}
// Delete their twitter handle
$deleted_twitter_handle = delete_user_meta( $user->ID, 'twitter_handle' );
if ( $deleted_twitter_handle ) {
$items_removed = true;
} else {
$messages[] = __( 'Your twitter url was unable to be removed at this time.');
$items_retained = true;
}
}
// Returns an array of exported items for this pass, but also a boolean whether this exporter is finished.
//If not it will be called again with $page increased by 1.
return array(
'items_removed' => $items_removed,
'items_retained' => $items_retained,
'messages' => $messages,
'done' => true,
);
}
plugin-privacy-user-data-exporter.php
@rgadon107

This comment has been minimized.

rgadon107 commented May 16, 2018

@danieliser Mind if I make some suggestions regarding the file above?

You've got an array where you're using the same set of keys twice in the same function. That's not DRY. I think that array be could abstracted out of the function into it's own separate function. Then use the conditional to check whether $email_address is empty or not. If yes, return the configuration array; if not, keep processing.

Those checks for $deleted_fb_url and $deleted_twitter_handle can also be abstracted away into their own functions. Then call those checker functions in the main function 'plugin_user_data_eraser'. This will make your main function skinnier and easier to read.

Another point: Could you please relocate add_filter( 'wp_privacy_personal_data_erasers', 'plugin_register_erasers' ); above the callback to which it's registered? PHP doesn't care (it will parse the same regardless). But it's easier for humans to read and follow the code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment