-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathclass-devtb-webhook.php
More file actions
218 lines (188 loc) · 5 KB
/
Copy pathclass-devtb-webhook.php
File metadata and controls
218 lines (188 loc) · 5 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
<?php
/**
* DEVTB Webhook Handler
*
* Manages webhook notifications for job completions and events
*
* @package DevelopmentTranslation_Bridge
* @subpackage API
* @version 5.1.0
*/
class DEVTB_Webhook {
/**
* Logger instance
*
* @var DEVTB_Logger
*/
private $logger;
/**
* Max retry attempts
*
* @var int
*/
private $max_retries = 3;
/**
* Constructor
*/
public function __construct() {
$this->logger = new DEVTB_Logger();
}
/**
* Send webhook notification
*
* @param string $event Event type.
* @param array $payload Payload data.
* @return bool Success.
*/
public function send( string $event, array $payload ): bool {
$webhook_url = get_option( 'devtb_webhook_url' );
if ( empty( $webhook_url ) ) {
return false;
}
// Add event metadata
$full_payload = array_merge([
'event' => $event,
'timestamp' => current_time( 'mysql' ),
'site_url' => get_site_url(),
], $payload );
// Send webhook
$response = $this->send_request( $webhook_url, $full_payload, $event );
if ( is_wp_error( $response ) ) {
$this->logger->error( 'Webhook failed', [
'event' => $event,
'url' => $webhook_url,
'error' => $response->get_error_message(),
]);
// Retry failed webhooks
$this->schedule_retry( $webhook_url, $event, $full_payload );
return false;
}
$this->logger->info( 'Webhook sent successfully', [
'event' => $event,
'url' => $webhook_url,
'status' => wp_remote_retrieve_response_code( $response ),
]);
return true;
}
/**
* Send HTTP request
*
* @param string $url Webhook URL.
* @param array $payload Payload.
* @param string $event Event type.
* @return array|WP_Error Response or error.
*/
private function send_request( string $url, array $payload, string $event ) {
$args = [
'body' => wp_json_encode( $payload ),
'headers' => [
'Content-Type' => 'application/json',
'X-DEVTB-Event' => $event,
'X-DEVTB-Signature' => $this->generate_signature( $payload ),
],
'timeout' => 10,
'blocking' => true,
'httpversion' => '1.1',
];
return wp_remote_post( $url, $args );
}
/**
* Generate HMAC signature for payload
*
* @param array $payload Payload data.
* @return string Signature.
*/
private function generate_signature( array $payload ): string {
$secret = get_option( 'devtb_webhook_secret' );
if ( empty( $secret ) ) {
$secret = wp_generate_password( 32, false );
update_option( 'devtb_webhook_secret', $secret );
}
$payload_json = wp_json_encode( $payload );
return hash_hmac( 'sha256', $payload_json, $secret );
}
/**
* Schedule retry for failed webhook
*
* @param string $url Webhook URL.
* @param string $event Event type.
* @param array $payload Payload.
*/
private function schedule_retry( string $url, string $event, array $payload ) {
// Get current retry count
$retry_count = $payload['retry_count'] ?? 0;
if ( $retry_count >= $this->max_retries ) {
$this->logger->warning( 'Webhook max retries exceeded', [
'event' => $event,
'url' => $url,
]);
return;
}
// Increment retry count
$payload['retry_count'] = $retry_count + 1;
// Schedule retry with exponential backoff
$delay = pow( 2, $retry_count ) * MINUTE_IN_SECONDS;
wp_schedule_single_event(
time() + $delay,
'devtb_retry_webhook',
[ $url, $event, $payload ]
);
$this->logger->info( 'Webhook retry scheduled', [
'event' => $event,
'retry_count' => $payload['retry_count'],
'delay' => $delay,
]);
}
/**
* Retry webhook (called by cron)
*
* @param string $url Webhook URL.
* @param string $event Event type.
* @param array $payload Payload.
*/
public function retry( string $url, string $event, array $payload ) {
$response = $this->send_request( $url, $payload, $event );
if ( is_wp_error( $response ) ) {
$this->schedule_retry( $url, $event, $payload );
} else {
$this->logger->info( 'Webhook retry successful', [
'event' => $event,
'retry_count' => $payload['retry_count'] ?? 0,
]);
}
}
/**
* Verify webhook signature
*
* @param string $payload_json JSON payload.
* @param string $signature Provided signature.
* @return bool Valid signature.
*/
public static function verify_signature( string $payload_json, string $signature ): bool {
$secret = get_option( 'devtb_webhook_secret' );
if ( empty( $secret ) ) {
return false;
}
$expected_signature = hash_hmac( 'sha256', $payload_json, $secret );
return hash_equals( $expected_signature, $signature );
}
/**
* Get webhook statistics
*
* @return array Statistics.
*/
public function get_stats(): array {
// This would query logs or a webhook history table
return [
'total_sent' => 0,
'successful' => 0,
'failed' => 0,
'pending' => 0,
];
}
}
// Register webhook retry cron action
add_action( 'devtb_retry_webhook', function( $url, $event, $payload ) {
$webhook = new DEVTB_Webhook();
$webhook->retry( $url, $event, $payload );
}, 10, 3 );