בס״ד

Admin Notify Settings

Admin Notification Email
roles; } } add_action( 'pre_user_update', 'Eliyahna\AdminNotify\admin_notify_capture_pre_update' ); // --------------------------------------------------------------------------- // profile_update — password changes and role changes via the profile screen // --------------------------------------------------------------------------- function admin_notify_user_updated( $user_id, $old_user_data ) { global $eliyahna_notified_users, $eliyahna_pre_update_roles; $user = get_user_by( 'id', $user_id ); $new_roles = (array) $user->roles; // Use pre-captured roles when available (more reliable than $old_user_data->roles // which is not always populated correctly by WordPress core). $old_roles = isset( $eliyahna_pre_update_roles[ $user_id ] ) ? $eliyahna_pre_update_roles[ $user_id ] : (array) $old_user_data->roles; $role_changed = ( $old_roles !== $new_roles ); // --- Role change notification --- // Only send here if the role actually changed AND set_user_role has not // already fired for this user (to avoid double notifications). if ( $role_changed && ! in_array( $user_id, $eliyahna_notified_users ) ) { $old_role_label = admin_notify_format_roles( $old_roles ); $new_role_label = admin_notify_format_roles( $new_roles ); $subject = 'User Role Change Notification'; $body = "A user's role has been changed.\n\n" . "Previous role: {$old_role_label}\n" . "New role: {$new_role_label}"; admin_notify_send_email( $user, $subject, $body ); $eliyahna_notified_users[] = $user_id; } // --- Password change notification --- // WordPress only updates user_pass when it has actually changed. if ( $old_user_data->user_pass !== $user->user_pass ) { $roles_label = admin_notify_format_roles( $new_roles ); $subject = 'User Password Change Notification'; $body = "A user's password has been changed.\n\nRole: {$roles_label}"; admin_notify_send_email( $user, $subject, $body ); // Note: password changes are not added to $eliyahna_notified_users so // that a simultaneous role change also gets its own email. } } add_action( 'profile_update', 'Eliyahna\AdminNotify\admin_notify_user_updated', 10, 2 ); // --------------------------------------------------------------------------- // set_user_role — role changes made programmatically or via the Users list // --------------------------------------------------------------------------- /** * @param int $user_id * @param string $new_role The role the user is being changed TO. * @param array $old_roles The roles the user had BEFORE the change. */ function admin_notify_role_changed( $user_id, $new_role, $old_roles ) { global $eliyahna_notified_users; // Prevent duplicate if profile_update already handled this. if ( in_array( $user_id, $eliyahna_notified_users ) ) { return; } $user = get_user_by( 'id', $user_id ); $old_role_label = admin_notify_format_roles( (array) $old_roles ); $new_role_label = admin_notify_format_role( $new_role ); $subject = 'User Role Change Notification'; $body = "A user's role has been changed.\n\n" . "Previous role: {$old_role_label}\n" . "New role: {$new_role_label}"; admin_notify_send_email( $user, $subject, $body ); $eliyahna_notified_users[] = $user_id; } // set_user_role passes 3 arguments: user_id, new_role, old_roles add_action( 'set_user_role', 'Eliyahna\AdminNotify\admin_notify_role_changed', 10, 3 ); // --------------------------------------------------------------------------- // user_register — new user added // --------------------------------------------------------------------------- function admin_notify_user_registered( $user_id ) { $user = get_user_by( 'id', $user_id ); $roles_label = admin_notify_format_roles( (array) $user->roles ); $subject = 'New User Account Notification'; $body = "A new user account has been created.\n\nRole: {$roles_label}"; admin_notify_send_email( $user, $subject, $body ); } add_action( 'user_register', 'Eliyahna\AdminNotify\admin_notify_user_registered' ); // --------------------------------------------------------------------------- // delete_user — any user deleted // --------------------------------------------------------------------------- function admin_notify_user_deleted( $user_id ) { $user = get_user_by( 'id', $user_id ); if ( $user ) { $roles_label = admin_notify_format_roles( (array) $user->roles ); $subject = 'User Account Deletion Notification'; $body = "A user account has been deleted.\n\nRole: {$roles_label}"; admin_notify_send_email( $user, $subject, $body ); } } add_action( 'delete_user', 'Eliyahna\AdminNotify\admin_notify_user_deleted' ); // --------------------------------------------------------------------------- // Helpers // --------------------------------------------------------------------------- /** * Converts a role slug to a human-readable display name. */ function admin_notify_format_role( $role_slug ) { global $wp_roles; if ( ! isset( $wp_roles ) ) { $wp_roles = new \WP_Roles(); } $names = $wp_roles->get_names(); return isset( $names[ $role_slug ] ) ? translate_user_role( $names[ $role_slug ] ) : ucfirst( $role_slug ); } /** * Converts an array of role slugs to a comma-separated display string. */ function admin_notify_format_roles( $roles ) { if ( empty( $roles ) ) { return 'None'; } return implode( ', ', array_map( 'Eliyahna\AdminNotify\admin_notify_format_role', $roles ) ); } /** * Sends the notification email. * * @param \WP_User $user * @param string $subject * @param string $message_body Event-specific description (already includes role info). */ function admin_notify_send_email( $user, $subject, $message_body ) { $notify_email = get_option( ELIYAHNA_ADMIN_NOTIFY_OPTION ); if ( $notify_email && is_email( $notify_email ) ) { $message = $message_body . "\n\n"; $message .= "Username: " . esc_html( $user->user_login ) . "\n"; $message .= "Email: " . esc_html( $user->user_email ) . "\n"; $message .= "Date: " . current_time( 'mysql' ) . "\n"; wp_mail( $notify_email, $subject, $message ); } } // --------------------------------------------------------------------------- // Save settings (with nonce check) // --------------------------------------------------------------------------- function admin_notify_save_settings() { if ( isset( $_POST['eliyahna_admin_notify_nonce'] ) && wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['eliyahna_admin_notify_nonce'] ) ), 'eliyahna_admin_notify_save_email' ) ) { if ( isset( $_POST[ ELIYAHNA_ADMIN_NOTIFY_OPTION ] ) ) { $email = sanitize_email( wp_unslash( $_POST[ ELIYAHNA_ADMIN_NOTIFY_OPTION ] ) ); update_option( ELIYAHNA_ADMIN_NOTIFY_OPTION, $email ); } } else { wp_die( 'Nonce verification failed!' ); } } add_action( 'admin_post_save_eliyahna_admin_notify_settings', 'Eliyahna\AdminNotify\admin_notify_save_settings' );