Fix browser phone permission issues for phone agents

- Fixed error message display when browser phone initialization fails (now shows actual error instead of "undefined")
- Fixed ajax_get_phone_numbers to use wp_send_json_error instead of wp_die for proper error handling
- Added error handling for phone numbers loading in browser phone interface
- Updated phone_agent role to ensure all necessary capabilities are added to existing roles
- Added automatic capability updates for existing phone_agent roles on plugin load

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-08-13 10:14:20 -07:00
parent 7beb0aa4f3
commit 140a585085
2 changed files with 25 additions and 4 deletions

View File

@@ -2976,7 +2976,8 @@ class TWP_Admin {
check_ajax_referer('twp_ajax_nonce', 'nonce'); check_ajax_referer('twp_ajax_nonce', 'nonce');
if (!current_user_can('manage_options') && !current_user_can('twp_access_phone_numbers')) { if (!current_user_can('manage_options') && !current_user_can('twp_access_phone_numbers')) {
wp_die('Unauthorized'); wp_send_json_error('Unauthorized - Phone number access required');
return;
} }
$twilio = new TWP_Twilio_API(); $twilio = new TWP_Twilio_API();
@@ -5508,7 +5509,9 @@ class TWP_Admin {
$('#browser-phone-error').hide(); $('#browser-phone-error').hide();
setupTwilioDevice(response.data.token); setupTwilioDevice(response.data.token);
} else { } else {
showError('Failed to initialize: ' + response.error); // WordPress wp_send_json_error sends the error message as response.data
var errorMsg = response.data || response.error || 'Unknown error';
showError('Failed to initialize: ' + errorMsg);
} }
}).fail(function() { }).fail(function() {
showError('Failed to connect to server'); showError('Failed to connect to server');
@@ -5690,7 +5693,13 @@ class TWP_Admin {
options += '<option value="' + number.phone_number + '">' + number.phone_number + '</option>'; options += '<option value="' + number.phone_number + '">' + number.phone_number + '</option>';
}); });
$('#caller-id-select').html(options); $('#caller-id-select').html(options);
} else {
console.error('Failed to load phone numbers:', response.data || response.error);
$('#caller-id-select').html('<option value="">Error loading numbers</option>');
} }
}).fail(function(xhr, status, error) {
console.error('Failed to load phone numbers - network error:', error);
$('#caller-id-select').html('<option value="">Error loading numbers</option>');
}); });
// Dialpad functionality // Dialpad functionality

View File

@@ -89,8 +89,20 @@ class TWP_Core {
)); ));
} else { } else {
// Update existing role with any missing capabilities // Update existing role with any missing capabilities
if (!$role->has_cap('twp_access_phone_numbers')) { $capabilities = array(
$role->add_cap('twp_access_phone_numbers'); 'twp_access_voicemails',
'twp_access_call_log',
'twp_access_agent_queue',
'twp_access_outbound_calls',
'twp_access_sms_inbox',
'twp_access_browser_phone',
'twp_access_phone_numbers'
);
foreach ($capabilities as $cap) {
if (!$role->has_cap($cap)) {
$role->add_cap($cap);
}
} }
} }
} }