-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix/55 Fix anonymize functionality from CLI #77
base: trunk
Are you sure you want to change the base?
Changes from all commits
6c6d2b9
3cc90d1
573a44d
5a36139
da906c0
349e9a7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,22 @@ | |
* @return void | ||
*/ | ||
function anonymize_data() { | ||
|
||
copy_and_clear_user_tables(); | ||
|
||
dispatch_anonymize_users(); | ||
|
||
dispatch_anonymize_orders(); | ||
|
||
dispatch_anonymize_customers(); | ||
} | ||
|
||
/** | ||
* Copy the user and user_meta tables to new temporary tables and delete all the non-admin data from the original tables. | ||
* | ||
* @return void | ||
*/ | ||
function copy_and_clear_user_tables() { | ||
global $wpdb; | ||
|
||
// Copy user table to a temporary table that will be anonymized later. | ||
|
@@ -36,6 +52,20 @@ function anonymize_data() { | |
dispatch_anonymize_customers(); | ||
} | ||
|
||
/** | ||
* Move all the anonymized users and their meta from the temp table to the real ones and remove the temp tables. | ||
* | ||
* @return void | ||
*/ | ||
function store_anonymized_user_data() { | ||
global $wpdb; | ||
|
||
$wpdb->query( "INSERT INTO $wpdb->users (SELECT * FROM {$wpdb->users}_temp WHERE id NOT IN (SELECT ID FROM $wpdb->users))" ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Error handling needed for non-standard table that might not exist. |
||
$wpdb->query( "DROP TABLE {$wpdb->users}_temp" ); | ||
$wpdb->query( "INSERT INTO $wpdb->usermeta (SELECT * FROM {$wpdb->usermeta}_temp WHERE user_id NOT IN (SELECT user_id FROM $wpdb->usermeta))" ); | ||
$wpdb->query( "DROP TABLE {$wpdb->usermeta}_temp" ); | ||
} | ||
|
||
/** | ||
* Dispatches a background process to anonymize users. | ||
*/ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
<?php | ||
|
||
use function SafetyNet\Anonymize\anonymize_orders; | ||
use function SafetyNet\Anonymize\copy_and_clear_user_tables; | ||
use function SafetyNet\Anonymize\store_anonymized_user_data; | ||
use function SafetyNet\Anonymize\anonymize_users; | ||
use function SafetyNet\Anonymize\anonymize_customers; | ||
use function SafetyNet\Delete\delete_users_and_orders; | ||
|
@@ -19,10 +21,27 @@ class SafetyNet_CLI extends WP_CLI_Command { | |
* | ||
*/ | ||
public function anonymize( $args ) { | ||
|
||
$info = WP_CLI::colorize( '%pThis process will anonymize your current users, orders and customers with dummy data.%n ' ); | ||
WP_CLI::log( $info ); | ||
|
||
WP_CLI::warning( 'Please proceed with caution if you have a site with a large number of users/orders/customers' ); | ||
|
||
WP_CLI::confirm( 'Are you sure you want to do this?' ); | ||
|
||
WP_CLI::log( '- Copying users to temporary tables ...' ); | ||
copy_and_clear_user_tables(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do any of these functions return data that could be shown to the user? e.g error handling, success message, count of changes rows etc? |
||
|
||
WP_CLI::log( '- Anonymizing users ... ' ); | ||
anonymize_users(); | ||
|
||
WP_CLI::log( '- Storing the anonymized users to the default tables ... ' ); | ||
store_anonymized_user_data(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Each of these steps definitely needs some more verbosity. A nice success message if everything goes to plan, and a detailed error output if not ideally. |
||
|
||
WP_CLI::log( '- Anonymizing orders ... ' ); | ||
anonymize_orders(); | ||
|
||
WP_CLI::log( '- Anonymizing customers ... ' ); | ||
anonymize_customers(); | ||
|
||
update_option( 'anonymized_status', true, false ); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Closing and opening PHP tags aren't needed here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
^ nitpicking 🤣