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

feat: handle user deletion event on the network #91

Merged
merged 4 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions includes/class-accepted-actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class Accepted_Actions {
'donation_new' => 'Donation_New',
'donation_subscription_cancelled' => 'Donation_Subscription_Cancelled',
'network_user_updated' => 'User_Updated',
'network_user_deleted' => 'User_Deleted',
'newspack_network_woo_membership_updated' => 'Woocommerce_Membership_Updated',
'network_manual_sync_user' => 'User_Manually_Synced',
'network_nodes_synced' => 'Nodes_Synced',
Expand All @@ -53,6 +54,7 @@ class Accepted_Actions {
'donation_new',
'donation_subscription_cancelled',
'network_user_updated',
'network_user_deleted',
'newspack_network_woo_membership_updated',
'network_manual_sync_user',
'network_nodes_synced',
Expand Down
21 changes: 21 additions & 0 deletions includes/class-data-listeners.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public static function register_listeners() {
Data_Events::register_listener( 'woocommerce_subscription_status_changed', 'newspack_node_subscription_changed', [ __CLASS__, 'item_changed' ] );
Data_Events::register_listener( 'woocommerce_order_status_changed', 'newspack_node_order_changed', [ __CLASS__, 'item_changed' ] );
Data_Events::register_listener( 'newspack_network_user_updated', 'network_user_updated', [ __CLASS__, 'user_updated' ] );
Data_Events::register_listener( 'delete_user', 'network_user_deleted', [ __CLASS__, 'user_deleted' ] );
Data_Events::register_listener( 'newspack_network_nodes_synced', 'network_nodes_synced', [ __CLASS__, 'nodes_synced' ] );
}

Expand Down Expand Up @@ -87,6 +88,26 @@ public static function user_updated( $user_data ) {
return $user_data;
}

/**
* Filters the user data for the event being triggered
*
* @param int $id ID of the user to delete.
* @param int|null $reassign ID of the user to reassign posts and links to.
* Default null, for no reassignment.
* @param WP_User $user WP_User object of the user to delete.
* @return array
*/
public static function user_deleted( $id, $reassign, $user ) {
$should_delete = apply_filters( 'newspack_network_process_user_deleted', true, $user->user_email );
if ( ! $should_delete ) {
Debugger::log( 'User deletion with email: ' . $user->user_email . ' was skipped due to filter use.' );
return;
}
return [
'email' => $user->user_email,
];
}

/**
* Filters the nodes data for the event being triggered
*
Expand Down
74 changes: 74 additions & 0 deletions includes/incoming-events/class-user-deleted.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php
/**
* Newspack Hub User Deleted Incoming Event class
*
* @package Newspack
*/

namespace Newspack_Network\Incoming_Events;

use Newspack_Network\Debugger;
use Newspack_Network\Utils\Users as User_Utils;

/**
* Class to handle the user deletion.
*/
class User_Deleted extends Abstract_Incoming_Event {
/**
* Processes the event
*
* @return void
*/
public function post_process_in_hub() {
$this->process_user_deleted();
}

/**
* Process event in Node
*
* @return void
*/
public function process_in_node() {
$this->process_user_deleted();
}

/**
* Process user deleted
*
* @return void
*/
public function process_user_deleted() {
$email = $this->get_email();
Debugger::log( 'Processing user deletion with email: ' . $email );
if ( ! $email ) {
return;
}
$user = get_user_by( 'email', $email );
if ( ! $user ) {
Debugger::log( sprintf( 'User to be deleted not found by email: %s, skipping.', $email ) );
return;
}

// Ensure this is a network reader.
$userdata = get_userdata( $user->ID );
if ( [ NEWSPACK_NETWORK_READER_ROLE ] !== $userdata->roles ) {
Debugger::log( sprintf( 'User %s is not only or not a network reader, skipping deletion.', $email ) );
return;
}

/** Make sure `wp_delete_user()` is available. */
require_once ABSPATH . 'wp-admin/includes/user.php';

// Don't broadcast this deletion on the network.
add_filter( 'newspack_network_process_user_deleted', '__return_false' );
Copy link
Contributor

Choose a reason for hiding this comment

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

Just noting this results in conflicting messaging in the logs where the account is logged as skipped, but then is logged as deleted:

[25-Apr-2024 14:07:47 UTC] [3110] /Users/raz/Sites/newspack/network/includes/class-data-listeners.php:103 User deletion with email: [email protected] was skipped due to filter use.
[25-Apr-2024 14:07:47 UTC] [3110] /Users/raz/Sites/newspack/network/includes/incoming-events/class-user-deleted.php:70 User [email protected] deleted.

Since the final log indicates the deletion was successful, I think this is not a blocker.

// Delete the user.
$result = \wp_delete_user( $user->ID );
remove_filter( 'newspack_network_process_user_deleted', '__return_false' );

if ( $result ) {
Debugger::log( sprintf( 'User %s deleted.', $email ) );
} else {
Debugger::log( sprintf( 'User %s could not be deleted.', $email ) );
}
}
}