-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathWebhookTestEndpoint.php
More file actions
171 lines (152 loc) · 4.45 KB
/
Copy pathWebhookTestEndpoint.php
File metadata and controls
171 lines (152 loc) · 4.45 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
<?php
/**
* REST API endpoint for testing webhooks.
*
* @package WPGraphQL\Webhooks\Rest
*/
declare(strict_types=1);
namespace WPGraphQL\Webhooks\Rest;
use WP_REST_Request;
use WP_REST_Response;
use WP_Error;
use WPGraphQL\Webhooks\Repository\WebhookRepository;
/**
* REST endpoint for testing webhooks.
*/
class WebhookTestEndpoint {
/**
* Repository instance.
*
* @var WebhookRepository
*/
private WebhookRepository $repository;
/**
* Constructor.
*
* @param WebhookRepository $repository Repository instance.
*/
public function __construct( WebhookRepository $repository ) {
$this->repository = $repository;
}
/**
* Register REST routes.
*/
public function register_routes(): void {
register_rest_route(
'graphql-webhooks/v1',
'/test',
[
'methods' => 'POST',
'callback' => [ $this, 'test_webhook' ],
'permission_callback' => [ $this, 'permission_callback' ],
'args' => [
'webhook_id' => [
'required' => true,
'type' => 'integer',
'sanitize_callback' => 'absint',
],
],
]
);
}
/**
* Permission callback.
*
* @return bool Whether user has permission.
*/
public function permission_callback(): bool {
return current_user_can( 'manage_options' );
}
/**
* Test a webhook.
*
* @param WP_REST_Request $request Request object.
* @return WP_REST_Response|WP_Error Response.
*/
public function test_webhook( WP_REST_Request $request ): WP_REST_Response|WP_Error {
$webhook_id = $request->get_param( 'webhook_id' );
$webhook = $this->repository->get( $webhook_id );
if ( ! $webhook ) {
return new WP_Error(
'webhook_not_found',
__( 'Webhook not found.', 'wp-graphql-webhooks' ),
[ 'status' => 404 ]
);
}
// Log test initiation
$test_timestamp = current_time( 'mysql' );
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
error_log( "\n========== WEBHOOK TEST INITIATED ==========" );
error_log( "Timestamp: {$test_timestamp}" );
error_log( "Webhook ID: {$webhook_id}" );
error_log( "Webhook Name: {$webhook->name}" );
// Do not log sensitive URL and headers
error_log( "HTTP Method: {$webhook->method}" );
error_log( "Event: {$webhook->event}" );
error_log( "==========================================\n" );
}
// Create test payload
$test_payload = [
'event' => 'test',
'timestamp' => $test_timestamp,
'webhook' => [
'id' => $webhook->id,
'name' => $webhook->name,
'event' => $webhook->event,
],
'test_data' => [
'message' => 'This is a test webhook payload',
'triggered_by' => 'admin',
'site_url' => get_site_url(),
],
];
// Allow filtering of test payload
$test_payload = apply_filters( 'graphql_webhooks_test_payload', $test_payload, $webhook );
// Trigger test event with enhanced logging
$start_time = microtime( true );
try {
do_action( 'graphql_webhooks_test_event', $webhook, $test_payload );
$end_time = microtime( true );
$duration = round( ( $end_time - $start_time ) * 1000, 2 ); // Convert to milliseconds
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
error_log( "\n========== WEBHOOK TEST COMPLETED ==========" );
error_log( "✅ SUCCESS: Test webhook dispatched" );
error_log( "Duration: {$duration}ms" );
error_log( "Completed at: " . current_time( 'mysql' ) );
error_log( "==========================================\n" );
}
return new WP_REST_Response(
[
'success' => true,
'message' => __( 'Test webhook dispatched successfully.', 'wp-graphql-webhooks' ),
'details' => [
'webhook_id' => $webhook_id,
'webhook_name' => $webhook->name,
'target_url' => $webhook->url,
'method' => $webhook->method,
'duration_ms' => $duration,
'timestamp' => $test_timestamp,
'payload_size' => strlen( wp_json_encode( $test_payload ) ) . ' bytes',
],
],
200
);
} catch ( \Exception $e ) {
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
error_log( "\n========== WEBHOOK TEST ERROR ==========" );
error_log( "❌ ERROR: " . $e->getMessage() );
// Do not log stack trace as it may contain sensitive information
error_log( "========================================\n" );
}
return new WP_Error(
'webhook_test_failed',
sprintf(
/* translators: %s: error message */
__( 'Failed to dispatch test webhook: %s', 'wp-graphql-webhooks' ),
$e->getMessage()
),
[ 'status' => 500 ]
);
}
}
}