-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-send.php
More file actions
96 lines (77 loc) · 3.36 KB
/
Copy pathtest-send.php
File metadata and controls
96 lines (77 loc) · 3.36 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
<?php
/**
* Test script to debug notification sending
* Visit: https://cod.local/wp-content/plugins/tls-web-push/test-send.php
*/
// Load WordPress
require_once(__DIR__ . '/../../../wp-load.php');
// Load Composer autoloader
require_once('vendor/autoload.php');
echo "<h1>Web Push Notification Test</h1>";
// Get VAPID keys
$vapid_public = get_option('tls_web_push_vapid_public_key', '');
$vapid_private = get_option('tls_web_push_vapid_private_key', '');
echo "<h2>VAPID Keys:</h2>";
echo "<p>Public: " . (!empty($vapid_public) ? substr($vapid_public, 0, 20) . "..." : "NOT SET") . "</p>";
echo "<p>Private: " . (!empty($vapid_private) ? substr($vapid_private, 0, 20) . "..." : "NOT SET") . "</p>";
if (empty($vapid_public) || empty($vapid_private)) {
die("<p style='color: red;'><strong>ERROR:</strong> VAPID keys not configured. Go to Web Push → Settings and generate keys.</p>");
}
// Get subscriptions
global $wpdb;
$table_name = $wpdb->prefix . 'tls_push_subscriptions';
$subscriptions = $wpdb->get_results("SELECT * FROM $table_name LIMIT 1");
echo "<h2>Subscriptions:</h2>";
echo "<p>Total: " . count($subscriptions) . "</p>";
if (empty($subscriptions)) {
die("<p style='color: red;'><strong>ERROR:</strong> No subscriptions found. Subscribe to notifications first.</p>");
}
$subscription = $subscriptions[0];
echo "<p>Endpoint: " . substr($subscription->endpoint, 0, 50) . "...</p>";
echo "<p>Auth: " . (!empty($subscription->auth_key) ? "✓" : "✗") . "</p>";
echo "<p>P256dh: " . (!empty($subscription->p256dh_key) ? "✓" : "✗") . "</p>";
// Try sending a notification
echo "<h2>Sending Test Notification...</h2>";
try {
$auth_data = array(
'VAPID' => array(
'subject' => get_bloginfo('url'),
'publicKey' => $vapid_public,
'privateKey' => $vapid_private
)
);
echo "<p>Creating WebPush instance...</p>";
$webPush = new \Minishlink\WebPush\WebPush($auth_data);
echo "<p>Creating subscription object...</p>";
$sub = \Minishlink\WebPush\Subscription::create(array(
'endpoint' => $subscription->endpoint,
'keys' => array(
'p256dh' => $subscription->p256dh_key,
'auth' => $subscription->auth_key
)
));
$payload = json_encode(array(
'notification_id' => 999,
'title' => 'Direct Test Notification',
'body' => 'This is a direct test from test-send.php!',
'icon' => '',
'url' => home_url(),
'timestamp' => time()
));
echo "<p>Payload: " . htmlspecialchars($payload) . "</p>";
echo "<p>Sending notification...</p>";
$report = $webPush->sendOneNotification($sub, $payload);
echo "<h3>Result:</h3>";
if ($report->isSuccess()) {
echo "<p style='color: green;'><strong>✓ SUCCESS!</strong> Notification sent successfully!</p>";
echo "<p>Check your browser for the notification.</p>";
} else {
echo "<p style='color: red;'><strong>✗ FAILED!</strong></p>";
echo "<p>Reason: " . $report->getReason() . "</p>";
echo "<p>Request: " . print_r($report->getRequest(), true) . "</p>";
echo "<p>Response: " . print_r($report->getResponse(), true) . "</p>";
}
} catch (Exception $e) {
echo "<p style='color: red;'><strong>ERROR:</strong> " . $e->getMessage() . "</p>";
echo "<pre>" . $e->getTraceAsString() . "</pre>";
}