From 30c4ba215bf59490a8658eb4d063778d71d9191c Mon Sep 17 00:00:00 2001 From: ashubawork Date: Tue, 3 Oct 2023 14:34:59 +0300 Subject: [PATCH] - delete comments after user deleting --- includes/admin/core/class-admin-settings.php | 6 ++++++ includes/class-config.php | 1 + includes/core/class-user.php | 12 +++++++++++- 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/includes/admin/core/class-admin-settings.php b/includes/admin/core/class-admin-settings.php index 0f4bb2bd..4dcbb88e 100644 --- a/includes/admin/core/class-admin-settings.php +++ b/includes/admin/core/class-admin-settings.php @@ -1229,6 +1229,12 @@ if ( ! class_exists( 'um\admin\core\Admin_Settings' ) ) { 'tooltip' => __( 'How long does an activation link live in seconds? Leave empty for endless links.', 'ultimate-member' ), 'size' => 'small', ), + array( + 'id' => 'delete_comments', + 'type' => 'checkbox', + 'label' => __( 'Deleting user comments after deleting a user', 'ultimate-member' ), + 'tooltip' => __( 'Do you want to delete a user\'s comments when that user deletes themself or is removed from the admin dashboard from the site?', 'ultimate-member' ), + ), ), ), 'account' => array( diff --git a/includes/class-config.php b/includes/class-config.php index c246004b..2b570c3b 100644 --- a/includes/class-config.php +++ b/includes/class-config.php @@ -596,6 +596,7 @@ if ( ! class_exists( 'um\Config' ) ) { 'secure_notify_admins_banned_accounts' => false, 'secure_notify_admins_banned_accounts__interval' => 'instant', 'secure_allowed_redirect_hosts' => '', + 'delete_comments' => 0, ); add_filter( 'um_get_tabs_from_config', '__return_true' ); diff --git a/includes/core/class-user.php b/includes/core/class-user.php index 737dabdd..3c97e305 100644 --- a/includes/core/class-user.php +++ b/includes/core/class-user.php @@ -569,7 +569,7 @@ if ( ! class_exists( 'um\core\User' ) ) { /** * @param $user_id */ - function delete_user_handler( $user_id ) { + public function delete_user_handler( $user_id ) { um_fetch_user( $user_id ); $this->deleted_user_id = $user_id; @@ -613,6 +613,16 @@ if ( ! class_exists( 'um\core\User' ) ) { */ do_action( 'um_delete_user', um_user( 'ID' ) ); + // remove user's comments + if ( UM()->options()->get( 'delete_comments' ) ) { + $user = get_user_by( 'id', um_user( 'ID' ) ); + + $comments = array_merge( get_comments( 'author_email=' . $user->user_email ), get_comments( 'user_id=' . um_user( 'ID' ) ) ); + foreach ( $comments as $comment ) { + wp_delete_comment( $comment->comment_ID, true ); + } + } + // send email notifications if ( $this->send_mail_on_delete ) { UM()->mail()->send( um_user( 'user_email' ), 'deletion_email' );