All checks were successful
Create Release / build (push) Successful in 5s
- Relocate update section (version check, repo config, token) to Settings - Fix download URL for private repos: append Gitea auth token - Mobile App page now only has FCM/notification settings Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
287 lines
12 KiB
PHP
287 lines
12 KiB
PHP
<?php
|
|
/**
|
|
* Mobile App Settings Page
|
|
*/
|
|
|
|
// Prevent direct access
|
|
if (!defined('WPINC')) {
|
|
die;
|
|
}
|
|
|
|
// Check user capabilities
|
|
if (!current_user_can('manage_options')) {
|
|
wp_die(__('You do not have sufficient permissions to access this page.'));
|
|
}
|
|
|
|
// Handle test notification
|
|
if (isset($_POST['twp_test_notification']) && check_admin_referer('twp_mobile_settings')) {
|
|
require_once TWP_PLUGIN_DIR . 'includes/class-twp-fcm.php';
|
|
$fcm = new TWP_FCM();
|
|
$test_user_id = get_current_user_id();
|
|
$notification_sent = $fcm->send_test_notification($test_user_id);
|
|
|
|
if ($notification_sent) {
|
|
$notification_result = array('success' => true, 'message' => 'Test notification sent successfully!');
|
|
} else {
|
|
$notification_result = array('success' => false, 'message' => 'Failed to send test notification. Check FCM configuration.');
|
|
}
|
|
}
|
|
|
|
// Save settings
|
|
if (isset($_POST['twp_save_mobile_settings']) && check_admin_referer('twp_mobile_settings')) {
|
|
update_option('twp_fcm_project_id', sanitize_text_field($_POST['twp_fcm_project_id']));
|
|
// Service account JSON — validate it parses as JSON before saving
|
|
$sa_json_raw = isset($_POST['twp_fcm_service_account_json']) ? wp_unslash($_POST['twp_fcm_service_account_json']) : '';
|
|
if (!empty($sa_json_raw)) {
|
|
$sa_parsed = json_decode($sa_json_raw, true);
|
|
if ($sa_parsed && isset($sa_parsed['client_email'], $sa_parsed['private_key'])) {
|
|
update_option('twp_fcm_service_account_json', $sa_json_raw);
|
|
} else {
|
|
$sa_json_error = 'Invalid service account JSON — must contain client_email and private_key fields.';
|
|
}
|
|
} else {
|
|
update_option('twp_fcm_service_account_json', '');
|
|
}
|
|
$settings_saved = true;
|
|
}
|
|
|
|
// Get current settings
|
|
$fcm_project_id = get_option('twp_fcm_project_id', '');
|
|
$fcm_service_account_json = get_option('twp_fcm_service_account_json', '');
|
|
$fcm_sa_configured = !empty($fcm_service_account_json) && !empty($fcm_project_id);
|
|
// Get mobile app statistics
|
|
global $wpdb;
|
|
$sessions_table = $wpdb->prefix . 'twp_mobile_sessions';
|
|
$active_sessions = $wpdb->get_var("SELECT COUNT(*) FROM $sessions_table WHERE is_active = 1 AND expires_at > NOW()");
|
|
$total_sessions = $wpdb->get_var("SELECT COUNT(*) FROM $sessions_table");
|
|
|
|
?>
|
|
|
|
<div class="wrap">
|
|
<h1><?php echo esc_html(get_admin_page_title()); ?></h1>
|
|
|
|
<?php if (isset($settings_saved)): ?>
|
|
<div class="notice notice-success is-dismissible">
|
|
<p><strong>Settings saved successfully!</strong></p>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<?php if (isset($notification_result)): ?>
|
|
<div class="notice notice-<?php echo $notification_result['success'] ? 'success' : 'error'; ?> is-dismissible">
|
|
<p><strong><?php echo esc_html($notification_result['message']); ?></strong></p>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<?php if (isset($sa_json_error)): ?>
|
|
<div class="notice notice-error is-dismissible">
|
|
<p><strong><?php echo esc_html($sa_json_error); ?></strong></p>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<div class="twp-mobile-settings">
|
|
<!-- Mobile App Overview -->
|
|
<div class="card" style="max-width: 100%; margin-bottom: 20px;">
|
|
<h2>Mobile App Overview</h2>
|
|
<table class="widefat">
|
|
<tbody>
|
|
<tr>
|
|
<td><strong>API Endpoint:</strong></td>
|
|
<td><code><?php echo esc_html(site_url('/wp-json/twilio-mobile/v1')); ?></code></td>
|
|
</tr>
|
|
<tr>
|
|
<td><strong>Active Sessions:</strong></td>
|
|
<td><?php echo esc_html($active_sessions); ?> active / <?php echo esc_html($total_sessions); ?> total</td>
|
|
</tr>
|
|
<tr>
|
|
<td><strong>Plugin Version:</strong></td>
|
|
<td><?php echo esc_html(TWP_VERSION); ?></td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
|
|
<!-- Mobile App Settings Form -->
|
|
<form method="post" action="">
|
|
<?php wp_nonce_field('twp_mobile_settings'); ?>
|
|
|
|
<!-- FCM Configuration -->
|
|
<div class="card" style="max-width: 100%; margin-bottom: 20px;">
|
|
<h2>Firebase Cloud Messaging (FCM) — HTTP v2 API</h2>
|
|
<p>Configure FCM using a service account for push notifications. The legacy server key API has been retired by Google.</p>
|
|
|
|
<table class="form-table">
|
|
<tr>
|
|
<th scope="row">
|
|
<label for="twp_fcm_project_id">Firebase Project ID</label>
|
|
</th>
|
|
<td>
|
|
<input type="text"
|
|
id="twp_fcm_project_id"
|
|
name="twp_fcm_project_id"
|
|
value="<?php echo esc_attr($fcm_project_id); ?>"
|
|
class="regular-text"
|
|
placeholder="my-project-12345">
|
|
<p class="description">
|
|
Found in Firebase Console > Project Settings > General > Project ID
|
|
</p>
|
|
</td>
|
|
</tr>
|
|
<tr>
|
|
<th scope="row">
|
|
<label for="twp_fcm_service_account_json">Service Account JSON</label>
|
|
</th>
|
|
<td>
|
|
<textarea id="twp_fcm_service_account_json"
|
|
name="twp_fcm_service_account_json"
|
|
rows="6"
|
|
class="large-text code"
|
|
placeholder='Paste the entire contents of your service account JSON file...'><?php echo esc_textarea($fcm_service_account_json); ?></textarea>
|
|
<p class="description">
|
|
Generate in Firebase Console > Project Settings > Service Accounts > Generate New Private Key.
|
|
Paste the entire JSON file contents here. Must contain <code>client_email</code> and <code>private_key</code> fields.
|
|
</p>
|
|
<?php if ($fcm_sa_configured): ?>
|
|
<p style="color: #00a32a; margin-top: 5px;">✓ Service account configured</p>
|
|
<?php endif; ?>
|
|
</td>
|
|
</tr>
|
|
</table>
|
|
|
|
<?php if ($fcm_sa_configured): ?>
|
|
<p>
|
|
<button type="submit" name="twp_test_notification" class="button">
|
|
Send Test Notification
|
|
</button>
|
|
<span class="description">Send a test notification to your devices</span>
|
|
</p>
|
|
<?php endif; ?>
|
|
</div>
|
|
|
|
<!-- API Documentation -->
|
|
<div class="card" style="max-width: 100%; margin-bottom: 20px;">
|
|
<h2>API Endpoints</h2>
|
|
<p>Available REST API endpoints for mobile app development:</p>
|
|
|
|
<table class="widefat striped">
|
|
<thead>
|
|
<tr>
|
|
<th>Endpoint</th>
|
|
<th>Method</th>
|
|
<th>Description</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<tr>
|
|
<td><code>/twilio-mobile/v1/auth/login</code></td>
|
|
<td>POST</td>
|
|
<td>Authenticate and get JWT tokens</td>
|
|
</tr>
|
|
<tr>
|
|
<td><code>/twilio-mobile/v1/auth/refresh</code></td>
|
|
<td>POST</td>
|
|
<td>Refresh access token</td>
|
|
</tr>
|
|
<tr>
|
|
<td><code>/twilio-mobile/v1/agent/status</code></td>
|
|
<td>GET/POST</td>
|
|
<td>Get or update agent status</td>
|
|
</tr>
|
|
<tr>
|
|
<td><code>/twilio-mobile/v1/queues/state</code></td>
|
|
<td>GET</td>
|
|
<td>Get all queue states</td>
|
|
</tr>
|
|
<tr>
|
|
<td><code>/twilio-mobile/v1/calls/{call_sid}/accept</code></td>
|
|
<td>POST</td>
|
|
<td>Accept a queued call</td>
|
|
</tr>
|
|
<tr>
|
|
<td><code>/twilio-mobile/v1/stream/events</code></td>
|
|
<td>GET</td>
|
|
<td>Server-Sent Events stream for real-time updates</td>
|
|
</tr>
|
|
<tr>
|
|
<td><code>/twilio-mobile/v1/voice/token</code></td>
|
|
<td>GET</td>
|
|
<td>Get Twilio Voice access token for VoIP</td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
|
|
<p style="margin-top: 15px;">
|
|
<strong>Authentication:</strong> All endpoints (except login/refresh) require
|
|
<code>Authorization: Bearer <access_token></code> header.
|
|
</p>
|
|
</div>
|
|
|
|
<p class="submit">
|
|
<button type="submit" name="twp_save_mobile_settings" class="button button-primary">
|
|
Save Settings
|
|
</button>
|
|
</p>
|
|
</form>
|
|
|
|
<!-- Active Sessions -->
|
|
<?php if ($active_sessions > 0): ?>
|
|
<div class="card" style="max-width: 100%; margin-bottom: 20px;">
|
|
<h2>Active Mobile Sessions</h2>
|
|
<?php
|
|
$sessions = $wpdb->get_results("
|
|
SELECT s.user_id, s.device_info, s.logged_in_at, s.last_used, u.user_login, u.display_name
|
|
FROM $sessions_table s
|
|
JOIN {$wpdb->users} u ON s.user_id = u.ID
|
|
WHERE s.is_active = 1 AND s.expires_at > NOW()
|
|
ORDER BY s.last_used DESC
|
|
LIMIT 20
|
|
");
|
|
?>
|
|
<table class="widefat striped">
|
|
<thead>
|
|
<tr>
|
|
<th>User</th>
|
|
<th>Device</th>
|
|
<th>Last Activity</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php foreach ($sessions as $session): ?>
|
|
<tr>
|
|
<td><?php echo esc_html($session->display_name ?: $session->user_login); ?></td>
|
|
<td><?php echo esc_html($session->device_info ?: 'Unknown device'); ?></td>
|
|
<td><?php echo esc_html(human_time_diff(strtotime($session->last_used), current_time('timestamp')) . ' ago'); ?></td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
<?php endif; ?>
|
|
</div>
|
|
</div>
|
|
|
|
<style>
|
|
.twp-mobile-settings .card {
|
|
padding: 20px;
|
|
background: #fff;
|
|
border: 1px solid #ccd0d4;
|
|
box-shadow: 0 1px 1px rgba(0,0,0,.04);
|
|
}
|
|
|
|
.twp-mobile-settings .card h2 {
|
|
margin-top: 0;
|
|
padding-bottom: 10px;
|
|
border-bottom: 1px solid #f0f0f1;
|
|
}
|
|
|
|
.twp-mobile-settings code {
|
|
background: #f0f0f1;
|
|
padding: 2px 6px;
|
|
border-radius: 3px;
|
|
font-size: 13px;
|
|
}
|
|
|
|
.twp-mobile-settings table.widefat td code {
|
|
background: #f6f7f7;
|
|
}
|
|
</style>
|