Skip to content
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

Open
wants to merge 6 commits into
base: trunk
Choose a base branch
from

Conversation

bernattorras
Copy link
Contributor

Description of the issue

In #55 we decided to remove the anonymization functionality from the plugin, leaving only the CLI command to anonymize the users, orders, and customers.

When I tried the wp safety-net anonymize CLI command it got stuck and didn't anonymize any user data.

After taking a look at it, I've found that the problem was that the temporary tables weren't being used for copying, processing and storing the user details. These DB operations were only used for the background processes (which the CLI isn't using).

Changes proposed in this PR

  • Move these DB operations to new standalone functions
  • Call these functions when needed from the CLI command (before and after anonymizing the users)
  • Since the anonymization process affects the database and can cause some issues on big sites, I've added a warning and a prompt asking for the customer to confirm the execution of this functionality.
  • Added some feedback lines in the terminal to show the process of the command (it could help in case it gets stuck again due to any issues).

image

Steps to test

  1. Use the trunk version of the plugin
  2. Create some dummy users (i.e wp user generate 100)
  3. Run the wp safety-net anonymize command
    [It should get stuck without anonymizing any user]
  4. Confirm that the users haven't been anonymized
  5. Switch to this branch
  6. Run the wp safety-net anonymize command again
  7. Confirm that a warning appears and you're asked for a confirmation in order to continue with the anonymization.
  8. Confirm that the anonymization process gets cancelled when choosing n.
  9. Confirm that the anonymization process starts when choosing y.
  10. Confirm that feedback lines appear before anonymizing users, orders and customers (check the previous image)
  11. Confirm that the users/orders/customers data has been properly anonymized.

Notes

  • I've removed an orphan wp_ajax_safety_net_anonymize_users hook (not needed since this functionality was removed from the admin in Added actions toggle, removed anonymize #62.
  • I've updated the code of includes/amin.php to fix some phpcs issues (mostly related to shorthand syntax)
  • The wording of the warning or feedback lines that appear when running the CLI command can probably be improved. Feel free to review them and suggest any changes.

Fixes #55

@tommusrhodus
Copy link
Contributor

@bernattorras I'm running into an issue running the testing steps at point 9. Output:

➜  public wp safety-net anonymize
This process will anonymize your current users, orders and customers with dummy data. 
Warning: Please proceed with caution if you have a site with a large number of users/orders/customers
Are you sure you want to do this? [y/n] y
- Copying users to temporary tables ...
- Anonymizing users ... 
<div id="error"><p class="wpdberror"><strong>WordPress database error:</strong> [Table &#039;local.wp_users_temp&#039; doesn&#039;t exist]<br /><code>SELECT ID 
				FROM wp_users_temp 
					LIMIT 500 
				OFFSET 0</code></p></div>

Copy link
Contributor

@tommusrhodus tommusrhodus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work! Left some comments on bits that jumped out at me.

@@ -45,14 +44,14 @@ function enqueue_scripts( string $hook_suffix ) {
return;
}

wp_enqueue_script( 'safety-net-admin', SAFETY_NET_URL . 'assets/js/safety-net-admin.js', [ 'jquery' ], '1.0', true );
wp_enqueue_script( 'safety-net-admin', SAFETY_NET_URL . 'assets/js/safety-net-admin.js', array( 'jquery' ), '1.0', true );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we tie asset version numbers to the plugin version, or maybe a constant defined in the main plugin file? Hard-coded version numbers may get forgotten.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not 100% sure which would be the best way to version this script... Linking it to the plugin version seems too strict (as I understand that minor changes could be applied without updating the plugin version number).

I kinda feel the same way about generic asset versioning...

For now, I've decided to use the filemtime of the file. I know that this can generate false updates when using multiple servers though...

@tommusrhodus which approach would you recommend for this specific case?

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))" );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Error handling needed for non-standard table that might not exist.

WP_CLI::confirm( 'Are you sure you want to do this?' );

WP_CLI::log( '- Copying users to temporary tables ...' );
copy_and_clear_user_tables();
Copy link
Contributor

Choose a reason for hiding this comment

The 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?

Comment on lines 163 to +165
} ?>

<?php if ( 'checkbox' === $args['type'] ) :
<?php
Copy link
Contributor

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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

^ nitpicking 🤣

anonymize_users();

WP_CLI::log( '- Storing the anonymized users to the default tables ... ' );
store_anonymized_user_data();
Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Remove Anonymization Functionality from plugin
2 participants