בס״ד

Admin Notify Settings

Admin Notification Email
roles; $new_roles = (array) $user->roles; // Check if the user is upgraded to admin if (in_array('administrator', $new_roles) && !in_array('administrator', $old_roles)) { admin_notify_send_email($user, 'Admin Role Upgrade Notification', 'A user has been upgraded to an administrator.'); $eliyahna_notified_users[] = $user_id; } // Check if the user is downgraded from admin if (!in_array('administrator', $new_roles) && in_array('administrator', $old_roles)) { admin_notify_send_email($user, 'Admin Role Downgrade Notification', 'A user has been downgraded from an administrator.'); $eliyahna_notified_users[] = $user_id; } } add_action('profile_update', 'Eliyahna\AdminNotify\admin_notify_user_updated', 10, 2); add_action('user_register', 'Eliyahna\AdminNotify\admin_notify_user_updated', 10, 2); // Hook into role change (ensure admin role upgrades and downgrades are notified) function admin_notify_role_changed($user_id, $role) { global $eliyahna_notified_users; // Prevent duplicate notifications for the same user if (in_array($user_id, $eliyahna_notified_users)) { return; } $user = get_user_by('id', $user_id); if ($role === 'administrator') { admin_notify_send_email($user, 'Admin Role Upgrade Notification', 'A user has been upgraded to an administrator.'); $eliyahna_notified_users[] = $user_id; } else { admin_notify_send_email($user, 'Admin Role Downgrade Notification', 'A user has been downgraded from an administrator.'); $eliyahna_notified_users[] = $user_id; } } add_action('set_user_role', 'Eliyahna\AdminNotify\admin_notify_role_changed', 10, 2); // Hook into user deletion (notify when admin account is deleted) function admin_notify_user_deleted($user_id) { $user = get_user_by('id', $user_id); if ($user && in_array('administrator', (array) $user->roles)) { admin_notify_send_email($user, 'Admin Account Deletion Notification', 'An administrator account has been deleted.'); } } add_action('delete_user', 'Eliyahna\AdminNotify\admin_notify_user_deleted'); // Function to send email notification 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');