| 1 |
<?php |
| 2 |
/** |
| 3 |
* User account related functions |
| 4 |
* |
| 5 |
* @since 1.2.1.2 |
| 6 |
* @author GeoDirectory Team <info@wpgeodirectory.com> |
| 7 |
*/ |
| 8 |
class UsersWP_Account { |
| 9 |
|
| 10 |
|
| 11 |
public function __construct() { |
| 12 |
add_action( 'uwp_account_form_display', array( $this, 'display_form' ), 10, 1 ); |
| 13 |
add_action( 'init', array( $this, 'submit_handler' ) ); |
| 14 |
|
| 15 |
add_filter( 'uwp_get_account_deletion_message', array( $this, 'get_deletion_message' ), 10, 2 ); |
| 16 |
add_action( 'uwp_send_account_deletion_emails', array( $this, 'send_account_deletion_emails' ), 10, 2 ); |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* Displays the account form |
| 21 |
* |
| 22 |
* @since 1.0.0 |
| 23 |
* |
| 24 |
* @param array $type Type of the form |
| 25 |
* |
| 26 |
*/ |
| 27 |
public function display_form( $type ) { |
| 28 |
if ( $type == 'account' ) { |
| 29 |
$design_style = uwp_get_option( 'design_style', 'bootstrap' ); |
| 30 |
$bs_btn_class = $design_style ? 'btn btn-primary btn-block text-uppercase' : ''; |
| 31 |
?> |
| 32 |
<form class="uwp-account-form uwp_form mt-3" method="post" enctype="multipart/form-data"> |
| 33 |
<?php do_action( 'uwp_template_fields', 'account' ); ?> |
| 34 |
<input type="hidden" name="uwp_account_nonce" value="<?php echo esc_attr( wp_create_nonce( 'uwp-account-nonce' ) ); ?>" /> |
| 35 |
<input name="uwp_account_submit" class="<?php echo esc_attr( $bs_btn_class ); ?>" value="<?php esc_attr_e( 'Update Account', 'userswp' ); ?>" type="submit"> |
| 36 |
</form> |
| 37 |
<?php |
| 38 |
} |
| 39 |
|
| 40 |
if ( $type == 'change-password' ) { |
| 41 |
$design_style = uwp_get_option( 'design_style', 'bootstrap' ); |
| 42 |
$bs_btn_class = $design_style ? 'btn btn-primary btn-block text-uppercase' : ''; |
| 43 |
?> |
| 44 |
<form class="uwp-account-form uwp_form mt-3" method="post" enctype="multipart/form-data"> |
| 45 |
<?php do_action( 'uwp_template_fields', 'change' ); ?> |
| 46 |
<input name="uwp_change_submit" class="<?php echo esc_attr( $bs_btn_class ); ?>" value="<?php esc_attr_e( 'Change Password', 'userswp' ); ?>" type="submit"> |
| 47 |
</form> |
| 48 |
<?php |
| 49 |
uwp_password_strength_inline_js(); |
| 50 |
} |
| 51 |
|
| 52 |
if ( $type == 'delete-account' ) { |
| 53 |
if ( 1 == uwp_get_option( 'disable_account_delete' ) || current_user_can( 'administrator' ) ) { |
| 54 |
return; |
| 55 |
} |
| 56 |
?> |
| 57 |
<form class="uwp-account-form uwp_form mt-3" method="post" enctype="multipart/form-data"> |
| 58 |
|
| 59 |
<?php |
| 60 |
$design_style = uwp_get_option( 'design_style', 'bootstrap' ); |
| 61 |
$bs_btn_class = $design_style ? 'btn btn-primary btn-block text-uppercase' : ''; |
| 62 |
|
| 63 |
do_action( 'uwp_template_fields', 'delete-account' ); |
| 64 |
|
| 65 |
$fields = (object) array( |
| 66 |
'htmlvar_name' => 'password', |
| 67 |
'field_type' => 'password', |
| 68 |
'data_type' => 'VARCHAR', |
| 69 |
'default_value' => '', |
| 70 |
'is_required' => 1, |
| 71 |
'help_text' => '', |
| 72 |
'form_label' => __( 'Password', 'userswp' ), |
| 73 |
'site_title' => __( 'Password', 'userswp' ), |
| 74 |
); |
| 75 |
|
| 76 |
$obj = new UsersWP_Templates(); |
| 77 |
$obj->template_fields_html( $fields, 'delete-account' ); |
| 78 |
|
| 79 |
?> |
| 80 |
<input type="hidden" name="uwp_delete_account_nonce" value="<?php echo esc_attr( wp_create_nonce( 'uwp-delete-account-nonce' ) ); ?>" /> |
| 81 |
<input name="uwp_delete_account_submit" class="<?php echo esc_attr( $bs_btn_class ); ?>" value="<?php esc_attr_e( 'Delete Account', 'userswp' ); ?>" type="submit"> |
| 82 |
</form> |
| 83 |
<?php |
| 84 |
} |
| 85 |
|
| 86 |
if ( $type == 'wp2fa' && class_exists( '\WP2FA\WP2FA' ) ) { |
| 87 |
if ( 1 == uwp_get_option( 'disable_wp_2fa' ) ) { |
| 88 |
return; |
| 89 |
} |
| 90 |
echo do_shortcode( '[wp-2fa-setup-form]' ); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Handles the delete account form submission. |
| 96 |
* |
| 97 |
* @since 1.2.1.2 |
| 98 |
* @return void |
| 99 |
*/ |
| 100 |
public function submit_handler() { |
| 101 |
if ( ! isset( $_POST['uwp_delete_account_submit'] ) ) { |
| 102 |
return; |
| 103 |
} |
| 104 |
|
| 105 |
if ( ! isset( $_POST['uwp_delete_account_nonce'] ) || ! wp_verify_nonce( $_POST['uwp_delete_account_nonce'], 'uwp-delete-account-nonce' ) ) { |
| 106 |
return; |
| 107 |
} |
| 108 |
|
| 109 |
global $uwp_notices; |
| 110 |
|
| 111 |
$password = isset( $_POST['password'] ) ? sanitize_text_field( $_POST['password'] ) : ''; |
| 112 |
$user_id = get_current_user_id(); |
| 113 |
$user = get_user_by( 'id', $user_id ); |
| 114 |
|
| 115 |
do_action( 'uwp_before_delete_account', $user_id ); |
| 116 |
|
| 117 |
if ( ! wp_check_password( $password, $user->data->user_pass, $user->ID ) ) { |
| 118 |
$uwp_notices[] = array( |
| 119 |
'account' => aui()->alert( |
| 120 |
array( |
| 121 |
'type' => 'error', |
| 122 |
'content' => __( '<strong>Error</strong>: Incorrect password.', 'userswp' ), |
| 123 |
) |
| 124 |
), |
| 125 |
); |
| 126 |
return; |
| 127 |
} |
| 128 |
|
| 129 |
$errors = apply_filters( 'uwp_delete_account_validate', $user ); |
| 130 |
|
| 131 |
if ( ! empty( $errors->get_error_code() ) ) { |
| 132 |
$uwp_notices[] = array( |
| 133 |
'account' => aui()->alert( |
| 134 |
array( |
| 135 |
'type' => 'error', |
| 136 |
'content' => $errors->get_error_message(), |
| 137 |
) |
| 138 |
), |
| 139 |
); |
| 140 |
return; |
| 141 |
} |
| 142 |
|
| 143 |
$ms_delete = apply_filters( 'uwp_delete_delete_from_network', true ); |
| 144 |
$num_blogs_of_user = is_multisite() ? count( get_blogs_of_user( $user_id ) ) : 1; |
| 145 |
$delete_from_network = ( is_multisite() && ( $ms_delete == true || $num_blogs_of_user == 1 ) ) ? true : false; |
| 146 |
|
| 147 |
include_once ABSPATH . 'wp-admin/includes/user.php'; |
| 148 |
|
| 149 |
if ( is_multisite() ) { |
| 150 |
include_once ABSPATH . WPINC . '/ms-functions.php'; |
| 151 |
include_once ABSPATH . 'wp-admin/includes/ms.php'; |
| 152 |
} |
| 153 |
|
| 154 |
$message = $this->get_deletion_message( '', $user ); |
| 155 |
|
| 156 |
if ( $delete_from_network ) { |
| 157 |
if ( in_array( $user->user_login, get_super_admins(), true ) ) { |
| 158 |
$uwp_notices[] = array( |
| 159 |
'account' => aui()->alert( |
| 160 |
array( |
| 161 |
'type' => 'error', |
| 162 |
'content' => __( '<strong>Error</strong>: Super Administrators cannot be deleted.', 'userswp' ), |
| 163 |
) |
| 164 |
), |
| 165 |
); |
| 166 |
return; |
| 167 |
} |
| 168 |
|
| 169 |
$deleted = wpmu_delete_user( $user_id ); |
| 170 |
} else { |
| 171 |
$deleted = wp_delete_user( $user_id ); |
| 172 |
} |
| 173 |
|
| 174 |
if ( $deleted ) { |
| 175 |
$this->send_account_deletion_emails( $user, $message ); |
| 176 |
} |
| 177 |
|
| 178 |
do_action( 'uwp_after_delete_account', $user_id, $deleted ); |
| 179 |
|
| 180 |
wp_logout(); |
| 181 |
wp_safe_redirect( home_url() ); |
| 182 |
exit(); |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* Generate the account deletion message. |
| 187 |
* |
| 188 |
* @since 1.2.28 |
| 189 |
* @param string $message The initial message (empty by default). |
| 190 |
* @param WP_User $user The user being deleted. |
| 191 |
* @return string The formatted deletion message. |
| 192 |
*/ |
| 193 |
public function get_deletion_message( $message, $user ) { |
| 194 |
$message_parts = array( |
| 195 |
'header' => '<p><strong>' . esc_html__( 'Deleted user information:', 'userswp' ) . '</strong></p>', |
| 196 |
'first_name' => '<p>' . esc_html__( 'First Name:', 'userswp' ) . ' ' . esc_html( $user->first_name ) . '</p>', |
| 197 |
'last_name' => '<p>' . esc_html__( 'Last Name:', 'userswp' ) . ' ' . esc_html( $user->last_name ) . '</p>', |
| 198 |
'username' => '<p>' . esc_html__( 'Username:', 'userswp' ) . ' ' . esc_html( $user->user_login ) . '</p>', |
| 199 |
'email' => '<p>' . esc_html__( 'Email:', 'userswp' ) . ' ' . esc_html( $user->user_email ) . '</p>', |
| 200 |
); |
| 201 |
|
| 202 |
/** |
| 203 |
* Filters the account deletion message parts. |
| 204 |
* |
| 205 |
* @since 1.2.28 |
| 206 |
* @param array $message_parts The message parts. |
| 207 |
* @param WP_User $user The user being deleted. |
| 208 |
*/ |
| 209 |
$message_parts = apply_filters( 'uwp_account_deletion_message_parts', $message_parts, $user ); |
| 210 |
|
| 211 |
$message .= implode( '', $message_parts ); |
| 212 |
|
| 213 |
/** |
| 214 |
* Filters the final account deletion message. |
| 215 |
* |
| 216 |
* @since 1.2.28 |
| 217 |
* @param string $message The deletion message. |
| 218 |
* @param WP_User $user The user being deleted. |
| 219 |
*/ |
| 220 |
return apply_filters( 'uwp_account_deletion_message', $message, $user ); |
| 221 |
} |
| 222 |
|
| 223 |
/** |
| 224 |
* Sends account deletion emails. |
| 225 |
* |
| 226 |
* @since 1.2.28 |
| 227 |
* @param WP_User $user The user being deleted. |
| 228 |
* @param string $message The deletion message. |
| 229 |
*/ |
| 230 |
public function send_account_deletion_emails( $user, $message ) { |
| 231 |
$email_vars = array( |
| 232 |
'login_details' => $message, |
| 233 |
'user_name' => ! empty( $user->display_name ) ? esc_attr( $user->display_name ) : '', |
| 234 |
); |
| 235 |
|
| 236 |
do_action( 'uwp_before_account_delete_email', $user, $email_vars ); |
| 237 |
|
| 238 |
UsersWP_Mails::send( $user->user_email, 'account_delete', $email_vars ); |
| 239 |
UsersWP_Mails::send( get_bloginfo( 'admin_email' ), 'account_delete', $email_vars, true ); |
| 240 |
|
| 241 |
do_action( 'uwp_after_account_delete_email', $user, $email_vars ); |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
new UsersWP_Account(); |
| 246 |
|