id === 'abmsense_page_abmsense_notifications') { wp_enqueue_style('abmsense-notifications', plugins_url('css/notifications.css', __FILE__)); wp_enqueue_script('abmsense-admin-notifications', plugins_url('js/notifications-admin.js', __FILE__), array('jquery'), '1.0.0', true); } } add_action('admin_enqueue_scripts', 'abmsense_notifications_admin_scripts'); /** * AJAX handler for deleting users */ function abmsense_toggle_user_state() { // Check nonce check_ajax_referer('abmsense_add_user', 'nonce'); // Check user permissions if (!current_user_can('manage_options')) { wp_send_json_error(array('message' => __('You do not have permission to perform this action.', 'abmsense'))); } // Validate required fields if (empty($_POST['uid'])) { wp_send_json_error(array('message' => __('User ID is required.', 'abmsense'))); } // Get API configuration $config = include(plugin_dir_path(__FILE__) . 'db_config.php'); $public_key = $config['public_key']; // Prepare data for API $data = array( 'uid' => intval($_POST['uid']) ); // Encrypt the data $encrypted_data = abmsense_encrypt_data($data, $public_key); if (is_wp_error($encrypted_data)) { wp_send_json_error(array('message' => $encrypted_data->get_error_message())); } // Make API request to delete (mark as deleted) user $args = abmsense_get_api_request_args(wp_json_encode($encrypted_data)); $response = abmsense_safe_api_request($config['delete_abmsense_user'], $args); if (is_wp_error($response)) { wp_send_json_error(array('message' => $response->get_error_message())); } $response_data = json_decode(wp_remote_retrieve_body($response), true); if (!$response_data || !isset($response_data['success']) || !$response_data['success']) { $error_message = isset($response_data['error']) ? $response_data['error'] : __('Failed to delete user.', 'abmsense'); wp_send_json_error(array('message' => $error_message)); } wp_send_json_success(array('message' => __('User deleted successfully.', 'abmsense'))); } add_action('wp_ajax_abmsense_toggle_user_state', 'abmsense_toggle_user_state'); /** * AJAX handler for adding a new user */ function abmsense_add_user_ajax() { // Check nonce check_ajax_referer('abmsense_add_user', 'nonce'); // Check user permissions if (!current_user_can('manage_options')) { wp_send_json_error(array('message' => __('You do not have permission to perform this action.', 'abmsense'))); } $required_fields = array('customer_cid', 'full_name', 'email', 'real_time_alert', 'scheduled_report'); foreach ($required_fields as $field) { if (empty($_POST[$field])) { // translators: %s: field name wp_send_json_error(array('message' => sprintf(__('Field %s is required.', 'abmsense'), $field))); } } // Get API configuration $config = include(plugin_dir_path(__FILE__) . 'db_config.php'); $public_key = $config['public_key']; // Get current customer name (domain) $customer_name = isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : ''; // Prepare data for API $user_data = array( 'customer_name' => $customer_name, 'full_name' => sanitize_text_field($_POST['full_name']), 'email' => sanitize_email($_POST['email']), 'real_time_alert' => sanitize_text_field($_POST['real_time_alert']), 'scheduled_report' => sanitize_text_field($_POST['scheduled_report']) ); // Encrypt the data $encrypted_data = abmsense_encrypt_data($user_data, $public_key); if (is_wp_error($encrypted_data)) { wp_send_json_error(array('message' => $encrypted_data->get_error_message())); } // Make API request to add user $args = abmsense_get_api_request_args(wp_json_encode($encrypted_data)); $response = abmsense_safe_api_request($config['add_abmsense_user'], $args); if (is_wp_error($response)) { wp_send_json_error(array('message' => $response->get_error_message())); } $response_data = json_decode(wp_remote_retrieve_body($response), true); if (!$response_data || !isset($response_data['success']) || !$response_data['success']) { $error_message = isset($response_data['error']) ? $response_data['error'] : __('Failed to add user.', 'abmsense'); wp_send_json_error(array('message' => $error_message)); } wp_send_json_success(array('message' => __('User added successfully.', 'abmsense'))); } add_action('wp_ajax_abmsense_add_user_ajax', 'abmsense_add_user_ajax'); /** * AJAX handler for editing a user */ function abmsense_edit_user_ajax() { try { // Check nonce check_ajax_referer('abmsense_add_user', 'nonce'); // Check user permissions if (!current_user_can('manage_options')) { throw new Exception(__('You do not have permission to perform this action.', 'abmsense')); } $required_fields = array('uid', 'full_name', 'email', 'real_time_alert', 'scheduled_report'); foreach ($required_fields as $field) { if (empty($_POST[$field])) { // translators: %s: field name throw new Exception(sprintf(__('Field %s is required.', 'abmsense'), $field)); } } // Get API configuration $config = include(plugin_dir_path(__FILE__) . 'db_config.php'); if (!isset($config['update_abmsense_user'])) { throw new Exception('API endpoint for updating user is not configured.'); } $public_key = $config['public_key']; // Prepare data for API $user_data = array( 'uid' => intval($_POST['uid']), 'full_name' => sanitize_text_field($_POST['full_name']), 'email' => sanitize_email($_POST['email']), 'real_time_alert' => sanitize_text_field($_POST['real_time_alert']), 'scheduled_report' => sanitize_text_field($_POST['scheduled_report']) ); // Encrypt the data $encrypted_data = abmsense_encrypt_data($user_data, $public_key); if (is_wp_error($encrypted_data)) { throw new Exception('Encryption failed: ' . $encrypted_data->get_error_message()); } // Make API request to update user $args = abmsense_get_api_request_args(wp_json_encode($encrypted_data)); $response = abmsense_safe_api_request($config['update_abmsense_user'], $args); if (is_wp_error($response)) { throw new Exception('API request failed: ' . $response->get_error_message()); } $response_body = wp_remote_retrieve_body($response); $response_data = json_decode($response_body, true); if (!$response_data) { throw new Exception('Invalid JSON response from API'); } if (!isset($response_data['success']) || !$response_data['success']) { $error_message = isset($response_data['error']) ? $response_data['error'] : 'Unknown error occurred'; throw new Exception('API returned error: ' . $error_message); } wp_send_json_success(array('message' => __('User updated successfully.', 'abmsense'))); } catch (Exception $e) { abmsense_log('Edit User Error: ' . $e->getMessage(), 'error'); abmsense_log('Edit User Error Trace: ' . $e->getTraceAsString(), 'error'); wp_send_json_error(array( 'message' => $e->getMessage(), 'debug' => WP_DEBUG ? array( 'error' => $e->getMessage(), 'trace' => $e->getTraceAsString() ) : null )); } } add_action('wp_ajax_abmsense_edit_user_ajax', 'abmsense_edit_user_ajax'); /** * Get current customer's users and their notification settings via API */ function abmsense_get_customer_users() { try { // Get API configuration $config = include(plugin_dir_path(__FILE__) . 'db_config.php'); $public_key = $config['public_key']; // Get current customer name (domain) $customer_name = isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : ''; // First get the CID using the existing endpoint $cid_data = array( 'customer_name' => $customer_name ); // Encrypt the data $encrypted_cid_data = abmsense_encrypt_data($cid_data, $public_key); if (is_wp_error($encrypted_cid_data)) { throw new Exception($encrypted_cid_data->get_error_message()); } // Make API request to get CID $args = abmsense_get_api_request_args(wp_json_encode($encrypted_cid_data)); $cid_response = abmsense_safe_api_request($config['get_customer_cid'], $args); if (is_wp_error($cid_response)) { throw new Exception($cid_response->get_error_message()); } $cid_response_data = json_decode(wp_remote_retrieve_body($cid_response), true); if (!$cid_response_data || !isset($cid_response_data['success']) || !$cid_response_data['success']) { throw new Exception('Failed to get customer ID: ' . ($cid_response_data['error'] ?? 'Unknown error')); } $customer = $cid_response_data['data']; // Now get users for this CID $users_data = array( 'cid' => $customer['cid'] ); // Encrypt the users request data $encrypted_users_data = abmsense_encrypt_data($users_data, $public_key); if (is_wp_error($encrypted_users_data)) { throw new Exception($encrypted_users_data->get_error_message()); } // Make API request to get users $args = abmsense_get_api_request_args(wp_json_encode($encrypted_users_data)); $users_response = abmsense_safe_api_request($config['get_abmsense_users'], $args); if (is_wp_error($users_response)) { throw new Exception($users_response->get_error_message()); } $users_response_data = json_decode(wp_remote_retrieve_body($users_response), true); if (!$users_response_data || !isset($users_response_data['success']) || !$users_response_data['success']) { throw new Exception('Failed to get users: ' . ($users_response_data['error'] ?? 'Unknown error')); } return array( 'customer' => (object)$customer, 'users' => isset($users_response_data['data']) ? $users_response_data['data'] : array() ); } catch (Exception $e) { abmsense_log('Error: ' . $e->getMessage(), 'error'); return false; } } /** * Callback function to display the Notifications page */ function abmsense_notifications_page() { if (!current_user_can('manage_options')) { wp_die(esc_html__('You do not have sufficient permissions to access this page.', 'abmsense')); } $data = abmsense_get_customer_users(); ?>

BETA

Info:

×

×