-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug.php
More file actions
234 lines (198 loc) · 9.39 KB
/
debug.php
File metadata and controls
234 lines (198 loc) · 9.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
<!DOCTYPE html>
<html>
<head>
<title>TLS Web Push Debug</title>
<style>
body { font-family: monospace; padding: 20px; background: #f0f0f0; }
.section { background: white; padding: 20px; margin: 10px 0; border-radius: 8px; }
.success { color: green; }
.error { color: red; }
pre { background: #f5f5f5; padding: 10px; overflow-x: auto; }
button { padding: 10px 20px; margin: 5px; cursor: pointer; }
</style>
</head>
<body>
<h1>TLS Web Push - Debug Console</h1>
<div class="section">
<h2>1. Database Tables</h2>
<div id="db-status">Checking...</div>
</div>
<div class="section">
<h2>2. Service Worker Status</h2>
<div id="sw-status">Checking...</div>
</div>
<div class="section">
<h2>3. VAPID Keys</h2>
<div id="vapid-status">Checking...</div>
</div>
<div class="section">
<h2>4. Test Subscription</h2>
<button onclick="testSubscription()">Test Subscribe</button>
<div id="subscription-result"></div>
</div>
<div class="section">
<h2>5. Current Subscriptions</h2>
<button onclick="checkSubscriptions()">Check Subscriptions</button>
<div id="subscriptions-list"></div>
</div>
<div class="section">
<h2>6. Console Logs</h2>
<pre id="console-logs"></pre>
</div>
<script>
const consoleLog = document.getElementById('console-logs');
function log(message) {
console.log(message);
consoleLog.textContent += message + '\n';
}
// Check database tables
fetch('<?php echo admin_url('admin-ajax.php'); ?>?action=tls_debug_check_tables')
.then(r => r.json())
.then(data => {
const div = document.getElementById('db-status');
if (data.success) {
div.innerHTML = '<span class="success">✓ Tables exist</span><pre>' + JSON.stringify(data.data, null, 2) + '</pre>';
} else {
div.innerHTML = '<span class="error">✗ ' + data.data + '</span>';
}
})
.catch(e => {
document.getElementById('db-status').innerHTML = '<span class="error">Error: ' + e.message + '</span>';
});
// Check Service Worker
if ('serviceWorker' in navigator) {
navigator.serviceWorker.getRegistration().then(reg => {
const div = document.getElementById('sw-status');
if (reg) {
div.innerHTML = '<span class="success">✓ Service Worker registered</span><pre>Scope: ' + reg.scope + '</pre>';
log('Service Worker: Registered at ' + reg.scope);
} else {
div.innerHTML = '<span class="error">✗ No service worker registered</span>';
log('Service Worker: Not registered');
}
});
} else {
document.getElementById('sw-status').innerHTML = '<span class="error">✗ Service Worker not supported</span>';
}
// Check VAPID key
const vapidKey = '<?php echo get_option('tls_web_push_vapid_public_key', ''); ?>';
const vapidDiv = document.getElementById('vapid-status');
if (vapidKey) {
vapidDiv.innerHTML = '<span class="success">✓ VAPID key configured</span><pre>' + vapidKey.substring(0, 20) + '...</pre>';
log('VAPID Key: Configured (' + vapidKey.length + ' chars)');
} else {
vapidDiv.innerHTML = '<span class="error">✗ No VAPID key set</span>';
log('VAPID Key: Not configured');
}
async function testSubscription() {
const resultDiv = document.getElementById('subscription-result');
resultDiv.innerHTML = 'Testing...';
log('Starting subscription test...');
try {
// Check notification permission
log('Current permission: ' + Notification.permission);
if (Notification.permission === 'denied') {
resultDiv.innerHTML = '<span class="error">✗ Notifications blocked. Reset in browser settings.</span>';
return;
}
// Request permission
const permission = await Notification.requestPermission();
log('Permission result: ' + permission);
if (permission !== 'granted') {
resultDiv.innerHTML = '<span class="error">✗ Permission denied</span>';
return;
}
// Register service worker
const registration = await navigator.serviceWorker.register('<?php echo plugins_url('service-worker.js', dirname(__FILE__)); ?>');
log('Service Worker registered: ' + registration.scope);
// Wait for service worker to be ready
await navigator.serviceWorker.ready;
log('Service Worker ready');
// Subscribe to push
const subscription = await registration.pushManager.subscribe({
userVisibleOnly: true,
applicationServerKey: urlB64ToUint8Array(vapidKey)
});
log('Subscription created: ' + subscription.endpoint.substring(0, 50) + '...');
// Send to server
const formData = new FormData();
formData.append('action', 'tls_subscribe_push');
formData.append('nonce', '<?php echo wp_create_nonce('tls_web_push_nonce'); ?>');
formData.append('subscription', JSON.stringify(subscription.toJSON()));
const response = await fetch('<?php echo admin_url('admin-ajax.php'); ?>', {
method: 'POST',
body: formData
});
const result = await response.json();
log('Server response: ' + JSON.stringify(result));
if (result.success) {
resultDiv.innerHTML = '<span class="success">✓ Subscription successful!</span><pre>' + JSON.stringify(result, null, 2) + '</pre>';
} else {
resultDiv.innerHTML = '<span class="error">✗ Server error: ' + result.data + '</span>';
}
} catch (error) {
log('Error: ' + error.message);
resultDiv.innerHTML = '<span class="error">✗ Error: ' + error.message + '</span><pre>' + error.stack + '</pre>';
}
}
function checkSubscriptions() {
const div = document.getElementById('subscriptions-list');
div.innerHTML = 'Loading...';
fetch('<?php echo admin_url('admin-ajax.php'); ?>?action=tls_debug_list_subscriptions')
.then(r => r.json())
.then(data => {
if (data.success) {
div.innerHTML = '<pre>' + JSON.stringify(data.data, null, 2) + '</pre>';
log('Subscriptions found: ' + data.data.count);
} else {
div.innerHTML = '<span class="error">' + data.data + '</span>';
}
});
}
function urlB64ToUint8Array(base64String) {
const padding = '='.repeat((4 - base64String.length % 4) % 4);
const base64 = (base64String + padding).replace(/\-/g, '+').replace(/_/g, '/');
const rawData = window.atob(base64);
const outputArray = new Uint8Array(rawData.length);
for (let i = 0; i < rawData.length; ++i) {
outputArray[i] = rawData.charCodeAt(i);
}
return outputArray;
}
</script>
</body>
</html>
<?php
// Add debug AJAX endpoints
add_action('wp_ajax_tls_debug_check_tables', 'tls_debug_check_tables');
add_action('wp_ajax_nopriv_tls_debug_check_tables', 'tls_debug_check_tables');
function tls_debug_check_tables() {
global $wpdb;
$tables = array(
'subscriptions' => $wpdb->prefix . 'tls_push_subscriptions',
'notifications' => $wpdb->prefix . 'tls_push_notifications',
'analytics' => $wpdb->prefix . 'tls_push_analytics'
);
$status = array();
foreach ($tables as $name => $table) {
$exists = $wpdb->get_var("SHOW TABLES LIKE '$table'");
$count = $exists ? $wpdb->get_var("SELECT COUNT(*) FROM $table") : 0;
$status[$name] = array(
'table' => $table,
'exists' => (bool)$exists,
'count' => (int)$count
);
}
wp_send_json_success($status);
}
add_action('wp_ajax_tls_debug_list_subscriptions', 'tls_debug_list_subscriptions');
add_action('wp_ajax_nopriv_tls_debug_list_subscriptions', 'tls_debug_list_subscriptions');
function tls_debug_list_subscriptions() {
global $wpdb;
$table = $wpdb->prefix . 'tls_push_subscriptions';
$subscriptions = $wpdb->get_results("SELECT id, endpoint, created_at FROM $table ORDER BY created_at DESC LIMIT 10");
wp_send_json_success(array(
'count' => count($subscriptions),
'subscriptions' => $subscriptions
));
}